ATLAS Offline Software
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 
11 from __future__ import print_function
12 import getopt,os,sys,glob,argparse,ROOT,time
13 from ROOT import gDirectory
14 
15 start = time.process_time()
16 
17 # define the categories (= sub-folders)
18 categories = ['jet',
19  'tracks',
20  'SV',
21  'tagger_IP3D',
22  'tagger_RNNIP',
23  'tagger_DIPS',
24  'tagger_SV1',
25  'tagger_DL1dv01',
26  'tagger_DL1r',
27  'tagger_GN2v01',
28  'tagger_GN2Xv01',
29  'old_taggers',
30  #'tagger_IP2D',
31  ]
32 
33 # name of the folder into which plots in no other category are sorted
34 restCategory = 'other'
35 
36 # make subcategories
37 categories_with_subcategories_type_1 = ['SV', 'tracks']
38 
39 sub_categories_type_1 = [ '_incl',
40  '_b',
41  '_c',
42  '_l',
43  '_muon',
44  ]
45 
46 categories_with_subcategories_type_2 = ['tagger_IP3D', 'tagger_RNNIP', 'tagger_DIPS', 'tagger_SV1', 'tagger_DL1dv01', 'tagger_DL1r', 'tagger_GN2v01','tagger_GN2Xv01']
47 
48 sub_categories_type_2 = [ '_pt_ttbar',
49  '_pt_Zprime',
50  '_Lxy'
51  ]
52 
53 categories_with_subcategories_type_3 = ['old_taggers']
54 
55 sub_categories_type_3 = [ '_IP2D',
56  ]
57 
58 categories_with_subcategories_type_4 = ['jet']
59 
60 sub_categories_type_4 = [ 'jet']
61 
62 # define the jet containers
63 jetcontainers = ['AntiKt4EMTopoJets',
64  'AntiKt4EMPFlowJets',
65  'AntiKt10UFOCSSKSoftDropBeta100Zcut10Jets',
66  'AntiKtVR30Rmax4Rmin02PV0TrackJets',
67  ]
68 
69 # parser arguments
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)
77 mergeDirs = args.dir
78 origDir = os.getcwd()
79 out = args.output
80 pattern = args.pattern
81 output_file = os.path.abspath(args.output)
82 os.chdir(folder)
83 
84 files = glob.glob(folder + "/" + pattern)
85 
86 # open the output file
87 f = ROOT.TFile(output_file, "recreate")
88 folder = os.getcwd()
89 f2 = ROOT.TFile(files[0])
90 
91 # check input files
92 print("Target file: " + output_file)
93 for infile in files:
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.")
97  quit()
98 
99 
100 # -- define a method to merge --
101 
102 errors = []
103 tagfolders = {}
104 
105 def mergeFolder(path):
106  # check input
107  print("Merging folder " + path)
108  d = f2.Get(path)
109  if not d:
110  error = "ERROR: Cannot find directory " + path + ". Omitting."
111  print(error)
112  errors.append(error)
113  return
114  dirlist = d.GetListOfKeys()
115 
116  # create folders
117  for jetcont in jetcontainers:
118  if jetcont in path:
119  currentdir = gDirectory.GetPath()
120  # create /restCategory
121  print("Create directory " + path + "/other_histograms/histos")
122  tagfolders[path+"/"+restCategory] = f.mkdir(path+"/other_histograms/histos")
123  # create category folders
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)
141 
142  for subdir in dirlist:
143  obj = subdir.ReadObj()
144 
145  if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
146  print("Now merging "+obj.GetName())
147  h1 = obj
148  hpath = d.GetPath()
149  hname = hpath[hpath.find(":")+2:]+"/"+obj.GetName()
150  print("Path: "+hname)
151 
152  for tup in files:
153  if tup==files[0]: continue
154  nextfile = ROOT.TFile(tup)
155  h2 = nextfile.Get(hname)
156  if not h2:
157  error = "ERROR: Cannot find " + hname + " in file " + tup + ". Omitting."
158  print(error)
159  errors.append(error)
160  continue
161  h1.Add(h2)
162  if tagfolders:
163  for category in reversed(categories):
164  # check for the categories
165  if ("_"+category+"_") in obj.GetName():
166  print("Category: " + category)
167  subfolder = f.Get(hpath[hpath.find(":")+2:]+"/"+category)
168  subfolder.cd()
169  # apply the sub-categories here
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)
178  subsubfolder.cd()
179  break
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)
186  subsubfolder.cd()
187  break
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)
194  subsubfolder.cd()
195  break
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)
202  subsubfolder.cd()
203  break
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()
207 
208  break
209  # the rest goes into the restCategory
210  else:
211  subfolder = f.Get(hpath[hpath.find(":")+2:]+"/other_histograms/histos")
212  subfolder.cd()
213 
214  print(gDirectory.GetPath())
215  h1.Write()
216 
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:])
222  subfolder.cd()
223  mergeFolder(hpath[hpath.find(":")+2:])
224 
225 
226 # merge the folders
227 for mergeDir in mergeDirs:
228  newfolder = f.mkdir(mergeDir,mergeDir)
229  ROOT.TH1.AddDirectory(False)
230  mergeFolder(mergeDir)
231 
232 # close file and report
233 f.Close()
234 if len(errors)>0:
235  print("Summary of all errors:")
236  for phrase in errors:
237  print(phrase)
238 
239 end = time.process_time()
240 print("Wall time used: %s sec" % (end - start))
mergePhysValFiles.mergeFolder
def mergeFolder(path)
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:45
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:569