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.1 by fgolf, Sun Aug 29 18:39:12 2010 UTC vs.
Revision 1.2 by fgolf, Thu May 26 03:34:52 2011 UTC

# Line 1 | Line 1
1 < #ifndef HISTTOOLS_H
2 < #define HISTTOOLS_H
3 <
4 < #include "TList.h"
5 < #include "TObjArray.h"
1 > #include "TCanvas.h"
2 > #include "TFile.h"
3   #include "TH1.h"
4   #include "TH2.h"
8 #include "TLegend.h"
5   #include "THStack.h"
10 #include "TCanvas.h"
11 #include "TFile.h"
12 #include "TRegexp.h"
6   #include "TKey.h"
7 + #include "TLegend.h"
8 + #include "TRegexp.h"
9 +
10   #include <iostream>
15 #include <vector>
11  
12 < #include "histtools.h"
12 > using namespace std;
13 >
14   namespace hist {
15  
16 <  //Add all histograms whose names match the given regular expression pattern
17 <  //or begin with the given prefix.  If the final histogram named outHistName
18 <  //does not exist it is created.
16 >     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);
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 <    TList* list = gDirectory->GetList() ;
48 <    TIterator* iter = list->MakeIterator();
47 >      TObject* obj = 0;
48 >      TObject* hist = 0;
49 >      Bool_t makeOutHist = false;
50  
51 <    TObject* obj = 0;
52 <    TObject* hist = 0;
53 <    Bool_t makeOutHist = false;
51 >      hist = gDirectory->Get(outHistName);
52 >      //If out hist does not exist, remember to create it
53 >      if (! hist) makeOutHist = true;
54  
55 <    hist = gDirectory->Get(outHistName);
56 <    //If out hist does not exist, remember to create it
36 <    if (! hist) makeOutHist = true;
55 >      while ( (obj = iter->Next()) ) {
56 >         if (! obj->InheritsFrom(TH1::Class())) continue;
57  
58 <    while ((obj = iter->Next())) {
59 <      if (! obj->InheritsFrom(TH1::Class())) continue;
58 >         TString name = obj->GetName();
59 >         //Don't add out hist
60 >         if (name == TString(outHistName)) continue;
61  
62 <      TString name = obj->GetName();
63 <      //Don't add out hist
64 <      if (name == TString(outHistName)) continue;
62 >         if (TString(patORpfx).MaybeRegexp()) {
63 >            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
64 >         } else if (! name.BeginsWith(patORpfx)) continue;
65  
66 <      if (TString(patORpfx).MaybeRegexp()) {
67 <        if (TString(obj->GetName()).Index(reg) < 0 ) continue;
47 <      } else if (! name.BeginsWith(patORpfx)) continue;
66 >         if (makeOutHist) {
67 >            hist = obj->Clone(outHistName);
68  
69 <      if (makeOutHist) {
70 <        hist = obj->Clone(outHistName);
69 >            if (hist->InheritsFrom(TH2::Class()))
70 >               ((TH2*)hist)->Reset();
71 >            else
72 >               ((TH1*)hist)->Reset();
73  
74 <        if (hist->InheritsFrom(TH2::Class()))
75 <          ((TH2*)hist)->Reset();
76 <        else
77 <          ((TH1*)hist)->Reset();
74 >            ((TH1*)hist)->SetTitle(outHistName);
75 >            ((TH1*)hist)->Sumw2();
76 >            makeOutHist = false;
77 >         }
78  
79 <        ((TH1*)hist)->SetTitle(outHistName);
58 <        ((TH1*)hist)->Sumw2();
59 <        makeOutHist = false;
79 >         ((TH1*)hist)->Add((TH1*)obj);
80        }
81 +   }
82  
83 <      ((TH1*)hist)->Add((TH1*)obj);
84 <    }
85 <  }
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 <  //Add all histograms whose names match one of ten possible regular expression
88 <  //patterns or begin with one of ten possible given prefixes.  Feel free to
68 <  //mix and match regular expression patterns and prefixes.  If the final hist-
69 <  //ogram named outHistName does not exist it is created.
70 <
71 <  void add(const char* outHistName,   const char* patORpfx0,     const char* patORpfx1,     const char* patORpfx2,
72 <           const char* patORpfx3, const char* patORpfx4, const char* patORpfx5, const char* patORpfx6,
73 <           const char* patORpfx7, const char* patORpfx8, const char* patORpfx9)
74 <  {
75 <    add(outHistName, patORpfx0);
76 <    add(outHistName, patORpfx1);
77 <    if (patORpfx2) add(outHistName, patORpfx2);
78 <    if (patORpfx3) add(outHistName, patORpfx3);
79 <    if (patORpfx4) add(outHistName, patORpfx4);
80 <    if (patORpfx5) add(outHistName, patORpfx5);
81 <    if (patORpfx6) add(outHistName, patORpfx6);
82 <    if (patORpfx7) add(outHistName, patORpfx7);
83 <    if (patORpfx8) add(outHistName, patORpfx8);
84 <    if (patORpfx9) add(outHistName, patORpfx9);
85 <  }
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 <  //For all histograms whose names match the given regular expression pattern
89 <  //or begin with the given prefix, set the fill, line and marker colors to the
90 <  //given value.
97 >      TObject* obj = 0;
98  
99 <  void color(const char* patORpfx, Color_t color) {
100 <      
94 <    TRegexp reg(patORpfx, kFALSE);
99 >      while ( (obj = iter->Next()) ) {
100 >         if (! obj->InheritsFrom(TH1::Class())) continue;
101  
102 <    TList* list = gDirectory->GetList() ;
102 >         TString name = obj->GetName();
103  
104 <    TIterator* iter = list->MakeIterator();
104 >         if (TString(patORpfx).MaybeRegexp()) {
105 >            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
106 >         } else if (! name.BeginsWith(patORpfx)) continue;
107  
108 <    TObject* obj = 0;
108 >         ((TH1*)obj)->SetFillColor(color);
109 >         ((TH1*)obj)->SetLineColor(color);
110 >         ((TH1*)obj)->SetMarkerColor(color);
111 >      }
112 >   }
113  
114 <    while ((obj = iter->Next())) {
115 <      if (! obj->InheritsFrom(TH1::Class())) continue;
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 <      TString name = obj->GetName();
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 <      if (TString(patORpfx).MaybeRegexp()) {
125 <        if (TString(obj->GetName()).Index(reg) < 0 ) continue;
126 <      } else if (! name.BeginsWith(patORpfx)) continue;
124 >      TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
125 >      TList* list = canvas->GetListOfPrimitives();
126 >      TIterator* iter = list->MakeIterator();
127  
128 <      ((TH1*)obj)->SetFillColor(color);
112 <      ((TH1*)obj)->SetLineColor(color);
113 <      ((TH1*)obj)->SetMarkerColor(color);
114 <    }
115 <  }
128 >      TObject* obj = 0;
129  
130 +      //Hist color iterator
131 +      Int_t colorIt = 1;
132  
133 <  void pattern(const char* patORpfx, Color_t pattern) {
134 <    TRegexp reg(patORpfx, kFALSE);
120 <    
121 <    TList* list = gDirectory->GetList() ;
122 <    TIterator* iter = list->MakeIterator();
123 <    
124 <    TObject* obj = 0;
125 <
126 <    while ((obj = iter->Next())) {
127 <      if (! obj->InheritsFrom(TH1::Class())) continue;
128 <
129 <      TString name = obj->GetName();
130 <
131 <      if (TString(patORpfx).MaybeRegexp()) {
132 <        if (TString(obj->GetName()).Index(reg) < 0 ) continue;
133 <      } else if (! name.BeginsWith(patORpfx)) continue;
133 >      while ( (obj = iter->Next()) ) {
134 >         if (! obj->InheritsFrom(TH1::Class())) continue;
135  
136 <      ((TH1*)obj)->SetFillStyle(pattern);
137 <    }
138 <  }
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 <  //Return a pointer to a TLegend with an entry for each histogram drawn on a
179 <  //given TCanvas.  Display either the line, point or fill values.  Optionally
180 <  //apply colors to all histograms.  By default, entry labels are the names of
181 <  //their respective histograms.  Optionally, if histogram names are of the
182 <  //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
183 <
184 <  TLegend* legend(TCanvas* canvas, Option_t* option, Bool_t addColor, Int_t token,
185 <                  Float_t xmin, Float_t ymin, Float_t xmax, Float_t ymax) {
186 <    if(! canvas) return 0;
187 <
188 <    TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
189 <    TList* list = canvas->GetListOfPrimitives();
190 <    //       TIterator* iter = list->MakeIterator();
191 <
192 <    TObject* obj = 0;
157 <
158 <    //Hist color iterator
159 <    Int_t colorIt = 1;
160 <
161 <    //while (obj = iter->Next()) {
162 <    for(int i = list->GetSize() - 1 ; i >=0 ; i--) {
163 <      obj = list->At(i);
164 <      if (! obj->InheritsFrom(TH1::Class())) continue;
165 <
166 <      if (addColor) {
167 <        hist::color(obj->GetName(), colorIt);
168 <        ++colorIt;
169 <      }
170 <        
171 <
172 <      if (token == -1)
173 <        leg->AddEntry(obj, obj->GetName(), option);
174 <      else {
175 <        TString name(obj->GetName());
176 <        TObjArray* a = name.Tokenize("_");
177 <        if (a->GetEntries() <= token)
178 <          leg->AddEntry(obj, obj->GetName(), option);
179 <        else
180 <          leg->AddEntry(obj, a->At(token)->GetName(), option);
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        }
182    }
194  
195 <    return leg;
196 <  }
195 >      return leg;
196 >   }
197  
198 <  //Return a pointer to a TLegend with an entry for each histogram added to a
199 <  //given THStack.  Display either the line, point or fill values.  Optionally
189 <  //apply colors to all histograms.  By default, entry labels are the names of
190 <  //their respective histograms.  Optionally, if histogram names are of the
191 <  //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
192 <
193 <  TLegend* legend(THStack* stack, Option_t* option, Bool_t addColor, Int_t token,
194 <                  Float_t xmin, Float_t ymin, Float_t xmax, Float_t ymax,
195 <                  TH1F *hdata) {
196 <    
197 <    if(! stack) return 0;
198 <
199 <    TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
200 <    leg->SetFillColor(kWhite);
201 <    TList* list = stack->GetHists();
202 <    //       TIterator* iter = list->MakeIterator();
203 <
204 <    TObject* obj = 0;
205 <
206 <
207 <    if(hdata != NULL)
208 <      leg->AddEntry(hdata, "Data", "P");
209 <      
210 <
211 <    //Hist color iterator
212 <    Int_t colorIt = 1;
213 <
214 <    //while (obj = iter->Next()) {
215 <    for(int i = list->GetSize() - 1 ; i >=0 ; i--) {
216 <      obj = list->At(i);
217 <      if (! obj->InheritsFrom(TH1::Class())) continue;
218 <      TString name(obj->GetName());
219 <
220 <      if(name.Contains("QCD"))
221 <        continue;
222 <
223 <      if (addColor) {
224 <        hist::color(obj->GetName(), colorIt);
225 <        ++colorIt;
226 <      }
227 <        
228 <      if (token == -1)
229 <        leg->AddEntry(obj, obj->GetName(), option);
230 <      else {
231 <        TString name(obj->GetName());
232 <        TObjArray* a = name.Tokenize("_");
233 <        TString entry(a->GetEntries() <= token ? obj->GetName() : a->At(token)->GetName());
234 <        
235 <        if (entry == "t"){
236 <          entry = "Single top";
237 <        }else if (entry == "ttbar"){
238 <          entry = "#font[12]{t#bar{t}}";
239 <        } else if (entry == "ttdil"){
240 <          entry = "#font[12]{t#bar{t}} signal";
241 <        } else if (entry == "ttotr"){
242 <          entry = "#font[12]{t#bar{t}} other";
243 <        } else if (entry == "VV"){
244 <          //entry = "#font[12]{ZZ}, #font[12]{WZ}, #font[12]{ZZ}";
245 <          entry = "#font[12]{VV}";
246 <        } else if (entry == "DYtautau"){
247 <          //entry = "DY#rightarrow #tau#tau";
248 <          entry = "Z/#gamma*#rightarrow#tau^{+}#tau^{-}";
249 <        } else if (entry == "DYeemm"){
250 <          //entry = "DY#rightarrow #font[12]{ee}, #mu#mu";
251 <          entry = "Z/#gamma*#rightarrowl^{+}l^{-}";
252 <        } else if (entry == "wjets"){
253 <          //entry = "#font[12]{W}+jets";
254 <          entry = "#font[12]{W}#rightarrowl#nu";
255 <        }else {
256 <          entry.ReplaceAll("tautau", "#tau#tau");
257 <          entry.ReplaceAll("mm", "#mu#mu");
258 <        }
259 <        leg->AddEntry(obj, entry, option);
260 <      }
261 <    }
198 >   //Normalize to one all histograms whose names match the given regular exp-
199 >   //ression pattern or begin with the given prefix.
200  
201 <    return leg;
202 <  }
201 >   void normalize(const char* patORpfx) {
202 >      TRegexp reg(patORpfx, kFALSE);
203  
204 <  //Normalize to one all histograms whose names match the given regular exp-
205 <  //ression pattern or begin with the given prefix.
204 >      TList* list = gDirectory->GetList() ;
205 >      TIterator* iter = list->MakeIterator();
206  
207 <  void normalize(const char* patORpfx) {
270 <    TRegexp reg(patORpfx, kFALSE);
207 >      TObject* obj = 0;
208  
209 <    TList* list = gDirectory->GetList() ;
210 <    TIterator* iter = list->MakeIterator();
209 >      while ( (obj = iter->Next()) ) {
210 >         if (! obj->InheritsFrom(TH1::Class())) continue;
211  
212 <    TObject* obj = 0;
212 >         TString name = obj->GetName();
213  
214 <    while ((obj = iter->Next())) {
215 <      if (! obj->InheritsFrom(TH1::Class())) continue;
214 >         if (TString(patORpfx).MaybeRegexp()) {
215 >            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
216 >         } else if (! name.BeginsWith(patORpfx)) continue;
217  
218 <      TString name = obj->GetName();
218 >         Double_t integral = 0;
219  
220 <      if (TString(patORpfx).MaybeRegexp()) {
221 <        if (TString(obj->GetName()).Index(reg) < 0 ) continue;
222 <      } else if (! name.BeginsWith(patORpfx)) continue;
220 >         if (obj->InheritsFrom(TH2::Class()))
221 >            integral = ((TH2*)obj)->Integral();
222 >         else
223 >            integral = ((TH1*)obj)->Integral();
224  
225 <      Double_t integral = 0;
225 >         if (integral) {
226 >            ((TH1*)obj)->Sumw2();
227 >            ((TH1*)obj)->Scale(1./integral);
228 >         }
229 >      }
230 >   }
231  
232 <      if (obj->InheritsFrom(TH2::Class()))
233 <        integral = ((TH2*)obj)->Integral();
234 <      else
235 <        integral = ((TH1*)obj)->Integral();
232 >   //Scale by the given value all histograms whose names match the given regular
233 >   //expression pattern or begin with the given prefix.
234 >
235 >   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();
247  
248 <      if (integral) {
249 <        ((TH1*)obj)->Sumw2();
250 <        ((TH1*)obj)->Scale(1./integral);
248 >         if (TString(patORpfx).MaybeRegexp()) {
249 >            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
250 >         } else if (! name.BeginsWith(patORpfx)) continue;
251 >
252 >         ((TH1*)obj)->Sumw2();
253 >         ((TH1*)obj)->Scale(scale);
254        }
255 <    }
298 <  }
255 >   }
256  
257 <  //Scale by the given value all histograms whose names match the given regular
258 <  //expression pattern or begin with the given prefix.
257 >   //Don't you hate it when you draw multiple histograms on the same canvas only
258 >   //to find that the bottom histogram's range does not encompass those of the
259 >   //histograms drawn on top?  This method determines the maximum and minimum y
260 >   //range of all the histograms drawn on a given TCanvas and appropriately re-
261 >   //sizes the bottom histogram.
262  
263 <  void scale(const char* patORpfx, Double_t scale) {
264 <    TRegexp reg(patORpfx, kFALSE);
263 >   void setrangey(TCanvas* canvas) {
264 >      if(! canvas) return;
265  
266 <    TList* list = gDirectory->GetList()   ;
267 <    TIterator* iter = list->MakeIterator();
266 >      TList* list = canvas->GetListOfPrimitives();
267 >      TIterator* iter = list->MakeIterator();
268  
269 <    TObject* obj = 0;
269 >      TObject* obj = 0;
270 >      TObject* top = 0;
271  
272 <    while ((obj = iter->Next())) {
273 <      if (! obj->InheritsFrom(TH1::Class())) continue;
272 >      //Extremes
273 >      Double_t maxy = -999999;
274 >      Double_t miny = 999999;
275  
276 <      TString name = obj->GetName();
276 >      while ( (obj = iter->Next()) ) {
277 >         if (! obj->InheritsFrom(TH1::Class())) continue;
278  
279 <      if (TString(patORpfx).MaybeRegexp()) {
317 <        if (TString(obj->GetName()).Index(reg) < 0 ) continue;
318 <      } else if (! name.BeginsWith(patORpfx)) continue;
279 >         if (! top) top = obj;
280  
281 +         if (((TH1*)obj)->GetMaximum() > maxy) maxy = ((TH1*)obj)->GetMaximum();
282 +         if (((TH1*)obj)->GetMinimum() < miny) miny = ((TH1*)obj)->GetMinimum();
283 +      }
284  
285 <      if ((((TH1*)obj)->GetNbinsX()+2) != ((TH1*)obj)->GetSumw2N())        
286 <           ((TH1*)obj)->Sumw2();
285 >      ((TH1*)top)->SetMaximum(maxy*1.3);
286 >      //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 >   }
292  
293 <      ((TH1*)obj)->Scale(scale);
294 <    }
295 <  }
293 >   //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.
299  
300 <  //Don't you hate it when you draw multiple histograms on the same canvas only
301 <  //to find that the bottom histogram's range does not encompass those of the
330 <  //histograms drawn on top?  This method determines the maximum and minimum y
331 <  //range of all the histograms drawn on a given TCanvas and appropriately re-
332 <  //sizes the bottom histogram.
300 >   void stack(const char* stackHistName, const char* patORpfx, Bool_t addColor = kFALSE, Option_t* drawOption = "") {
301 >      TRegexp reg(patORpfx, kFALSE);
302  
303 <  void setrangey(TCanvas* canvas) {
304 <    if(! canvas) return ;
303 >      TList* list = gDirectory->GetList() ;
304 >      TIterator* iter = list->MakeIterator();
305  
306 <    TList* list = canvas->GetListOfPrimitives();
307 <    TIterator* iter = list->MakeIterator();
306 >      TObject* obj = 0;
307 >      TObject* stack = 0;
308 >      Bool_t makeStackHist = false;
309  
310 <    TObject* obj = 0;
311 <    TObject* top = 0;
310 >      stack = gDirectory->Get(stackHistName);
311 >      //If stack hist does not exist, remember to create it
312 >      if (! stack) makeStackHist = true;
313  
314 <    //Extremes
315 <    Double_t maxy = -999999;
345 <    Double_t miny = 999999;
314 >      //Hist color iterator
315 >      Int_t colorIt = 1;
316  
317 <    while ((obj = iter->Next())) {
318 <      if (! obj->InheritsFrom(TH1::Class())) continue;
317 >      while ( (obj = iter->Next()) ) {
318 >         if (! obj->InheritsFrom(TH1::Class())) continue;
319  
320 <      if (! top) top = obj;
320 >         TString name = obj->GetName();
321  
322 <      if (((TH1*)obj)->GetMaximum() > maxy) maxy = ((TH1*)obj)->GetMaximum();
323 <      if (((TH1*)obj)->GetMinimum() < miny) miny = ((TH1*)obj)->GetMinimum();
324 <    }
322 >         if (TString(patORpfx).MaybeRegexp()) {
323 >            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
324 >         } else if (! name.BeginsWith(patORpfx)) continue;
325  
326 <    ((TH1*)top)->SetMaximum(maxy*1.3);
327 <    //Protect against log scale
328 <    if (canvas->GetLogy() && ! miny)
329 <      ((TH1*)top)->SetMinimum(1E-3);
360 <    else
361 <      ((TH1*)top)->SetMinimum(miny*0.7);
362 <  }
326 >         if (makeStackHist) {
327 >            stack = new THStack(stackHistName, stackHistName);
328 >            makeStackHist = false;
329 >         }
330  
331 <  //Create a stacked histogram consisting of all histograms whose names match
332 <  //the given regular expression pattern or begin with the given prefix.  If
333 <  //the THStack named stackHistName does not exist it is created.  Optionally
334 <  //apply colors to all histograms.  Set drawOption to "nostack" if you do not
368 <  //want to stack, to "hist" to display histograms without errors, to "histe"
369 <  //to display histograms with errors, etc.
370 <
371 <  void stack(const char* stackHistName, const char* patORpfx, Bool_t addColor, Option_t* drawOption, Int_t orderScheme,
372 <             const char* bsmName, bool doRefPats, std::string prefixToSkip){//, float yMinimum) {
373 <    TRegexp reg(patORpfx, kTRUE);
374 <      
375 <    TList* list = gDirectory->GetList() ;
376 <
377 <    TObject* obj = 0;
378 <    TObject* stack = 0;
379 <    Bool_t makeStackHist = false;
380 <
381 <    stack = gDirectory->Get(stackHistName);
382 <    //If stack hist does not exist, remember to create it
383 <    if (! stack) makeStackHist = true;
384 <
385 <    //Hist color iterator
386 <    Int_t colorIt = 1;
387 <
388 <    std::vector<TString*>  pats(0);
389 <    if (orderScheme == 0){
390 <      pats.push_back(new TString(""));
391 <      //pats.push_back(new TString(""));
392 <      //pats.push_back(new TString("ttdil_"));
393 <      //pats.push_back(new TString("ttotr_"));
394 <      //pats.push_back(new TString("DYeemm_"));
395 <      //pats.push_back(new TString("DYtautau_"));
396 <      //pats.push_back(new TString("wjets_"));
397 <      //pats.push_back(new TString("VV_"));
398 <      //pats.push_back(new TString("tw_"));
399 <      //pats.push_back(new TString("data_"));      
400 <    } else if (orderScheme == 1) {
401 <      //this requires some care: only the histograms beginning with what's in the pattern will be processed
402 <      pats.push_back(new TString("ttdil_"));
403 <      pats.push_back(new TString("ttotr_"));
404 <      pats.push_back(new TString("t_"));
405 <      pats.push_back(new TString("VV_"));
406 <      pats.push_back(new TString("DYtautau_"));
407 <      pats.push_back(new TString("DYeemm_"));
408 <      pats.push_back(new TString("wjets_"));
409 <      pats.push_back(new TString("QCD_"));
410 <      //      pats.push_back(new TString("Vgamma_"));
411 <
412 <      if (doRefPats){
413 <        pats.push_back(new TString("ref_ttdil_"));
414 <        pats.push_back(new TString("ref_ttotr_"));
415 <        pats.push_back(new TString("ref_t_"));
416 <        pats.push_back(new TString("ref_VV_"));
417 <        pats.push_back(new TString("ref_DYtautau_"));
418 <        pats.push_back(new TString("ref_DYeemm_"));
419 <        pats.push_back(new TString("ref_wjets_"));
420 <        pats.push_back(new TString("ref_QCD_"));
421 <        //      pats.push_back(new TString("ref_Vgamma_"));
422 <      }
423 <    } else if (orderScheme == 2) {
424 <      //this requires some care: only the histograms beginning with what's in the pattern will be processed
425 <      pats.push_back(new TString("ttdil_"));
426 <      pats.push_back(new TString("ttotr_"));
427 <      pats.push_back(new TString("t_"));
428 <      pats.push_back(new TString("VV_"));
429 <      pats.push_back(new TString("DYtautau_"));
430 <      pats.push_back(new TString("DYeemm_"));
431 <      pats.push_back(new TString("wjets_"));
432 <      pats.push_back(new TString("QCD_"));
433 <      pats.push_back(new TString("Vgamma_"));
434 <      if (bsmName!=0) pats.push_back(new TString(Form("%s_",bsmName)));
435 <
436 <      if (doRefPats){
437 <        pats.push_back(new TString("ref_ttdil_"));
438 <        pats.push_back(new TString("ref_ttotr_"));
439 <        pats.push_back(new TString("ref_t_"));
440 <        pats.push_back(new TString("ref_VV_"));
441 <        pats.push_back(new TString("ref_DYtautau_"));
442 <        pats.push_back(new TString("ref_DYeemm_"));
443 <        pats.push_back(new TString("ref_wjets_"));
444 <        pats.push_back(new TString("ref_QCD_"));
445 <        pats.push_back(new TString("ref_Vgamma_"));
446 <        if(bsmName!=0) pats.push_back(new TString(Form("ref_%s_",bsmName)));
447 <      }
448 <    } else {
449 <      pats.push_back(new TString(""));
450 <    }
331 >         if (addColor) {
332 >            hist::color(obj->GetName(), colorIt);
333 >            ++colorIt;
334 >         }
335  
336 <    for (unsigned int iPat = 0; iPat < pats.size(); ++iPat) {
453 <      TIterator* iter = list->MakeIterator();
454 <      while ((obj = iter->Next())) {
455 <        if (! obj->InheritsFrom(TH1::Class())) continue;
456 <        //((TH1F*)obj)->SetMinimum(yMinimum);
457 <        TString name = obj->GetName();
458 <        if(prefixToSkip != "" && name.BeginsWith(prefixToSkip.c_str())) continue;
459 <        //if(name.Contains("data"))
460 <        if (!( name.BeginsWith(pats[iPat]->Data()))) continue;
461 <        
462 <        if (TString(patORpfx).MaybeRegexp()) {
463 <          if (TString(obj->GetName()).Index(reg) < 0 ) continue;
464 <        } else if (! name.BeginsWith(patORpfx)) continue;
465 <        
466 <        if (makeStackHist) {
467 <          stack = new THStack(stackHistName, stackHistName);
468 <          makeStackHist = false;
469 <        }
470 <        
471 <        if (addColor) {
472 <          hist::color(obj->GetName(), colorIt);
473 <          ++colorIt;
474 <        }
475 <        ((THStack*)stack)->Add((TH1*)obj, drawOption);
476 <        
336 >         ((THStack*)stack)->Add((TH1*)obj, drawOption);
337        }
478      //      delete pats[iPat];//cleanup
479    }
480  
338  
339 <    // Currently breaks .ls
340 <    //gDirectory->Append(stack);
341 <  }
485 <  
486 <  //Set the x-axis title of all histograms whose names match the given regular
487 <  //expression pattern or begin with the given prefix.
488 <  
489 <  void xaxis(const char* patORpfx, const char* title) {
490 <    TRegexp reg(patORpfx, kFALSE);
491 <
492 <    TList* list = gDirectory->GetList() ;
493 <    TIterator* iter = list->MakeIterator();
494 <
495 <    TObject* obj = 0;
496 <
497 <    while ((obj = iter->Next())) {
498 <      if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
499 <
500 <      TString name = obj->GetName();
501 <
502 <      if (TString(patORpfx).MaybeRegexp()) {
503 <        if (TString(obj->GetName()).Index(reg) < 0 ) continue;
504 <      } else if (! name.BeginsWith(patORpfx)) continue;
505 <
506 <      if (obj->InheritsFrom(TH1::Class()))
507 <        ((TH1*)obj)->GetXaxis()->SetTitle(title);
508 <      if (obj->InheritsFrom(THStack::Class())) {
509 <        ((THStack*)obj)->Draw();
510 <        ((THStack*)obj)->GetXaxis()->SetTitle(title);
511 <      }
512 <    }
513 <  }
339 >      // Currently breaks .ls
340 >      //gDirectory->Append(stack);
341 >   }
342  
343 <  //Set the y-axis title of all histograms whose names match the given regular
344 <  //expression pattern or begin with the given prefix.
343 >   //Set the x-axis title of all histograms whose names match the given regular
344 >   //expression pattern or begin with the given prefix.
345  
346 <  void yaxis(const char* patORpfx, const char* title) {
347 <    TRegexp reg(patORpfx, kFALSE);
346 >   void xaxis(const char* patORpfx, const char* title) {
347 >      TRegexp reg(patORpfx, kFALSE);
348  
349 <    TList* list = gDirectory->GetList() ;
350 <    TIterator* iter = list->MakeIterator();
349 >      TList* list = gDirectory->GetList() ;
350 >      TIterator* iter = list->MakeIterator();
351  
352 <    TObject* obj = 0;
352 >      TObject* obj = 0;
353  
354 <    while ((obj = iter->Next())) {
355 <      if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
354 >      while ( (obj = iter->Next()) ) {
355 >         if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
356  
357 <      TString name = obj->GetName();
357 >         TString name = obj->GetName();
358  
359 <      if (TString(patORpfx).MaybeRegexp()) {
360 <        if (TString(obj->GetName()).Index(reg) < 0 ) continue;
361 <      } else if (! name.BeginsWith(patORpfx)) continue;
359 >         if (TString(patORpfx).MaybeRegexp()) {
360 >            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
361 >         } else if (! name.BeginsWith(patORpfx)) continue;
362  
363 <      if (obj->InheritsFrom(TH1::Class()))
364 <        ((TH1*)obj)->GetYaxis()->SetTitle(title);
365 <      if (obj->InheritsFrom(THStack::Class())) {
366 <        ((THStack*)obj)->Draw();
367 <        ((THStack*)obj)->GetYaxis()->SetTitle(title);
363 >         if (obj->InheritsFrom(TH1::Class()))
364 >            ((TH1*)obj)->GetXaxis()->SetTitle(title);
365 >         if (obj->InheritsFrom(THStack::Class())) {
366 >            ((THStack*)obj)->Draw();
367 >            ((THStack*)obj)->GetXaxis()->SetTitle(title);
368 >         }
369        }
370 <    }
542 <  }
370 >   }
371  
372 <  // Input:  2 histogram
373 <  // Output: one histogram which is the efficiency:
546 <  // h1 :  TOTAL NUMBER OF EVENTS
547 <  // h2 :  NUMBER OF EVENTS THAT PASS
372 >   //Set the y-axis title of all histograms whose names match the given regular
373 >   //expression pattern or begin with the given prefix.
374  
375 < #include "TH1.h"
375 >   void yaxis(const char* patORpfx, const char* title) {
376 >      TRegexp reg(patORpfx, kFALSE);
377  
378 <  // Method by pointer
379 <  TH1F* eff(TH1F* h1, TH1F* h2, const char* name){
378 >      TList* list = gDirectory->GetList() ;
379 >      TIterator* iter = list->MakeIterator();
380  
381 <    // first, verify that all histograms have same binning
555 <    // nx is the number of visible bins
556 <    // nxtot = nx+2 includes underflow and overflow
557 <    Int_t nx = h1->GetNbinsX();
558 <    if (h2->GetNbinsX() != nx) {
559 <      cout << "Histograms must have same number of bins" << endl;
560 <      return 0;
561 <    }
381 >      TObject* obj = 0;
382  
383 <    // get the new histogram
384 <    TH1F* temp = (TH1F*) h1->Clone(name);
565 <    temp->SetTitle(name);
566 <    temp->Reset();
567 <    temp->Sumw2();
383 >      while ( (obj = iter->Next()) ) {
384 >         if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
385  
386 <    // Do the calculation
570 <    temp->Divide(h2,h1,1.,1.,"B");
386 >         TString name = obj->GetName();
387  
388 <    // Done
389 <    return temp;
390 <  }
388 >         if (TString(patORpfx).MaybeRegexp()) {
389 >            if (TString(obj->GetName()).Index(reg) < 0 ) continue;
390 >         } else if (! name.BeginsWith(patORpfx)) continue;
391  
392 +         if (obj->InheritsFrom(TH1::Class()))
393 +            ((TH1*)obj)->GetYaxis()->SetTitle(title);
394 +         if (obj->InheritsFrom(THStack::Class())) {
395 +            ((THStack*)obj)->Draw();
396 +            ((THStack*)obj)->GetYaxis()->SetTitle(title);
397 +         }
398 +      }
399 +   }
400  
401 <  // Method by name
402 <  TH1F* eff(const char* name1, const char* name2, const char* name){
401 > }
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 <    // Get a list of object and their iterator
581 <    TList* list = gDirectory->GetList() ;
582 <    TIterator* iter = list->MakeIterator();
583 <
584 <    // Loop over objects, set the pointers
585 <    TObject* obj;
586 <    TH1F* h1=0;
587 <    TH1F* h2=0;
588 <    TString str1 = Form("%s",name1);
589 <    TString str2 = Form("%s",name2);
590 <    while((obj=iter->Next())) {
591 <      TString objName = obj->GetName();
592 <      if (objName == str1) h1 = (TH1F*) obj;
593 <      if (objName == str2) h2 = (TH1F*) obj;
594 <    }
407 > #include "TH1.h"
408  
409 <    // quit if not found
410 <    if (h1 == 0) {
598 <      cout << "Histogram " << name1 << " not found" << endl;
599 <      return 0;
600 <    }
601 <    if (h2 == 0) {
602 <      cout << "Histogram " << name2 << " not found" << endl;
603 <      return 0;
604 <    }
409 > // Method by pointer
410 > TH1F* eff(TH1F* h1, TH1F* h2, const char* name="eff"){
411  
412 <    // Call the method by pointer
413 <    TH1F* temp = eff(h1, h2, name);
414 <    return temp;
415 <  }
416 <  // Input:  4 histogram
417 <  // Output: one histogram which is the BG subtracted efficiency:
418 <  // h1 :  TOTAL NUMBER OF EVENTS, SIGNAL REGION
419 <  // h2 :  NUMBER OF EVENTS THAT PASS, SIGNAL REGION
420 <  // h3 :  TOTAL NUMBER OF EVENTS, SIDE BAND
421 <  // h4 :  NUMBER OF EVENTS THAT PASS, SIDE BAND
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();
426 >
427 >  // Do the calculation
428 >  temp->Divide(h2,h1,1.,1.,"B");
429 >
430 >  // Done
431 >  return temp;
432 > }
433 >
434 >
435 > // Method by name
436 > TH1F* eff(const char* name1, const char* name2, const char* name="eff"){
437 >
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 > }
468 > // Input:  4 histogram
469 > // Output: one histogram which is the BG subtracted efficiency:
470 > // h1 :  TOTAL NUMBER OF EVENTS, SIGNAL REGION
471 > // h2 :  NUMBER OF EVENTS THAT PASS, SIGNAL REGION
472 > // h3 :  TOTAL NUMBER OF EVENTS, SIDE BAND
473 > // h4 :  NUMBER OF EVENTS THAT PASS, SIDE BAND
474  
475   #include "TH1.h"
476  
477  
478 <  TH1F* eff_bg(TH1F* h1, TH1F* h2, TH1F* h3, TH1F* h4, const char* name){
478 > TH1F* eff_bg(TH1F* h1, TH1F* h2, TH1F* h3, TH1F* h4, const char* name="eff"){
479  
480 <    // first, verify that all histograms have same binning
481 <    // nx is the number of visible bins
482 <    // 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;
480 >  // first, verify that all histograms have same binning
481 >  // nx is the number of visible bins
482 >  // 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 <
518 <    // get the new histogram
519 <    TH1F* temp = (TH1F*) h1->Clone(name);
520 <    temp->SetTitle(name);
521 <    temp->Reset();
522 <    temp->Sumw2();
523 <
524 <    // Loop over bins, calculate efficiency and error, put it in histogram
525 <    for (Int_t i=0; i<nxtot; i++) {
648 <      Double_t x1 = h1->GetBinContent(i);
649 <      Double_t x2 = h2->GetBinContent(i);
650 <      Double_t x3 = h3->GetBinContent(i);
651 <      Double_t x4 = h4->GetBinContent(i);
652 <      Double_t denom = x1 - x3;
653 <      Double_t eff;
654 <      if (denom == 0.) {
655 <        eff = 0;
656 <      } else {
657 <        eff   = (x2-x4)/denom;
658 <      }
659 <      Double_t failSig = x1 - x2;
660 <      Double_t failBg  = x3 - x4;
661 <      Double_t blah    = (1-eff)*(1-eff)*(x2+x4) + eff*eff*(failSig+failBg);
662 <      if (blah <= 0.) blah=0.0;
663 <      Double_t err;
664 <      if (denom == 0) {
665 <        err = 0.;
666 <      } else {
667 <        err = sqrt(blah)/denom;
668 <      }
669 <      temp->SetBinContent(i,eff);
670 <      temp->SetBinError(i,err);
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 <
528 <    // Done
674 <    return temp;
527 >    temp->SetBinContent(i,eff);
528 >    temp->SetBinError(i,err);
529    }
530  
531 +  // Done
532 +  return temp;
533 + }
534 +
535   #include <TList.h>
536   #include <TIterator.h>
537  
538 <  void deleteHistos() {
539 <    // Delete all existing histograms in memory
540 <    TObject* obj;
541 <    TList* list = gDirectory->GetList() ;
542 <    TIterator* iter = list->MakeIterator();
543 <    while ((obj=iter->Next())) {
544 <      if (obj->IsA()->InheritsFrom(TH1::Class()) ||
545 <          obj->IsA()->InheritsFrom(TH2::Class()) ) {delete obj;}
546 <    }
547 <  }
548 <  void histio()
549 <  {
550 <  }
538 > void deleteHistos() {
539 >   // Delete all existing histograms in memory
540 >   TObject* obj;
541 >   TList* list = gDirectory->GetList() ;
542 >   TIterator* iter = list->MakeIterator();
543 >   while ( (obj=iter->Next()) ) {
544 >     if (obj->IsA()->InheritsFrom(TH1::Class()) ||
545 >         obj->IsA()->InheritsFrom(TH2::Class()) ) {delete obj;}
546 >   }
547 > }
548 >
549 > void histio()
550 > {
551 > }
552  
553 <  void saveHist(const char* filename, const char* pat)
554 <  {
555 <    TList* list = gDirectory->GetList() ;
556 <    TIterator* iter = list->MakeIterator();
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) ;
558 >   TRegexp re(pat,kTRUE) ;
559  
560 <    TFile outf(filename,"RECREATE") ;
561 <    while(TObject* obj=iter->Next()) {
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() ;
563 >         obj->Write() ;
564 >         cout << "." ;
565 >         cout.flush() ;
566 >      }
567 >   }
568 >   cout << endl ;
569 >   outf.Close() ;
570  
571 <    delete iter ;
572 <  }
571 >   delete iter ;
572 > }
573  
574  
575 <  void loadHist(const char* filename, const char* pfx, const char* pat, Bool_t doAdd)
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 <    if (pat!=0) cout << "pat = " << pat << endl ;
584 <    else cout<<"no pattern: read all"<<endl;
585 <
586 <    gDirectory->cd("Rint:") ;
587 <
588 <    TObject* obj ;
589 <    TKey* key ;
590 <    cout << "doAdd = " << (doAdd?"T":"F") << endl ;
591 <    cout << "loadHist: reading." ;
592 <    while((key=(TKey*)iter->Next())) {
593 <
594 <      //      Int_t ridx = TString(key->GetName()).Index(re) ;
595 <      //      cout<<"reading "<<key->GetName()<<std::endl;
737 <      if (pat!=0){
738 <        //      cout<<"Check patt "<<pat<<endl;
739 <        if (TString(key->GetName()).Index(re)==-1) {
740 <          continue ;
741 <        }
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 <        }
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 <        }
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) ;
637 >         gDirectory->Append(clone) ;
638        }
639 <      //cout << "." ;
639 >      cout << "." ;
640        cout.flush() ;
641 <    }
642 <    cout << endl;
643 <    inf.Close() ;
644 <    delete iter ;
791 <  }
792 <
793 <
794 <  //takes in a vector of prefixes, and a output prefix. Combines all histograms
795 <  //whose prefixes matches those in the input vector of TStrings into a new histogram
796 <  //with the the outPrefix
797 <  void combineHists(const std::vector<TString> v_prfxsToCombine, const TString outPrefix) {
798 <
799 <    
800 <    if(v_prfxsToCombine.size() == 0) {
801 <      cout << "Input vector size is 0" << endl;
802 <    }
803 <    //histogram of common suffixes
804 <    vector<TString> v_suffixes;
805 <    //get the list of histograms that match the first entry in the prefix
806 <    TString temp = v_prfxsToCombine.at(0);
807 <    TList *rootlist = gDirectory->GetList();
808 <    
809 <    for(int i = 0; i < rootlist->GetSize(); i++) {
810 <      
811 <      TObject *obj = rootlist->At(i);
812 <      TString name = obj->GetName();
813 <      if(!obj->InheritsFrom(TH1::Class()))
814 <        continue;
815 <      if(!name.BeginsWith(temp+"_"))
816 <        continue;
817 <
818 <      name.ReplaceAll(temp+"_", "_");
819 <
820 <      
821 <      if(obj->InheritsFrom(TH2::Class())) {
822 <        TH2F *h = dynamic_cast<TH2F*>(obj->Clone());
823 <        h->SetName((outPrefix+name).Data());
824 <        h->SetTitle((outPrefix+name).Data());
825 <        for(unsigned int j = 1; j < v_prfxsToCombine.size(); j++) {
826 <          TH2F *htemp = dynamic_cast<TH2F*>(gDirectory->Get((v_prfxsToCombine.at(j) + name).Data()));
827 <          h->Add(htemp);
828 <        }      
829 <      } else if(obj->InheritsFrom(TH1::Class())) {
830 <        TH1F *h = dynamic_cast<TH1F*>(obj->Clone());
831 <        h->SetName((outPrefix+name).Data());
832 <        h->SetTitle((outPrefix+name).Data());
833 <        for(unsigned int j = 1; j < v_prfxsToCombine.size(); j++) {
834 <          TH1F *htemp = dynamic_cast<TH1F*>(gDirectory->Get((v_prfxsToCombine.at(j) + name).Data()));
835 <          //cout << "TH1F: " << v_prfxsToCombine.at(j) + name << endl;
836 <          if(htemp == NULL)
837 <            cout << "htemp is NULL" << endl;
838 <          h->Add(htemp);
839 <        }
840 <      }
841 <    }//rootlist loop
842 <
843 <
844 <    //now delete the histos that match the prfxs to combine
845 <    for(unsigned int i = 0; i < v_prfxsToCombine.size(); i++ ) {
846 <      
847 <      // Delete all existing histograms in memory
848 <      TObject* obj;
849 <      TList* list = gDirectory->GetList() ;
850 <      TIterator* iter = list->MakeIterator();
851 <      while ((obj=iter->Next())) {
852 <        TString name = obj->GetName();
853 <        if(name.BeginsWith(outPrefix+"_"))
854 <          continue;
855 <        if(TString(obj->GetName()).BeginsWith(v_prfxsToCombine.at(i).Data())) {
856 <          if (obj->IsA()->InheritsFrom(TH1::Class()) ||
857 <              obj->IsA()->InheritsFrom(TH2::Class()) )
858 <            delete obj;
859 <        }
860 <      }
861 <    }//loop over prefixes
862 <  
863 <  }//fnc declaration
864 <  
641 >   }
642 >   cout << endl;
643 >   inf.Close() ;
644 >   delete iter ;
645   }
866 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines