11 from __future__
import print_function
12 import getopt,os,sys,glob,argparse,ROOT,time
13 from ROOT
import gDirectory
15 start = time.process_time()
34 restCategory =
'other'
37 categories_with_subcategories_type_1 = [
'SV',
'tracks']
39 sub_categories_type_1 = [
'_incl',
46 categories_with_subcategories_type_2 = [
'tagger_IP3D',
'tagger_RNNIP',
'tagger_DIPS',
'tagger_SV1',
'tagger_DL1dv01',
'tagger_DL1r',
'tagger_GN2v01',
'tagger_GN2Xv01']
48 sub_categories_type_2 = [
'_pt_ttbar',
53 categories_with_subcategories_type_3 = [
'old_taggers']
55 sub_categories_type_3 = [
'_IP2D',
58 categories_with_subcategories_type_4 = [
'jet']
60 sub_categories_type_4 = [
'jet']
63 jetcontainers = [
'AntiKt4EMTopoJets',
65 'AntiKt10UFOCSSKSoftDropBeta100Zcut10Jets',
66 'AntiKtVR30Rmax4Rmin02PV0TrackJets',
70 parser = argparse.ArgumentParser(description=
'Merge specific folder(s) in root files.')
71 parser.add_argument(
"-i",
"--input", help=
"path to the folder holding the samples (default: ./)", default=os.getcwd())
72 parser.add_argument(
"-o",
"--output", help=
"path for the output (default: ./merge.root", default=os.getcwd()+
"/merge.root")
73 parser.add_argument(
"-d",
"--dir", nargs=
'+', help=
"ROOT directory to be merged, multiple arguments are possible (default: Summary)", default=[
"Summary"])
74 parser.add_argument(
"-p",
"--pattern", help=
'pattern of files to merge (default: "*PHYSVAL*")', default=
"*PHYSVAL*")
75 args = parser.parse_args()
76 folder = os.path.abspath(args.input)
80 pattern = args.pattern
81 output_file = os.path.abspath(args.output)
84 files = glob.glob(folder +
"/" + pattern)
87 f = ROOT.TFile(output_file,
"recreate")
89 f2 = ROOT.TFile(files[0])
92 print(
"Target file: " + output_file)
94 print(
"Found input file: " + infile)
95 if os.path.samefile(output_file, infile):
96 print(
"Please make sure that the output file is not part of the input files! Stopping.")
107 print(
"Merging folder " + path)
110 error =
"ERROR: Cannot find directory " + path +
". Omitting."
114 dirlist = d.GetListOfKeys()
117 for jetcont
in jetcontainers:
119 currentdir = gDirectory.GetPath()
121 print(
"Create directory " + path +
"/other_histograms/histos")
122 tagfolders[path+
"/"+restCategory] = f.mkdir(path+
"/other_histograms/histos")
124 for category
in categories:
125 print(
"Create directory " + path +
"/" + category)
126 tagfolders[path+
"/"+category] = f.mkdir(path+
"/"+category)
127 if category
in categories_with_subcategories_type_1:
128 for sub_category
in sub_categories_type_1:
129 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
130 elif category
in categories_with_subcategories_type_2:
131 for sub_category
in sub_categories_type_2:
132 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
133 elif category
in categories_with_subcategories_type_3:
134 for sub_category
in sub_categories_type_3:
135 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
136 elif category
in categories_with_subcategories_type_4:
137 for sub_category
in sub_categories_type_4:
138 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
139 tagfolders[path+
"/"+category+
"/"+restCategory] = f.mkdir(path+
"/"+category+
"/"+restCategory)
140 gDirectory.cd(currentdir)
142 for subdir
in dirlist:
143 obj = subdir.ReadObj()
145 if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
146 print(
"Now merging "+obj.GetName())
149 hname = hpath[hpath.find(
":")+2:]+
"/"+obj.GetName()
150 print(
"Path: "+hname)
153 if tup==files[0]:
continue
154 nextfile = ROOT.TFile(tup)
155 h2 = nextfile.Get(hname)
157 error =
"ERROR: Cannot find " + hname +
" in file " + tup +
". Omitting."
163 for category
in reversed(categories):
165 if (
"_"+category+
"_")
in obj.GetName():
166 print(
"Category: " + category)
167 subfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category)
170 should_be_in_a_subcategory =
False
171 is_in_subcategory =
False
172 if category
in categories_with_subcategories_type_1:
173 should_be_in_a_subcategory =
True
174 for sub_category
in reversed(sub_categories_type_1):
175 if(sub_category
in obj.GetName()):
176 is_in_subcategory =
True
177 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
180 elif category
in categories_with_subcategories_type_2:
181 should_be_in_a_subcategory =
True
182 for sub_category
in reversed(sub_categories_type_2):
183 if(sub_category
in obj.GetName()):
184 is_in_subcategory =
True
185 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
188 elif category
in categories_with_subcategories_type_3:
189 should_be_in_a_subcategory =
True
190 for sub_category
in reversed(sub_categories_type_3):
191 if(sub_category
in obj.GetName()):
192 is_in_subcategory =
True
193 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
196 elif category
in categories_with_subcategories_type_4:
197 should_be_in_a_subcategory =
True
198 for sub_category
in reversed(sub_categories_type_4):
199 if(sub_category
in obj.GetName()):
200 is_in_subcategory =
True
201 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
204 if should_be_in_a_subcategory
and not is_in_subcategory:
205 rest_subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+restCategory)
206 rest_subsubfolder.cd()
211 subfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/other_histograms/histos")
214 print(gDirectory.GetPath())
217 if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
218 print(
"Found subdirectory "+obj.GetName())
219 hpath = obj.GetPath()
220 subfolder = f.mkdir(hpath[hpath.find(
":")+2:],obj.GetTitle())
221 print(
"Created new output directory " + hpath[hpath.find(
":")+2:])
227 for mergeDir
in mergeDirs:
228 newfolder = f.mkdir(mergeDir,mergeDir)
229 ROOT.TH1.AddDirectory(
False)
235 print(
"Summary of all errors:")
236 for phrase
in errors:
239 end = time.process_time()
240 print(
"Wall time used: %s sec" % (end - start))