12import os,glob,argparse,ROOT,time
13from ROOT
import gDirectory
15start = time.process_time()
30categories_with_subcategories_type_1 = [
'SV',
'tracks']
32sub_categories_type_1 = [
'_incl',
39categories_with_subcategories_type_2 = [
'tagger_GN2v01',
'tagger_GN3XPV01']
41sub_categories_type_2 = [
'_pt_ttbar',
46categories_with_subcategories_type_3 = [
'old_taggers']
48sub_categories_type_3 = [ ]
50categories_with_subcategories_type_4 = [
'jet']
52sub_categories_type_4 = [
'jet']
55jetcontainers = [
'AntiKt4EMTopoJets',
57 'AntiKt10UFOCSSKSoftDropBeta100Zcut10Jets',
61parser = argparse.ArgumentParser(description=
'Merge specific folder(s) in root files.')
62parser.add_argument(
"-i",
"--input", help=
"path to the folder holding the samples (default: ./)", default=os.getcwd())
63parser.add_argument(
"-o",
"--output", help=
"path for the output (default: ./merge.root", default=os.getcwd()+
"/merge.root")
64parser.add_argument(
"-d",
"--dir", nargs=
'+', help=
"ROOT directory to be merged, multiple arguments are possible (default: Summary)", default=[
"Summary"])
65parser.add_argument(
"-p",
"--pattern", help=
'pattern of files to merge (default: "*PHYSVAL*")', default=
"*PHYSVAL*")
66args = parser.parse_args()
67folder = os.path.abspath(args.input)
72output_file = os.path.abspath(args.output)
75files = glob.glob(folder +
"/" + pattern)
78f = ROOT.TFile(output_file,
"recreate")
80f2 = ROOT.TFile(files[0])
83print(
"Target file: " + output_file)
85 print(
"Found input file: " + infile)
86 if os.path.samefile(output_file, infile):
87 print(
"Please make sure that the output file is not part of the input files! Stopping.")
98 print(
"Merging folder " + path)
101 error =
"ERROR: Cannot find directory " + path +
". Omitting."
105 dirlist = d.GetListOfKeys()
108 for jetcont
in jetcontainers:
110 currentdir = gDirectory.GetPath()
112 print(
"Create directory " + path +
"/other_histograms/histos")
113 tagfolders[path+
"/"+restCategory] = f.mkdir(path+
"/other_histograms/histos")
115 for category
in categories:
116 print(
"Create directory " + path +
"/" + category)
117 tagfolders[path+
"/"+category] = f.mkdir(path+
"/"+category)
118 if category
in categories_with_subcategories_type_1:
119 for sub_category
in sub_categories_type_1:
120 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
121 elif category
in categories_with_subcategories_type_2:
122 for sub_category
in sub_categories_type_2:
123 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
124 elif category
in categories_with_subcategories_type_3:
125 for sub_category
in sub_categories_type_3:
126 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
127 elif category
in categories_with_subcategories_type_4:
128 for sub_category
in sub_categories_type_4:
129 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
130 tagfolders[path+
"/"+category+
"/"+restCategory] = f.mkdir(path+
"/"+category+
"/"+restCategory)
131 gDirectory.cd(currentdir)
133 for subdir
in dirlist:
134 obj = subdir.ReadObj()
136 if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
137 print(
"Now merging "+obj.GetName())
140 hname = hpath[hpath.find(
":")+2:]+
"/"+obj.GetName()
141 print(
"Path: "+hname)
144 if tup==files[0]:
continue
145 nextfile = ROOT.TFile(tup)
146 h2 = nextfile.Get(hname)
148 error =
"ERROR: Cannot find " + hname +
" in file " + tup +
". Omitting."
154 for category
in reversed(categories):
156 if (
"_"+category+
"_")
in obj.GetName():
157 print(
"Category: " + category)
158 subfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category)
161 should_be_in_a_subcategory =
False
162 is_in_subcategory =
False
163 if category
in categories_with_subcategories_type_1:
164 should_be_in_a_subcategory =
True
165 for sub_category
in reversed(sub_categories_type_1):
166 if(sub_category
in obj.GetName()):
167 is_in_subcategory =
True
168 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
171 elif category
in categories_with_subcategories_type_2:
172 should_be_in_a_subcategory =
True
173 for sub_category
in reversed(sub_categories_type_2):
174 if(sub_category
in obj.GetName()):
175 is_in_subcategory =
True
176 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
179 elif category
in categories_with_subcategories_type_3:
180 should_be_in_a_subcategory =
True
181 for sub_category
in reversed(sub_categories_type_3):
182 if(sub_category
in obj.GetName()):
183 is_in_subcategory =
True
184 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
187 elif category
in categories_with_subcategories_type_4:
188 should_be_in_a_subcategory =
True
189 for sub_category
in reversed(sub_categories_type_4):
190 if(sub_category
in obj.GetName()):
191 is_in_subcategory =
True
192 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
195 if should_be_in_a_subcategory
and not is_in_subcategory:
196 rest_subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+restCategory)
197 rest_subsubfolder.cd()
202 subfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/other_histograms/histos")
205 print(gDirectory.GetPath())
208 if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
209 print(
"Found subdirectory "+obj.GetName())
210 hpath = obj.GetPath()
211 subfolder = f.mkdir(hpath[hpath.find(
":")+2:],obj.GetTitle())
212 print(
"Created new output directory " + hpath[hpath.find(
":")+2:])
218for mergeDir
in mergeDirs:
219 newfolder = f.mkdir(mergeDir,mergeDir)
220 ROOT.TH1.AddDirectory(
False)
226 print(
"Summary of all errors:")
227 for phrase
in errors:
230end = time.process_time()
231print(
"Wall time used: %s sec" % (end - start))
void print(char *figname, TCanvas *c1)