Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
h1analysis.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook -header -nodraw
4 /// Example of analysis class for the H1 data.
5 ///
6 /// This file uses 4 large data sets from the H1 collaboration at DESY Hamburg.
7 /// One can access these data sets (277 MBytes) from the standard Root web site
8 /// at: `ftp:/// root.cern.ch/root/h1analysis`
9 /// The Physics plots below generated by this example cannot be produced when
10 /// using smaller data sets.
11 ///
12 /// There are several ways to analyze data stored in a Root Tree
13 /// - Using TTree::Draw: This is very convenient and efficient for small tasks.
14 /// A TTree::Draw call produces one histogram at the time. The histogram
15 /// is automatically generated. The selection expression may be specified
16 /// in the command line.
17 ///
18 /// - Using the TTreeViewer: This is a graphical interface to TTree::Draw
19 /// with the same functionality.
20 ///
21 /// - Using the code generated by TTree::MakeClass: In this case, the user
22 /// creates an instance of the analysis class. They have the control over
23 /// the event loop and he can generate an unlimited number of histograms.
24 ///
25 /// - Using the code generated by TTree::MakeSelector. Like for the code
26 /// generated by TTree::MakeClass, the user can do complex analysis.
27 /// However, they cannot control the event loop. The event loop is controlled
28 /// by TTree::Process called by the user. This solution is illustrated
29 /// by the current code. The advantage of this method is that it can be run
30 /// in a parallel environment using PROOF (the Parallel Root Facility).
31 ///
32 /// A chain of 4 files (originally converted from PAW ntuples) is used
33 /// to illustrate the various ways to loop on Root data sets.
34 /// Each data set contains a Root Tree named "h42"
35 /// The class definition in h1analysis.h has been generated automatically
36 /// by the Root utility TTree::MakeSelector using one of the files with the
37 /// following statement:
38 ///
39 /// ~~~{.cpp}
40 /// h42->MakeSelector("h1analysis");
41 /// ~~~
42 ///
43 /// This produces two files: h1analysis.h and h1analysis.C (skeleton of this file)
44 /// The h1analysis class is derived from the Root class TSelector.
45 ///
46 /// The following members functions are called by the TTree::Process functions.
47 /// - **Begin()**: Called every time a loop on the tree starts.
48 /// A convenient place to create your histograms.
49 /// - **Notify()**: This function is called at the first entry of a new Tree
50 /// in a chain.
51 /// - **Process()**: Called to analyze each entry.
52 ///
53 /// - **Terminate()**: Called at the end of a loop on a TTree.
54 /// A convenient place to draw/fit your histograms.
55 ///
56 /// To use this file, try the following sessions
57 ///
58 /// ~~~{.cpp}
59 /// Root > gROOT->Time(); /// will show RT & CPU time per command
60 /// ~~~
61 ///
62 /// ### Case A: Create a TChain with the 4 H1 data files
63 ///
64 /// The chain can be created by executed the short macro h1chain.C below:
65 ///
66 /// ~~~{.cpp}
67 /// {
68 /// TChain chain("h42");
69 /// chain.Add("$H1/dstarmb.root"); /// 21330730 bytes 21920 events
70 /// chain.Add("$H1/dstarp1a.root"); /// 71464503 bytes 73243 events
71 /// chain.Add("$H1/dstarp1b.root"); /// 83827959 bytes 85597 events
72 /// chain.Add("$H1/dstarp2.root"); /// 100675234 bytes 103053 events
73 /// /// where $H1 is a system symbol pointing to the H1 data directory.
74 /// }
75 /// ~~~
76 ///
77 /// ### Case B: Loop on all events
78 ///
79 /// ~~~{.cpp}
80 /// Root > chain.Process("h1analysis.C")
81 /// ~~~
82 ///
83 /// ### Case C: Same as B, but in addition fill the entry list with selected entries.
84 ///
85 /// The entry list is saved to a file "elist.root" by the Terminate function.
86 /// To see the list of selected events, you can do `elist->Print("all")`.
87 /// The selection function has selected 7525 events out of the 283813 events
88 /// in the chain of files. (2.65 per cent)
89 ///
90 /// ~~~{.cpp}
91 /// Root > chain.Process("h1analysis.C","fillList")
92 /// ~~~
93 ///
94 /// ### Case D: Process only entries in the entry list
95 ///
96 /// The entry list is read from the file in elist.root generated by step C
97 ///
98 /// ~~~{.cpp}
99 /// Root > chain.Process("h1analysis.C","useList")
100 /// ~~~
101 ///
102 /// ### Case E: The above steps have been executed via the interpreter.
103 /// You can repeat the steps B, C and D using the script compiler
104 /// by replacing "h1analysis.C" by "h1analysis.C+" or "h1analysis.C++"
105 /// in a new session (see F).
106 ///
107 /// ### Case F: Create the chain as in A, then execute
108 ///
109 /// ~~~{.cpp}
110 /// Root > chain.Process("h1analysis.C+","useList")
111 /// ~~~
112 ///
113 /// The same analysis can be run on PROOF. For a quick try start a PROOF-Lite
114 /// session
115 ///
116 /// ~~~{.cpp}
117 /// Root > TProof *p = TProof::Open("")
118 /// ~~~
119 ///
120 /// create (if not already done) the chain by executing the 'h1chain.C' macro
121 /// mentioned above, and then tell ROOT to use PROOF to process the chain:
122 ///
123 /// ~~~{.cpp}
124 /// Root > chain.SetProof()
125 /// ~~~
126 ///
127 /// You can then repeat step B above. Step C can also be executed in PROOF. However,
128 /// step D cannot be executed in PROOF as in the local session (i.e. just passing
129 /// option 'useList'): to use the entry list you have to
130 ///
131 /// ### Case G: Load first in the session the list form the file
132 ///
133 /// ~~~{.cpp}
134 /// Root > TFile f("elist.root")
135 /// Root > TEntryList *elist = (TEntryList *) f.Get("elist")
136 /// ~~~
137 ///
138 /// set it on the chain:
139 ///
140 /// ~~~{.cpp}
141 /// Root > chain.SetEntryList(elist)
142 /// ~~~
143 ///
144 /// call Process as in step B. Of course this works also for local processing.
145 ///
146 /// \macro_code
147 ///
148 /// \author Rene Brun
149 
150 #include "h1analysis.h"
151 #include "TH2.h"
152 #include "TF1.h"
153 #include "TStyle.h"
154 #include "TCanvas.h"
155 #include "TPaveStats.h"
156 #include "TLine.h"
157 #include "TMath.h"
158 
159 const Double_t dxbin = (0.17-0.13)/40; // Bin-width
160 const Double_t sigma = 0.0012;
161 
162 
163 Double_t fdm5(Double_t *xx, Double_t *par)
164 {
165  Double_t x = xx[0];
166  if (x <= 0.13957) return 0;
167  Double_t xp3 = (x-par[3])*(x-par[3]);
168  Double_t res = dxbin*(par[0]*TMath::Power(x-0.13957, par[1])
169  + par[2] / 2.5066/par[4]*TMath::Exp(-xp3/2/par[4]/par[4]));
170  return res;
171 }
172 
173 
174 Double_t fdm2(Double_t *xx, Double_t *par)
175 {
176  Double_t x = xx[0];
177  if (x <= 0.13957) return 0;
178  Double_t xp3 = (x-0.1454)*(x-0.1454);
179  Double_t res = dxbin*(par[0]*TMath::Power(x-0.13957, 0.25)
180  + par[1] / 2.5066/sigma*TMath::Exp(-xp3/2/sigma/sigma));
181  return res;
182 }
183 
184 
185 void h1analysis::Begin(TTree * /*tree*/)
186 {
187 // function called before starting the event loop
188 // -it performs some cleanup
189 // -it creates histograms
190 // -it sets some initialisation for the entry list
191 
192  // This is needed when re-processing the object
193  Reset();
194 
195  //print the option specified in the Process function.
196  TString option = GetOption();
197  Info("Begin", "starting h1analysis with process option: %s", option.Data());
198 
199  //process cases with entry list
200  if (fChain) fChain->SetEntryList(0);
201  delete gDirectory->GetList()->FindObject("elist");
202 
203  // case when one creates/fills the entry list
204  if (option.Contains("fillList")) {
205  fillList = kTRUE;
206  elist = new TEntryList("elist", "H1 selection from Cut");
207  // Add to the input list for processing in PROOF, if needed
208  if (fInput) {
209  fInput->Add(new TNamed("fillList",""));
210  // We send a clone to avoid double deletes when importing the result
211  fInput->Add(elist);
212  // This is needed to avoid warnings from output-to-members mapping
213  elist = 0;
214  }
215  Info("Begin", "creating an entry-list");
216  }
217  // case when one uses the entry list generated in a previous call
218  if (option.Contains("useList")) {
219  useList = kTRUE;
220  if (fInput) {
221  // In PROOF option "useList" is processed in SlaveBegin and we do not need
222  // to do anything here
223  } else {
224  TFile f("elist.root");
225  elist = (TEntryList*)f.Get("elist");
226  if (elist) elist->SetDirectory(0); //otherwise the file destructor will delete elist
227  }
228  }
229 }
230 
231 
232 void h1analysis::SlaveBegin(TTree *tree)
233 {
234 // function called before starting the event loop
235 // -it performs some cleanup
236 // -it creates histograms
237 // -it sets some initialisation for the entry list
238 
239  //initialize the Tree branch addresses
240  Init(tree);
241 
242  //print the option specified in the Process function.
243  TString option = GetOption();
244  Info("SlaveBegin",
245  "starting h1analysis with process option: %s (tree: %p)", option.Data(), tree);
246 
247  //create histograms
248  hdmd = new TH1F("hdmd","dm_d",40,0.13,0.17);
249  h2 = new TH2F("h2","ptD0 vs dm_d",30,0.135,0.165,30,-3,6);
250 
251  fOutput->Add(hdmd);
252  fOutput->Add(h2);
253 
254  // Entry list stuff (re-parse option because on PROOF only SlaveBegin is called)
255  if (option.Contains("fillList")) {
256  fillList = kTRUE;
257  // Get the list
258  if (fInput) {
259  if ((elist = (TEntryList *) fInput->FindObject("elist")))
260  // Need to clone to avoid problems when destroying the selector
261  elist = (TEntryList *) elist->Clone();
262  if (elist)
263  fOutput->Add(elist);
264  else
265  fillList = kFALSE;
266  }
267  }
268  if (fillList) Info("SlaveBegin", "creating an entry-list");
269  if (option.Contains("useList")) useList = kTRUE;
270 }
271 
272 
273 Bool_t h1analysis::Process(Long64_t entry)
274 {
275 // entry is the entry number in the current Tree
276 // Selection function to select D* and D0.
277 
278  fProcessed++;
279  //in case one entry list is given in input, the selection has already been done.
280  if (!useList) {
281  // Read only the necessary branches to select entries.
282  // return as soon as a bad entry is detected
283  // to read complete event, call fChain->GetTree()->GetEntry(entry)
284  b_md0_d->GetEntry(entry); if (TMath::Abs(md0_d-1.8646) >= 0.04) return kFALSE;
285  b_ptds_d->GetEntry(entry); if (ptds_d <= 2.5) return kFALSE;
286  b_etads_d->GetEntry(entry); if (TMath::Abs(etads_d) >= 1.5) return kFALSE;
287  b_ik->GetEntry(entry); ik--; //original ik used f77 convention starting at 1
288  b_ipi->GetEntry(entry); ipi--;
289  b_ntracks->GetEntry(entry);
290  b_nhitrp->GetEntry(entry);
291  if (nhitrp[ik]*nhitrp[ipi] <= 1) return kFALSE;
292  b_rend->GetEntry(entry);
293  b_rstart->GetEntry(entry);
294  if (rend[ik] -rstart[ik] <= 22) return kFALSE;
295  if (rend[ipi]-rstart[ipi] <= 22) return kFALSE;
296  b_nlhk->GetEntry(entry); if (nlhk[ik] <= 0.1) return kFALSE;
297  b_nlhpi->GetEntry(entry); if (nlhpi[ipi] <= 0.1) return kFALSE;
298  b_ipis->GetEntry(entry); ipis--; if (nlhpi[ipis] <= 0.1) return kFALSE;
299  b_njets->GetEntry(entry); if (njets < 1) return kFALSE;
300  }
301  // if option fillList, fill the entry list
302  if (fillList) elist->Enter(entry);
303 
304  // to read complete event, call fChain->GetTree()->GetEntry(entry)
305  // read branches not processed in ProcessCut
306  b_dm_d->GetEntry(entry); //read branch holding dm_d
307  b_rpd0_t->GetEntry(entry); //read branch holding rpd0_t
308  b_ptd0_d->GetEntry(entry); //read branch holding ptd0_d
309 
310  //fill some histograms
311  hdmd->Fill(dm_d);
312  h2->Fill(dm_d,rpd0_t/0.029979*1.8646/ptd0_d);
313 
314  // Count the number of selected events
315  fStatus++;
316 
317  return kTRUE;
318 }
319 
320 
321 
322 void h1analysis::SlaveTerminate()
323 {
324  // nothing to be done
325 }
326 
327 
328 void h1analysis::Terminate()
329 {
330 // function called at the end of the event loop
331 
332  hdmd = dynamic_cast<TH1F*>(fOutput->FindObject("hdmd"));
333  h2 = dynamic_cast<TH2F*>(fOutput->FindObject("h2"));
334 
335  if (hdmd == 0 || h2 == 0) {
336  Error("Terminate", "hdmd = %p , h2 = %p", hdmd, h2);
337  return;
338  }
339 
340  //create the canvas for the h1analysis fit
341  gStyle->SetOptFit();
342  TCanvas *c1 = new TCanvas("c1","h1analysis analysis",10,10,800,600);
343  c1->SetBottomMargin(0.15);
344  hdmd->GetXaxis()->SetTitle("m_{K#pi#pi} - m_{K#pi}[GeV/c^{2}]");
345  hdmd->GetXaxis()->SetTitleOffset(1.4);
346 
347  //fit histogram hdmd with function f5 using the log-likelihood option
348  if (gROOT->GetListOfFunctions()->FindObject("f5"))
349  delete gROOT->GetFunction("f5");
350  TF1 *f5 = new TF1("f5",fdm5,0.139,0.17,5);
351  f5->SetParameters(1000000, .25, 2000, .1454, .001);
352  hdmd->Fit("f5","lr");
353 
354  //create the canvas for tau d0
355  gStyle->SetOptFit(0);
356  gStyle->SetOptStat(1100);
357  TCanvas *c2 = new TCanvas("c2","tauD0",100,100,800,600);
358  c2->SetGrid();
359  c2->SetBottomMargin(0.15);
360 
361  // Project slices of 2-d histogram h2 along X , then fit each slice
362  // with function f2 and make a histogram for each fit parameter
363  // Note that the generated histograms are added to the list of objects
364  // in the current directory.
365  if (gROOT->GetListOfFunctions()->FindObject("f2"))
366  delete gROOT->GetFunction("f2");
367  TF1 *f2 = new TF1("f2",fdm2,0.139,0.17,2);
368  f2->SetParameters(10000, 10);
369  h2->FitSlicesX(f2,0,-1,1,"qln");
370  TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
371  h2_1->GetXaxis()->SetTitle("#tau[ps]");
372  h2_1->SetMarkerStyle(21);
373  h2_1->Draw();
374  c2->Update();
375  TLine *line = new TLine(0,0,0,c2->GetUymax());
376  line->Draw();
377 
378  // Have the number of entries on the first histogram (to cross check when running
379  // with entry lists)
380  TPaveStats *psdmd = (TPaveStats *)hdmd->GetListOfFunctions()->FindObject("stats");
381  psdmd->SetOptStat(1110);
382  c1->Modified();
383 
384  //save the entry list to a Root file if one was produced
385  if (fillList) {
386  if (!elist)
387  elist = dynamic_cast<TEntryList*>(fOutput->FindObject("elist"));
388  if (elist) {
389  Printf("Entry list 'elist' created:");
390  elist->Print();
391  TFile efile("elist.root","recreate");
392  elist->Write();
393  } else {
394  Error("Terminate", "entry list requested but not found in output");
395  }
396  }
397  // Notify the amount of processed events
398  if (!fInput) Info("Terminate", "processed %lld events", fProcessed);
399 }