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 from __future__ import print_function
12 import os,glob,argparse,ROOT,time
13 
14 start = time.clock()
15 
16 parser = argparse.ArgumentParser(description='Merge specific folder(s) in root files.')
17 parser.add_argument("-i", "--input", help="path to the folder holding the samples (default: ./)", default=os.getcwd())
18 parser.add_argument("-o", "--output", help="path for the output (default: ./merge.root", default=os.getcwd()+"/merge.root")
19 parser.add_argument("-d", "--dir", nargs='+', help="ROOT directory to be merged, multiple arguments are possible (default: Summary)", default=["Summary"])
20 parser.add_argument("-p", "--pattern", help='pattern of files to merge (default: "*PHYSVAL*")', default="*PHYSVAL*")
21 args = parser.parse_args()
22 folder = os.path.abspath(args.input)
23 mergeDirs = args.dir
24 origDir = os.getcwd()
25 out = args.output
26 pattern = args.pattern
27 output_file = os.path.abspath(args.output)
28 os.chdir(folder)
29 
30 files = glob.glob(folder + "/" + pattern)
31 
32 f = ROOT.TFile(output_file, "recreate")
33 folder = os.getcwd()
34 f2 = ROOT.TFile(files[1])
35 
36 print("Target file: " + output_file)
37 for infile in files:
38  print("Found input file: " + infile)
39  if os.path.samefile(output_file, infile):
40  print("Please make sure that the output file is not part of the input files! Stopping.")
41  quit()
42 
43 errors = []
44 
45 def mergeFolder(path) :
46  print("Merging folder " + path)
47  d = f2.Get(path)
48  if not d:
49  error = "ERROR: Cannot find directory " + path + ". Omitting."
50  print(error)
51  errors.append(error)
52  return
53  dirlist = d.GetListOfKeys()
54  for subdir in dirlist:
55  obj = subdir.ReadObj()
56  if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
57  print("Now merging "+obj.GetName())
58  h1 = obj
59  hpath = d.GetPath()
60  hname = hpath[hpath.find(":")+2:]+"/"+obj.GetName()
61  print("Path: "+hname)
62  for tup in files:
63  if tup==files[1]: continue
64  nextfile = ROOT.TFile(tup)
65  h2 = nextfile.Get(hname)
66  if not h2:
67  error = "ERROR: Cannot find " + hname + " in file " + tup + ". Omitting."
68  print(error)
69  errors.append(error)
70  continue
71  h1.Add(h2)
72  subfolder = f.Get(hpath[hpath.find(":")+2:])
73  subfolder.cd()
74  h1.Write()
75  if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
76  print("Found subdirectory "+obj.GetName())
77  hpath = obj.GetPath()
78  subfolder = f.mkdir(hpath[hpath.find(":")+2:],obj.GetTitle())
79  subfolder.cd()
80  mergeFolder(hpath[hpath.find(":")+2:])
81 
82 for mergeDir in mergeDirs:
83  newfolder = f.mkdir(mergeDir,mergeDir)
84  ROOT.TH1.AddDirectory(False)
85  mergeFolder(mergeDir)
86 
87 f.Close()
88 if len(errors)>0:
89  print("Summary of all errors:")
90  for phrase in errors:
91  print(phrase)
92 
93 end = time.clock()
94 print("Wall time used: %s sec" % (end - start))
mergePhysValFiles.mergeFolder
def mergeFolder(path)
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:45
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28