ATLAS Offline Software
SwapComponents.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
2 
3 import sys
4 import re
5 from ROOT import *
6 
7 def GetKeyNames(self,dir=""):
8  self.cd(dir)
9  return [key.GetName() for key in gDirectory.GetListOfKeys()]
10 TFile.GetKeyNames = GetKeyNames
11 
12 
13 if len(sys.argv) < 4:
14  print "Too few arguments. Expected the following:"
15  print " 1. Output file"
16  print " 2. Primary input file (to be overwritten in parts)"
17  print " 3. New input file (to use to overwrite parts)"
18  print " 4. Optional: whether to interpret new histograms (TRUE) as additions or to exit (FALSE)"
19  sys.exit(1)
20 
21 outFile = TFile.Open(sys.argv[1],"RECREATE")
22 primaryFile = TFile.Open(sys.argv[2],"READ")
23 updateFile = TFile.Open(sys.argv[3],"READ")
24 addNew = True if len(sys.argv) > 4 and (sys.argv[4] == "TRUE" or sys.argv[4] == "True" or sys.argv[4] == "true") else False
25 
26 
27 # Figure out which components we want to replace and store the corresponding histograms
28 updates = {}
29 for histName in updateFile.GetKeyNames():
30  hist = updateFile.Get(histName)
31  hist.SetDirectory(0)
32  updates[histName] = hist
33 updateFile.Close()
34 
35 # Check that we can find the components to replace
36 newHists = []
37 for histName in updates.keys():
38  if not histName in primaryFile.GetKeyNames():
39  if not addNew:
40  print "Failed to find the original histogram to replace for key =",histName
41  sys.exit(2)
42  else:
43  newHists.append(histName)
44 
45 # We found everything we want to replace, so do it
46 for histName in primaryFile.GetKeyNames():
47  hist = None
48  if histName in updates.keys():
49  print "Updating component:",histName
50  hist = updates[histName]
51  else:
52  hist = primaryFile.Get(histName)
53  outFile.cd()
54  hist.Write(histName)
55 
56 # Add new histograms if requested
57 if addNew:
58  for newHist in newHists:
59  print "Adding new component:",newHist
60  hist = updates[newHist]
61  outFile.cd()
62  hist.Write(newHist)
63 
64 
65 outFile.Close()
66 primaryFile.Close()
67 
SwapComponents.GetKeyNames
def GetKeyNames(self, dir="")
Definition: SwapComponents.py:7