ATLAS Offline Software
Loading...
Searching...
No Matches
January2018_specialStatTerms/ParseInputs.py
Go to the documentation of this file.
1from ROOT import *
2from array import array
3import sys
4import os
5import glob
6import re
7import math
8
9from ParseEtaIntercalInput_detailed import ReadEtaIntercalibrationHistograms
10from ParseInsituInput import ReadInSituHistograms
11from ParsePileupInput import ReadPileupHistograms
12from ParseNonClosureInput import ReadNonClosureHistograms
13from ParseHighPtInput import ReadHighPtHistograms,ReadHighPtHistogramsFromOldFile
14from ParseFlavourInput import ReadFlavourHistograms
15from ParsebJESInput import ReadBJESHistograms
16from ParsePunchthroughInput import ReadPunchthroughHistograms
17from CopyJERHists import ReadJERHistograms
18
19#http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
20from itertools import groupby
21def natural_sort(s):
22 return [int(''.join(g)) if k else ''.join(g) for k, g in groupby(s[0], str.isdigit)]
23
24# Design: This is to make a file entirely from scratch.
25# At present the high pT term has not been updated since 2012, so
26# that one will be retrieved from the final 2012 recommendations.
27# All else is needed new.
28
29# Full list of required inputs, distinguished by needing them
30# from a different person
31# - In situ terms
32# - Eta intercalibration
33# - Single particle
34# - MC non-closure
35# - Pileup
36# - Flavour
37# - B-JES
38# - Punchthrough
39
40
41# NOTES
42# Need to carefully check all flavour histograms in comparison to 2012 to make sure they look sensible!!
43# Things below were done in pre-recs and are currently being done here:
44# - 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.
45# - FlavorResponse histogram inverted from input file to match Run I convention
46freezeFlavourInPt = False
47
48
49
50# Check useage and store arguments
51if len(sys.argv) < 3:
52 print "USAGE: Expected the following arguments"
53 print " 1. InSitu directory path"
54 print " 2. Eta intercalibration directory path"
55 print " 3. Path or directory for single-particle: currently 2012 recommendation"
56 print " 4. MC nonclosure directory path"
57 print " 5. Pileup directory path"
58 print " 6. Flavour directory path"
59 print " 7. bJES directory path"
60 print " 8. Punchthrough directory path"
61 exit(1)
62
63outBaselineFile = "JESUncertainty_AllComponents_{0}.root"
64inSituDir = sys.argv[1]
65etaIntercalDir = sys.argv[2]
66highPtDir = sys.argv[3]
67nonclosureDir = sys.argv[4]
68pileupDir = sys.argv[5]
69flavourDir = sys.argv[6]
70bJESDir = sys.argv[7]
71punchthroughDir = sys.argv[8]
72oldJERFile = sys.argv[9]
73
74# Ensure that all of the directories exist
75if not outBaselineFile.endswith(".root"):
76 print "Output baseline ROOT file doesn't appear to be a root file:",outBaselineFile
77 exit(2)
78if not os.path.exists(inSituDir):
79 print "InSitu directory does not exist:",inSituDir
80 exit(3)
81if not os.path.exists(etaIntercalDir):
82 print "Eta intercalibration directory does not exist:",etaIntercalDir
83 exit(4)
84if not os.path.exists(highPtDir):
85 print "HighPt directory does not exist:",highPtDir
86 exit(5)
87if not os.path.exists(nonclosureDir):
88 print "MC nonclosure directory does not exist:",nonclosureDir
89 exit(6)
90if not os.path.exists(pileupDir):
91 print "Pileup directory does not exist:",pileupDir
92 exit(7)
93if not os.path.exists(flavourDir):
94 print "Flavour directory does not exist:",flavourDir
95 exit(8)
96if not os.path.exists(bJESDir):
97 print "bJES directory does not exist:",bJESDir
98 exit(9)
99if not os.path.exists(punchthroughDir):
100 print "Punchthrough directory does not exist:",punchthroughDir
101 exit(10)
102if not os.path.exists(oldJERFile) :
103 print "You need to specify a file to pick up JER from!",oldJERFile
104
105# Store everything in memory!
106currentDir = gDirectory
107gROOT.cd()
108
109# Now read the histograms
110print "Reading inputs..."
111# For now, the high pT component
112# is read in from the final 2012 calibration files.
113inSituHistos = ReadInSituHistograms(inSituDir)
114etaIntercalHistos_20152016 = ReadEtaIntercalibrationHistograms(etaIntercalDir,"2015_16_")
115etaIntercalHistos_2017 = ReadEtaIntercalibrationHistograms(etaIntercalDir,"2017_")
116etaIntercalHistos_general = ReadEtaIntercalibrationHistograms(etaIntercalDir,"")
117highPtHistos = ReadHighPtHistogramsFromOldFile(highPtDir)#ReadHighPtHistograms(highPtDir)
118nonclosureHistos = ReadNonClosureHistograms(nonclosureDir,True)
119pileupHistos = ReadPileupHistograms(pileupDir)
120flavourHistos = ReadFlavourHistograms(flavourDir,freezeFlavourInPt) # True flag freezes uncertainty above pT = 2TeV (lower at higher eta)
121bJESHistos = ReadBJESHistograms(bJESDir)
122punchthroughHistos = ReadPunchthroughHistograms(punchthroughDir)
123JERHistos = ReadJERHistograms(oldJERFile)
124
125# Make one mega-dictionary
126print "Merging inputs..."
127jetDefs = {"AntiKt4Topo_EMJES" : "AntiKt4EMTopo", 'AntiKt4PFlow_EMJES' : 'AntiKt4EMPFlow'}
128systematics = {}
129
130for years in ("2015_16","2017") :
131 systematics[years] = {}
132 for aJetDef in jetDefs.keys():
133
134 etaIntercalHistos_timeDep = etaIntercalHistos_2017 if "2017" in years else etaIntercalHistos_20152016
135
136 systematics[years][aJetDef] = dict(
137 inSituHistos[aJetDef].items() +
138 etaIntercalHistos_timeDep[aJetDef].items() +
139 etaIntercalHistos_general[aJetDef].items() +
140 highPtHistos[aJetDef].items() +
141 nonclosureHistos[aJetDef].items() +
142 pileupHistos[aJetDef].items() +
143 flavourHistos[aJetDef].items() +
144 bJESHistos[aJetDef].items() +
145 punchthroughHistos[aJetDef].items()
146 )
147
148# Loop over the mega-dictionary and write results
149# Want one output file per dataset
150for years in ("2015_16","2017") :
151 outFileName = outBaselineFile.format(years)
152 print "Writing to output file",outFileName,"..."
153 baselineFile = TFile(outFileName,"RECREATE")
154 theseHists = systematics[years]
155 for aJetDef,aSyst in sorted(theseHists.iteritems(),key=natural_sort):
156 for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
157 baselineFile.cd()
158 aSystHisto.SetTitle(aSystName+"_"+jetDefs[aJetDef])
159 aSystHisto.Write(aSystHisto.GetTitle())
160
161 # Write JER last cause it's kind of separate
162 for aJetDef,aSyst in sorted(JERHistos.iteritems(),key=natural_sort):
163 for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
164 aSystHisto.SetTitle(aSystName+"_"+jetDefs[aJetDef])
165 aSystHisto.Write(aSystHisto.GetTitle())
166
167 # Done, close the files, revert directory
168 baselineFile.Close()
169 gDirectory = currentDir
170
171print "Done!"
172
173
174