-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRootPlotter.cxx
2091 lines (1666 loc) · 57.3 KB
/
RootPlotter.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "RootPlotter.h"
#include <iostream>
#include <iomanip>
#include <TROOT.h>
#include <TSystem.h>
#include <TString.h>
#include <TStyle.h>
#include <TObjString.h>
#include <TObjArray.h>
#include <TClass.h>
#include <TKey.h>
#include <TCanvas.h>
#include <TPad.h>
#include <TLegend.h>
#include <TLegendEntry.h>
#include <TText.h>
#include <TPaveText.h>
#include <TMath.h>
#include <TProfile.h>
#include <TF1.h>
#include <THStack.h>
#include <TFile.h>
#include "tdrstyle.C"
using namespace std;
/********************************************************
*
* RootPlotter
*
* A plotting tool using ROOT's functionality.
*
*******************************************************/
RootPlotter::RootPlotter()
{
// some default settings
fRootfiles = NULL;
fNumOfSamples = 0;
bShapeNorm = true;
fSampleNames = NULL;
bPortrait = true;
bDrawEntries = false;
bFitPtBalanceHists = false;
bJetShapesPerSlice = false;
bSubstractBkgd = false;
bPlotRatio = false;
bDrawLumi = false;
}
RootPlotter::~RootPlotter()
{
// destructor: clean up
if (fRootfiles){
delete[] fRootfiles;
}
}
void RootPlotter::OpenRootFiles(TObjArray* filenames, const char* cyclename)
{
// open the root files with names given in the TObjArray
cout << endl << "----------------------------------- RootPlotter -----------------------------------" << endl;
fRootfiles = new TFile*[fNumOfSamples];
fNumOfSamples = filenames->GetEntries();
for (Int_t i=0; i<fNumOfSamples; ++i){
TString name(((TObjString*)filenames->At(i))->GetName());
if (cyclename){
TString Prefix(cyclename);
Prefix.Append(".");
name.Prepend(Prefix);
}
// check if name consists of a wildcard, if so use hadd to combine histograms
if (name.Contains("*")){
TString target(name);
target.ReplaceAll("*","");
TString command = "hadd -f " + target + " " + name;
gSystem->Exec(command);
name = target;
}
//cout << "Opening file with name " << name << "..." << endl;
fRootfiles[i] = new TFile(name, "READ");
//cout << "... success! pointer = " << fRootfiles[i] << endl;
//cout << "name = " << fRootfiles[i]->GetName() << endl;
//fRootfiles[i]->ls();
//cout << " is open? " << fRootfiles[i]->IsOpen() << endl;
if (!fRootfiles[i]->IsOpen()) {
cout << endl << "RootPlotter: File " << name << " does not exist!!!" << endl;
exit(EXIT_FAILURE);
} else { // success!
cout << "RootPlotter: Successfully opened file " << name << endl;
}
}
cout << "-----------------------------------------------------------------------------------" << endl << endl;
return;
}
void RootPlotter::SetSampleNames(TObjArray* SampleNames)
{
// set the names of the chains which should be plotted
fSampleNames = SampleNames;
fNumOfSamples = fSampleNames->GetEntries();
}
void RootPlotter::SetSamplesToStack(TObjArray* names)
{
// set the names of the chains which should be stacked
// and plotted in one histogram
fSamplesToStack = names;
fNumOfSamplesToStack = fSamplesToStack->GetEntries();
}
void RootPlotter::SetSamplesWeight(TArrayF weights)
{
// set the names of the chains which should be summed
// and plotted in one histogram
fSamplesWeight = weights;
}
void RootPlotter::SetHistColors(TArrayI colors)
{
// set the histogram colors
fSampleColors = colors;
}
void RootPlotter::SetHistMarkers(TArrayI markers)
{
// set the histogram markers
fSampleMarkers = markers;
}
TPostScript* RootPlotter::MakeNewPsFile(const char* psfilename)
{
// create a new ps file with name psfilename
TPostScript *PSFile = NULL;
if (bPortrait){
PSFile = new TPostScript(psfilename, 111); // ps output
PSFile->Range(20.0, 30.0);
} else {
PSFile = new TPostScript(psfilename, 112); // ps output
PSFile->Range(27.0, 18.0);
}
return PSFile;
}
Bool_t RootPlotter::ShouldBeStacked(const char* name)
{
// check if the sample with name 'name' is in the list of
// samples to stack
TString inname(name);
for (Int_t i=0; i<fNumOfSamplesToStack; ++i){
TString sname(fSamplesToStack->At(i)->GetName());
// easy case:
if (inname == sname) return true;
// difficult case: name contains directory structure
TObjArray* arr = inname.Tokenize("/");
TString last;
for (Int_t it=0; it<arr->GetEntries(); ++it){
TString tok(arr->At(it)->GetName());
if (tok.Contains(".root")) last = tok;
}
if (last.Contains(sname)) return true;
}
return false;
}
void RootPlotter::PlotHistos(const char* psfilename)
{
// loops through all directories found in the 'data' directory
// and writes the contents (histograms, hopefully) to a ps file
// looks for every histogram for the corresponding MC reconstructed
// histogram and plots it onto the same canvas
static Int_t iPageNum = 0;
Int_t CanWidth;
Int_t CanHeight;
if (bPortrait){
CanWidth = 600;
CanHeight = 830;
} else {
CanWidth = 800;
CanHeight = 600;
}
// find all subdirectories (former histogram collections) in the first file
TDirectory* firstdir = (TDirectory*) fRootfiles[0];
TObjArray* dirs = FindSubdirs(firstdir);
// general appearance and style
gROOT->SetStyle("Plain");
gStyle->SetOptStat(0);
gStyle -> SetPadTickX(1);
gStyle -> SetPadTickY(1);
gStyle->SetPadBorderMode(0);
gStyle->SetPadColor(kWhite);
gStyle->SetPadGridX(false);
gStyle->SetPadGridY(false);
gStyle->SetGridColor(0);
gStyle->SetGridStyle(3);
gStyle->SetGridWidth(1);
gStyle->SetFrameBorderMode(0);
gStyle->SetFrameBorderSize(1);
gStyle->SetFrameFillColor(0);
gStyle->SetFrameFillStyle(0);
gStyle->SetFrameLineColor(1);
gStyle->SetFrameLineStyle(1);
gStyle->SetFrameLineWidth(1);
gStyle->SetTitleFont(42, "XYZ");
gStyle->SetLabelFont(42, "XYZ");
gStyle->SetAxisColor(1, "XYZ");
gStyle->SetStripDecimals(kTRUE);
gStyle->SetTickLength(0.03, "XYZ");
gStyle->SetNdivisions(510, "XYZ");
gStyle->UseCurrentStyle();
// set up the canvas
TCanvas *can = new TCanvas("canvas","Control Plots", CanWidth, CanHeight);
Float_t yplot = 0.3;
Float_t yratio = 0.15;
// coordinates:
//
// set up the coordinates of the two pads: // y6 +-------------+
Float_t y1, y2, y3, y4, y5, y6; // | |
y6 = 0.97; // | pad1 |
y5 = y6-yplot; // y5 |-------------|
y4 = y5-yratio; // | rp1 |
y3 = 0.49; // y4 +-------------+
y2 = y3-yplot; //
y1 = y2-yratio; // y3 +-------------+
Float_t x1, x2; // | |
x1 = 0.01; // | pad2 |
x2 = 0.99; // y2 |-------------|
// | rp2 |
// y1 +-------------+
// x1 x2
TPad *pad1=0; TPad *rp1=0; TPad *pad2=0; TPad *rp2=0;
if (bPlotRatio){
pad1 = new TPad("pad1", "Control Plots 1", x1, y5, x2, y6);
rp1 = new TPad("rp1", "Ratio1", x1, y4, x2, y5);
pad2 = new TPad("pad2", "Control Plots 2", x1, y2, x2, y3);
rp2 = new TPad("rp2", "Ratio2", x1, y1, x2, y2);
} else {
pad1 = new TPad("pad1", "Control Plots 1", x1, y4, x2, y6);
pad2 = new TPad("pad2", "Control Plots 2", x1, y1, x2, y3);
}
// set margins for portrait mode
if (bPortrait){
pad1->SetTopMargin(0.07); pad1->SetBottomMargin(0.13); pad1->SetLeftMargin(0.19); pad1->SetRightMargin(0.05);
pad2->SetTopMargin(0.07); pad2->SetBottomMargin(0.13); pad2->SetLeftMargin(0.19); pad2->SetRightMargin(0.05);
if (bPlotRatio){
pad1->SetTopMargin(0.07); pad1->SetBottomMargin(0.0); pad1->SetLeftMargin(0.19); pad1->SetRightMargin(0.05);
pad2->SetTopMargin(0.07); pad2->SetBottomMargin(0.0); pad2->SetLeftMargin(0.19); pad2->SetRightMargin(0.05);
rp1->SetTopMargin(0.0); rp1->SetBottomMargin(0.33); rp1->SetLeftMargin(0.19); rp1->SetRightMargin(0.05);
rp2->SetTopMargin(0.0); rp2->SetBottomMargin(0.33); rp2->SetLeftMargin(0.19); rp2->SetRightMargin(0.05);
}
// margins for landscape
} else {
pad1->SetTopMargin(0.02); pad1->SetBottomMargin(0.0); pad1->SetLeftMargin(0.13); pad1->SetRightMargin(0.05);
pad2->SetTopMargin(0.02); pad2->SetBottomMargin(0.0); pad2->SetLeftMargin(0.13); pad2->SetRightMargin(0.05);
if (bPlotRatio){
rp1->SetTopMargin(0.0); rp1->SetBottomMargin(0.35); rp1->SetLeftMargin(0.13); rp1->SetRightMargin(0.05);
rp2->SetTopMargin(0.0); rp2->SetBottomMargin(0.35); rp2->SetLeftMargin(0.13); rp2->SetRightMargin(0.05);
}
}
pad1->Draw();
pad2->Draw();
if (bPlotRatio){
rp1->Draw();
rp2->Draw();
}
Int_t ihist = 0; // histogram index
TObjArray* OneDHistArray = new TObjArray();
TH1* OneDHist = NULL;
THStack* StackHist = NULL;
TObjArray* TwoDHistArray = new TObjArray();
TH2D* TwoDHist = NULL;
TH1* BkgdHist = NULL; // Background Histogram
// array which histograms are already in the stacked one, so that they are not plotted twice
Bool_t IsInStack[fNumOfSamples];
for (Int_t i=0; i<fNumOfSamples; ++i) IsInStack[i] = false;
// collector for cleaning up
TObjArray* Collector = new TObjArray();
// the postscript file
TPostScript *PSFile = NULL;
// loop over all directories and plot the histograms
for (Int_t i=0; i<dirs->GetEntries(); ++i){
Int_t NumHistosPerObject = 0; // how many histograms per object should be plotted?
// e.g. for a 2d-histo plot slices of it
TString dirname(((TObjString*) dirs->At(i))->GetString());
TString text(dirname.Data());
text.Prepend("Plotting all histograms in directory ");
cout << "\n+---------------------- histograms with ratios -----------------------+" << endl;
cout << "| " << setw(60)<< text << " |" << endl;
cout << "+---------------------------------------------------------------------+" << endl;
ihist = 0;
// create a new ps file
if (PSFile){
PSFile->Close();
delete PSFile;
PSFile = NULL;
}
iPageNum = 0;
TString filename(psfilename);
filename.ReplaceAll(".ps","");
filename.Append("_");
filename.Append(dirname);
filename.Append(".ps");
PSFile = MakeNewPsFile(filename);
// make a page title
can->cd();
//TPaveText* pagetitle = new TPaveText(0.2,0.97,0.8,1.0, "NDC");
//pagetitle->SetBorderSize(0);
//pagetitle->SetFillColor(0);
//pagetitle->AddText(((TObjString*) dirs->At(i))->GetString());
//pagetitle->SetTextFont(42);
//pagetitle->SetTextColor(kBlack);
//pagetitle->SetTextSize(0.05);
//pagetitle->Draw();
firstdir->cd();
gDirectory->Cd(((TObjString*) dirs->At(i))->GetString());
Float_t TotMax = 1;
// loop over all histograms in the directory and plot them
TKey *key;
TIter nextkey( gDirectory->GetListOfKeys() );
while ( (key = (TKey*)nextkey())) {
TotMax = 1;
TObject *obj = key->ReadObj();
Int_t Nhists = fNumOfSamples;
Double_t TotMax = 0.;
if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
// first histogram found
OneDHist = (TH1*) obj;
TString histname(OneDHist->GetName());
TString oldchainname(((TObjString*) fSampleNames->At(0))->GetName());
if (OneDHist->GetMaximum()>TotMax){
TotMax = OneDHist->GetMaximum();
}
// cosmetics
Cosmetics(OneDHist, 0);
ApplyWeight(OneDHist, 0);
if (OneDHist->GetMaximum()>TotMax) TotMax = OneDHist->GetMaximum();
// if there is a lumi yield plot, plot only the first chain
if (dirname.Contains("Yield")){
TString NewName = TString::Format("%s;1", OneDHist->GetName());
OneDHist = (TH1*) gDirectory->Get(NewName);
PSFile->NewPage();
PlotYields(OneDHist);
OneDHistArray->Clear();
break;
}
// build and substract background if requested
if (bSubstractBkgd){
BkgdHist = BuildBackground(dirname, histname);
Collector->Add(BkgdHist);
OneDHist->Add(BkgdHist, -1);
}
if (ShouldBeStacked(firstdir->GetName())){
if (StackHist==NULL){
StackHist = new THStack(histname, "stack");
}
Nhists--;
StackHist->Add(OneDHist);
} else {
OneDHistArray->Add(OneDHist);
}
if (obj->IsA()->InheritsFrom( TH2D::Class() ) ) {
TwoDHist = (TH2D*) OneDHist;
TwoDHist->SetTitle(firstdir->GetName());
TwoDHistArray->Add(TwoDHist);
}
// find the same histogram for the other samples
for (Int_t ichain=1; ichain<fNumOfSamples; ++ichain){
TFile* thisdir = fRootfiles[ichain];
thisdir->cd();
gDirectory->Cd(((TObjString*) dirs->At(i))->GetString());
// get the right histogram
OneDHist = NULL;
TObject* obj = gDirectory->Get(histname);
if (!obj){
cerr << "Could not find histogram with name " << histname << " in file " << thisdir->GetName()
<< " and directory " << ((TObjString*) dirs->At(i))->GetString() << " - please check." << endl;
exit(EXIT_FAILURE);
}
if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
OneDHist = (TH1*) obj;
Cosmetics(OneDHist, ichain);
ApplyWeight(OneDHist, ichain);
if (OneDHist->GetMaximum()>TotMax) TotMax = OneDHist->GetMaximum();
} else {
cerr << "Found an object with name " << histname << " in file " << thisdir->GetName()
<< " and directory " << ((TObjString*) dirs->At(i))->GetString() << "." << endl;
cerr << "But it's not a TH1! Please correct the error." << endl;
exit(EXIT_FAILURE);
}
// if the requested sample name is in the list of samples to stack, add it to the stack
// chains to create on histogram
TString chainname( thisdir->GetName() );
if (ShouldBeStacked(chainname)){
if (StackHist==NULL){
StackHist = new THStack(histname, "stack");
}
Nhists--;
StackHist->Add(OneDHist);
} else {
OneDHistArray->Add(OneDHist);
}
if (obj->IsA()->InheritsFrom( "TH2D" ) ) {
TwoDHist = (TH2D*) OneDHist;
TwoDHist->SetTitle(chainname);
TwoDHistArray->Add(TwoDHist);
}
}
NumHistosPerObject = 1;
// check if the 2d-histograms need 'special care'
if (obj->IsA()->InheritsFrom( "TH2D" ) ) {
if (histname.Contains("PtFraction")) {
OneDHistArray->Clear();
for (Int_t j=0; j<TwoDHistArray->GetEntries(); ++j){
TH2D* hist = (TH2D*) TwoDHistArray->At(j);
TH1* fractionhist = MakePtFractionHisto(hist);
OneDHistArray->Add(fractionhist);
}
NumHistosPerObject = 1;
// } else if ( histname.Contains("JetShape") ){
// NumHistosPerObject = OneDHist->GetNbinsX()+1;
// IsJetShape = true;
} else {
NumHistosPerObject = OneDHist->GetNbinsX();
}
}
} else { // do nothing for an unknown object
continue;
}
// do nothing for empty histograms
/*
cout << "checkin entries..." << endl;
if (OneDHist->GetEntries() == 0) {
cout << "Entries are 0! Histname = " << OneDHist->GetName() << endl;
TwoDHistArray->Clear();
OneDHistArray->Clear();
continue;
}
*/
// skip the ptbalance calibration histograms -> plot them separately
TString histname(OneDHist->GetName());
if ( histname.Contains("PtBalanceFit") ){
TwoDHistArray->Clear();
OneDHistArray->Clear();
continue;
}
// skip denominators of efficiency histograms
if ( histname.Contains("EffBot") ){
TwoDHistArray->Clear();
OneDHistArray->Clear();
continue;
}
// loop over the histograms that should be plotted per canvas
for (Int_t jobjhist=0; jobjhist<NumHistosPerObject; ++jobjhist){
// if( IsJetShape && !bJetShapesPerSlice) jobjhist = NumHistosPerObject-1; // exception for the jetshapes
// get 1-dimensional histograms out of the 2d ones
if (NumHistosPerObject>1){
// reset the 1-d hists, to be filled with slices
OneDHistArray->Clear();
if (StackHist){
delete StackHist;
StackHist=NULL;
}
Nhists=fNumOfSamples;
// create the slices
for (Int_t j=0; j<TwoDHistArray->GetEntries(); ++j){
TH2D* hist = (TH2D*) TwoDHistArray->At(j);
TString hname = hist->GetName();
OneDHist = GetSliceHisto(hist, jobjhist+1);
if (OneDHist->GetMaximum()>TotMax) TotMax = OneDHist->GetMaximum();
TString htitle = hist->GetTitle();
if (ShouldBeStacked(htitle)){
if (StackHist==NULL){
StackHist = new THStack(histname, "stack");
}
Nhists--;
StackHist->Add(OneDHist);
} else {
//Collector->Add(OneDHist);
OneDHistArray->Add(OneDHist);
}
if (Nhists != TwoDHistArray->GetEntries()){
cout << "Could not convert 2d histogram into 1d one. HistName = " << ((TH2D*) TwoDHistArray->At(0))->GetName() << " Aborting." << endl;
exit(EXIT_FAILURE);
}
}
}
if ((Nhists+fNumOfSamplesToStack) != fNumOfSamples){
cout << "For histogram " << ((TH2D*) TwoDHistArray->At(0))->GetName()
<< ", found " << OneDHistArray->GetEntries() << " different samples, requested were " << fNumOfSamples << endl;
cout << "Skipping histogram." << endl;
continue;
}
if (StackHist){
//invert the ordering of all histograms in the stack
THStack *StackHist2 = new THStack("stack2","stack2");
TList* histlist = StackHist->GetHists();
for(int i=fNumOfSamplesToStack-1; i>=0; --i){
StackHist2->Add( (TH1*)histlist->At(i));
}
StackHist = NULL;
StackHist = StackHist2;
if (StackHist->GetMaximum()>TotMax) TotMax = StackHist->GetMaximum();
}
TH1* FirstHist = (TH1*) OneDHistArray->At(0);
TString histname(FirstHist->GetName());
cout << "ihist = " << ihist << " histname = " << histname << endl;
// set up the canvas
if (ihist%2==0){
if (ihist != 0){
++iPageNum;
DrawPageNum(can, iPageNum);
}
can->Update();
PSFile->NewPage();
// clean up
if (ihist!=0){
can->Clear("D");
for (Int_t i=0;i<Collector->GetEntries();++i){
TObject* obj = Collector->At(i);
delete obj;
}
Collector->Clear();
}
}
switch (ihist%2){
case 0: pad1->cd(); break;
case 1: pad2->cd(); break;
default: break;
}
gPad->SetLogx(0);
gPad->SetLogy(0);
/*------------------------------------------------------------------\
| DRAW THE HISTOGRAMS AS WELL AS THE RATIOS |
\------------------------------------------------------------------*/
if (StackHist){
TList* list = StackHist->GetHists();
TH1* hist = (TH1*) list->At(list->GetEntries()-1);
if (hist->GetMaximum()>TotMax){
TotMax = hist->GetMaximum();
}
}
//////////////////////////////////////
// draw the histograms //
//////////////////////////////////////
Int_t NOneDHists = OneDHistArray->GetEntries();
if (bShapeNorm){
ShapeNormalize(FirstHist);
for (Int_t jhist=1; jhist<NOneDHists; ++jhist){
ShapeNormalize((TH1*) OneDHistArray->At(jhist));
}
// shape normalisation of the stack (overall factor for the sum)
if (StackHist){
ShapeNormalize(StackHist);
}
}
// if the first hist has no entries, then take the second one
if (FirstHist->GetEntries()==0){
FirstHist = (TH1*) OneDHistArray->At(1);
}
// store the subtitle if it was set in GetSliceHistos
TString oldtitle = FirstHist->GetTitle();
TString subtitle;
Bool_t PlotSubtitle = false;
if (oldtitle.Contains("SUBTITLE") ){
subtitle = oldtitle;
subtitle.ReplaceAll("SUBTITLE:","");
PlotSubtitle = true;
}
// set the title, logscales and maxima
Ssiz_t pos2 = oldtitle.First("(");
TString title(oldtitle(0,pos2));
FirstHist->SetTitle(title);
histname.ReplaceAll("(",", "); histname.ReplaceAll("_Data",""); histname.ReplaceAll(")","");
FirstHist->SetTitle("");
//FirstHist->SetTitle(histname);
Float_t MaxScale = 1.2;
Float_t LogScale = 20.0;
Bool_t IsLogPlot = false;
Bool_t IsEffHist = false;
TString CheckEff(FirstHist->GetYaxis()->GetTitle());
if (CheckEff.Contains("#epsilon")) IsEffHist=true;
if (bDrawEntries){
MaxScale += fNumOfSamples / 10.;
LogScale *= fNumOfSamples;
}
if (histname.EndsWith("_lxy")){
IsLogPlot = true;
gPad->SetLogx(1);
gPad->SetLogy(1);
if (TotMax>0){
FirstHist->SetMaximum(LogScale*TotMax);
}
if (!bShapeNorm){
//FirstHist->SetMinimum( 0.3 );
}
} else if (histname.EndsWith("_ly")) {
IsLogPlot = true;
gPad->SetLogy(1);
if (TotMax>0){
FirstHist->SetMaximum(LogScale*TotMax);
}
if (!bShapeNorm){
//FirstHist->SetMinimum( 0.3 );
}
// no log-y
} else {
if (histname.EndsWith("_lx")){
gPad->SetLogx(1);
}
FirstHist->SetMinimum(0.001);
if (TotMax>0){
if (FirstHist->GetMaximum() != 0){
FirstHist->SetMaximum(MaxScale*TotMax);
} else {
FirstHist->SetMaximum(TotMax);
}
}
}
// set range for resolution plots
if (FirstHist->InheritsFrom(TProfile::Class())){
FirstHist->SetMinimum(-0.2);
FirstHist->SetMaximum(0.2);
}
// efficiency with efficiency close to 1: redefine minimum
if (IsEffHist){
if (FirstHist->GetMaximum() > 1.1){
FirstHist->SetMinimum(0.76);
FirstHist->SetMaximum(1.23);
}
}
// draw the histograms
if (FirstHist->GetMarkerStyle() < 2){
FirstHist->Draw("HIST");
} else {
FirstHist->Draw("P");
}
// draw background if requested
/*
if (bSubstractBkgd && BkgdHist){
BkgdHist->SetLineColor(kGray);
BkgdHist->SetFillColor(kGray);
BkgdHist->SetFillStyle(3002);
BkgdHist->Draw("Histsame");
}
*/
// now draw the stack if it exists
if (StackHist){
StackHist->Draw("hist,same");
}
for (Int_t mhist=1; mhist<OneDHistArray->GetEntries(); ++mhist){
TH1* hist = (TH1*) OneDHistArray->At(mhist);
// check entries in case of log plot
if (IsLogPlot){
if (hist->GetEntries()<1) continue;
}
if (hist->GetMarkerStyle() == 0){
hist->Draw("HISTsame");
} else {
hist->Draw("P same");
}
}
if (FirstHist->GetMarkerStyle() < 2){
FirstHist->Draw("HISTsame");
} else {
FirstHist->Draw("P same");
}
/*
TPaveText *p = (TPaveText*) (gPad->GetListOfPrimitives()->FindObject("title"));
if (p){
p->SetLineColor(kWhite);
p->Draw("same");
}
*/
gPad->RedrawAxis();
//////////////////////
// draw a legend //
//////////////////////
Float_t yfrac = 0.06;
if (!bPlotRatio) yfrac = 0.05;
Float_t top = 0.92;
Float_t ysize = yfrac*fNumOfSamples;
Float_t xleft = 0.65;
Float_t xright = 0.92;
if (!bPortrait){
top = 0.99;
ysize = 0.07*fNumOfSamples;
xleft = 0.72;
xright = 0.96;
}
TLegend *leg = new TLegend(xleft,top-ysize,xright,top, NULL,"brNDC");
Collector->Add(leg);
leg->SetFillColor(0);
leg->SetLineColor(1);
leg->SetBorderSize(0);
leg->SetTextFont(42);
leg->SetFillStyle(0);
for (Int_t i=0; i<fNumOfSamples; ++i){
TString legname = TString::Format("leg_entry_%i",i);
TString legtitle = ((TObjString*) fSampleNames->At(i))->GetName();
legtitle.ReplaceAll("SPACE", " ");
legtitle.ReplaceAll("[", "{");
legtitle.ReplaceAll("]", "}");
TLegendEntry* entry = NULL;
if(fSampleMarkers.At(i)!=0) {
entry = leg->AddEntry(legname, legtitle, "lpf");
entry->SetLineColor(fSampleColors.At(i));
} else if (fSampleMarkers.At(i)==0) {
entry = leg->AddEntry(legname, legtitle, "f");
} else if (fSampleMarkers.At(i)<0) {
entry = leg->AddEntry(legname, legtitle, "l");
}
entry->SetLineWidth(1);
if (fSampleMarkers.At(i)>0){
entry->SetMarkerColor(fSampleColors.At(i));
entry->SetMarkerStyle(fSampleMarkers.At(i));
entry->SetMarkerSize(0.8);
//entry->SetLineWidth(2);
} else {
entry->SetMarkerStyle(0);
entry->SetMarkerSize(0);
entry->SetMarkerColor(fSampleColors.At(i));
//entry->SetLineWidth(2);
// only line
if (fSampleMarkers.At(i)<0){
entry->SetLineWidth(2);
if (fSampleMarkers.At(i)==-1) entry->SetLineStyle(kSolid);
if (fSampleMarkers.At(i)==-2) entry->SetLineStyle(kDotted);
if (fSampleMarkers.At(i)==-3) entry->SetLineStyle(kDashDotted);
if (fSampleMarkers.At(i)==-4) entry->SetLineStyle(kDashDotted);
// fill
} else {
entry->SetFillColor(fSampleColors.At(i));
entry->SetFillStyle(1001);
}
}
entry->SetTextAlign(12);
//entry->SetTextColor(fSampleColors.At(i));
}
leg->Draw();
// subtitle, if given
if ( PlotSubtitle ){
TPaveText* text;
if (bDrawEntries){
text = new TPaveText(xleft,top-ysize-0.1, xright-0.03, top-ysize-0.03, "NDC");
} else {
text = new TPaveText(0.23, 0.88, xleft-0.03, 0.94, "NDC");
}
Collector->Add(text);
text->SetBorderSize(0);
text->SetFillColor(0);
text->AddText(subtitle);
text->Draw();
text->Paint();
}
///////////////////
// draw entries
//////////////////
if (bDrawEntries){
TPaveText* NumEntries = new TPaveText(xleft-0.4, top-ysize, xleft-0.02, top, "brNDC");
Collector->Add(NumEntries);
NumEntries->SetFillColor(kWhite);
NumEntries->SetBorderSize(2);
NumEntries->SetShadowColor(kWhite);
// bug: (TODO!)
// use OneDHistArray->GetEntries() instead of fNumOfSamples
// question: how to get the numbers from the stack?
for (Int_t jhist=0; jhist<fNumOfSamples; ++jhist){
TH1* hist = (TH1*) OneDHistArray->At(jhist);
// number of entries
Double_t HistEntries;
if (!bShapeNorm){
HistEntries = hist->GetSumOfWeights();
} else {
HistEntries = hist->GetEntries();
}
Int_t Num = static_cast<Int_t>(HistEntries);
Float_t HistMean = static_cast<Float_t>(hist->GetMean());
TString entlegtitle;
entlegtitle.Form("entries: %i mean: %5.3f", Num, HistMean);
TText* text = NumEntries->AddText(entlegtitle);
text->SetTextSize(0.03);
text->SetTextAlign(12);
text->SetTextColor(fSampleColors.At(jhist));
}
NumEntries->Draw();
}
if(bDrawLumi){
TLatex *text1 = new TLatex(3.570061,23.08044,"CMS Preliminary");
text1->SetNDC();
text1->SetTextAlign(13);
text1->SetX(0.22);
text1->SetY(0.918);
text1->SetTextFont(42);
text1->SetTextSizePixels(24);
text1->Draw();
TLatex *text2 = new TLatex(3.570061,23.08044,"5.2 fb^{-1} at #sqrt{s} = 8 TeV");
text2->SetNDC();
text2->SetTextAlign(13);
text2->SetX(0.22);
text2->SetY(0.87);
text2->SetTextFont(42);
text2->SetTextSizePixels(24);
text2->Draw();
}
////////////////////////
// draw the ratio //
////////////////////////
if (bPlotRatio){
switch (ihist%2){
case 0: rp1->cd(); break;
case 1: rp2->cd(); break;
default: break;
}
gPad->SetLogx(0);
gPad->SetLogy(0);
// set up ratio
TObjArray* RatioHistArray = new TObjArray();
// special treatment for TProfiles
TH1D* tempRatioHist = NULL;
for ( Int_t iChain=1; iChain<NOneDHists; iChain++ ){
if (FirstHist->InheritsFrom(TProfile::Class())){
tempRatioHist = ((TProfile*)FirstHist)->ProjectionX();
Collector->Add(tempRatioHist);
TH1D* denom = (TProfile*) OneDHistArray->At(iChain);
tempRatioHist->Divide( denom );
tempRatioHist->GetYaxis()->SetRangeUser(0.3, 1.7);
CopyStyle((*tempRatioHist), denom);
RatioHistArray->Add(tempRatioHist);