11import getopt,os,sys,glob,argparse,ROOT,time
12from ROOT
import gDirectory
14start = time.process_time()
29categories_with_subcategories_type_1 = [
'SV',
'tracks']
31sub_categories_type_1 = [
'_incl',
38categories_with_subcategories_type_2 = [
'tagger_GN2v01',
'tagger_GN3XPV01']
40sub_categories_type_2 = [
'_pt_ttbar',
45categories_with_subcategories_type_3 = [
'old_taggers']
47sub_categories_type_3 = [ ]
49categories_with_subcategories_type_4 = [
'jet']
51sub_categories_type_4 = [
'jet']
54jetcontainers = [
'AntiKt4EMTopoJets',
56 'AntiKt10UFOCSSKSoftDropBeta100Zcut10Jets',
60parser = argparse.ArgumentParser(description=
'Merge specific folder(s) in root files.')
61parser.add_argument(
"-i",
"--input", help=
"path to the folder holding the samples (default: ./)", default=os.getcwd())
62parser.add_argument(
"-o",
"--output", help=
"path for the output (default: ./merge.root", default=os.getcwd()+
"/merge.root")
63parser.add_argument(
"-d",
"--dir", nargs=
'+', help=
"ROOT directory to be merged, multiple arguments are possible (default: Summary)", default=[
"Summary"])
64parser.add_argument(
"-p",
"--pattern", help=
'pattern of files to merge (default: "*PHYSVAL*")', default=
"*PHYSVAL*")
65args = parser.parse_args()
66folder = os.path.abspath(args.input)
71output_file = os.path.abspath(args.output)
74files = glob.glob(folder +
"/" + pattern)
77f = ROOT.TFile(output_file,
"recreate")
79f2 = ROOT.TFile(files[0])
82print(
"Target file: " + output_file)
84 print(
"Found input file: " + infile)
85 if os.path.samefile(output_file, infile):
86 print(
"Please make sure that the output file is not part of the input files! Stopping.")
97 print(
"Merging folder " + path)
100 error =
"ERROR: Cannot find directory " + path +
". Omitting."
104 dirlist = d.GetListOfKeys()
107 for jetcont
in jetcontainers:
109 currentdir = gDirectory.GetPath()
111 print(
"Create directory " + path +
"/other_histograms/histos")
112 tagfolders[path+
"/"+restCategory] = f.mkdir(path+
"/other_histograms/histos")
114 for category
in categories:
115 print(
"Create directory " + path +
"/" + category)
116 tagfolders[path+
"/"+category] = f.mkdir(path+
"/"+category)
117 if category
in categories_with_subcategories_type_1:
118 for sub_category
in sub_categories_type_1:
119 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
120 elif category
in categories_with_subcategories_type_2:
121 for sub_category
in sub_categories_type_2:
122 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
123 elif category
in categories_with_subcategories_type_3:
124 for sub_category
in sub_categories_type_3:
125 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
126 elif category
in categories_with_subcategories_type_4:
127 for sub_category
in sub_categories_type_4:
128 tagfolders[path+
"/"+category+
"/"+sub_category] = f.mkdir(path+
"/"+category+
"/"+sub_category)
129 tagfolders[path+
"/"+category+
"/"+restCategory] = f.mkdir(path+
"/"+category+
"/"+restCategory)
130 gDirectory.cd(currentdir)
132 for subdir
in dirlist:
133 obj = subdir.ReadObj()
135 if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
136 print(
"Now merging "+obj.GetName())
139 hname = hpath[hpath.find(
":")+2:]+
"/"+obj.GetName()
140 print(
"Path: "+hname)
143 if tup==files[0]:
continue
144 nextfile = ROOT.TFile(tup)
145 h2 = nextfile.Get(hname)
147 error =
"ERROR: Cannot find " + hname +
" in file " + tup +
". Omitting."
153 for category
in reversed(categories):
155 if (
"_"+category+
"_")
in obj.GetName():
156 print(
"Category: " + category)
157 subfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category)
160 should_be_in_a_subcategory =
False
161 is_in_subcategory =
False
162 if category
in categories_with_subcategories_type_1:
163 should_be_in_a_subcategory =
True
164 for sub_category
in reversed(sub_categories_type_1):
165 if(sub_category
in obj.GetName()):
166 is_in_subcategory =
True
167 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
170 elif category
in categories_with_subcategories_type_2:
171 should_be_in_a_subcategory =
True
172 for sub_category
in reversed(sub_categories_type_2):
173 if(sub_category
in obj.GetName()):
174 is_in_subcategory =
True
175 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
178 elif category
in categories_with_subcategories_type_3:
179 should_be_in_a_subcategory =
True
180 for sub_category
in reversed(sub_categories_type_3):
181 if(sub_category
in obj.GetName()):
182 is_in_subcategory =
True
183 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
186 elif category
in categories_with_subcategories_type_4:
187 should_be_in_a_subcategory =
True
188 for sub_category
in reversed(sub_categories_type_4):
189 if(sub_category
in obj.GetName()):
190 is_in_subcategory =
True
191 subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+sub_category)
194 if should_be_in_a_subcategory
and not is_in_subcategory:
195 rest_subsubfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/"+category+
"/"+restCategory)
196 rest_subsubfolder.cd()
201 subfolder = f.Get(hpath[hpath.find(
":")+2:]+
"/other_histograms/histos")
204 print(gDirectory.GetPath())
207 if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
208 print(
"Found subdirectory "+obj.GetName())
209 hpath = obj.GetPath()
210 subfolder = f.mkdir(hpath[hpath.find(
":")+2:],obj.GetTitle())
211 print(
"Created new output directory " + hpath[hpath.find(
":")+2:])
217for mergeDir
in mergeDirs:
218 newfolder = f.mkdir(mergeDir,mergeDir)
219 ROOT.TH1.AddDirectory(
False)
225 print(
"Summary of all errors:")
226 for phrase
in errors:
229end = time.process_time()
230print(
"Wall time used: %s sec" % (end - start))
void print(char *figname, TCanvas *c1)