-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistHolder.cxx
324 lines (246 loc) · 9.06 KB
/
HistHolder.cxx
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
/*
Goal of this class is to make a class to hold a number of histograms
The base data structure is a map using a string (the name of the histogram)
as a key, that references to a pointer that holds the histogram
We also have a secondary data structure to map by both string and
TFile*, to allow this class to hold a bunch of histograms with the
same name
Utilities should be provided that call Sumw2() and do other useful things
Creating a histogram should be as easy calling a function and passing the name, title, and
other standard arguments
*/
#include <iostream>
#include "HistHolder.h"
HistHolder::HistHolder() :
select_("*"),
reject_("")
{
}
HistHolder::~HistHolder()
{
/* causes a crash
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i )
delete i->second;
*/
reset();
}
void HistHolder::reset() {
// std::cout<<"HistHolder reset()"<<std::endl;
histHolder_.clear();
histHolder2_.clear();
histHolderP_.clear();
histHolderP2_.clear();
}
void
HistHolder::Print(TString opt) {
opt.ToUpper();
if (opt=="V") {
std::cout<<"histHolder_ size = "<<histHolder_.size()<<std::endl;
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
std::cout<<i->first<<"\t"<<i->second<<std::endl;
}
std::cout<<"histHolder2_ size = "<<histHolder2_.size()<<std::endl;
for ( std::map< std::string, TH2F*>::const_iterator i = histHolder2_.begin() ; i!= histHolder2_.end() ; ++i ) {
std::cout<<i->first<<"\t"<<i->second<<std::endl;
}
std::cout<<"histHolderP_ size = "<<histHolderP_.size()<<std::endl;
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ; i!= histHolderP_.end() ; ++i ) {
std::cout<<i->first.first<<" ; "<<i->first.second<<"\t"<<i->second<<std::endl;
}
std::cout<<"histHolderP2_ size = "<<histHolderP2_.size()<<std::endl;
for ( std::map< std::pair<std::string, TFile*>, TH2F*>::const_iterator i = histHolderP2_.begin() ; i!= histHolderP2_.end() ; ++i ) {
std::cout<<i->first.first<<" ; "<<i->first.second<<"\t"<<i->second<<std::endl;
}
}
else {
std::cout<<"histHolder_ size = "<<histHolder_.size()<<std::endl;
std::cout<<"histHolder2_ size = "<<histHolder2_.size()<<std::endl;
std::cout<<"histHolderP_ size = "<<histHolderP_.size()<<std::endl;
std::cout<<"histHolderP2_ size = "<<histHolderP2_.size()<<std::endl;
}
}
void HistHolder::Reset() {
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
i->second->Reset();
}
for ( std::map< std::string, TH2F*>::const_iterator i = histHolder2_.begin() ; i!= histHolder2_.end() ; ++i ) {
i->second->Reset();
}
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ; i!= histHolderP_.end() ; ++i ) {
i->second->Reset();
}
for ( std::map< std::pair<std::string, TFile*>, TH2F*>::const_iterator i = histHolderP2_.begin() ; i!= histHolderP2_.end() ; ++i ) {
i->second->Reset();
}
}
void
HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max) {
TH1D* hist = new TH1D(name.c_str(),title.c_str(),nbins,min,max);
histHolder_[name] = hist;
}
void
HistHolder::make2(std::string name, std::string title, Int_t nx, Double_t minx, Double_t maxx,
Int_t ny, Double_t miny, Double_t maxy) {
TH2F* hist = new TH2F(name.c_str(),title.c_str(),nx,minx,maxx,ny,miny,maxy);
histHolder2_[name] = hist;
}
Int_t
HistHolder::load(TString name, TFile* file) {
Int_t code=0;
TObject* h = file->Get(name);
if (h==0) {std::cout<<"Problem loading "<<name<<std::endl; return code;}
if ( h->InheritsFrom("TH2F") ) {
histHolderP2_[make_pair(std::string(name.Data()),file)] = (TH2F*) h;
code=2;
}
else if ( h->InheritsFrom("TH1D") ) {
histHolderP_[make_pair(std::string(name.Data()),file)] = (TH1D*) h;
code=1;
}
else {
std::cout<<"Could not determine type of histogram "<<name<<std::endl;
}
return code;
}
// void
// HistHolder::load2(TString name, const TFile* file) {
// TH2F* hist = ((TH2F*)file->Get(name));
// histHolder2_[std::string(name.Data())] = hist;
// }
void
HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max, std::string xtitle) {
make(name,title,nbins,min,max);
histHolder_[name]->SetXTitle(xtitle.c_str());
}
void
HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,
std::string xtitle, std::string ytitle) {
make(name,title,nbins,min,max,xtitle);
histHolder_[name]->SetYTitle(ytitle.c_str());
}
void
HistHolder::Write() {
//FIXME need to add histHolder2 and other histHolders
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
if ( passesFilter( i->first))
i->second->Write();
}
}
void HistHolder::Sumw2() {
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
if ( passesFilter( i->first))
i->second->Sumw2();
}
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
i!= histHolderP_.end() ; ++i ) {
if ( passesFilter(i->first))
i->second->Sumw2();
}
}
void HistHolder::SetMaximum( Double_t max) {
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
if ( passesFilter(i->first))
i->second->SetMaximum(max);
}
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
i!= histHolderP_.end() ; ++i ) {
if ( passesFilter( i->first))
i->second->SetMaximum(max);
}
}
void HistHolder::SetMinimum( Double_t min) {
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
if ( passesFilter(i->first))
i->second->SetMinimum(min);
}
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
i!= histHolderP_.end() ; ++i ) {
if ( passesFilter(i->first))
i->second->SetMinimum(min);
}
}
Float_t HistHolder::GetMaximum() {
Float_t max = -99999999;
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
if ( passesFilter(i->first)) {
if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
}
}
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
i!= histHolderP_.end() ; ++i ) {
if ( passesFilter(i->first)) {
if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
}
}
return max;
}
TString HistHolder::getMaximumName() {
//most code copied from GetMaximum()
Float_t max = -99999999;
TString nameofmax="";
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
if ( passesFilter(i->first)) {
if ( i->second->GetMaximum() > max ) {
max = i->second->GetMaximum();
nameofmax = TString(i->first);
}
}
}
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
i!= histHolderP_.end() ; ++i ) {
if ( passesFilter(i->first)) {
if ( i->second->GetMaximum() > max ) {
max = i->second->GetMaximum();
nameofmax = TString(i->first.first);
}
}
}
return nameofmax;
}
void HistHolder::normalize() {
for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
if ( passesFilter(i->first)) {
double factor = i->second->Integral();
i->second->Scale(1.0/factor);
}
}
for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
i!= histHolderP_.end() ; ++i ) {
if ( passesFilter( i->first)) {
double factor = i->second->Integral();
i->second->Scale(1.0/factor);
}
}
}
// utility functions
bool HistHolder::passesFilter(const std::string mystr, TFile* filep) {
//if reject_ is empty, then reject nothing
//if select_ is empty, then accept everything
if (select_=="") select_="*";
std::string mystr2="";
if (filep!=0) mystr2 = std::string(filep->GetName());
bool rejected=false;
if (reject_ != "") {
bool rejected1 = (mystr.find(reject_) != std::string::npos);
bool rejected2=false;
if (filep !=0) {
rejected2 = (mystr2.find(reject_) != std::string::npos);
}
rejected = rejected1 || rejected2;
}
if (rejected) return false;
if (select_ == "*") return true;
// std::cout<<"input string = "<<mystr<<std::endl;
// std::cout<<"compare str = "<<select_<<std::endl;
// std::cout<<"found it = "<<( mystr.find(select_) != std::string::npos )<<std::endl;
bool pass1 = mystr.find(select_) != std::string::npos ;
bool pass2=false;
if ( filep!=0) {
pass2 = (mystr2.find(select_) != std::string::npos);
}
return pass1 || pass2;
}
FileHolder::FileHolder() {}
FileHolder::~FileHolder() {
files_.clear();
}