ATLAS Offline Software
Loading...
Searching...
No Matches
PhysicsAnalysis/JetTagging/JetTagValidation/JetTagDQA/scripts/mergePhysValFiles.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3#----------------------------------------------------------------------
4#stand-alone script to merge specific directories of NTUP_PHYSVAL files
5#author: philipp.mogg@cern.ch
6#16 May 2016
7#updates: judith.hoefer@cern.ch
8#Nov 2020
9#----------------------------------------------------------------------
10
11import getopt,os,sys,glob,argparse,ROOT,time
12from ROOT import gDirectory
13
14start = time.process_time()
15
16# define the categories (= sub-folders)
17categories = ['jet',
18 'tracks',
19 'SV',
20 'tagger_GN2v01',
21 'tagger_GN3XPV01',
22 'old_taggers',
23 ]
24
25# name of the folder into which plots in no other category are sorted
26restCategory = 'other'
27
28# make subcategories
29categories_with_subcategories_type_1 = ['SV', 'tracks']
30
31sub_categories_type_1 = [ '_incl',
32 '_b',
33 '_c',
34 '_l',
35 '_muon',
36 ]
37
38categories_with_subcategories_type_2 = ['tagger_GN2v01','tagger_GN3XPV01']
39
40sub_categories_type_2 = [ '_pt_ttbar',
41 '_pt_Zprime',
42 '_Lxy'
43 ]
44
45categories_with_subcategories_type_3 = ['old_taggers']
46
47sub_categories_type_3 = [ ]
48
49categories_with_subcategories_type_4 = ['jet']
50
51sub_categories_type_4 = [ 'jet']
52
53# define the jet containers
54jetcontainers = ['AntiKt4EMTopoJets',
55 'AntiKt4EMPFlowJets',
56 'AntiKt10UFOCSSKSoftDropBeta100Zcut10Jets',
57 ]
58
59# parser arguments
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)
67mergeDirs = args.dir
68origDir = os.getcwd()
69out = args.output
70pattern = args.pattern
71output_file = os.path.abspath(args.output)
72os.chdir(folder)
73
74files = glob.glob(folder + "/" + pattern)
75
76# open the output file
77f = ROOT.TFile(output_file, "recreate")
78folder = os.getcwd()
79f2 = ROOT.TFile(files[0])
80
81# check input files
82print("Target file: " + output_file)
83for infile in files:
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.")
87 quit()
88
89
90# -- define a method to merge --
91
92errors = []
93tagfolders = {}
94
95def mergeFolder(path):
96 # check input
97 print("Merging folder " + path)
98 d = f2.Get(path)
99 if not d:
100 error = "ERROR: Cannot find directory " + path + ". Omitting."
101 print(error)
102 errors.append(error)
103 return
104 dirlist = d.GetListOfKeys()
105
106 # create folders
107 for jetcont in jetcontainers:
108 if jetcont in path:
109 currentdir = gDirectory.GetPath()
110 # create /restCategory
111 print("Create directory " + path + "/other_histograms/histos")
112 tagfolders[path+"/"+restCategory] = f.mkdir(path+"/other_histograms/histos")
113 # create category folders
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)
131
132 for subdir in dirlist:
133 obj = subdir.ReadObj()
134
135 if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
136 print("Now merging "+obj.GetName())
137 h1 = obj
138 hpath = d.GetPath()
139 hname = hpath[hpath.find(":")+2:]+"/"+obj.GetName()
140 print("Path: "+hname)
141
142 for tup in files:
143 if tup==files[0]: continue
144 nextfile = ROOT.TFile(tup)
145 h2 = nextfile.Get(hname)
146 if not h2:
147 error = "ERROR: Cannot find " + hname + " in file " + tup + ". Omitting."
148 print(error)
149 errors.append(error)
150 continue
151 h1.Add(h2)
152 if tagfolders:
153 for category in reversed(categories):
154 # check for the categories
155 if ("_"+category+"_") in obj.GetName():
156 print("Category: " + category)
157 subfolder = f.Get(hpath[hpath.find(":")+2:]+"/"+category)
158 subfolder.cd()
159 # apply the sub-categories here
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)
168 subsubfolder.cd()
169 break
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)
176 subsubfolder.cd()
177 break
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)
184 subsubfolder.cd()
185 break
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)
192 subsubfolder.cd()
193 break
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()
197
198 break
199 # the rest goes into the restCategory
200 else:
201 subfolder = f.Get(hpath[hpath.find(":")+2:]+"/other_histograms/histos")
202 subfolder.cd()
203
204 print(gDirectory.GetPath())
205 h1.Write()
206
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:])
212 subfolder.cd()
213 mergeFolder(hpath[hpath.find(":")+2:])
214
215
216# merge the folders
217for mergeDir in mergeDirs:
218 newfolder = f.mkdir(mergeDir,mergeDir)
219 ROOT.TH1.AddDirectory(False)
220 mergeFolder(mergeDir)
221
222# close file and report
223f.Close()
224if len(errors)>0:
225 print("Summary of all errors:")
226 for phrase in errors:
227 print(phrase)
228
229end = time.process_time()
230print("Wall time used: %s sec" % (end - start))
if(febId1==febId2)
void print(char *figname, TCanvas *c1)