-
Notifications
You must be signed in to change notification settings - Fork 2
/
plotTemplate.py
1053 lines (911 loc) · 40.5 KB
/
plotTemplate.py
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
import sys
sys.path.append('cfg/')
from frameworkStructure import pathes
sys.path.append(pathes.basePath)
import ROOT
import ratios
from setTDRStyle import setTDRStyle
style = setTDRStyle()
style.SetPadLeftMargin(0.13)
style.SetTitleYOffset(0.9)
style.SetPadTopMargin(0.0675)
from helpers import *
################## SUMMARY OF CLASS plotTemplate #######################
## Constructors:
# * plotTemplate()
#
## Methods:
# * setPrimaryPlot(plot, drawOption)
# Plot (histogram, graph) to be drawn first with drawOption,
# defining the axes and boundaries, redrawn
# after secondary plots by default so it will be on top
#
# * addSecondaryPlot(plot, drawOption)
# Adds plot (anything with Draw(drawOption) method in root) to
# list of secondary plots
#
# * clearSecondaryPlots()
# Resets list of secondary plots
#
# * addRatioErrorByHist(self,title, histUp, histDown, color, fillStyle)
# See the RatioGraph class
#
# * addRatioErrorBySize(self,title, size, color, fillStyle, add)
# See the RatioGraph class
#
# * addRatioPair(nominator, denominator, color)
# Add a pair of histograms to be drawn in the ratioGraph. Will
# also be used for efficiency plots.
#
# * addResidualPlot(h1, h2, range=None, color=ROOT.kBlack, markerStyle=20, markerSize=1, fillColor=ROOT.kWhite, errList=None, options="")
# Add a residual plot to be drawn with h1 and h2. h1 can be TH1 or
# TGraphErrors, while h2 can also be TF1. errList can be used to
# apply custom errors, each entry in the list being the error to be
# used. A range can be defined in the format (min, max), where min
# max are included.
# options: "-" to multiply residuals by -1.
# "P" to create a pull plot (divide by uncertainties)
# "H" to draw as a histogram (without errors). fillColor
# is used as the histogram bar color
#
# * clearRatioPairs()
# Empty list of ratioPairs
#
# * draw()
# Draw full canvas
#
# * drawCanvas()
# Draws canvas and pads in case one does not want to use draw()
#
# * drawLatexLabels()
# If any of the labels were changed after calling draw() or one
# does not want to use draw(), this can be called to (re)draw all
# the labels
#
# * drawRatioPlot()
# Draws all ratio or efficiency plots that were added as ratio
# pairs
#
# * drawLegend()
# Draws legend of all plots with specified labels if hasLegend is True
#
# * setFolderPath(folderName)
# Set Name of folder in fig/ to store output
#
# * saveAs(fileName)
# Print canvas to fileName in folder that was defined earlier,
# prints with all defined filetypes, so fileName should not contain
# file ending
#
# * clean()
# Resets the plotTemplate, this is sometimes neccessary to clear
# all objects before exiting a function
#
# * addLabel(label)
# Adds a LatexLabel to the plotTemplate. This label is accessible
# through the name defined for it, the name cannot be a duplicate
# of any class member or of another label that is currently added.
# If name is "", a default unique name is given.
# The label will be accessible via plotTemplate.name where name
# is the label's specified name
#
# * removeLabel(name)
# Removes the label with the specified name from the plotTemplate.
# It will no longer be accessible by plotTemplate.name or any other
# plotTemplate function
#
# * getLabelNames()
# Returns a list of all label names currently included in the
# plotTemplate
#
# * getLabelAttr(name, attr)
# returns specified attribute of label with specified name.
# Can be used instead of direct access to label object
#
# * getLabelAttr(name, attr, value)
# sets specified attribute of label with specified name to value.
# Can be used instead of direct access to label object
#
#
## Members for options:
# -logX,logY,logZ(bools):
# Draw axis with logarithmic scale, false by default
# -nDivX, nDivY (ints):
# Set number of divisions on axes, elsewise just take from primary plot
# -maximumScale(float):
# Maximum of plot scaled by maximum value of primary plot. Default: 1.8
# -minimumScale(float):
# Minimum of plot scaled by minimum value of primary plot. Default: 1.0
# -minimumX/Y/Z, maximumX/Y/Z(floats):
# Overwrites maximum scale, set minimum and maximum x/y-value
# -labelX,labelY,labelZ(strings):
# Set axis title of primary plot, None (default) will not change titles of primary plot
# -marginTop, marginBottom, marginLeft, marginRight(floats):
# Pad outer margins
# -personalWork, preliminary, forTWIKI, plotData(bools):
# For text below CMS logo. Default: True, False, False, False
# -hasRatio, hasEfficiency, hasBottomWindow, hasRatio(bools):
# Draw ratio/efficiency/residual graph, if all are true, only ratio graph is drawn. hasBottomWindow only creates lower pad.
# Default: False
# -hasLegend (bool):
# Automatically draw legend of all added plots that have specified labels
# -ratioLabel, efficiencyLabel(strings):
# Y-axis label of ratio/efficiency graph
# -ratioMin, ratioMax(floats):
# Min/Max values in ratio graph
# -ratioPairs (list of tuples of histogram, histogram, color):
# If not equal to [], will add ratios of the given pairs to the ratioPlot. Will also be used for efficiency plots.
# -redrawPrimary(bool):
# Should primary plot be redrawn to be on top of other plots. On by default, not recommended for 2D plots
# -dilepton (string): // For legacy reasons only
# Used dilepton combination in plot. "SF", "EE", "EMu", "MuMu" will result in predefined dilepton strings.
# If dilepton is set to a different string, this will be used directly.
# -fileTypes (list of strings):
# List of file endings to print the canvas into. Default: ["pdf"]
# -cutsText(string): // For legacy reasons only
# Text to describe cuts, for latexCuts; if not set, the label will not be drawn
# -regionName(string): // For legacy reasons only
# Text for label latexRegion
# -lumiInt, (float):
# Integrated luminosity [fb^-1]
# ---
# Following members are for specifications about the residual plot
# -residualLabel (string):
# Y-axis title of residual graph
# -residualRangeUp (float):
# Upper bound for residual plot if set manually
# -residualRangeLow (float):
# Lower bound for residual plot if set manually. Default "None", so
# -residualRangeUp will be used
# -residualRangeMode (string):
# "MANUAL", "AUTO" or "AUTOASYMM". If AUTO is used, the maximum
# values of residuals will be used (scaled by residualRangeScale [float])
# "AUTOASYMM" allows for residual plot not centered around 0 if the
# distribution is asymmetric around 0. Default: "AUTO"
# -residualZeroLineDo (bool):
# draw line through 0. Default: True
# -residualZeroLineWidth (float):
# width of zero line. Default: 2.0
# -residualZeroLineStyle (float):
# line style of zero line. Default: 2.0
# -residualZeroLineColor (int):
# color of zero line. Default: ROOT.kBlack
################### SUMMARY OF CLASS LatexLabel ########################
#
# This class has the following member attributes to customize:
# -name, text, posX, posY, size, font, align, angle, color
#
# Each of these variables has a getter function with the prefix get_ which
# is used in the method draw(), which draws the LatexLabel with all defined
# options. By default, every method returns just the related attribute, but
# the functions can be overloaded by custom classes which inherit from the
# LatexLabel class. Other functions that can be overloaded are draw() and
# is_drawn() (which returns True by default)
#
# When calling the constructor, an arbitrary number of arguments with
# specified names can be given, these arguments are then added to the object
# as attributes.
# For example, label = LatexLabel(var=1, posX = 0.5) will set label.var = 1
# and label.posX = 0.5
#
# ! When added to a plotTemplate by addLabel(), it is useful to have a unique
# name for the label in order for it to be added. If no such name is defined,
# a name is automatically generated and written to label.name.
# ! A label which is added to plotTemplate by addLabel() also sets the value of
# self.template to the current plotTemplate, so adding the same label to
# different plotTemplates can lead to unexpected behavior if custom functions that
# use label.template are defined.
#
# After being added to a plotTemplate, the LatexLabel object is also accessible as
# an attribute of the plotTemplate, as long as one knows its name
#
# Existing custom LatexLabels are CMSLabel, CMSExtraLabel, CutsLabel, DileptonLabel,
# RegionLabel and LumiLabel. All of these but the first by default access members
# of plotTemplate to define the position or text which is to be drawn, but this
# behavior of the get_ functions is usually overwritten when setting the relevant
# variable to a custom value.
#
def countNumbersUp():
countNumbersUp.counter += 1
return countNumbersUp.counter
countNumbersUp.counter = 0
#TODO implement way for more than one bottom panel
#TODO implement axis label/title size/font configuration
class LatexLabel:
name = ""
latex = None
text = ""
posX = 0
posY = 0
size = 0.04
font = 42
align = 11
angle = 0
color = ROOT.kBlack
drawn = True
template = None
def __init__(self, **kwargs):
self.latex = ROOT.TLatex()
for varname, value in kwargs.iteritems():
setattr(self, varname, value)
def get_text(self):
return self.text
def get_posX(self):
return self.posX
def get_posY(self):
return self.posY
def get_size(self):
return self.size
def get_font(self):
return self.font
def get_align(self):
return self.align
def get_color(self):
return self.color
def get_angle(self):
return self.angle
def is_drawn(self):
return self.drawn
def draw(self):
if self.is_drawn():
self.latex.SetTextSize(self.get_size())
self.latex.SetTextFont(self.get_font())
self.latex.SetTextColor(self.get_color())
self.latex.SetTextAlign(self.get_align())
self.latex.SetTextAngle(self.get_angle())
self.latex.SetNDC(True)
self.latex.DrawLatex(self.get_posX(), self.get_posY(), self.get_text())
class CMSLabel(LatexLabel):
name = "latexCMS"
text = "CMS"
posX = 0.15
posY = 0.9125
size = 0.08
align = 13
font = 61
class CMSExtraLabel(LatexLabel):
name = "latexCMSExtra"
text = None
posX = 0.15
posY = 0.815
simPosY = 0.80
#size = 0.045
size = 0.035
font = 52
def get_text(self):
if self.text != None:
return self.text
text = ""
if self.template.personalWork:
#text = "Work in Progress"
text = "Private Work"
if not self.template.plotData:
#text = "#splitline{Work in Progress}{Simulation}"
text = "#splitline{Private Work}{Simulation}"
elif not self.template.plotData:
text = "Simulation"
elif self.template.preliminary:
text = "Preliminary"
elif self.template.forTWIKI:
text = "Unpublished"
elif self.template.additional:
text = "Supplementary"
return text
def get_posY(self):
if self.template.personalWork and not self.template.plotData:
return self.simPosY
else:
return self.posY
def is_drawn(self):
return self.template.personalWork or not self.template.plotData or self.template.preliminary or self.template.forTWIKI or self.template.additional
class LumiLabel(LatexLabel):
name = "latexLumi"
text = None
posX = None
posY = 0.9475
size = 0.055
align = 31
def get_text(self):
if self.text != None:
return self.text
if self.template.lumiInt == None:
return "13 TeV"
else:
return "%s fb^{-1} (13 TeV)"%(self.template.lumiInt)
def get_posX(self):
if self.posX == None:
return 1 - self.template.marginRight
else:
return self.posX
class CutsLabel(LatexLabel):
name = "latexCuts"
text = None
posX = 0.92
posY = 0.81
size = 0.03
align = 32
def get_text(self):
if self.text != None:
return self.text
if self.template.cutsText == None:
return ""
else:
return self.template.cutsText
class RegionLabel(LatexLabel):
name = "latexRegion"
text = None
posX = 0.92
posY = 0.905
size = 0.03
align = 32
def get_text(self):
if self.text != None:
return self.text
if self.template.regionName == None:
return ""
else:
return self.template.regionName
class DileptonLabel(LatexLabel):
text = None
name = "latexDilepton"
posX = 0.92
posY = 0.875
size = 0.03
align = 32
def get_text(self):
if self.text != None:
return self.text
if self.template.dilepton == "EE":
return "ee"
elif self.template.dilepton == "EMu" or self.template.dilepton == "DF" or self.template.dilepton == "OF":
return "e#mu"
elif self.template.dilepton == "MuMu":
return "#mu#mu"
elif self.template.dilepton == "SF":
return "ee+#mu#mu"
return ""
class plotTemplate:
def __init__(self):
self.pathName = "fig/"
self.folderName = ""
self.fileTypes = ["pdf"]
self.dilepton = None
self.canvasSizeX = 800
self.canvasSizeY = 800
self.logX = False
self.logY = False
self.logZ = False
self.labeledAxisX = False
self.labeledAxisY = False
self.redrawPrimary = True
self.maximumScale = 1.3
self.minimumScale = 1.0
self.maximumX = None
self.minimumX = None
self.maximumY = None
self.minimumY = None
self.maximumZ = None
self.minimumZ = None
self.marginLeft = 0.15
self.marginRight = 0.05
self.marginTop = 0.05
self.marginBottom = 0.14
self.labels = {}
self.lumiInt = None
self.regionName = ""
self.cutsText = None
##############
self.hasLegend = False
self.legend = None
self.legendColumns = 1
self.legendPosX1 = 0.55
self.legendPosX2 = 0.90
self.legendPosY1 = 0.55
self.legendPosY2 = 0.70
self.legendFont = 42
self.legendTextSize = 0.03
self.legendTextColor = 1
self.legendBorderSize = 0
self.legendFillColor = 0
self.legendFillStyle = 0
##############
self.labelX = None
self.labelY = None
self.labelZ = None
self.ratioLabel = "ratio"
self.efficiencyLabel = "efficiency"
self.ratioMin = 0
self.ratioMax = 2
self.ratioTitleOffset = None
self.ratioTitleSize = None
self.ratioLabelSize = None
self.efficiencyMin = 0
self.efficiencyMax = 1.2
self.ratioErrsSize = []
self.ratioErrsHist = []
self.ratioPairs = []
self.ratioPadHeight = 0.3
self.hasEfficiency = False
self.efficiencyOption = "cp"
self.hasRatio = False
self.hasBottomWindow = False
self.hasResidual = False
self.nDivX = None
self.nDivY = None
self.is2D = False
self.residualPlots = []
self.residualGraphs = []
self.residualLabel = ""
self.residualRangeUp = 3
self.residualRangeLow = None
self.residualRangeMode = "AUTO" # MANUAL, AUTO, AUTOASYMM
self.residualRangeScale = 1.1
self.residualZeroLineDo = True
self.residualZeroLine = None
self.residualZeroLineWidth = 2
self.residualZeroLineStyle = 2
self.residualZeroLineColor = ROOT.kBlack
self.forTWIKI = False
self.additional = False
self.preliminary = False
self.personalWork = True
self.plotData = False
self.canvas = None
self.plotPad = None
self.ratioPad = None
self.primaryPlot = None
self.secondaryPlots = []
self.ratioErrsSize = []
self.ratioErrsHist = []
self.ratioPairs = []
self.labels = {}
self.addLabel(CMSLabel ())
self.addLabel(CMSExtraLabel ())
self.addLabel(RegionLabel ())
self.addLabel(CutsLabel ())
self.addLabel(LumiLabel ())
self.addLabel(DileptonLabel ())
def addRatioPair(self, nominator, denominator, color=ROOT.kBlack, markerstyle=20):
self.ratioPairs.append((nominator, denominator, color, markerstyle))
def clearRatioPairs(self):
self.ratioPairs = []
def addRatioErrorBySize(self,title, size, color, fillStyle, add,number=0):
self.ratioErrsSize.append((title,size,color,fillStyle,add,number))
def addRatioErrorByHist(self,title, histUp, histDown, color, fillStyle,number=0):
self.ratioErrsHist.append((title,histUp,histDown,color,fillStyle,number))
def setFolderName(self,name):
self.folderName = name
if name != "":
self.pathName = "fig/%s/"%(name)
else:
self.pathName = "fig/"
def setLabelAttr(self, name, attr, value):
setattr(self.labels[name], attr, value)
def getLabelAttr(self, name, attr):
return getattr(self.labels[name], attr)
def addLabel(self, label):
label.template = self
if label.name == "":
label.name = "Label%s"%(countNumbersUp())
if not hasattr(self, label.name):
self.labels[label.name] = label
setattr(self, label.name, label)
else:
print "plotTemplate Error: Label name %s is either plotTemplate attribute name or name of existing label"%(label.name)
exit()
def getLabelNames(self):
return self.labels.keys()
def removeLabel(self, name):
if name in self.labels:
self.labels[name].template = None
del self.labels[name]
delattr(self, name)
else:
print "plotTemplate Error: Label name to delete was not found"
exit()
def saveAs(self,fileName):
ensurePathExists(self.pathName)
for typ in self.fileTypes:
fn = self.pathName+fileName+"."+typ
self.canvas.Print(fn)
if typ == "eps": # if TMathText is used, these cannot by saved as pdf by root
import os
from locations import locations
os.system("sed -i 's/STIX/STIXX/' %s"%(fn))
os.system("%s %s"%(locations.epsToPdfPath, fn))
os.remove(fn)
def setPrimaryPlot(self,hist, drawOption, label=None, legOption=None):
self.primaryPlot = (hist, drawOption, label, legOption)
def addSecondaryPlot(self,hist, drawOption="", label = None, legOption=None):
self.secondaryPlots.append((hist, drawOption, label, legOption))
def addResidualPlot(self, h1, h2, resRange=None, color=ROOT.kBlack, markerStyle=20, markerSize=1, fillColor=ROOT.kWhite, errList=None, options=""):
self.residualPlots.append((h1, h2, resRange, color, markerStyle, markerSize, fillColor, errList, options))
def clearSecondaryPlots(self):
self.secondaryPlots = []
def drawCanvas(self):
self.canvas = ROOT.TCanvas("hCanvas%d"%(countNumbersUp()), "", self.canvasSizeX,self.canvasSizeY)
doRatioPad = any((self.hasRatio, self.hasEfficiency, self.hasResidual, self.hasBottomWindow))
if doRatioPad:
self.plotPad = ROOT.TPad("plotPad","plotPad",0,self.ratioPadHeight,1,1)
else:
self.plotPad = ROOT.TPad("plotPad","plotPad",0,0,1,1)
self.plotPad.UseCurrentStyle()
#self.plotPad.Draw()
if doRatioPad:
self.ratioPad = ROOT.TPad("ratioPad","ratioPad",0,0,1,self.ratioPadHeight)
self.ratioPad.UseCurrentStyle()
#self.ratioPad.Draw()
if doRatioPad:
self.plotPad.SetTopMargin (self.marginTop)
self.plotPad.SetLeftMargin (self.marginLeft)
self.plotPad.SetRightMargin (self.marginRight)
#self.plotPad.SetBottomMargin(0.14)
#self.ratioPad.SetTopMargin(0.05)
self.ratioPad.SetBottomMargin(self.marginBottom)
self.ratioPad.SetLeftMargin (self.marginLeft)
self.ratioPad.SetRightMargin (self.marginRight)
else:
self.plotPad.SetTopMargin (self.marginTop)
self.plotPad.SetLeftMargin (self.marginLeft)
self.plotPad.SetRightMargin (self.marginRight)
self.plotPad.SetBottomMargin(self.marginBottom)
self.plotPad.Draw()
if doRatioPad:
self.ratioPad.Draw()
self.plotPad.cd()
if self.logX:
self.plotPad.SetLogx()
if doRatioPad:
self.ratioPad.SetLogx()
if self.logY:
self.plotPad.SetLogy()
if self.logZ:
self.plotPad.SetLogz()
def drawLatexLabels(self):
for name, label in self.labels.iteritems():
label.draw()
def clean(self):
for key in self.labels.keys():
self.removeLabel(key)
self.__init__()
#self.addLabel(CMSLabel ())
#self.addLabel(CMSExtraLabel ())
#self.addLabel(RegionLabel ())
#self.addLabel(CutsLabel ())
#self.addLabel(LumiLabel ())
#self.addLabel(DileptonLabel ())
#self.primaryPlot = None
#self.legend = None
#self.secondaryPlots = []
#self.ratioGraphs = []
#self.residualGraphs = []
#self.residualPlots = []
#self.residualZeroLine= None
#self.ratioPairs = []
#self.pathName = "fig/"
#self.folderName = ""
#self.canvas = None
#self.plotPad = None
#self.ratioPad = None
#self.hAxis = None
#self.axisField = None
def drawRatioPlots(self):
if self.hasRatio or self.hasEfficiency or self.hasResidual:
self.ratioPad.cd()
if self.hasRatio:
self.ratioGraphs = []
self.plotPad.Update() # So that Uxmin and Uxmax are retrievable
for nominator, denominator, color, markerstyle in self.ratioPairs:
self.ratioGraphs.append(ratios.RatioGraph(nominator, denominator, xMin=self.plotPad.GetUxmin(), xMax=self.plotPad.GetUxmax(),title=self.ratioLabel,yMin=self.ratioMin,yMax=self.ratioMax,ndivisions=10,color=color, adaptiveBinning=1000 ))
for err in self.ratioErrsSize:
self.ratioGraphs[err[5]].addErrorBySize(err[0],err[1],err[2],err[3],err[4])
for err in self.ratioErrsHist:
self.ratioGraphs[err[5]].addErrorByHistograms(err[0],err[1],err[2],err[3],err[4])
for number,graph in enumerate(self.ratioGraphs):
if number == 0:
graph.draw(ROOT.gPad,True,False,True,chi2Pos=0.8)
if self.nDivX == None:
graph.hAxis.GetXaxis().SetNdivisions(self.primaryPlot[0].GetXaxis().GetNdivisions())
else:
graph.hAxis.GetXaxis().SetNdivisions(self.nDivX)
graph.hAxis.GetYaxis().SetNdivisions(408)
if self.ratioLabelSize !=None:
graph.hAxis.GetYaxis().SetLabelSize(self.ratioLabelSize)
if self.ratioTitleSize !=None:
graph.hAxis.GetYaxis().SetTitleSize(self.ratioTitleSize)
if self.ratioTitleOffset !=None:
graph.hAxis.GetYaxis().SetTitleOffset(self.ratioTitleOffset)
graph.hAxis.GetYaxis().CenterTitle()
else:
graph.draw(self.ratioPad,False,False,True,chi2Pos=0.8)
graph.graph.SetMarkerStyle(markerstyle)
elif self.hasEfficiency:
self.ratioGraphs = []
self.plotPad.Update() # So that Uxmin and Uxmax are retrievable
self.hAxis = ROOT.TH2F("hAxis%d"%(countNumbersUp()), "", 20, self.plotPad.GetUxmin(), self.plotPad.GetUxmax(), 10, self.efficiencyMin, self.efficiencyMax)
self.hAxis.GetYaxis().SetNdivisions(408)
self.hAxis.GetYaxis().SetTitleOffset(0.4)
self.hAxis.GetYaxis().SetTitleSize(0.15)
self.hAxis.GetXaxis().SetLabelSize(0.0)
self.hAxis.GetYaxis().SetLabelSize(0.15)
if self.nDivX == None:
self.hAxis.GetXaxis().SetNdivisions(self.primaryPlot[0].GetXaxis().GetNdivisions())
else:
self.hAxis.GetXaxis().SetNdivisions(self.nDivX)
self.hAxis.GetYaxis().SetTitle(self.efficiencyLabel)
self.hAxis.Draw("AXIS")
for nominator, denominator, color, markerstyle in self.ratioPairs:
tmp = ROOT.TGraphAsymmErrors(nominator,denominator, self.efficiencyOption)
tmp.SetMarkerColor(color)
tmp.SetLineColor(color)
tmp.SetMarkerStyle(markerstyle)
self.ratioGraphs.append(tmp)
self.ratioGraphs[len(self.ratioGraphs)-1].Draw("same P")
elif self.hasResidual:
low = self.residualRangeLow if (self.residualRangeLow != None) else -self.residualRangeUp
up = self.residualRangeUp
minRes = 100000000000.0
maxRes = -100000000000.0
self.plotPad.Update() # So that Uxmin and Uxmax are retrievable
self.hAxis = ROOT.TH2F("hAxis%d"%(countNumbersUp()), "", 20, self.plotPad.GetUxmin(), self.plotPad.GetUxmax(), 10, low, up)
self.hAxis.GetYaxis().SetNdivisions(408)
self.hAxis.GetYaxis().SetTitleOffset(0.4)
self.hAxis.GetYaxis().SetTitleSize(0.15)
self.hAxis.GetXaxis().SetLabelSize(0.0)
self.hAxis.GetYaxis().SetLabelSize(0.15)
if self.nDivX == None:
self.hAxis.GetXaxis().SetNdivisions(self.primaryPlot[0].GetXaxis().GetNdivisions())
else:
self.hAxis.GetXaxis().SetNdivisions(self.nDivX)
self.hAxis.GetYaxis().SetTitle(self.residualLabel)
self.hAxis.Draw("AXIS")
if self.residualZeroLineDo:
self.residualZeroLine = ROOT.TLine(self.plotPad.GetUxmin(),0,self.plotPad.GetUxmax(),0)
self.residualZeroLine.SetLineColor(self.residualZeroLineColor)
self.residualZeroLine.SetLineWidth(self.residualZeroLineWidth)
self.residualZeroLine.SetLineStyle(self.residualZeroLineStyle)
self.residualZeroLine.Draw("same")
self.residualGraphs = []
for h1, h2, resRange, color, markerstyle, markersize, fillcolor, errList, options in self.residualPlots:
tmp = ROOT.TGraphAsymmErrors()
tmp.SetMarkerColor(color)
tmp.SetMarkerSize(markersize)
tmp.SetLineColor(color)
tmp.SetFillColor(fillcolor)
tmp.SetMarkerStyle(markerstyle)
self.residualGraphs.append(tmp)
xpos = []
minu = []
subt = []
minu_err = []
subt_err = []
if h1.InheritsFrom(ROOT.TH1.Class()):
slope = 0
for i in range(1, h1.GetNbinsX()+1):
xPos = h1.GetBinCenter(i)
if resRange != None:
if xPos > resRange[1] or xPos < resRange[0]:
#tmp.SetPoint(i, xPos, 0)
#tmp.SetPointError(i, 0, 0, 0, 0)
continue
minuent = h1.GetBinContent(i)
minuent_err = (h1.GetBinErrorLow(i), h1.GetBinErrorUp(i))
if h2.InheritsFrom(ROOT.TH1.Class()):
subtrahent = h2.GetBinContent(h2.FindBin(xPos))
subtrahent_err = (h2.GetBinErrorLow(h2.FindBin(xPos)), h2.GetBinErrorUp(h2.FindBin(xPos)))
elif h2.InheritsFrom(ROOT.TF1.Class()):
subtrahent = h2.Eval(xPos)
slope = h2.Derivative(xPos)
subtrahent_err = (0,0)
else:
print "Error in drawResiduals: Invalid type of subtrahent", h1
exit()
xpos.append(xPos)
minu.append(minuent)
subt.append(subtrahent)
minu_err.append(minuent_err)
subt_err.append(subtrahent_err)
elif h1.InheritsFrom(ROOT.TGraphErrors.Class()):
for i in range(h1.GetN()):
xPos = h1.GetX()[i]
if resRange != None:
if xPos > resRange[1] or xPos < resRange[0]:
#tmp.SetPoint(i, xPos, 0)
#tmp.SetPointError(i, 0, 0, 0, 0)
continue
minuent = h1.GetY()[i]
if h1.InheritsFrom(ROOT.TGraphAsymmErrors.Class()):
minuent_err = (h1.GetEYlow()[i], h1.GetEYhigh()[i])
else:
minuent_err = (h1.GetEY()[i], h1.GetEY()[i])
if h2.InheritsFrom(ROOT.TH1.Class()):
subtrahent = h2.GetBinContent(h2.FindBin(xPos))
subtrahent_err = h2.GetBinError(h2.FindBin(xPos))
elif h2.InheritsFrom(ROOT.TGraphErrors.Class()):
subtrahent = h2.GetY()[i]
if h2.InheritsFrom(ROOT.TGraphAsymmErrors.Class()):
subtrahent_err = (h2.GetEYlow()[i], h2.GetEYhigh()[i])
else:
subtrahent_err = (h2.GetEY()[i], h2.GetEY()[i])
elif h2.InheritsFrom(ROOT.TF1.Class()):
subtrahent = h2.Eval(xPos)
subtrahent_err = (0,0)
xpos.append(xPos)
minu.append(minuent)
subt.append(subtrahent)
minu_err.append(minuent_err)
subt_err.append(subtrahent_err)
else:
print "plotTemplate Error: in drawResiduals: Invalid type of minuent ", h2
exit()
for i, (xPos, minuent, subtrahent, minuent_err, subtrahent_err) in enumerate(zip(xpos, minu, subt, minu_err, subt_err)):
residual = minuent - subtrahent
if residual >= 0:
i_m = 0
i_s = 1
else:
i_m = 1
i_s = 0
residual_err = (minuent_err[i_m]**2 + subtrahent_err[i_s]**2 )**0.5
if errList != None:
residual_err = errList[i]
if "P" in options:
if residual_err > 0:
residual /= residual_err
residual_err = 1
else:
residual = 0
residual_err = 0
if "-" in options:
residual = -residual
if residual < minRes:
minRes = residual
if residual > maxRes:
maxRes = residual
tmp.SetPoint(i, xPos, residual)
tmp.SetPointError(i, 0, 0, residual_err, residual_err)
if "H" in options:
tmp.Draw("same BX")
else:
tmp.Draw("same P")
if self.residualRangeMode.upper() == "AUTO":
boundaries = max(abs(minRes), maxRes)
boundLow = -boundaries * self.residualRangeScale
boundUp = boundaries * self.residualRangeScale
self.hAxis.GetYaxis().SetLimits(boundLow, boundUp)
elif self.residualRangeMode.upper() == "AUTOASYMM":
boundLow = minRes * self.residualRangeScale
boundUp = maxRes * self.residualRangeScale
self.hAxis.GetYaxis().SetLimits(boundLow, boundUp)
def draw(self):
self.drawCanvas()
self.primaryPlot[0].Draw(self.primaryPlot[1]) # just so a THStack would have defined axes (which is not the case before drawing), will be overdrawn by axisField
#self.is2D = self.primaryPlot[0].InheritsFrom(ROOT.TH2.Class())
if self.minimumY == None:
if self.is2D:
minimumY = (self.primaryPlot[0].GetYaxis().GetBinLowEdge(1))
else:
minimumY = self.primaryPlot[0].GetMinimum()*self.minimumScale
else:
minimumY = (self.minimumY)
if self.maximumY == None:
if self.is2D:
maximumY = (self.primaryPlot[0].GetYaxis().GetBinUpEdge(self.primaryPlot[0].GetYaxis().GetNbins()))
else:
maximumY = self.primaryPlot[0].GetMaximum()*self.maximumScale
else:
maximumY = (self.maximumY)
if self.minimumX == None:
minimumX = (self.primaryPlot[0].GetXaxis().GetBinLowEdge(1))
else:
minimumX = (self.minimumX)
if self.maximumX == None:
maximumX = (self.primaryPlot[0].GetXaxis().GetBinUpEdge(self.primaryPlot[0].GetXaxis().GetNbins()))
else:
maximumX = (self.maximumX)
if self.is2D:
if self.minimumZ == None:
minimumZ = (self.primaryPlot[0].GetMinimum()*self.minimumScale)
else:
minimumZ = (self.minimumZ)
if self.maximumZ == None:
maximumZ = (self.primaryPlot[0].GetMaximum()*self.maximumScale)
else:
maximumZ = (self.maximumZ)
self.axisField = ROOT.TH2F("hAxis%d"%(countNumbersUp()), ";x;y;z", self.primaryPlot[0].GetXaxis().GetNbins(), minimumX, maximumX, self.primaryPlot[0].GetYaxis().GetNbins(), minimumY, maximumY)
if self.labeledAxisX:
for i in range(1, self.primaryPlot[0].GetXaxis().GetNbins()+1):
self.axisField.GetXaxis().SetBinLabel(i, self.primaryPlot[0].GetXaxis().GetBinLabel(i))
if self.labeledAxisY:
for i in range(1, self.primaryPlot[0].GetYaxis().GetNbins()+1):
self.axisField.GetYaxis().SetBinLabel(i, self.primaryPlot[0].GetYaxis().GetBinLabel(i))
if self.labelX != None:
self.axisField.GetXaxis().SetTitle(self.labelX)
else:
self.axisField.GetXaxis().SetTitle(self.primaryPlot[0].GetXaxis().GetTitle())
if self.labelY != None:
self.axisField.GetYaxis().SetTitle(self.labelY)
else:
self.axisField.GetYaxis().SetTitle(self.primaryPlot[0].GetYaxis().GetTitle())
if self.is2D:
if self.labelZ != None:
self.axisField.GetZaxis().SetTitle(self.labelZ)
self.primaryPlot[0].GetZaxis().SetTitle(self.labelZ) # needed because axisField does not have a z-axis
else:
self.axisField.GetZaxis().SetTitle(self.primaryPlot[0].GetZaxis().GetTitle())
if self.nDivX == None:
self.axisField.GetXaxis().SetNdivisions(self.primaryPlot[0].GetXaxis().GetNdivisions())
else:
self.axisField.GetXaxis().SetNdivisions(self.nDivX)
if self.nDivY == None:
self.axisField.GetYaxis().SetNdivisions(self.primaryPlot[0].GetYaxis().GetNdivisions())
else:
self.axisField.GetYaxis().SetNdivisions(self.nDivY)
self.axisField.Draw("AXIS")
self.primaryPlot[0].Draw(self.primaryPlot[1]+"same")
for plot, drawStyle, label, legOption in self.secondaryPlots:
plot.Draw(drawStyle+"same")
if self.redrawPrimary:
self.primaryPlot[0].Draw(self.primaryPlot[1]+"same")
if self.is2D:
self.axisField.GetZaxis().SetRangeUser(minimumZ,maximumZ) # This line is only needed for older ROOT versions
self.primaryPlot[0].GetZaxis().SetRangeUser(minimumZ,maximumZ)
else:
self.primaryPlot[0].GetYaxis().SetRangeUser(minimumY,maximumY)
self.canvas.Update()
self.drawLatexLabels()
self.drawLegend()
self.plotPad.RedrawAxis()
self.drawRatioPlots()
def drawLegend(self):
if self.hasLegend:
if self.legend == None:
self.legend = ROOT.TLegend(self.legendPosX1, self.legendPosY1, self.legendPosX2, self.legendPosY2)
self.legend.SetTextSize(self.legendTextSize)
self.legend.SetTextFont(self.legendFont)
self.legend.SetTextColor(self.legendTextColor)
self.legend.SetBorderSize(self.legendBorderSize)
self.legend.SetNColumns(self.legendColumns)
self.legend.SetFillStyle(self.legendFillStyle)
self.legend.SetFillColor(self.legendFillColor)