ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/HistHolder.cxx
Revision: 1.4
Committed: Fri Feb 5 12:25:51 2010 UTC (15 years, 2 months ago) by joshmt
Content type: text/plain
Branch: MAIN
CVS Tags: legacy20100329
Changes since 1.3: +17 -1 lines
Log Message:
add reset() for running repeatedly in a root session

File Contents

# User Rev Content
1 joshmt 1.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 joshmt 1.2 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 joshmt 1.1 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 joshmt 1.3
17     #include <iostream>
18 joshmt 1.1 #include <map>
19     #include <string>
20     #include <TString.h>
21     #include <TH1.h>
22     #include <TH2.h>
23 joshmt 1.3 #include <TFile.h>
24 joshmt 1.1
25     /*
26     to do:
27     all sort of protections are needed...
28 joshmt 1.3
29     also, i should make sure that all interfaces accept all 3 possibilites:
30     const char* ("hello world")
31     std::string
32     TString
33 joshmt 1.1 */
34    
35     class HistHolder {
36     public :
37    
38     HistHolder();
39     virtual ~HistHolder();
40    
41     //convention is to use lowercase for methods that are custom to this class
42     //and uppercase for those that share a name with a normal ROOT method
43    
44     void make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max);
45     void make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,std::string xtitle);
46     void make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,
47     std::string xtitle, std::string ytitle);
48    
49     void make2(std::string name, std::string title, Int_t nx, Double_t minx, Double_t maxx,
50     Int_t ny, Double_t miny, Double_t maxy);
51    
52     //load histograms from files
53     //note that if the file is closed, the histo pointer may go away
54 joshmt 1.3 void load(TString name, TFile* file);
55     // void load2(TString name, TFile* file);
56 joshmt 1.1
57     //select a group of histograms for the next operation
58 joshmt 1.3 void select(const std::string str) {select_=str;}
59     void reject(const std::string str) {reject_=str;}
60 joshmt 1.1
61     //these methods operate on the selected histograms
62 joshmt 1.3 //use * to work with all (default)
63    
64     void normalize(); //scale to unit area
65 joshmt 1.1 void Write();
66     void Sumw2();
67 joshmt 1.3 void SetMinimum(Double_t min);
68 joshmt 1.1 void SetMaximum(Double_t max);
69 joshmt 1.3 Float_t GetMaximum();
70 joshmt 1.1
71     TH1F* operator[](std::string name) {return histHolder_[name]; }
72    
73     TH1F* find(std::string name) {return histHolder_[name];}
74 joshmt 1.3 TH1F* find(std::string name, TFile* file) {return histHolderP_[make_pair(name,file)];}
75 joshmt 1.1 TH2F* find2(std::string name) {return histHolder2_[name];}
76 joshmt 1.3
77     TH1F* find(TString name) {return find(std::string(name.Data()));}
78     TH1F* find(TString name, TFile* file) {return find( std::string(name.Data()),file); }
79     TH2F* find2(TString name) {return find2( std::string(name.Data()));}
80 joshmt 1.4
81     TH1F* find(const char* name) {return find(std::string(name));}
82     TH1F* find(const char* name, TFile* file) {return find( std::string(name),file); }
83     TH2F* find2(const char* name) {return find2( std::string(name));}
84 joshmt 1.3
85     void print();
86 joshmt 1.4 void reset();
87 joshmt 1.3
88 joshmt 1.1 private :
89 joshmt 1.3 bool passesFilter(const std::string mystr, TFile* filep=0);
90     bool passesFilter( std::pair<std::string , TFile*> mypair) {return passesFilter( mypair.first,mypair.second);}
91    
92 joshmt 1.1 std::map< std::string, TH1F*> histHolder_;
93     std::map< std::string, TH2F*> histHolder2_;
94 joshmt 1.2
95 joshmt 1.3 std::map< std::pair< std::string, TFile*>, TH1F* > histHolderP_;
96 joshmt 1.2
97 joshmt 1.1 std::string select_;
98 joshmt 1.3 std::string reject_;
99 joshmt 1.1 };
100    
101     HistHolder::HistHolder() :
102 joshmt 1.3 select_("*"),
103     reject_("")
104 joshmt 1.1 {
105    
106     }
107    
108     HistHolder::~HistHolder()
109     {
110     /* causes a crash
111     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i )
112     delete i->second;
113     */
114 joshmt 1.4 reset();
115     }
116    
117     void HistHolder::reset() {
118     std::cout<<"HistHolder reset()"<<std::endl;
119     histHolder_.clear();
120     histHolder2_.clear();
121     histHolderP_.clear();
122 joshmt 1.1 }
123    
124     void
125 joshmt 1.3 HistHolder::print() {
126    
127     std::cout<<"histHolder_ size = "<<histHolder_.size()<<std::endl;
128     std::cout<<"histHolder2_ size = "<<histHolder2_.size()<<std::endl;
129     std::cout<<"histHolderP_ size = "<<histHolderP_.size()<<std::endl;
130    
131     }
132    
133     void
134 joshmt 1.1 HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max) {
135    
136     TH1F* hist = new TH1F(name.c_str(),title.c_str(),nbins,min,max);
137     histHolder_[name] = hist;
138    
139     }
140    
141     void
142     HistHolder::make2(std::string name, std::string title, Int_t nx, Double_t minx, Double_t maxx,
143     Int_t ny, Double_t miny, Double_t maxy) {
144     TH2F* hist = new TH2F(name.c_str(),title.c_str(),nx,minx,maxx,ny,miny,maxy);
145     histHolder2_[name] = hist;
146     }
147    
148 joshmt 1.3 void
149     HistHolder::load(TString name, TFile* file) {
150 joshmt 1.2
151 joshmt 1.3 TH1F* hist = ((TH1F*)file->Get(name));
152     histHolderP_[make_pair(std::string(name.Data()),file)] = hist;
153 joshmt 1.1
154 joshmt 1.3 }
155 joshmt 1.1
156 joshmt 1.2 // void
157     // HistHolder::load2(TString name, const TFile* file) {
158 joshmt 1.1
159 joshmt 1.2 // TH2F* hist = ((TH2F*)file->Get(name));
160     // histHolder2_[std::string(name.Data())] = hist;
161 joshmt 1.1
162 joshmt 1.2 // }
163 joshmt 1.1
164     void
165     HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max, std::string xtitle) {
166    
167     make(name,title,nbins,min,max);
168     histHolder_[name]->SetXTitle(xtitle.c_str());
169     }
170    
171     void
172     HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,
173     std::string xtitle, std::string ytitle) {
174    
175     make(name,title,nbins,min,max,xtitle);
176     histHolder_[name]->SetYTitle(ytitle.c_str());
177     }
178    
179     void
180     HistHolder::Write() {
181     //FIXME need to add histHolder2
182     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
183 joshmt 1.3 if ( passesFilter( i->first))
184 joshmt 1.1 i->second->Write();
185     }
186    
187     }
188    
189     void HistHolder::Sumw2() {
190    
191     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
192 joshmt 1.3 if ( passesFilter( i->first))
193 joshmt 1.1 i->second->Sumw2();
194 joshmt 1.3 }
195    
196     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
197     i!= histHolderP_.end() ; ++i ) {
198    
199     if ( passesFilter(i->first))
200     i->second->Sumw2();
201     }
202    
203 joshmt 1.1 }
204    
205     void HistHolder::SetMaximum( Double_t max) {
206     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
207 joshmt 1.3 if ( passesFilter(i->first))
208 joshmt 1.1 i->second->SetMaximum(max);
209     }
210    
211 joshmt 1.3 for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
212     i!= histHolderP_.end() ; ++i ) {
213     if ( passesFilter( i->first))
214     i->second->SetMaximum(max);
215     }
216    
217     }
218    
219     void HistHolder::SetMinimum( Double_t min) {
220     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
221     if ( passesFilter(i->first))
222     i->second->SetMinimum(min);
223     }
224    
225     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
226     i!= histHolderP_.end() ; ++i ) {
227     if ( passesFilter(i->first))
228     i->second->SetMinimum(min);
229     }
230    
231     }
232    
233     Float_t HistHolder::GetMaximum() {
234    
235     Float_t max = -99999999;
236    
237     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
238     if ( passesFilter(i->first)) {
239     if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
240     }
241     }
242    
243     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
244     i!= histHolderP_.end() ; ++i ) {
245     if ( passesFilter(i->first)) {
246     if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
247     }
248     }
249    
250     return max;
251     }
252    
253     void HistHolder::normalize() {
254     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
255     if ( passesFilter(i->first)) {
256     double factor = i->second->Integral();
257     i->second->Scale(1.0/factor);
258     }
259     }
260    
261     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
262     i!= histHolderP_.end() ; ++i ) {
263     if ( passesFilter( i->first)) {
264     double factor = i->second->Integral();
265     i->second->Scale(1.0/factor);
266     }
267     }
268    
269 joshmt 1.1 }
270 joshmt 1.3
271     // utility functions
272    
273     bool HistHolder::passesFilter(const std::string mystr, TFile* filep) {
274    
275     //if reject_ is empty, then reject nothing
276     //if select_ is empty, then accept everything
277     if (select_=="") select_="*";
278    
279     std::string mystr2="";
280     if (filep!=0) mystr2 = std::string(filep->GetName());
281    
282     bool rejected=false;
283     if (reject_ != "") {
284     bool rejected1 = (mystr.find(reject_) != std::string::npos);
285     bool rejected2=false;
286     if (filep !=0) {
287     rejected2 = (mystr2.find(reject_) != std::string::npos);
288     }
289     rejected = rejected1 || rejected2;
290     }
291    
292     if (rejected) return false;
293    
294     if (select_ == "*") return true;
295    
296     // std::cout<<"input string = "<<mystr<<std::endl;
297     // std::cout<<"compare str = "<<select_<<std::endl;
298     // std::cout<<"found it = "<<( mystr.find(select_) != std::string::npos )<<std::endl;
299    
300     bool pass1 = mystr.find(select_) != std::string::npos ;
301     bool pass2=false;
302     if ( filep!=0) {
303     pass2 = (mystr2.find(select_) != std::string::npos);
304     }
305    
306     return pass1 || pass2;
307    
308     }
309    
310     // an auxilliary class
311     // problem = in CINT I cannot use a std::vector<TFile*>
312     // this is supposed to be an easy solution, basically just a minimal interface to std::vector
313    
314     class FileHolder {
315     public :
316     FileHolder();
317     virtual ~FileHolder();
318    
319     void add(TFile* filep) {files_.push_back(filep);}
320     unsigned int size() {return files_.size();}
321    
322     TFile* at(unsigned int n) { return files_.at(n); }
323    
324     private :
325    
326     std::vector<TFile*> files_;
327    
328     };
329    
330     FileHolder::FileHolder() {}
331    
332 joshmt 1.4 FileHolder::~FileHolder() {
333     files_.clear();
334    
335     }