ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/HistHolder.cxx
Revision: 1.6
Committed: Fri Apr 23 16:41:12 2010 UTC (15 years ago) by joshmt
Content type: text/plain
Branch: MAIN
Changes since 1.5: +21 -0 lines
Log Message:
add histo reset etc

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.5
19     #include "HistHolder.h"
20 joshmt 1.1
21     HistHolder::HistHolder() :
22 joshmt 1.3 select_("*"),
23     reject_("")
24 joshmt 1.1 {
25    
26     }
27    
28     HistHolder::~HistHolder()
29     {
30     /* causes a crash
31     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i )
32     delete i->second;
33     */
34 joshmt 1.4 reset();
35     }
36    
37     void HistHolder::reset() {
38 joshmt 1.5 // std::cout<<"HistHolder reset()"<<std::endl;
39 joshmt 1.4 histHolder_.clear();
40     histHolder2_.clear();
41     histHolderP_.clear();
42 joshmt 1.5 histHolderP2_.clear();
43 joshmt 1.1 }
44    
45     void
46 joshmt 1.5 HistHolder::Print(TString opt) {
47 joshmt 1.3
48 joshmt 1.5 opt.ToUpper();
49     if (opt=="V") {
50     std::cout<<"histHolder_ size = "<<histHolder_.size()<<std::endl;
51     for ( std::map< std::string, TH1F*>::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*>, TH1F*>::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 joshmt 1.3 }
76    
77 joshmt 1.6 void HistHolder::Reset() {
78    
79    
80     for ( std::map< std::string, TH1F*>::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*>, TH1F*>::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 joshmt 1.3 void
99 joshmt 1.1 HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max) {
100    
101     TH1F* hist = new TH1F(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 joshmt 1.5 Int_t
114 joshmt 1.3 HistHolder::load(TString name, TFile* file) {
115 joshmt 1.2
116 joshmt 1.5 Int_t code=0;
117 joshmt 1.1
118 joshmt 1.5 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("TH1F") ) {
126     histHolderP_[make_pair(std::string(name.Data()),file)] = (TH1F*) 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 joshmt 1.3 }
135 joshmt 1.1
136 joshmt 1.2 // void
137     // HistHolder::load2(TString name, const TFile* file) {
138 joshmt 1.1
139 joshmt 1.2 // TH2F* hist = ((TH2F*)file->Get(name));
140     // histHolder2_[std::string(name.Data())] = hist;
141 joshmt 1.1
142 joshmt 1.2 // }
143 joshmt 1.1
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 joshmt 1.5 //FIXME need to add histHolder2 and other histHolders
162 joshmt 1.1 for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
163 joshmt 1.3 if ( passesFilter( i->first))
164 joshmt 1.1 i->second->Write();
165     }
166    
167     }
168    
169     void HistHolder::Sumw2() {
170    
171     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
172 joshmt 1.3 if ( passesFilter( i->first))
173 joshmt 1.1 i->second->Sumw2();
174 joshmt 1.3 }
175    
176     for ( std::map< std::pair<std::string, TFile*>, TH1F*>::const_iterator i = histHolderP_.begin() ;
177     i!= histHolderP_.end() ; ++i ) {
178    
179     if ( passesFilter(i->first))
180     i->second->Sumw2();
181     }
182    
183 joshmt 1.1 }
184    
185     void HistHolder::SetMaximum( Double_t max) {
186     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
187 joshmt 1.3 if ( passesFilter(i->first))
188 joshmt 1.1 i->second->SetMaximum(max);
189     }
190    
191 joshmt 1.3 for ( std::map< std::pair<std::string, TFile*>, TH1F*>::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, TH1F*>::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*>, TH1F*>::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, TH1F*>::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*>, TH1F*>::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 joshmt 1.5 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, TH1F*>::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*>, TH1F*>::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 joshmt 1.3 void HistHolder::normalize() {
262     for ( std::map< std::string, TH1F*>::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*>, TH1F*>::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 joshmt 1.1 }
278 joshmt 1.3
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 joshmt 1.4 FileHolder::~FileHolder() {
322     files_.clear();
323    
324     }