ATLAS Offline Software
ICHEP2016/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"}
21 replacements = {}
22 replacements["/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
27 deletions = []
28 
29 # Check useage and store arguments
30 if 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 
36 inputFile = sys.argv[1]
37 outBaselineFile = sys.argv[2]
38 
39 # Ensure that all of the directories exist
40 if not outBaselineFile.endswith(".root"):
41  print "Output ROOT file doesn't appear to be a root file:",outBaselineFile
42  exit(2)
43 if not os.path.exists(inputFile):
44  print "Input file does not exist:",inputFile
45  exit(3)
46 for 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
52 print "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
56 theCurrentHistos = ReadCurrentHistograms(inputFile,jetreplacements.keys())
57 
58 import ParseFlavourInput
59 import ParseNonClosureInput
60 import ParsePunchthroughInput
61 
62 theReplacementHistos = {}
63 for 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 :
67  thisset = ParseNonClosureInput.ReadNonClosureHistograms(filename,True)
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
77 print "Merging inputs..."
78 jetDefs = ["AntiKt4EMTopo"]#,"AntiKt4Topo_LCJES","AntiKt6Topo_EMJES","AntiKt6Topo_LCJES"]
79 systematics = {}
80 validities = {}
81 for aJetDef in jetDefs :
82  systematics[aJetDef] = dict(
83  theCurrentHistos[aJetDef].items()
84  )
85 
86 # Loop over the mega-dictionary and write results
87 print "Writing to output file",outBaselineFile,"..."
88 baselineFile = TFile(outBaselineFile,"RECREATE")
89 for 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
110 baselineFile.Close()
111 print "Done!"
112 
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