00001 // Example of analysis class for the H1 data using code generated by MakeProxy. 00002 // =========================================================================== 00003 // 00004 // This file uses 4 large data sets from the H1 collaboration at DESY Hamburg. 00005 // One can access these data sets (277 MBytes) from the standard Root web site 00006 // at: ftp://root.cern.ch/root/h1analysis/ 00007 // The Physics plots below generated by this example cannot be produced when 00008 // using smaller data sets. 00009 // 00010 // There are several ways to analyze data stored in a Root Tree 00011 // -Using TTree::Draw: This is very convenient and efficient for small tasks. 00012 // A TTree::Draw call produces one histogram at the time. The histogram 00013 // is automatically generated. The selection expression may be specified 00014 // in the command line. 00015 // 00016 // -Using the TTreeViewer: This is a graphical interface to TTree::Draw 00017 // with the same functionality. 00018 // 00019 // -Using the code generated by TTree::MakeClass: In this case, the user 00020 // creates an instance of the analysis class. He has the control over 00021 // the event loop and he can generate an unlimited number of histograms. 00022 // 00023 // -Using the code generated by TTree::MakeSelector. Like for the code 00024 // generated by TTree::MakeClass, the user can do complex analysis. 00025 // However, he cannot control the event loop. The event loop is controlled 00026 // by TTree::Process called by the user. This solution is illustrated 00027 // by the current code. The advantage of this method is that it can be run 00028 // in a parallel environment using PROOF (the Parallel Root Facility). 00029 // 00030 // A chain of 4 files (originally converted from PAW ntuples) is used 00031 // to illustrate the various ways to loop on Root data sets. 00032 // Each data set contains a Root Tree named "h42" 00033 // 00034 // h1analysProxy.C can be used either via TTree::Draw: 00035 // h42->Draw("h1analysisProxy.C"); 00036 // or it can be used directly with TTree::MakeProxy, for example to generate a 00037 // shared library. TTree::MakeProxy will generate a TSelector skeleton that 00038 // include h1analysProxy.C: 00039 // h42->MakeProxy("h1sel","h1analysisProxy.C"); 00040 // This produces one file: h1sel.h which does a #include "h1analysProxy.C" 00041 // The h1sel class is derived from the Root class TSelector and can then 00042 // be used as: 00043 // h42->Process("h1sel.h+"); 00044 // 00045 // The following members functions are called by the TTree::Process functions. 00046 // h1analysProxy_Begin(): called everytime a loop on the tree starts. 00047 // a convenient place to create your histograms. 00048 // h1analysProxy_SlaveBegin(): 00049 // 00050 // h1analysProxy_Notify(): This function is called at the first entry of a new Tree 00051 // in a chain. 00052 // h1analysProxy_Process(): called to analyze each entry. 00053 // 00054 // h1analysProxy_SlaveTerminate(): 00055 // 00056 // h1analysProxy_Terminate(): called at the end of a loop on a TTree. 00057 // a convenient place to draw/fit your histograms. 00058 // 00059 // To use this file, try the following session 00060 // 00061 // Root > gROOT->Time(); //will show RT & CPU time per command 00062 // 00063 //==> A- create a TChain with the 4 H1 data files 00064 // The chain can be created by executed the short macro h1chain.C below: 00065 // { 00066 // TChain chain("h42"); 00067 // chain.Add("$H1/dstarmb.root"); // 21330730 bytes 21920 events 00068 // chain.Add("$H1/dstarp1a.root"); // 71464503 bytes 73243 events 00069 // chain.Add("$H1/dstarp1b.root"); // 83827959 bytes 85597 events 00070 // chain.Add("$H1/dstarp2.root"); // 100675234 bytes 103053 events 00071 // //where $H1 is a system symbol pointing to the H1 data directory. 00072 // } 00073 // 00074 // Root > .x h1chain.C 00075 // 00076 //==> B- loop on all events 00077 // Root > chain.Draw("h1analysisProxy.C") 00078 // 00079 //==> C- same as B, but in addition fill the event list with selected entries. 00080 // The event list is saved to a file "elist.root" by the Terminate function. 00081 // To see the list of selected events, you can do elist->Print("all"). 00082 // The selection function has selected 7525 events out of the 283813 events 00083 // in the chain of files. (2.65 per cent) 00084 // Root > chain.Draw("h1analysisProxy.C","","fillList") 00085 // 00086 //==> D- Process only entries in the event list 00087 // The event list is read from the file in elist.root generated by step C 00088 // Root > chain.Draw("h1analysisProxy.C","","useList") 00089 //// 00090 // The commands executed with the 3 different methods B,C and D 00091 // produce two canvases shown below: 00092 // begin_html <a href="gif/h1analysis_dstar.gif" >the Dstar plot</a> end_html 00093 // begin_html <a href="gif/h1analysis_tau.gif" >the Tau D0 plot</a> end_html 00094 // 00095 //Author; Philippe Canal from original h1analysis.C by Rene Brun 00096 00097 TEntryList *elist; 00098 Bool_t useList, fillList; 00099 TH1F *hdmd; 00100 TH2F *h2; 00101 00102 //_____________________________________________________________________ 00103 void h1analysisProxy_Begin(TTree *tree) 00104 { 00105 // function called before starting the event loop 00106 // -it performs some cleanup 00107 // -it creates histograms 00108 // -it sets some initialisation for the event list 00109 00110 //print the option specified in the Process function. 00111 TString option = GetOption(); 00112 printf("Starting (begin) h1analysis with process option: %s\n",option.Data()); 00113 00114 //process cases with event list 00115 fillList = kFALSE; 00116 useList = kFALSE; 00117 if (fChain) fChain->SetEntryList(0); 00118 delete gDirectory->GetList()->FindObject("elist"); 00119 00120 // case when one creates/fills the event list 00121 if (option.Contains("fillList")) { 00122 fillList = kTRUE; 00123 elist = new TEntryList("elist","H1 selection from Cut"); 00124 // Add to the input list for processing in PROOF, if needed 00125 if (fInput) { 00126 fInput->Add(new TNamed("fillList","")); 00127 fInput->Add(elist); 00128 } 00129 } else elist = 0; 00130 00131 // case when one uses the event list generated in a previous call 00132 if (option.Contains("useList")) { 00133 useList = kTRUE; 00134 if (fInput) { 00135 tree->SetEntryList(elist); 00136 TFile f("elist.root"); 00137 elist = (TEntryList*)f.Get("elist"); 00138 if (elist) elist->SetDirectory(0); //otherwise the file destructor will delete elist 00139 } else { 00140 // Option "useList" not supported in PROOF directly 00141 Warning("Begin", "option 'useList' not supported in PROOF - ignoring"); 00142 Warning("Begin", "the entry list must be set on the chain *before* calling Process"); 00143 } 00144 } 00145 } 00146 00147 //_____________________________________________________________________ 00148 void h1analysisProxy_SlaveBegin(TTree *tree) 00149 { 00150 // function called before starting the event loop 00151 // -it performs some cleanup 00152 // -it creates histograms 00153 // -it sets some initialisation for the entry list 00154 00155 //initialize the Tree branch addresses 00156 Init(tree); 00157 00158 //print the option specified in the Process function. 00159 TString option = GetOption(); 00160 printf("Starting (slave) h1analysis with process option: %s\n",option.Data()); 00161 00162 //create histograms 00163 hdmd = new TH1F("hdmd","dm_d",40,0.13,0.17); 00164 h2 = new TH2F("h2","ptD0 vs dm_d",30,0.135,0.165,30,-3,6); 00165 00166 fOutput->Add(hdmd); 00167 fOutput->Add(h2); 00168 00169 //process cases with entry list 00170 fillList = kFALSE; 00171 useList = kFALSE; 00172 00173 // case when one creates/fills the entry list 00174 if (option.Contains("fillList")) { 00175 fillList = kTRUE; 00176 // Get the list 00177 if (fInput) { 00178 if ((elist = (TEntryList *) fInput->FindObject("elist"))) 00179 // Need to clone to avoid problems when destroying the selector 00180 elist = (TEntryList *) elist->Clone(); 00181 } 00182 if (elist) 00183 fOutput->Add(elist); 00184 else 00185 fillList = kFALSE; 00186 } else elist = 0; 00187 00188 // case when one uses the entry list generated in a previous call 00189 if (option.Contains("useList")) { 00190 useList = kTRUE; 00191 TFile f("elist.root"); 00192 elist = (TEntryList*)f.Get("elist"); 00193 if (elist) elist->SetDirectory(0); //otherwise the file destructor will delete elist 00194 if (tree) tree->SetEntryList(elist); 00195 else { 00196 // Option "useList" not supported in PROOF directly 00197 Warning("Begin", "option 'useList' not supported in PROOF - ignoring"); 00198 Warning("Begin", "the entry list must be set on the chain *before* calling Process"); 00199 } 00200 } 00201 00202 } 00203 00204 Double_t h1analysisProxy() { 00205 return 0; 00206 } 00207 00208 //_____________________________________________________________________ 00209 Bool_t h1analysisProxy_Process(Long64_t entry) 00210 { 00211 // entry is the entry number in the current Tree 00212 // Selection function to select D* and D0. 00213 00214 //in case one entry list is given in input, the selection has already been done. 00215 if (!useList) { 00216 00217 float f1 = md0_d; 00218 float f2 = md0_d-1.8646; 00219 bool test = TMath::Abs(md0_d-1.8646) >= 0.04; 00220 if (gDebug>0) fprintf(stderr,"entry #%lld f1=%f f2=%f test=%d\n", 00221 fChain->GetReadEntry(),f1,f2,test); 00222 00223 if (TMath::Abs(md0_d-1.8646) >= 0.04) return kFALSE; 00224 if (ptds_d <= 2.5) return kFALSE; 00225 if (TMath::Abs(etads_d) >= 1.5) return kFALSE; 00226 00227 int cik = ik-1; //original ik used f77 convention starting at 1 00228 int cipi = ipi-1; //original ipi used f77 convention starting at 1 00229 00230 f1 = nhitrp[cik]; 00231 f2 = nhitrp[cipi]; 00232 test = nhitrp[cik]*nhitrp[cipi] <= 1; 00233 if (gDebug>0) fprintf(stderr,"entry #%lld f1=%f f2=%f test=%d\n", 00234 fChain->GetReadEntry(),f1,f2,test); 00235 00236 if (nhitrp[cik]*nhitrp[cipi] <= 1) return kFALSE; 00237 if (rend[cik] -rstart[cik] <= 22) return kFALSE; 00238 if (rend[cipi]-rstart[cipi] <= 22) return kFALSE; 00239 if (nlhk[cik] <= 0.1) return kFALSE; 00240 if (nlhpi[cipi] <= 0.1) return kFALSE; 00241 // fix because read-only 00242 if (nlhpi[ipis-1] <= 0.1) return kFALSE; 00243 if (njets < 1) return kFALSE; 00244 00245 } 00246 // if option fillList, fill the event list 00247 if (fillList) elist->Enter(entry); 00248 00249 //fill some histograms 00250 hdmd->Fill(dm_d); 00251 h2->Fill(dm_d,rpd0_t/0.029979*1.8646/ptd0_d); 00252 00253 return kTRUE; 00254 } 00255 00256 00257 //_____________________________________________________________________ 00258 void h1analysisProxy_SlaveTerminate() 00259 { 00260 // nothing to be done 00261 printf("Terminate (slave) h1analysis\n"); 00262 } 00263 00264 //_____________________________________________________________________ 00265 void h1analysisProxy_Terminate() 00266 { 00267 printf("Terminate (final) h1analysis\n"); 00268 00269 // function called at the end of the event loop 00270 00271 hdmd = dynamic_cast<TH1F*>(fOutput->FindObject("hdmd")); 00272 h2 = dynamic_cast<TH2F*>(fOutput->FindObject("h2")); 00273 00274 if (hdmd == 0 || h2 == 0) { 00275 Error("Terminate", "hdmd = %p , h2 = %p", hdmd, h2); 00276 return; 00277 } 00278 00279 //create the canvas for the h1analysis fit 00280 gStyle->SetOptFit(); 00281 TCanvas *c1 = new TCanvas("c1","h1analysis analysis",10,10,800,600); 00282 c1->SetBottomMargin(0.15); 00283 hdmd->GetXaxis()->SetTitle("m_{K#pi#pi} - m_{K#pi}[GeV/c^{2}]"); 00284 hdmd->GetXaxis()->SetTitleOffset(1.4); 00285 00286 //fit histogram hdmd with function f5 using the loglikelihood option 00287 TF1 *f5 = new TF1("f5",fdm5,0.139,0.17,5); 00288 f5->SetParameters(1000000, .25, 2000, .1454, .001); 00289 hdmd->Fit("f5","lr"); 00290 00291 //create the canvas for tau d0 00292 gStyle->SetOptFit(0); 00293 gStyle->SetOptStat(1100); 00294 TCanvas *c2 = new TCanvas("c2","tauD0",100,100,800,600); 00295 c2->SetGrid(); 00296 c2->SetBottomMargin(0.15); 00297 00298 // Project slices of 2-d histogram h2 along X , then fit each slice 00299 // with function f2 and make a histogram for each fit parameter 00300 // Note that the generated histograms are added to the list of objects 00301 // in the current directory. 00302 TF1 *f2 = new TF1("f2",fdm2,0.139,0.17,2); 00303 f2->SetParameters(10000, 10); 00304 h2->FitSlicesX(f2,0,-1,1,"qln"); 00305 TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1"); 00306 h2_1->GetXaxis()->SetTitle("#tau[ps]"); 00307 h2_1->SetMarkerStyle(21); 00308 h2_1->Draw(); 00309 c2->Update(); 00310 TLine *line = new TLine(0,0,0,c2->GetUymax()); 00311 line->Draw(); 00312 00313 // Have the number of entries on the first histogram (to cross check when running 00314 // with entry lists) 00315 TPaveStats *psdmd = (TPaveStats *)hdmd->GetListOfFunctions()->FindObject("stats"); 00316 psdmd->SetOptStat(1110); 00317 c1->Modified(); 00318 00319 //save the entry list to a Root file if one was produced 00320 if (fillList) { 00321 elist = dynamic_cast<TEntryList*>(fOutput->FindObject("elist")); 00322 if (elist) { 00323 TFile efile("elist.root","recreate"); 00324 elist->Write(); 00325 } else { 00326 Error("Terminate", "entry list requested but not found in output"); 00327 } 00328 } 00329 }