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