Skip to content

Commit

Permalink
Upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
vkucera committed Feb 20, 2025
1 parent 9196167 commit c26b78b
Show file tree
Hide file tree
Showing 27 changed files with 196 additions and 189 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repos:
rev: v3.19.1
hooks:
- id: pyupgrade
args: ["--py310-plus"]
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
hooks:
Expand Down
22 changes: 9 additions & 13 deletions machine_learning_hep/analysis/analyzerdhadrons.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def efficiency(self):
print(self.n_fileff)
lfileeff = TFile.Open(self.n_fileff)
lfileeff.ls()
fileouteff = TFile.Open("%s/efficiencies%s%s.root" % (self.d_resultsallpmc, self.case, self.typean), "recreate")
fileouteff = TFile.Open(f"{self.d_resultsallpmc}/efficiencies{self.case}{self.typean}.root", "recreate")
cEff = TCanvas("cEff", "The Fit Canvas")
cEff.SetCanvasSize(1900, 1500)
cEff.SetWindowSize(500, 500)
Expand All @@ -446,11 +446,11 @@ def efficiency(self):
h_sel_pr.SetName("eff")
h_sel_pr.Write()
h_sel_pr.GetXaxis().SetTitle("#it{p}_{T} (GeV/#it{c})")
h_sel_pr.GetYaxis().SetTitle("Acc x efficiency (prompt) %s %s (1/GeV)" % (self.p_latexnhadron, self.typean))
h_sel_pr.GetYaxis().SetTitle(f"Acc x efficiency (prompt) {self.p_latexnhadron} {self.typean} (1/GeV)")
h_sel_pr.SetMinimum(0.001)
h_sel_pr.SetMaximum(1.0)
gPad.SetLogy()
cEff.SaveAs("%s/Eff%s%s.eps" % (self.d_resultsallpmc, self.case, self.typean))
cEff.SaveAs(f"{self.d_resultsallpmc}/Eff{self.case}{self.typean}.eps")

cEffFD = TCanvas("cEffFD", "The Fit Canvas")
cEffFD.SetCanvasSize(1900, 1500)
Expand All @@ -471,12 +471,12 @@ def efficiency(self):
h_sel_fd.SetName("eff_fd")
h_sel_fd.Write()
h_sel_fd.GetXaxis().SetTitle("#it{p}_{T} (GeV/#it{c})")
h_sel_fd.GetYaxis().SetTitle("Acc x efficiency feed-down %s %s (1/GeV)" % (self.p_latexnhadron, self.typean))
h_sel_fd.GetYaxis().SetTitle(f"Acc x efficiency feed-down {self.p_latexnhadron} {self.typean} (1/GeV)")
h_sel_fd.SetMinimum(0.001)
h_sel_fd.SetMaximum(1.0)
gPad.SetLogy()
legeffFD.Draw()
cEffFD.SaveAs("%s/EffFD%s%s.eps" % (self.d_resultsallpmc, self.case, self.typean))
cEffFD.SaveAs(f"{self.d_resultsallpmc}/EffFD{self.case}{self.typean}.eps")

@staticmethod
def calculate_norm(logger, hevents, hselevents): # TO BE FIXED WITH EV SEL
Expand Down Expand Up @@ -506,7 +506,7 @@ def makenormyields(self): # pylint: disable=import-outside-toplevel, too-many-b
if not os.path.exists(fileouteff):
self.logger.fatal("Efficiency file %s could not be found", fileouteff)

fileoutcross = "%s/finalcross%s%s.root" % (self.d_resultsallpdata, self.case, self.typean)
fileoutcross = f"{self.d_resultsallpdata}/finalcross{self.case}{self.typean}.root"

namehistoeffprompt = "eff"
namehistoefffeed = "eff_fd"
Expand All @@ -528,13 +528,11 @@ def makenormyields(self): # pylint: disable=import-outside-toplevel, too-many-b
self.logger.warning("Number of events after event selection %d", selnorm)

if self.p_dobkgfromsideband:
fileoutbkg = TFile.Open(
"%s/Background_fromsidebands_%s_%s.root" % (self.d_resultsallpdata, self.case, self.typean)
)
fileoutbkg = TFile.Open(f"{self.d_resultsallpdata}/Background_fromsidebands_{self.case}_{self.typean}.root")
hbkg = fileoutbkg.Get("hbkg_fromsidebands")
hbkg.Scale(1.0 / selnorm)
fileoutbkgscaled = TFile.Open(
"%s/NormBackground_fromsidebands_%s_%s.root" % (self.d_resultsallpdata, self.case, self.typean),
f"{self.d_resultsallpdata}/NormBackground_fromsidebands_{self.case}_{self.typean}.root",
"RECREATE",
)
fileoutbkgscaled.cd()
Expand All @@ -559,9 +557,7 @@ def makenormyields(self): # pylint: disable=import-outside-toplevel, too-many-b
fileoutcross,
)

fileoutcrosstot = TFile.Open(
"%s/finalcross%s%stot.root" % (self.d_resultsallpdata, self.case, self.typean), "recreate"
)
fileoutcrosstot = TFile.Open(f"{self.d_resultsallpdata}/finalcross{self.case}{self.typean}tot.root", "recreate")

f_fileoutcross = TFile.Open(fileoutcross)
if f_fileoutcross:
Expand Down
60 changes: 31 additions & 29 deletions machine_learning_hep/analysis/analyzerdhadrons_mult.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,15 @@ def fit(self):
fileout.Close()

def get_efficiency(self, ibin1, ibin2):
fileouteff = TFile.Open("%s/efficiencies%s%s.root" % (self.d_resultsallpmc, self.case, self.typean), "read")
fileouteff = TFile.Open(f"{self.d_resultsallpmc}/efficiencies{self.case}{self.typean}.root", "read")
h = fileouteff.Get(f"eff_mult{ibin2}")
return h.GetBinContent(ibin1 + 1), h.GetBinError(ibin1 + 1)

def efficiency(self):
self.loadstyle()

lfileeff = TFile.Open(self.n_fileff)
fileouteff = TFile.Open("%s/efficiencies%s%s.root" % (self.d_resultsallpmc, self.case, self.typean), "recreate")
fileouteff = TFile.Open(f"{self.d_resultsallpmc}/efficiencies{self.case}{self.typean}.root", "recreate")
cEff = TCanvas("cEff", "The Fit Canvas")
cEff.SetCanvasSize(1900, 1500)
cEff.SetWindowSize(500, 500)
Expand All @@ -489,8 +489,10 @@ def efficiency(self):
legsl.SetTextSize(0.035)

for imult in range(self.p_nbin2):
stringbin2 = "_%s_%.2f_%.2f" % (self.v_var2_binning, self.lvar2_binmin[imult], self.lvar2_binmax[imult])
legeffstring = "%.1f #leq %s < %.1f" % (
stringbin2 = "_{}_{:.2f}_{:.2f}".format(
self.v_var2_binning, self.lvar2_binmin[imult], self.lvar2_binmax[imult]
)
legeffstring = "{:.1f} #leq {} < {:.1f}".format(
self.lvar2_binmin[imult],
self.p_latexbin2var,
self.lvar2_binmax[imult],
Expand Down Expand Up @@ -539,7 +541,7 @@ def efficiency(self):
if self.signal_loss:
cSl.cd()
legsl.Draw()
cSl.SaveAs("%s/SignalLoss%s%s.eps" % (self.d_resultsallpmc, self.case, self.typean))
cSl.SaveAs(f"{self.d_resultsallpmc}/SignalLoss{self.case}{self.typean}.eps")

cSlFD = TCanvas("cSlFD", "The Fit Canvas")
cSlFD.SetCanvasSize(1900, 1500)
Expand All @@ -553,7 +555,7 @@ def efficiency(self):

cEff.cd()
legeff.Draw()
cEff.SaveAs("%s/Eff%s%s.eps" % (self.d_resultsallpmc, self.case, self.typean))
cEff.SaveAs(f"{self.d_resultsallpmc}/Eff{self.case}{self.typean}.eps")

cEffFD = TCanvas("cEffFD", "The Fit Canvas")
cEffFD.SetCanvasSize(1900, 1500)
Expand All @@ -567,8 +569,10 @@ def efficiency(self):
legeffFD.SetTextSize(0.035)

for imult in range(self.p_nbin2):
stringbin2 = "_%s_%.2f_%.2f" % (self.v_var2_binning, self.lvar2_binmin[imult], self.lvar2_binmax[imult])
legeffFDstring = "%.1f #leq %s < %.1f" % (
stringbin2 = "_{}_{:.2f}_{:.2f}".format(
self.v_var2_binning, self.lvar2_binmin[imult], self.lvar2_binmax[imult]
)
legeffFDstring = "{:.1f} #leq {} < {:.1f}".format(
self.lvar2_binmin[imult],
self.p_latexbin2var,
self.lvar2_binmax[imult],
Expand Down Expand Up @@ -616,24 +620,22 @@ def efficiency(self):

cEffFD.cd()
legeffFD.Draw()
cEffFD.SaveAs("%s/EffFD%s%s.eps" % (self.d_resultsallpmc, self.case, self.typean))
cEffFD.SaveAs(f"{self.d_resultsallpmc}/EffFD{self.case}{self.typean}.eps")
if self.signal_loss:
cSlFD.cd()
legslFD.Draw()
cSlFD.SaveAs("%s/SignalLossFD%s%s.eps" % (self.d_resultsallpmc, self.case, self.typean))
cSlFD.SaveAs(f"{self.d_resultsallpmc}/SignalLossFD{self.case}{self.typean}.eps")

def plotter(self):
gROOT.SetBatch(True)
self.loadstyle()

fileouteff = TFile.Open("%s/efficiencies%s%s.root" % (self.d_resultsallpmc, self.case, self.typean))
fileouteff = TFile.Open(f"{self.d_resultsallpmc}/efficiencies{self.case}{self.typean}.root")
yield_filename = self.make_file_path(
self.d_resultsallpdata, self.yields_filename, "root", None, [self.case, self.typean]
)
fileoutyield = TFile.Open(yield_filename, "READ")
fileoutcross = TFile.Open(
"%s/finalcross%s%s.root" % (self.d_resultsallpdata, self.case, self.typean), "recreate"
)
fileoutcross = TFile.Open(f"{self.d_resultsallpdata}/finalcross{self.case}{self.typean}.root", "recreate")

cCrossvsvar1 = TCanvas("cCrossvsvar1", "The Fit Canvas")
cCrossvsvar1.SetCanvasSize(1900, 1500)
Expand Down Expand Up @@ -667,10 +669,10 @@ def plotter(self):
hcross.Scale(1.0 / norm)
fileoutcross.cd()
hcross.GetXaxis().SetTitle("#it{p}_{T} %s (GeV/#it{c})" % self.p_latexnhadron)
hcross.GetYaxis().SetTitle("d#sigma/d#it{p}_{T} (%s) %s" % (self.p_latexnhadron, self.typean))
hcross.GetYaxis().SetTitle(f"d#sigma/d#it{{p}}_{{T}} ({self.p_latexnhadron}) {self.typean}")
hcross.SetName("hcross%d" % imult)
hcross.GetYaxis().SetRangeUser(1e1, 1e10)
legvsvar1endstring = "%.1f < %s < %.1f" % (
legvsvar1endstring = "{:.1f} < {} < {:.1f}".format(
self.lvar2_binmin[imult],
self.p_latexbin2var,
self.lvar2_binmax[imult],
Expand All @@ -683,9 +685,7 @@ def plotter(self):
listvalerrpt = [hcross.GetBinError(ipt + 1) for ipt in range(self.p_nptbins)]
listvalueserr.append(listvalerrpt)
legvsvar1.Draw()
cCrossvsvar1.SaveAs(
"%s/Cross%s%sVs%s.eps" % (self.d_resultsallpdata, self.case, self.typean, self.v_var_binning)
)
cCrossvsvar1.SaveAs(f"{self.d_resultsallpdata}/Cross{self.case}{self.typean}Vs{self.v_var_binning}.eps")

cCrossvsvar2 = TCanvas("cCrossvsvar2", "The Fit Canvas")
cCrossvsvar2.SetCanvasSize(1900, 1500)
Expand Down Expand Up @@ -713,17 +713,15 @@ def plotter(self):
hcrossvsvar2[ipt].SetBinError(imult + 1, listvalueserr[imult][ipt])

hcrossvsvar2[ipt].GetYaxis().SetRangeUser(1e4, 1e10)
legvsvar2endstring = "%.1f < %s < %.1f GeV/#it{c}" % (
legvsvar2endstring = "{:.1f} < {} < {:.1f} GeV/#it{{c}}".format(
self.lpt_finbinmin[ipt],
"#it{p}_{T}",
self.lpt_finbinmax[ipt],
)
hcrossvsvar2[ipt].Draw("same")
legvsvar2.AddEntry(hcrossvsvar2[ipt], legvsvar2endstring, "LEP")
legvsvar2.Draw()
cCrossvsvar2.SaveAs(
"%s/Cross%s%sVs%s.eps" % (self.d_resultsallpdata, self.case, self.typean, self.v_var2_binning)
)
cCrossvsvar2.SaveAs(f"{self.d_resultsallpdata}/Cross{self.case}{self.typean}Vs{self.v_var2_binning}.eps")

@staticmethod
def calculate_norm(logger, hevents, hselevents): # TO BE FIXED WITH EV SEL
Expand Down Expand Up @@ -783,7 +781,9 @@ def makenormyields(self): # pylint: disable=import-outside-toplevel, too-many-b

if self.p_fprompt_from_mb:
if imult == 0:
fileoutcrossmb = "%s/finalcross%s%smult0.root" % (self.d_resultsallpdata, self.case, self.typean)
fileoutcrossmb = "{}/finalcross{}{}mult0.root".format(
self.d_resultsallpdata, self.case, self.typean
)
output_prompt = []
if self.p_nevents is not None:
norm = self.p_nevents
Expand Down Expand Up @@ -844,7 +844,7 @@ def makenormyields(self): # pylint: disable=import-outside-toplevel, too-many-b
)

fileoutcrosstot = TFile.Open(
"%s/finalcross%s%smulttot.root" % (self.d_resultsallpdata, self.case, self.typean), "recreate"
f"{self.d_resultsallpdata}/finalcross{self.case}{self.typean}multtot.root", "recreate"
)

for imult in range(self.p_nbin2):
Expand All @@ -871,7 +871,7 @@ def plotternormyields(self):
legvsvar1.SetFillStyle(0)
legvsvar1.SetTextFont(42)
legvsvar1.SetTextSize(0.035)
fileoutcrosstot = TFile.Open("%s/finalcross%s%smulttot.root" % (self.d_resultsallpdata, self.case, self.typean))
fileoutcrosstot = TFile.Open(f"{self.d_resultsallpdata}/finalcross{self.case}{self.typean}multtot.root")

for imult in range(self.p_nbin2):
hcross = fileoutcrosstot.Get("histoSigmaCorr%d" % imult)
Expand All @@ -880,9 +880,9 @@ def plotternormyields(self):
hcross.SetMarkerColor(imult + 1)
hcross.GetXaxis().SetTitle("#it{p}_{T} %s (GeV/#it{c})" % self.p_latexnhadron)
hcross.GetYaxis().SetTitleOffset(1.3)
hcross.GetYaxis().SetTitle("Corrected yield/events (%s) %s" % (self.p_latexnhadron, self.typean))
hcross.GetYaxis().SetTitle(f"Corrected yield/events ({self.p_latexnhadron}) {self.typean}")
hcross.GetYaxis().SetRangeUser(1e-10, 1)
legvsvar1endstring = "%.1f #leq %s < %.1f" % (
legvsvar1endstring = "{:.1f} #leq {} < {:.1f}".format(
self.lvar2_binmin[imult],
self.p_latexbin2var,
self.lvar2_binmax[imult],
Expand All @@ -891,6 +891,8 @@ def plotternormyields(self):
hcross.Draw("same")
legvsvar1.Draw()
cCrossvsvar1.SaveAs(
"%s/CorrectedYieldsNorm%s%sVs%s.eps" % (self.d_resultsallpdata, self.case, self.typean, self.v_var_binning)
"{}/CorrectedYieldsNorm{}{}Vs{}.eps".format(
self.d_resultsallpdata, self.case, self.typean, self.v_var_binning
)
)
fileoutcrosstot.Close()
36 changes: 22 additions & 14 deletions machine_learning_hep/analysis/do_systematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(self, path_database_analysis: str, typean: str):
self.logger.setLevel(logging.INFO)
self.verbose = False

with open(path_database_analysis, "r", encoding="utf-8") as file_in:
with open(path_database_analysis, encoding="utf-8") as file_in:
db_analysis = yaml.safe_load(file_in)
case = list(db_analysis.keys())[0]
self.datap = db_analysis[case]
Expand Down Expand Up @@ -135,7 +135,7 @@ def __init__(self, path_database_analysis: str, typean: str):
self.logger.critical(make_message_notfound("the variation database"))
if "/" not in path_database_variations:
path_database_variations = f"{os.path.dirname(path_database_analysis)}/{path_database_variations}"
with open(path_database_variations, "r", encoding="utf-8") as file_sys:
with open(path_database_variations, encoding="utf-8") as file_sys:
db_variations = yaml.safe_load(file_sys)

if not healthy_structure(db_variations):
Expand Down Expand Up @@ -492,16 +492,28 @@ def do_jet_systematics(self, var: str):
print("Variation: %s" % self.systematic_varlabels[sys_cat][sys_var])
leg_sysvar.AddEntry(
input_histograms_sys[iptjet][sys_cat][sys_var],
("%s, %s" % (self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var])),
(
"{}, {}".format(
self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var]
)
),
"P",
)
self.logger.info(
"Adding label %s",
("%s, %s" % (self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var])),
(
"{}, {}".format(
self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var]
)
),
)
print(
"Adding label %s"
% ("%s, %s" % (self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var]))
% (
"{}, {}".format(
self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var]
)
)
)
setup_histogram(input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys + 1))
input_histograms_sys[iptjet][sys_cat][sys_var].Draw("same")
Expand All @@ -522,7 +534,7 @@ def do_jet_systematics(self, var: str):
for sys_cat in range(self.n_sys_cat):
suffix2 = self.systematic_catnames[sys_cat]
nsys = 0
csysvar_each = TCanvas("csysvar_%s_%s" % (suffix2, suffix), "systematic variations" + suffix2 + suffix)
csysvar_each = TCanvas(f"csysvar_{suffix2}_{suffix}", "systematic variations" + suffix2 + suffix)
setup_canvas(csysvar_each)
csysvar_each.SetRightMargin(0.25)
leg_sysvar_each = TLegend(0.77, 0.2, 0.95, 0.85, self.systematic_catlabels[sys_cat]) # Rg
Expand Down Expand Up @@ -554,7 +566,7 @@ def do_jet_systematics(self, var: str):
)
input_histograms_sys[iptjet][sys_cat][sys_var].Draw("same")
nsys = nsys + 1
latex_text = "%g #leq %s < %g GeV/#it{c}" % (
latex_text = "{:g} #leq {} < {:g} GeV/#it{{c}}".format(
self.edges_ptjet_gen_min[iptjet],
self.latex_ptjet,
self.edges_ptjet_gen_max[iptjet],
Expand All @@ -571,9 +583,7 @@ def do_jet_systematics(self, var: str):
# plot ratios to the default

nsys = 0
csysvar_ratio = TCanvas(
"csysvar_ratio_%s_%s" % (suffix2, suffix), "systematic variations" + suffix2 + suffix
)
csysvar_ratio = TCanvas(f"csysvar_ratio_{suffix2}_{suffix}", "systematic variations" + suffix2 + suffix)
setup_canvas(csysvar_ratio)
csysvar_ratio.SetRightMargin(0.25)
leg_sysvar_ratio = TLegend(0.77, 0.2, 0.95, 0.85, self.systematic_catlabels[sys_cat]) # Rg
Expand Down Expand Up @@ -655,9 +665,7 @@ def do_jet_systematics(self, var: str):

# Plot efficiency variations

csysvar_eff = TCanvas(
"csysvar_eff_%s_%s" % (suffix2, suffix), "systematic variations" + suffix2 + suffix
)
csysvar_eff = TCanvas(f"csysvar_eff_{suffix2}_{suffix}", "systematic variations" + suffix2 + suffix)
setup_canvas(csysvar_eff)
csysvar_eff.SetRightMargin(0.25)
leg_sysvar_eff = TLegend(
Expand Down Expand Up @@ -715,7 +723,7 @@ def do_jet_systematics(self, var: str):
# Plot ratios of efficiency variations to the default efficiency

csysvar_eff_ratio = TCanvas(
"csysvar_eff_ratio_%s_%s" % (suffix2, suffix), "systematic variations" + suffix2 + suffix
f"csysvar_eff_ratio_{suffix2}_{suffix}", "systematic variations" + suffix2 + suffix
)
setup_canvas(csysvar_eff_ratio)
csysvar_eff_ratio.SetRightMargin(0.25)
Expand Down
Loading

0 comments on commit c26b78b

Please sign in to comment.