ATLAS Offline Software
Loading...
Searching...
No Matches
CorrelationMatrixHelpers.CorrMat4D Class Reference
Collaboration diagram for CorrelationMatrixHelpers.CorrMat4D:

Public Member Functions

 __init__ (self, relMet=False)
 setInfo (self, jetDef, varType, plotType)
 setInfoCopy (self, toCopy, plotType="")
 cloneHist4D (self, toClone)
 applyAbsValue (self)
 fillHist4DFromFile (self, inFile, fixedString, fixedX, fixedY, filterStartString="", granularityFactor=1)
 4D histogram building from 2D histograms #
 fillHist4DFromDifference (self, hist1, hist2)
 4D histograms from differences of 4D hist sets #
 fillHist4DFromMinOfSet (self, hists)
 4D histogram from minimum value of a 4D set #
 fillHist4DFromMaxOfSet (self, hists)
 4D histogram from maximum value of a 4D set #
 fillHist4DFromEnvelopeOfSet (self, hists)
 4D histogram from envelope of a 4D set #
 fillHist4DFromCoverageOfSet (self, minDiffFromNominal, maxDiffBetweenScenarios, plotStyle, nominalHist=None)
 4D histogram from coverage of a 4D set #

Public Attributes

 hist4D = None
 jetDef = None
 varType = None
str plotType = None
int OOB = -1234
int OOBT = -1000
 relativeMetric = relMet
str ATLASLabelName = "Internal"
bool DrawATLASLabel = True
int iEPS = 0
 histName = None

Detailed Description

                                           #

4D histogram class # #

Definition at line 22 of file CorrelationMatrixHelpers.py.

Constructor & Destructor Documentation

◆ __init__()

CorrelationMatrixHelpers.CorrMat4D.__init__ ( self,
relMet = False )

Definition at line 24 of file CorrelationMatrixHelpers.py.

24 def __init__(self,relMet=False):
25 self.hist4D = None
26 self.jetDef = None
27 self.varType = None
28 self.plotType = None
29 self.OOB = -1234 # out of bounds value
30 self.OOBT = -1000 # out of bounds threshold
31 # Whether to use (nominal-reduced) or (1-nominal)/(1-reduced)
32 self.relativeMetric = relMet
33
34 # For the plotting functions
35 self.ATLASLabelName = "Internal"
36 self.DrawATLASLabel = True
37 self.iEPS = 0
38
39

Member Function Documentation

◆ applyAbsValue()

CorrelationMatrixHelpers.CorrMat4D.applyAbsValue ( self)

Definition at line 57 of file CorrelationMatrixHelpers.py.

57 def applyAbsValue(self):
58 if not self.hist4D:
59 print "Cannot apply absolute value when the histogram hasn't been created"
60 return False
61
62 for binX in range(1,self.hist4D.GetNbinsX()+1):
63 for binY in range(1,self.hist4D.GetNbinsY()+1):
64 if self.hist4D.GetBinContent(binX,binY) > self.OOBT:
65 self.hist4D.SetBinContent(binX,binY,fabs(self.hist4D.GetBinContent(binX,binY)))
66 return True
67

◆ cloneHist4D()

CorrelationMatrixHelpers.CorrMat4D.cloneHist4D ( self,
toClone )

Definition at line 50 of file CorrelationMatrixHelpers.py.

50 def cloneHist4D(self,toClone):
51 try:
52 self.hist4D = toClone.Clone()
53 except AttributeError:
54 self.hist4D = toClone.hist4D.Clone()
55 self.hist4D.SetName("4D_var%s_%s_%s"%(self.varType,self.jetDef,self.plotType))
56

◆ fillHist4DFromCoverageOfSet()

CorrelationMatrixHelpers.CorrMat4D.fillHist4DFromCoverageOfSet ( self,
minDiffFromNominal,
maxDiffBetweenScenarios,
plotStyle,
nominalHist = None )

4D histogram from coverage of a 4D set #

Definition at line 340 of file CorrelationMatrixHelpers.py.

340 def fillHist4DFromCoverageOfSet(self,minDiffFromNominal,maxDiffBetweenScenarios,plotStyle,nominalHist=None):
341 # Ensure the histogram wasn't already filled
342 if self.hist4D:
343 print "Blocking re-filling of existing CorrMat4D histogram of name ",self.hist4D.GetName()
344 return False
345
346 # Ensure the histogram(s) exist
347 if not minDiffFromNominal or not maxDiffBetweenScenarios:
348 print "Argument is None or empty list: ",minDiffFromNominal,maxDiffBetweenScenarios
349 return False
350 if plotStyle==2 and not nominalHist:
351 print "NominalHist is None for style which requires it"
352 return False
353
354 # Copy the minDiff histogram for formatting purposes
355 self.cloneHist4D(minDiffFromNominal)
356
357 # Now fill with the value controlled by plotStyle
358 # 0: 0 if max(scenarioDiff) > min(nominalDiff) else min(nominalDiff)
359 # 1: 0 if max(scenarioDiff) > min(nominalDiff) else min(nominalDiff) - max(scenarioDiff)
360 # 2: 0 if max(scenarioDiff) > min(nominalDiff) else nominalValue
361
362 for binX in range(1,self.hist4D.GetNbinsX()+1):
363 for binY in range(1,self.hist4D.GetNbinsY()+1):
364 if self.hist4D.GetBinContent(binX,binY) > self.OOBT:
365 minDiff = fabs(minDiffFromNominal.hist4D.GetBinContent(binX,binY))
366 maxDiff = fabs(maxDiffBetweenScenarios.hist4D.GetBinContent(binX,binY))
367
368 if not self.relativeMetric:
369 if minDiff <= maxDiff:
370 self.hist4D.SetBinContent(binX,binY,0)
371 elif plotStyle == 0:
372 self.hist4D.SetBinContent(binX,binY,minDiffFromNominal.hist4D.GetBinContent(binX,binY))
373 elif plotStyle == 1:
374 self.hist4D.SetBinContent(binX,binY,maxDiff-minDiff)
375 elif plotStyle == 2:
376 self.hist4D.SetBinContent(binX,binY,nominalHist.hist4D.GetBinContent(binX,binY))
377 else:
378 print "Unrecognized plotStyle of ",plotStyle
379 return False
380 else:
381 # The min difference from 1 (nominal vs reduced) should be smaller
382 # than the max difference from 1 (reduced vs reduced) as a relative comparison
383 if (minDiff <= 1 and maxDiff <= 1) and (minDiff >= maxDiff):
384 self.hist4D.SetBinContent(binX,binY,1)
385 elif (minDiff >= 1 and maxDiff >= 1) and (minDiff <= maxDiff):
386 self.hist4D.SetBinContent(binX,binY,1)
387 elif (minDiff >= 1 and maxDiff <= 1) and (minDiff <= 1/maxDiff):
388 self.hist4D.SetBinContent(binX,binY,1)
389 elif (minDiff <= 1 and maxDiff >= 1) and (minDiff >= 1/maxDiff):
390 self.hist4D.SetBinContent(binX,binY,1)
391 else:
392 if plotStyle == 0:
393 self.hist4D.SetBinContent(binX,binY,minDiffFromNominal.hist4D.GetBinContent(binX,binY))
394 elif plotStyle == 1:
395 if (minDiff <= 1 and maxDiff <= 1) or (minDiff >= 1 and maxDiff >= 1):
396 self.hist4D.SetBinContent(binX,binY,minDiff/maxDiff)
397 else:
398 self.hist4D.SetBinContent(binX,binY,minDiff*maxDiff)
399 elif plotStyle == 2:
400 self.hist4D.SetBinContent(binX,binY,nominalHist.hist4D.GetBinContent(binX,binY))
401 else:
402 print "Unrecognized plotStyle of ",plotStyle
403 return False
404
405 return True
406
407

◆ fillHist4DFromDifference()

CorrelationMatrixHelpers.CorrMat4D.fillHist4DFromDifference ( self,
hist1,
hist2 )

4D histograms from differences of 4D hist sets #

Definition at line 175 of file CorrelationMatrixHelpers.py.

175 def fillHist4DFromDifference(self,hist1,hist2):
176 # Ensure the histogram wasn't already filled
177 if self.hist4D:
178 print "Blocking re-filling of existing CorrMat4D histogram of name ",self.hist4D.GetName()
179 return False
180
181 # Ensure the histograms exist
182 if not hist1 or not hist2:
183 print "Argument(s) are None: ",hist1,hist2
184 return False
185
186 # Copy the first histogram and subtract the second
187 self.cloneHist4D(hist1)
188
189 # Manually do the difference so that we can watch for out-of-bounds
190 for binX in range(1,hist1.hist4D.GetNbinsX()+1):
191 for binY in range(1,hist2.hist4D.GetNbinsX()+1):
192 if hist1.hist4D.GetBinContent(binX,binY) > self.OOBT and hist2.hist4D.GetBinContent(binX,binY) > self.OOBT:
193 if not hist1.relativeMetric:
194 self.hist4D.SetBinContent(binX,binY,hist1.hist4D.GetBinContent(binX,binY) - hist2.hist4D.GetBinContent(binX,binY))
195 else:
196 self.hist4D.SetBinContent(binX,binY,relativeMetric(hist1.hist4D.GetBinContent(binX,binY),hist2.hist4D.GetBinContent(binX,binY)))
197
198 return True
199
double relativeMetric(const double numerator, const double denominator)

◆ fillHist4DFromEnvelopeOfSet()

CorrelationMatrixHelpers.CorrMat4D.fillHist4DFromEnvelopeOfSet ( self,
hists )

4D histogram from envelope of a 4D set #

Definition at line 298 of file CorrelationMatrixHelpers.py.

298 def fillHist4DFromEnvelopeOfSet(self,hists):
299 # Ensure the histogram wasn't already filled
300 if self.hist4D:
301 print "Blocking re-filling of existing CorrMat4D histogram of name ",self.hist4D.GetName()
302 return False
303
304 # Ensure the histogram(s) exist
305 if not hists or len(hists) < 2:
306 print "Argument is None or contains less than two histograms: ",hists
307 return False
308
309 # Copy the first histogram for formatting purposes
310 self.cloneHist4D(hists[0])
311
312 # The values of the first histogram were copied from cloning
313 # We need to set these back to 0 (excluding out-of-bounds values)
314 for binX in range(1,self.hist4D.GetNbinsX()+1):
315 for binY in range(1,self.hist4D.GetNbinsY()+1):
316 if self.hist4D.GetBinContent(binX,binY) > self.OOBT:
317 self.hist4D.SetBinContent(binX,binY,0)
318
319 # Now construct the envelope
320 # Fill with the maximum |difference| from zero when subtracting config X from config Y for all combinations of X,Y
321 for iHist1 in range(0,len(hists)):
322 hist1 = hists[iHist1]
323 for iHist2 in range(iHist1+1,len(hists)):
324 hist2 = hists[iHist2]
325 for binX in range(self.hist4D.GetNbinsX()+1):
326 for binY in range(self.hist4D.GetNbinsY()+1):
327 if self.hist4D.GetBinContent(binX,binY) > self.OOBT:
328 if aHist.relativeMetric:
329 diff = relativeMetric(1-hist1.hist4D.GetBinContent(binX,binY),1-hist2.hist4D.GetBinContent(binX,binY))
330 else:
331 diff = fabs(hist2.hist4D.GetBinContent(binX,binY) - hist1.hist4D.GetBinContent(binX,binY))
332 if diff > self.hist4D.GetBinContent(binX,binY):
333 self.hist4D.SetBinContent(binX,binY,diff)
334
335 return True
336

◆ fillHist4DFromFile()

CorrelationMatrixHelpers.CorrMat4D.fillHist4DFromFile ( self,
inFile,
fixedString,
fixedX,
fixedY,
filterStartString = "",
granularityFactor = 1 )

4D histogram building from 2D histograms #

Definition at line 70 of file CorrelationMatrixHelpers.py.

70 def fillHist4DFromFile(self,inFile,fixedString,fixedX,fixedY,filterStartString="",granularityFactor=1):
71 # Ensure the histogram wasn't already filled
72 if self.hist4D:
73 print "Blocking re-filling of existing CorrMat4D histogram of name ",self.hist4D.GetName()
74 return False
75
76 # Begin by checking the size of the 2D matrices
77 numBins = -1
78 for histName in inFile.GetKeyNames():
79 if filterStartString != "" and not histName.startswith(filterStartString):
80 continue
81 if self.jetDef not in histName:
82 continue
83 if "_%s"%(fixedString) in histName:
84 hist = inFile.Get(histName)
85 numBins = hist.GetNbinsX()
86 break
87 if numBins < 0:
88 print "Failed to find histogram matching criteria:"
89 print "jetDef = \"%s\""%(self.jetDef)
90 print "filterStartString = \"%s\""%(filterStartString)
91 return False
92
93 # Granularity scaling if requested
94 localNumBins = int(numBins / granularityFactor) if granularityFactor > 1 else numBins
95 if granularityFactor > 1:
96 # Ensure the granularity fits
97 if numBins % granularityFactor != 0:
98 print "Cannot apply granularity factor: %d bins can't be divided by %d"%(numBins,granularityFactor)
99 return False
100
101 # Now build the empty 4D matrix
102 histName4D = "4D_var%s_%s_%s"%(self.varType,self.jetDef,inFile.GetNameNoDir())
103 self.hist4D = TH2D(histName4D,"",len(fixedX)*localNumBins,0.,len(fixedX)*localNumBins,len(fixedY)*localNumBins,0.,len(fixedY)*localNumBins)
104
105 # Fill with the out-of-bounds value
106 for binX in range(1,self.hist4D.GetNbinsX()+1):
107 for binY in range(1,self.hist4D.GetNbinsY()+1):
108 self.hist4D.SetBinContent(binX,binY,self.OOB)
109
110 # Fill the histogram
111 for histName in inFile.GetKeyNames():
112 if filterStartString != "" and not histName.startswith(filterStartString):
113 continue
114 if self.jetDef not in histName:
115 continue
116
117 # Get the fixed values for this 2D histogram
118 # Sanity checks were already done before, so we just need to retrieve them
119 fixed1,fixed2 = getFixedValuesFromName(histName)
120 if not fixed1.startswith(fixedString): continue
121 fixed1 = float(fixed1.replace(fixedString,"",1))
122 fixed2 = float(fixed2.replace(fixedString,"",1))
123
124 # We have the fixed values, now determine the respective indices
125 index1 = -1
126 index2 = -1
127 for aIndex in range(0,len(fixedX)):
128 aValue = fixedX[aIndex]
129 if fabs(aValue - fixed1) < 1.e-4:
130 index1 = aIndex
131 break
132 for aIndex in range(0,len(fixedY)):
133 aValue = fixedY[aIndex]
134 if fabs(aValue - fixed2) < 1.e-4:
135 index2 = aIndex
136 break
137 if index1 < 0 or index2 < 0:
138 print "Failed to find index1 (%d) or index2 (%d) for histogram (%s)"%(index1,index2,histName)
139 self.histName = None
140 return False
141
142 # We now have the indices
143 # Get the 2D histogram and use it to fill the relevant portion of the 4D histogram
144 # Watch for granularity factors
145 hist2D = inFile.Get(histName)
146 offsetX = int((self.hist4D.GetNbinsX()/len(fixedX))*index1)
147 offsetY = int((self.hist4D.GetNbinsY()/len(fixedY))*index2)
148 if localNumBins == numBins:
149 for binX in range(1,hist2D.GetNbinsX()+1):
150 for binY in range(1,hist2D.GetNbinsY()+1):
151 self.hist4D.SetBinContent(binX+offsetX,binY+offsetY,hist2D.GetBinContent(binX,binY))
152 else:
153 # Split into pieces by the granularity factor
154 # Watch for the root indexing from 1, which has to be in the sub-bin indices
155 # Also have to be careful about bins which are on the edge of the kinematic limit
156 for binX in range(0,hist2D.GetNbinsX()/granularityFactor):
157 for binY in range(0,hist2D.GetNbinsY()/granularityFactor):
158 subVal = 0
159 numVal = 0
160 for subBinX in range(1,granularityFactor+1):
161 for subBinY in range(1,granularityFactor+1):
162 binContent = hist2D.GetBinContent(binX*granularityFactor+subBinX,binY*granularityFactor+subBinY)
163 if binContent > self.OOBT:
164 subVal += binContent
165 numVal += 1
166 self.hist4D.SetBinContent(binX+1+offsetX,binY+1+offsetY,subVal/numVal if numVal > 0 else self.OOB)
167
168 # Done with this 2D histogram
169 # Done with this 4D histogram
170 return True
171

◆ fillHist4DFromMaxOfSet()

CorrelationMatrixHelpers.CorrMat4D.fillHist4DFromMaxOfSet ( self,
hists )

4D histogram from maximum value of a 4D set #

Definition at line 253 of file CorrelationMatrixHelpers.py.

253 def fillHist4DFromMaxOfSet(self,hists):
254 # Ensure the histogram wasn't already filled
255 if self.hist4D:
256 print "Blocking re-filling of existing CorrMat4D histogram of name ",self.hist4D.GetName()
257 return False
258
259 # Ensure the histogram(s) exist
260 if not hists or len(hists) == 0:
261 print "Argument is None or empty list: ",hists
262 return False
263
264 # Copy the first histogram for formatting purposes
265 self.cloneHist4D(hists[0])
266
267 # The values from the first histogram are already set from cloning
268 # Now just check if any other hists have smaller values
269 # Note that this may be values with respect to 1 if relative metric
270 # Watch for out-of-bounds
271 for aHist in hists[1:]:
272 for binX in range(1,aHist.hist4D.GetNbinsX()+1):
273 for binY in range(1,aHist.hist4D.GetNbinsY()+1):
274 if aHist.hist4D.GetBinContent(binX,binY) > aHist.OOBT and aHist.hist4D.GetBinContent(binX,binY) < -aHist.OOBT:
275 if not aHist.relativeMetric:
276 if fabs(aHist.hist4D.GetBinContent(binX,binY)) > fabs(self.hist4D.GetBinContent(binX,binY)):
277 self.hist4D.SetBinContent(binX,binY,aHist.hist4D.GetBinContent(binX,binY))
278 else:
279 currVal = self.hist4D.GetBinContent(binX,binY)
280 newVal = aHist.hist4D.GetBinContent(binX,binY)
281 if currVal < 1 and newVal < 1:
282 if newVal < currVal:
283 self.hist4D.SetBinContent(binX,binY,newVal)
284 elif currVal < 1 and newVal > 1:
285 if newVal > 1/currVal:
286 self.hist4D.SetBinContent(binX,binY,newVal)
287 elif currVal > 1 and newVal < 1:
288 if newVal < 1/currVal:
289 self.hist4D.SetBinContent(binX,binY,newVal)
290 elif currVal > 1 and newVal > 1:
291 if newVal > currVal:
292 self.hist4D.SetBinContent(binX,binY,newVal)
293 return True
294

◆ fillHist4DFromMinOfSet()

CorrelationMatrixHelpers.CorrMat4D.fillHist4DFromMinOfSet ( self,
hists )

4D histogram from minimum value of a 4D set #

Definition at line 203 of file CorrelationMatrixHelpers.py.

203 def fillHist4DFromMinOfSet(self,hists):
204 # Ensure the histogram wasn't already filled
205 if self.hist4D:
206 print "Blocking re-filling of existing CorrMat4D histogram of name ",self.hist4D.GetName()
207 return False
208
209 # Ensure the histogram(s) exist
210 if not hists or len(hists) == 0:
211 print "Argument is None or empty list: ",hists
212 return False
213
214 # Copy the first histogram for formatting purposes
215 self.cloneHist4D(hists[0])
216
217 # The values from the first histogram are already set from cloning
218 # Now just check if any other hists have smaller values
219 # Note that smaller may be relative to 1, not zero (for the relative metric)
220 # Watch for out-of-bounds
221 for aHist in hists[1:]:
222 for binX in range(1,aHist.hist4D.GetNbinsX()+1):
223 for binY in range(1,aHist.hist4D.GetNbinsY()+1):
224 if aHist.hist4D.GetBinContent(binX,binY) > aHist.OOBT:
225 if not aHist.relativeMetric:
226 if fabs(aHist.hist4D.GetBinContent(binX,binY)) < fabs(self.hist4D.GetBinContent(binX,binY)):
227 self.hist4D.SetBinContent(binX,binY,aHist.hist4D.GetBinContent(binX,binY))
228 else:
229 currVal = self.hist4D.GetBinContent(binX,binY)
230 newVal = aHist.hist4D.GetBinContent(binX,binY)
231 # If the values bracket one, we're safe
232 if currVal <= 1 and newVal >= 1:
233 self.hist4D.SetBinContent(binX,binY,1)
234 elif currVal >= 1 and newVal <= 1:
235 self.hist4D.SetBinContent(binX,binY,1)
236 elif currVal < 1 and newVal < 1:
237 if newVal > currVal:
238 self.hist4D.SetBinContent(binX,binY,newVal)
239 #elif currVal < 1 and newVal > 1:
240 # if newVal < 1/currVal:
241 # self.hist4D.SetBinContent(binX,binY,newVal)
242 #elif currVal > 1 and newVal < 1:
243 # if newVal > 1/currVal:
244 # self.hist4D.SetBinContent(binX,binY,newVal)
245 elif currVal > 1 and newVal > 1:
246 if newVal < currVal:
247 self.hist4D.SetBinContent(binX,binY,newVal)
248 return True
249

◆ setInfo()

CorrelationMatrixHelpers.CorrMat4D.setInfo ( self,
jetDef,
varType,
plotType )

Definition at line 40 of file CorrelationMatrixHelpers.py.

40 def setInfo(self,jetDef,varType,plotType):
41 self.jetDef = jetDef
42 self.varType = varType
43 self.plotType = plotType
44

◆ setInfoCopy()

CorrelationMatrixHelpers.CorrMat4D.setInfoCopy ( self,
toCopy,
plotType = "" )

Definition at line 45 of file CorrelationMatrixHelpers.py.

45 def setInfoCopy(self,toCopy,plotType = ""):
46 self.jetDef = toCopy.jetDef
47 self.varType = toCopy.varType
48 self.plotType = plotType if plotType != "" else toCopy.plotType
49

Member Data Documentation

◆ ATLASLabelName

str CorrelationMatrixHelpers.CorrMat4D.ATLASLabelName = "Internal"

Definition at line 35 of file CorrelationMatrixHelpers.py.

◆ DrawATLASLabel

bool CorrelationMatrixHelpers.CorrMat4D.DrawATLASLabel = True

Definition at line 36 of file CorrelationMatrixHelpers.py.

◆ hist4D

CorrelationMatrixHelpers.CorrMat4D.hist4D = None

Definition at line 25 of file CorrelationMatrixHelpers.py.

◆ histName

CorrelationMatrixHelpers.CorrMat4D.histName = None

Definition at line 139 of file CorrelationMatrixHelpers.py.

◆ iEPS

int CorrelationMatrixHelpers.CorrMat4D.iEPS = 0

Definition at line 37 of file CorrelationMatrixHelpers.py.

◆ jetDef

CorrelationMatrixHelpers.CorrMat4D.jetDef = None

Definition at line 26 of file CorrelationMatrixHelpers.py.

◆ OOB

CorrelationMatrixHelpers.CorrMat4D.OOB = -1234

Definition at line 29 of file CorrelationMatrixHelpers.py.

◆ OOBT

int CorrelationMatrixHelpers.CorrMat4D.OOBT = -1000

Definition at line 30 of file CorrelationMatrixHelpers.py.

◆ plotType

CorrelationMatrixHelpers.CorrMat4D.plotType = None

Definition at line 28 of file CorrelationMatrixHelpers.py.

◆ relativeMetric

CorrelationMatrixHelpers.CorrMat4D.relativeMetric = relMet

Definition at line 32 of file CorrelationMatrixHelpers.py.

◆ varType

CorrelationMatrixHelpers.CorrMat4D.varType = None

Definition at line 27 of file CorrelationMatrixHelpers.py.


The documentation for this class was generated from the following file: