ATLAS Offline Software
Loading...
Searching...
No Matches
January2018_specialStatTerms/MakeNewFileFromOldAndSubstitution.py
Go to the documentation of this file.
1from ROOT import *
2from array import array
3import sys
4import os
5import glob
6import re
7import math
8
9from ParseCurrentFile import ReadCurrentHistograms
10
11
12#http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
13from itertools import groupby
14def natural_sort(s):
15 return [int(''.join(g)) if k else ''.join(g) for k, g in groupby(s[0], str.isdigit)]
16
17# Here, set file(s) from which to take replacement histograms and what names to switch
18jetreplacements = {"AntiKt4EMTopo":"AntiKt4Topo_EMJES"}
19replacements = {}
20replacements["/cluster/warehouse/kpachal/JetCalibration/JetUncertainties/JetUncertainties/inputs/PunchThrough/"] = {
21 "PunchThrough_MC15" : "PunchThrough_MC15",
22}
23
24# Here, specify histograms you want to get rid of altogether
25deletions = []
26
27# Check useage and store arguments
28if len(sys.argv) < 3:
29 print "USAGE: Expected the following arguments"
30 print " 1. Root file to use as template"
31 print " 2. Name of output root file to create"
32 exit(1)
33
34inputFile = sys.argv[1]
35outBaselineFile = sys.argv[2]
36
37# Ensure that all of the directories exist
38if not outBaselineFile.endswith(".root"):
39 print "Output ROOT file doesn't appear to be a root file:",outBaselineFile
40 exit(2)
41if not os.path.exists(inputFile):
42 print "Input file does not exist:",inputFile
43 exit(3)
44for filename in replacements.keys():
45 if not os.path.exists(filename) :
46 print "Replacement input file does not exist:",filename
47 exit(4)
48
49# Now read the histograms
50print "Reading inputs..."
51# For now, everything except flavour and cross calibration inputs
52# are read in from the final 2012 calibration files.
53# Input file and jet types
54theCurrentHistos = ReadCurrentHistograms(inputFile,jetreplacements.keys())
55
56import ParseFlavourInput
57import ParseNonClosureInput
58import ParsePunchthroughInput
59
60theReplacementHistos = {}
61for filename in replacements.keys() :
62 if "flavor" in filename or "Flavor" in filename or "flavour" in filename or "Flavour" in filename :
64 elif "AFII_Uncertainty" in filename :
66 print "Just retrieved",thisset
67 elif "PunchThrough" in filename :
69 else :
70 thisset = ReadCurrentHistograms(filename,jetreplacements.keys())
71 print thisset
72 theReplacementHistos.update(thisset)
73
74# Make one mega-dictionary
75print "Merging inputs..."
76jetDefs = ["AntiKt4EMTopo"]#,"AntiKt4Topo_LCJES","AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
77systematics = {}
78validities = {}
79for aJetDef in jetDefs :
80 systematics[aJetDef] = dict(
81 theCurrentHistos[aJetDef].items()
82 )
83
84# Loop over the mega-dictionary and write results
85print "Writing to output file",outBaselineFile,"..."
86baselineFile = TFile(outBaselineFile,"RECREATE")
87for aJetDef,aSyst in sorted(systematics.iteritems(),key=natural_sort):
88 for aSystName,aSystHisto in sorted(aSyst.iteritems(),key=natural_sort):
89 baselineFile.cd()
90 for filename in replacements.keys() :
91 if aSystName in replacements[filename].keys() :
92 print "Writing new version of",aSystName
93 newName = replacements[filename][aSystName]
94 newHist = theReplacementHistos[jetreplacements[aJetDef]][newName]
95 newHist.SetDirectory(0)
96 newHist.SetTitle(aSystName+"_"+aJetDef)
97 print "Giving it name",newHist.GetTitle()
98 newHist.Write(newHist.GetTitle())
99 elif aSystName in deletions :
100 print "Deleting histogram",aSystName
101 continue
102 else :
103 print "Keeping original version of",aSystName
104 aSystHisto.SetTitle(aSystName+"_"+aJetDef)
105 aSystHisto.Write(aSystHisto.GetTitle())
106
107# Done, close the files, revert directory
108baselineFile.Close()
109print "Done!"
110