Logo ROOT  
Reference Guide
h1analysisTreeReader.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook
4 /// H1 analysis example expressed in terms of TTreeReader
5 ///
6 /// \macro_code
7 ///
8 /// \author Anders Eie, 2013
9 
10 #include "h1analysisTreeReader.h"
11 #include "TStyle.h"
12 #include "TCanvas.h"
13 #include "TPaveStats.h"
14 #include "TLine.h"
15 #include "TMath.h"
16 #include "TFile.h"
17 #include "TROOT.h"
18 
19 
20 const Double_t dxbin = (0.17-0.13)/40; // Bin-width
21 const Double_t sigma = 0.0012;
22 
23 //_____________________________________________________________________
24 Double_t fdm5(Double_t *xx, Double_t *par)
25 {
26  Double_t x = xx[0];
27  if (x <= 0.13957) return 0;
28  Double_t xp3 = (x-par[3])*(x-par[3]);
29  Double_t res = dxbin*(par[0]*TMath::Power(x-0.13957, par[1])
30  + par[2] / 2.5066/par[4]*TMath::Exp(-xp3/2/par[4]/par[4]));
31  return res;
32 }
33 
34 //_____________________________________________________________________
35 Double_t fdm2(Double_t *xx, Double_t *par)
36 {
37  Double_t x = xx[0];
38  if (x <= 0.13957) return 0;
39  Double_t xp3 = (x-0.1454)*(x-0.1454);
40  Double_t res = dxbin*(par[0]*TMath::Power(x-0.13957, 0.25)
41  + par[1] / 2.5066/sigma*TMath::Exp(-xp3/2/sigma/sigma));
42  return res;
43 }
44 //_____________________________________________________________________
46 // entry is the entry number in the current Tree
47 // Selection function to select D* and D0.
49  fProcessed++;
50  //in case one entry list is given in input, the selection has already been done.
51  if (!useList) {
52  // Return as soon as a bad entry is detected
53  if (TMath::Abs(*fMd0_d-1.8646) >= 0.04) return kFALSE;
54  if (*fPtds_d <= 2.5) return kFALSE;
55  if (TMath::Abs(*fEtads_d) >= 1.5) return kFALSE;
56  (*fIk)--; //original fIk used f77 convention starting at 1
57  (*fIpi)--;
58 
59 
60  if (fNhitrp.At(*fIk)* fNhitrp.At(*fIpi) <= 1) return kFALSE;
61 
62 
63  if (fRend.At(*fIk) -fRstart.At(*fIk) <= 22) return kFALSE;
64  if (fRend.At(*fIpi)-fRstart.At(*fIpi) <= 22) return kFALSE;
65  if (fNlhk.At(*fIk) <= 0.1) return kFALSE;
66  if (fNlhpi.At(*fIpi) <= 0.1) return kFALSE;
67  (*fIpis)--; if (fNlhpi.At(*fIpis) <= 0.1) return kFALSE;
68  if (*fNjets < 1) return kFALSE;
69  }
70  // if option fillList, fill the entry list
71  if (fillList) elist->Enter(entry);
72 
73  //fill some histograms
74  hdmd->Fill(*fDm_d);
75  h2->Fill(*fDm_d,*fRpd0_t/0.029979*1.8646/ *fPtd0_d);
76 
77  return kTRUE;
78 }
79 
80 void h1analysisTreeReader::Begin(TTree* /*myTree*/) {
81 // function called before starting the event loop
82 // -it performs some cleanup
83 // -it creates histograms
84 // -it sets some initialisation for the entry list
85 
86  Reset();
87 
88  //print the option specified in the Process function.
89  TString option = GetOption();
90  Info("Begin", "starting h1analysis with process option: %s", option.Data());
91 
92  delete gDirectory->GetList()->FindObject("elist");
93 
94  // case when one creates/fills the entry list
95  if (option.Contains("fillList")) {
96  fillList = kTRUE;
97  elist = new TEntryList("elist", "H1 selection from Cut");
98  // Add to the input list for processing in PROOF, if needed
99  if (fInput) {
100  fInput->Add(new TNamed("fillList",""));
101  // We send a clone to avoid double deletes when importing the result
102  fInput->Add(elist);
103  // This is needed to avoid warnings from output-to-members mapping
104  elist = 0;
105  }
106  Info("Begin", "creating an entry-list");
107  }
108  // case when one uses the entry list generated in a previous call
109  if (option.Contains("useList")) {
110  useList = kTRUE;
111  if (fInput) {
112  // In PROOF option "useList" is processed in SlaveBegin and we do not need
113  // to do anything here
114  } else {
115  TFile f("elist.root");
116  elist = (TEntryList*)f.Get("elist");
117  if (elist) elist->SetDirectory(0); //otherwise the file destructor will delete elist
118  }
119  }
120 }
121 
123 
124 // function called before starting the event loop
125 // -it performs some cleanup
126 // -it creates histograms
127 // -it sets some initialisation for the entry list
128 
129  Init(myTree);
130 
131  //print the option specified in the Process function.
132  TString option = GetOption();
133  Info("SlaveBegin",
134  "starting h1analysis with process option: %s (tree: %p)", option.Data(), myTree);
135 
136  //create histograms
137  hdmd = new TH1F("hdmd","Dm_d",40,0.13,0.17);
138  h2 = new TH2F("h2","ptD0 vs Dm_d",30,0.135,0.165,30,-3,6);
139 
140  fOutput->Add(hdmd);
141  fOutput->Add(h2);
142 
143  // Entry list stuff (re-parse option because on PROOF only SlaveBegin is called)
144  if (option.Contains("fillList")) {
145  fillList = kTRUE;
146  // Get the list
147  if (fInput) {
148  if ((elist = (TEntryList *) fInput->FindObject("elist")))
149  // Need to clone to avoid problems when destroying the selector
150  elist = (TEntryList *) elist->Clone();
151  if (elist)
152  fOutput->Add(elist);
153  else
154  fillList = kFALSE;
155  }
156  }
157  if (fillList) Info("SlaveBegin", "creating an entry-list");
158  if (option.Contains("useList")) useList = kTRUE;
159 }
160 
162  // function called at the end of the event loop
163 
164  hdmd = dynamic_cast<TH1F*>(fOutput->FindObject("hdmd"));
165  h2 = dynamic_cast<TH2F*>(fOutput->FindObject("h2"));
166 
167  if (hdmd == 0 || h2 == 0) {
168  Error("Terminate", "hdmd = %p , h2 = %p", hdmd, h2);
169  return;
170  }
171 
172  //create the canvas for the h1analysis fit
173  gStyle->SetOptFit();
174  TCanvas *c1 = new TCanvas("c1","h1analysis analysis",10,10,800,600);
175  c1->SetBottomMargin(0.15);
176  hdmd->GetXaxis()->SetTitle("m_{K#pi#pi} - m_{K#pi}[GeV/c^{2}]");
177  hdmd->GetXaxis()->SetTitleOffset(1.4);
178 
179  //fit histogram hdmd with function f5 using the loglfIkelihood option
180  if (gROOT->GetListOfFunctions()->FindObject("f5"))
181  delete gROOT->GetFunction("f5");
182  TF1 *f5 = new TF1("f5",fdm5,0.139,0.17,5);
183  f5->SetParameters(1000000, .25, 2000, .1454, .001);
184  hdmd->Fit("f5","lr");
185 
186  //create the canvas for tau d0
187  gStyle->SetOptFit(0);
188  gStyle->SetOptStat(1100);
189  TCanvas *c2 = new TCanvas("c2","tauD0",100,100,800,600);
190  c2->SetGrid();
191  c2->SetBottomMargin(0.15);
192 
193  // Project slices of 2-d histogram h2 along X , then fit each slice
194  // with function f2 and make a histogram for each fit parameter
195  // Note that the generated histograms are added to the list of objects
196  // in the current directory.
197  if (gROOT->GetListOfFunctions()->FindObject("f2"))
198  delete gROOT->GetFunction("f2");
199  TF1 *f2 = new TF1("f2",fdm2,0.139,0.17,2);
200  f2->SetParameters(10000, 10);
201  h2->FitSlicesX(f2,0,-1,1,"qln");
202  TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
203  h2_1->GetXaxis()->SetTitle("#tau[ps]");
204  h2_1->SetMarkerStyle(21);
205  h2_1->Draw();
206  c2->Update();
207  TLine *line = new TLine(0,0,0,c2->GetUymax());
208  line->Draw();
209 
210  // Have the number of entries on the first histogram (to cross check when running
211  // with entry lists)
212  TPaveStats *psdmd = (TPaveStats *)hdmd->GetListOfFunctions()->FindObject("stats");
213  psdmd->SetOptStat(1110);
214  c1->Modified();
215 
216  //save the entry list to a Root file if one was produced
217  if (fillList) {
218  if (!elist)
219  elist = dynamic_cast<TEntryList*>(fOutput->FindObject("elist"));
220  if (elist) {
221  Printf("Entry list 'elist' created:");
222  elist->Print();
223  TFile efile("elist.root","recreate");
224  elist->Write();
225  } else {
226  Error("Terminate", "entry list requested but not found in output");
227  }
228  }
229  // Notify the amount of processed events
230  if (!fInput) Info("Terminate", "processed %lld events", fProcessed);
231 }
232 
234 
235 }
236 
238 // called when loading a new file
239 // get branch pointers
240 
241  Info("Notify","processing file: %s",myTreeReader.GetTree()->GetCurrentFile()->GetName());
242 
243  if (elist && myTreeReader.GetTree()) {
244  if (fillList) {
246  } else if (useList) {
248  }
249  }
250  return kTRUE;
251 }
h1analysisTreeReader::hdmd
TH1F * hdmd
Definition: h1analysisTreeReader.h:32
h1analysisTreeReader::fRpd0_t
TTreeReaderValue< Float_t > fRpd0_t
Definition: h1analysisTreeReader.h:24
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TLine.h
TSelector::GetOption
virtual const char * GetOption() const
Definition: TSelector.h:57
f
#define f(i)
Definition: RSha256.hxx:104
h1analysisTreeReader::fIpi
TTreeReaderValue< Int_t > fIpi
Definition: h1analysisTreeReader.h:20
TLine
A simple line.
Definition: TLine.h:22
h1analysisTreeReader::fIk
TTreeReaderValue< Int_t > fIk
Definition: h1analysisTreeReader.h:19
h1analysisTreeReader::fPtds_d
TTreeReaderValue< Float_t > fPtds_d
Definition: h1analysisTreeReader.h:16
h1analysisTreeReader::fEtads_d
TTreeReaderValue< Float_t > fEtads_d
Definition: h1analysisTreeReader.h:17
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
TList::FindObject
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:577
TString::Data
const char * Data() const
Definition: TString.h:369
TObject::Info
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:864
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
h1analysisTreeReader::fRstart
TTreeReaderArray< Float_t > fRstart
Definition: h1analysisTreeReader.h:26
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:616
h1analysisTreeReader::fNjets
TTreeReaderValue< Int_t > fNjets
Definition: h1analysisTreeReader.h:30
TMath::Exp
Double_t Exp(Double_t x)
Definition: TMath.h:727
TStyle.h
TPaveStats.h
h1analysisTreeReader::Process
Bool_t Process(Long64_t entry)
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
x
Double_t x[n]
Definition: legend1.C:17
h1analysisTreeReader::Init
void Init(TTree *myTree)
Definition: h1analysisTreeReader.h:67
h1analysisTreeReader::Notify
Bool_t Notify()
This method must be overridden to handle object notification.
TH1::GetListOfFunctions
TList * GetListOfFunctions() const
Definition: TH1.h:241
TTreeReaderArray::At
T & At(std::size_t idx)
Definition: TTreeReaderArray.h:205
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TCanvas.h
TEntryList::SetTree
virtual void SetTree(const TTree *tree)
If a list for a tree with such name and filename exists, sets it as the current sublist If not,...
Definition: TEntryList.cxx:1194
TString
Basic string class.
Definition: TString.h:136
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
fdm5
Double_t fdm5(Double_t *xx, Double_t *par)
Definition: h1analysisProxy.h:14
TFile.h
TStyle::SetOptFit
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:1541
TPaveStats
The histogram statistics painter class.
Definition: TPaveStats.h:18
h1analysisTreeReader::fIpis
TTreeReaderValue< Int_t > fIpis
Definition: h1analysisTreeReader.h:21
TROOT.h
TF1::SetParameters
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:643
h1analysisTreeReader::useList
Bool_t useList
Definition: h1analysisTreeReader.h:35
h1analysisTreeReader::myTreeReader
TTreeReader myTreeReader
Definition: h1analysisTreeReader.h:14
h1analysisTreeReader::fDm_d
TTreeReaderValue< Float_t > fDm_d
Definition: h1analysisTreeReader.h:18
TTree::GetCurrentFile
TFile * GetCurrentFile() const
Return pointer to the current file.
Definition: TTree.cxx:5378
h1analysisTreeReader::SlaveBegin
void SlaveBegin(TTree *)
h1analysisTreeReader::fRend
TTreeReaderArray< Float_t > fRend
Definition: h1analysisTreeReader.h:27
TTreeReader::GetTree
TTree * GetTree() const
Definition: TTreeReader.h:173
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
dxbin
const Double_t dxbin
Definition: h1analysisProxy.h:10
TEntryList::Enter
virtual Bool_t Enter(Long64_t entry, TTree *tree=0)
Add entry #entry to the list.
Definition: TEntryList.cxx:560
THashList::FindObject
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:262
h1analysisTreeReader::Begin
void Begin(TTree *)
h1analysisTreeReader::fillList
Bool_t fillList
Definition: h1analysisTreeReader.h:36
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TTree::SetEntryList
virtual void SetEntryList(TEntryList *list, Option_t *opt="")
Set an EntryList.
Definition: TTree.cxx:8866
TNamed::Clone
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:74
TMath::Power
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:735
gDirectory
#define gDirectory
Definition: TDirectory.h:236
h1analysisTreeReader::h2
TH2F * h2
Definition: h1analysisTreeReader.h:33
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3327
h1analysisTreeReader::fMd0_d
TTreeReaderValue< Float_t > fMd0_d
Definition: h1analysisTreeReader.h:23
h1analysisTreeReader::Reset
void Reset()
Definition: h1analysisTreeReader.h:81
h1analysisTreeReader::fPtd0_d
TTreeReaderValue< Float_t > fPtd0_d
Definition: h1analysisTreeReader.h:22
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
line
TLine * line
Definition: entrylistblock_figure1.C:235
h1analysisTreeReader.h
TStyle::SetOptStat
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:1589
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
h1analysisTreeReader::elist
TEntryList * elist
Definition: h1analysisTreeReader.h:37
TH2::FitSlicesX
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:905
TH2::Fill
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:294
TEntryList
A List of entry numbers in a TTree or TChain.
Definition: TEntryList.h:26
Printf
void Printf(const char *fmt,...)
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
TObject::Draw
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
Double_t
double Double_t
Definition: RtypesCore.h:59
TSelector::fOutput
TSelectorList * fOutput
! List of objects created during processing
Definition: TSelector.h:42
TTreeReader::SetLocalEntry
EEntryStatus SetLocalEntry(Long64_t entry)
Set the next local tree entry.
Definition: TTreeReader.h:201
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:573
h1analysisTreeReader::Terminate
void Terminate()
TObject::Write
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:795
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TEntryList::Print
virtual void Print(const Option_t *option="") const
Print this list.
Definition: TEntryList.cxx:999
h1analysisTreeReader::fNhitrp
TTreeReaderArray< Int_t > fNhitrp
Definition: h1analysisTreeReader.h:25
h1analysisTreeReader::fNlhpi
TTreeReaderArray< Float_t > fNlhpi
Definition: h1analysisTreeReader.h:29
h1analysisTreeReader::fNlhk
TTreeReaderArray< Float_t > fNlhk
Definition: h1analysisTreeReader.h:28
h1analysisTreeReader::fProcessed
Long64_t fProcessed
Definition: h1analysisTreeReader.h:38
c2
return c2
Definition: legend2.C:14
TH1::Fit
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3869
TAttMarker::SetMarkerStyle
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
TF1
1-Dim function class
Definition: TF1.h:213
TSelector::fInput
TList * fInput
List of objects available during processing.
Definition: TSelector.h:41
fdm2
Double_t fdm2(Double_t *xx, Double_t *par)
Definition: h1analysisProxy.h:25
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:318
TPaveStats::SetOptStat
void SetOptStat(Int_t stat=1)
Set the stat option.
Definition: TPaveStats.cxx:303
TEntryList::SetDirectory
virtual void SetDirectory(TDirectory *dir)
Add reference to directory dir. dir can be 0.
Definition: TEntryList.cxx:1068
TMath.h
h1analysisTreeReader::SlaveTerminate
void SlaveTerminate()
gROOT
#define gROOT
Definition: TROOT.h:406
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050
c1
return c1
Definition: legend1.C:41