11 from __future__
import print_function
12 import os,glob,argparse,ROOT,time
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)
26 pattern = args.pattern
27 output_file = os.path.abspath(args.output)
30 files = glob.glob(folder +
"/" + pattern)
32 f = ROOT.TFile(output_file,
"recreate")
34 f2 = ROOT.TFile(files[1])
36 print(
"Target file: " + output_file)
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.")
46 print(
"Merging folder " + path)
49 error =
"ERROR: Cannot find directory " + path +
". Omitting."
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())
60 hname = hpath[hpath.find(
":")+2:]+
"/"+obj.GetName()
63 if tup==files[1]:
continue
64 nextfile = ROOT.TFile(tup)
65 h2 = nextfile.Get(hname)
67 error =
"ERROR: Cannot find " + hname +
" in file " + tup +
". Omitting."
72 subfolder = f.Get(hpath[hpath.find(
":")+2:])
75 if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
76 print(
"Found subdirectory "+obj.GetName())
78 subfolder = f.mkdir(hpath[hpath.find(
":")+2:],obj.GetTitle())
82 for mergeDir
in mergeDirs:
83 newfolder = f.mkdir(mergeDir,mergeDir)
84 ROOT.TH1.AddDirectory(
False)
89 print(
"Summary of all errors:")
94 print(
"Wall time used: %s sec" % (end - start))