ATLAS Offline Software
DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 #----------------------------------------------------------------------
6 #stand-alone script to merge specific directories of NTUP_PHYSVAL files
7 #author: philipp.mogg@cern.ch
8 #16 May 2016
9 #----------------------------------------------------------------------
10 
11 import os,glob,argparse,ROOT,time
12 
13 start = time.clock()
14 
15 parser = argparse.ArgumentParser(description='Merge specific folder(s) in root files.')
16 parser.add_argument("-i", "--input", help="path to the folder holding the samples (default: ./)", default=os.getcwd())
17 parser.add_argument("-o", "--output", help="path for the output (default: ./merge.root", default=os.getcwd()+"/merge.root")
18 parser.add_argument("-d", "--dir", nargs='+', help="ROOT directory to be merged, multiple arguments are possible (default: Summary)", default=["Summary"])
19 parser.add_argument("-p", "--pattern", help='pattern of files to merge (default: "*PHYSVAL*")', default="*PHYSVAL*")
20 args = parser.parse_args()
21 folder = os.path.abspath(args.input)
22 mergeDirs = args.dir
23 origDir = os.getcwd()
24 out = args.output
25 pattern = args.pattern
26 output_file = os.path.abspath(args.output)
27 os.chdir(folder)
28 
29 files = glob.glob(folder + "/" + pattern)
30 
31 f = ROOT.TFile(output_file, "recreate")
32 folder = os.getcwd()
33 f2 = ROOT.TFile(files[1])
34 
35 print("Target file: " + output_file)
36 for infile in files:
37  print("Found input file: " + infile)
38  if os.path.samefile(output_file, infile):
39  print("Please make sure that the output file is not part of the input files! Stopping.")
40  quit()
41 
42 errors = []
43 
44 def mergeFolder(path) :
45  print("Merging folder " + path)
46  d = f2.Get(path)
47  if not d:
48  error = "ERROR: Cannot find directory " + path + ". Omitting."
49  print(error)
50  errors.append(error)
51  return
52  dirlist = d.GetListOfKeys()
53  for subdir in dirlist:
54  obj = subdir.ReadObj()
55  if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
56  print("Now merging "+obj.GetName())
57  h1 = obj
58  hpath = d.GetPath()
59  hname = hpath[hpath.find(":")+2:]+"/"+obj.GetName()
60  print("Path: "+hname)
61  for tup in files:
62  if tup==files[1]: continue
63  nextfile = ROOT.TFile(tup)
64  h2 = nextfile.Get(hname)
65  if not h2:
66  error = "ERROR: Cannot find " + hname + " in file " + tup + ". Omitting."
67  print(error)
68  errors.append(error)
69  continue
70  h1.Add(h2)
71  subfolder = f.Get(hpath[hpath.find(":")+2:])
72  subfolder.cd()
73  h1.Write()
74  if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
75  print("Found subdirectory "+obj.GetName())
76  hpath = obj.GetPath()
77  subfolder = f.mkdir(hpath[hpath.find(":")+2:],obj.GetTitle())
78  subfolder.cd()
79  mergeFolder(hpath[hpath.find(":")+2:])
80 
81 for mergeDir in mergeDirs:
82  newfolder = f.mkdir(mergeDir,mergeDir)
83  ROOT.TH1.AddDirectory(False)
84  mergeFolder(mergeDir)
85 
86 f.Close()
87 if len(errors)>0:
88  print("Summary of all errors:")
89  for phrase in errors:
90  print(phrase)
91 
92 end = time.clock()
93 print("Wall time used: %s sec" % (end - start))
mergePhysValFiles.mergeFolder
def mergeFolder(path)
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:44
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25