ATLAS Offline Software
ICHEP2016/MakeFileForMJB.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 ParseEtaIntercalInput import ReadEtaIntercalibrationHistograms
12 from ParseInsituInput_MJB import ReadInSituHistograms
13 from ParsePileupInput import ReadPileupHistograms
14 from ParseFlavourInput import ReadFlavourHistograms
15 from ParsePunchthroughInput import ReadPunchthroughHistograms
16 from Parse2012Input import Read2012Histograms
17 
18 #http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
19 from itertools import groupby
20 def natural_sort(s):
21  return [int(''.join(g)) if k else ''.join(g) for k, g in groupby(s[0], str.isdigit)]
22 
23 # Option 2
24 #
25 # Design: This is to make a file entirely from scratch.
26 # All components are assumed available new.
27 # If that is not the case borrow parallel script from
28 # the Moriond 2016 directory (where punchthrough was kept from 2012)
29 
30 # Full list of required inputs, distinguished by needing them
31 # from a different person
32 # - In situ terms
33 # - Eta intercalibration
34 # - Pileup
35 # - Flavour
36 # - Punchthrough
37 
38 
39 # NOTES
40 # Need to carefully check all flavour histograms in comparison to 2012 to make sure they look sensible!!
41 # Things below were done in pre-recs and are currently being done here:
42 # - 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.
43 # - FlavorResponse histogram inverted from input file to match Run I convention
44 freezeFlavourInPt = True
45 
46 
47 
48 # Check useage and store arguments
49 if len(sys.argv) < 3:
50  print "USAGE: Expected the following arguments"
51  print " 1. Directory path to V+jet calibrations"
52  print " 2. Eta intercalibration directory path"
53  print " 3. Pileup directory path"
54  print " 5. Flavour directory path"
55  print " 6. Punchthrough directory path"
56  exit(1)
57 
58 outBaselineFile = "JESUncertainty_forMJB.root"
59 theVPlusJetsDir = sys.argv[1]
60 etaIntercalDir = sys.argv[2]
61 pileupDir = sys.argv[3]
62 flavourDir = sys.argv[4]
63 punchthroughDir = sys.argv[5]
64 
65 # Ensure that all of the directories exist
66 if not outBaselineFile.endswith(".root"):
67  print "Output baseline ROOT file doesn't appear to be a root file:",outBaselineFile
68  exit(2)
69 if not os.path.exists(theVPlusJetsDir):
70  print "V+jets directory does not exist:",theVPlusJetsDir
71  exit(3)
72 if not os.path.exists(etaIntercalDir):
73  print "Eta intercalibration directory does not exist:",etaIntercalDir
74  exit(4)
75 if not os.path.exists(pileupDir):
76  print "Pileup directory does not exist:",pileupDir
77  exit(5)
78 if not os.path.exists(flavourDir):
79  print "Flavour directory does not exist:",flavourDir
80  exit(6)
81 if not os.path.exists(punchthroughDir):
82  print "Punchthrough directory does not exist:",punchthroughDir
83  exit(7)
84 
85 
86 # Store everything in memory!
87 currentDir = gDirectory
88 gROOT.cd()
89 
90 # Now read the histograms
91 print "Reading inputs..."
92 # For now, everything except flavour and cross calibration inputs
93 # are read in from the final 2012 calibration files.
94 theVPlusJetsHistos = ReadInSituHistograms(theVPlusJetsDir) # Check!
95 etaIntercalHistos = ReadEtaIntercalibrationHistograms(etaIntercalDir)
96 pileupHistos = ReadPileupHistograms(pileupDir)
97 print "For pileup, got:"
98 for jetdef in pileupHistos.keys() :
99  print "Jet def",jetdef
100  print pileupHistos[jetdef]
101 
102 flavourHistos = ReadFlavourHistograms(flavourDir,freezeFlavourInPt) # True flag freezes uncertainty above pT = 2TeV (lower at higher eta)
103 print "For flavour, got:"
104 for jetdef in flavourHistos.keys() :
105  print "Jet def",jetdef
106  print flavourHistos[jetdef]
107 
108 punchthroughHistos = ReadPunchthroughHistograms(punchthroughDir)
109 print "For punchthrough, got:"
110 for jetdef in punchthroughHistos.keys() :
111  print "Jet def",jetdef
112  print punchthroughHistos[jetdef]
113 
114 # Make one mega-dictionary
115 print "Merging inputs..."
116 jetDefs = {"AntiKt4Topo_EMJES" : "AntiKt4EMTopo", "AntiKt4Topo_LCJES" : "AntiKt4LCTopo"}#"AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
117 systematics = {}
118 for aJetDef in jetDefs.keys():
119 
120  systematics[aJetDef] = {}
121  dictsToUse = [punchthroughHistos,
122  etaIntercalHistos,
123  theVPlusJetsHistos,
124  pileupHistos,
125  flavourHistos]
126 
127  for dictionary in dictsToUse :
128 
129  # We don't have LC inputs for some of these
130  #if "LC" in aJetDef :
131  # if aJetDef not in dictionary.keys() :
132  # useInstead = dictionary["AntiKt4Topo_EMJES"]
133  # Need to change name & copy hists so I don't have
134  # memory problems down the road
135  # myNewDict = {}
136  # for item in useInstead.keys() :
137  # thisHist = useInstead[item].Clone()
138  # thisHistName = useInstead[item].GetName().replace("EM","LC")
139  # thisHist.SetName(thisHistName)
140  # myNewDict[item] = thisHist
141 
142  # dictionary[aJetDef] = myNewDict
143 
144  systematics[aJetDef].update(dictionary[aJetDef].items())
145 
146  print "\nFor jet def",aJetDef,"added:"
147  print systematics[aJetDef]
148 
149 
150 # Loop over the mega-dictionary and write results
151 print "Writing to output file",outBaselineFile,"..."
152 baselineFile = TFile(outBaselineFile,"RECREATE")
153 for aJetDef,aSyst in sorted(systematics.iteritems(),key=natural_sort):
154  for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
155  baselineFile.cd()
156  aSystHisto.SetTitle(aSystName+"_"+jetDefs[aJetDef])
157  aSystHisto.Write(aSystHisto.GetTitle())
158 
159 # Done, close the files, revert directory
160 baselineFile.Close()
161 gDirectory = currentDir
162 print "Done!"
163 
ParsePunchthroughInput.ReadPunchthroughHistograms
def ReadPunchthroughHistograms(dirName)
Definition: Final2012/ParsePunchthroughInput.py:31
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ParseEtaIntercalInput.ReadEtaIntercalibrationHistograms
def ReadEtaIntercalibrationHistograms(dirName)
Definition: Final2012/ParseEtaIntercalInput.py:19
ParseFlavourInput.ReadFlavourHistograms
def ReadFlavourHistograms(dirName)
Definition: Final2012/ParseFlavourInput.py:30
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
ParseInsituInput.ReadInSituHistograms
def ReadInSituHistograms(dirName)
Definition: Final2012/ParseInsituInput.py:82
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
MakeFileForMJB.natural_sort
def natural_sort(s)
Definition: ICHEP2016/MakeFileForMJB.py:20
dqt_zlumi_pandas.update
update
Definition: dqt_zlumi_pandas.py:42
ParsePileupInput.ReadPileupHistograms
def ReadPileupHistograms(dirName)
Definition: Final2012/ParsePileupInput.py:15