ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/rf/histtools.C
Revision: 1.1
Committed: Wed Dec 23 17:33:25 2009 UTC (15 years, 4 months ago) by benhoob
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Error occurred while calculating annotation data.
Log Message:
*** empty log message ***

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