-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiTreeUtilPhoton2011.h
326 lines (272 loc) · 9 KB
/
multiTreeUtilPhoton2011.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#ifndef MULTITREETUIL_H
#define MULTITREETUIL_H
// =========================================================================
// Multi Tree Utility
// Author: Yen-Jie Lee (MIT)
// Modified by Yongsun Kim (MIT)
// A tool for multiTree handling
// =========================================================================
#include <TGraphAsymmErrors.h>
#include <TLegend.h>
#include <TF1.h>
#include <TCanvas.h>
#include <TLatex.h>
#include <TLine.h>
#include <TCanvas.h>
#include <TBox.h>
#include <TH1F.h>
#include <TH1D.h>
#include <TH2D.h>
#include <TH2F.h>
#include <TGaxis.h>
#include <TTree.h>
#include <TCut.h>
#include <TFile.h>
//#include <vector.h>
#include <TH1.h>
class multiTreeUtil
{
public:
multiTreeUtil() {
nTrees=0;
for (int i=0;i<100;i++)
{
color[i]=i;
}
color[0]=kOrange-3;
color[1]=3;
color[2]=4;
color[3]=5;
color[4]=6;
color[5]=9;
color[6]=1;
color[7]=kAzure+3;
color[8]=kOrange+3;
color[9]=kSpring-7;
color[10]=kPink+4;
/*
handsomeTH1(tmp1[1],2);
handsomeTH1(tmp1[2],4);
handsomeTH1(tmp1[3],kGreen+2);
handsomeTH1(tmp1[4],6);
handsomeTH1(tmp1[5],7);
handsomeTH1(tmp1[6],kGreen+4);
handsomeTH1(tmp1[7],kBlue-2);
handsomeTH1(tmp1[8],kAzure+3);
handsomeTH1(tmp1[9],kOrange+3);
handsomeTH1(tmp1[10],kSpring-7);
handsomeTH1(tmp1[11],kPink+4);
*/
}
~multiTreeUtil() {
}
void addFile(TString filename, TString treeName, TCut cut, Float_t scaleFactor=1);
void Draw(TH1D* h,TString expression,TCut cut = "" , TString indWeight="", TString opt="");
void Draw2(TH1D* h,TString expression,TCut cut = "", TString indWeight="", TString opt="",bool hist=true);
void Draw2D(TH2D* h,TString expression,TCut cut = "", TString indWeight="", TString opt="",bool hist=true);
void Draw3(TH1D* h,TString expression,TCut cut = "", TString indWeight="", TString opt="",bool hist=true);
void Print(Option_t* option = "");
void AddFriend(TString friendname="");
void SetAlias(TString nameAl="", TString varAl="");
TTree *getTree(int i) { return trees_[i]; };
private:
int nTrees;
vector<TFile*> tfiles_; // TFile
vector<TTree*> trees_; // Trees
vector<TString> tfileNames_; // File names
vector<TCut> tcuts_;
vector<Float_t> scaleFactors_;
int color[100];
void cleanHist(TH1* h);
};
// =========================================================================
// Add file
// =========================================================================
void multiTreeUtil::addFile(TString filename, TString treeName, TCut cut, Float_t scaleFactor)
{
TFile *f = new TFile(filename.Data());
TTree *t = (TTree*) f->FindObjectAny(treeName.Data());
if (t!=0) {
nTrees++;
tfileNames_.push_back(filename);
tfiles_.push_back(f);
trees_.push_back(t);
tcuts_.push_back(cut);
scaleFactors_.push_back(scaleFactor);
}
else
cout << " there is no such tree in this file :" << treeName << endl;
}
// =========================================================================
// Draw
// =========================================================================
void multiTreeUtil::Draw(TH1D *h, TString expression, TCut cut,TString indWeight, TString opt)
{
TH1::SetDefaultSumw2();
h->Reset();
// h->Sumw2();
for (int i=0;i<(int)trees_.size();i++)
{
TH1D *htmp = (TH1D*)h->Clone();
htmp->SetName(Form("my_htmp_%d",i));
htmp->Reset();
// htmp->Sumw2();
TCut fCut = cut&&tcuts_[i];
if ( indWeight=="")
trees_[i]->Draw(Form("%s>>my_htmp_%d",expression.Data(),i), Form("(%s)", fCut.GetTitle() ));
else
trees_[i]->Draw(Form("%s>>my_htmp_%d",expression.Data(),i), Form("(%s) * (%s)", fCut.GetTitle(), indWeight.Data() ) );
htmp->Scale(scaleFactors_[i]);
h->Add(htmp);
delete htmp;
}
h->Draw(opt.Data());
}
// =========================================================================
// Draw components
// =========================================================================
void multiTreeUtil::Draw2(TH1D *h, TString expression, TCut cut, TString indWeight, TString opt,bool hist)
{
TH1::SetDefaultSumw2();
//TH1D *hComponent[100];
h->Reset();
// h->Sumw2();
for (int i=0;i<(int)trees_.size();i++)
{
TString hName = Form("%s_Draw_%d",h->GetName(),i);
TH1D *htmp= (TH1D*)h->Clone();
htmp->SetName(hName.Data());
htmp->Reset();
// htmp->Sumw2();
TCut fCut = cut&&tcuts_[i];
if ( indWeight=="" ) {
trees_[i]->Draw(Form("%s>>%s",expression.Data(),hName.Data()), Form("(%s)", fCut.GetTitle() ));
}
else {
trees_[i]->Draw(Form("%s>>%s",expression.Data(),hName.Data()), Form("( %s ) * (%s)", fCut.GetTitle(), indWeight.Data()) );
}
cout << " Running draw2" << endl;
cout << " Var : " << expression.Data() << endl;
cout << "(cut) * (weight) : " << endl << Form("( %s ) * ( %s )", fCut.GetTitle(), indWeight.Data()) << endl;
cout << " and scale factor = " << double(scaleFactors_[i]) << endl;
h->Add(htmp, double(scaleFactors_[i]) );
delete htmp;
// hComponent[i]=htmp;
}
// h->Sumw2();
// h->DrawCopy(opt.Data());
/*
for (int i=0;i<(int)trees_.size();i++)
{
hComponent[i]->SetLineColor(color[i]);
hComponent[i]->SetMarkerColor(color[i]);
hComponent[i]->SetFillColor(color[i]);
if ( hist) opt ="hist";
// hComponent[i]->DrawCopy(Form("%s same",opt.Data()));
delete hComponent[i];
}
*/
}
void multiTreeUtil::Draw2D(TH2D *h, TString expression, TCut cut, TString indWeight, TString opt,bool hist)
{
TH1::SetDefaultSumw2();
TH2D *hComponent[20];
h->Reset();
for (int i=0;i<(int)trees_.size();i++)
{
TString hName = Form("%s_Draw_%d",h->GetName(),i);
TH2D *htmp= (TH2D*)h->Clone();
// htmp->Sumw2();
htmp->SetName(hName.Data());
TCut fCut = cut&&tcuts_[i];
if ( indWeight=="" ) {
trees_[i]->Draw(Form("%s>>%s",expression.Data(),hName.Data()), Form("(%s)", fCut.GetTitle() ));
}
else {
trees_[i]->Draw(Form("%s>>%s",expression.Data(),hName.Data()), Form("( %s ) * %s", fCut.GetTitle(), indWeight.Data()) );
}
htmp->Scale(scaleFactors_[i]);
h->Add(htmp);
hComponent[i]=htmp;
}
// h->Sumw2();
// h->DrawCopy(opt.Data());
for (int i=0;i<(int)trees_.size();i++)
{
hComponent[i]->SetLineColor(color[i]);
hComponent[i]->SetMarkerColor(color[i]);
hComponent[i]->SetFillColor(color[i]);
if ( hist) opt ="hist";
// hComponent[i]->DrawCopy(Form("%s same",opt.Data()));
delete hComponent[i];
}
}
void multiTreeUtil::Draw3(TH1D *h, TString expression, TCut cut, TString indWeight, TString opt,bool hist)
{
TH1::SetDefaultSumw2();
TH1D *hComponent[100];
h->Reset();
for (int i=0;i<(int)trees_.size();i++)
{
TString hName = Form("%s_Draw_%d",h->GetName(),i);
TH1D *htmp= (TH1D*)h->Clone();
htmp->SetName(hName.Data());
TCut fCut = cut&&tcuts_[i];
if ( indWeight=="" )
trees_[i]->Draw(Form("%s>>%s",expression.Data(),hName.Data()), Form("%s", fCut.GetTitle()));
else
trees_[i]->Draw(Form("%s>>%s",expression.Data(),hName.Data()), Form("(%s) * %s", fCut.GetTitle(), indWeight.Data()) );
// htmp->Sumw2();
htmp->Scale(scaleFactors_[i]);
h->Add(htmp);
hComponent[i]=htmp;
hComponent[i]->SetLineColor(color[i]);
hComponent[i]->SetMarkerColor(color[i]);
hComponent[i]->SetFillColor(color[i]);
}
// h->Sumw2();
h->DrawCopy(opt.Data());
for (int i=0;i<(int)trees_.size();i++)
{
if ( i<(int)trees_.size() -1 )
hComponent[i+1]->Add( hComponent[i]);
}
for (int i=0;i<(int)trees_.size();i++)
{
if ( hist) opt ="hist";
hComponent[trees_.size()-i-1]->DrawCopy(Form("%s same",opt.Data()));
}
}
// =========================================================================
// Print variables from the first tree
// =========================================================================
void multiTreeUtil::AddFriend(TString friendname)
{
for (int i=0;i<(int)trees_.size();i++)
{
trees_[i]->AddFriend(friendname.Data(), tfileNames_[i]);
}
}
void multiTreeUtil::SetAlias(TString nameAl, TString varAl)
{
for (int i=0;i<(int)trees_.size();i++)
{
trees_[i]->SetAlias( nameAl.Data(), varAl.Data());
}
}
void multiTreeUtil::Print(Option_t* option)
{
trees_[0]->Print(option);
}
//--------------------------------PRIVATE-----------------------------------
// =========================================================================
// Clear
// =========================================================================
void multiTreeUtil::cleanHist(TH1* h)
{
for (int i=0;i<=h->GetNbinsX()+1;i++) {
h->SetBinContent(i,0);
h->SetBinError(i,0);
}
}
#endif