ATLAS Offline Software
Loading...
Searching...
No Matches
SwapComponents.py
Go to the documentation of this file.
1# Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
2
3import sys
4import re
5from ROOT import *
6
7def GetKeyNames(self,dir=""):
8 self.cd(dir)
9 return [key.GetName() for key in gDirectory.GetListOfKeys()]
10TFile.GetKeyNames = GetKeyNames
11
12
13if 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
21outFile = TFile.Open(sys.argv[1],"RECREATE")
22primaryFile = TFile.Open(sys.argv[2],"READ")
23updateFile = TFile.Open(sys.argv[3],"READ")
24addNew = 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
28updates = {}
29for histName in updateFile.GetKeyNames():
30 hist = updateFile.Get(histName)
31 hist.SetDirectory(0)
32 updates[histName] = hist
33updateFile.Close()
34
35# Check that we can find the components to replace
36newHists = []
37for 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
46for 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
57if 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
65outFile.Close()
66primaryFile.Close()
67