-
Notifications
You must be signed in to change notification settings - Fork 2
/
rootfuncs.h
186 lines (130 loc) · 3.7 KB
/
rootfuncs.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
TFile* OpenRootFile(const TString& rootfile) {
cout << "Rootfile: " << rootfile << endl;
TFile *file;
if( gSystem->AccessPathName(rootfile) ){
cout << endl << "File: " << rootfile << " not there!!!" << endl << endl;
file=0;
}
else
file = new TFile(rootfile);
return file;
}
TFile* OpenDCacheFile(const TString& rootfile) {
cout << "Rootfile: " << rootfile << endl;
TFile *file;
if( gSystem->AccessPathName(rootfile) ){
cout << endl << "File: " << rootfile << " not there!!!" << endl << endl;
file=0;
}
else
file = new TDCacheFile(rootfile,"READ","Demo ROOT file with histograms",0);
return file;
}
bool hExist(TFile *file,const TString& hname){
bool retval=true;
TKey *key = file->FindKey(hname);
if (key ==0){
cout << "!!Histogram " << hname << " does not exist!!" << endl;
retval=false;
}
return retval;
}
TH1F* GetHist(TFile* file,const TString& hname){
TH1F* h=0;
if (hExist(file,hname)){
h=(TH1F*)file->Get(hname);
}
return h;
}
TH2* GetHist2D(TFile* file,const TString& hname){
TH2* h=0;
if (hExist(file,hname)){
h=(TH2F*)file->Get(hname);
}
return h;
}
TH2* get2DHist(TFile* file, const TString& subdir,const TString& hname, bool listDir=false){
//cout << "Subdirectory " << subdir << endl;
//gDirectory->pwd();
file->cd(subdir);
if (listDir)
gDirectory->ls();
TH2F *h;
gDirectory->GetObject(hname,h);
if (!h) {
cout << "Histogram with name " << hname << " not found" << endl;
}
return h;
}
TH1* get1DHist(TFile* file, const TString& subdir,const TString& hname, bool listDir=false){
//cout << "Subdirectory " << subdir << endl;
//gDirectory->pwd();
file->cd(subdir);
if (listDir)
gDirectory->ls();
TH1F *h;
gDirectory->GetObject(hname,h);
if (!h) {
cout << "Histogram with name " << hname << " not found" << endl;
}
return h;
}
void mySetup(int ilogy=0){
gROOT->Reset();
gStyle->SetOptTitle(0);
gStyle->SetOptStat(0);
gStyle->SetTitleOffset(1.1,"y");
gStyle->SetHistLineWidth(2);
//gStyle->SetTitleSize(0.05,"y");
//gStyle->SetTitleSize(0.05,"x");
//gStyle->SetLabelSize(0.04,"y");
//gStyle->SetLabelSize(0.045,"x");
gStyle->SetHistFillColor(kYellow);
gStyle->SetOptLogy(ilogy);
gROOT->ForceStyle();
return;
}
void divideByBinWidth(TH1* h){
Int_t nbins=h->GetNbinsX();
//cout << "Number of bins: " << nbins << endl;
for (Int_t ibin=0; ibin<nbins; ++ibin){
Float_t cont=h->GetBinContent(ibin+1);
Float_t err =h->GetBinError(ibin+1);
Float_t bw =h->GetBinWidth(ibin+1);
cont=cont/bw;
err=err/bw;
h->SetBinContent(ibin+1,cont);
h->SetBinError(ibin+1,err);
}
}
void scaleErrors(TH1* h, const double fact){
Int_t nbins=h->GetNbinsX();
//cout << "Number of bins: " << nbins << endl;
cout << "Scale Factor: " << fact << endl;
for (Int_t ibin=0; ibin<nbins; ++ibin){
Float_t err =h->GetBinError(ibin+1);
err=err*fact;
h->SetBinError(ibin+1,err);
}
}
void printLeaves(const TString& rootname="xxx.root", const TString& treename="HltTree" ){
//rootfile=OpenRootFile(rootname); if (!rootfile) return;
TFile *_file0=TFile::Open(rootname);
// _file0->GetListOfKeys()->Print();
TTree *_tree = dynamic_cast<TTree*>(_file0->Get(treename));
TObjArray *leaves = _tree->GetListOfBranches();
Int_t leafEnts=leaves->GetSize();
cout << "Number of leaves: " << leafEnts << endl;
for(Int_t i = 0; i < leafEnts; i++){
std::string leafName = leaves->At(i)->GetName();
cout << "\t" <<leafName << "\n";
}
cout << "\nDone" << endl;
}
TString chInt(const int& value){
TString conv="xxx";
ostringstream ch_val("");
ch_val << value;
conv=ch_val.str();
return conv;
}