ATLAS Offline Software
January2018_specialStatTerms/MakeNewFileFromOldAndSubstitution.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 ParseCurrentFile import ReadCurrentHistograms
10 
11 
12 #http://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
13 from itertools import groupby
14 def 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
18 jetreplacements = {"AntiKt4EMTopo":"AntiKt4Topo_EMJES"}
19 replacements = {}
20 replacements["/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
25 deletions = []
26 
27 # Check useage and store arguments
28 if 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 
34 inputFile = sys.argv[1]
35 outBaselineFile = sys.argv[2]
36 
37 # Ensure that all of the directories exist
38 if not outBaselineFile.endswith(".root"):
39  print "Output ROOT file doesn't appear to be a root file:",outBaselineFile
40  exit(2)
41 if not os.path.exists(inputFile):
42  print "Input file does not exist:",inputFile
43  exit(3)
44 for 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
50 print "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
54 theCurrentHistos = ReadCurrentHistograms(inputFile,jetreplacements.keys())
55 
56 import ParseFlavourInput
57 import ParseNonClosureInput
58 import ParsePunchthroughInput
59 
60 theReplacementHistos = {}
61 for filename in replacements.keys() :
62  if "flavor" in filename or "Flavor" in filename or "flavour" in filename or "Flavour" in filename :
63  thisset = ParseFlavourInput.ReadFlavourHistograms(filename)
64  elif "AFII_Uncertainty" in filename :
65  thisset = ParseNonClosureInput.ReadNonClosureHistograms(filename,True)
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
75 print "Merging inputs..."
76 jetDefs = ["AntiKt4EMTopo"]#,"AntiKt4Topo_LCJES","AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
77 systematics = {}
78 validities = {}
79 for aJetDef in jetDefs :
80  systematics[aJetDef] = dict(
81  theCurrentHistos[aJetDef].items()
82  )
83 
84 # Loop over the mega-dictionary and write results
85 print "Writing to output file",outBaselineFile,"..."
86 baselineFile = TFile(outBaselineFile,"RECREATE")
87 for 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
108 baselineFile.Close()
109 print "Done!"
110 
ParsePunchthroughInput.ReadPunchthroughHistograms
def ReadPunchthroughHistograms(dirName)
Definition: Final2012/ParsePunchthroughInput.py:31
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:798
ParseNonClosureInput.ReadNonClosureHistograms
def ReadNonClosureHistograms(dirName, freezepT=False)
Definition: ICHEP2016/ParseNonClosureInput.py:23