ATLAS Offline Software
Prerec2012/ParseInputs.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 sys
6 import os
7 import glob
8 import re
9 import math
10 
11 from Parse2012Input import Read2012Histograms
12 from ParseEtaIntercalInput import ReadEtaIntercalibrationHistograms
13 from ParseInsituInput import ReadInSituHistograms
14 from ParsePileupInput import ReadPileupHistograms
15 from ParseNonClosureInput import ReadNonClosureHistograms
16 from ParseHighPtInput import ReadHighPtHistograms
17 from ParseFlavourInput import ReadFlavourHistograms
18 from ParsebJESInput import ReadBJESHistograms
19 from ParsePunchthroughInput import ReadPunchthroughHistograms
20 from ParseCrossCalInput import ReadCrossCalHistograms,ReadCrossCalValidityHistograms
21 
22 #http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
23 from itertools import groupby
24 def natural_sort(s):
25  return [int(''.join(g)) if k else ''.join(g) for k, g in groupby(s[0], str.isdigit)]
26 
27 # NOTES
28 # Option 1
29 # - Freeze cross-calibration histograms by hand outside of eta=0.8 to twice the value of the last sensible bin
30 # - Freeze flavour histograms by hand wherever input histogram falls below 0: extrapolate forward in pT from around 2 TeV centrally and from lower pT as eta increases.
31 # - Set eta-intercalibration by hand to:
32 # - The run-1 uncertainty for |eta| < 0.8
33 # - Twice the run-1 uncertainty /in these same bins/ for 0.8 < |eta| < 2.5
34 # - Three times the run-1 uncertainty /in these same bins/ for 2.5 < |eta|
35 # - FlavorResponse histogram inverted from input file to match Run I convention
36 # - Pileup values not changed!!
37 # Use flag OPT1
38 
39 # Option 2
40 #
41 
42 flag = "NOMINAL"
43 
44 
45 
46 if flag == "NOMINAL" :
47  ext = "3EtaScaleRegions_FreezeCrossCalEverywhereSingle"
48  scaleCrossCalVal=1.0
49  scaleEtaInter3Regions = True
50  scaleEtaInter2Regions = False
51  freezeCrossCalAll = True
52  freezeCrossCalBarrel = False
53  freezeCrossCalCentral = False
54  freezeFlavourInPt = True
55 
56 if flag == "OPT1" :
57  ext = "3EtaScaleRegions_FreezeCrossCalEverywhereDouble"
58  scaleCrossCalVal=2.0
59  scaleEtaInter3Regions = True
60  scaleEtaInter2Regions = False
61  freezeCrossCalAll = True
62  freezeCrossCalBarrel = False
63  freezeFlavourInPt = True
64  freezeCrossCalCentral = False
65 
66 if flag == "OPT2" :
67  ext = "2EtaScaleRegions_FreezeCrossCalBarrelSingle"
68  scaleCrossCalVal=1.0
69  scaleEtaInter3Regions = False
70  scaleEtaInter2Regions = True
71  freezeCrossCalAll = False
72  freezeCrossCalBarrel = True
73  freezeCrossCalCentral = False
74  freezeFlavourInPt = True
75 
76 if flag == "OPT3" :
77  ext = "3EtaScaleRegions_FreezeCrossCalBarrelSingle"
78  scaleCrossCalVal=1.0
79  scaleEtaInter3Regions = True
80  scaleEtaInter2Regions = False
81  freezeCrossCalAll = False
82  freezeCrossCalBarrel = True
83  freezeFlavourInPt = True
84  freezeCrossCalCentral = False
85 
86 if flag == "OPT4" :
87  ext = "2EtaScaleRegions_FreezeCrossCalEverywhereDouble"
88  scaleCrossCalVal=2.0
89  scaleEtaInter3Regions = False
90  scaleEtaInter2Regions = True
91  freezeCrossCalAll = True
92  freezeCrossCalBarrel = False
93  freezeCrossCalCentral = False
94  freezeFlavourInPt = True
95 
96 # Check useage and store arguments
97 if len(sys.argv) < 3:
98  print "USAGE: Expected the following arguments"
99  print " 1. Directory path to 2012 final calibrations"
100  print " 2. Eta intercalibration directory path"
101 # print " 2. InSitu directory path"
102  print " 3. Pileup directory path"
103  print " 4. MC nonclosure directory path"
104 # print " 5. HighPt directory path"
105  print " 5. Flavour directory path"
106 # print " 7. bJES directory path"
107 # print " 8. Punchthrough directory path"
108  print " 6. Cross calibration directory path"
109  exit(1)
110 
111 outBaselineFile = "JESUncertainty_BaseComponents_{0}.root".format(ext)
112 #outMultijetFile = "MJESUncertainty_2015.root"
113 the2012Dir = sys.argv[1]
114 etaIntercalDir = sys.argv[2]
115 #inSituDir = sys.argv[2]
116 pileupDir = sys.argv[3]
117 nonclosureDir = sys.argv[4]
118 #highPtDir = sys.argv[5]
119 flavourDir = sys.argv[5]
120 #bJESDir = sys.argv[7]
121 #punchthroughDir = sys.argv[8]
122 crosscalDir = sys.argv[6]
123 
124 # Ensure that all of the directories exist
125 if not outBaselineFile.endswith(".root"):
126  print "Output baseline ROOT file doesn't appear to be a root file:",outBaselineFile
127  exit(2)
128 #if not outMultijetFile.endswith(".root"):
129 # print "Output multijet ROOT file doesn't appear to be a root file:",outMultijetFile
130 # exit(3)
131 if not os.path.exists(the2012Dir):
132  print "2012 directory does not exist:",the2012Dir
133  exit(3)
134 if not os.path.exists(etaIntercalDir):
135  print "Eta intercalibration directory does not exist:",etaIntercalDir
136  exit(4)
137 #if not os.path.exists(inSituDir):
138 # print "InSitu directory does not exist:",inSituDir
139 # exit(5)
140 if not os.path.exists(pileupDir):
141  print "Pileup directory does not exist:",pileupDir
142  exit(6)
143 if not os.path.exists(nonclosureDir):
144  print "MC nonclosure directory does not exist:",nonclosureDir
145  exit(7)
146 #if not os.path.exists(highPtDir):
147 # print "HighPt directory does not exist:",highPtDir
148 # exit(8)
149 if not os.path.exists(flavourDir):
150  print "Flavour directory does not exist:",flavourDir
151  exit(9)
152 #if not os.path.exists(bJESDir):
153 # print "bJES directory does not exist:",bJESDir
154 # exit(10)
155 #if not os.path.exists(punchthroughDir):
156 # print "Punchthrough directory does not exist:",punchthroughDir
157 # exit(11)
158 if not os.path.exists(crosscalDir):
159  print "Cross calibration directory does not exist:",crosscalDir
160  exit(12)
161 
162 # Store everything in memory!
163 currentDir = gDirectory
164 gROOT.cd()
165 
166 # Now read the histograms
167 print "Reading inputs..."
168 # For now, everything except flavour and cross calibration inputs
169 # are read in from the final 2012 calibration files.
170 the2012Histos = Read2012Histograms(the2012Dir,scaleEtaInter3Regions,scaleEtaInter2Regions) # First true activates eta-intercalibration scaling. Second true turns off relative in situ components at large eta
171 etaIntercalHistos = ReadEtaIntercalibrationHistograms(etaIntercalDir)
172 #inSituHistos = ReadInSituHistograms(inSituDir)
173 pileupHistos = ReadPileupHistograms(pileupDir)
174 nonclosureHistos = ReadNonClosureHistograms(nonclosureDir,True)
175 #highPtHistos = ReadHighPtHistograms(highPtDir)
176 flavourHistos = ReadFlavourHistograms(flavourDir,freezeFlavourInPt) # True flag freezes uncertainty above pT = 2TeV (lower at higher eta)
177 #bJESHistos = ReadBJESHistograms(bJESDir)
178 #punchthroughHistos = ReadPunchthroughHistograms(punchthroughDir)
179 crosscalHistos = ReadCrossCalHistograms(crosscalDir,scaleCrossCalVal,freezeCrossCalAll,freezeCrossCalBarrel,freezeCrossCalCentral) # First true freezes uncertainty above eta = 0.8. Second freezes it between 0.8 and 2.5 and sets it to 0 outside 2.5. Third turns off uncertainty above eta = 0.8. Use only one!
180 crosscalValHistos = ReadCrossCalValidityHistograms(crosscalDir)
181 
182 # Make one mega-dictionary
183 print "Merging inputs..."
184 jetDefs = {"AntiKt4Topo_EMJES" : "AntiKt4EMTopo"}#,"AntiKt4Topo_LCJES","AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
185 systematics = {}
186 validities = {}
187 for aJetDef in jetDefs.keys():
188  systematics[aJetDef] = dict(
189  the2012Histos[aJetDef].items() +
190  etaIntercalHistos[aJetDef].items() +
191 # inSituHistos[aJetDef].items() +
192  pileupHistos[aJetDef].items() +
193  nonclosureHistos[aJetDef].items() +
194 # highPtHistos[aJetDef].items() +
195  flavourHistos[aJetDef].items() +
196 # bJESHistos[aJetDef].items() +
197 # punchthroughHistos[aJetDef].items() +
198  crosscalHistos[aJetDef].items()
199  )
200  validities[aJetDef] = dict(
201  crosscalValHistos[aJetDef].items()
202  )
203 
204 # Loop over the mega-dictionary and write results
205 print "Writing to output file",outBaselineFile,"..."
206 baselineFile = TFile(outBaselineFile,"RECREATE")
207 #multijetFile = TFile(outMultijetFile,"RECREATE")
208 for aJetDef,aSyst in sorted(systematics.iteritems(),key=natural_sort):
209  for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
210 # if "bJES" in aSystName or "flavor" in aSystName or "Flavor" in aSystName or "PunchThrough" in aSystName or "Close" in aSystName:
211 # multijetFile.cd()
212 # else:
213  baselineFile.cd()
214  aSystHisto.SetTitle(aSystName+"_"+jetDefs[aJetDef])
215  aSystHisto.Write(aSystHisto.GetTitle())
216 for aJetDef,aVal in sorted(validities.iteritems(),key=natural_sort):
217  for aSystName,aValHisto in sorted(aVal.iteritems(),key=natural_sort):
218  baselineFile.cd()
219  aValHisto.SetTitle("valid_"+aSystName+"_"+jetDefs[aJetDef])
220 
221 # Done, close the files, revert directory
222 baselineFile.Close()
223 #multijetFile.Close()
224 gDirectory = currentDir
225 print "Done!"
226 
ParseNonClosureInput.ReadNonClosureHistograms
def ReadNonClosureHistograms(dirName)
Definition: Final2012/ParseNonClosureInput.py:22
vtune_athena.format
format
Definition: vtune_athena.py:14
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ParseEtaIntercalInput.ReadEtaIntercalibrationHistograms
def ReadEtaIntercalibrationHistograms(dirName)
Definition: Final2012/ParseEtaIntercalInput.py:19
python.KeyStore.dict
def dict(self)
Definition: KeyStore.py:321
ParseFlavourInput.ReadFlavourHistograms
def ReadFlavourHistograms(dirName)
Definition: Final2012/ParseFlavourInput.py:30
ParseInputs.natural_sort
def natural_sort(s)
Definition: Final2012/ParseInputs.py:23
calibdata.exit
exit
Definition: calibdata.py:236
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.
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Parse2012Input.Read2012Histograms
def Read2012Histograms(dirName, scaleEtaInter3Regions=False, scaleEtaInter2Regions=False)
Definition: ICHEP2016/Parse2012Input.py:32
python.test_cfgItemList.items
items
Definition: test_cfgItemList.py:23
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
ParsePileupInput.ReadPileupHistograms
def ReadPileupHistograms(dirName)
Definition: Final2012/ParsePileupInput.py:15