ATLAS Offline Software
Prerec2012/MakeNewFileFromOldAndSubstitution.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 ParseCurrentFile import ReadCurrentHistograms
12 
13 
14 #http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
15 from itertools import groupby
16 def natural_sort(s):
17  return [int(''.join(g)) if k else ''.join(g) for k, g in groupby(s[0], str.isdigit)]
18 
19 # Here, set file(s) from which to take replacement histograms and what names to switch
20 jetreplacements = {"AntiKt4EMTopo":"AntiKt4Topo_EMJES","AntiKt4LCTopo":"AntiKt4Topo_LCJES"}
21 replacements = {}
22 #replacements["/cluster/warehouse/kpachal/JetCalibration/JetUncertainties/JetUncertainties/inputs/EtaIntercalibration/Aug_11_EtaIntercalibration13TeV50ns_MM_nominal_AntiKt4EMTopo_Uncertainties.root"] = {
23 # "EtaIntercalibration_Modelling" : "EtaIntercalibration_TotalSyst",
24 # "EtaIntercalibration_TotalStat" : "EtaIntercalibration_TotalStat"
25 # }
26 #replacements["/cluster/warehouse/kpachal/JetCalibration/JetUncertainties/JetUncertainties/inputs/Flavour/flavorUncertaintiesMC15_v01.root"] = {
27 # "flavorCompGlu" : "flavorCompGlu",
28 # "flavorCompLight" : "flavorCompLight",
29 # "FlavorResponse" : "FlavorResponse"
30 #}
31 replacements["/cluster/warehouse/kpachal/JetCalibration/JetUncertainties/JetUncertainties/inputs/AFII/AFII_Uncertainty_4EM.root"] = {
32  "RelativeNonClosure_AFII" : "RelativeNonClosure_AFII",
33 }
34 
35 # Here, specify histograms you want to get rid of altogether
36 deletions = ["Xcalib_5Vs4sample","Xcalib_50nsVs25ns",
37 "Xcalib_50nsVs25ns_Central",
38 "Xcalib_50nsVs25ns_Forward",
39 "Xcalib_EarlyData",
40 "Xcalib_EarlyData_Forward",
41 "Xcalib_NoiseThreshold",
42 "Xcalib_TopoClustering",
43 "Xcalib_UnderlyingEvent"]
44 
45 # Check useage and store arguments
46 if len(sys.argv) < 3:
47  print "USAGE: Expected the following arguments"
48  print " 1. Root file to use as template"
49  print " 2. Name of output root file to create"
50  exit(1)
51 
52 inputFile = sys.argv[1]
53 outBaselineFile = sys.argv[2]
54 
55 # Ensure that all of the directories exist
56 if not outBaselineFile.endswith(".root"):
57  print "Output ROOT file doesn't appear to be a root file:",outBaselineFile
58  exit(2)
59 if not os.path.exists(inputFile):
60  print "Input file does not exist:",inputFile
61  exit(3)
62 for filename in replacements.keys():
63  if not os.path.exists(filename) :
64  print "Replacement input file does not exist:",filename
65  exit(4)
66 
67 # Now read the histograms
68 print "Reading inputs..."
69 # For now, everything except flavour and cross calibration inputs
70 # are read in from the final 2012 calibration files.
71 theCurrentHistos = ReadCurrentHistograms(inputFile,jetreplacements.keys()) # First true activates eta-intercalibration scaling. Second true turns off relative in situ components at large eta
72 
73 import ParseFlavourInput
74 import ParseNonClosureInput
75 theReplacementHistos = {}
76 for filename in replacements.keys() :
77  if "flavor" in filename or "Flavor" in filename or "flavour" in filename or "Flavour" in filename :
78  thisset = ParseFlavourInput.ReadFlavourHistograms(filename)
79  elif "AFII_Uncertainty" in filename :
80  thisset = ParseNonClosureInput.ReadNonClosureHistograms(filename,True)
81  print "Just retrieved",thisset
82  else :
83  thisset = ReadCurrentHistograms(filename,jetreplacements.keys())
84  print thisset
85  theReplacementHistos.update(thisset)
86 
87 # Make one mega-dictionary
88 print "Merging inputs..."
89 jetDefs = ["AntiKt4EMTopo"]#,"AntiKt4Topo_LCJES","AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
90 systematics = {}
91 validities = {}
92 for aJetDef in jetDefs :
93  systematics[aJetDef] = dict(
94  theCurrentHistos[aJetDef].items()
95  )
96 
97 # Loop over the mega-dictionary and write results
98 print "Writing to output file",outBaselineFile,"..."
99 baselineFile = TFile(outBaselineFile,"RECREATE")
100 for aJetDef,aSyst in sorted(systematics.iteritems(),key=natural_sort):
101  for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
102  baselineFile.cd()
103  for filename in replacements.keys() :
104  if aSystName in replacements[filename].keys() :
105  print "Writing new version of",aSystName
106  newName = replacements[filename][aSystName]
107  newHist = theReplacementHistos[jetreplacements[aJetDef]][newName]
108  newHist.SetDirectory(0)
109  newHist.SetTitle(aSystName+"_"+aJetDef)
110  print "Giving it name",newHist.GetTitle()
111  newHist.Write(newHist.GetTitle())
112  elif aSystName in deletions :
113  print "Deleting histogram",aSystName
114  continue
115  else :
116  print "Keeping original version of",aSystName
117  aSystHisto.SetTitle(aSystName+"_"+aJetDef)
118  aSystHisto.Write(aSystHisto.GetTitle())
119 
120 # Done, close the files, revert directory
121 baselineFile.Close()
122 print "Done!"
123 
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ParseCurrentFile.ReadCurrentHistograms
def ReadCurrentHistograms(aSystFile, newjetsdict=[])
Definition: ICHEP2016/ParseCurrentFile.py:15
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.
ParseFlavourInput.ReadFlavourHistograms
def ReadFlavourHistograms(dirName, freezepT=False)
Definition: ICHEP2016/ParseFlavourInput.py:23
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
MakeNewFileFromOldAndSubstitution.natural_sort
def natural_sort(s)
Definition: ICHEP2016/MakeNewFileFromOldAndSubstitution.py:16
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
ParseNonClosureInput.ReadNonClosureHistograms
def ReadNonClosureHistograms(dirName, freezepT=False)
Definition: ICHEP2016/ParseNonClosureInput.py:23