ATLAS Offline Software
Prerec2012/ParseCrossCalInput.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 from ROOT import *
4 from array import array
5 import glob
6 import re
7 import math
8 import ProviderHistoHelpers
9 
10 SystematicNameDictionary = {
11  'Xcalib_50nsVs25ns_AntiKt4EMTopo':'Xcalib_50nsVs25ns',
12  'Xcalib_5Vs4sample_AntiKt4EMTopo':'Xcalib_5Vs4sample',
13  'Xcalib_TopoClustering_AntiKt4EMTopo':'Xcalib_TopoClustering',
14  'Xcalib_EarlyData_AntiKt4EMTopo':'Xcalib_EarlyData',
15  'Xcalib_NoiseThreshold_AntiKt4EMTopo':'Xcalib_NoiseThreshold',
16  'Xcalib_UnderlyingEvent_AntiKt4EMTopo':'Xcalib_UnderlyingEvent'
17  }
18 
19 jetDefDict = {
20  '' : 'AntiKt4Topo_EMJES',
21  }
22 
23 def ReadCrossCalHistograms(dirName,scaleCrossCalVal=1.0,freezeCrossCalAll=False,freezeCrossCalBarrel=False,freezeCrossCalCentral=False):
24  if not dirName.endswith("/"):
25  dirName = dirName + "/"
26 
27  # Run over each file (one per jet definition)
28  histos = {}
29  files = sorted(glob.glob(dirName+"*.root"))
30  for aFileName in files:
31  # Determine the jet definition
32  jetDef = ""
33  for aFileDef,aJetDef in jetDefDict.iteritems():
34  if aFileDef in aFileName:
35  jetDef = aJetDef
36  break
37  if jetDef == "":
38  print "Failed to determine jet definition for file:",aFileName
39  return None
40 
41  histos[jetDef] = {}
42 
43  # Read in the histograms of interest from the file
44  inFile = TFile(aFileName,"READ")
45 
46  for histName in inFile.GetKeyNames():
47 
48  # Skip validity histograms.
49  if 'valid' in histName :
50  continue
51 
52  systematicName = SystematicNameDictionary[histName]+"_"+jetDef
53  histo = inFile.Get(histName)
54  if histo is None:
55  print "Failed to get histogram:",systematicName
56  return None
57  histo.SetName(systematicName+"_old")
58 
59  if freezeCrossCalAll or freezeCrossCalBarrel or freezeCrossCalCentral :
60  # Loop over pT bins.
61  for xbin in range(histo.GetNbinsX()+2) :
62  # Loop out in eta.
63  freezeval = 0
64  for ybin in range(histo.GetNbinsY()+2) :
65  #print "Bin at pT ",histo.GetXaxis().GetBinCenter(xbin)," and [ylow,y,yhigh] = ", binYLow, histo.GetYaxis().GetBinCenter(ybin), binYHigh
66  # Store bin contents as we go out: last one is one we want as frozen value.
67  eta = abs(histo.GetYaxis().GetBinCenter(ybin))
68  if eta < 0.8 :
69  freezeval = histo.GetBinContent(xbin,ybin)
70  else :
71  if (eta < 2.5 and freezeCrossCalBarrel) or freezeCrossCalAll :
72  histo.SetBinContent(xbin,ybin,freezeval*scaleCrossCalVal)
73  else :
74  histo.SetBinContent(xbin,ybin,0)
75 
76 
77  # Histo has different range, extend it
78  histoNew = ProviderHistoHelpers.ExtendPtRangeOfHisto(histo,systematicName)
79  histoSym = ProviderHistoHelpers.SymmetrizeHistoInEta(histoNew,systematicName)
80  histoSym.SetDirectory(0)
81 
82  # If this is the 50 vs 25 ns histogram, we need to separate it by eta region.
83  if '50nsVs25ns' in histName :
84  CentralHist = histo.Clone()
85  CentralHist.SetName("Central_holder")
86  ForwardHist = histo.Clone()
87  ForwardHist.SetName("Forward_holder")
88  # Loop over pT bins.
89  for xbin in range(histo.GetNbinsX()+2) :
90  # Loop out in eta.
91  freezeval = 0
92  for ybin in range(histo.GetNbinsY()+2) :
93  #print "Bin at pT ",histo.GetXaxis().GetBinCenter(xbin)," and [ylow,y,yhigh] = ", binYLow, histo.GetYaxis().GetBinCenter(ybin), binYHigh
94  # Store bin contents as we go out: last one is one we want as frozen value.
95  eta = abs(histo.GetYaxis().GetBinCenter(ybin))
96  if eta < 0.8 :
97  freezeval = histo.GetBinContent(xbin,ybin)
98  ForwardHist.SetBinContent(xbin,ybin,0.0)
99  CentralHist.SetBinContent(xbin,ybin,freezeval)
100  else :
101  ForwardHist.SetBinContent(xbin,ybin,freezeval)
102  CentralHist.SetBinContent(xbin,ybin,0.0)
103 
104  # Now do symmetrization for these hists and write to file.
105  NewCentral = ProviderHistoHelpers.ExtendPtRangeOfHisto(CentralHist,systematicName+"holder")
106  CentralSym = ProviderHistoHelpers.SymmetrizeHistoInEta(NewCentral,SystematicNameDictionary[histName]+"_Central_"+jetDef)
107  CentralSym.SetDirectory(0)
108  histos[jetDef][SystematicNameDictionary[histName]+"_Central"] = CentralSym
109  NewForward = ProviderHistoHelpers.ExtendPtRangeOfHisto(ForwardHist,systematicName+"holder")
110  ForwardSym = ProviderHistoHelpers.SymmetrizeHistoInEta(NewForward,SystematicNameDictionary[histName]+"_Forward_"+jetDef)
111  ForwardSym.SetDirectory(0)
112  histos[jetDef][SystematicNameDictionary[histName]+"_Forward"] = ForwardSym
113 
114  # If this is the early data histogram, we need to deactivate it in the central region for 25 ns data.
115  if 'EarlyData' in histName :
116  ForwardHist = histo.Clone()
117  ForwardHist.SetName("EarlyData_Forward_holder")
118  # Loop over pT bins.
119  for xbin in range(histo.GetNbinsX()+2) :
120  # Loop out in eta.
121  for ybin in range(histo.GetNbinsY()+2) :
122  #print "Bin at pT ",histo.GetXaxis().GetBinCenter(xbin)," and [ylow,y,yhigh] = ", binYLow, histo.GetYaxis().GetBinCenter(ybin), binYHigh
123  # Store bin contents as we go out: last one is one we want as frozen value.
124  eta = abs(histo.GetYaxis().GetBinCenter(ybin))
125  if eta < 0.8 :
126  ForwardHist.SetBinContent(xbin,ybin,0.0)
127 
128  # Now do symmetrization and write to file.
129  NewForward = ProviderHistoHelpers.ExtendPtRangeOfHisto(ForwardHist,systematicName+"holder")
130  ForwardSym = ProviderHistoHelpers.SymmetrizeHistoInEta(NewForward,SystematicNameDictionary[histName]+"_Forward_"+jetDef)
131  ForwardSym.SetDirectory(0)
132  histos[jetDef][SystematicNameDictionary[histName]+"_Forward"] = ForwardSym
133  histos[jetDef][SystematicNameDictionary[histName]] = histoSym
134 
135  else :
136  histos[jetDef][SystematicNameDictionary[histName]] = histoSym
137 
138  # Done reading, close the file
139  inFile.Close()
140 
141  return histos
142 
143 def ReadCrossCalValidityHistograms(dirName):
144  if not dirName.endswith("/"):
145  dirName = dirName + "/"
146 
147  # Run over each file (one per jet definition)
148  histos = {}
149  files = sorted(glob.glob(dirName+"*.root"))
150  for aFileName in files:
151  # Determine the jet definition
152  jetDef = ""
153  for aFileDef,aJetDef in jetDefDict.iteritems():
154  if aFileDef in aFileName:
155  jetDef = aJetDef
156  break
157  if jetDef == "":
158  print "Failed to determine jet definition for file:",aFileName
159  return None
160  histos[jetDef] = {}
161 
162  # Read in the histograms of interest from the file
163  inFile = TFile(aFileName,"READ")
164  for histName in inFile.GetKeyNames():
165  if 'valid' not in histName :
166  continue
167  dictkey = histName.replace("valid_","")
168  systematicName = "valid_"+SystematicNameDictionary[dictkey]+"_"+jetDef
169  histo = inFile.Get(histName)
170  if histo is None:
171  print "Failed to get histogram:",systematicName
172  return None
173  histo.SetName(systematicName+"_old")
174 
175  # Histo has different range, extend it
176  histoNew = ProviderHistoHelpers.ExtendPtRangeOfHisto(histo,"valid_"+systematicName)
177  histoSym = ProviderHistoHelpers.SymmetrizeHistoInEta(histoNew,"valid_"+systematicName)
178  histoSym.SetDirectory(0)
179  histos[jetDef][SystematicNameDictionary[dictkey]] = histoSym
180 
181  # Done reading, close the file
182  inFile.Close()
183 
184  return histos
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
ProviderHistoHelpers.SymmetrizeHistoInEta
def SymmetrizeHistoInEta(histo, histoName)
Definition: Final2012/ProviderHistoHelpers.py:127
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
ProviderHistoHelpers.ExtendPtRangeOfHisto
def ExtendPtRangeOfHisto(histo, histoName)
Definition: Final2012/ProviderHistoHelpers.py:97
ParseCrossCalInput.ReadCrossCalHistograms
def ReadCrossCalHistograms(dirName, scaleCrossCalVal=1.0, freezeCrossCalAll=False, freezeCrossCalBarrel=False, freezeCrossCalCentral=False)
Definition: ICHEP2016/ParseCrossCalInput.py:23
ParseCrossCalInput.ReadCrossCalValidityHistograms
def ReadCrossCalValidityHistograms(dirName)
Definition: ICHEP2016/ParseCrossCalInput.py:143