ATLAS Offline Software
Loading...
Searching...
No Matches
Prerec2012/ParseInputs.py
Go to the documentation of this file.
1# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2
3from ROOT import *
4from array import array
5import sys
6import os
7import glob
8import re
9import math
10
11from Parse2012Input import Read2012Histograms
12from ParseEtaIntercalInput import ReadEtaIntercalibrationHistograms
13from ParseInsituInput import ReadInSituHistograms
14from ParsePileupInput import ReadPileupHistograms
15from ParseNonClosureInput import ReadNonClosureHistograms
16from ParseHighPtInput import ReadHighPtHistograms
17from ParseFlavourInput import ReadFlavourHistograms
18from ParsebJESInput import ReadBJESHistograms
19from ParsePunchthroughInput import ReadPunchthroughHistograms
20from ParseCrossCalInput import ReadCrossCalHistograms,ReadCrossCalValidityHistograms
21
22#http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
23from itertools import groupby
24def 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
42flag = "NOMINAL"
43
44
45
46if 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
56if 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
66if 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
76if 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
86if 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
97if 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
111outBaselineFile = "JESUncertainty_BaseComponents_{0}.root".format(ext)
112#outMultijetFile = "MJESUncertainty_2015.root"
113the2012Dir = sys.argv[1]
114etaIntercalDir = sys.argv[2]
115#inSituDir = sys.argv[2]
116pileupDir = sys.argv[3]
117nonclosureDir = sys.argv[4]
118#highPtDir = sys.argv[5]
119flavourDir = sys.argv[5]
120#bJESDir = sys.argv[7]
121#punchthroughDir = sys.argv[8]
122crosscalDir = sys.argv[6]
123
124# Ensure that all of the directories exist
125if 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)
131if not os.path.exists(the2012Dir):
132 print "2012 directory does not exist:",the2012Dir
133 exit(3)
134if 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)
140if not os.path.exists(pileupDir):
141 print "Pileup directory does not exist:",pileupDir
142 exit(6)
143if 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)
149if 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)
158if not os.path.exists(crosscalDir):
159 print "Cross calibration directory does not exist:",crosscalDir
160 exit(12)
161
162# Store everything in memory!
163currentDir = gDirectory
164gROOT.cd()
165
166# Now read the histograms
167print "Reading inputs..."
168# For now, everything except flavour and cross calibration inputs
169# are read in from the final 2012 calibration files.
170the2012Histos = Read2012Histograms(the2012Dir,scaleEtaInter3Regions,scaleEtaInter2Regions) # First true activates eta-intercalibration scaling. Second true turns off relative in situ components at large eta
171etaIntercalHistos = ReadEtaIntercalibrationHistograms(etaIntercalDir)
172#inSituHistos = ReadInSituHistograms(inSituDir)
173pileupHistos = ReadPileupHistograms(pileupDir)
174nonclosureHistos = ReadNonClosureHistograms(nonclosureDir,True)
175#highPtHistos = ReadHighPtHistograms(highPtDir)
176flavourHistos = ReadFlavourHistograms(flavourDir,freezeFlavourInPt) # True flag freezes uncertainty above pT = 2TeV (lower at higher eta)
177#bJESHistos = ReadBJESHistograms(bJESDir)
178#punchthroughHistos = ReadPunchthroughHistograms(punchthroughDir)
179crosscalHistos = 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!
180crosscalValHistos = ReadCrossCalValidityHistograms(crosscalDir)
181
182# Make one mega-dictionary
183print "Merging inputs..."
184jetDefs = {"AntiKt4Topo_EMJES" : "AntiKt4EMTopo"}#,"AntiKt4Topo_LCJES","AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
185systematics = {}
186validities = {}
187for 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
205print "Writing to output file",outBaselineFile,"..."
206baselineFile = TFile(outBaselineFile,"RECREATE")
207#multijetFile = TFile(outMultijetFile,"RECREATE")
208for 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())
216for 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
222baselineFile.Close()
223#multijetFile.Close()
224gDirectory = currentDir
225print "Done!"
226