ATLAS Offline Software
rename_histos_in_files_from_old_code.py
Go to the documentation of this file.
1 
8 
9 from __future__ import print_function
10 import os
11 import ROOT
12 
13 # run this on the unmerged files
14 
15 # input_file_name = 'NTUP_PHYSVAL_compare_to_standard.root'
16 # input_file_name = '/afs/cern.ch/user/j/juhofer/WORK/validation/data/run_standard/NTUP_PHYSVAL_standard.root'
17 input_file_name = '/afs/cern.ch/user/j/juhofer/WORK/validation/data/QSP_on/valid1.410000.PowhegPythiaEvtGen_P2012_ttbar_hdamp172p5_nonallhad.merge.NTUP_PHYSVAL.e4993_s3641_r12287_p4360_p3821_tid24086162_00/NTUP_PHYSVAL.24086162._000001.pool.root.1'
18 
19 
20 production_process = 'ttbar'
21 
22 verbose = False
23 
24 jet_collections = [ 'AntiKt2PV0TrackJets',
25  'AntiKt4EMPFlowJets',
26  'AntiKt4EMTopoJets',
27  'AntiKt4PV0TrackJets',
28  'AntiKtVR30Rmax4Rmin02TrackJets']
29 
30 
31 
32 if production_process not in ['ttbar', 'Zprime']:
33  print("Typo in the given production process: ", production_process, ". Exiting.")
34  exit()
35 
36 var_name_list = [
37 'IP3D_b_matched_weight ',
38 'IP3D_b_matched_weight_trackCuts ',
39 'IP3D_b_50_matched_pt ',
40 'IP3D_b_70_matched_pt ',
41 'IP3D_b_80_matched_pt ',
42 'IP3D_c_matched_weight ',
43 'IP3D_c_matched_weight_trackCuts ',
44 'IP3D_c_50_matched_pt ',
45 'IP3D_c_70_matched_pt ',
46 'IP3D_c_80_matched_pt ',
47 'IP3D_tau_matched_weight ',
48 'IP3D_tau_matched_weight_trackCuts ',
49 'IP3D_tau_50_matched_pt ',
50 'IP3D_tau_70_matched_pt ',
51 'IP3D_tau_80_matched_pt ',
52 'IP3D_u_matched_weight ',
53 'IP3D_u_matched_weight_trackCuts ',
54 'IP3D_u_50_matched_pt ',
55 'IP3D_u_70_matched_pt ',
56 'IP3D_u_80_matched_pt ',
57 'IP2D_b_matched_weight ',
58 'IP2D_b_matched_weight_trackCuts ',
59 'IP2D_b_50_matched_pt ',
60 'IP2D_b_70_matched_pt ',
61 'IP2D_b_80_matched_pt ',
62 'IP2D_c_matched_weight ',
63 'IP2D_c_matched_weight_trackCuts ',
64 'IP2D_c_50_matched_pt ',
65 'IP2D_c_70_matched_pt ',
66 'IP2D_c_80_matched_pt ',
67 'IP2D_tau_matched_weight ',
68 'IP2D_tau_matched_weight_trackCuts ',
69 'IP2D_tau_50_matched_pt ',
70 'IP2D_tau_70_matched_pt ',
71 'IP2D_tau_80_matched_pt ',
72 'IP2D_u_matched_weight ',
73 'IP2D_u_matched_weight_trackCuts ',
74 'IP2D_u_50_matched_pt ',
75 'IP2D_u_70_matched_pt ',
76 'IP2D_u_80_matched_pt ',
77 'SV1_b_matched_weight ',
78 'SV1_b_matched_weight_trackCuts ',
79 'SV1_b_40_matched_pt ',
80 'SV1_b_50_matched_pt ',
81 'SV1_b_60_matched_pt ',
82 'SV1_c_matched_weight ',
83 'SV1_c_matched_weight_trackCuts ',
84 'SV1_c_40_matched_pt ',
85 'SV1_c_50_matched_pt ',
86 'SV1_c_60_matched_pt ',
87 'SV1_tau_matched_weight ',
88 'SV1_tau_matched_weight_trackCuts ',
89 'SV1_tau_40_matched_pt ',
90 'SV1_tau_50_matched_pt ',
91 'SV1_tau_60_matched_pt ',
92 'SV1_u_matched_weight ',
93 'SV1_u_matched_weight_trackCuts ',
94 'SV1_u_40_matched_pt ',
95 'SV1_u_50_matched_pt ',
96 'SV1_u_60_matched_pt ',
97 'JetFitter_b_matched_weight ',
98 'JetFitter_b_matched_weight_trackCuts ',
99 'JetFitter_b_50_matched_pt ',
100 'JetFitter_b_70_matched_pt ',
101 'JetFitter_b_80_matched_pt ',
102 'JetFitter_c_matched_weight ',
103 'JetFitter_c_matched_weight_trackCuts ',
104 'JetFitter_c_50_matched_pt ',
105 'JetFitter_c_70_matched_pt ',
106 'JetFitter_c_80_matched_pt ',
107 'JetFitter_tau_matched_weight ',
108 'JetFitter_tau_matched_weight_trackCuts ',
109 'JetFitter_tau_50_matched_pt ',
110 'JetFitter_tau_70_matched_pt ',
111 'JetFitter_tau_80_matched_pt ',
112 'JetFitter_u_matched_weight ',
113 'JetFitter_u_matched_weight_trackCuts ',
114 'JetFitter_u_50_matched_pt ',
115 'JetFitter_u_70_matched_pt ',
116 'JetFitter_u_80_matched_pt ',
117 'truthLabel ',
118 'TruthBpt ',
119 'TruthLpt ',
120 'TruthCpt ',
121 'TruthTauPt ',
122 'IP3Dpb ',
123 'IP3Dpc ',
124 'IP3Dpu ',
125 'SV1pb ',
126 'SV1pc ',
127 'SV1pu ',
128 'NTracksIP3D ',
129 'NTracksIP2D ',
130 'NGTinSvx1 ',
131 'NGTinSvx0 ',
132 'e ',
133 'pt ',
134 'eta ',
135 'phi ',
136 'd0 ',
137 'z0 ',
138 'IP3DgradeOfTracks ',
139 'IP2DgradeOfTracks ',
140 'IP3D_valD0wrtPVofTracks ',
141 'IP3D_valZ0wrtPVofTracks ',
142 'IP3D_sigD0wrtPVofTracks ',
143 'IP3D_sigZ0wrtPVofTracks ',
144 'IP3D_weightBofTracks ',
145 'IP3D_weightUofTracks ',
146 'IP2D_weightBofTracks ',
147 'IP2D_weightUofTracks '
148 ]
149 
151 
152  taggers = ["_IP3D_", "_SV1_", "_JetFitter_"]
153  old_taggers = ["_IP2D_"]
154  oneToOne_associations = {
155  '_truthLabel ': '_jet_truthLabel ',
156  '_IP3Dpb ': '_tagger_IP3D_pb ',
157  '_IP3Dpc ': '_tagger_IP3D_pc ',
158  '_IP3Dpu ': '_tagger_IP3D_pu ',
159  '_SV1pb ': '_tagger_SV1_pb ',
160  '_SV1pc ': '_tagger_SV1_pc ',
161  '_SV1pu ': '_tagger_SV1_pu ',
162  '_NTracksIP3D ': '_tagger_IP3D_NTracks_incl ',
163  '_NTracksIP2D ': '_old_taggers_IP2D_NTracks_incl ',
164  '_NGTinSvx1 ': '_tagger_SV1_nGoodTracks_incl ',
165  '_eta ': '_jet_jet_eta ',
166  '_phi ': '_jet_jet_phi ',
167  '_d0 ': '_tracks_d0_b ',
168  '_z0 ': '_tracks_z0_b ',
169  '_IP3DgradeOfTracks ': '_tagger_IP3D_gradeOfTracks_incl ',
170  '_IP2DgradeOfTracks ': '_old_taggers_IP2D_gradeOfTracks_incl ',
171  }
172  oneToOne_associations_ttbar = {
173  '_TruthBpt ': '_jet_jet_pt_b_ttbar ',
174  '_TruthLpt ': '_jet_jet_pt_l_ttbar ',
175  '_TruthCpt ': '_jet_jet_pt_c_ttbar ',
176  '_e ': '_jet_jet_e ',
177  '_pt ': '_jet_jet_pt_ttbar ',
178  }
179  oneToOne_associations_Zprime = {
180  '_TruthBpt ': '_jet_jet_pt_b_Zprime ',
181  '_TruthLpt ': '_jet_jet_pt_l_Zprime ',
182  '_TruthCpt ': '_jet_jet_pt_c_Zprime ',
183  '_e ': '_jet_jet_e_Zprime ',
184  '_pt ': '_jet_jet_pt_Zprime ',
185  }
186 
187  name_associations = {}
188 
189  for name in list:
190  new_name = name
191 
192  for tag in taggers:
193  if tag in new_name:
194  new_name = new_name.replace(tag, "_tagger" + tag)
195 
196  for tag in old_taggers:
197  if tag in new_name:
198  new_name = new_name.replace(tag, "_old_taggers" + tag)
199 
200  if "_matched_pt" in new_name:
201  new_name = new_name.replace("_matched_pt", "_matched_pt_" + production_process)
202 
203  if "AntiKt4EMPFlowJets" in new_name:
204  new_name = new_name.replace("AntiKt4EMPFlowJets", "AntiKt4EMPFlowJets_")
205 
206  if "AntiKtVR30Rmax4Rmin02TrackJets" in new_name:
207  new_name = new_name.replace("AntiKtVR30Rmax4Rmin02TrackJets", "AntiKtVR30Rmax4Rmin02TrackJets_")
208 
209  for key in oneToOne_associations:
210  if key in new_name:
211  new_name = new_name.replace(key, oneToOne_associations[key])
212 
213  if production_process == 'ttbar':
214  for key in oneToOne_associations_ttbar:
215  if key in new_name:
216  new_name = new_name.replace(key, oneToOne_associations_ttbar[key])
217 
218  if production_process == 'Zprime':
219  for key in oneToOne_associations_Zprime:
220  if key in new_name:
221  new_name = new_name.replace(key, oneToOne_associations_Zprime[key])
222 
223  # if new_name not in new_list and "tau" not in new_name and "Tau" not in new_name and not "_NGTinSvx0" in new_name:
224  # print(new_name)
225 
226  # remove spaces
227  name = name.replace(" ", "")
228  new_name = new_name.replace(" ", "")
229 
230  # fill the dict
231  name_associations[name] = new_name
232 
233  return name_associations
234 
235 
236 
237 # get the name_list for all jet collections
238 name_list = []
239 for jet_col in jet_collections:
240  for var_name in var_name_list:
241  if jet_col == "AntiKt4EMPFlowJets" or jet_col == "AntiKtVR30Rmax4Rmin02TrackJets":
242  name_list.append("BTag_" + jet_col + var_name)
243  else:
244  name_list.append("BTag_" + jet_col + "_" + var_name)
245 
246 # get the name associations
247 name_associations = get_name_associations(name_list)
248 if verbose:
249  print(name_associations)
250 
251 # prepare the output file
252 if input_file_name.endswith('.root'):
253  output_file_name = input_file_name[:-5] + '_renamed.root'
254 elif input_file_name.endswith('.pool.root.1'):
255  output_file_name = input_file_name[:-12] + '_renamed.pool.root.1'
256 else:
257  print("Input file ending is not .root and not .pool.root.1, I can't handle the conversion to an output name.")
258  exit()
259 
260 output_file = ROOT.TFile(output_file_name, 'RECREATE')
261 
262 # make the subdirectories
263 output_file.mkdir("BTag")
264 for jet_col in jet_collections:
265  path = "BTag/" + jet_col + "/"
266  output_file.mkdir(path)
267 
268 # close the file
269 output_file.Close()
270 
271 # loop over all the jet collections
272 for jet_col in jet_collections:
273 
274  # open the input file
275  input_file = ROOT.TFile(input_file_name, 'READ')
276 
277  if input_file.IsOpen(): #and output_file.IsOpen():
278 
279  # go into the directory
280  path = "BTag;1/" + jet_col + ";1/"
281  ROOT.gDirectory.cd(path)
282 
283  if verbose:
284  print("\njust cd'ed into: " + path)
285  print("\nls")
286  input_file.ls()
287 
288  # get the subdirectories here
289  currentdir = ROOT.gDirectory.GetPath()
290  dirlist = input_file.Get(currentdir).GetListOfKeys()
291 
292  if verbose:
293  print("\ndirlist\n", dirlist)
294 
295  histograms = []
296 
297  # loop over the objects in here
298  for subdir in dirlist:
299  obj = subdir.ReadObj()
300 
301  # and see if they are histograms and ignore TProfiles
302  if obj.IsA().InheritsFrom(ROOT.TH1.Class()) and jet_col in obj.GetName():
303 
304  if verbose:
305  print("\nobj: ", obj)
306  print("'", obj.GetName(), "'," )
307 
308  # check if the name is on the list
309  if obj.GetName() not in name_associations:
310  print("This name is not defined!!!", obj.GetName())
311  exit()
312 
313  # rename the histo and fill the list with histograms
314  new_histo = obj
315  new_histo.SetName(name_associations[obj.GetName()])
316  histograms.append(new_histo)
317 
318  # now open the output file and got to the right dir
319  output_file = ROOT.TFile(output_file_name, 'UPDATE')
320 
321  if output_file.IsOpen():
322 
323  ROOT.gDirectory.cd(path)
324 
325  # loop over the histos
326  for histo in histograms:
327  histo.Write()
328 
329  output_file.Close()
330 
331  input_file.Close()
332 
333 
334 print("\n the output was written to:\n ", output_file_name)
rename_histos_in_files_from_old_code.get_name_associations
def get_name_associations(list)
Definition: rename_histos_in_files_from_old_code.py:150
calibdata.exit
exit
Definition: calibdata.py:236
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28