ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/HistHolder.cxx
Revision: 1.3
Committed: Mon Feb 1 12:41:36 2010 UTC (15 years, 2 months ago) by joshmt
Content type: text/plain
Branch: MAIN
Changes since 1.2: +179 -18 lines
Log Message:
major update. tested and working.

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    
81     void print();
82    
83 joshmt 1.1 private :
84 joshmt 1.3 bool passesFilter(const std::string mystr, TFile* filep=0);
85     bool passesFilter( std::pair<std::string , TFile*> mypair) {return passesFilter( mypair.first,mypair.second);}
86    
87 joshmt 1.1 std::map< std::string, TH1F*> histHolder_;
88     std::map< std::string, TH2F*> histHolder2_;
89 joshmt 1.2
90 joshmt 1.3 std::map< std::pair< std::string, TFile*>, TH1F* > histHolderP_;
91 joshmt 1.2
92 joshmt 1.1 std::string select_;
93 joshmt 1.3 std::string reject_;
94 joshmt 1.1 };
95    
96     HistHolder::HistHolder() :
97 joshmt 1.3 select_("*"),
98     reject_("")
99 joshmt 1.1 {
100    
101     }
102    
103     HistHolder::~HistHolder()
104     {
105     /* causes a crash
106     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i )
107     delete i->second;
108     */
109     }
110    
111     void
112 joshmt 1.3 HistHolder::print() {
113    
114     std::cout<<"histHolder_ size = "<<histHolder_.size()<<std::endl;
115     std::cout<<"histHolder2_ size = "<<histHolder2_.size()<<std::endl;
116     std::cout<<"histHolderP_ size = "<<histHolderP_.size()<<std::endl;
117    
118     }
119    
120     void
121 joshmt 1.1 HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max) {
122    
123     TH1F* hist = new TH1F(name.c_str(),title.c_str(),nbins,min,max);
124     histHolder_[name] = hist;
125    
126     }
127    
128     void
129     HistHolder::make2(std::string name, std::string title, Int_t nx, Double_t minx, Double_t maxx,
130     Int_t ny, Double_t miny, Double_t maxy) {
131     TH2F* hist = new TH2F(name.c_str(),title.c_str(),nx,minx,maxx,ny,miny,maxy);
132     histHolder2_[name] = hist;
133     }
134    
135 joshmt 1.3 void
136     HistHolder::load(TString name, TFile* file) {
137 joshmt 1.2
138 joshmt 1.3 TH1F* hist = ((TH1F*)file->Get(name));
139     histHolderP_[make_pair(std::string(name.Data()),file)] = hist;
140 joshmt 1.1
141 joshmt 1.3 }
142 joshmt 1.1
143 joshmt 1.2 // void
144     // HistHolder::load2(TString name, const TFile* file) {
145 joshmt 1.1
146 joshmt 1.2 // TH2F* hist = ((TH2F*)file->Get(name));
147     // histHolder2_[std::string(name.Data())] = hist;
148 joshmt 1.1
149 joshmt 1.2 // }
150 joshmt 1.1
151     void
152     HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max, std::string xtitle) {
153    
154     make(name,title,nbins,min,max);
155     histHolder_[name]->SetXTitle(xtitle.c_str());
156     }
157    
158     void
159     HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,
160     std::string xtitle, std::string ytitle) {
161    
162     make(name,title,nbins,min,max,xtitle);
163     histHolder_[name]->SetYTitle(ytitle.c_str());
164     }
165    
166     void
167     HistHolder::Write() {
168     //FIXME need to add histHolder2
169     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
170 joshmt 1.3 if ( passesFilter( i->first))
171 joshmt 1.1 i->second->Write();
172     }
173    
174     }
175    
176     void HistHolder::Sumw2() {
177    
178     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
179 joshmt 1.3 if ( passesFilter( i->first))
180 joshmt 1.1 i->second->Sumw2();
181 joshmt 1.3 }
182    
183     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
184     i!= histHolderP_.end() ; ++i ) {
185    
186     if ( passesFilter(i->first))
187     i->second->Sumw2();
188     }
189    
190 joshmt 1.1 }
191    
192     void HistHolder::SetMaximum( Double_t max) {
193     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
194 joshmt 1.3 if ( passesFilter(i->first))
195 joshmt 1.1 i->second->SetMaximum(max);
196     }
197    
198 joshmt 1.3 for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
199     i!= histHolderP_.end() ; ++i ) {
200     if ( passesFilter( i->first))
201     i->second->SetMaximum(max);
202     }
203    
204     }
205    
206     void HistHolder::SetMinimum( Double_t min) {
207     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
208     if ( passesFilter(i->first))
209     i->second->SetMinimum(min);
210     }
211    
212     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
213     i!= histHolderP_.end() ; ++i ) {
214     if ( passesFilter(i->first))
215     i->second->SetMinimum(min);
216     }
217    
218     }
219    
220     Float_t HistHolder::GetMaximum() {
221    
222     Float_t max = -99999999;
223    
224     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
225     if ( passesFilter(i->first)) {
226     if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
227     }
228     }
229    
230     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
231     i!= histHolderP_.end() ; ++i ) {
232     if ( passesFilter(i->first)) {
233     if ( i->second->GetMaximum() > max ) max = i->second->GetMaximum();
234     }
235     }
236    
237     return max;
238     }
239    
240     void HistHolder::normalize() {
241     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
242     if ( passesFilter(i->first)) {
243     double factor = i->second->Integral();
244     i->second->Scale(1.0/factor);
245     }
246     }
247    
248     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
249     i!= histHolderP_.end() ; ++i ) {
250     if ( passesFilter( i->first)) {
251     double factor = i->second->Integral();
252     i->second->Scale(1.0/factor);
253     }
254     }
255    
256 joshmt 1.1 }
257 joshmt 1.3
258     // utility functions
259    
260     bool HistHolder::passesFilter(const std::string mystr, TFile* filep) {
261    
262     //if reject_ is empty, then reject nothing
263     //if select_ is empty, then accept everything
264     if (select_=="") select_="*";
265    
266     std::string mystr2="";
267     if (filep!=0) mystr2 = std::string(filep->GetName());
268    
269     bool rejected=false;
270     if (reject_ != "") {
271     bool rejected1 = (mystr.find(reject_) != std::string::npos);
272     bool rejected2=false;
273     if (filep !=0) {
274     rejected2 = (mystr2.find(reject_) != std::string::npos);
275     }
276     rejected = rejected1 || rejected2;
277     }
278    
279     if (rejected) return false;
280    
281     if (select_ == "*") return true;
282    
283     // std::cout<<"input string = "<<mystr<<std::endl;
284     // std::cout<<"compare str = "<<select_<<std::endl;
285     // std::cout<<"found it = "<<( mystr.find(select_) != std::string::npos )<<std::endl;
286    
287     bool pass1 = mystr.find(select_) != std::string::npos ;
288     bool pass2=false;
289     if ( filep!=0) {
290     pass2 = (mystr2.find(select_) != std::string::npos);
291     }
292    
293     return pass1 || pass2;
294    
295     }
296    
297     // an auxilliary class
298     // problem = in CINT I cannot use a std::vector<TFile*>
299     // this is supposed to be an easy solution, basically just a minimal interface to std::vector
300    
301     class FileHolder {
302     public :
303     FileHolder();
304     virtual ~FileHolder();
305    
306     void add(TFile* filep) {files_.push_back(filep);}
307     unsigned int size() {return files_.size();}
308    
309     TFile* at(unsigned int n) { return files_.at(n); }
310    
311     private :
312    
313     std::vector<TFile*> files_;
314    
315     };
316    
317     FileHolder::FileHolder() {}
318    
319     FileHolder::~FileHolder() {}