ATLAS Offline Software
January2018_specialStatTerms/MakeFileForMJB.py
Go to the documentation of this file.
1 from ROOT import *
2 from array import array
3 import sys
4 import os
5 import glob
6 import re
7 import math
8 
9 from ParseEtaIntercalInput import ReadEtaIntercalibrationHistograms
10 from ParseInsituInput_MJB import ReadInSituHistograms
11 from ParsePileupInput import ReadPileupHistograms
12 from ParseFlavourInput import ReadFlavourHistograms
13 from ParsePunchthroughInput import ReadPunchthroughHistograms
14 from Parse2012Input import Read2012Histograms
15 
16 #http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
17 from itertools import groupby
18 def natural_sort(s):
19  return [int(''.join(g)) if k else ''.join(g) for k, g in groupby(s[0], str.isdigit)]
20 
21 # Design: This is to make a file entirely from scratch.
22 # All components are assumed available in new versions.
23 # If that is not the case borrow parallel script from
24 # the Moriond 2016 directory (where punchthrough was kept from 2012)
25 
26 # Full list of required inputs, distinguished by needing them
27 # from a different person
28 # - In situ terms
29 # - Eta intercalibration
30 # - Pileup
31 # - Flavour
32 # - Punchthrough
33 
34 
35 # NOTES
36 # For this Moriond release, all MC histograms being kept from 20.1 as no changes observed to MC.
37 # Thus flavour histograms already validated at an earlier stage.
38 # Things below were done in pre-recs and are currently being done here:
39 # - 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.
40 # - FlavorResponse histogram inverted from input file to match Run I convention
41 freezeFlavourInPt = False
42 # Punchthrough needs to be checked to ensure axes are where we think they are
43 # Eta intercalibration currently includes TWO non-closure terms:
44 # - one with all bins used
45 # - one with only bins in 2.0 to 2.6 used (consider this an OFC uncertainty)
46 # Pileup fixed <mu> and <NPV> values are being kept from 2015 at this stage.
47 # In situ terms:
48 # - Using OOC from DB for MPF Z+jet results
49 # - Using small value MC generator uncertainty in Z+jets
50 # essentially due to how close nominal and alternate
51 # samples are. Not prioritising rebinned version, since fluctuations
52 # appear statistical.
53 
54 
55 
56 # Check useage and store arguments
57 if len(sys.argv) < 3:
58  print "USAGE: Expected the following arguments"
59  print " 1. Directory path to V+jet calibrations"
60  print " 2. Eta intercalibration directory path"
61  print " 3. Pileup directory path"
62  print " 5. Flavour directory path"
63  print " 6. Punchthrough directory path"
64  exit(1)
65 
66 outBaselineFile = "JESUncertainty_forMJB.root"
67 theVPlusJetsDir = sys.argv[1]
68 etaIntercalDir = sys.argv[2]
69 pileupDir = sys.argv[3]
70 flavourDir = sys.argv[4]
71 punchthroughDir = sys.argv[5]
72 
73 # Ensure that all of the directories exist
74 if not outBaselineFile.endswith(".root"):
75  print "Output baseline ROOT file doesn't appear to be a root file:",outBaselineFile
76  exit(2)
77 if not os.path.exists(theVPlusJetsDir):
78  print "V+jets directory does not exist:",theVPlusJetsDir
79  exit(3)
80 if not os.path.exists(etaIntercalDir):
81  print "Eta intercalibration directory does not exist:",etaIntercalDir
82  exit(4)
83 if not os.path.exists(pileupDir):
84  print "Pileup directory does not exist:",pileupDir
85  exit(5)
86 if not os.path.exists(flavourDir):
87  print "Flavour directory does not exist:",flavourDir
88  exit(6)
89 if not os.path.exists(punchthroughDir):
90  print "Punchthrough directory does not exist:",punchthroughDir
91  exit(7)
92 
93 
94 # Store everything in memory!
95 currentDir = gDirectory
96 gROOT.cd()
97 
98 # Now read the histograms
99 print "Reading inputs..."
100 # For now, everything except flavour and cross calibration inputs
101 # are read in from the final 2012 calibration files.
102 theVPlusJetsHistos = ReadInSituHistograms(theVPlusJetsDir) # Check!
103 etaIntercalHistos = ReadEtaIntercalibrationHistograms(etaIntercalDir)
104 pileupHistos = ReadPileupHistograms(pileupDir)
105 
106 flavourHistos = ReadFlavourHistograms(flavourDir,freezeFlavourInPt) # True flag freezes uncertainty above pT = 2TeV (lower at higher eta)
107 
108 punchthroughHistos = ReadPunchthroughHistograms(punchthroughDir)
109 
110 # Make one mega-dictionary
111 print "Merging inputs..."
112 jetDefs = {"AntiKt4Topo_EMJES" : "AntiKt4EMTopo", "AntiKt4PFlow_EMJES" : "AntiKt4EMPFlow"}
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  print dictionary
125  print aJetDef
126  systematics[aJetDef].update(dictionary[aJetDef].items())
127 
128  print systematics[aJetDef]
129 
130 
131 # Loop over the mega-dictionary and write results
132 print "Writing to output file",outBaselineFile,"..."
133 baselineFile = TFile(outBaselineFile,"RECREATE")
134 for aJetDef,aSyst in sorted(systematics.iteritems(),key=natural_sort):
135  for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
136  baselineFile.cd()
137  aSystHisto.SetTitle(aSystName+"_"+jetDefs[aJetDef])
138  aSystHisto.Write(aSystHisto.GetTitle())
139 
140 # Done, close the files, revert directory
141 baselineFile.Close()
142 gDirectory = currentDir
143 print "Done!"
144 
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
WriteBchToCool.update
update
Definition: WriteBchToCool.py:67
ParsePileupInput.ReadPileupHistograms
def ReadPileupHistograms(dirName)
Definition: Final2012/ParsePileupInput.py:15