ATLAS Offline Software
CombineRootFiles.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 
4 import sys
5 import re
6 import math
7 import os
8 from ROOT import *
9 def GetKeyNames(self,dir=""):
10  self.cd(dir)
11  return [key.GetName() for key in gDirectory.GetListOfKeys()]
12 TFile.GetKeyNames = GetKeyNames
13 
14 
15 if 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 
21 outFileName = sys.argv[1]
22 inFileNames = sys.argv[2:]
23 
24 if 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)
28 for 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 
38 outFile = TFile(outFileName,"RECREATE")
39 
40 # Store the names of the histograms we are adding to enforce uniqueness
41 uniqueHistos = {}
42 
43 for 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 
72 outFile.Close()
73 
calibdata.exit
exit
Definition: calibdata.py:236
CombineRootFiles.GetKeyNames
def GetKeyNames(self, dir="")
Definition: CombineRootFiles.py:9