-
Notifications
You must be signed in to change notification settings - Fork 1
/
SPlotter.cxx
1978 lines (1614 loc) · 55.6 KB
/
SPlotter.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 <iostream>
#include <iomanip>
#include <TObjArray.h>
#include <TObjString.h>
#include <TStyle.h>
#include <TFile.h>
#include <TROOT.h>
#include <TRandom3.h>
#include <TPaveText.h>
#include <TLegend.h>
#include <TLegendEntry.h>
#include <TLatex.h>
#include <TEllipse.h>
#include <TF1.h>
#include <TGraphAsymmErrors.h>
#include <TMath.h>
#include <TColor.h>
#include "SPlotter.h"
using namespace std;
SPlotter::SPlotter()
{
m_can = NULL;
m_ps = NULL;
m_ps_name = "default.ps";
m_pad1 = NULL;
m_pad2 = NULL;
m_rp1_top = NULL;
m_rp1 = NULL;
m_rp2_top = NULL;
m_rp2 = NULL;
m_page = 0;
m_lumi = 0;
m_syserr = -1;
debug = false;
bShapeNorm = false;
bPortrait = true;
bDrawEntries = false;
bDrawLumi = true;
bForPrelim = false;
bForPublication = false;
bDrawLegend = true;
bPlotRatio = false;
bZScoreInRatio = false;
bSingleEPS = false;
need_update = true;
bPlotLogy = false;
bIgnoreEmptyBins = false;
}
SPlotter::~SPlotter()
{
Cleanup();
}
void SPlotter::SetPsFilename(TString name)
{
if (!name.EndsWith(".ps")){
cerr << "SPlotter::SetPsFilename, given filename: " << name
<< " does not end with .ps, intention? Please correct steering." << endl;
exit(EXIT_FAILURE);
}
m_ps_name = name;
}
void SPlotter::DoStacking(vector<TObjArray*>& hists, TObjArray* StackNames, bool rename)
{
if (hists.size()==0){
cerr << "SPlotter::DoStacking: Empty array of histograms. Aborting." << endl;
exit(EXIT_FAILURE);
}
if (!StackNames){ // trivial case: do nothing
return;
}
// loop over all histogram arrays
int narr = hists.size();
for (int i=narr-1; i>=0; --i){
TObjArray* ha = hists[i];
if (ha->GetEntries()<1) continue;
TString proc = ((SHist*)ha->At(0))->GetProcessName();
if (debug) cout << "SPlotter::DoStacking, hist array = " << i
<< " process name = " << proc << endl;
// loop over all stack-names
for (int j=0; j<StackNames->GetEntries(); ++j){
TString sname = ((TObjString*)StackNames->At(j))->GetString();
if (debug) cout << " stack name = " << sname << endl;
if (proc.Contains(sname)){
if (debug) cout << " -> found match, stacking this array." << endl;
StackHists(hists, i, rename);
break;
}
}
}
if (debug) cout << "SPlotter::DoStacking: Done." << endl;
return;
}
void SPlotter::StackHists(std::vector<TObjArray*>& hists, int index, bool rename)
{
// stack histograms at position 'index' with an existing array of stacks
// in hists
// if the stacks don't exist, they are created and added to the array
// get the stack (create a new one if it doesn't exist yet)
TObjArray* stacks = GetStacks(hists, index);
// add the histograms at 'index' to the stack
for (int i=0; i<stacks->GetEntries(); ++i){
SHist* stack = (SHist*)stacks->At(i);
SHist* hist = (SHist*)hists[index]->At(i);
if (!stack || !hist){
cerr << "SPlotter::StackHists: stack or hist at position " << i
<< " does not exist! Abort." << endl;
cerr << "index of hists = " << hists.size() << " histograms = " << hists[index]->GetEntries() << endl;
exit(EXIT_FAILURE);
}
// sanity check: compare names
TString stackname = stack->GetStack()->GetName();
TString histname = hist->GetHist()->GetName();
if (!stackname.Contains(histname)){
cerr << "SPlotter::StackHists: incompatible histograms at position " << i
<< ", stackname = " << stackname << " histname = " << histname
<< ". Prefer to exit because of consistency." << endl;
exit(EXIT_FAILURE);
}
// still here? do the stackin'!
hist->GetHist()->SetFillColor(hist->GetHist()->GetLineColor());
hist->GetHist()->SetFillStyle(1001);
if (rename){
TString pname = hist->GetProcessName();
hist->SetName(histname + "__" + pname);
}
stack->GetStack()->Add(hist->GetHist());
stack->GetStack()->SetTitle(hist->GetHist()->GetTitle());
hist->SetIsUsedInStack(true);
hist->SetDoDraw(false);
stack->SetUnc(hist->GetUnc(), stack->GetStack()->GetHists()->GetSize()-1);
if (debug) cout << "stacking hist " << histname << " on " << stackname << " for process " << hist->GetProcessName()
<< " (dir = " << stack->GetDir() << ")" << endl;
if (i==0){
cout << "stacking process " << hist->GetProcessName() << " with weight " << hist->GetWeight() << " and uncertainty " << hist->GetUnc() << endl;
}
}
return;
}
TObjArray* SPlotter::GetStacks(std::vector<TObjArray*>& hists, int index)
{
// get the array of stacks from the input hists
// if it doesn't exist, a new array will be created if index>0
// and the hists at position 'index' will be used as blue-print
// try to find a stack in the array
TObjArray* arr = NULL;
int narr = hists.size();
for (int i=0; i<narr; ++i){
if (hists[i]->GetEntries()==0){
cerr << "SPlotter::GetStacks: Got no histograms in array " << i
<< " unexpected behaviour - abort." << endl;
exit(EXIT_FAILURE);
}
arr = hists[i];
SHist* sh = (SHist*) arr->At(0);
if (sh->IsStack()){
if (debug) cout << "SPlotter::GetStacks: Found stack at position " << i << endl;
return arr;
}
}
// no stack found, create a new array with THStacks -> use position 'index'
// in the array as a blue-print
if (index>-1){
if (debug) cout << "SPlotter::GetStacks: Creating new array of THStacks "
<< "using position " << index << " as blueprint." << endl;
if (index>narr){
cerr << "SPlotter::GetStacks: Can not create an array of stacks from array"
<< " index " << index << ", since size is only " << hists.size()
<< ". Unexpected behaviour - abort." << endl;
exit(EXIT_FAILURE);
}
arr = new TObjArray();
for (int i=0; i<hists[index]->GetEntries();++i){
TString hname = ((SHist*)hists[index]->At(i))->GetHist()->GetName();
TString name = hname + "_stack";
THStack* st = new THStack(name, "");
SHist* sh = new SHist(st);
sh->SetDir(((SHist*)hists[index]->At(i))->GetDir());
sh->SetProcessName("SM");
sh->SetDoDraw(true);
arr->Add(sh);
if (debug) cout << "SPlotter::GetStacks: Adding stack with name " << name
<< " in directory " << sh->GetDir() << " at position " << i << endl;
}
hists.push_back(arr);
if (debug) cout << "SPlotter::GetStacks: Added stack array to collection. "
<< "New size = " << hists.size() << ", old = " << narr << endl;
}
return arr;
}
void SPlotter::CopyStyle(TH1& h1, TH1* h2)
{
// copy the style from hist2 to hist1
h1.SetMarkerStyle(h2->GetMarkerStyle());
h1.SetMarkerSize(h2->GetMarkerSize());
h1.SetMarkerColor(h2->GetMarkerColor());
h1.SetLineWidth(h2->GetLineWidth());
h1.SetLineStyle(h2->GetLineStyle());
h1.SetLineColor(h2->GetLineColor());
h1.SetFillColor(h2->GetFillColor());
return;
}
void SPlotter::SetupGlobalStyle()
{
// 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();
}
void SPlotter::Cleanup()
{
// do what the name suggests
ClosePostscript();
if (m_can){
delete m_can;
m_can = NULL;
}
}
void SPlotter::SetupCanvas()
{
// set up a canvas, different possibilities
// to take into account portrait/landscape
// and ratio/no ratio plots
Int_t CanWidth;
Int_t CanHeight;
if (bPortrait){
CanWidth = 600;
CanHeight = 830;
} else {
CanWidth = 800;
CanHeight = 600;
}
// set up the canvas
m_can = new TCanvas("canvas","Control Plots", CanWidth, CanHeight);
Float_t yplot = 0.3;
Float_t yratio = 0.17;
// 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
m_rp1_top = new TPad("pad1", "Control Plots 1", x1, y5, x2, y6);
m_rp1 = new TPad("rp1", "Ratio1", x1, y4, x2, y5);
m_rp2_top = new TPad("pad2", "Control Plots 2", x1, y2, x2, y3);
m_rp2 = new TPad("rp2", "Ratio2", x1, y1, x2, y2);
m_pad1 = new TPad("pad1", "Control Plots 1", x1, y4, x2, y6);
m_pad2 = new TPad("pad2", "Control Plots 2", x1, y1, x2, y3);
// set margins for portrait mode
if (bPortrait){
m_pad1->SetTopMargin(0.05); m_pad1->SetBottomMargin(0.16); m_pad1->SetLeftMargin(0.19); m_pad1->SetRightMargin(0.05);
m_pad2->SetTopMargin(0.05); m_pad2->SetBottomMargin(0.16); m_pad2->SetLeftMargin(0.19); m_pad2->SetRightMargin(0.05);
m_rp1_top->SetTopMargin(0.065); m_rp1_top->SetBottomMargin(0.0); m_rp1_top->SetLeftMargin(0.19); m_rp1_top->SetRightMargin(0.05);
m_rp2_top->SetTopMargin(0.065); m_rp2_top->SetBottomMargin(0.0); m_rp2_top->SetLeftMargin(0.19); m_rp2_top->SetRightMargin(0.05);
m_rp1->SetTopMargin(0.0); m_rp1->SetBottomMargin(0.35); m_rp1->SetLeftMargin(0.19); m_rp1->SetRightMargin(0.05);
m_rp2->SetTopMargin(0.0); m_rp2->SetBottomMargin(0.35); m_rp2->SetLeftMargin(0.19); m_rp2->SetRightMargin(0.05);
// margins for landscape
} else {
m_rp1_top->SetTopMargin(0.065); m_rp1_top->SetBottomMargin(0.0); m_rp1_top->SetLeftMargin(0.13); m_rp1_top->SetRightMargin(0.05);
m_rp2_top->SetTopMargin(0.065); m_rp2_top->SetBottomMargin(0.0); m_rp2_top->SetLeftMargin(0.13); m_rp2_top->SetRightMargin(0.05);
if (bPlotRatio){
m_rp1->SetTopMargin(0.0); m_rp1->SetBottomMargin(0.35); m_rp1->SetLeftMargin(0.13); m_rp1->SetRightMargin(0.05);
m_rp2->SetTopMargin(0.0); m_rp2->SetBottomMargin(0.35); m_rp2->SetLeftMargin(0.13); m_rp2->SetRightMargin(0.05);
}
}
if (debug){
m_rp1_top->SetFillColor(kYellow);
m_rp2_top->SetFillColor(kOrange);
if (bPlotRatio){
m_rp1->SetFillColor(kGray);
m_rp2->SetFillColor(kGray);
}
}
m_pad1->Draw();
m_pad2->Draw();
m_rp1_top->Draw();
m_rp2_top->Draw();
if (bPlotRatio){
m_rp1->Draw();
m_rp2->Draw();
}
return;
}
void SPlotter::SetupCanvasForEPS()
{
// set up a canvas for single EPS files
// optimised plots for including in theses or publications and documents
// different possibilities
// ratio/no ratio plots
Int_t CanWidth;
Int_t CanHeight;
CanWidth = 400;
CanHeight = 400;
// set up the canvas
m_can = new TCanvas("canvas","Control Plots", CanWidth, CanHeight);
Float_t yplot = 0.65;
Float_t yratio = 0.34;
// coordinates:
// set up the coordinates of the two pads: //
Float_t y1, y2, y3; // y3 +-------------+
y3 = 0.99; // | |
y2 = y3-yplot; // | pad1 |
y1 = y2-yratio; // y2 |-------------|
Float_t x1, x2; // | rp1 |
x1 = 0.01; // y1 +-------------+
x2 = 0.99; // x1 x2
//
// No Pad 2!
m_rp1_top = new TPad("pad1", "Control Plots 2", x1, y2, x2, y3);
m_rp1 = new TPad("rp1", "Ratio2", x1, y1, x2, y2);
m_pad1 = new TPad("pad1", "Control Plots 2", x1, y1, x2, y3);
m_rp2_top = new TPad("pad1", "Control Plots 2", x1, y2, x2, y3);
m_rp2 = new TPad("rp1", "Ratio2", x1, y1, x2, y2);
m_pad2 = new TPad("pad1", "Control Plots 2", x1, y1, x2, y3);
m_pad1->SetTopMargin(0.05); m_pad1->SetBottomMargin(0.16); m_pad1->SetLeftMargin(0.19); m_pad1->SetRightMargin(0.05);
m_pad2->SetTopMargin(0.05); m_pad2->SetBottomMargin(0.16); m_pad2->SetLeftMargin(0.19); m_pad2->SetRightMargin(0.05);
m_rp1_top->SetTopMargin(0.065); m_rp1_top->SetBottomMargin(0.0); m_rp1_top->SetLeftMargin(0.19); m_rp1_top->SetRightMargin(0.05);
m_rp2_top->SetTopMargin(0.065); m_rp2_top->SetBottomMargin(0.0); m_rp2_top->SetLeftMargin(0.19); m_rp2_top->SetRightMargin(0.05);
m_rp1->SetTopMargin(0.0); m_rp1->SetBottomMargin(0.35); m_rp1->SetLeftMargin(0.19); m_rp1->SetRightMargin(0.05);
m_rp2->SetTopMargin(0.0); m_rp2->SetBottomMargin(0.35); m_rp2->SetLeftMargin(0.19); m_rp2->SetRightMargin(0.05);
if (debug){
m_rp1_top->SetFillColor(kYellow);
m_rp2_top->SetFillColor(kOrange);
if (bPlotRatio){
m_rp1->SetFillColor(kGray);
m_rp2->SetFillColor(kGray);
}
}
m_pad1->Draw();
m_pad2->Draw();
m_rp1_top->Draw();
m_rp2_top->Draw();
if (bPlotRatio){
m_rp1->Draw();
m_rp2->Draw();
}
return;
}
void SPlotter::OpenPostscript(TString dir, TString hname)
{
// create a new ps file with the directory in the name
// optional: for EPS files add the name of the histogram
TString filename(m_ps_name);
filename.ReplaceAll(".ps","");
filename.Append("_");
filename.Append(dir);
filename.Append(".ps");
if (bSingleEPS){
filename.ReplaceAll(".ps","");
filename.Append("_");
filename.Append(hname);
filename.Append(".eps");
} else {
TString text(dir);
text.Prepend("Plotting all histograms in directory ");
cout << "\n+-------------------------- SFrame Plotter ---------------------------+" << endl;
cout << "| " << setw(60)<< text << " |" << endl;
cout << "+---------------------------------------------------------------------+" << endl;
m_page = 0;
}
m_ps = NULL;
if (bSingleEPS){
m_ps = new TPostScript(filename, 113); // eps output
} else {
if (bPortrait){
m_ps = new TPostScript(filename, 111); // ps output
m_ps->Range(20.0, 30.0);
} else {
m_ps = new TPostScript(filename, 112); // ps output
m_ps->Range(27.0, 18.0);
}
}
}
void SPlotter::ClosePostscript()
{
// close the ps file and set page number to 0
if (m_ps){
m_ps->Close();
delete m_ps;
m_ps = NULL;
}
m_page = 0;
}
void SPlotter::ProcessAndPlot(std::vector<TObjArray*> histarr)
{
// loop over all arrays in the input array and plot them
if (histarr.size()<1){
cerr << "SPlotter::ProcessAndPlot: No arrays of histograms given. Abort." << endl;
exit(EXIT_FAILURE);
}
if (histarr[0]->GetEntries()<1){
cerr << "SPlotter::ProcessAndPlot: No histograms given. Abort." << endl;
exit(EXIT_FAILURE);
}
if (bPlotRatio && histarr.size()==1){
cerr << "SPlotter::ProcessAndPlot: Only one process given, can not plot "
<< " ratio. Steering correct?" << endl;
exit(EXIT_FAILURE);
}
SetupGlobalStyle();
TString psname = m_ps_name;
TString current_dir = "";
// loop over all histograms and plot them!
int iplot = 1;
bool bleg = true;
for (int i=0; i<histarr[0]->GetEntries(); ++i){
// get the histograms for the different processes
vector<SHist*> hists = GetPlottableHists(histarr, i);
// no plottable hists found at position i
if (debug) cout << "Number of plottable hists at index " << i << " = " << hists.size() << endl;
if (hists.size()==0) continue;
if (bShapeNorm) ShapeNormalise(hists);
int ipad = GetCurrentPad(iplot);
if (debug) cout << "Plotting histograms " << hists[0]->GetName()
<< " iplot = " << iplot << " ipad = " << ipad << endl;
// new directory? create new ps file for ps-book!
if (!bSingleEPS){
TString dir = hists[0]->GetDir();
if (dir.CompareTo(current_dir)!=0){
if (iplot!=1) DrawPageNum();
Cleanup();
SetupCanvas();
OpenPostscript(dir);
current_dir = dir;
iplot = 1;
ipad = 1;
bleg = true;
}
// new page every second plot
if (iplot%2==1){
if (debug) cout << "Creating new page with number " << m_page << endl;
DrawPageNum();
if (need_update) m_can->Update();
m_ps->NewPage();
++m_page;
}
// new file for each plot in single EPS mode
} else {
TString dir = hists[0]->GetDir();
TString hname = hists[0]->GetName();
Cleanup();
SetupCanvasForEPS();
if (debug) cout << "Creating new eps file with name " << dir << "_" << hname << endl;
OpenPostscript(dir, hname);
current_dir = dir;
iplot = 1;
bleg = true;
}
// cosmetics
DoCosmetics(hists);
// ---------- do what we set out to do: plot! ----------------
if (hists[0]->IsYieldPlot()){ // special treatment for lumi yield plot
PlotLumiYield(hists[0], ipad);
} else { // usual plots
PlotHists(hists, ipad);
// draw a legend
if (bleg){
DrawLegend(GetHistsAtIndex(histarr, i));
if (!bDrawLegend) bleg = false;
}
// draw lumi information
if (bDrawLumi) DrawLumi();
// draw the ratio
if (bPlotRatio){
if (bZScoreInRatio) PlotZScore(hists, ipad);
else PlotRatios(hists, ipad);
}
}
++iplot;
}
// done!
if (!bSingleEPS) DrawPageNum();
if (need_update) m_can->Update();
Cleanup();
}
void SPlotter::PlotLumiYield(SHist* hist, int ipad)
{
// plot the lumi yield histogram
if (ipad==1) m_pad1->cd();
if (ipad==2) m_pad2->cd();
hist->Draw();
// calculate the average
TH1* h = hist->GetHist();
double sum=0;
int bins=0;
for (int i=1; i<h->GetNbinsX()+1; ++i){
if (h->GetBinContent(i)>0){
sum += h->GetBinContent(i);
bins++;
}
}
double av = sum / bins;
// calculate average with outlier-rejection (4sigma)
sum=0;
bins=0;
for (int i=1; i<h->GetNbinsX()+1; ++i){
if (h->GetBinContent(i)>0){
double dev = TMath::Abs( (h->GetBinContent(i) - av)/h->GetBinError(i) );
if (dev<4){
sum += h->GetBinContent(i);
bins++;
} else {
cout << "Lumi yield: outlier in bin " << i << " with content " << h->GetBinContent(i) << " average = " << av << endl;
}
}
}
av = sum / bins;
// calculate error on mean and chi2
double dev = 0;
double chi2 = 0;
for (int i=1; i<h->GetNbinsX()+1; ++i){
if (h->GetBinContent(i)>0){
double pull = (h->GetBinContent(i) - av)/h->GetBinError(i);
if (TMath::Abs(pull)<4){
dev += TMath::Power(h->GetBinContent(i)-av, 2);
chi2 += pull*pull;
}
}
}
double err = TMath::Sqrt(dev/bins);
// highlight points with deviations of more than 3, 4 and 5 sigma
double xr = h->GetXaxis()->GetXmax() - h->GetXaxis()->GetXmin();
double wi = gPad->GetAbsWNDC() * (1 - gPad->GetLeftMargin() - gPad->GetRightMargin());
double he = gPad->GetAbsHNDC() * (1 - gPad->GetTopMargin() - gPad->GetBottomMargin());
double ar = wi/he;
double fudge = 1.;
if (bSingleEPS) fudge = 1.2;
double r1 = 0.02*xr*fudge;
double yr = h->GetMaximum()-h->GetMinimum();
double r2 = 0.016*yr*ar*fudge;
for (int i=1; i<h->GetNbinsX()+1; ++i){
if (h->GetBinContent(i)>0){
double pull = (h->GetBinContent(i) - av)/h->GetBinError(i);
if (TMath::Abs(pull)>5){
TEllipse* circ = new TEllipse(h->GetXaxis()->GetBinCenter(i), h->GetBinContent(i), r1, r2);
circ->SetFillColor(kWhite);
circ->SetLineColor(kRed);
circ->Draw();
} else if (TMath::Abs(pull)>4){
TEllipse* circ = new TEllipse(h->GetXaxis()->GetBinCenter(i), h->GetBinContent(i), r1, r2);
circ->SetFillColor(kWhite);
circ->SetLineColor(kOrange);
circ->Draw();
} else if (TMath::Abs(pull)>3){
TEllipse* circ = new TEllipse(h->GetXaxis()->GetBinCenter(i), h->GetBinContent(i), r1, r2);
circ->SetFillColor(kWhite);
circ->SetLineColor(kSpring);
circ->Draw();
}
}
}
// draw the average
TF1* f = new TF1("average", "[0]", h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
f->SetLineColor(kAzure+1);
f->SetLineWidth(1);
f->SetParameter(0, av);
f->Draw("same");
TF1* fup = new TF1("up", "[0]", h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
TF1* fdown = new TF1("down", "[0]", h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
fup->SetParameter(0, av+err);
fdown->SetParameter(0, av-err);
fup->SetLineColor(kAzure+1);
fdown->SetLineColor(kAzure+1);
fup->SetLineWidth(1);
fdown->SetLineWidth(1);
fup->SetLineStyle(kDashed);
fdown->SetLineStyle(kDashed);
fup->Draw("same");
fdown->Draw("same");
TLatex* text = new TLatex();
text->SetTextFont(42);
text->SetNDC();
text->SetTextColor(kBlack);
text->SetTextSize(0.05);
if (bSingleEPS) text->SetTextSize(0.04);
text->SetTextAlign(11);
TString info = TString::Format("#chi^{2} / ndf");
text->DrawLatex(0.5, 0.30, info.Data());
info = TString::Format("%3.1f / %d", chi2, bins-1);
text->DrawLatex(0.65, 0.30, info.Data());
info = TString::Format("average");
text->DrawLatex(0.5, 0.23, info.Data());
info = TString::Format("%4.1f #pm %4.1f", av, err);
text->DrawLatex(0.65, 0.23, info.Data());
hist->Draw("same");
return;
}
void SPlotter::PlotHists(vector<SHist*> hists, int ipad)
{
// plot all histograms in the array
if (ipad==1){
if (bPlotRatio) m_rp1_top->cd();
else m_pad1->cd();
}
if (ipad==2){
if (bPlotRatio) m_rp2_top->cd();
else m_pad2->cd();
}
bool isok = SetMinMax(hists);
if (isok) SetLogAxes(hists);
// first get some basic histograms
SHist* sstack = SelStack(hists);
SHist* sdata = SelData(hists);
// first, draw data if it exists
int ndrawn = 0;
if (sdata){
sdata->Draw();
++ndrawn;
}
if (debug){
if (sdata){
cout << "\nHist name = " << sdata->GetName() << " process = " << sdata->GetProcessName() << endl;
cout << "hists, entries = " << hists.size() << endl;
cout << "Data entries = " << sdata->GetHist()->Integral() << endl;
}
if (sstack){
double stack_entries = 0;
TObjArray* arr = sstack->GetStack()->GetStack();
TH1* h = (TH1*)arr->At(arr->GetEntries()-1);
stack_entries = h->Integral();
cout << "Stack entries = " << stack_entries << endl;
TList* hists = sstack->GetStack()->GetHists();
// calculate individual area
for (int i=0; i<hists->GetSize(); ++i){
TH1* h = (TH1*) hists->At(i);
int iend = h->GetNbinsX();
double area = h->Integral(1,iend);
cout << " entries of histogram " << i << " in stack = " << area << endl;
}
}
}
// first round
int nh = hists.size();
for (int i=0; i<nh; ++i){
SHist* sh = hists[i];
if (sh->IsStack()) continue;
if (sh==sdata) continue;
if (ndrawn==0) sh->Draw();
else sh->Draw("same");
++ndrawn;
}
// now draw the stack
if (sstack){
if (ndrawn==0){
sstack->Draw();
StackCosmetics(sstack->GetStack());
gPad->Update();
need_update = false;
} else {
sstack->Draw("same");
}
}
// second round
for (int i=0; i<nh; ++i){
SHist* sh = hists[i];
if (sh->IsStack()) continue;
if (sh==sdata) continue;
sh->Draw("same");
}
// draw normalisation error if it is given
if (sstack){
DrawSysError(sstack);
}
// draw data on top
if (sdata) sdata->Draw("same");
gPad->RedrawAxis();
}
void SPlotter::DrawSysError(SHist* stack)
{
// plot an error band corresponding to the overall
// systematic error
// if a theta file is used as input with shape variations,
// also the error from those is drawn
TH1* h = (TH1*) stack->GetStack()->GetStack()->At( stack->GetStack()->GetStack()->GetEntries()-1 );
//TH1* e = (TH1*) h->Clone();
TGraphAsymmErrors* eAsym = new TGraphAsymmErrors();
for (Int_t i=1; i<h->GetNbinsX()+1; ++i){
Double_t sys = 0;
if (m_syserr>0) sys = m_syserr*h->GetBinContent(i);
Double_t stat = h->GetBinError(i);
Double_t norm_err = CalcNormErrorForBin(stack, i);
Double_t sys_err_plus = CalcShapeSysErrorForBinFromTheta(stack, i, "plus");
Double_t sys_err_minus = CalcShapeSysErrorForBinFromTheta(stack, i, "minus");
Double_t ey_low = TMath::Sqrt(norm_err*norm_err + sys*sys + stat*stat + sys_err_minus*sys_err_minus);
Double_t ey_up = TMath::Sqrt(norm_err*norm_err + sys*sys + stat*stat + sys_err_plus*sys_err_plus);
Double_t ex_low = (h->GetXaxis()->GetBinCenter(i)) - (h->GetXaxis()->GetBinLowEdge(i));
Double_t ex_up = (h->GetXaxis()->GetBinUpEdge(i))-h->GetXaxis()->GetBinCenter(i);
eAsym -> SetPoint(i, h->GetXaxis()->GetBinCenter(i), h->GetBinContent(i));
eAsym -> SetPointError(i, ex_low, ex_up, ey_low, ey_up);
}
static Int_t LightGray = TColor::GetColor( "#aaaaaa" );
//h->SetFillColor(kGray+2);
eAsym->SetFillColor(LightGray);
eAsym->SetLineWidth(1);
eAsym->SetFillStyle(3245);
eAsym->Draw("E2 same");
}
double SPlotter::CalcNormErrorForBin(SHist* stack, int ibin)
{
// calculate the normalisation uncertainty of a single bin in the stack
// due to normalisation uncertainty on different processes
double err = 0;
for (int i=0; i<stack->GetStack()->GetStack()->GetEntries(); ++i){
TH1* h = (TH1*) stack->GetStack()->GetHists()->At(i);
err += h->GetBinContent(ibin)*stack->GetUnc(i);
}
return err;
}
double SPlotter::CalcShapeSysErrorForBinFromTheta(SHist* stack, int ibin, TString sign)
{
double absoluteerr = 0;
double squarederr = 0;
double err = 0;
if (m_shapesys_arr.size()==0)//no systamtics given in theta file
return err;
if (sign!="plus" && sign!="minus"){
cout << "error in call to CalcShapeSysErrorForBinFromTheta: sign can only be 'plus' or 'minus', no systematic error will be calculated" << endl;
return err;
}
// loop over all background samples to find the process
for (int i=0; i<stack->GetStack()->GetStack()->GetEntries(); ++i){
TH1* h = (TH1*) stack->GetStack()->GetHists()->At(i);
TString histname = h->GetName(); //e.g. HT__QCD
histname.ReplaceAll("__", "#");
TObjArray* histnamePieces = histname.Tokenize("#");
TString variableName = ((TObjString*)histnamePieces->At(0))-> GetString(); //this is the sample name e.g. HT
TString sampleName = ((TObjString*)histnamePieces->At(1))-> GetString(); //this is the sample name e.g. QCD or TTbar
// loop over all systematic error samples
for (unsigned int syst = 0; syst < m_shapesys_arr.size(); ++syst){
TObjArray* arr = m_shapesys_arr[syst];
for (int nplots = 0; nplots < arr->GetEntries(); ++nplots){
SHist* hSys = (SHist*)arr->At(nplots);
TH1* hSyst = hSys->GetHist();
TString systFullName = hSys -> GetProcessName();//e.g. QCD__uncert__plus
systFullName.ReplaceAll("__","#");
TObjArray* systFullNamePieces = systFullName.Tokenize("#");
TString systVariableName = hSys -> GetName();
// continue if the channel of the systematic sample has the same name as the channel of the background process
if (variableName == systVariableName){
// check if the the sign is the same as requested
TString syssign = ((TObjString*) systFullNamePieces->At(2))->GetString();
if (syssign == sign){
// check if systematic uncertainty comes from the same sample as the background (e.g. ttbar)
if (systFullNamePieces->Contains(sampleName)){
absoluteerr = (hSyst->GetBinContent(ibin))-(h->GetBinContent(ibin));
// the second one contains the name of the uncertainty: check if the error should be reduced
TString sysname = ((TObjString*) systFullNamePieces->At(1))->GetString();
// loop over systematics that should be reduced, find the right factor
for (Int_t j=0; j<m_ScaleSysUncName->GetEntries(); ++j){
TString sysname_to_red = ((TObjString*) m_ScaleSysUncName->At(j))->GetString();
if (sysname == sysname_to_red){
absoluteerr *= m_sysweight.At(j);
}
}
// got it: add to the total error in quadrature
squarederr += absoluteerr*absoluteerr;
}
}
}
}
}
}
err = TMath::Sqrt(squarederr);
return err;
}
void SPlotter::PlotRatios(vector<SHist*> hists, int ipad)
{
// plot all histograms in the array
if (ipad==1) m_rp1->cd();
if (ipad==2) m_rp2->cd();
// calculate ratios
vector<SHist*> ratios = CalcRatios(hists);
gPad->SetLogx(0);
gPad->SetLogy(0);
int ndrawn = 0;
int nh = ratios.size();
for (int i=0; i<nh; ++i){
SHist* rh = ratios[i];
rh->DrawNoErrorX(false);
TString name = rh->GetName();
if (name.Contains("_lx")) gPad->SetLogx(1);
if (ndrawn==0) rh->Draw();
else rh->Draw("same");
++ndrawn;
}
gPad->RedrawAxis();
}
vector<SHist*> SPlotter::CalcRatios(vector<SHist*> hists)
{
// build ratios from the array 'hists'
// by default it is checked if a data histogram exists,