Logo ROOT   6.10/09
Reference Guide
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 /// ~~~{.cpp}
39 /// h42->MakeSelector("h1analysis");
40 /// ~~~
41 /// This produces two files: h1analysis.h and h1analysis.C (skeleton of this file)
42 /// The h1analysis class is derived from the Root class TSelector.
43 ///
44 /// The following members functions are called by the TTree::Process functions.
45 /// - **Begin()**: Called every time a loop on the tree starts.
46 /// A convenient place to create your histograms.
47 /// - **Notify()**: This function is called at the first entry of a new Tree
48 /// in a chain.
49 /// - **Process()**: Called to analyze each entry.
50 ///
51 /// - **Terminate()**: Called at the end of a loop on a TTree.
52 /// A convenient place to draw/fit your histograms.
53 ///
54 /// To use this file, try the following sessions
55 /// ~~~{.cpp}
56 /// Root > gROOT->Time(); /// will show RT & CPU time per command
57 /// ~~~
58 /// ### Case A: Create a TChain with the 4 H1 data files
59 /// The chain can be created by executed the short macro h1chain.C below:
60 /// ~~~{.cpp}
61 /// {
62 /// TChain chain("h42");
63 /// chain.Add("$H1/dstarmb.root"); /// 21330730 bytes 21920 events
64 /// chain.Add("$H1/dstarp1a.root"); /// 71464503 bytes 73243 events
65 /// chain.Add("$H1/dstarp1b.root"); /// 83827959 bytes 85597 events
66 /// chain.Add("$H1/dstarp2.root"); /// 100675234 bytes 103053 events
67 /// /// where $H1 is a system symbol pointing to the H1 data directory.
68 /// }
69 /// ~~~
70 /// ### Case B: Loop on all events
71 /// ~~~{.cpp}
72 /// Root > chain.Process("h1analysis.C")
73 /// ~~~
74 /// ### Case C: Same as B, but in addition fill the entry list with selected entries.
75 /// The entry list is saved to a file "elist.root" by the Terminate function.
76 /// To see the list of selected events, you can do `elist->Print("all")`.
77 /// The selection function has selected 7525 events out of the 283813 events
78 /// in the chain of files. (2.65 per cent)
79 /// ~~~{.cpp}
80 /// Root > chain.Process("h1analysis.C","fillList")
81 /// ~~~
82 /// ### Case D: Process only entries in the entry list
83 /// The entry list is read from the file in elist.root generated by step C
84 /// ~~~{.cpp}
85 /// Root > chain.Process("h1analysis.C","useList")
86 /// ~~~
87 /// ### Case E: The above steps have been executed via the interpreter.
88 /// You can repeat the steps B, C and D using the script compiler
89 /// by replacing "h1analysis.C" by "h1analysis.C+" or "h1analysis.C++"
90 /// in a new session (see F).
91 ///
92 /// ### Case F: Create the chain as in A, then execute
93 /// ~~~{.cpp}
94 /// Root > chain.Process("h1analysis.C+","useList")
95 /// ~~~
96 ///
97 /// The same analysis can be run on PROOF. For a quick try start a PROOF-Lite
98 /// session
99 /// ~~~{.cpp}
100 /// Root > TProof *p = TProof::Open("")
101 /// ~~~
102 /// create (if not already done) the chain by executing the 'h1chain.C' macro
103 /// mentioned above, and then tell ROOT to use PROOF to process the chain:
104 /// ~~~{.cpp}
105 /// Root > chain.SetProof()
106 /// ~~~
107 /// You can then repeat step B above. Step C can also be executed in PROOF. However,
108 /// step D cannot be executed in PROOF as in the local session (i.e. just passing
109 /// option 'useList'): to use the entry list you have to
110 ///
111 /// ### Case G: Load first in the session the list form the file
112 /// ~~~{.cpp}
113 /// Root > TFile f("elist.root")
114 /// Root > TEntryList *elist = (TEntryList *) f.Get("elist")
115 /// ~~~
116 /// set it on the chain:
117 /// ~~~{.cpp}
118 /// Root > chain.SetEntryList(elist)
119 /// ~~~
120 /// call Process as in step B. Of course this works also for local processing.
121 ///
122 /// \macro_code
123 ///
124 /// \author Rene Brun
125 
126 #include "h1analysis.h"
127 #include "TH2.h"
128 #include "TF1.h"
129 #include "TStyle.h"
130 #include "TCanvas.h"
131 #include "TPaveStats.h"
132 #include "TLine.h"
133 #include "TMath.h"
134 
135 const Double_t dxbin = (0.17-0.13)/40; // Bin-width
136 const Double_t sigma = 0.0012;
137 
138 
140 {
141  Double_t x = xx[0];
142  if (x <= 0.13957) return 0;
143  Double_t xp3 = (x-par[3])*(x-par[3]);
144  Double_t res = dxbin*(par[0]*TMath::Power(x-0.13957, par[1])
145  + par[2] / 2.5066/par[4]*TMath::Exp(-xp3/2/par[4]/par[4]));
146  return res;
147 }
148 
149 
150 Double_t fdm2(Double_t *xx, Double_t *par)
151 {
152  Double_t x = xx[0];
153  if (x <= 0.13957) return 0;
154  Double_t xp3 = (x-0.1454)*(x-0.1454);
155  Double_t res = dxbin*(par[0]*TMath::Power(x-0.13957, 0.25)
156  + par[1] / 2.5066/sigma*TMath::Exp(-xp3/2/sigma/sigma));
157  return res;
158 }
159 
160 
161 void h1analysis::Begin(TTree * /*tree*/)
162 {
163 // function called before starting the event loop
164 // -it performs some cleanup
165 // -it creates histograms
166 // -it sets some initialisation for the entry list
167 
168  // This is needed when re-processing the object
169  Reset();
170 
171  //print the option specified in the Process function.
172  TString option = GetOption();
173  Info("Begin", "starting h1analysis with process option: %s", option.Data());
174 
175  //process cases with entry list
176  if (fChain) fChain->SetEntryList(0);
177  delete gDirectory->GetList()->FindObject("elist");
178 
179  // case when one creates/fills the entry list
180  if (option.Contains("fillList")) {
181  fillList = kTRUE;
182  elist = new TEntryList("elist", "H1 selection from Cut");
183  // Add to the input list for processing in PROOF, if needed
184  if (fInput) {
185  fInput->Add(new TNamed("fillList",""));
186  // We send a clone to avoid double deletes when importing the result
187  fInput->Add(elist);
188  // This is needed to avoid warnings from output-to-members mapping
189  elist = 0;
190  }
191  Info("Begin", "creating an entry-list");
192  }
193  // case when one uses the entry list generated in a previous call
194  if (option.Contains("useList")) {
195  useList = kTRUE;
196  if (fInput) {
197  // In PROOF option "useList" is processed in SlaveBegin and we do not need
198  // to do anything here
199  } else {
200  TFile f("elist.root");
201  elist = (TEntryList*)f.Get("elist");
202  if (elist) elist->SetDirectory(0); //otherwise the file destructor will delete elist
203  }
204  }
205 }
206 
207 
209 {
210 // function called before starting the event loop
211 // -it performs some cleanup
212 // -it creates histograms
213 // -it sets some initialisation for the entry list
214 
215  //initialize the Tree branch addresses
216  Init(tree);
217 
218  //print the option specified in the Process function.
219  TString option = GetOption();
220  Info("SlaveBegin",
221  "starting h1analysis with process option: %s (tree: %p)", option.Data(), tree);
222 
223  //create histograms
224  hdmd = new TH1F("hdmd","dm_d",40,0.13,0.17);
225  h2 = new TH2F("h2","ptD0 vs dm_d",30,0.135,0.165,30,-3,6);
226 
227  fOutput->Add(hdmd);
228  fOutput->Add(h2);
229 
230  // Entry list stuff (re-parse option because on PROOF only SlaveBegin is called)
231  if (option.Contains("fillList")) {
232  fillList = kTRUE;
233  // Get the list
234  if (fInput) {
235  if ((elist = (TEntryList *) fInput->FindObject("elist")))
236  // Need to clone to avoid problems when destroying the selector
237  elist = (TEntryList *) elist->Clone();
238  if (elist)
239  fOutput->Add(elist);
240  else
241  fillList = kFALSE;
242  }
243  }
244  if (fillList) Info("SlaveBegin", "creating an entry-list");
245  if (option.Contains("useList")) useList = kTRUE;
246 }
247 
248 
250 {
251 // entry is the entry number in the current Tree
252 // Selection function to select D* and D0.
253 
254  fProcessed++;
255  //in case one entry list is given in input, the selection has already been done.
256  if (!useList) {
257  // Read only the necessary branches to select entries.
258  // return as soon as a bad entry is detected
259  // to read complete event, call fChain->GetTree()->GetEntry(entry)
260  b_md0_d->GetEntry(entry); if (TMath::Abs(md0_d-1.8646) >= 0.04) return kFALSE;
261  b_ptds_d->GetEntry(entry); if (ptds_d <= 2.5) return kFALSE;
262  b_etads_d->GetEntry(entry); if (TMath::Abs(etads_d) >= 1.5) return kFALSE;
263  b_ik->GetEntry(entry); ik--; //original ik used f77 convention starting at 1
264  b_ipi->GetEntry(entry); ipi--;
265  b_ntracks->GetEntry(entry);
266  b_nhitrp->GetEntry(entry);
267  if (nhitrp[ik]*nhitrp[ipi] <= 1) return kFALSE;
268  b_rend->GetEntry(entry);
269  b_rstart->GetEntry(entry);
270  if (rend[ik] -rstart[ik] <= 22) return kFALSE;
271  if (rend[ipi]-rstart[ipi] <= 22) return kFALSE;
272  b_nlhk->GetEntry(entry); if (nlhk[ik] <= 0.1) return kFALSE;
273  b_nlhpi->GetEntry(entry); if (nlhpi[ipi] <= 0.1) return kFALSE;
274  b_ipis->GetEntry(entry); ipis--; if (nlhpi[ipis] <= 0.1) return kFALSE;
275  b_njets->GetEntry(entry); if (njets < 1) return kFALSE;
276  }
277  // if option fillList, fill the entry list
278  if (fillList) elist->Enter(entry);
279 
280  // to read complete event, call fChain->GetTree()->GetEntry(entry)
281  // read branches not processed in ProcessCut
282  b_dm_d->GetEntry(entry); //read branch holding dm_d
283  b_rpd0_t->GetEntry(entry); //read branch holding rpd0_t
284  b_ptd0_d->GetEntry(entry); //read branch holding ptd0_d
285 
286  //fill some histograms
287  hdmd->Fill(dm_d);
288  h2->Fill(dm_d,rpd0_t/0.029979*1.8646/ptd0_d);
289 
290  // Count the number of selected events
291  fStatus++;
292 
293  return kTRUE;
294 }
295 
296 
297 
299 {
300  // nothing to be done
301 }
302 
303 
305 {
306 // function called at the end of the event loop
307 
308  hdmd = dynamic_cast<TH1F*>(fOutput->FindObject("hdmd"));
309  h2 = dynamic_cast<TH2F*>(fOutput->FindObject("h2"));
310 
311  if (hdmd == 0 || h2 == 0) {
312  Error("Terminate", "hdmd = %p , h2 = %p", hdmd, h2);
313  return;
314  }
315 
316  //create the canvas for the h1analysis fit
317  gStyle->SetOptFit();
318  TCanvas *c1 = new TCanvas("c1","h1analysis analysis",10,10,800,600);
319  c1->SetBottomMargin(0.15);
320  hdmd->GetXaxis()->SetTitle("m_{K#pi#pi} - m_{K#pi}[GeV/c^{2}]");
321  hdmd->GetXaxis()->SetTitleOffset(1.4);
322 
323  //fit histogram hdmd with function f5 using the log-likelihood option
324  if (gROOT->GetListOfFunctions()->FindObject("f5"))
325  delete gROOT->GetFunction("f5");
326  TF1 *f5 = new TF1("f5",fdm5,0.139,0.17,5);
327  f5->SetParameters(1000000, .25, 2000, .1454, .001);
328  hdmd->Fit("f5","lr");
329 
330  //create the canvas for tau d0
331  gStyle->SetOptFit(0);
332  gStyle->SetOptStat(1100);
333  TCanvas *c2 = new TCanvas("c2","tauD0",100,100,800,600);
334  c2->SetGrid();
335  c2->SetBottomMargin(0.15);
336 
337  // Project slices of 2-d histogram h2 along X , then fit each slice
338  // with function f2 and make a histogram for each fit parameter
339  // Note that the generated histograms are added to the list of objects
340  // in the current directory.
341  if (gROOT->GetListOfFunctions()->FindObject("f2"))
342  delete gROOT->GetFunction("f2");
343  TF1 *f2 = new TF1("f2",fdm2,0.139,0.17,2);
344  f2->SetParameters(10000, 10);
345  // Restrict to three bins in this example
346  Info("Fit Slices","Restricting fit to two bins only in this example...");
347  h2->FitSlicesX(f2,10,20,10,"g5 l");
348  TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
349  h2_1->GetXaxis()->SetTitle("#tau[ps]");
350  h2_1->SetMarkerStyle(21);
351  h2_1->Draw();
352  c2->Update();
353  TLine *line = new TLine(0,0,0,c2->GetUymax());
354  line->Draw();
355 
356  // Have the number of entries on the first histogram (to cross check when running
357  // with entry lists)
358  TPaveStats *psdmd = (TPaveStats *)hdmd->GetListOfFunctions()->FindObject("stats");
359  psdmd->SetOptStat(1110);
360  c1->Modified();
361 
362  //save the entry list to a Root file if one was produced
363  if (fillList) {
364  if (!elist)
365  elist = dynamic_cast<TEntryList*>(fOutput->FindObject("elist"));
366  if (elist) {
367  Printf("Entry list 'elist' created:");
368  elist->Print();
369  TFile efile("elist.root","recreate");
370  elist->Write();
371  } else {
372  Error("Terminate", "entry list requested but not found in output");
373  }
374  }
375  // Notify the amount of processed events
376  if (!fInput) Info("Terminate", "processed %lld events", fProcessed);
377 }
TBranch * b_md0_d
Definition: h1analysis.h:265
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:778
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:588
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:847
TSelectorList * fOutput
! List of objects created during processing
Definition: TSelector.h:44
long long Long64_t
Definition: RtypesCore.h:69
Bool_t Process(Long64_t entry)
TLine * line
TBranch * b_rstart
Definition: h1analysis.h:315
return c1
Definition: legend1.C:41
Float_t rstart[200]
Definition: h1analysis.h:161
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
Int_t njets
Definition: h1analysis.h:174
Double_t fdm2(Double_t *xx, Double_t *par)
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
TBranch * b_ptds_d
Definition: h1analysis.h:252
#define gROOT
Definition: TROOT.h:375
Float_t etads_d
Definition: h1analysis.h:99
Basic string class.
Definition: TString.h:129
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
TBranch * b_rpd0_t
Definition: h1analysis.h:285
bool Bool_t
Definition: RtypesCore.h:59
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:202
void Terminate()
The histogram statistics painter class.
Definition: TPaveStats.h:18
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:213
TBranch * b_ptd0_d
Definition: h1analysis.h:263
void Reset()
Definition: h1analysis.h:376
TBranch * b_ipi
Definition: h1analysis.h:260
Short_t Abs(Short_t d)
Definition: TMathBase.h:108
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:628
TBranch * b_rend
Definition: h1analysis.h:316
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:501
TEntryList * elist
Definition: h1analysis.h:32
void SlaveBegin(TTree *tree)
virtual void SetDirectory(TDirectory *dir)
Add reference to directory dir. dir can be 0.
TH1F * hdmd
Definition: h1analysis.h:27
Double_t x[n]
Definition: legend1.C:17
Float_t nlhpi[200]
Definition: h1analysis.h:166
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TH2F * h2
Definition: h1analysis.h:28
Float_t ptd0_d
Definition: h1analysis.h:109
Float_t md0_d
Definition: h1analysis.h:111
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:323
TBranch * b_etads_d
Definition: h1analysis.h:253
virtual const char * GetOption() const
Definition: TSelector.h:59
const Double_t sigma
TBranch * b_njets
Definition: h1analysis.h:328
Int_t nhitrp[200]
Definition: h1analysis.h:157
Long64_t fProcessed
Definition: h1analysis.h:33
void SlaveTerminate()
TBranch * b_ik
Definition: h1analysis.h:259
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:328
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:100
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
Long64_t fStatus
Selector status.
Definition: TSelector.h:39
void Init(TTree *tree)
Definition: h1analysis.h:390
virtual void FitSlicesX(TF1 *f1=0, Int_t firstybin=0, Int_t lastybin=-1, Int_t cut=0, Option_t *option="QNR", TObjArray *arr=0)
Project slices along X in case of a 2-D histogram, then fit each slice with function f1 and make a hi...
Definition: TH2.cxx:896
void Begin(TTree *tree)
TBranch * b_nlhk
Definition: h1analysis.h:319
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
Float_t dm_d
Definition: h1analysis.h:100
void SetOptFit(Int_t fit=1)
The type of information about fit parameters printed in the histogram statistics box can be selected ...
Definition: TStyle.cxx:1219
const Double_t dxbin
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
A simple line.
Definition: TLine.h:23
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1291
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
TTree * fChain
Definition: h1analysis.h:35
Int_t ipi
Definition: h1analysis.h:106
Bool_t useList
Definition: h1analysis.h:30
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
Float_t rend[200]
Definition: h1analysis.h:162
#define Printf
Definition: TGeoToOCC.h:18
const Bool_t kFALSE
Definition: RtypesCore.h:92
The Canvas class.
Definition: TCanvas.h:31
Double_t Exp(Double_t x)
Definition: TMath.h:622
return c2
Definition: legend2.C:14
Int_t ik
Definition: h1analysis.h:105
Float_t nlhk[200]
Definition: h1analysis.h:165
double f(double x)
virtual void SetEntryList(TEntryList *list, Option_t *opt="")
Set an EntryList.
Definition: TTree.cxx:8516
TBranch * b_ipis
Definition: h1analysis.h:261
double Double_t
Definition: RtypesCore.h:55
TBranch * b_dm_d
Definition: h1analysis.h:254
TBranch * b_ntracks
Definition: h1analysis.h:303
Float_t rpd0_t
Definition: h1analysis.h:131
Int_t ipis
Definition: h1analysis.h:107
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:572
virtual Bool_t Enter(Long64_t entry, TTree *tree=0)
Add entry #entry to the list.
Definition: TEntryList.cxx:562
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:65
Double_t fdm5(Double_t *xx, Double_t *par)
Double_t GetUymax() const
Definition: TPad.h:225
virtual void Print(const Option_t *option="") const
Print this list.
Definition: TEntryList.cxx:993
TList * fInput
List of objects available during processing.
Definition: TSelector.h:43
TBranch * b_nlhpi
Definition: h1analysis.h:320
virtual void Add(TObject *obj)
Definition: TList.h:77
double f2(const double *x)
Float_t ptds_d
Definition: h1analysis.h:98
1-Dim function class
Definition: TF1.h:150
TBranch * b_nhitrp
Definition: h1analysis.h:311
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1267
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:78
#define gDirectory
Definition: TDirectory.h:211
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
A List of entry numbers in a TTree or TChain.
Definition: TEntryList.h:25
const Bool_t kTRUE
Definition: RtypesCore.h:91
void Modified(Bool_t flag=1)
Definition: TPad.h:410
TAxis * GetXaxis()
Definition: TH1.h:300
Bool_t fillList
Definition: h1analysis.h:31
void SetOptStat(Int_t stat=1)
Set the stat option.
Definition: TPaveStats.cxx:303
const char * Data() const
Definition: TString.h:347