ATLAS Offline Software
Loading...
Searching...
No Matches
CombineRootFiles.py
Go to the documentation of this file.
1# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2
3
4import sys
5import re
6import math
7import os
8from ROOT import *
9def GetKeyNames(self,dir=""):
10 self.cd(dir)
11 return [key.GetName() for key in gDirectory.GetListOfKeys()]
12TFile.GetKeyNames = GetKeyNames
13
14
15if len(sys.argv) < 3:
16 print "Too few arguments. Expected the following:"
17 print " 1. Output root file"
18 print " 2+ Input root files"
19 exit(1)
20
21outFileName = sys.argv[1]
22inFileNames = sys.argv[2:]
23
24if not outFileName.endswith(".root"):
25 print "Output file doesn't appear to be a root file:",outFile
26 print "Blocking for safety"
27 exit(2)
28for aFileName in inFileNames:
29 if not aFileName.endswith(".root"):
30 print "Input file doesn't appear to be a root file:",aFileName
31 print "Blocking for safety"
32 exit(3)
33 if not os.path.isfile(aFileName):
34 print "Input file doesn't exist:",aFileName
35 print "Blocking for safety"
36 exit(3)
37
38outFile = TFile(outFileName,"RECREATE")
39
40# Store the names of the histograms we are adding to enforce uniqueness
41uniqueHistos = {}
42
43for aFileName in inFileNames:
44 print "Reading file:",aFileName
45 aFile = TFile(aFileName,"READ")
46
47 for histName in aFile.GetKeyNames():
48 if histName not in uniqueHistos.keys():
49 hist = aFile.Get(histName)
50 outFile.cd()
51 hist.Write(histName)
52
53 # Store the histogram and name
54 hist.SetDirectory(0)
55 uniqueHistos[histName] = hist
56 else:
57 # Check if they are identical histograms
58 hist = aFile.Get(histName)
59 hist.Add(uniqueHistos[histName],-1)
60 areDifferent = ( math.fabs(hist.GetMinimum()) > 1.e-8 or math.fabs(hist.GetMaximum()) > 1.e-8 )
61
62 if areDifferent:
63 print "[ERROR] Two non-identical histograms with the same name were provided: %s"%(histName)
64 print "Aborting..."
65 exit(1)
66 else:
67 print "[SAFE] Skipping non-unique histogram name which is identical to the old one: %s"%(histName)
68
69
70 aFile.Close()
71
72outFile.Close()
73