ATLAS Offline Software
Loading...
Searching...
No Matches
Prerec2012/MakeNewFileFromOldAndSubstitution.py
Go to the documentation of this file.
1# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2
3from ROOT import *
4from array import array
5import sys
6import os
7import glob
8import re
9import math
10
11from ParseCurrentFile import ReadCurrentHistograms
12
13
14#http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
15from itertools import groupby
16def 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
20jetreplacements = {"AntiKt4EMTopo":"AntiKt4Topo_EMJES","AntiKt4LCTopo":"AntiKt4Topo_LCJES"}
21replacements = {}
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#}
31replacements["/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
36deletions = ["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
46if 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
52inputFile = sys.argv[1]
53outBaselineFile = sys.argv[2]
54
55# Ensure that all of the directories exist
56if not outBaselineFile.endswith(".root"):
57 print "Output ROOT file doesn't appear to be a root file:",outBaselineFile
58 exit(2)
59if not os.path.exists(inputFile):
60 print "Input file does not exist:",inputFile
61 exit(3)
62for 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
68print "Reading inputs..."
69# For now, everything except flavour and cross calibration inputs
70# are read in from the final 2012 calibration files.
71theCurrentHistos = ReadCurrentHistograms(inputFile,jetreplacements.keys()) # First true activates eta-intercalibration scaling. Second true turns off relative in situ components at large eta
72
73import ParseFlavourInput
74import ParseNonClosureInput
75theReplacementHistos = {}
76for filename in replacements.keys() :
77 if "flavor" in filename or "Flavor" in filename or "flavour" in filename or "Flavour" in filename :
79 elif "AFII_Uncertainty" in filename :
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
88print "Merging inputs..."
89jetDefs = ["AntiKt4EMTopo"]#,"AntiKt4Topo_LCJES","AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
90systematics = {}
91validities = {}
92for aJetDef in jetDefs :
93 systematics[aJetDef] = dict(
94 theCurrentHistos[aJetDef].items()
95 )
96
97# Loop over the mega-dictionary and write results
98print "Writing to output file",outBaselineFile,"..."
99baselineFile = TFile(outBaselineFile,"RECREATE")
100for 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
121baselineFile.Close()
122print "Done!"
123