ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/histtools.C
(Generate patch)

Comparing UserCode/FGolf/Tools/histtools.C (file contents):
Revision 1.2 by fgolf, Thu May 26 03:34:52 2011 UTC vs.
Revision 1.3 by fgolf, Fri Jul 8 09:16:48 2011 UTC

# Line 6 | Line 6
6   #include "TKey.h"
7   #include "TLegend.h"
8   #include "TRegexp.h"
9 + #include "histtools.h"
10 + #include <TIterator.h>
11  
12   #include <iostream>
13  
# Line 13 | Line 15 | using namespace std;
15  
16   namespace hist {
17  
18 <     void add(const char* outHistName, const char* patORpfx);
17 <
18 <   //Add all histograms whose names match one of ten possible regular expression
19 <   //patterns or begin with one of ten possible given prefixes.  Feel free to
20 <   //mix and match regular expression patterns and prefixes.  If the final hist-
21 <   //ogram named outHistName does not exist it is created.
22 <
23 <   void add(const char* outHistName, const char* patORpfx0, const char* patORpfx1, const char* patORpfx2 = 0, const char* patORpfx3 = 0, const char* patORpfx4 = 0, const char* patORpfx5 = 0, const char* patORpfx6 = 0, const char* patORpfx7 = 0, const char* patORpfx8 = 0, const char* patORpfx9 = 0)
24 <   {
25 <      add(outHistName, patORpfx0);
26 <      add(outHistName, patORpfx1);
27 <      if (patORpfx2) add(outHistName, patORpfx2);
28 <      if (patORpfx3) add(outHistName, patORpfx3);
29 <      if (patORpfx4) add(outHistName, patORpfx4);
30 <      if (patORpfx5) add(outHistName, patORpfx5);
31 <      if (patORpfx6) add(outHistName, patORpfx6);
32 <      if (patORpfx7) add(outHistName, patORpfx7);
33 <      if (patORpfx8) add(outHistName, patORpfx8);
34 <      if (patORpfx9) add(outHistName, patORpfx9);
35 <   }
36 <
37 <   //Add all histograms whose names match the given regular expression pattern
38 <   //or begin with the given prefix.  If the final histogram named outHistName
39 <   //does not exist it is created.
40 <
41 <   void add(const char* outHistName, const char* patORpfx) {
42 <      TRegexp reg(patORpfx, kFALSE);
43 <
44 <      TList* list = gDirectory->GetList() ;
45 <      TIterator* iter = list->MakeIterator();
46 <
47 <      TObject* obj = 0;
48 <      TObject* hist = 0;
49 <      Bool_t makeOutHist = false;
50 <
51 <      hist = gDirectory->Get(outHistName);
52 <      //If out hist does not exist, remember to create it
53 <      if (! hist) makeOutHist = true;
54 <
55 <      while ( (obj = iter->Next()) ) {
56 <         if (! obj->InheritsFrom(TH1::Class())) continue;
57 <
58 <         TString name = obj->GetName();
59 <         //Don't add out hist
60 <         if (name == TString(outHistName)) continue;
61 <
62 <         if (TString(patORpfx).MaybeRegexp()) {
63 <            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
64 <         } else if (! name.BeginsWith(patORpfx)) continue;
65 <
66 <         if (makeOutHist) {
67 <            hist = obj->Clone(outHistName);
68 <
69 <            if (hist->InheritsFrom(TH2::Class()))
70 <               ((TH2*)hist)->Reset();
71 <            else
72 <               ((TH1*)hist)->Reset();
73 <
74 <            ((TH1*)hist)->SetTitle(outHistName);
75 <            ((TH1*)hist)->Sumw2();
76 <            makeOutHist = false;
77 <         }
78 <
79 <         ((TH1*)hist)->Add((TH1*)obj);
80 <      }
81 <   }
82 <
83 <   //For all histograms whose names match the given regular expression pattern
84 <   //or begin with the given prefix, set the fill, line and marker colors to the
85 <   //given value.
86 <
87 <   void color(const char* patORpfx, Color_t color) {
88 <      TRegexp reg(patORpfx, kFALSE);
89 <
90 <      TList* list = gDirectory->GetList() ;
91 <      if (!list) {
92 <        cout << "Failed to set color for " << patORpfx << endl;
93 <        return;
94 <      }
95 <      TIterator* iter = list->MakeIterator();
96 <
97 <      TObject* obj = 0;
98 <
99 <      while ( (obj = iter->Next()) ) {
100 <         if (! obj->InheritsFrom(TH1::Class())) continue;
101 <
102 <         TString name = obj->GetName();
103 <
104 <         if (TString(patORpfx).MaybeRegexp()) {
105 <            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
106 <         } else if (! name.BeginsWith(patORpfx)) continue;
107 <
108 <         ((TH1*)obj)->SetFillColor(color);
109 <         ((TH1*)obj)->SetLineColor(color);
110 <         ((TH1*)obj)->SetMarkerColor(color);
111 <      }
112 <   }
113 <
114 <   //Return a pointer to a TLegend with an entry for each histogram drawn on a
115 <   //given TCanvas.  Display either the line, point or fill values.  Optionally
116 <   //apply colors to all histograms.  By default, entry labels are the names of
117 <   //their respective histograms.  Optionally, if histogram names are of the
118 <   //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
119 <
120 <     TLegend* legend(TCanvas* canvas, Option_t* option = "lpf", Bool_t addColor = kFALSE, Int_t token = -1,
121 <                     Float_t xmin = 0.75, Float_t ymin = 0.75, Float_t xmax = 0.99, Float_t ymax = 0.99) {
122 <      if(! canvas) return 0;
123 <
124 <      TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
125 <      TList* list = canvas->GetListOfPrimitives();
126 <      TIterator* iter = list->MakeIterator();
127 <
128 <      TObject* obj = 0;
129 <
130 <      //Hist color iterator
131 <      Int_t colorIt = 1;
132 <
133 <      while ( (obj = iter->Next()) ) {
134 <         if (! obj->InheritsFrom(TH1::Class())) continue;
135 <
136 <         if (addColor) {
137 <            hist::color(obj->GetName(), colorIt);
138 <            ++colorIt;
139 <         }
140 <
141 <         if (token == -1)
142 <            leg->AddEntry(obj, obj->GetName(), option);
143 <         else {
144 <            TString name(obj->GetName());
145 <            TObjArray* a = name.Tokenize("_");
146 <            if (a->GetEntries() <= token)
147 <               leg->AddEntry(obj, obj->GetName(), option);
148 <            else
149 <               leg->AddEntry(obj, a->At(token)->GetName(), option);
150 <         }
151 <      }
152 <
153 <      return leg;
154 <   }
155 <
156 <   //Return a pointer to a TLegend with an entry for each histogram added to a
157 <   //given THStack.  Display either the line, point or fill values.  Optionally
158 <   //apply colors to all histograms.  By default, entry labels are the names of
159 <   //their respective histograms.  Optionally, if histogram names are of the
160 <   //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
161 <
162 <   TLegend* legend(THStack* stack, Option_t* option = "lpf", Bool_t addColor = kFALSE, Int_t token = -1,
163 <                   Float_t xmin = 0.75, Float_t ymin = 0.75, Float_t xmax = 0.99, Float_t ymax = 0.99) {
164 <      if(! stack) return 0;
165 <
166 <      TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
167 <      TList* list = stack->GetHists();
168 <      TIterator* iter = list->MakeIterator();
169 <
170 <      TObject* obj = 0;
171 <
172 <      //Hist color iterator
173 <      Int_t colorIt = 1;
174 <
175 <      while ( (obj = iter->Next()) ) {
176 <         if (! obj->InheritsFrom(TH1::Class())) continue;
177 <
178 <         if (addColor) {
179 <            hist::color(obj->GetName(), colorIt);
180 <            ++colorIt;
181 <         }
182 <
183 <         if (token == -1)
184 <            leg->AddEntry(obj, obj->GetName(), option);
185 <         else {
186 <            TString name(obj->GetName());
187 <            TObjArray* a = name.Tokenize("_");
188 <            if (a->GetEntries() <= token)
189 <               leg->AddEntry(obj, obj->GetName(), option);
190 <            else
191 <               leg->AddEntry(obj, a->At(token)->GetName(), option);
192 <         }
193 <      }
194 <
195 <      return leg;
196 <   }
197 <
198 <   //Normalize to one all histograms whose names match the given regular exp-
199 <   //ression pattern or begin with the given prefix.
200 <
201 <   void normalize(const char* patORpfx) {
202 <      TRegexp reg(patORpfx, kFALSE);
203 <
204 <      TList* list = gDirectory->GetList() ;
205 <      TIterator* iter = list->MakeIterator();
206 <
207 <      TObject* obj = 0;
208 <
209 <      while ( (obj = iter->Next()) ) {
210 <         if (! obj->InheritsFrom(TH1::Class())) continue;
211 <
212 <         TString name = obj->GetName();
213 <
214 <         if (TString(patORpfx).MaybeRegexp()) {
215 <            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
216 <         } else if (! name.BeginsWith(patORpfx)) continue;
18 >    void add(const char* outHistName, const char* patORpfx);
19  
20 <         Double_t integral = 0;
21 <
22 <         if (obj->InheritsFrom(TH2::Class()))
23 <            integral = ((TH2*)obj)->Integral();
24 <         else
25 <            integral = ((TH1*)obj)->Integral();
26 <
27 <         if (integral) {
28 <            ((TH1*)obj)->Sumw2();
29 <            ((TH1*)obj)->Scale(1./integral);
30 <         }
31 <      }
32 <   }
33 <
34 <   //Scale by the given value all histograms whose names match the given regular
35 <   //expression pattern or begin with the given prefix.
36 <
37 <   void scale(const char* patORpfx, Double_t scale) {
236 <      TRegexp reg(patORpfx, kFALSE);
237 <
238 <      TList* list = gDirectory->GetList() ;
239 <      TIterator* iter = list->MakeIterator();
240 <
241 <      TObject* obj = 0;
242 <
243 <      while ( (obj = iter->Next()) ) {
244 <         if (! obj->InheritsFrom(TH1::Class())) continue;
245 <
246 <         TString name = obj->GetName();
20 >    //Add all histograms whose names match one of ten possible regular expression
21 >    //patterns or begin with one of ten possible given prefixes.  Feel free to
22 >    //mix and match regular expression patterns and prefixes.  If the final hist-
23 >    //ogram named outHistName does not exist it is created.
24 >
25 >    void add(const char* outHistName, const char* patORpfx0, const char* patORpfx1, const char* patORpfx2, const char* patORpfx3, const char* patORpfx4, const char* patORpfx5, const char* patORpfx6, const char* patORpfx7, const char* patORpfx8, const char* patORpfx9)
26 >    {
27 >        add(outHistName, patORpfx0);
28 >        add(outHistName, patORpfx1);
29 >        if (patORpfx2) add(outHistName, patORpfx2);
30 >        if (patORpfx3) add(outHistName, patORpfx3);
31 >        if (patORpfx4) add(outHistName, patORpfx4);
32 >        if (patORpfx5) add(outHistName, patORpfx5);
33 >        if (patORpfx6) add(outHistName, patORpfx6);
34 >        if (patORpfx7) add(outHistName, patORpfx7);
35 >        if (patORpfx8) add(outHistName, patORpfx8);
36 >        if (patORpfx9) add(outHistName, patORpfx9);
37 >    }
38  
39 <         if (TString(patORpfx).MaybeRegexp()) {
40 <            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
41 <         } else if (! name.BeginsWith(patORpfx)) continue;
39 >    //Add all histograms whose names match the given regular expression pattern
40 >    //or begin with the given prefix.  If the final histogram named outHistName
41 >    //does not exist it is created.
42 >
43 >    void add(const char* outHistName, const char* patORpfx) {
44 >        TRegexp reg(patORpfx, kFALSE);
45 >
46 >        TList* list = gDirectory->GetList() ;
47 >        TIterator* iter = list->MakeIterator();
48 >
49 >        TObject* obj = 0;
50 >        TObject* hist = 0;
51 >        Bool_t makeOutHist = false;
52 >
53 >        hist = gDirectory->Get(outHistName);
54 >        //If out hist does not exist, remember to create it
55 >        if (! hist) makeOutHist = true;
56 >
57 >        while ( (obj = iter->Next()) ) {
58 >            if (! obj->InheritsFrom(TH1::Class())) continue;
59 >
60 >            TString name = obj->GetName();
61 >            //Don't add out hist
62 >            if (name == TString(outHistName)) continue;
63 >
64 >            if (TString(patORpfx).MaybeRegexp()) {
65 >                if (TString(obj->GetName()).Index(reg) < 0 ) continue;
66 >            } else if (! name.BeginsWith(patORpfx)) continue;
67 >
68 >            if (makeOutHist) {
69 >                hist = obj->Clone(outHistName);
70 >
71 >                if (hist->InheritsFrom(TH2::Class()))
72 >                    ((TH2*)hist)->Reset();
73 >                else
74 >                    ((TH1*)hist)->Reset();
75 >
76 >                ((TH1*)hist)->SetTitle(outHistName);
77 >                ((TH1*)hist)->Sumw2();
78 >                makeOutHist = false;
79 >            }
80  
81 <         ((TH1*)obj)->Sumw2();
82 <         ((TH1*)obj)->Scale(scale);
83 <      }
255 <   }
81 >            ((TH1*)hist)->Add((TH1*)obj);
82 >        }
83 >    }
84  
85 <   //Don't you hate it when you draw multiple histograms on the same canvas only
86 <   //to find that the bottom histogram's range does not encompass those of the
87 <   //histograms drawn on top?  This method determines the maximum and minimum y
88 <   //range of all the histograms drawn on a given TCanvas and appropriately re-
89 <   //sizes the bottom histogram.
85 >    //For all histograms whose names match the given regular expression pattern
86 >    //or begin with the given prefix, set the fill, line and marker colors to the
87 >    //given value.
88 >
89 >    void color(const char* patORpfx, Color_t color) {
90 >        TRegexp reg(patORpfx, kFALSE);
91 >
92 >        TList* list = gDirectory->GetList() ;
93 >        if (!list) {
94 >            cout << "Failed to set color for " << patORpfx << endl;
95 >            return;
96 >        }
97 >        TIterator* iter = list->MakeIterator();
98 >
99 >        TObject* obj = 0;
100 >
101 >        while ( (obj = iter->Next()) ) {
102 >            if (! obj->InheritsFrom(TH1::Class())) continue;
103 >
104 >            TString name = obj->GetName();
105 >
106 >            if (TString(patORpfx).MaybeRegexp()) {
107 >                if (TString(obj->GetName()).Index(reg) < 0 ) continue;
108 >            } else if (! name.BeginsWith(patORpfx)) continue;
109 >
110 >            ((TH1*)obj)->SetFillColor(color);
111 >            ((TH1*)obj)->SetLineColor(color);
112 >            ((TH1*)obj)->SetMarkerColor(color);
113 >        }
114 >    }
115  
116 <   void setrangey(TCanvas* canvas) {
117 <      if(! canvas) return;
116 >    //Return a pointer to a TLegend with an entry for each histogram drawn on a
117 >    //given TCanvas.  Display either the line, point or fill values.  Optionally
118 >    //apply colors to all histograms.  By default, entry labels are the names of
119 >    //their respective histograms.  Optionally, if histogram names are of the
120 >    //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
121 >
122 >    TLegend* legend(TCanvas* canvas, Option_t* option, Bool_t addColor, Int_t token,
123 >                    Float_t xmin, Float_t ymin, Float_t xmax, Float_t ymax) {
124 >        if(! canvas) return 0;
125 >
126 >        TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
127 >        TList* list = canvas->GetListOfPrimitives();
128 >        TIterator* iter = list->MakeIterator();
129 >
130 >        TObject* obj = 0;
131 >
132 >        //Hist color iterator
133 >        Int_t colorIt = 1;
134 >
135 >        while ( (obj = iter->Next()) ) {
136 >            if (! obj->InheritsFrom(TH1::Class())) continue;
137 >
138 >            if (addColor) {
139 >                hist::color(obj->GetName(), colorIt);
140 >                ++colorIt;
141 >            }
142  
143 <      TList* list = canvas->GetListOfPrimitives();
144 <      TIterator* iter = list->MakeIterator();
143 >            if (token == -1)
144 >                leg->AddEntry(obj, obj->GetName(), option);
145 >            else {
146 >                TString name(obj->GetName());
147 >                TObjArray* a = name.Tokenize("_");
148 >                if (a->GetEntries() <= token)
149 >                    leg->AddEntry(obj, obj->GetName(), option);
150 >                else
151 >                    leg->AddEntry(obj, a->At(token)->GetName(), option);
152 >            }
153 >        }
154  
155 <      TObject* obj = 0;
156 <      TObject* top = 0;
155 >        return leg;
156 >    }
157  
158 <      //Extremes
159 <      Double_t maxy = -999999;
274 <      Double_t miny = 999999;
158 >    //Normalize to one all histograms whose names match the given regular exp-
159 >    //ression pattern or begin with the given prefix.
160  
161 <      while ( (obj = iter->Next()) ) {
162 <         if (! obj->InheritsFrom(TH1::Class())) continue;
161 >    void normalize(const char* patORpfx) {
162 >        TRegexp reg(patORpfx, kFALSE);
163  
164 <         if (! top) top = obj;
164 >        TList* list = gDirectory->GetList() ;
165 >        TIterator* iter = list->MakeIterator();
166  
167 <         if (((TH1*)obj)->GetMaximum() > maxy) maxy = ((TH1*)obj)->GetMaximum();
282 <         if (((TH1*)obj)->GetMinimum() < miny) miny = ((TH1*)obj)->GetMinimum();
283 <      }
167 >        TObject* obj = 0;
168  
169 <      ((TH1*)top)->SetMaximum(maxy*1.3);
170 <      //Protect against log scale
287 <      if (canvas->GetLogy() && ! miny)
288 <         ((TH1*)top)->SetMinimum(1E-4);
289 <      else
290 <         ((TH1*)top)->SetMinimum(miny*0.7);
291 <   }
169 >        while ( (obj = iter->Next()) ) {
170 >            if (! obj->InheritsFrom(TH1::Class())) continue;
171  
172 <   //Create a stacked histogram consisting of all histograms whose names match
294 <   //the given regular expression pattern or begin with the given prefix.  If
295 <   //the THStack named stackHistName does not exist it is created.  Optionally
296 <   //apply colors to all histograms.  Set drawOption to "nostack" if you do not
297 <   //want to stack, to "hist" to display histograms without errors, to "histe"
298 <   //to display histograms with errors, etc.
172 >            TString name = obj->GetName();
173  
174 <   void stack(const char* stackHistName, const char* patORpfx, Bool_t addColor = kFALSE, Option_t* drawOption = "") {
175 <      TRegexp reg(patORpfx, kFALSE);
174 >            if (TString(patORpfx).MaybeRegexp()) {
175 >                if (TString(obj->GetName()).Index(reg) < 0 ) continue;
176 >            } else if (! name.BeginsWith(patORpfx)) continue;
177  
178 <      TList* list = gDirectory->GetList() ;
304 <      TIterator* iter = list->MakeIterator();
178 >            Double_t integral = 0;
179  
180 <      TObject* obj = 0;
181 <      TObject* stack = 0;
182 <      Bool_t makeStackHist = false;
180 >            if (obj->InheritsFrom(TH2::Class()))
181 >                integral = ((TH2*)obj)->Integral();
182 >            else
183 >                integral = ((TH1*)obj)->Integral();
184  
185 <      stack = gDirectory->Get(stackHistName);
186 <      //If stack hist does not exist, remember to create it
187 <      if (! stack) makeStackHist = true;
185 >            if (integral) {
186 >                ((TH1*)obj)->Sumw2();
187 >                ((TH1*)obj)->Scale(1./integral);
188 >            }
189 >        }
190 >    }
191  
192 <      //Hist color iterator
193 <      Int_t colorIt = 1;
192 >    //Scale by the given value all histograms whose names match the given regular
193 >    //expression pattern or begin with the given prefix.
194  
195 <      while ( (obj = iter->Next()) ) {
196 <         if (! obj->InheritsFrom(TH1::Class())) continue;
195 >    void scale(const char* patORpfx, Double_t scale) {
196 >        TRegexp reg(patORpfx, kFALSE);
197  
198 <         TString name = obj->GetName();
198 >        TList* list = gDirectory->GetList() ;
199 >        TIterator* iter = list->MakeIterator();
200  
201 <         if (TString(patORpfx).MaybeRegexp()) {
323 <            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
324 <         } else if (! name.BeginsWith(patORpfx)) continue;
201 >        TObject* obj = 0;
202  
203 <         if (makeStackHist) {
204 <            stack = new THStack(stackHistName, stackHistName);
328 <            makeStackHist = false;
329 <         }
203 >        while ( (obj = iter->Next()) ) {
204 >            if (! obj->InheritsFrom(TH1::Class())) continue;
205  
206 <         if (addColor) {
332 <            hist::color(obj->GetName(), colorIt);
333 <            ++colorIt;
334 <         }
206 >            TString name = obj->GetName();
207  
208 <         ((THStack*)stack)->Add((TH1*)obj, drawOption);
209 <      }
208 >            if (TString(patORpfx).MaybeRegexp()) {
209 >                if (TString(obj->GetName()).Index(reg) < 0 ) continue;
210 >            } else if (! name.BeginsWith(patORpfx)) continue;
211  
212 <      // Currently breaks .ls
213 <      //gDirectory->Append(stack);
214 <   }
212 >            if (!(((TH1*)obj)->GetSumw2N()))
213 >                ((TH1*)obj)->Sumw2();
214 >            ((TH1*)obj)->Scale(scale);
215 >        }
216 >    }
217  
218 <   //Set the x-axis title of all histograms whose names match the given regular
219 <   //expression pattern or begin with the given prefix.
218 >    //Don't you hate it when you draw multiple histograms on the same canvas only
219 >    //to find that the bottom histogram's range does not encompass those of the
220 >    //histograms drawn on top?  This method determines the maximum and minimum y
221 >    //range of all the histograms drawn on a given TCanvas and appropriately re-
222 >    //sizes the bottom histogram.
223 >
224 >    void setrangey(TCanvas* canvas) {
225 >        if(! canvas) return;
226 >
227 >        TList* list = canvas->GetListOfPrimitives();
228 >        TIterator* iter = list->MakeIterator();
229 >
230 >        TObject* obj = 0;
231 >        TObject* top = 0;
232 >
233 >        //Extremes
234 >        Double_t maxy = -999999;
235 >        Double_t miny = 999999;
236 >
237 >        while ( (obj = iter->Next()) ) {
238 >            if (! obj->InheritsFrom(TH1::Class())) continue;
239 >
240 >            if (! top) top = obj;
241 >
242 >            if (((TH1*)obj)->GetMaximum() > maxy) maxy = ((TH1*)obj)->GetMaximum();
243 >            if (((TH1*)obj)->GetMinimum() < miny) miny = ((TH1*)obj)->GetMinimum();
244 >        }
245 >
246 >        ((TH1*)top)->SetMaximum(maxy*1.9);
247 >        //Protect against log scale
248 >        if (canvas->GetLogy() && ! miny)
249 >            ((TH1*)top)->SetMinimum(1E-4);
250 >        else
251 >            ((TH1*)top)->SetMinimum(miny*0.7);
252 >    }
253  
254 <   void xaxis(const char* patORpfx, const char* title) {
255 <      TRegexp reg(patORpfx, kFALSE);
254 >    //Create a stacked histogram consisting of all histograms whose names match
255 >    //the given regular expression pattern or begin with the given prefix.  If
256 >    //the THStack named stackHistName does not exist it is created.  Optionally
257 >    //apply colors to all histograms.  Set drawOption to "nostack" if you do not
258 >    //want to stack, to "hist" to display histograms without errors, to "histe"
259 >    //to display histograms with errors, etc.
260 >
261 >    void stack(const char* stackHistName, const char* patORpfx, Bool_t addColor, Option_t* drawOption) {
262 >        TRegexp reg(patORpfx, kFALSE);
263 >
264 >        TList* list = gDirectory->GetList() ;
265 >        TIterator* iter = list->MakeIterator();
266 >
267 >        TObject* obj = 0;
268 >        TObject* stack = 0;
269 >        Bool_t makeStackHist = false;
270 >
271 >        stack = gDirectory->Get(stackHistName);
272 >        //If stack hist does not exist, remember to create it
273 >        if (! stack) makeStackHist = true;
274 >
275 >        //Hist color iterator
276 >        Int_t colorIt = 1;
277 >
278 >        while ( (obj = iter->Next()) ) {
279 >            if (! obj->InheritsFrom(TH1::Class())) continue;
280 >
281 >            TString name = obj->GetName();
282 >
283 >            if (TString(patORpfx).MaybeRegexp()) {
284 >                if (TString(obj->GetName()).Index(reg) < 0 ) continue;
285 >            } else if (! name.BeginsWith(patORpfx)) continue;
286 >
287 >            if (makeStackHist) {
288 >                stack = new THStack(stackHistName, stackHistName);
289 >                makeStackHist = false;
290 >            }
291  
292 <      TList* list = gDirectory->GetList() ;
293 <      TIterator* iter = list->MakeIterator();
292 >            if (addColor) {
293 >                hist::color(obj->GetName(), colorIt);
294 >                ++colorIt;
295 >            }
296  
297 <      TObject* obj = 0;
297 >            ((THStack*)stack)->Add((TH1*)obj, drawOption);
298 >        }
299  
300 <      while ( (obj = iter->Next()) ) {
301 <         if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
300 >        // Currently breaks .ls
301 >        //gDirectory->Append(stack);
302 >    }
303  
304 <         TString name = obj->GetName();
304 >    //Set the x-axis title of all histograms whose names match the given regular
305 >    //expression pattern or begin with the given prefix.
306  
307 <         if (TString(patORpfx).MaybeRegexp()) {
308 <            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
361 <         } else if (! name.BeginsWith(patORpfx)) continue;
307 >    void xaxis(const char* patORpfx, const char* title) {
308 >        TRegexp reg(patORpfx, kFALSE);
309  
310 <         if (obj->InheritsFrom(TH1::Class()))
311 <            ((TH1*)obj)->GetXaxis()->SetTitle(title);
365 <         if (obj->InheritsFrom(THStack::Class())) {
366 <            ((THStack*)obj)->Draw();
367 <            ((THStack*)obj)->GetXaxis()->SetTitle(title);
368 <         }
369 <      }
370 <   }
310 >        TList* list = gDirectory->GetList() ;
311 >        TIterator* iter = list->MakeIterator();
312  
313 <   //Set the y-axis title of all histograms whose names match the given regular
373 <   //expression pattern or begin with the given prefix.
313 >        TObject* obj = 0;
314  
315 <   void yaxis(const char* patORpfx, const char* title) {
316 <      TRegexp reg(patORpfx, kFALSE);
315 >        while ( (obj = iter->Next()) ) {
316 >            if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
317  
318 <      TList* list = gDirectory->GetList() ;
379 <      TIterator* iter = list->MakeIterator();
318 >            TString name = obj->GetName();
319  
320 <      TObject* obj = 0;
320 >            if (TString(patORpfx).MaybeRegexp()) {
321 >                if (TString(obj->GetName()).Index(reg) < 0 ) continue;
322 >            } else if (! name.BeginsWith(patORpfx)) continue;
323  
324 <      while ( (obj = iter->Next()) ) {
325 <         if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
324 >            if (obj->InheritsFrom(TH1::Class()))
325 >                ((TH1*)obj)->GetXaxis()->SetTitle(title);
326 >            if (obj->InheritsFrom(THStack::Class())) {
327 >                ((THStack*)obj)->Draw();
328 >                ((THStack*)obj)->GetXaxis()->SetTitle(title);
329 >            }
330 >        }
331 >    }
332  
333 <         TString name = obj->GetName();
333 >    //Set the y-axis title of all histograms whose names match the given regular
334 >    //expression pattern or begin with the given prefix.
335  
336 <         if (TString(patORpfx).MaybeRegexp()) {
337 <            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
390 <         } else if (! name.BeginsWith(patORpfx)) continue;
336 >    void yaxis(const char* patORpfx, const char* title) {
337 >        TRegexp reg(patORpfx, kFALSE);
338  
339 <         if (obj->InheritsFrom(TH1::Class()))
340 <            ((TH1*)obj)->GetYaxis()->SetTitle(title);
394 <         if (obj->InheritsFrom(THStack::Class())) {
395 <            ((THStack*)obj)->Draw();
396 <            ((THStack*)obj)->GetYaxis()->SetTitle(title);
397 <         }
398 <      }
399 <   }
339 >        TList* list = gDirectory->GetList() ;
340 >        TIterator* iter = list->MakeIterator();
341  
342 < }
402 < // Input:  2 histogram
403 < // Output: one histogram which is the efficiency:
404 < // h1 :  TOTAL NUMBER OF EVENTS
405 < // h2 :  NUMBER OF EVENTS THAT PASS
406 <
407 < #include "TH1.h"
408 <
409 < // Method by pointer
410 < TH1F* eff(TH1F* h1, TH1F* h2, const char* name="eff"){
411 <
412 <  // first, verify that all histograms have same binning
413 <  // nx is the number of visible bins
414 <  // nxtot = nx+2 includes underflow and overflow
415 <  Int_t nx = h1->GetNbinsX();
416 <  if (h2->GetNbinsX() != nx) {
417 <    cout << "Histograms must have same number of bins" << endl;
418 <    return 0;
419 <  }
420 <
421 <  // get the new histogram
422 <  TH1F* temp = (TH1F*) h1->Clone(name);
423 <  temp->SetTitle(name);
424 <  temp->Reset();
425 <  temp->Sumw2();
342 >        TObject* obj = 0;
343  
344 <  // Do the calculation
345 <  temp->Divide(h2,h1,1.,1.,"B");
344 >        while ( (obj = iter->Next()) ) {
345 >            if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
346  
347 <  // Done
431 <  return temp;
432 < }
347 >            TString name = obj->GetName();
348  
349 +            if (TString(patORpfx).MaybeRegexp()) {
350 +                if (TString(obj->GetName()).Index(reg) < 0 ) continue;
351 +            } else if (! name.BeginsWith(patORpfx)) continue;
352  
353 < // Method by name
354 < TH1F* eff(const char* name1, const char* name2, const char* name="eff"){
353 >            if (obj->InheritsFrom(TH1::Class()))
354 >                ((TH1*)obj)->GetYaxis()->SetTitle(title);
355 >            if (obj->InheritsFrom(THStack::Class())) {
356 >                ((THStack*)obj)->Draw();
357 >                ((THStack*)obj)->GetYaxis()->SetTitle(title);
358 >            }
359 >        }
360 >    }
361  
438  // Get a list of object and their iterator
439  TList* list = gDirectory->GetList() ;
440  TIterator* iter = list->MakeIterator();
441
442  // Loop over objects, set the pointers
443  TObject* obj;
444  TH1F* h1=0;
445  TH1F* h2=0;
446  TString str1 = Form("%s",name1);
447  TString str2 = Form("%s",name2);
448  while( (obj=iter->Next()) ) {
449    TString objName = obj->GetName();
450    if (objName == str1) h1 = (TH1F*) obj;
451    if (objName == str2) h2 = (TH1F*) obj;
452  }
453
454  // quit if not found
455  if (h1 == 0) {
456    cout << "Histogram " << name1 << " not found" << endl;
457    return 0;
458  }
459  if (h2 == 0) {
460    cout << "Histogram " << name2 << " not found" << endl;
461    return 0;
462  }
463
464  // Call the method by pointer
465  TH1F* temp = eff(h1, h2, name);
466  return temp;
467 }
362   // Input:  4 histogram
363   // Output: one histogram which is the BG subtracted efficiency:
364   // h1 :  TOTAL NUMBER OF EVENTS, SIGNAL REGION
# Line 472 | Line 366 | TH1F* eff(const char* name1, const char*
366   // h3 :  TOTAL NUMBER OF EVENTS, SIDE BAND
367   // h4 :  NUMBER OF EVENTS THAT PASS, SIDE BAND
368  
475 #include "TH1.h"
369  
370 +    TH1F* eff_bg(TH1F* h1, TH1F* h2, TH1F* h3, TH1F* h4, const char* name){
371  
372 < TH1F* eff_bg(TH1F* h1, TH1F* h2, TH1F* h3, TH1F* h4, const char* name="eff"){
372 >        // first, verify that all histograms have same binning
373 >        // nx is the number of visible bins
374 >        // nxtot = nx+2 includes underflow and overflow
375 >        Int_t nx = h1->GetNbinsX();
376 >        Int_t nxtot = nx + 2;
377 >        if (h2->GetNbinsX() != nx) {
378 >            cout << "Histograms must have same number of bins" << endl;
379 >            return 0;
380 >        }
381 >        if (h3->GetNbinsX() != nx) {
382 >            cout << "Histograms must have same number of bins" << endl;
383 >            return 0;
384 >        }
385 >        if (h3->GetNbinsX() != nx) {
386 >            cout << "Histograms must have same number of bins" << endl;
387 >            return 0;
388 >        }
389 >
390 >        // get the new histogram
391 >        TH1F* temp = (TH1F*) h1->Clone(name);
392 >        temp->SetTitle(name);
393 >        temp->Reset();
394 >        temp->Sumw2();
395 >
396 >        // Loop over bins, calculate efficiency and error, put it in histogram
397 >        for (Int_t i=0; i<nxtot; i++) {
398 >            Double_t x1 = h1->GetBinContent(i);
399 >            Double_t x2 = h2->GetBinContent(i);
400 >            Double_t x3 = h3->GetBinContent(i);
401 >            Double_t x4 = h4->GetBinContent(i);
402 >            Double_t denom = x1 - x3;
403 >            Double_t eff;
404 >            if (denom == 0.) {
405 >                eff = 0;
406 >            } else {
407 >                eff   = (x2-x4)/denom;
408 >            }
409 >            Double_t failSig = x1 - x2;
410 >            Double_t failBg  = x3 - x4;
411 >            Double_t blah    = (1-eff)*(1-eff)*(x2+x4) + eff*eff*(failSig+failBg);
412 >            if (blah <= 0.) blah=0.0;
413 >            Double_t err;
414 >            if (denom == 0) {
415 >                err = 0.;
416 >            } else {
417 >                err = sqrt(blah)/denom;
418 >            }
419 >            temp->SetBinContent(i,eff);
420 >            temp->SetBinError(i,err);
421 >        }
422  
423 <  // first, verify that all histograms have same binning
424 <  // nx is the number of visible bins
425 <  // nxtot = nx+2 includes underflow and overflow
483 <  Int_t nx = h1->GetNbinsX();
484 <  Int_t nxtot = nx + 2;
485 <  if (h2->GetNbinsX() != nx) {
486 <    cout << "Histograms must have same number of bins" << endl;
487 <    return 0;
488 <  }
489 <  if (h3->GetNbinsX() != nx) {
490 <    cout << "Histograms must have same number of bins" << endl;
491 <    return 0;
492 <  }
493 <  if (h3->GetNbinsX() != nx) {
494 <    cout << "Histograms must have same number of bins" << endl;
495 <    return 0;
496 <  }
497 <
498 <  // get the new histogram
499 <  TH1F* temp = (TH1F*) h1->Clone(name);
500 <  temp->SetTitle(name);
501 <  temp->Reset();
502 <  temp->Sumw2();
503 <
504 <  // Loop over bins, calculate efficiency and error, put it in histogram
505 <  for (Int_t i=0; i<nxtot; i++) {
506 <    Double_t x1 = h1->GetBinContent(i);
507 <    Double_t x2 = h2->GetBinContent(i);
508 <    Double_t x3 = h3->GetBinContent(i);
509 <    Double_t x4 = h4->GetBinContent(i);
510 <    Double_t denom = x1 - x3;
511 <    Double_t eff;
512 <    if (denom == 0.) {
513 <      eff = 0;
514 <    } else {
515 <      eff   = (x2-x4)/denom;
516 <    }
517 <    Double_t failSig = x1 - x2;
518 <    Double_t failBg  = x3 - x4;
519 <    Double_t blah    = (1-eff)*(1-eff)*(x2+x4) + eff*eff*(failSig+failBg);
520 <    if (blah <= 0.) blah=0.0;
521 <    Double_t err;
522 <    if (denom == 0) {
523 <      err = 0.;
524 <    } else {
525 <      err = sqrt(blah)/denom;
526 <    }
527 <    temp->SetBinContent(i,eff);
528 <    temp->SetBinError(i,err);
529 <  }
423 >        // Done
424 >        return temp;
425 >    }
426  
427 <  // Done
428 <  return temp;
429 < }
427 >    void deleteHistos() {
428 >        // Delete all existing histograms in memory
429 >        TObject* obj;
430 >        TList* list = gDirectory->GetList() ;
431 >        TIterator* iter = list->MakeIterator();
432 >        while ( (obj=iter->Next()) ) {
433 >            if (obj->IsA()->InheritsFrom(TH1::Class()) ||
434 >                obj->IsA()->InheritsFrom(TH2::Class()) ) {delete obj;}
435 >        }
436 >    }
437  
438 < #include <TList.h>
439 < #include <TIterator.h>
438 >    void histio()
439 >    {
440 >    }
441  
442 < void deleteHistos() {
443 <   // Delete all existing histograms in memory
444 <   TObject* obj;
445 <   TList* list = gDirectory->GetList() ;
446 <   TIterator* iter = list->MakeIterator();
447 <   while ( (obj=iter->Next()) ) {
448 <     if (obj->IsA()->InheritsFrom(TH1::Class()) ||
449 <         obj->IsA()->InheritsFrom(TH2::Class()) ) {delete obj;}
450 <   }
451 < }
442 >    void saveHist(const char* filename, const char* pat)
443 >    {
444 >        TList* list = gDirectory->GetList() ;
445 >        TIterator* iter = list->MakeIterator();
446 >
447 >        TRegexp re(pat,kTRUE) ;
448 >
449 >        TFile outf(filename,"RECREATE") ;
450 >        while(TObject *obj=iter->Next()) {
451 >            if (TString(obj->GetName()).Index(re)>=0) {
452 >                obj->Write() ;
453 >                cout << "." ;
454 >                cout.flush() ;
455 >            }
456 >        }
457 >        cout << endl ;
458 >        outf.Close() ;
459  
460 < void histio()
461 < {
551 < }
460 >        delete iter ;
461 >    }
462  
553 void saveHist(const char* filename, const char* pat="*")
554 {
555   TList* list = gDirectory->GetList() ;
556   TIterator* iter = list->MakeIterator();
557
558   TRegexp re(pat,kTRUE) ;
559
560   TFile outf(filename,"RECREATE") ;
561   while(TObject *obj=iter->Next()) {
562      if (TString(obj->GetName()).Index(re)>=0) {
563         obj->Write() ;
564         cout << "." ;
565         cout.flush() ;
566      }
567   }
568   cout << endl ;
569   outf.Close() ;
463  
464 <   delete iter ;
465 < }
464 >    void loadHist(const char* filename, const char* pfx, const char* pat, Bool_t verbose, Bool_t doAdd)
465 >    {
466 >        TFile inf(filename) ;
467 >        //inf.ReadAll() ;
468 >        TList* list = inf.GetListOfKeys() ;
469 >        TIterator* iter = list->MakeIterator();
470 >
471 >        TRegexp re(pat,kTRUE) ;
472 >        if (verbose)
473 >            std::cout << "pat = " << pat << std::endl;
474 >
475 >        gDirectory->cd("Rint:") ;
476 >
477 >        TObject* obj ;
478 >        TKey* key ;
479 >        if (verbose) {
480 >            std::cout << "doAdd = " << (doAdd?"T":"F") << std::endl;
481 >            std::cout << "loadHist: reading.";
482 >        }
483 >        while( (key=(TKey*)iter->Next()) ) {
484 >
485 >            Int_t ridx = TString(key->GetName()).Index(re) ;
486 >            if (ridx==-1) {
487 >                continue ;
488 >            }
489  
490 +            obj = inf.Get(key->GetName()) ;
491 +            TObject* clone ;
492 +            if (pfx) {
493 +
494 +                // Find existing TH1-derived objects
495 +                TObject* oldObj = 0 ;
496 +                if (doAdd){
497 +                    oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ;
498 +                    if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
499 +                        oldObj = 0 ;
500 +                    }
501 +                }
502 +                if (oldObj) {
503 +                    clone = oldObj ;
504 +                    ((TH1*)clone)->Add((TH1*)obj) ;
505 +                } else {
506 +                    clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ;
507 +                }
508 +
509 +
510 +            } else {
511 +
512 +                // Find existing TH1-derived objects
513 +                TObject* oldObj = 0 ;
514 +                if (doAdd){
515 +                    oldObj = gDirectory->Get(key->GetName()) ;
516 +                    if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
517 +                        oldObj = 0 ;
518 +                    }
519 +                }
520 +
521 +                if (oldObj) {
522 +                    clone = oldObj ;
523 +                    ((TH1*)clone)->Add((TH1*)obj) ;
524 +                } else {
525 +                    clone = obj->Clone() ;
526 +                }
527 +            }
528 +            if (!gDirectory->GetList()->FindObject(clone)) {
529 +                gDirectory->Append(clone) ;
530 +            }
531 +            if (verbose) {
532 +                std::cout << ".";
533 +                std::cout.flush();
534 +            }
535 +        }
536 +        if (verbose)
537 +            std::cout << std::endl;
538 +        inf.Close() ;
539 +        delete iter ;
540 +    }
541  
575 void loadHist(const char* filename, const char* pfx=0, const char* pat="*", Bool_t doAdd=kFALSE)
576 {
577   TFile inf(filename) ;
578   //inf.ReadAll() ;
579   TList* list = inf.GetListOfKeys() ;
580   TIterator* iter = list->MakeIterator();
581
582   TRegexp re(pat,kTRUE) ;
583   cout << "pat = " << pat << endl ;
584
585   gDirectory->cd("Rint:") ;
586
587   TObject* obj ;
588   TKey* key ;
589   cout << "doAdd = " << (doAdd?"T":"F") << endl ;
590   cout << "loadHist: reading." ;
591   while( (key=(TKey*)iter->Next()) ) {
592
593      Int_t ridx = TString(key->GetName()).Index(re) ;
594      if (ridx==-1) {
595         continue ;
596      }
597
598      obj = inf.Get(key->GetName()) ;
599      TObject* clone ;
600      if (pfx) {
601
602         // Find existing TH1-derived objects
603         TObject* oldObj = 0 ;
604         if (doAdd){
605            oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ;
606            if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
607               oldObj = 0 ;
608            }
609         }
610         if (oldObj) {
611            clone = oldObj ;
612            ((TH1*)clone)->Add((TH1*)obj) ;
613         } else {
614            clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ;
615         }
616
617
618      } else {
619
620         // Find existing TH1-derived objects
621         TObject* oldObj = 0 ;
622         if (doAdd){
623            oldObj = gDirectory->Get(key->GetName()) ;
624            if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
625               oldObj = 0 ;
626            }
627         }
628
629         if (oldObj) {
630            clone = oldObj ;
631            ((TH1*)clone)->Add((TH1*)obj) ;
632         } else {
633            clone = obj->Clone() ;
634         }
635      }
636      if (!gDirectory->GetList()->FindObject(clone)) {
637         gDirectory->Append(clone) ;
638      }
639      cout << "." ;
640      cout.flush() ;
641   }
642   cout << endl;
643   inf.Close() ;
644   delete iter ;
542   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines