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