ATLAS Offline Software
Loading...
Searching...
No Matches
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
6from ROOT import *
7
8from PlotHelpers import *
9
10from inspect import getmembers, isfunction
11
12print "Importing my new stuff"
13
14import CorrelationMatrixHelpers
15#from CorrelationMatrixHelpers import *
16
17# Controls
18print "Setting controls"
19#isSquaredMetric = False
20granularityFactor = 1
21SetAxisRange.forceOneSided = True
22#DrawLabels.ATLASLabelName = "Preliminary"
23drawATLASLabel = True
24SetAxisRange.invertAxisColour = False
25DetermineStatValues.minInsteadOfMax = False
26
27# Check arguments
28if 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
40print "Getting arguments"
41
42# Store arguments and open inputs for reading
43jetDef = sys.argv[1]
44plotFileName = sys.argv[2]
45if 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)
49corrUncFileName = sys.argv[3]
50corrUncFile = None if not corrUncFileName.endswith(".root") else TFile.Open(corrUncFileName,"READ")
51
52# Relative or absolute differences
53print "Setting up matrices"
54useRelativeMetric = TString(sys.argv[4]).EqualTo("true",TString.kIgnoreCase)
55SetAxisRange.relativeAxis = useRelativeMetric
56
57print "Getting file names"
58inFileNames = sys.argv[5:]
59inFiles = [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
62print "Checking and retrieving fixed pT/eta scan values from input files"
63fixedPtX,fixedPtY,fixedEtaX,fixedEtaY = CorrelationMatrixHelpers.getFixedValuesFromFiles(inFiles,jetDef)
64
65print fixedPtX,fixedPtY, fixedEtaX, fixedEtaY
66
67
68# Set the style settings for the histograms
70
71# Build the raw difference matrices per file
72print "Building raw 4D difference histograms by file"
73rawDiffPt = CorrelationMatrixHelpers.buildAndFillHists4DFromFiles(inFiles,jetDef,"eta","pt",fixedPtX,fixedPtY,"diff_",granularityFactor,relativeMetric=useRelativeMetric) if len(fixedPtX)>0 and len(fixedPtY)>0 else None
74rawDiffEta = CorrelationMatrixHelpers.buildAndFillHists4DFromFiles(inFiles,jetDef,"pt","eta",fixedEtaX,fixedEtaY,"diff_",granularityFactor,relativeMetric=useRelativeMetric) if len(fixedEtaX)>0 and len(fixedEtaY)>0 else None
75
76print "1) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
77
78# Build the nominal histograms if multiple files for use below
79nominalPt = 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
80nominalEta = 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
81if 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]
87if 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
94print "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
103print "Building 4D correlation uncertainty histograms"
104alternatesPt = CorrelationMatrixHelpers.buildAndFillHists4DFromFile(corrUncFile,jetDef,"eta","pt",fixedPtX,fixedPtY,"diff_",granularityFactor,relativeMetric=useRelativeMetric) if corrUncFile and len(fixedPtX)>0 and len(fixedPtY)>0 else None
105alternatesEta = 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
107alternatesUncPt = CorrelationMatrixHelpers.buildAndFillHists4DFromEnvelopeOfSet(alternatesPt,relativeMetric=useRelativeMetric) if alternatesPt else None
108alternatesUncEta = CorrelationMatrixHelpers.buildAndFillHists4DFromEnvelopeOfSet(alternatesEta,relativeMetric=useRelativeMetric) if alternatesEta else None
109
110
111print "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
116print "Building 4D minimum difference histograms (with respect to nominal)"
117minDiffPt = CorrelationMatrixHelpers.buildAndFillHist4DFromMinOfSet(rawDiffPt,relativeMetric=useRelativeMetric) if rawDiffPt and len(rawDiffPt)>1 else None
118minDiffEta = 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
122print "Building 4D file-by-file difference histograms"
123diffsPt = CorrelationMatrixHelpers.buildAndFillHists4DFromDifferenceHists(rawDiffPt,relativeMetric=useRelativeMetric) if rawDiffPt and len(rawDiffPt)>1 else None
124diffsEta = 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
127print "Building 4D maximum difference histograms (with respect to each other)"
128maxDiffPt = CorrelationMatrixHelpers.buildAndFillHist4DFromMaxOfSet(diffsPt,relativeMetric=useRelativeMetric) if diffsPt and len(diffsPt)>1 else None
129maxDiffEta = 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
132if maxDiffPt: maxDiffPt.applyAbsValue()
133if maxDiffEta: maxDiffEta.applyAbsValue()
134
135
136print "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)
141print "Building 4D coverage histograms"
142coveragePt = CorrelationMatrixHelpers.buildAndFillHist4DFromCoverageOfSet(minDiffPt,maxDiffPt,1,relativeMetric=useRelativeMetric) if rawDiffPt and len(rawDiffPt)>1 else None
143coverageEta = 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
146if coveragePt: coveragePt.plotType = "Metric2"
147if coverageEta: coverageEta.plotType = "Metric2"
148
149
150print "5) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
151
152saveFixedEtaY = fixedEtaY[:]
153saveFixedEtaX = fixedEtaX[:]
154saveFixedPtX = fixedPtX[:]
155saveFixedPtY = fixedPtY[:]
156
157# Build coverage plots as above, but including correlation uncertainties (Metric3 and variant)
158print "Building 4D coverage histograms including correlation uncertainties"
159coverageWithUncPt = CorrelationMatrixHelpers.buildAndFillHist4DFromCoverageOfSet(coveragePt,alternatesUncPt,1,relativeMetric=useRelativeMetric) if coveragePt and alternatesUncPt else None
160#print "a)",fixedEtaX
161coverageWithUncEta = 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
165if coverageWithUncPt: coverageWithUncPt.plotType = "Metric3"
166#print "c)",fixedEtaX
167if coverageWithUncEta: coverageWithUncEta.plotType = "Metric3"
168
169fixedEtaX = saveFixedEtaX
170fixedEtaY = saveFixedEtaY
171fixedPtX = saveFixedPtX
172fixedPtY = saveFixedPtY
173
174print "6) still defined as:",fixedPtX,fixedPtY,fixedEtaX,fixedEtaY
175
176
177# Prepare the canvas
178print "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))
179canvas = TCanvas("canvas","4D Correlation Matrix",0,0,2600,2200)
180canvas.SetLeftMargin(0.09 if not useRelativeMetric else 0.08)
181canvas.SetRightMargin(0.19 if not useRelativeMetric else 0.17)
182canvas.SetBottomMargin(0.04)
183canvas.SetTopMargin(0.12)
184canvas.SetLogz(useRelativeMetric)
185canvas.cd()
186canvas.Draw()
187
188# Write the plots
189if 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
203print "here, it is",fixedEtaX
204
205CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,rawDiffEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",scenarioLabel="",drawATLASLabel=drawATLASLabel,additionalString =4)
206CorrelationMatrixHelpers.saveHists4D(canvas,plotFileName,minDiffEta,not useRelativeMetric,fixedEtaX,fixedEtaY,"#eta=%.1f",scenarioLabel="",drawATLASLabel=drawATLASLabel,additionalString =5)
207CorrelationMatrixHelpers.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)
209CorrelationMatrixHelpers.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
212if not (plotFileName.endswith(".eps") or plotFileName.endswith(".png")):
213 canvas.Print(plotFileName+"]")
214
215
buildAndFillHists4DFromEnvelopeOfSet(hists, relativeMetric=False)
buildAndFillHists4DFromDifferenceHists(hists, relativeMetric=False)
buildAndFillHist4DFromMinOfSet(hists, relativeMetric=False)
buildAndFillHists4DFromFile(inFile, jetDef, varString, fixedString, fixedX, fixedY, excludeStartString="", granularityFactor=1, relativeMetric=False)
buildAndFillHist4DFromMaxOfSet(hists, relativeMetric=False)
saveHists4D(canvas, plotFileName, hist, oneSidedAxis, fixedX, fixedY, fixedStr, scenarioLabel="", drawATLASLabel=True, additionalString="")
buildAndFillHist4DFromCoverageOfSet(minDiffFromNominal, maxDiffBetweenScenarios, plotStyle, nominalHist=None, relativeMetric=False)
buildAndFillHists4DFromFiles(inFiles, jetDef, varString, fixedString, fixedX, fixedY, filterStartString="", granularityFactor=1, relativeMetric=False)
set_style(style="Plain")