ATLAS Offline Software
Prerec2012/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 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 # NOTES
24 # Option 1
25 # - Freeze cross-calibration histograms by hand outside of eta=0.8 to twice the value of the last sensible bin
26 # - 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.
27 # - Set eta-intercalibration by hand to:
28 # - The run-1 uncertainty for |eta| < 0.8
29 # - Twice the run-1 uncertainty /in these same bins/ for 0.8 < |eta| < 2.5
30 # - Three times the run-1 uncertainty /in these same bins/ for 2.5 < |eta|
31 # - FlavorResponse histogram inverted from input file to match Run I convention
32 # - Pileup values not changed!!
33 # Use flag OPT1
34 
35 # Option 2
36 #
37 
38 
39 
40 
41 ext = ""
42 scaleCrossCalVal=1.0
43 scaleEtaInter3Regions = True
44 scaleEtaInter2Regions = False
45 freezeCrossCalAll = True
46 freezeCrossCalBarrel = False
47 freezeCrossCalCentral = False
48 freezeFlavourInPt = True
49 
50 # Check useage and store arguments
51 if len(sys.argv) < 3:
52  print "USAGE: Expected the following arguments"
53  print " 1. Directory path to V+jet calibrations"
54  print " 2. Eta intercalibration directory path"
55  print " 3. Pileup directory path"
56  print " 5. Flavour directory path"
57  print " 6. Old 2012 file from which to get punchthrough"
58  exit(1)
59 
60 outBaselineFile = "JESUncertainty_forMJB.root"
61 theVPlusJetsDir = sys.argv[1]
62 etaIntercalDir = sys.argv[2]
63 pileupDir = sys.argv[3]
64 flavourDir = sys.argv[4]
65 the2012Dir = sys.argv[5]
66 
67 # Ensure that all of the directories exist
68 if not outBaselineFile.endswith(".root"):
69  print "Output baseline ROOT file doesn't appear to be a root file:",outBaselineFile
70  exit(2)
71 if not os.path.exists(theVPlusJetsDir):
72  print "V+jets directory does not exist:",theVPlusJetsDir
73  exit(3)
74 if not os.path.exists(etaIntercalDir):
75  print "Eta intercalibration directory does not exist:",etaIntercalDir
76  exit(4)
77 if not os.path.exists(pileupDir):
78  print "Pileup directory does not exist:",pileupDir
79  exit(5)
80 if not os.path.exists(flavourDir):
81  print "Flavour directory does not exist:",flavourDir
82  exit(6)
83 if not os.path.exists(the2012Dir) :
84  print "No 2012 file:",the2012Dir
85  exit(7)
86 
87 # Store everything in memory!
88 currentDir = gDirectory
89 gROOT.cd()
90 
91 # Now read the histograms
92 print "Reading inputs..."
93 # For now, everything except flavour and cross calibration inputs
94 # are read in from the final 2012 calibration files.
95 theVPlusJetsHistos = ReadInSituHistograms(theVPlusJetsDir)
96 etaIntercalHistos = ReadEtaIntercalibrationHistograms(etaIntercalDir)
97 pileupHistos = ReadPileupHistograms(pileupDir)
98 flavourHistos = ReadFlavourHistograms(flavourDir,freezeFlavourInPt) # True flag freezes uncertainty above pT = 2TeV (lower at higher eta)
99 the2012Histos = Read2012Histograms(the2012Dir,scaleEtaInter3Regions,scaleEtaInter2Regions)
100 
101 punchthroughHistos = {}
102 for key in the2012Histos :
103  innerDict = the2012Histos[key]
104  newDict = {}
105  for name in innerDict.keys() :
106  if "PunchThrough" in name :
107  newDict[name] = innerDict[name]
108  punchthroughHistos[key] = newDict
109 
110 # Make one mega-dictionary
111 print "Merging inputs..."
112 jetDefs = {"AntiKt4Topo_EMJES" : "AntiKt4EMTopo", "AntiKt4Topo_LCJES" : "AntiKt4LCTopo"}#"AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
113 systematics = {}
114 for aJetDef in jetDefs.keys():
115 
116  systematics[aJetDef] = {}
117  dictsToUse = [punchthroughHistos,
118  etaIntercalHistos,
119  theVPlusJetsHistos,
120  pileupHistos,
121  flavourHistos]
122 
123  for dictionary in dictsToUse :
124 
125  # We don't have LC inputs for some of these
126  if "LC" in aJetDef :
127  if aJetDef not in dictionary.keys() :
128  useInstead = dictionary["AntiKt4Topo_EMJES"]
129  # Need to change name & copy hists so I don't have
130  # memory problems down the road
131  myNewDict = {}
132  for item in useInstead.keys() :
133  thisHist = useInstead[item].Clone()
134  thisHistName = useInstead[item].GetName().replace("EM","LC")
135  thisHist.SetName(thisHistName)
136  myNewDict[item] = thisHist
137 
138  dictionary[aJetDef] = myNewDict
139 
140  systematics[aJetDef].update(dictionary[aJetDef].items())
141 
142 # Loop over the mega-dictionary and write results
143 print "Writing to output file",outBaselineFile,"..."
144 baselineFile = TFile(outBaselineFile,"RECREATE")
145 for aJetDef,aSyst in sorted(systematics.iteritems(),key=natural_sort):
146  for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
147  baselineFile.cd()
148  aSystHisto.SetTitle(aSystName+"_"+jetDefs[aJetDef])
149  aSystHisto.Write(aSystHisto.GetTitle())
150 
151 # Done, close the files, revert directory
152 baselineFile.Close()
153 gDirectory = currentDir
154 print "Done!"
155 
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
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
Parse2012Input.Read2012Histograms
def Read2012Histograms(dirName, scaleEtaInter3Regions=False, scaleEtaInter2Regions=False)
Definition: ICHEP2016/Parse2012Input.py:32
dqt_zlumi_pandas.update
update
Definition: dqt_zlumi_pandas.py:42
ParsePileupInput.ReadPileupHistograms
def ReadPileupHistograms(dirName)
Definition: Final2012/ParsePileupInput.py:15