ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/tcmet/histtools.C
Revision: 1.2
Committed: Tue Jan 26 06:46:07 2010 UTC (15 years, 3 months ago) by benhoob
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +808 -644 lines
Error occurred while calculating annotation data.
Log Message:
Added function to get TGraph from TH2

File Contents

# Content
1 #include "TList.h"
2 #include "TObjArray.h"
3 #include "TH1.h"
4 #include "TH2.h"
5 #include "TLegend.h"
6 #include "THStack.h"
7 #include "TCanvas.h"
8 #include "TFile.h"
9 #include "TRegexp.h"
10 #include "TKey.h"
11 #include "TROOT.h"
12 #include <iostream>
13 #include "histtools.h"
14 #include <vector>
15 namespace hist {
16
17 /*
18 void plotHistos(vector<TH1F*> h, vector<char*> c,
19 char* title,char* xtitle, float min,
20 bool drawLeg, bool log, bool norm){
21
22 if(log){
23 gPad->SetLogy(1);
24 if(min == 0.) cout<<"ERROR LOG PLOT WITH MIN SET TO ZERO"<<endl;
25 }
26
27 int nh = (int)h.size();
28 if(nh.size()!=c.size()) cout<<"ERROR HIST SIZE NOT EQUAL TO CHAR SIZE"<<endl;
29
30 int colors[]={1,2,4,5,6,7,8};
31
32 for(int i=0;i<nh;i++){
33 h.at(i)->SetFillColor(0);
34 h.at(i)->SetLineWidth(2);
35 h.at(i)->SetLineColor(colors[i]);
36 h.at(i)->SetMarkerColor(colors[i]);
37
38 if(norm){
39 if(h.at(i)->GetEntries() > 0) h.at(i)->Scale(1./h.at(i)->GetEntries());
40 }
41 }
42 h.at(0)->SetTitle(title);
43 h.at(0)->GetXaxis()->SetTitle(xtitle);
44 h.at(0)->Draw();
45 float max=h.at(0)->GetMaximum();
46
47 for(int i=1;i<nh;i++){
48 h.at(i)->Draw();
49 if(h.at(i)->GetMaximum() > max) max=h.at(i)->GetMaximum();
50 }
51
52 if(log) h.at(0)->SetMaximum(1.5*max);
53 else h.at(0)->SetMaximum(1.1*max);
54 h.at(0)->SetMinimum(min);
55
56 if(drawLeg){
57 TLegend *leg=new TLegend(0.6,0.6,0.85,0.85);
58 for(int i=0;i<nh;i++) leg->AddEntry(h.at(i),c.at(i));
59 leg->SetFillColor(0);
60 leg->SetBorderSize(1);
61 leg->Draw();
62 }
63
64 }
65 */
66
67 void plotHistos(TH1F* h1, TH1F* h2,TH1F* h3,
68 char* c1, char* c2, char* c3,
69 char* title,char* xtitle, float min,
70 bool drawLeg, bool log, bool norm){
71
72 if(log){
73 gPad->SetLogy(1);
74 if(min == 0.) cout<<"ERROR LOG PLOT WITH MIN SET TO ZERO"<<endl;
75 }
76
77 h1->SetFillColor(0);
78 h2->SetFillColor(0);
79 h3->SetFillColor(0);
80 h1->SetLineWidth(2);
81 h2->SetLineWidth(2);
82 h3->SetLineWidth(2);
83 h1->SetLineColor(1);
84 h2->SetLineColor(2);
85 h3->SetLineColor(4);
86 h2->SetMarkerColor(2);
87 h3->SetMarkerColor(4);
88 h1->SetTitle(title);
89 h1->GetXaxis()->SetTitle(xtitle);
90
91 if(norm){
92 if(h1->GetEntries() > 0) h1->Scale(1./h1->GetEntries());
93 if(h2->GetEntries() > 0) h2->Scale(1./h2->GetEntries());
94 if(h3->GetEntries() > 0) h3->Scale(1./h3->GetEntries());
95 }
96
97 h1->Draw();
98 h2->Draw("same");
99 h3->Draw("same");
100
101 float max=h1->GetMaximum();
102 if(h2->GetMaximum()>max) max=h2->GetMaximum();
103 if(h3->GetMaximum()>max) max=h3->GetMaximum();
104
105 if(log) h1->SetMaximum(1.5*max);
106 else h1->SetMaximum(1.1*max);
107 h1->SetMinimum(min);
108
109 if(drawLeg){
110 TLegend *leg=new TLegend(0.6,0.6,0.85,0.85);
111 leg->AddEntry(h1,c1);
112 leg->AddEntry(h2,c2);
113 leg->AddEntry(h3,c3);
114 leg->SetFillColor(0);
115 leg->SetBorderSize(1);
116 leg->Draw();
117 }
118
119 }
120
121
122
123 TGraphErrors *getTGraphFromTH2(TH2* h,vector<float> xbins, int method){
124
125 const int nbins = (int)xbins.size()-1;
126 TH2* htemp[nbins];
127 TH1* hx[nbins];
128 TH1* hy[nbins];
129
130 float x[nbins];
131 float y[nbins];
132 float xerr[nbins];
133 float yerr[nbins];
134
135 for(int ibin=0;ibin<nbins;ibin++){
136
137 htemp[ibin] = hist::suppressHist(h,ibin,xbins.at(ibin),xbins.at(ibin+1));
138 float width = xbins.at(ibin+1) - xbins.at(ibin);
139 hx[ibin] = htemp[ibin]->ProjectionX();
140 hy[ibin] = htemp[ibin]->ProjectionY();
141
142 x[ibin] = hx[ibin]->GetMean(1);
143 xerr[ibin] = width/2.;
144
145 if(method == 0){
146 y[ibin] = hy[ibin]->GetMean(1);
147 yerr[ibin] = (hy[ibin]->GetEntries()>0) ?
148 hy[ibin]->GetRMS(1)/sqrt(hy[ibin]->GetEntries()) : 0.;
149 }
150 if(method == 1){
151 y[ibin] = hy[ibin]->GetRMS(1);
152 yerr[ibin] = 0.;
153 }
154
155 }
156
157 TGraphErrors *g=new TGraphErrors(nbins,x,y,xerr,yerr);
158 g->GetXaxis()->SetLimits(xbins.at(0),xbins.at(nbins));
159
160 return g;
161 }
162
163 TH2* suppressHist(TH2* hist,int iclone,float xmin,float xmax){
164
165 TH2F* h=new TH2F(Form("%s_%s%i",hist->GetName(),"clone",iclone),hist->GetTitle(),
166 hist->GetNbinsX(),hist->GetXaxis()->GetXmin(),hist->GetXaxis()->GetXmax(),
167 hist->GetNbinsY(),hist->GetYaxis()->GetXmin(),hist->GetYaxis()->GetXmax());
168
169 for(int ibinx=1;ibinx<=hist->GetNbinsX();ibinx++){
170 for(int ibiny=1;ibiny<=hist->GetNbinsY();ibiny++){
171 if(hist->GetBinCenter(ibinx)>xmin && hist->GetBinCenter(ibinx)<xmax){
172 h->SetBinContent(ibinx,ibiny,hist->GetBinContent(ibinx,ibiny));
173 }else{
174 h->SetBinContent(ibinx,ibiny,0);
175 }
176 }
177 }
178 return h;
179 }
180
181 //Add all histograms whose names match one of ten possible regular expression
182 //patterns or begin with one of ten possible given prefixes. Feel free to
183 //mix and match regular expression patterns and prefixes. If the final hist-
184 //ogram named outHistName does not exist it is created.
185
186 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)
187 {
188 add(outHistName, patORpfx0);
189 add(outHistName, patORpfx1);
190 if (patORpfx2) add(outHistName, patORpfx2);
191 if (patORpfx3) add(outHistName, patORpfx3);
192 if (patORpfx4) add(outHistName, patORpfx4);
193 if (patORpfx5) add(outHistName, patORpfx5);
194 if (patORpfx6) add(outHistName, patORpfx6);
195 if (patORpfx7) add(outHistName, patORpfx7);
196 if (patORpfx8) add(outHistName, patORpfx8);
197 if (patORpfx9) add(outHistName, patORpfx9);
198 }
199
200 //Add all histograms whose names match the given regular expression pattern
201 //or begin with the given prefix. If the final histogram named outHistName
202 //does not exist it is created.
203
204 void add(const char* outHistName, const char* patORpfx) {
205 TRegexp reg(patORpfx, kFALSE);
206
207 TList* list = gDirectory->GetList() ;
208 TIterator* iter = list->MakeIterator();
209
210 TObject* obj = 0;
211 TObject* hist = 0;
212 Bool_t makeOutHist = false;
213
214 hist = gDirectory->Get(outHistName);
215 //If out hist does not exist, remember to create it
216 if (! hist) makeOutHist = true;
217
218 cout << "Adding to " << outHistName << endl;
219
220 while (obj = iter->Next()) {
221 if (! obj->InheritsFrom(TH1::Class())) continue;
222 TString name = obj->GetName();
223
224 //Don't add out hist
225 if (name == TString(outHistName)) continue;
226
227 if (TString(patORpfx).MaybeRegexp()) {
228 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
229 } else if (! name.BeginsWith(patORpfx)) continue;
230
231 if (makeOutHist) {
232 hist = obj->Clone(outHistName);
233
234 if (hist->InheritsFrom(TH2::Class()))
235 ((TH2*)hist)->Reset();
236 else
237 ((TH1*)hist)->Reset();
238
239 ((TH1*)hist)->SetTitle(outHistName);
240 ((TH1*)hist)->Sumw2();
241 makeOutHist = false;
242 }
243
244 cout << "Adding " << name << endl;
245 ((TH1*)hist)->Add((TH1*)obj);
246 }
247 }
248
249 //For all histograms whose names match the given regular expression pattern
250 //or begin with the given prefix, set the fill, line and marker colors to the
251 //given value.
252
253 void color(const char* patORpfx, Color_t color) {
254 TRegexp reg(patORpfx, kFALSE);
255
256 TList* list = gDirectory->GetList() ;
257 TIterator* iter = list->MakeIterator();
258
259 TObject* obj = 0;
260
261 while (obj = iter->Next()) {
262 if (! obj->InheritsFrom(TH1::Class())) continue;
263
264 TString name = obj->GetName();
265
266 if (TString(patORpfx).MaybeRegexp()) {
267 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
268 } else if (! name.BeginsWith(patORpfx)) continue;
269
270 ((TH1*)obj)->SetFillColor(color);
271 ((TH1*)obj)->SetLineColor(color);
272 ((TH1*)obj)->SetMarkerColor(color);
273 }
274 }
275
276 void invertx(const char* patORpfx) {
277 TRegexp reg(patORpfx, kFALSE);
278
279 TList* list = gDirectory->GetList() ;
280 TIterator* iter = list->MakeIterator();
281
282 TObject* obj = 0;
283 TH1* h = 0;
284 TH1* htemp = 0;
285
286 while (obj = iter->Next()) {
287 if (! obj->InheritsFrom(TH1::Class())) continue;
288
289 TString name = obj->GetName();
290
291 if (TString(patORpfx).MaybeRegexp()) {
292 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
293 } else if (! name.BeginsWith(patORpfx)) continue;
294
295 cout << "Inverting " << name.Data() << endl;
296
297 h = (TH1*)obj;
298 htemp = (TH1*)h->Clone("htemp");
299
300 unsigned int nbinsp2 = h->GetNbinsX()+2;
301 for(unsigned int i = 0; i < nbinsp2; i++) {
302 h->SetBinContent(i, htemp->GetBinContent(nbinsp2-1-i));
303 h->SetBinError(i, htemp->GetBinError(nbinsp2-1-i));
304 }
305 }
306
307 delete htemp;
308 }
309
310 //For all histograms drawn on a given TCanvas, set the fill, line and marker
311 //colors in ascending order starting from the given color.
312
313 void colors(TCanvas* canvas, Color_t color) {
314 if(! canvas) return;
315
316 TList* list = canvas->GetListOfPrimitives();
317 TIterator* iter = list->MakeIterator();
318
319 TObject* obj = 0;
320
321 //Hist color iterator
322 Int_t colorIt = color;
323
324 while (obj = iter->Next()) {
325 if (! obj->InheritsFrom(TH1::Class())) continue;
326
327 //yellow
328 if (colorIt == 5)
329 ++colorIt;
330
331 hist::color(obj->GetName(), colorIt);
332 if (colorIt == 7)
333 colorIt = 1;
334 else
335 ++colorIt;
336 }
337 }
338
339 //For all histograms added to a given THStack, set the fill, line and marker
340 //colors in ascending order starting from the given color.
341
342 void colors(THStack* stack, Color_t color) {
343 if(! stack) return;
344
345 TList* list = stack->GetHists();
346 TIterator* iter = list->MakeIterator();
347
348 TObject* obj = 0;
349
350 //Hist color iterator
351 Int_t colorIt = color;
352
353 while (obj = iter->Next()) {
354 if (! obj->InheritsFrom(TH1::Class())) continue;
355
356 hist::color(obj->GetName(), colorIt);
357 if (colorIt == 7)
358 colorIt = 1;
359 else
360 ++colorIt;
361 }
362 }
363
364 //Create a canvas and Draw("same") all histograms whose names match the given
365 //regular expression pattern or begin with the given prefix. If the TCanvas
366 //named canvasName does not exist it is created. Optionally apply colors and
367 //styles to all histograms.
368
369 void drawsame(const char* canvasName, const char* patORpfx, Bool_t addColor, Bool_t addStyle, Option_t* drawOption) {
370 TRegexp reg(patORpfx, kFALSE);
371
372 TList* list = gDirectory->GetList() ;
373 TIterator* iter = list->MakeIterator();
374
375 TObject* obj = 0;
376 TObject* canvas = 0;
377 Bool_t makeCanvas = false;
378
379 canvas = gROOT->GetListOfCanvases()->FindObject(canvasName);
380 //If canvas does not exist, remember to create it
381 if (! canvas) makeCanvas = true;
382
383 while (obj = iter->Next()) {
384 if (! obj->InheritsFrom(TH1::Class())) continue;
385
386 TString name = obj->GetName();
387
388 if (TString(patORpfx).MaybeRegexp()) {
389 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
390 } else if (! name.BeginsWith(patORpfx)) continue;
391
392 if (makeCanvas) {
393 canvas = new TCanvas(canvasName, canvasName);
394 makeCanvas = false;
395 }
396
397 ((TCanvas*)canvas)->cd();
398 if (!((TCanvas*)canvas)->GetListOfPrimitives()->GetEntries())
399 ((TH1*)obj)->Draw(Form("%s", drawOption));
400 else
401 ((TH1*)obj)->Draw(Form("SAME%s", drawOption));
402 }
403
404 if (addColor)
405 hist::colors((TCanvas*)canvas);
406 if (addStyle)
407 hist::styles((TCanvas*)canvas);
408 }
409
410 //Return a pointer to a TLegend with an entry for each histogram drawn on a
411 //given TCanvas. Display either the line, point or fill values. Optionally
412 //apply colors to all histograms. By default, entry labels are the names of
413 //their respective histograms. Optionally, if histogram names are of the
414 //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
415
416 TLegend* legend(const char* canvasName, Option_t* option, Bool_t addColor, Int_t token, Float_t xmin, Float_t ymin, Float_t xmax, Float_t ymax) {
417 if(! canvasName) return 0;
418
419 TCanvas* canvas = 0;
420 TObject* obj = 0;
421 obj = gROOT->FindObjectAny(canvasName);
422
423 if(obj && obj->InheritsFrom(TCanvas::Class()))
424 canvas = (TCanvas*)obj;
425 else
426 return 0;
427
428 TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
429 TList* list = canvas->GetListOfPrimitives();
430 TIterator* iter = list->MakeIterator();
431
432 //Hist color iterator
433 Int_t colorIt = 1;
434
435 while (obj = iter->Next()) {
436 if (! obj->InheritsFrom(TH1::Class())) continue;
437
438 if (addColor) {
439 hist::color(obj->GetName(), colorIt);
440 ++colorIt;
441 }
442
443 if (token == -1)
444 leg->AddEntry(obj, obj->GetName(), option);
445 else {
446 TString name(obj->GetName());
447 TObjArray* a = name.Tokenize("_");
448 if (a->GetEntries() <= token)
449 leg->AddEntry(obj, obj->GetName(), option);
450 else
451 leg->AddEntry(obj, a->At(token)->GetName(), option);
452 }
453 }
454
455 return leg;
456 }
457
458 //Return a pointer to a TLegend with an entry for each histogram drawn on a
459 //given TCanvas. Display either the line, point or fill values. Optionally
460 //apply colors to all histograms. By default, entry labels are the names of
461 //their respective histograms. Optionally, if histogram names are of the
462 //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
463
464 TLegend* legend(TCanvas* canvas, Option_t* option, Bool_t addColor, Int_t token, Float_t xmin, Float_t ymin, Float_t xmax, Float_t ymax) {
465 if(! canvas) return 0;
466
467 TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
468 TList* list = canvas->GetListOfPrimitives();
469 TIterator* iter = list->MakeIterator();
470
471 TObject* obj = 0;
472
473 //Hist color iterator
474 Int_t colorIt = 1;
475
476 while (obj = iter->Next()) {
477 if (! obj->InheritsFrom(TH1::Class())) continue;
478
479 if (addColor) {
480 hist::color(obj->GetName(), colorIt);
481 ++colorIt;
482 }
483
484 if (token == -1)
485 leg->AddEntry(obj, obj->GetName(), option);
486 else {
487 TString name(obj->GetName());
488 TObjArray* a = name.Tokenize("_");
489 if (a->GetEntries() <= token)
490 leg->AddEntry(obj, obj->GetName(), option);
491 else
492 leg->AddEntry(obj, a->At(token)->GetName(), option);
493 }
494 }
495
496 return leg;
497 }
498
499 //Return a pointer to a TLegend with an entry for each histogram added to a
500 //given THStack. Display either the line, point or fill values. Optionally
501 //apply colors to all histograms. By default, entry labels are the names of
502 //their respective histograms. Optionally, if histogram names are of the
503 //form XX_YY_ZZ_WW, entry labels can be XX (token=0), YY (token=1), etc.
504
505 TLegend* legend(THStack* stack, Option_t* option, Bool_t addColor, Int_t token, Float_t xmin, Float_t ymin, Float_t xmax, Float_t ymax) {
506 if(! stack) return 0;
507
508 TLegend* leg = new TLegend(xmin, ymin, xmax, ymax);
509 TList* list = stack->GetHists();
510 TIterator* iter = list->MakeIterator();
511
512 TObject* obj = 0;
513
514 //Hist color iterator
515 Int_t colorIt = 1;
516
517 while (obj = iter->Next()) {
518 if (! obj->InheritsFrom(TH1::Class())) continue;
519
520 if (addColor) {
521 hist::color(obj->GetName(), colorIt);
522 ++colorIt;
523 }
524
525 if (token == -1)
526 leg->AddEntry(obj, obj->GetName(), option);
527 else {
528 TString name(obj->GetName());
529 TObjArray* a = name.Tokenize("_");
530 if (a->GetEntries() <= token)
531 leg->AddEntry(obj, obj->GetName(), option);
532 else
533 leg->AddEntry(obj, a->At(token)->GetName(), option);
534 }
535 }
536
537 return leg;
538 }
539
540 //Normalize to one all histograms whose names match the given regular exp-
541 //ression pattern or begin with the given prefix.
542
543 void normalize(const char* patORpfx) {
544 TRegexp reg(patORpfx, kFALSE);
545
546 TList* list = gDirectory->GetList() ;
547 TIterator* iter = list->MakeIterator();
548
549 TObject* obj = 0;
550
551 while (obj = iter->Next()) {
552 if (! obj->InheritsFrom(TH1::Class())) continue;
553
554 TString name = obj->GetName();
555
556 if (TString(patORpfx).MaybeRegexp()) {
557 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
558 } else if (! name.BeginsWith(patORpfx)) continue;
559
560 Double_t integral = 0;
561
562 if (obj->InheritsFrom(TH2::Class()))
563 integral = ((TH2*)obj)->Integral();
564 else
565 integral = ((TH1*)obj)->Integral();
566
567 if (integral) {
568 ((TH1*)obj)->Sumw2();
569 ((TH1*)obj)->Scale(1./integral);
570 }
571 }
572 }
573
574 //Scale by the given value all histograms whose names match the given regular
575 //expression pattern or begin with the given prefix.
576
577 void scale(const char* patORpfx, Double_t scale) {
578 TRegexp reg(patORpfx, kFALSE);
579
580 TList* list = gDirectory->GetList() ;
581 TIterator* iter = list->MakeIterator();
582
583 TObject* obj = 0;
584
585 while (obj = iter->Next()) {
586 if (! obj->InheritsFrom(TH1::Class())) continue;
587
588 TString name = obj->GetName();
589
590 if (TString(patORpfx).MaybeRegexp()) {
591 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
592 } else if (! name.BeginsWith(patORpfx)) continue;
593
594 ((TH1*)obj)->Sumw2();
595 ((TH1*)obj)->Scale(scale);
596 }
597 }
598
599 //Scale bins to given value of x-axis units for all histograms whose names
600 //match the given regular expression pattern or begin with the given prefix.
601
602 void scalebins(const char* patORpfx, Double_t scale) {
603 TRegexp reg(patORpfx, kFALSE);
604
605 TList* list = gDirectory->GetList() ;
606 TIterator* iter = list->MakeIterator();
607
608 TObject* obj = 0;
609
610 while (obj = iter->Next()) {
611 if (! obj->InheritsFrom(TH1::Class())) continue;
612
613 TString name = obj->GetName();
614
615 if (TString(patORpfx).MaybeRegexp()) {
616 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
617 } else if (! name.BeginsWith(patORpfx)) continue;
618
619 Double_t binWidth, binContent, binError, newBinContent, newBinError;
620 for (Int_t i = 1; i <= ((TH1*)obj)->GetNbinsX(); ++i) {
621 binWidth = ((TH1*)obj)->GetBinWidth(i);
622 binContent = ((TH1*)obj)->GetBinContent(i);
623 binError = ((TH1*)obj)->GetBinError(i);
624 newBinContent = (binContent*scale)/binWidth;
625 newBinError = (binError*scale)/binWidth;
626
627 ((TH1*)obj)->SetBinContent(i, newBinContent);
628 ((TH1*)obj)->SetBinError(i, newBinContent);
629 // Rename y axis with scale
630 }
631 }
632 }
633
634 //Don't you hate it when you draw multiple histograms on the same canvas only
635 //to find that the bottom histogram's range does not encompass those of the
636 //histograms drawn on top? This method determines the maximum and minimum y
637 //range of all the histograms drawn on a given TCanvas and appropriately re-
638 //sizes the bottom histogram.
639
640 void setrangey(const char* canvasName, Double_t maxy, Double_t miny) {
641 if(! canvasName) return;
642
643 TCanvas* canvas = 0;
644 TObject* obj = 0;
645 obj = gROOT->FindObjectAny(canvasName);
646
647 if(obj && obj->InheritsFrom(TCanvas::Class()))
648 canvas = (TCanvas*)obj;
649 else
650 return;
651
652 TList* list = canvas->GetListOfPrimitives();
653 TIterator* iter = list->MakeIterator();
654
655 TObject* top = 0;
656
657 while (obj = iter->Next()) {
658 if (! obj->InheritsFrom(TH1::Class())) continue;
659
660 if (! top) top = obj;
661
662 if (((TH1*)obj)->GetMaximum() > maxy) maxy = ((TH1*)obj)->GetMaximum();
663
664 // JAKE Set to log scale if max/min > 10
665
666 if (canvas->GetLogy()) {
667 //protect against user supplied argument
668 if (miny == 0) miny = 999999;
669
670 for(Int_t bin = 1; bin <= ((TH1*)obj)->GetNbinsX(); ++bin) {
671 Double_t binContent = ((TH1*)obj)->GetBinContent(bin);
672 if (binContent != 0 && binContent < miny)
673 miny = binContent;
674 }
675 } else if (((TH1*)obj)->GetMinimum() < miny) miny = ((TH1*)obj)->GetMinimum();
676 }
677
678 ((TH1*)top)->SetMaximum(maxy*1.1);
679 ((TH1*)top)->SetMinimum(miny*0.9);
680 }
681
682 //Create a stacked histogram consisting of all histograms whose names match
683 //the given regular expression pattern or begin with the given prefix. If
684 //the THStack named stackHistName does not exist it is created. Optionally
685 //apply colors to all histograms. Set drawOption to "nostack" if you do not
686 //want to stack, to "hist" to display histograms without errors, to "histe"
687 //to display histograms with errors, etc.
688
689 //Don't you hate it when you draw multiple histograms on the same canvas only
690 //to find that the bottom histogram's range does not encompass those of the
691 //histograms drawn on top? This method determines the maximum and minimum y
692 //range of all the histograms drawn on a given TCanvas and appropriately re-
693 //sizes the bottom histogram.
694
695 void setrangey(TCanvas* canvas, Double_t maxy, Double_t miny) {
696 if(! canvas) return;
697
698 TList* list = canvas->GetListOfPrimitives();
699 TIterator* iter = list->MakeIterator();
700
701 TObject* obj = 0;
702 TObject* top = 0;
703
704 while (obj = iter->Next()) {
705 if (! obj->InheritsFrom(TH1::Class())) continue;
706
707 if (! top) top = obj;
708
709 if (((TH1*)obj)->GetMaximum() > maxy) maxy = ((TH1*)obj)->GetMaximum();
710
711 // JAKE Set to log scale if max/min > 10
712
713 if (canvas->GetLogy()) {
714 //protect against user supplied argument
715 if (miny == 0) miny = 999999;
716
717 for(Int_t bin = 1; bin <= ((TH1*)obj)->GetNbinsX(); ++bin) {
718 Double_t binContent = ((TH1*)obj)->GetBinContent(bin);
719 if (binContent != 0 && binContent < miny)
720 miny = binContent;
721 }
722 } else if (((TH1*)obj)->GetMinimum() < miny) miny = ((TH1*)obj)->GetMinimum();
723 }
724
725 ((TH1*)top)->SetMaximum(maxy*1.1);
726 ((TH1*)top)->SetMinimum(miny*0.9);
727 }
728
729 //Create a stacked histogram consisting of all histograms whose names match
730 //the given regular expression pattern or begin with the given prefix. If
731 //the THStack named stackHistName does not exist it is created. Optionally
732 //apply colors to all histograms. Set drawOption to "nostack" if you do not
733 //want to stack, to "hist" to display histograms without errors, to "histe"
734 //to display histograms with errors, etc.
735
736 void stack(const char* stackHistName, const char* patORpfx, Bool_t addColor, Option_t* drawOption) {
737 TRegexp reg(patORpfx, kFALSE);
738
739 TList* list = gDirectory->GetList() ;
740 TIterator* iter = list->MakeIterator();
741
742 TObject* obj = 0;
743 TObject* stack = 0;
744 Bool_t makeStackHist = false;
745
746 stack = gDirectory->Get(stackHistName);
747 //If stack hist does not exist, remember to create it
748 if (! stack) makeStackHist = true;
749
750 //Hist color iterator
751 Int_t colorIt = 1;
752
753 while (obj = iter->Next()) {
754 if (! obj->InheritsFrom(TH1::Class())) continue;
755
756 TString name = obj->GetName();
757
758 if (TString(patORpfx).MaybeRegexp()) {
759 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
760 } else if (! name.BeginsWith(patORpfx)) continue;
761
762 if (makeStackHist) {
763 stack = new THStack(stackHistName, stackHistName);
764 makeStackHist = false;
765 }
766
767 if (addColor) {
768 hist::color(obj->GetName(), colorIt);
769 ++colorIt;
770 }
771
772 ((THStack*)stack)->Add((TH1*)obj, drawOption);
773 }
774
775 // Currently breaks .ls
776 //gDirectory->Append(stack);
777 }
778
779 //For all histograms whose names match the given regular expression pattern
780 //or begin with the given prefix, set the marker style.
781
782 void style(const char* patORpfx, Style_t style) {
783 TRegexp reg(patORpfx, kFALSE);
784
785 TList* list = gDirectory->GetList() ;
786 TIterator* iter = list->MakeIterator();
787
788 TObject* obj = 0;
789
790 while (obj = iter->Next()) {
791 if (! obj->InheritsFrom(TH1::Class())) continue;
792
793 TString name = obj->GetName();
794
795 if (TString(patORpfx).MaybeRegexp()) {
796 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
797 } else if (! name.BeginsWith(patORpfx)) continue;
798
799 ((TH1*)obj)->SetMarkerStyle(style);
800 }
801 }
802
803 //For all histograms drawn on a given TCanvas, set the marker styles in
804 //ascending order starting from the given style.
805
806 void styles(TCanvas* canvas, Style_t style) {
807 if(! canvas) return;
808
809 TList* list = canvas->GetListOfPrimitives();
810 TIterator* iter = list->MakeIterator();
811
812 TObject* obj = 0;
813
814 //Hist style iterator
815 Int_t styleIt = style;
816
817 while (obj = iter->Next()) {
818 if (! obj->InheritsFrom(TH1::Class())) continue;
819
820 ((TH1*)obj)->SetMarkerStyle(styleIt);
821 ++styleIt;
822 }
823 }
824
825 //For all histograms drawn on a given THStack, set the marker styles in
826 //ascending order starting from the given style.
827
828 void styles(THStack* stack, Style_t style) {
829 if(! stack) return;
830
831 TList* list = stack->GetHists();
832 TIterator* iter = list->MakeIterator();
833
834 TObject* obj = 0;
835
836 //Hist style iterator
837 Int_t styleIt = style;
838
839 while (obj = iter->Next()) {
840 if (! obj->InheritsFrom(TH1::Class())) continue;
841
842 ((TH1*)obj)->SetMarkerStyle(styleIt);
843 ++styleIt;
844 }
845 }
846
847 //Set the x-axis title of all histograms whose names match the given regular
848 //expression pattern or begin with the given prefix.
849
850 void xaxis(const char* patORpfx, const char* title) {
851 TRegexp reg(patORpfx, kFALSE);
852
853 TList* list = gDirectory->GetList() ;
854 TIterator* iter = list->MakeIterator();
855
856 TObject* obj = 0;
857
858 while (obj = iter->Next()) {
859 if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
860
861 TString name = obj->GetName();
862
863 if (TString(patORpfx).MaybeRegexp()) {
864 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
865 } else if (! name.BeginsWith(patORpfx)) continue;
866
867 if (obj->InheritsFrom(TH1::Class()))
868 ((TH1*)obj)->GetXaxis()->SetTitle(title);
869 if (obj->InheritsFrom(THStack::Class())) {
870 ((THStack*)obj)->Draw();
871 ((THStack*)obj)->GetXaxis()->SetTitle(title);
872 }
873 }
874 }
875
876 //Set the y-axis title of all histograms whose names match the given regular
877 //expression pattern or begin with the given prefix.
878
879 void yaxis(const char* patORpfx, const char* title) {
880 TRegexp reg(patORpfx, kFALSE);
881
882 TList* list = gDirectory->GetList() ;
883 TIterator* iter = list->MakeIterator();
884
885 TObject* obj = 0;
886
887 while (obj = iter->Next()) {
888 if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue;
889
890 TString name = obj->GetName();
891
892 if (TString(patORpfx).MaybeRegexp()) {
893 if (TString(obj->GetName()).Index(reg) < 0 ) continue;
894 } else if (! name.BeginsWith(patORpfx)) continue;
895
896 if (obj->InheritsFrom(TH1::Class()))
897 ((TH1*)obj)->GetYaxis()->SetTitle(title);
898 if (obj->InheritsFrom(THStack::Class())) {
899 ((THStack*)obj)->Draw();
900 ((THStack*)obj)->GetYaxis()->SetTitle(title);
901 }
902 }
903 }
904 }
905
906 // Input: 2 histogram
907 // Output: one histogram which is the efficiency:
908 // h1 : TOTAL NUMBER OF EVENTS
909 // h2 : NUMBER OF EVENTS THAT PASS
910
911 #include "TH1.h"
912
913 // Method by pointer
914 TH1F* eff(TH1F* h1, TH1F* h2, const char* name){
915
916 // first, verify that all histograms have same binning
917 // nx is the number of visible bins
918 // nxtot = nx+2 includes underflow and overflow
919 Int_t nx = h1->GetNbinsX();
920 if (h2->GetNbinsX() != nx) {
921 cout << "Histograms must have same number of bins" << endl;
922 return 0;
923 }
924
925 // get the new histogram
926 TH1F* temp = (TH1F*) h1->Clone(name);
927 temp->SetTitle(name);
928 temp->Reset();
929 temp->Sumw2();
930
931 // Do the calculation
932 temp->Divide(h2,h1,1.,1.,"B");
933
934 // Done
935 return temp;
936 }
937
938
939 // Method by name
940 TH1F* eff(const char* name1, const char* name2, const char* name){
941
942 // Get a list of object and their iterator
943 TList* list = gDirectory->GetList() ;
944 TIterator* iter = list->MakeIterator();
945
946 // Loop over objects, set the pointers
947 TObject* obj;
948 TH1F* h1=0;
949 TH1F* h2=0;
950 TString str1 = Form("%s",name1);
951 TString str2 = Form("%s",name2);
952 while(obj=iter->Next()) {
953 TString objName = obj->GetName();
954 if (objName == str1) h1 = (TH1F*) obj;
955 if (objName == str2) h2 = (TH1F*) obj;
956 }
957
958 // quit if not found
959 if (h1 == 0) {
960 cout << "Histogram " << name1 << " not found" << endl;
961 return 0;
962 }
963 if (h2 == 0) {
964 cout << "Histogram " << name2 << " not found" << endl;
965 return 0;
966 }
967
968 // Call the method by pointer
969 TH1F* temp = eff(h1, h2, name);
970 return temp;
971 }
972 // Input: 4 histogram
973 // Output: one histogram which is the BG subtracted efficiency:
974 // h1 : TOTAL NUMBER OF EVENTS, SIGNAL REGION
975 // h2 : NUMBER OF EVENTS THAT PASS, SIGNAL REGION
976 // h3 : TOTAL NUMBER OF EVENTS, SIDE BAND
977 // h4 : NUMBER OF EVENTS THAT PASS, SIDE BAND
978
979 #include "TH1.h"
980
981
982 TH1F* eff_bg(TH1F* h1, TH1F* h2, TH1F* h3, TH1F* h4, const char* name){
983
984 // first, verify that all histograms have same binning
985 // nx is the number of visible bins
986 // nxtot = nx+2 includes underflow and overflow
987 Int_t nx = h1->GetNbinsX();
988 Int_t nxtot = nx + 2;
989 if (h2->GetNbinsX() != nx) {
990 cout << "Histograms must have same number of bins" << endl;
991 return 0;
992 }
993 if (h3->GetNbinsX() != nx) {
994 cout << "Histograms must have same number of bins" << endl;
995 return 0;
996 }
997 if (h3->GetNbinsX() != nx) {
998 cout << "Histograms must have same number of bins" << endl;
999 return 0;
1000 }
1001
1002 // get the new histogram
1003 TH1F* temp = (TH1F*) h1->Clone(name);
1004 temp->SetTitle(name);
1005 temp->Reset();
1006 temp->Sumw2();
1007
1008 // Loop over bins, calculate efficiency and error, put it in histogram
1009 for (Int_t i=0; i<nxtot; i++) {
1010 Double_t x1 = h1->GetBinContent(i);
1011 Double_t x2 = h2->GetBinContent(i);
1012 Double_t x3 = h3->GetBinContent(i);
1013 Double_t x4 = h4->GetBinContent(i);
1014 Double_t denom = x1 - x3;
1015 Double_t eff;
1016 if (denom == 0.) {
1017 eff = 0;
1018 } else {
1019 eff = (x2-x4)/denom;
1020 }
1021 Double_t failSig = x1 - x2;
1022 Double_t failBg = x3 - x4;
1023 Double_t blah = (1-eff)*(1-eff)*(x2+x4) + eff*eff*(failSig+failBg);
1024 if (blah <= 0.) blah=0.0;
1025 Double_t err;
1026 if (denom == 0) {
1027 err = 0.;
1028 } else {
1029 err = sqrt(blah)/denom;
1030 }
1031 temp->SetBinContent(i,eff);
1032 temp->SetBinError(i,err);
1033 }
1034
1035 // Done
1036 return temp;
1037 }
1038
1039 #include <TList.h>
1040 #include <TIterator.h>
1041
1042 void deleteHistos() {
1043 // Delete all existing histograms in memory
1044 TObject* obj;
1045 TList* list = gDirectory->GetList() ;
1046 TIterator* iter = list->MakeIterator();
1047 while (obj=iter->Next()) {
1048 if (obj->IsA()->InheritsFrom(TH1::Class()) ||
1049 obj->IsA()->InheritsFrom(TH2::Class()) ) {delete obj;}
1050 }
1051 }
1052
1053 void saveHist(const char* filename, const char* pat)
1054 {
1055 TList* list = gDirectory->GetList() ;
1056 TIterator* iter = list->MakeIterator();
1057
1058 TRegexp re(pat,kTRUE) ;
1059 TObject *obj;
1060
1061 TFile outf(filename,"RECREATE") ;
1062 while(obj=iter->Next()) {
1063 if (TString(obj->GetName()).Index(re)>=0) {
1064 obj->Write() ;
1065 cout << "." ;
1066 cout.flush() ;
1067 }
1068 }
1069 cout << endl ;
1070 outf.Close() ;
1071
1072 delete iter ;
1073 }
1074
1075
1076 void loadHist(const char* filename, const char* directory, const char* pfx, const char* pat, Bool_t doAdd)
1077 {
1078 TFile inf(filename) ;
1079 //inf.ReadAll() ;
1080 TDirectory* dir;
1081 if (directory)
1082 dir = (TDirectory*)inf.Get(directory);
1083 else
1084 dir = &inf;
1085 TList* list = dir->GetListOfKeys() ;
1086 TIterator* iter = list->MakeIterator();
1087
1088 TRegexp re(pat,kTRUE) ;
1089 cout << "pat = " << pat << endl ;
1090
1091 gDirectory->cd("Rint:") ;
1092
1093 TObject* obj ;
1094 TKey* key ;
1095 cout << "doAdd = " << (doAdd?"T":"F") << endl ;
1096 cout << "loadHist: reading." ;
1097 while(key=(TKey*)iter->Next()) {
1098
1099 Int_t ridx = TString(key->GetName()).Index(re) ;
1100 if (ridx==-1) {
1101 continue ;
1102 }
1103
1104 obj = dir->Get(key->GetName()) ;
1105 TObject* clone ;
1106 if (pfx) {
1107
1108 // Find existing TH1-derived objects
1109 TObject* oldObj = 0 ;
1110 if (doAdd){
1111 oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ;
1112 if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
1113 oldObj = 0 ;
1114 }
1115 }
1116 if (oldObj) {
1117 clone = oldObj ;
1118 ((TH1*)clone)->Add((TH1*)obj) ;
1119 } else {
1120 clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ;
1121 }
1122
1123
1124 } else {
1125
1126 // Find existing TH1-derived objects
1127 TObject* oldObj = 0 ;
1128 if (doAdd){
1129 oldObj = gDirectory->Get(key->GetName()) ;
1130 if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
1131 oldObj = 0 ;
1132 }
1133 }
1134
1135 if (oldObj) {
1136 clone = oldObj ;
1137 ((TH1*)clone)->Add((TH1*)obj) ;
1138 } else {
1139 clone = obj->Clone() ;
1140 }
1141 }
1142 if (!gDirectory->GetList()->FindObject(clone)) {
1143 gDirectory->Append(clone) ;
1144 }
1145 cout << "." ;
1146 cout.flush() ;
1147 }
1148 cout << endl;
1149 inf.Close() ;
1150 delete iter ;
1151 }