4 from array
import array
5 import ROOT, argparse, sys, gc, os, math
6 from PlotUtils
import PlotUtils, DiagnosticHisto
12 var_name = "", axis_title ="",
13 bins = -1, bmin = 0., bmax = 0., bin_width = -1,
16 name_old_rel ="", name_new_rel ="",
19 branch_old = "", branch_new = "",
20 weight_old = None, weight_new = None,
27 raise NameError(
"Could not find "+branch_old+
" in the Tree")
30 raise NameError(
"Could not find "+branch_new+
" in the Tree")
32 self.
__old_weight = 1.
if not weight_old
else test_tree.GetLeaf(weight_old)
33 self.
__new_weight = 1.
if not weight_new
else test_tree.GetLeaf(weight_new)
35 raise NameError(
"Could not find "+weight_old+
" in the Tree")
37 raise NameError(
"Could not find "+weight_new+
" in the Tree")
40 if branch_old.find(
"HighPt") != -1 : self.
__quality_branch = test_tree.GetLeaf(
"Muon_isHighPt")
41 if branch_old.find(
"LowPt") != -1 : self.
__quality_branch = test_tree.GetLeaf(
"Muon_isLowPt")
43 self.
__min_quality = 0
if branch_old.find(
"HighPt") != -1
and branch_old.find(
"LowPt") != -1
else 1
51 name =
"%s_%s"%(name_old_rel, var_name),
52 axis_title = axis_title,
53 bins = bins, bmin = bmin, bmax = bmax,
54 bin_width = bin_width, bdir = bdir, log_binning = log_binning)
56 name =
"%s_%s"%(name_new_rel, var_name),
57 axis_title = axis_title,
58 bins = bins, bmin = bmin, bmax = bmax,
59 bin_width = bin_width, bdir = bdir, log_binning = log_binning)
109 ReleaseComparer.__init__(self,
111 axis_title = axis_title,
112 bins = bins, bmin = 5.e-4, bmax = 4.,
113 name_old_rel =name_old_rel,
114 name_new_rel =name_new_rel,
115 test_tree = test_tree,
116 branch_old = branch_old,
117 branch_new = branch_new,
118 weight_old = weight_old,
119 weight_new = weight_new,
121 self.
__sys_old = test_tree.GetLeaf(branch_sys_old)
122 self.
__sys_new = test_tree.GetLeaf(branch_sys_new)
124 raise NameError(
"Failed to retrieve "+branch_sys_old)
126 raise NameError(
"Failed to retrieve "+branch_sys_new)
141 "HightPt3Layers":
"RECO",
148 "FCTight_FixedRadIso":
"ISO",
149 "FCLoose_FixedRadIso":
"ISO",
150 "FixedCutHighPtTrackOnlyIso":
"ISO",
152 "FixedCutPflowLooseIso":
"ISO",
153 "FCTightTrackOnlyIso":
"ISO",
154 "FixedCutPflowTightIso":
"ISO",
155 "FCTightTrackOnly_FixedRadIso":
"ISO",
156 "BadMuonVeto_HighPt" :
"BADMUON",
160 parser = argparse.ArgumentParser(description=
'This script checks applied scale factors written to a file by MuonEfficiencyCorrections/MuonEfficiencyCorrectionsSFFilesTest. For more help type \"python CheckAppliedSFs.py -h\"', prog=
'CheckAppliedSFs', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
161 parser.add_argument(
'-i',
'--InputFile', help=
'Specify an input root file', default=
"SFTest.root")
162 parser.add_argument(
'-o',
'--outDir', help=
'Specify a destination directory', default=
"Plots")
163 parser.add_argument(
'-l',
'--label', help=
'Specify the dataset you used with MuonEfficiencyCorrectionsSFFilesTest', default=
"Internal")
164 parser.add_argument(
'-w',
'--WP', help=
'Specify a WP to plot', nargs=
'+', default=[])
165 parser.add_argument(
'-c',
'--SFConstituent', help=
'Specify if you want to plot nominal value, sys or stat error', nargs=
'+', default=[
"SF",
"DataEff",
"MCEff"])
166 parser.add_argument(
'--bonusname', help=
'Specify a bonus name for the filename', default=
"")
167 parser.add_argument(
'--bonuslabel', help=
'Specify a bonus label printed in the histogram', default=
"")
168 parser.add_argument(
'--noComparison', help=
'do not plot comparison to old release', action=
'store_true', default=
False)
169 parser.add_argument(
'-n',
'--nBins', help=
'specify number of bins for histograms', type=int, default=50)
173 branchesInFile = [key.GetName()
for key
in tree.GetListOfBranches()]
175 allWPs =
set([wp
for wp
in KnownWPs.iterkeys() ])
177 for i
in branchesInFile:
179 if not i.endswith(
"SF"):
continue
180 if not i.startswith(
"c"):
continue
183 wp_str = i[ : i.rfind(
"_")]
184 beststr = wp_str[wp_str.rfind(
"_")+1 : ]
185 if beststr
in allWPs:
186 if not beststr
in WPs: WPs.append(beststr)
187 if not calibCand[ : calibCand.find(beststr)-1]
in calibReleases: calibReleases.append(calibCand[ : calibCand.find(beststr)-1])
188 print "INFO: Found the following working points: %s"%(
", ".
join(WPs))
189 return calibReleases, WPs
192 search_str =
"c%s_%s_SF"%(calib_release, wp)
193 syst_names = [key.GetName()[len(search_str) + 2:]
for key
in tree.GetListOfBranches()
if key.GetName().startswith(search_str)
and key.GetName() != search_str]
197 if __name__ ==
"__main__":
200 if not os.path.exists(Options.InputFile):
201 print 'ERROR: File %s does not exist!'%Options.InputFile
203 infile = ROOT.TFile(Options.InputFile)
205 tree = infile.Get(
"MuonEfficiencyTest")
208 if len(calibReleases)==2:
print "INFO: Found the following calibration releases to compare: %s"%(
",".
join(calibReleases))
210 if len(Options.WP)>0:
212 for wp
in Options.WP:
213 if wp
in WPs: userWPs.append(wp)
215 print 'INFO: WPs given by user, only plot: %s'%(
",".
join(WPs))
218 ROOT.gROOT.Macro(
"rootlogon.C")
219 ROOT.gROOT.SetStyle(
"ATLAS")
220 ROOT.gROOT.SetBatch(1)
223 if os.path.isdir(Options.outDir) ==
False:
224 os.system(
"mkdir -p %s"%(Options.outDir))
226 bonusname=Options.bonusname
232 for t
in Options.SFConstituent:
233 corrType =
"Scale Factor"
234 if t ==
"DataEff": corrType =
"Data efficiency"
235 elif t ==
"MCEff": corrType =
"MC efficiency"
237 for var
in systematics:
241 var_name =
"%s_%s"%(wp,t), axis_title =
" %s %s"%(corrType,wp),
242 bmin = 1., bin_width = 0.0001,
243 name_old_rel =calibReleases[0], name_new_rel = calibReleases[1],
245 branch_old =
"c%s_%s_%s"%(calibReleases[0],wp,t), branch_new =
"c%s_%s_%s"%(calibReleases[1],wp,t),
250 var_name =
"%s_%s_%s"%(wp,t,var.replace(
"RECO",KnownWPs[wp])),
251 axis_title =
"%s %s (%s)"%(var.replace(
"RECO",KnownWPs[wp]), corrType,wp),
252 bins = Options.nBins,
253 name_old_rel =calibReleases[0], name_new_rel = calibReleases[1],
255 branch_old =
"c%s_%s_%s"%(calibReleases[0],wp,t), branch_new =
"c%s_%s_%s"%(calibReleases[1],wp,t),
257 branch_sys_old =
"c%s_%s_%s__%s"%(calibReleases[0],wp,t,var),
258 branch_sys_new =
"c%s_%s_%s__%s"%(calibReleases[1],wp,t,var),
263 ReleaseComparer(var_name =
"pt_%s"%(wp), axis_title =
"p_{T} #mu(%s) [MeV]"%(wp),
264 bins = 15, bmin = 15.e3, bmax = 200.e3,
265 name_old_rel =calibReleases[0], name_new_rel = calibReleases[1],
267 branch_old =
"Muon_pt", branch_new =
"Muon_pt",
268 weight_old =
"c%s_%s_SF"%(calibReleases[0],wp),
269 weight_new =
"c%s_%s_SF"%(calibReleases[1],wp)),
271 ReleaseComparer(var_name =
"eta_%s"%(wp), axis_title =
"#eta #mu(%s) [MeV]"%(wp),
272 bins = 54, bmin = -2.7, bmax = 2.7,
273 name_old_rel =calibReleases[0], name_new_rel = calibReleases[1],
275 branch_old =
"Muon_eta", branch_new =
"Muon_eta",
276 weight_old =
"c%s_%s_SF"%(calibReleases[0],wp),
277 weight_new =
"c%s_%s_SF"%(calibReleases[1],wp)),
278 ReleaseComparer(var_name =
"phi_%s"%(wp), axis_title =
"#phi #mu(%s) [MeV]"%(wp),
279 bins = 16, bmin = -3.15, bmax = 3.15,
280 name_old_rel =calibReleases[0], name_new_rel = calibReleases[1],
282 branch_old =
"Muon_phi", branch_new =
"Muon_phi",
283 weight_old =
"c%s_%s_SF"%(calibReleases[0],wp),
284 weight_new =
"c%s_%s_SF"%(calibReleases[1],wp)),
288 for i
in range(tree.GetEntries()):
290 if i > 0
and i % 2500 == 0:
291 print "INFO: %d/%d events processed"%(i, tree.GetEntries())
293 if math.fabs(tree.Muon_eta) > 2.5
or math.fabs(tree.Muon_eta) < 0.1
or math.fabs(tree.Muon_pt) > 15.e3:
continue
297 print "INFO: Histograms filled"
301 if len(calibReleases)==2:
303 dummy = ROOT.TCanvas(
"dummy",
"dummy", 800, 600)
304 dummy.SaveAs(
"%s/AllAppliedSFCheckPlots%s.pdf[" % (Options.outDir, bonusname))
308 histoCR1 = comp.get_old_histo().TH1()
309 histoCR2 = comp.get_new_histo().TH1()
312 pu.Prepare1PadCanvas(comp.name())
313 pu.GetCanvas().SetLogy()
314 if comp.get_old_histo().has_log_binnnig(): pu.GetCanvas().SetLogx()
316 histoCR1.GetYaxis().SetTitle(
"Fraction of muons")
318 pu.drawStyling(histoCR1, 1,
max([histoCR1.GetMaximum(),histoCR2.GetMaximum()]) *1.e3, TopPad =
False)
319 histoCR1.SetTitle(
"%s, Mean: %.8f"%(calibReleases[0],histoCR1.GetMean()))
320 histoCR2.SetTitle(
"%s, Mean: %.8f"%(calibReleases[1],histoCR2.GetMean()))
322 histoCR1.Draw(
"sameHIST")
323 histoCR2.SetLineColor(ROOT.kRed)
324 histoCR2.SetMarkerColor(ROOT.kRed)
325 histoCR2.SetLineStyle(9)
326 histoCR2.Draw(
"sameHIST")
327 pu.CreateLegend(0.2, 0.7, 0.6, 0.8,18)
329 pu.AddToLegend([histoCR1, histoCR2])
331 variationDrawn =
"Nominal"
332 if "STAT" in comp.name(): variationDrawn =
"|Stat-Nominal|/Nominal"
333 elif "SYS" in comp.name(): variationDrawn =
"|Sys-Nominal|/Nominal"
334 type_drawn =
"Scale factor"
335 if comp.name().
split(
"_")[1] ==
"DataEff": type_drawn =
"data efficiency"
336 elif comp.name().
split(
"_")[1] ==
"MCEff": type_drawn =
"MC efficiency"
337 pu.DrawTLatex(0.55, 0.5, Options.bonuslabel)
338 pu.DrawTLatex(0.55, 0.85,
"WP: %s, %s"%(comp.name().
split(
"_")[0],variationDrawn))
339 pu.DrawTLatex(0.55, 0.9, type_drawn)
341 pu.DrawAtlas(0.2, 0.9)
342 pu.DrawSqrtS(0.2, 0.85)
345 pu.saveHisto(
"%s/AppliedSFCheck_%s%s"%(Options.outDir, comp.name(),bonusname), [
"pdf"])
346 pu.saveHisto(
"%s/AllAppliedSFCheckPlots%s" % (Options.outDir, bonusname), [
"pdf"])
348 dummy.SaveAs(
"%s/AllAppliedSFCheckPlots%s.pdf]" % (Options.outDir, bonusname))
351 print "INFO: Currently, only release comaparisons are implemented"