ATLAS Offline Software
plotIsoValidation.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2 import os, sys, re, ROOT, argparse
3 from math import fabs
4 import logging
5 
6 
7 def DrawTLatex(x, y, text, size=18, font=43, align=11):
8  tex = ROOT.TLatex()
9  tex.SetTextAlign(align)
10  tex.SetTextSize(size)
11  tex.SetTextFont(font)
12  tex.SetNDC()
13  tex.DrawLatex(x, y, text)
14 
15 
16 
17 def saveHisto(plot_options, histo):
18  if not histo.GetEntries(): return False
19 
20  Canvas_Name = "Isolation_%s_%s"%("Corr" if options.useCorrectedCones else "Orig" ,
21  histo.GetName())
22  c1 = ROOT.TCanvas(Canvas_Name, "canvas", 800 , 600)
23 
24  c1.cd()
25  c1.SetTopMargin(0.13)
26  if histo.GetDimension() == 2:
27  c1.SetRightMargin(0.15)
28  c1.SetLeftMargin(0.12)
29  if histo.GetDimension() == 1:
30  histo.SetLineColor(ROOT.kRed)
31  histo.SetLineWidth(2)
32 
33  histo.Draw()
34  else: histo.Draw("colz")
35  DrawTLatex(c1.GetLeftMargin(), 0.92,"%s (%s)" %(histo.GetTitle(), "corrected" if plot_options.useCorrectedCones else "vanilla") )
36 
37  c1.SaveAs("%s/%s.pdf"%(options.outDir, Canvas_Name))
38  c1.SaveAs("%s/AllIsolationPlots_%s.pdf"%(options.outDir, "Corr" if options.useCorrectedCones else "Orig" ))
39 
40  return True
41 if __name__ == '__main__':
42  parser = argparse.ArgumentParser(description='This script generates validation plots of the isolation correction tool. For more help type \"python plotIsoValidation.py -h\"',
43  prog='plotIsoValidation',
44  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
45 
46  parser.add_argument("--inFile", help="Where is the input generated by the IsolationCorrection test alg 'IsolationSelection/testIsoCloseByCorr_jobOptions.py'",
47  default="IsoCloseByTestFile.root")
48  parser.add_argument("--outDir", help = "Output directory of the final plots", default="Plots/")
49  parser.add_argument("--useCorrectedCones", help="", action ='store_true', default=False)
50  options = parser.parse_args()
51 
52  if not os.path.exists(options.inFile):
53  logging.error("There is no file %s"%(options.inFile))
54  exit(1)
55 
56  inFile = ROOT.TFile.Open(options.inFile, "READ")
57  if not inFile or not inFile.IsOpen():
58  logging.error("ROOT file %s is invalid please check whether the test job processed successfully"%(options.inFile))
59  exit(1)
60 
61  anaTree = inFile.Get("IsoCorrTest")
62  if not anaTree:
63  logging.error(" Where is my Testing tree in file %s"%(options.inFile))
64  exit(1)
65 
68  Elec_Track_Iso = ROOT.TH1D("elec_track_iso", "track isolation", 55, -5, 50)
69  Elec_Track_Iso.GetXaxis().SetTitle(" p_{T}^{TrackIsolation} (electrons) [GeV]")
70  Elec_Calo_Iso = ROOT.TH1D("elec_calo_iso", "calorimeter isolation", 55, -5, 50)
71  Elec_Calo_Iso.GetXaxis().SetTitle(" E_{T}^{CaloIsolation} (electrons) [GeV]")
72 
73  Muon_Track_Iso = ROOT.TH1D("muon_track_iso", "track isolation", 55, -5, 50)
74  Muon_Track_Iso.GetXaxis().SetTitle(" p_{T}^{TrackIsolation} (muons) [GeV]")
75  Muon_Calo_Iso = ROOT.TH1D("muon_calo_iso", "calorimeter isolation", 55, -5, 50)
76  Muon_Calo_Iso.GetXaxis().SetTitle(" E_{T}^{CaloIsolation} (muons) [GeV]")
77 
78 
79  Elec_RelTrack_Iso = ROOT.TH1D("elec_rel_track_iso", "track isolation", 52, -0.04, 1)
80  Elec_RelTrack_Iso.GetXaxis().SetTitle("p_{T}^{TrackIsolation} / p_{T} (electrons)")
81  Elec_RelCalo_Iso = ROOT.TH1D("elec_rel_calo_iso", "track isolation", 52, -0.04, 1)
82  Elec_RelCalo_Iso.GetXaxis().SetTitle("E_{T}^{CaloIsolation} / p_{T} (electrons)")
83 
84  Muon_RelTrack_Iso = ROOT.TH1D("muon_rel_track_iso", "track isolation", 52, -0.04, 1)
85  Muon_RelTrack_Iso.GetXaxis().SetTitle("p_{T}^{TrackIsolation} / p_{T} (muons)")
86  Muon_RelCalo_Iso = ROOT.TH1D("muon_rel_calo_iso", "track isolation", 52, -0.04, 1)
87  Muon_RelCalo_Iso.GetXaxis().SetTitle("E_{T}^{CaloIsolation} / p_{T} (muons)")
88 
89 
90  Elec_Track_polution_dR = ROOT.TH2D("elec_track_polution_dR", "track_isolaiton against dR",50, 0, 0.5, 100, -1.5, 1.5 )
91  Elec_Track_polution_dR.GetXaxis().SetTitle("#Delta R (e, #it{l})")
92  Elec_Track_polution_dR.GetYaxis().SetTitle("p_{T}^{TrackIsolation}(electrons) / p_{T}( Associated close-by track)")
93 
94  Elec_Topo_polution_dR = ROOT.TH2D("elec_calo_polution_dR", "calorimeter isolation against dR",50, 0, 0.5, 100, -1.5, 1.5)
95  Elec_Topo_polution_dR.GetXaxis().SetTitle("#Delta R (e, #it{l})")
96  Elec_Topo_polution_dR.GetYaxis().SetTitle("E_{T}^{CaloIsolation} (electrons) / E_{T}( Associated close-by cluster)")
97 
98  Muon_Track_polution_dR = ROOT.TH2D("muon_track_polution_dR", "track_isolaiton against dR", 50, 0, 0.5, 100, -1.5, 1.5)
99  Muon_Track_polution_dR.GetXaxis().SetTitle("#Delta R (#mu, #it{l})")
100  Muon_Track_polution_dR.GetYaxis().SetTitle("p_{T}^{TrackIsolation}(muons) / p_{T}( Associated close-by track)")
101 
102  Muon_Topo_polution_dR = ROOT.TH2D("muon_calo_polution_dR", "calorimeter isolation against dR", 50, 0, 0.5, 100, -1.5, 1.5)
103  Muon_Topo_polution_dR.GetXaxis().SetTitle("#Delta R (#mu, #it{l})")
104  Muon_Topo_polution_dR.GetYaxis().SetTitle("E_{T}^{CaloIsolation} (muons) / E_{T}( Associated close-by cluster)")
105 
106  N_Isolated_Muon = ROOT.TH1D("N_muons", "Isolated #mu", 6, 0, 6);
107  N_Isolated_Muon.GetXaxis().SetTitle("N(#mu) - passing iso WP" );
108 
109  N_Isolated_Elec = ROOT.TH1D("N_elec", "Elec #mu", 6, 0, 6);
110  N_Isolated_Elec.GetXaxis().SetTitle("N(e) - passing iso WP");
111 
112  AllHistos = [Elec_Track_Iso,
113  Elec_Calo_Iso ,
114  Muon_Track_Iso,
115  Muon_Calo_Iso,
116  Elec_RelTrack_Iso,
117  Muon_RelTrack_Iso,
118  Elec_RelCalo_Iso,
119  Muon_RelCalo_Iso,
120  Elec_Track_polution_dR,
121  Elec_Topo_polution_dR,
122  Muon_Track_polution_dR,
123  Muon_Topo_polution_dR ,
124  N_Isolated_Muon,
125  N_Isolated_Elec,
126  ]
127  treeReader = ROOT.TTreeReader("IsoCorrTest", inFile)
128  branch_names = [ B.GetName() for B in anaTree.GetListOfBranches() ]
129 
130  mu_trk_cone_names = [b [ b.find("Corr" if options.useCorrectedCones else "Orig") + 5 :] for b in branch_names if b.find("Muons_%s_pt"%("Corr" if options.useCorrectedCones else "Orig")) != -1]
131  mu_calo_cone_names = [ b [ b.find("Corr" if options.useCorrectedCones else "Orig") + 5 :] for b in branch_names if b.find("Muons_%s_topo"%("Corr" if options.useCorrectedCones else "Orig")) != -1 or
132  b.find("Muons_%s_neflow"%("Corr" if options.useCorrectedCones else "Orig")) != -1
133  ]
134 
135  el_trk_cone_names = [ b [ b.find("Corr" if options.useCorrectedCones else "Orig") + 5 :] for b in branch_names if b.find("Electrons_%s_pt"%("Corr" if options.useCorrectedCones else "Orig")) != -1]
136  el_calo_cone_names = [ b [ b.find("Corr" if options.useCorrectedCones else "Orig") + 5 :] for b in branch_names if b.find("Electrons_%s_topo"%("Corr" if options.useCorrectedCones else "Orig")) != -1 or
137  b.find("Electrons_%s_neflow"%("Corr" if options.useCorrectedCones else "Orig")) != -1
138  ]
139 
140  if len (el_calo_cone_names) > 0:
141  for H in [H for H in AllHistos if H.GetName().find("elec") != -1 and H.GetName().find("calo") != -1]: H.SetTitle(el_calo_cone_names[0])
142  if len (el_trk_cone_names) > 0:
143  for H in [H for H in AllHistos if H.GetName().find("elec") != -1 and H.GetName().find("track") != -1]: H.SetTitle(el_trk_cone_names[0])
144  if len (mu_calo_cone_names) > 0:
145  for H in [H for H in AllHistos if H.GetName().find("muon") != -1 and H.GetName().find("calo") != -1]: H.SetTitle(mu_calo_cone_names[0])
146  if len (mu_trk_cone_names) > 0:
147  for H in [H for H in AllHistos if H.GetName().find("muon") != -1 and H.GetName().find("track") != -1]: H.SetTitle(mu_trk_cone_names[0])
148 
149 
150  mu_pt = ROOT.TTreeReaderArray(float)(treeReader, "Muons_pt")
151  mu_eta = ROOT.TTreeReaderArray(float)(treeReader, "Muons_eta")
152  mu_phi = ROOT.TTreeReaderArray(float)(treeReader, "Muons_phi")
153  mu_trk = ROOT.TTreeReaderArray(float)(treeReader, "Muons_trackPt")
154 
155  mu_clu = ROOT.TTreeReaderArray(float)(treeReader, "Muons_clusterEt")
156  mu_clu_eta = ROOT.TTreeReaderArray(float)(treeReader, "Muons_clusterEta")
157  mu_clu_phi = ROOT.TTreeReaderArray(float)(treeReader, "Muons_clusterPhi")
158 
159 
160  mu_trk_iso = ROOT.TTreeReaderArray(float)(treeReader, "Muons_%s_%s"%("Corr" if options.useCorrectedCones else "Orig", mu_trk_cone_names[0])) if len(mu_trk_cone_names) > 0 else None
161  mu_calo_iso = ROOT.TTreeReaderArray(float)(treeReader, "Muons_%s_%s"%("Corr" if options.useCorrectedCones else "Orig",mu_calo_cone_names[0])) if len (mu_calo_cone_names) > 0 else None
162 
163  el_pt = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_pt")
164  el_eta = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_eta")
165  el_phi = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_phi")
166  el_trk = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_trackPt")
167  el_clu = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_clusterEt")
168  el_clu_eta = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_clusterEta")
169  el_clu_phi = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_clusterPhi")
170 
171  el_trk_iso = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_%s_%s"%("Corr" if options.useCorrectedCones else "Orig", el_trk_cone_names[0])) if len(el_trk_cone_names) > 0 else None
172  el_calo_iso = ROOT.TTreeReaderArray(float)(treeReader, "Electrons_%s_%s"%("Corr" if options.useCorrectedCones else "Orig",el_calo_cone_names[0])) if len(el_calo_cone_names) > 0 else None
173 
174  el_pass_iso = ROOT.TTreeReaderArray(bool)(treeReader, "Muons_%sPassIso"%("Corr" if options.useCorrectedCones else "Orig"))
175  mu_pass_iso = ROOT.TTreeReaderArray(bool)(treeReader, "Electrons_%sPassIso"%("Corr" if options.useCorrectedCones else "Orig"))
176 
177 
178  while treeReader.Next():
179  muon_t_vec = []
180  elec_t_vec = []
181 
182  elec_t_calo_vec = []
183  muon_t_calo_vec = []
184 
185  N_Isolated_Elec.Fill(len([i for i in range(el_pass_iso.GetSize()) if el_pass_iso[i] == True]))
186  N_Isolated_Muon.Fill(len([i for i in range(mu_pass_iso.GetSize()) if mu_pass_iso[i] == True]))
187 
188  for m in range(mu_pt.GetSize()):
189  mu = ROOT.TLorentzVector()
190  mu.SetPtEtaPhiM( mu_pt[m], mu_eta[m], mu_phi[m], 0)
191  muon_t_vec += [mu]
192  calo_mu = ROOT.TLorentzVector()
193  calo_mu.SetPtEtaPhiM( mu_clu[m], mu_clu_eta[m], mu_clu_phi[m], 0)
194  muon_t_calo_vec += [calo_mu]
195 
196  for e in range(el_pt.GetSize()):
197  el = ROOT.TLorentzVector()
198  el.SetPtEtaPhiM( el_pt[e], el_eta[e], el_phi[e], 0)
199  elec_t_vec += [el]
200  calo_el = ROOT.TLorentzVector()
201  calo_el.SetPtEtaPhiM( el_clu[e], el_clu_eta[e], el_clu_phi[e], 0)
202  elec_t_calo_vec += [calo_el]
203 
204  for m, mu in enumerate(muon_t_vec):
205  if mu_trk_iso:
206  Muon_Track_Iso.Fill(mu_trk_iso[m] / 1.e3)
207  Muon_RelTrack_Iso.Fill( mu_trk_iso[m]/ mu.Pt())
208  if mu_calo_iso:
209  Muon_Calo_Iso.Fill(mu_calo_iso[m] / 1.e3)
210  Muon_RelTrack_Iso.Fill(mu_calo_iso[m] / mu.Pt())
211 
212  for m1 in range(m):
213  if mu_trk_iso: Muon_Track_polution_dR.Fill( mu.DeltaR(muon_t_vec[m1]), mu_trk_iso[m] / mu_trk[m1] )
214  if mu_calo_iso: Muon_Topo_polution_dR.Fill( mu.DeltaR(muon_t_calo_vec[m1]), mu_calo_iso[m] / (mu_clu[m1] if mu_clu[m1]!= 0. else 1.) )
215 
216  for e, el in enumerate(elec_t_vec):
217  if mu_trk_iso: Muon_Track_polution_dR.Fill( mu.DeltaR(el), mu_trk_iso[m] / el_trk[e] )
218  if mu_calo_iso: Muon_Topo_polution_dR.Fill( mu.DeltaR(el), mu_calo_iso[m] / el.E() )
219 
220 
221  for e, el in enumerate(elec_t_vec):
222  if el_trk_iso:
223  Elec_Track_Iso.Fill(el_trk_iso[e] / 1.e3)
224  Elec_RelTrack_Iso.Fill( el_trk_iso[e]/ el.Pt())
225  for e1 in range(e):
226  if el_trk_iso: Elec_Track_polution_dR.Fill( el.DeltaR(elec_t_vec[e1]), el_trk_iso[e] / el_trk[e1] )
227  if el_calo_iso: Elec_Topo_polution_dR.Fill( el.DeltaR(elec_t_vec[e1]), el_calo_iso[e] / elec_t_vec[e1].E() )
228 
229  for m, mu in enumerate(muon_t_vec):
230  if el_trk_iso: Elec_Track_polution_dR.Fill( mu.DeltaR(el), el_trk_iso[e] / mu_trk[m])
231  if el_calo_iso: Elec_Topo_polution_dR.Fill( mu.DeltaR(muon_t_calo_vec[m]), el_calo_iso[e] / (mu_clu[m] if mu_clu[m]!= 0. else 1.))
232 
233 
234  # do this here, since before it destroys the argparse
235  ROOT.gROOT.Macro("rootlogon.C")
236  ROOT.gROOT.SetStyle("ATLAS")
237  ROOT.gROOT.SetBatch(1)
238  ROOT.gStyle.SetPalette(ROOT.kViridis)
239  if not os.path.exists(options.outDir):
240  logging.info("Create directory %s"%(options.outDir))
241  os.system("mkdir -p %s"%(options.outDir))
242 
243  dummy_canvas = ROOT.TCanvas("dummy", "dummy", 800, 600)
244  dummy_canvas.SaveAs("%s/AllIsolationPlots_%s.pdf["%(options.outDir,
245  "Corr" if options.useCorrectedCones else "Orig" ) )
246 
247  saved_once = False
248  for H in AllHistos: saved_once = saveHisto(options, H) or saved_once
249 
250  if saved_once: dummy_canvas.SaveAs("%s/AllIsolationPlots_%s.pdf]"%(options.outDir,
251  "Corr" if options.useCorrectedCones else "Orig" ) )
252  else: os.system("rm %s/AllIsolationPlots_%s.pdf"%(options.outDir,
253  "Corr" if options.useCorrectedCones else "Orig" ) )
254 
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
plotIsoValidation.DrawTLatex
def DrawTLatex(x, y, text, size=18, font=43, align=11)
Definition: plotIsoValidation.py:7
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
plotIsoValidation.saveHisto
def saveHisto(plot_options, histo)
Definition: plotIsoValidation.py:17
calibdata.exit
exit
Definition: calibdata.py:236
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21