ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/HistHolder.cxx
Revision: 1.7
Committed: Wed Aug 25 10:15:27 2010 UTC (14 years, 8 months ago) by joshmt
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +21 -21 lines
Log Message:
change all TH1F to TH1D

File Contents

# Content
1 /*
2 Goal of this class is to make a class to hold a number of histograms
3
4 The base data structure is a map using a string (the name of the histogram)
5 as a key, that references to a pointer that holds the histogram
6
7 We also have a secondary data structure to map by both string and
8 TFile*, to allow this class to hold a bunch of histograms with the
9 same name
10
11 Utilities should be provided that call Sumw2() and do other useful things
12 Creating a histogram should be as easy calling a function and passing the name, title, and
13 other standard arguments
14
15 */
16
17 #include <iostream>
18
19 #include "HistHolder.h"
20
21 HistHolder::HistHolder() :
22 select_("*"),
23 reject_("")
24 {
25
26 }
27
28 HistHolder::~HistHolder()
29 {
30 /* causes a crash
31 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i )
32 delete i->second;
33 */
34 reset();
35 }
36
37 void HistHolder::reset() {
38 // std::cout<<"HistHolder reset()"<<std::endl;
39 histHolder_.clear();
40 histHolder2_.clear();
41 histHolderP_.clear();
42 histHolderP2_.clear();
43 }
44
45 void
46 HistHolder::Print(TString opt) {
47
48 opt.ToUpper();
49 if (opt=="V") {
50 std::cout<<"histHolder_ size = "<<histHolder_.size()<<std::endl;
51 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
52 std::cout<<i->first<<"\t"<<i->second<<std::endl;
53 }
54 std::cout<<"histHolder2_ size = "<<histHolder2_.size()<<std::endl;
55 for ( std::map< std::string, TH2F*>::const_iterator i = histHolder2_.begin() ; i!= histHolder2_.end() ; ++i ) {
56 std::cout<<i->first<<"\t"<<i->second<<std::endl;
57 }
58 std::cout<<"histHolderP_ size = "<<histHolderP_.size()<<std::endl;
59 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ; i!= histHolderP_.end() ; ++i ) {
60 std::cout<<i->first.first<<" ; "<<i->first.second<<"\t"<<i->second<<std::endl;
61 }
62 std::cout<<"histHolderP2_ size = "<<histHolderP2_.size()<<std::endl;
63 for ( std::map< std::pair<std::string, TFile*>, TH2F*>::const_iterator i = histHolderP2_.begin() ; i!= histHolderP2_.end() ; ++i ) {
64 std::cout<<i->first.first<<" ; "<<i->first.second<<"\t"<<i->second<<std::endl;
65 }
66
67 }
68 else {
69 std::cout<<"histHolder_ size = "<<histHolder_.size()<<std::endl;
70 std::cout<<"histHolder2_ size = "<<histHolder2_.size()<<std::endl;
71 std::cout<<"histHolderP_ size = "<<histHolderP_.size()<<std::endl;
72 std::cout<<"histHolderP2_ size = "<<histHolderP2_.size()<<std::endl;
73 }
74
75 }
76
77 void HistHolder::Reset() {
78
79
80 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
81 i->second->Reset();
82 }
83
84 for ( std::map< std::string, TH2F*>::const_iterator i = histHolder2_.begin() ; i!= histHolder2_.end() ; ++i ) {
85 i->second->Reset();
86 }
87
88 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ; i!= histHolderP_.end() ; ++i ) {
89 i->second->Reset();
90 }
91
92 for ( std::map< std::pair<std::string, TFile*>, TH2F*>::const_iterator i = histHolderP2_.begin() ; i!= histHolderP2_.end() ; ++i ) {
93 i->second->Reset();
94 }
95
96 }
97
98 void
99 HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max) {
100
101 TH1D* hist = new TH1D(name.c_str(),title.c_str(),nbins,min,max);
102 histHolder_[name] = hist;
103
104 }
105
106 void
107 HistHolder::make2(std::string name, std::string title, Int_t nx, Double_t minx, Double_t maxx,
108 Int_t ny, Double_t miny, Double_t maxy) {
109 TH2F* hist = new TH2F(name.c_str(),title.c_str(),nx,minx,maxx,ny,miny,maxy);
110 histHolder2_[name] = hist;
111 }
112
113 Int_t
114 HistHolder::load(TString name, TFile* file) {
115
116 Int_t code=0;
117
118 TObject* h = file->Get(name);
119 if (h==0) {std::cout<<"Problem loading "<<name<<std::endl; return code;}
120
121 if ( h->InheritsFrom("TH2F") ) {
122 histHolderP2_[make_pair(std::string(name.Data()),file)] = (TH2F*) h;
123 code=2;
124 }
125 else if ( h->InheritsFrom("TH1D") ) {
126 histHolderP_[make_pair(std::string(name.Data()),file)] = (TH1D*) h;
127 code=1;
128 }
129 else {
130 std::cout<<"Could not determine type of histogram "<<name<<std::endl;
131 }
132
133 return code;
134 }
135
136 // void
137 // HistHolder::load2(TString name, const TFile* file) {
138
139 // TH2F* hist = ((TH2F*)file->Get(name));
140 // histHolder2_[std::string(name.Data())] = hist;
141
142 // }
143
144 void
145 HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max, std::string xtitle) {
146
147 make(name,title,nbins,min,max);
148 histHolder_[name]->SetXTitle(xtitle.c_str());
149 }
150
151 void
152 HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,
153 std::string xtitle, std::string ytitle) {
154
155 make(name,title,nbins,min,max,xtitle);
156 histHolder_[name]->SetYTitle(ytitle.c_str());
157 }
158
159 void
160 HistHolder::Write() {
161 //FIXME need to add histHolder2 and other histHolders
162 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
163 if ( passesFilter( i->first))
164 i->second->Write();
165 }
166
167 }
168
169 void HistHolder::Sumw2() {
170
171 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
172 if ( passesFilter( i->first))
173 i->second->Sumw2();
174 }
175
176 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
177 i!= histHolderP_.end() ; ++i ) {
178
179 if ( passesFilter(i->first))
180 i->second->Sumw2();
181 }
182
183 }
184
185 void HistHolder::SetMaximum( Double_t max) {
186 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
187 if ( passesFilter(i->first))
188 i->second->SetMaximum(max);
189 }
190
191 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
192 i!= histHolderP_.end() ; ++i ) {
193 if ( passesFilter( i->first))
194 i->second->SetMaximum(max);
195 }
196
197 }
198
199 void HistHolder::SetMinimum( Double_t min) {
200 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
201 if ( passesFilter(i->first))
202 i->second->SetMinimum(min);
203 }
204
205 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
206 i!= histHolderP_.end() ; ++i ) {
207 if ( passesFilter(i->first))
208 i->second->SetMinimum(min);
209 }
210
211 }
212
213 Float_t HistHolder::GetMaximum() {
214
215 Float_t max = -99999999;
216
217 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
218 if ( passesFilter(i->first)) {
219 if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
220 }
221 }
222
223 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
224 i!= histHolderP_.end() ; ++i ) {
225 if ( passesFilter(i->first)) {
226 if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
227 }
228 }
229
230 return max;
231 }
232
233 TString HistHolder::getMaximumName() {
234
235 //most code copied from GetMaximum()
236 Float_t max = -99999999;
237 TString nameofmax="";
238
239 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
240 if ( passesFilter(i->first)) {
241 if ( i->second->GetMaximum() > max ) {
242 max = i->second->GetMaximum();
243 nameofmax = TString(i->first);
244 }
245 }
246 }
247
248 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
249 i!= histHolderP_.end() ; ++i ) {
250 if ( passesFilter(i->first)) {
251 if ( i->second->GetMaximum() > max ) {
252 max = i->second->GetMaximum();
253 nameofmax = TString(i->first.first);
254 }
255 }
256 }
257
258 return nameofmax;
259 }
260
261 void HistHolder::normalize() {
262 for ( std::map< std::string, TH1D*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
263 if ( passesFilter(i->first)) {
264 double factor = i->second->Integral();
265 i->second->Scale(1.0/factor);
266 }
267 }
268
269 for ( std::map< std::pair<std::string, TFile*>, TH1D*>::const_iterator i = histHolderP_.begin() ;
270 i!= histHolderP_.end() ; ++i ) {
271 if ( passesFilter( i->first)) {
272 double factor = i->second->Integral();
273 i->second->Scale(1.0/factor);
274 }
275 }
276
277 }
278
279 // utility functions
280
281 bool HistHolder::passesFilter(const std::string mystr, TFile* filep) {
282
283 //if reject_ is empty, then reject nothing
284 //if select_ is empty, then accept everything
285 if (select_=="") select_="*";
286
287 std::string mystr2="";
288 if (filep!=0) mystr2 = std::string(filep->GetName());
289
290 bool rejected=false;
291 if (reject_ != "") {
292 bool rejected1 = (mystr.find(reject_) != std::string::npos);
293 bool rejected2=false;
294 if (filep !=0) {
295 rejected2 = (mystr2.find(reject_) != std::string::npos);
296 }
297 rejected = rejected1 || rejected2;
298 }
299
300 if (rejected) return false;
301
302 if (select_ == "*") return true;
303
304 // std::cout<<"input string = "<<mystr<<std::endl;
305 // std::cout<<"compare str = "<<select_<<std::endl;
306 // std::cout<<"found it = "<<( mystr.find(select_) != std::string::npos )<<std::endl;
307
308 bool pass1 = mystr.find(select_) != std::string::npos ;
309 bool pass2=false;
310 if ( filep!=0) {
311 pass2 = (mystr2.find(select_) != std::string::npos);
312 }
313
314 return pass1 || pass2;
315
316 }
317
318
319 FileHolder::FileHolder() {}
320
321 FileHolder::~FileHolder() {
322 files_.clear();
323
324 }