ATLAS Offline Software
CombineRootAndConfigFilesAfterReduction.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 #/bin/python
4 
5 import sys
6 import re
7 import datetime
8 import calendar
9 from ROOT import *
10 
11 def GetKeyNames(self,dir=""):
12  self.cd(dir)
13  return [key.GetName() for key in gDirectory.GetListOfKeys()]
14 
15 #Copy everything from the reduced file to the output file
16 #Copy everything excluding any overlap with the reduced file from the full file to the output file
17 def CombineRootFiles(fullFileName,reducedFileName,combinedFileName):
18  print "Creating reduced root file..."
19  print "Full file: " + fullFileName
20  print "Reduced file: " + reducedFileName
21  print "Output file: " + combinedFileName
22  AllNuisanceParametersFile = TFile.Open(fullFileName,"READ")
23  ReducedNuisanceParametersFile = TFile.Open(reducedFileName,"READ")
24  OutputFile = TFile.Open(combinedFileName,"RECREATE")
25 
26  for histName in AllNuisanceParametersFile.GetKeyNames():
27  hist = AllNuisanceParametersFile.Get(histName)
28  OutputFile.cd()
29  hist.Write(histName)
30 
31  reducedComponentNames = []
32  for histName in ReducedNuisanceParametersFile.GetKeyNames():
33  if "EffectiveNP_" in histName:
34  hist = ReducedNuisanceParametersFile.Get(histName)
35  OutputFile.cd()
36  hist.Write(histName)
37  reducedComponentNames.append(histName)
38 
39  ''' This method is no longer relevant due to Reduction changes, retained in case this changes
40 
41  for histName in ReducedNuisanceParametersFile.GetKeyNames():
42  hist = ReducedNuisanceParametersFile.Get(histName)
43  OutputFile.cd()
44  hist.Write(histName)
45  reducedComponentNames.append(histName)
46 
47  for histName in AllNuisanceParametersFile.GetKeyNames():
48  alreadyCopied = False
49  for aComponent in reducedComponentNames:
50  if histName in aComponent:
51  alreadyCopied = True
52  break
53  if not alreadyCopied:
54  hist = AllNuisanceParametersFile.Get(histName)
55  OutputFile.cd()
56  hist.Write(histName)
57  '''
58 
59  AllNuisanceParametersFile.Close()
60  ReducedNuisanceParametersFile.Close()
61  OutputFile.Close()
62 
63  return reducedComponentNames
64 
65 # Needed due to end of line differences between OS
67  line = file.readline()
68  sanitizedLine = re.sub('\r','',line)
69 # line = re.sub('\n','',sanitizedLine)
70 # sanitizedLine = line+"\n\r"
71  return sanitizedLine
72 
74  sanitizedName = re.sub(';1','',aName)
75  sanitizedName = re.sub('_AntiKt4Topo_EMJES','',sanitizedName)
76  sanitizedName = re.sub('_AntiKt6Topo_EMJES','',sanitizedName)
77  sanitizedName = re.sub('_AntiKt4Topo_LCJES','',sanitizedName)
78  sanitizedName = re.sub('_AntiKt6Topo_LCJES','',sanitizedName)
79  return sanitizedName
80 
81 def getDescFromName(aName):
82  if "Stat" in aName:
83  return "Effective JES Statistical and Method Component %c"%(aName[-1])
84  elif "Detector" in aName:
85  return "Effective JES Detector Component %c"%(aName[-1])
86  elif "Model" in aName:
87  return "Effective JES Modelling Component %c"%(aName[-1])
88  elif "Mixed" in aName:
89  return "Effective JES Mixed Modelling and Detector Component %c"%(aName[-1])
90  elif "Special" in aName:
91  print "WARNING: Including a special component directly - %s"%(aName)
92  return "Effective JES Special Component %c"%(aName[-1])
93  elif "UNKNOWN" in aName:
94  print "WARNING: Including an unknown component - %s"%(aName)
95  return "Effecitve JES Unknown Category Component %c"%(aName[-1])
96  elif "EffectiveNP_" in aName:
97  if "restTerm" in aName:
98  return "Effective JES Uncertainty Component %c"%(aName[-9])
99  else:
100  return "Effective JES Uncertainty Component %c"%(aName[-1])
101  else:
102  print "ERROR: Including an unrecognized component"
103  exit(1)
104 
106  # Special for eta intercalibration components
107  if "etaintercalibration_totalstat" in aName.lower():
108  return "Statistical"
109  elif "etaintercalibration_modelling" in aName.lower():
110  return "Modelling"
111  elif "stat" in aName.lower():
112  return "Statistical"
113  elif "detector" in aName.lower():
114  return "Detector"
115  elif "model" in aName.lower():
116  return "Modelling"
117  elif "mixed" in aName.lower():
118  return "Mixed"
119  elif "special" in aName.lower():
120  return "Special"
121  elif "EffectiveNP_" in aName:
122  return "Special" # Global reduction parameters are of type "Special"
123  else:
124  return "UNKNOWN"
125 
127  for aName in listOfNames:
128  if "Stat" in aName:
129  return True
130  elif "Detector" in aName:
131  return True
132  elif "Model" in aName:
133  return True
134  elif "Mixed" in aName:
135  return True
136  return False
137 
138 def replaceComponentNumber(line,newNumber):
139  # Line is in the following form:
140  # JESComponent.#.etc
141  # Replace the first part
142  return re.sub("JESComponent.[0-9]*.","JESComponent.%d."%(newNumber),line)
143 
144 def CombineConfigFiles(fullFileName,combinedFileName,combinedRootFileName,componentNames):
145  print "Creating reduced config file..."
146 
147  # Open the input file for reading, output file for writing
148  inputFile = open(fullFileName,"r")
149  outputFile = open(combinedFileName,"w")
150 
151  # Determine the type of reduction (for the header)
152  reductionString = ""
153  if "GlobalReduction" in combinedFileName:
154  reductionString = "Reduction was applied globally"
155  elif "CategoryReduction" in combinedFileName:
156  reductionString = "Reduction was applied within each category"
157  else:
158  reductionString = "Unknown type of reduction"
159 
160  # Create the file header
161  now = datetime.datetime.now()
162  outputFile.write("\n#######################################\n#\n")
163  outputFile.write("# JESProvider Input Settings\n")
164  outputFile.write("# Reduced set of nuisance parameters from final 2011 iteration of the in-situ JES\n")
165  outputFile.write("# %s\n"%(reductionString))
166  #outputFile.write("# %s\n"%(fullFileName))
167  outputFile.write("# %s %d, %d\n#\n"%(calendar.month_name[now.month],now.day,now.year))
168  #outputFile.write("# \n") # AUTHORS HERE
169  outputFile.write("#\n#######################################\n\n")
170 
171  # Specify the root file that goes with the header
172  outputFile.write("JESUncertaintyRootFile: %s\n"%(combinedRootFileName))
173 
174  # Settings header
175  outputFile.write("\n#######################################\n#\n")
176  outputFile.write("# Settings for JES Components\n")
177  outputFile.write("#\n#######################################\n\n")
178 
179  # JES Components
180  # Parse the base names
181  compNamesSanitized = []
182  for aName in componentNames:
183  sanitizedName = sanitizeComponentName(aName)
184  if sanitizedName not in compNamesSanitized:
185  compNamesSanitized.append(sanitizedName)
186 
187  i=1
188  for aName in compNamesSanitized:
189  outputFile.write("JESComponent.%d.Desc: %s\n"%(i,getDescFromName(aName)))
190  outputFile.write("JESComponent.%d.Name: %s\n"%(i,aName))
191  outputFile.write("JESComponent.%d.Type: %s\n"%(i,getCategoryFromName(aName)))
192  outputFile.write("\n")
193  i = i + 1
194 
195  # Special components
196  # Currently, read from other config file
197  outputFile.write("\n#######################################\n\n")
198 
199  # Look for "Special" type components in the original file
200  # EOF returns an empty string
201  inputFile = open(fullFileName,"r")
202  nextLine = readSanitizedLine(inputFile)
203  currLine = ""
204  previousLine = ""
205  while (nextLine != ""):
206  if "Special" not in nextLine and "EtaIntercal" not in currLine:
207  # Not interesting (except for eta intercal), just iterate
208  previousLine = currLine
209  currLine = nextLine
210  nextLine = readSanitizedLine(inputFile)
211  else:
212  # Found a special component, so save it
213  outputFile.write(replaceComponentNumber(previousLine,i))
214  outputFile.write(replaceComponentNumber(currLine,i))
215  outputFile.write(replaceComponentNumber(nextLine,i))
216 
217  # Iterate
218  previousLine = currLine
219  currLine = nextLine
220  nextLine = readSanitizedLine(inputFile)
221 
222  # Check if sub-components or correlated sub-components
223  if ("SubComponent" in nextLine):
224  outputFile.write(replaceComponentNumber(nextLine,i))
225  previousLine = currLine
226  currLine = nextLine
227  nextLine = readSanitizedLine(inputFile)
228 
229  outputFile.write("\n")
230  i = i + 1
231 
232 
233  outputFile.write("\n#\n#######################################\n")
234 
235  # Done, close files
236  inputFile.close()
237  outputFile.close()
238  print "Created config file: %s\n"%(combinedFileName)
239 
240 # Main method here
241 if (len(sys.argv) < 7):
242  print "Too few arguments. Expected the following:"
243  print "\t1. Path to the JetUncertainties share directory"
244  print "\t2. Input (full) config file"
245  print "\t3. Input (full) root file"
246  print "\t4. Reduced root file (only effective nuisance parameters)"
247  print "\t5. Output config file"
248  print "\t6. Output root file"
249  exit(1)
250 
251 sharePath = sys.argv[1]
252 fullConfigFile = sys.argv[2]
253 fullRootFile = sys.argv[3]
254 reducedRootFile = sys.argv[4]
255 outConfigFile = sys.argv[5]
256 outRootFile = sys.argv[6]
257 
258 
259 TFile.GetKeyNames = GetKeyNames
260 
261 
262 compNames = CombineRootFiles(sharePath+"/"+fullRootFile,reducedRootFile,sharePath+"/"+outRootFile)
263 CombineConfigFiles(sharePath+"/"+fullConfigFile,sharePath+"/"+outConfigFile,outRootFile,compNames)
264 
265 
CombineRootAndConfigFilesAfterReduction.checkIfWasCategoryReduction
def checkIfWasCategoryReduction(listOfNames)
Definition: CombineRootAndConfigFilesAfterReduction.py:126
CombineRootAndConfigFilesAfterReduction.replaceComponentNumber
def replaceComponentNumber(line, newNumber)
Definition: CombineRootAndConfigFilesAfterReduction.py:138
CombineRootAndConfigFilesAfterReduction.getDescFromName
def getDescFromName(aName)
Definition: CombineRootAndConfigFilesAfterReduction.py:81
CombineRootAndConfigFilesAfterReduction.getCategoryFromName
def getCategoryFromName(aName)
Definition: CombineRootAndConfigFilesAfterReduction.py:105
CombineRootFiles
Definition: CombineRootFiles.py:1
CombineRootAndConfigFilesAfterReduction.sanitizeComponentName
def sanitizeComponentName(aName)
Definition: CombineRootAndConfigFilesAfterReduction.py:73
CombineRootAndConfigFilesAfterReduction.GetKeyNames
GetKeyNames
Definition: CombineRootAndConfigFilesAfterReduction.py:259
calibdata.exit
exit
Definition: calibdata.py:236
CombineRootAndConfigFilesAfterReduction.CombineRootFiles
def CombineRootFiles(fullFileName, reducedFileName, combinedFileName)
Definition: CombineRootAndConfigFilesAfterReduction.py:17
CombineRootAndConfigFilesAfterReduction.CombineConfigFiles
def CombineConfigFiles(fullFileName, combinedFileName, combinedRootFileName, componentNames)
Definition: CombineRootAndConfigFilesAfterReduction.py:144
CombineRootAndConfigFilesAfterReduction.readSanitizedLine
def readSanitizedLine(file)
Definition: CombineRootAndConfigFilesAfterReduction.py:66
Trk::open
@ open
Definition: BinningType.h:40