ATLAS Offline Software
Loading...
Searching...
No Matches
January2018_specialStatTerms/ParsePunchthroughInput.py
Go to the documentation of this file.
1from ROOT import *
2from array import array
3import glob
4import re
5import math
6import ProviderHistoHelpers
7
8def GetKeyNames(self,dir=""):
9 self.cd(dir)
10 return [key.GetName() for key in gDirectory.GetListOfKeys()]
11TFile.GetKeyNames = GetKeyNames
12
13jetDefDict = {
14 'AntiKt4EMTopo' : 'AntiKt4Topo_EMJES',
15 'AntiKt4EMPFlow' : 'AntiKt4PFlow_EMJES',
16 }
17
18SystematicNameList = ['PunchThrough_MC16','PunchThrough_AFII']
19
20def ReadPunchthroughHistograms(dirName):
21 if not dirName.endswith("/"):
22 dirName = dirName + "/"
23
24 # Run over the one file
25 rootFileList = sorted(glob.glob(dirName+"*.root"))
26 fileDict = {}
27 if len(rootFileList) != 1:
28 print "Found a number of root files not equal to 1 in dir:",dirName
29 print "Assume separate files for",
30 for i in jetDefDict.keys() : print i,
31 print
32 for aJetDef in jetDefDict.keys() :
33 for file in rootFileList :
34 if aJetDef in file :
35 if aJetDef not in fileDict.keys() :
36 fileDict[aJetDef] = file
37 else :
38 print "Oh no! More than one file corresponding to this jet definition:",aJetDef
39 return None
40 else :
41 print "Found only one root file: assume contents can be used for all jet collections."
42 for aJetDef in jetDefDict.keys() :
43 fileDict[aJetDef] = rootFileList[0]
44
45 histos = {}
46 for aJetDef,aJetDefProv in jetDefDict.iteritems():
47 rootFile = TFile(fileDict[aJetDef],"READ")
48 histos[aJetDefProv] = {}
49
50 for histName in rootFile.GetKeyNames():
51 if aJetDef not in histName: continue
52 for aKey in SystematicNameList:
53 if "_MC" in aKey :
54 gethistName = aKey+"_"+aJetDef
55 histo = rootFile.Get(gethistName)
56 if histo is None:
57 print "Failed to get histogram:",histName
58 return None
59 histo.SetName(aKey+"_"+aJetDefProv)
60 histo.SetDirectory(0)
61
62 # In Lydia's histogram:
63 # x axis is pT.
64 # y axis is eta
65 # z axis is NSegments
66 # Last two axes need to get swapped to be compatible with 8TeV!!!!
67
68 # In Christopher's histogram,
69 # these are back to being as requested.
70
71 pTBinArray = []
72 nPTBins = histo.GetXaxis().GetNbins()
73 for bin in range(1,nPTBins+2) :
74 binEdge = histo.GetXaxis().GetBinLowEdge(bin)
75 pTBinArray.append(binEdge)
76 pTBinArray = array('d',pTBinArray)
77
78 etaBinArray = []
79# if "EM" in aJetDef :
80# nEtaBins = histo.GetYaxis().GetNbins()
81# for bin in range(1,nEtaBins+2) :
82# binEdge = histo.GetYaxis().GetBinLowEdge(bin)
83# etaBinArray.append(binEdge)
84# else :
85 nEtaBins = histo.GetZaxis().GetNbins()
86 for bin in range(1,nEtaBins+2) :
87 binEdge = histo.GetZaxis().GetBinLowEdge(bin)
88 etaBinArray.append(binEdge)
89 etaBinArray = array('d',etaBinArray)
90
91 nSegmentsBinArray = []
92# if "EM" in aJetDef :
93# nNSegBins = histo.GetZaxis().GetNbins()
94# for bin in range(1,nNSegBins+2) :
95# binEdge = histo.GetZaxis().GetBinLowEdge(bin)
96# nSegmentsBinArray.append(binEdge)
97# else :
98 nNSegBins = histo.GetYaxis().GetNbins()
99 for bin in range(1,nNSegBins+2) :
100 binEdge = histo.GetYaxis().GetBinLowEdge(bin)
101 nSegmentsBinArray.append(binEdge)
102
103 nSegmentsBinArray = array('d',nSegmentsBinArray)
104
105 outhisto = TH3D(aKey+"_"+aJetDefProv,aKey+"_"+aJetDefProv,nPTBins,pTBinArray,nNSegBins,nSegmentsBinArray,nEtaBins,etaBinArray)
106 outhisto.SetDirectory(0)
107
108 # Fix histo margins to make sure it only applies the calibration where it should.
109 # Loop over all bins in x, y, z.
110 for xbin in range(histo.GetNbinsX()+2) :
111 for ybin in range(histo.GetNbinsY()+2) :
112 for zbin in range(histo.GetNbinsZ()+2) :
113 # Permitted regions are only:
114 # - pT > 50 GeV
115 # - eta < 2.7
116 # - NSegments > 20
117
118# if "EM" in aJetDef :
119# if histo.GetXaxis().GetBinCenter(xbin) < 50 or \
120# histo.GetYaxis().GetBinCenter(ybin) > 2.7 or \
121# histo.GetZaxis().GetBinCenter(zbin) < 20 :
122# histo.SetBinContent(xbin,ybin,zbin,0.0)
123# outhisto.SetBinContent(xbin,zbin,ybin,0.0)
124# else :
125# outhisto.SetBinContent(xbin,zbin,ybin,histo.GetBinContent(xbin,ybin,zbin))
126
127# elif "LC" in aJetDef :
128 if histo.GetXaxis().GetBinCenter(xbin) < 50 or \
129 histo.GetZaxis().GetBinCenter(zbin) > 2.7 or \
130 histo.GetYaxis().GetBinCenter(ybin) < 20 :
131 histo.SetBinContent(xbin,ybin,zbin,0.0)
132 outhisto.SetBinContent(xbin,ybin,zbin,0.0)
133 else :
134 outhisto.SetBinContent(xbin,ybin,zbin,histo.GetBinContent(xbin,ybin,zbin))
135
136
137 histos[aJetDefProv][aKey] = outhisto
138
139 # Add a blank histogram for AFII corresponding to this
140 else :
141 outhisto = TH3D(aKey+"_"+aJetDefProv,aKey+"_"+aJetDefProv,2,15.0,4500.0,2,0.0,1000.0,2,-4.5,4.5)
142 outhisto.SetDirectory(0)
143 histos[aJetDefProv][aKey] = outhisto
144
145 # Done reading, close the file
146 rootFile.Close()
147 print "Done punchthrough.\n"
148
149 return histos
150
STL class.