Skip to content

Commit 5715930

Browse files
authored
[PWGHF] Add methods to compute fraction, provided both efficiencies and cut-variation results externally (AliceO2Group#9616)
1 parent 5081d30 commit 5715930

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

PWGHF/D2H/Macros/compute_fraction_cutvar.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import argparse
1111
import json
1212
import os
13+
import sys
1314

1415
import numpy as np # pylint: disable=import-error
1516
import ROOT # pylint: disable=import-error
17+
sys.path.insert(0, '..')
1618
from cut_variation import CutVarMinimiser
17-
from style_formatter import set_object_style
19+
from utils.style_formatter import set_object_style
1820

1921
# pylint: disable=no-member,too-many-locals,too-many-statements
2022

@@ -176,24 +178,39 @@ def main(config):
176178
canv_cov.Write()
177179
histo_cov.Write()
178180

181+
canv_combined = ROOT.TCanvas("canv_combined", "", 1000, 1000)
182+
canv_combined.Divide(2, 2)
183+
canv_combined.cd(1)
184+
canv_rawy.DrawClonePad()
185+
canv_combined.cd(2)
186+
canv_eff.DrawClonePad()
187+
canv_combined.cd(3)
188+
canv_frac.DrawClonePad()
189+
canv_combined.cd(4)
190+
canv_cov.DrawClonePad()
191+
179192
output_name_rawy_pdf = f"Distr_{cfg['output']['file'].replace('.root', '.pdf')}"
180193
output_name_eff_pdf = f"Eff_{cfg['output']['file'].replace('.root', '.pdf')}"
181194
output_name_frac_pdf = f"Frac_{cfg['output']['file'].replace('.root', '.pdf')}"
182195
output_name_covmat_pdf = f"CovMatrix_{cfg['output']['file'].replace('.root', '.pdf')}"
196+
output_name_pdf = f"{cfg['output']['file'].replace('.root', '.pdf')}"
183197
if ipt == 0:
184198
canv_rawy.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_rawy_pdf)}[")
185199
canv_eff.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_eff_pdf)}[")
186200
canv_frac.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_frac_pdf)}[")
187201
canv_cov.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_covmat_pdf)}[")
202+
canv_combined.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_pdf)}[")
188203
canv_rawy.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_rawy_pdf)}")
189204
canv_eff.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_eff_pdf)}")
190205
canv_frac.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_frac_pdf)}")
191206
canv_cov.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_covmat_pdf)}")
207+
canv_combined.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_pdf)}")
192208
if ipt == hist_rawy[0].GetNbinsX() - 1:
193209
canv_rawy.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_rawy_pdf)}]")
194210
canv_eff.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_eff_pdf)}]")
195211
canv_frac.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_frac_pdf)}]")
196212
canv_cov.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_covmat_pdf)}]")
213+
canv_combined.SaveAs(f"{os.path.join(cfg['output']['directory'], output_name_pdf)}]")
197214

198215
output.cd()
199216
hist_corry_prompt.Write()

PWGHF/D2H/Macros/cut_variation.py

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import numpy as np # pylint: disable=import-error
1313
import ROOT # pylint: disable=import-error
14-
from style_formatter import set_global_style, set_object_style
14+
sys.path.insert(0, '..')
15+
from utils.style_formatter import set_global_style, set_object_style
1516

1617

1718
# pylint: disable=too-many-instance-attributes
@@ -295,6 +296,49 @@ def get_raw_prompt_fraction(self, effacc_p, effacc_np):
295296

296297
return f_p, f_p_unc
297298

299+
def get_raw_prompt_fraction_ext(self, corry_p, corry_np, unc_corry_p,
300+
unc_corry_np, cov_p_np, effacc_p, effacc_np):
301+
"""
302+
Helper function to get the raw prompt fraction given the efficiencies
303+
304+
Parameters
305+
-----------------------------------------------------
306+
- corry_p: float
307+
corrected yield for prompt signal
308+
- corry_np: float
309+
corrected yield for non-prompt signal
310+
- unc_corry_np: float
311+
uncertainty on corrected yield for prompt signal
312+
- unc_corry_np: float
313+
uncertainty on corrected yield for non-prompt signal
314+
- cov_p_np: float
315+
covariance between prompt and non-prompt signal
316+
- effacc_p: float
317+
eff x acc for prompt signal
318+
- effacc_np: float
319+
eff x acc for non-prompt signal
320+
321+
Returns
322+
-----------------------------------------------------
323+
- f_p, f_p_unc: (float, float)
324+
raw prompt fraction with its uncertainty
325+
"""
326+
327+
rawy_p = effacc_p * corry_p
328+
rawy_np = effacc_np * corry_np
329+
f_p = rawy_p / (rawy_p + rawy_np)
330+
331+
# derivatives of prompt fraction wrt corr yields
332+
d_p = (effacc_p * (rawy_p + rawy_np) - effacc_p**2 * corry_p) / (rawy_p + rawy_np) ** 2
333+
d_np = -effacc_np * rawy_p / (rawy_p + rawy_np) ** 2
334+
f_p_unc = np.sqrt(
335+
d_p**2 * unc_corry_p**2
336+
+ d_np**2 * unc_corry_np**2
337+
+ 2 * d_p * d_np * cov_p_np
338+
)
339+
340+
return f_p, f_p_unc
341+
298342
def get_raw_nonprompt_fraction(self, effacc_p, effacc_np):
299343
"""
300344
Helper function to get the raw non-prompt fraction given the efficiencies
@@ -318,6 +362,41 @@ def get_raw_nonprompt_fraction(self, effacc_p, effacc_np):
318362

319363
return f_np, f_np_unc
320364

365+
def get_raw_nonprompt_fraction_ext(self, corry_p, corry_np, unc_corry_p,
366+
unc_corry_np, cov_p_np, effacc_p, effacc_np):
367+
"""
368+
Helper function to get the raw non-prompt fraction given the efficiencies
369+
370+
Parameters
371+
-----------------------------------------------------
372+
- corry_p: float
373+
corrected yield for prompt signal
374+
- corry_np: float
375+
corrected yield for non-prompt signal
376+
- unc_corry_np: float
377+
uncertainty on corrected yield for prompt signal
378+
- unc_corry_np: float
379+
uncertainty on corrected yield for non-prompt signal
380+
- cov_p_np: float
381+
covariance between prompt and non-prompt signal
382+
- effacc_p: float
383+
eff x acc for prompt signal
384+
- effacc_np: float
385+
eff x acc for non-prompt signal
386+
387+
Returns
388+
-----------------------------------------------------
389+
- f_np, f_np_unc: (float, float)
390+
raw non-prompt fraction with its uncertainty
391+
392+
"""
393+
394+
f_p, f_np_unc = self.get_raw_prompt_fraction_ext(corry_p, corry_np, unc_corry_p,
395+
unc_corry_np, cov_p_np, effacc_p, effacc_np)
396+
f_np = 1 - f_p
397+
398+
return f_np, f_np_unc
399+
321400
def get_corr_prompt_fraction(self):
322401
"""
323402
Helper function to get the corrected prompt fraction
@@ -365,7 +444,7 @@ def plot_result(self, suffix=""):
365444
needed otherwise it is destroyed
366445
"""
367446

368-
set_global_style(padleftmargin=0.16, padbottommargin=0.12, titleoffsety=1.6)
447+
set_global_style(padleftmargin=0.16, padbottommargin=0.12, padtopmargin=0.075, titleoffsety=1.6)
369448

370449
hist_raw_yield = ROOT.TH1F(
371450
f"hRawYieldVsCut{suffix}",
@@ -435,7 +514,7 @@ def plot_result(self, suffix=""):
435514
hist_raw_yield.GetMaximum() * 1.2,
436515
";cut set;raw yield",
437516
)
438-
leg = ROOT.TLegend(0.6, 0.65, 0.8, 0.9)
517+
leg = ROOT.TLegend(0.6, 0.65, 0.8, 0.85)
439518
leg.SetBorderSize(0)
440519
leg.SetFillStyle(0)
441520
leg.SetTextSize(0.04)

0 commit comments

Comments
 (0)