2 import os, sys, re, ROOT, argparse
7 def DrawTLatex(x, y, text, size=18, font=43, align=11):
9 tex.SetTextAlign(align)
13 tex.DrawLatex(x, y, text)
18 if not histo.GetEntries():
return False
20 Canvas_Name =
"Isolation_%s_%s"%(
"Corr" if options.useCorrectedCones
else "Orig" ,
22 c1 = ROOT.TCanvas(Canvas_Name,
"canvas", 800 , 600)
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)
34 else: histo.Draw(
"colz")
35 DrawTLatex(c1.GetLeftMargin(), 0.92,
"%s (%s)" %(histo.GetTitle(),
"corrected" if plot_options.useCorrectedCones
else "vanilla") )
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" ))
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)
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()
52 if not os.path.exists(options.inFile):
53 logging.error(
"There is no file %s"%(options.inFile))
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))
61 anaTree = inFile.Get(
"IsoCorrTest")
63 logging.error(
" Where is my Testing tree in file %s"%(options.inFile))
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]")
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]")
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)")
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)")
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)")
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)")
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)")
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)")
106 N_Isolated_Muon = ROOT.TH1D(
"N_muons",
"Isolated #mu", 6, 0, 6);
107 N_Isolated_Muon.GetXaxis().SetTitle(
"N(#mu) - passing iso WP" );
109 N_Isolated_Elec = ROOT.TH1D(
"N_elec",
"Elec #mu", 6, 0, 6);
110 N_Isolated_Elec.GetXaxis().SetTitle(
"N(e) - passing iso WP");
112 AllHistos = [Elec_Track_Iso,
120 Elec_Track_polution_dR,
121 Elec_Topo_polution_dR,
122 Muon_Track_polution_dR,
123 Muon_Topo_polution_dR ,
127 treeReader = ROOT.TTreeReader(
"IsoCorrTest", inFile)
128 branch_names = [ B.GetName()
for B
in anaTree.GetListOfBranches() ]
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
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
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])
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")
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")
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
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")
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
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"))
178 while treeReader.Next():
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]))
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)
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]
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)
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]
204 for m, mu
in enumerate(muon_t_vec):
206 Muon_Track_Iso.Fill(mu_trk_iso[m] / 1.e3)
207 Muon_RelTrack_Iso.Fill( mu_trk_iso[m]/ mu.Pt())
209 Muon_Calo_Iso.Fill(mu_calo_iso[m] / 1.e3)
210 Muon_RelTrack_Iso.Fill(mu_calo_iso[m] / mu.Pt())
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.) )
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() )
221 for e, el
in enumerate(elec_t_vec):
223 Elec_Track_Iso.Fill(el_trk_iso[e] / 1.e3)
224 Elec_RelTrack_Iso.Fill( el_trk_iso[e]/ el.Pt())
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() )
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.))
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))
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" ) )
248 for H
in AllHistos: saved_once =
saveHisto(options, H)
or saved_once
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" ) )