ATLAS Offline Software
Make4DCorrelationMatrix.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 # Script by Steven Schramm, originally based off of 4D histogram code by Sophie Pataraia
4 # somewhat altered by Kate, Dec 2016
5 
6 from ROOT import *
7 
8 from PlotHelpers import *
9 
10 from inspect import getmembers, isfunction
11 
12 print "Importing my new stuff"
13 
14 import CorrelationMatrixHelpers
15 #from CorrelationMatrixHelpers import *
16 
17 # Controls
18 print "Setting controls"
19 #isSquaredMetric = False
20 granularityFactor = 1
21 SetAxisRange.forceOneSided = True
22 #DrawLabels.ATLASLabelName = "Preliminary"
23 drawATLASLabel = True
24 SetAxisRange.invertAxisColour = False
25 DetermineStatValues.minInsteadOfMax = False
26 
27 # Check arguments
28 if len(sys.argv) < 6:
29  print "Too few arguments. Expected the following:"
30  print " 1. Jet definition to study, such as AntiKt4LCTopo"
31  print " 2. Output plot file"
32  print " 3. Correlation uncertainties file, \"None\" is allowed"
33  print " 4. Whether to use relative differences instead of absolute, False is default"
34  print " 5. Input ROOT file in proper format"
35  print " Typically should be the output file from util/MakeCorrelationMatrixPlots.cxx"
36  print " 6+ Additional input ROOT files in proper format (optional)"
37  print " If additional files are specified, minimum correlation differences are plotted"
38  sys.exit(1)
39 
40 print "Getting arguments"
41 
42 # Store arguments and open inputs for reading
43 jetDef = sys.argv[1]
44 plotFileName = sys.argv[2]
45 if plotFileName.endswith(".root"):
46  print "Output plot file appears to be a root file, blocking for safety to not overwrite inputs"
47  print "Check input file:",plotFileName
48  sys.exit(2)
49 corrUncFileName = sys.argv[3]
50 corrUncFile = None if not corrUncFileName.endswith(".root") else TFile.Open(corrUncFileName,"READ")
51 
52 # Relative or absolute differences
53 print "Setting up matrices"
54 useRelativeMetric = TString(sys.argv[4]).EqualTo("true",TString.kIgnoreCase)
55 SetAxisRange.relativeAxis = useRelativeMetric
56 
57 print "Getting file names"
58 inFileNames = sys.argv[5:]
59 inFiles = [TFile.Open(name,"READ") for name in inFileNames]
60 
61 # Get the fixed values of pt1,pt2 and eta1,eta2 which are available in the files
62 print "Checking and retrieving fixed pT/eta scan values from input files"
63 fixedPtX,fixedPtY,fixedEtaX,fixedEtaY = CorrelationMatrixHelpers.getFixedValuesFromFiles(inFiles,jetDef)
64 
65 print fixedPtX,fixedPtY, fixedEtaX, fixedEtaY
66 
67 
68 # Set the style settings for the histograms
69 set_style()
70 
71 # Build the raw difference matrices per file
72 print "Building raw 4D difference histograms by file"
73 rawDiffPt = CorrelationMatrixHelpers.buildAndFillHists4DFromFiles(inFiles,jetDef,"eta","pt",fixedPtX,fixedPtY,"diff_",granularityFactor,relativeMetric=useRelativeMetric) if len(fixedPtX)>0 and len(fixedPtY)>0 else None
74 rawDiffEta = CorrelationMatrixHelpers.buildAndFillHists4DFromFiles(inFiles,jetDef,"pt","eta",fixedEtaX,fixedEtaY,"diff_",granularityFactor,relativeMetric=useRelativeMetric) if len(fixedEtaX)>0 and len(fixedEtaY)>0 else None
75 
76 print "1) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
77 
78 # Build the nominal histograms if multiple files for use below
79 nominalPt = CorrelationMatrixHelpers.buildAndFillHists4DFromFiles(inFiles[0],jetDef,"eta","pt",fixedPtX,fixedPtY,"%s_0_"%(jetDef),granularityFactor,relativeMetric=useRelativeMetric) if len(fixedPtX)>0 and len(fixedPtY)>0 and len(inFiles)>1 else None
80 nominalEta = CorrelationMatrixHelpers.buildAndFillHists4DFromFiles(inFiles[0],jetDef,"pt","eta",fixedEtaX,fixedEtaY,"%s_0_"%(jetDef),granularityFactor,relativeMetric=useRelativeMetric) if len(fixedEtaX)>0 and len(fixedEtaY)>0 and len(inFiles)>1 else None
81 if nominalPt:
82  if len(nominalPt) > 1:
83  print "Unexpected length of the nominal pT set, expected 1 but got ",len(nominalPt)
84  sys.exit(3)
85  else:
86  nominalPt = nominalPt[0]
87 if nominalEta:
88  if len(nominalEta) > 1:
89  print "Unexpected length of the nominal eta set, expected 1 but got ",len(nominalEta)
90  sys.exit(3)
91  else:
92  nominalEta = nominalEta[0]
93 
94 print "2) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
95 
96 
97 
98 # Get correlation uncertainties if provided
99 # Uncertainty is largest difference from 0 when doing any combination of subtractions
100 # So we need to build the 4D matrices for each scenario
101 # Then subtract them in each possible way
102 # Then fill the uncertainty matrix with the largest resulting value for each point
103 print "Building 4D correlation uncertainty histograms"
104 alternatesPt = CorrelationMatrixHelpers.buildAndFillHists4DFromFile(corrUncFile,jetDef,"eta","pt",fixedPtX,fixedPtY,"diff_",granularityFactor,relativeMetric=useRelativeMetric) if corrUncFile and len(fixedPtX)>0 and len(fixedPtY)>0 else None
105 alternatesEta = CorrelationMatrixHelpers.buildAndFillHists4DFromFile(corrUncFile,jetDef,"pt","eta",fixedEtaX,fixedEtaY,"diff_",granularityFactor,relativeMetric=useRelativeMetric) if corrUncFile and len(fixedEtaX)>0 and len(fixedEtaY)>0 else None
106 # Now determine the maximum value of the envelope for each point
107 alternatesUncPt = CorrelationMatrixHelpers.buildAndFillHists4DFromEnvelopeOfSet(alternatesPt,relativeMetric=useRelativeMetric) if alternatesPt else None
108 alternatesUncEta = CorrelationMatrixHelpers.buildAndFillHists4DFromEnvelopeOfSet(alternatesEta,relativeMetric=useRelativeMetric) if alternatesEta else None
109 
110 
111 print "3) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
112 
113 
114 
115 # Build minimum difference histograms from the set of 4D matrices if more than one
116 print "Building 4D minimum difference histograms (with respect to nominal)"
117 minDiffPt = CorrelationMatrixHelpers.buildAndFillHist4DFromMinOfSet(rawDiffPt,relativeMetric=useRelativeMetric) if rawDiffPt and len(rawDiffPt)>1 else None
118 minDiffEta = CorrelationMatrixHelpers.buildAndFillHist4DFromMinOfSet(rawDiffEta,relativeMetric=useRelativeMetric) if rawDiffEta and len(rawDiffEta)>1 else None
119 
120 
121 # Build per-file difference histograms from the set of 4D matrices if more than one
122 print "Building 4D file-by-file difference histograms"
123 diffsPt = CorrelationMatrixHelpers.buildAndFillHists4DFromDifferenceHists(rawDiffPt,relativeMetric=useRelativeMetric) if rawDiffPt and len(rawDiffPt)>1 else None
124 diffsEta = CorrelationMatrixHelpers.buildAndFillHists4DFromDifferenceHists(rawDiffEta,relativeMetric=useRelativeMetric) if rawDiffEta and len(rawDiffEta)>1 else None
125 
126 # Build maximum difference histogram from the set of difference matrices if existing
127 print "Building 4D maximum difference histograms (with respect to each other)"
128 maxDiffPt = CorrelationMatrixHelpers.buildAndFillHist4DFromMaxOfSet(diffsPt,relativeMetric=useRelativeMetric) if diffsPt and len(diffsPt)>1 else None
129 maxDiffEta = CorrelationMatrixHelpers.buildAndFillHist4DFromMaxOfSet(diffsEta,relativeMetric=useRelativeMetric) if diffsEta and len(diffsEta)>1 else None
130 
131 # Set absolute value for max diff histograms if they exist, as the order was arbitrary
132 if maxDiffPt: maxDiffPt.applyAbsValue()
133 if maxDiffEta: maxDiffEta.applyAbsValue()
134 
135 
136 print "4) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
137 
138 
139 
140 # Build coverage plot of styles 1 (Metric2) and 2 (correlations of uncovered points)
141 print "Building 4D coverage histograms"
142 coveragePt = CorrelationMatrixHelpers.buildAndFillHist4DFromCoverageOfSet(minDiffPt,maxDiffPt,1,relativeMetric=useRelativeMetric) if rawDiffPt and len(rawDiffPt)>1 else None
143 coverageEta = CorrelationMatrixHelpers.buildAndFillHist4DFromCoverageOfSet(minDiffEta,maxDiffEta,1,relativeMetric=useRelativeMetric) if rawDiffEta and len(rawDiffEta)>1 else None
144 #coverageCorrPt = buildAndFillHist4DFromCoverageOfSet(minDiffPt,maxDiffPt,2,nominalPt) if rawDiffPt and len(rawDiffPt)>1 else None
145 #coverageCorrEta = buildAndFillHist4DFromCoverageOfSet(minDiffEta,maxDiffEta,2,nominalEta) if rawDiffEta and len(rawDiffEta)>1 else None
146 if coveragePt: coveragePt.plotType = "Metric2"
147 if coverageEta: coverageEta.plotType = "Metric2"
148 
149 
150 print "5) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
151 
152 saveFixedEtaY = fixedEtaY[:]
153 saveFixedEtaX = fixedEtaX[:]
154 saveFixedPtX = fixedPtX[:]
155 saveFixedPtY = fixedPtY[:]
156 
157 # Build coverage plots as above, but including correlation uncertainties (Metric3 and variant)
158 print "Building 4D coverage histograms including correlation uncertainties"
159 coverageWithUncPt = CorrelationMatrixHelpers.buildAndFillHist4DFromCoverageOfSet(coveragePt,alternatesUncPt,1,relativeMetric=useRelativeMetric) if coveragePt and alternatesUncPt else None
160 #print "a)",fixedEtaX
161 coverageWithUncEta = CorrelationMatrixHelpers.buildAndFillHist4DFromCoverageOfSet(coverageEta,alternatesUncEta,1,relativeMetric=useRelativeMetric) if coverageEta and alternatesUncEta else None
162 #print "b)",fixedEtaX
163 #coverageCorrWithUncPt = buildAndFillHist4DFromCoverageOfSet(coveragePt,alternatesUncPt,2,nominalPt) if coveragePt and alternatesUncPt else None
164 #coverageCorrWithUncEta = buildAndFillHist4DFromCoverageOfSet(coverageEta,alternatesUncEta,2,nominalEta) if coverageEta and alternatesUncEta else None
165 if coverageWithUncPt: coverageWithUncPt.plotType = "Metric3"
166 #print "c)",fixedEtaX
167 if coverageWithUncEta: coverageWithUncEta.plotType = "Metric3"
168 
169 fixedEtaX = saveFixedEtaX
170 fixedEtaY = saveFixedEtaY
171 fixedPtX = saveFixedPtX
172 fixedPtY = saveFixedPtY
173 
174 print "6) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
175 
176 
177 # Prepare the canvas
178 print "Writing results to file: ",(re.sub(".eps","-*.eps",plotFileName) if plotFileName.endswith(".eps") else (re.sub(".png","-*.png",plotFileName) if plotFileName.endswith(".png") else plotFileName))
179 canvas = TCanvas("canvas","4D Correlation Matrix",0,0,2600,2200)
180 canvas.SetLeftMargin(0.09 if not useRelativeMetric else 0.08)
181 canvas.SetRightMargin(0.19 if not useRelativeMetric else 0.17)
182 canvas.SetBottomMargin(0.04)
183 canvas.SetTopMargin(0.12)
184 canvas.SetLogz(useRelativeMetric)
185 canvas.cd()
186 canvas.Draw()
187 
188 # Write the plots
189 if not (plotFileName.endswith(".eps") or plotFileName.endswith(".png")):
190  canvas.Print(plotFileName+"[")
191 
192 #rawDiffPt.iEPS = 0
193 #CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,rawDiffPt, not useRelativeMetric,fixedPtX,fixedPtY,"#it{p}_{T}=%.f",scenarioLabel="",drawATLASLabel=drawATLASLabel)
194 #minDiffPt.iEPS = 1
195 #CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,minDiffPt, not useRelativeMetric,fixedPtX,fixedPtY,"#it{p}_{T}=%.f",scenarioLabel="",drawATLASLabel=drawATLASLabel)
196 #coveragePt.iEPS = 2
197 #CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,coveragePt, not useRelativeMetric,fixedPtX,fixedPtY,"#it{p}_{T}=%.f",scenarioLabel="",drawATLASLabel=drawATLASLabel)
198 
202 
203 print "here, it is",fixedEtaX
204 
205 CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,rawDiffEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",scenarioLabel="",drawATLASLabel=drawATLASLabel,additionalString =4)
206 CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,minDiffEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",scenarioLabel="",drawATLASLabel=drawATLASLabel,additionalString =5)
207 CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,coverageEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",scenarioLabel="",drawATLASLabel=drawATLASLabel,additionalString=6)
208 #saveHists4D(canvas,plotFileName,coverageCorrEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",drawATLASLabel)
209 CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,coverageWithUncEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",scenarioLabel="",drawATLASLabel=drawATLASLabel,additionalString=7)
210 #saveHists4D(canvas,plotFileName,coverageCorrWithUncEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",drawATLASLabel)
211 
212 if not (plotFileName.endswith(".eps") or plotFileName.endswith(".png")):
213  canvas.Print(plotFileName+"]")
214 
215 
CorrelationMatrixHelpers.buildAndFillHists4DFromDifferenceHists
def buildAndFillHists4DFromDifferenceHists(hists, relativeMetric=False)
Definition: CorrelationMatrixHelpers.py:732
PlotHelpers.set_style
def set_style(style="Plain")
Definition: PlotHelpers.py:81
CorrelationMatrixHelpers.buildAndFillHist4DFromMinOfSet
def buildAndFillHist4DFromMinOfSet(hists, relativeMetric=False)
Definition: CorrelationMatrixHelpers.py:745
CorrelationMatrixHelpers.saveHists4D
def saveHists4D(canvas, plotFileName, hist, oneSidedAxis, fixedX, fixedY, fixedStr, scenarioLabel="", drawATLASLabel=True, additionalString="")
Definition: CorrelationMatrixHelpers.py:499
CorrelationMatrixHelpers.buildAndFillHist4DFromMaxOfSet
def buildAndFillHist4DFromMaxOfSet(hists, relativeMetric=False)
Definition: CorrelationMatrixHelpers.py:757
CorrelationMatrixHelpers.getFixedValuesFromFiles
def getFixedValuesFromFiles(inFiles, jetDef)
Definition: CorrelationMatrixHelpers.py:602
CorrelationMatrixHelpers.buildAndFillHists4DFromEnvelopeOfSet
def buildAndFillHists4DFromEnvelopeOfSet(hists, relativeMetric=False)
Definition: CorrelationMatrixHelpers.py:769
CorrelationMatrixHelpers.buildAndFillHist4DFromCoverageOfSet
def buildAndFillHist4DFromCoverageOfSet(minDiffFromNominal, maxDiffBetweenScenarios, plotStyle, nominalHist=None, relativeMetric=False)
Definition: CorrelationMatrixHelpers.py:781
CorrelationMatrixHelpers.buildAndFillHists4DFromFiles
def buildAndFillHists4DFromFiles(inFiles, jetDef, varString, fixedString, fixedX, fixedY, filterStartString="", granularityFactor=1, relativeMetric=False)
Definition: CorrelationMatrixHelpers.py:670
CorrelationMatrixHelpers.buildAndFillHists4DFromFile
def buildAndFillHists4DFromFile(inFile, jetDef, varString, fixedString, fixedX, fixedY, excludeStartString="", granularityFactor=1, relativeMetric=False)
Definition: CorrelationMatrixHelpers.py:696