ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/HistHolder.cxx
Revision: 1.2
Committed: Thu Jan 28 17:27:47 2010 UTC (15 years, 3 months ago) by joshmt
Content type: text/plain
Branch: MAIN
Changes since 1.1: +20 -12 lines
Log Message:
previous version was not actually working. this is the simple, working version

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     #include <map>
17     #include <string>
18     #include <TString.h>
19     #include <TH1.h>
20     #include <TH2.h>
21    
22     #define DUMMYSTR "ignoreme123"
23    
24     /*
25     to do:
26     all sort of protections are needed...
27     */
28    
29     class HistHolder {
30     public :
31    
32     HistHolder();
33     virtual ~HistHolder();
34    
35     //convention is to use lowercase for methods that are custom to this class
36     //and uppercase for those that share a name with a normal ROOT method
37    
38     void make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max);
39     void make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,std::string xtitle);
40     void make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,
41     std::string xtitle, std::string ytitle);
42    
43     void make2(std::string name, std::string title, Int_t nx, Double_t minx, Double_t maxx,
44     Int_t ny, Double_t miny, Double_t maxy);
45    
46     //load histograms from files
47     //note that if the file is closed, the histo pointer may go away
48 joshmt 1.2 //void load(TString name, const TFile* file);
49     // void load2(TString name, const TFile* file);
50 joshmt 1.1
51     //select a group of histograms for the next operation
52     void select(std::string name) {select_=name;}
53    
54     //these methods operate on the selected histograms
55     //use * to work with all
56     void Write();
57     void Sumw2();
58     void SetMaximum(Double_t max);
59    
60     TH1F* operator[](std::string name) {return histHolder_[name]; }
61    
62     TH1F* find(std::string name) {return histHolder_[name];}
63     TH2F* find2(std::string name) {return histHolder2_[name];}
64    
65     private :
66     std::map< std::string, TH1F*> histHolder_;
67     std::map< std::string, TH2F*> histHolder2_;
68 joshmt 1.2
69     //std::map< std::pair< std::string, TFile*>, TH1F* > histHolderP_;
70    
71 joshmt 1.1 std::string select_;
72     };
73    
74     HistHolder::HistHolder() :
75     select_("")
76     {
77    
78     }
79    
80     HistHolder::~HistHolder()
81     {
82     /* causes a crash
83     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i )
84     delete i->second;
85     */
86     }
87    
88     void
89     HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max) {
90    
91     TH1F* hist = new TH1F(name.c_str(),title.c_str(),nbins,min,max);
92     histHolder_[name] = hist;
93    
94     }
95    
96     void
97     HistHolder::make2(std::string name, std::string title, Int_t nx, Double_t minx, Double_t maxx,
98     Int_t ny, Double_t miny, Double_t maxy) {
99     TH2F* hist = new TH2F(name.c_str(),title.c_str(),nx,minx,maxx,ny,miny,maxy);
100     histHolder2_[name] = hist;
101     }
102    
103 joshmt 1.2 // void
104     // HistHolder::load(TString name, const TFile* file) {
105    
106     // TH1F* hist = ((TH1F*)file->Get(name));
107     // histHolderP_[make_pair(std::string(name.Data()),file)] = hist;
108 joshmt 1.1
109 joshmt 1.2 // }
110 joshmt 1.1
111 joshmt 1.2 // void
112     // HistHolder::load2(TString name, const TFile* file) {
113 joshmt 1.1
114 joshmt 1.2 // TH2F* hist = ((TH2F*)file->Get(name));
115     // histHolder2_[std::string(name.Data())] = hist;
116 joshmt 1.1
117 joshmt 1.2 // }
118 joshmt 1.1
119     void
120     HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max, std::string xtitle) {
121    
122     make(name,title,nbins,min,max);
123     histHolder_[name]->SetXTitle(xtitle.c_str());
124     }
125    
126     void
127     HistHolder::make(std::string name, std::string title, Int_t nbins, Double_t min, Double_t max,
128     std::string xtitle, std::string ytitle) {
129    
130     make(name,title,nbins,min,max,xtitle);
131     histHolder_[name]->SetYTitle(ytitle.c_str());
132     }
133    
134     void
135     HistHolder::Write() {
136     //FIXME need to add histHolder2
137     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
138     if ( select_ == "*" || i->first.find(select_)!=std::string::npos)
139     i->second->Write();
140     }
141    
142     }
143    
144     void HistHolder::Sumw2() {
145    
146     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
147     if ( select_ == "*" || i->first.find(select_)!=std::string::npos)
148     i->second->Sumw2();
149     }
150     }
151    
152     void HistHolder::SetMaximum( Double_t max) {
153     for ( std::map< std::string, TH1F*>::const_iterator i = histHolder_.begin() ; i!= histHolder_.end() ; ++i ) {
154     if ( select_ == "*" || i->first.find(select_)!=std::string::npos)
155     i->second->SetMaximum(max);
156     }
157    
158     }