1616import ROOT # pylint: disable=import-error
1717sys .path .insert (0 , '..' )
1818from cut_variation import CutVarMinimiser
19- from utils . style_formatter import set_object_style
19+ from style_formatter import set_object_style
2020
2121# pylint: disable=no-member,too-many-locals,too-many-statements
2222
@@ -27,29 +27,45 @@ def main(config):
2727 """
2828
2929 ROOT .gROOT .SetBatch (True )
30+ ROOT .TH1 .AddDirectory (False )
3031
3132 with open (config , encoding = "utf8" ) as fil :
3233 cfg = json .load (fil )
3334
3435 hist_rawy , hist_effp , hist_effnp = ([] for _ in range (3 ))
3536 for filename_rawy , filename_eff in zip (cfg ["rawyields" ]["inputfiles" ], cfg ["efficiencies" ]["inputfiles" ]):
3637 infile_rawy = ROOT .TFile .Open (os .path .join (cfg ["rawyields" ]["inputdir" ], filename_rawy ))
37- hist_rawy .append (infile_rawy .Get (cfg ["rawyields" ]["histoname" ]))
38+ hist_rawy_name = cfg ["rawyields" ]["histoname" ]
39+ hist_rawy .append (infile_rawy .Get (hist_rawy_name ))
40+ if (hist_rawy [- 1 ] is None ):
41+ sys .exit (f"Fatal error: Histogram with raw yield \" { hist_rawy_name } \" is absent. Exit." )
3842 hist_rawy [- 1 ].SetDirectory (0 )
3943 infile_rawy .Close ()
4044
4145 infile_eff = ROOT .TFile .Open (os .path .join (cfg ["efficiencies" ]["inputdir" ], filename_eff ))
42- hist_effp .append (infile_eff .Get (cfg ["efficiencies" ]["histonames" ]["prompt" ]))
43- hist_effnp .append (infile_eff .Get (cfg ["efficiencies" ]["histonames" ]["nonprompt" ]))
46+ hist_effp_name = cfg ["efficiencies" ]["histonames" ]["prompt" ]
47+ hist_effnp_name = cfg ["efficiencies" ]["histonames" ]["nonprompt" ]
48+ hist_effp .append (infile_eff .Get (hist_effp_name ))
49+ hist_effnp .append (infile_eff .Get (hist_effnp_name ))
50+ if (hist_effp [- 1 ] is None ):
51+ sys .exit (f"Fatal error: Histogram with efficiency for prompt \" { hist_effp_name } \" is absent. Exit." )
52+ if (hist_effnp [- 1 ] is None ):
53+ sys .exit (f"Fatal error: Histogram with efficiency for nonprompt \" { hist_effnp } \" is absent. Exit." )
4454 hist_effp [- 1 ].SetDirectory (0 )
4555 hist_effnp [- 1 ].SetDirectory (0 )
4656 infile_eff .Close ()
4757
4858 if cfg ["central_efficiency" ]["computerawfrac" ]:
4959 infile_name = os .path .join (cfg ["central_efficiency" ]["inputdir" ], cfg ["central_efficiency" ]["inputfile" ])
5060 infile_central_eff = ROOT .TFile .Open (infile_name )
51- hist_central_effp = infile_central_eff .Get (cfg ["central_efficiency" ]["histonames" ]["prompt" ])
52- hist_central_effnp = infile_central_eff .Get (cfg ["central_efficiency" ]["histonames" ]["nonprompt" ])
61+ hist_central_effp_name = cfg ["central_efficiency" ]["histonames" ]["prompt" ]
62+ hist_central_effp = infile_central_eff .Get (hist_central_effp_name )
63+ if (hist_central_effp is None ):
64+ sys .exit (f"Fatal error: Histogram with central efficiency for prompt \" { hist_central_effp_name } \" is absent. Exit." )
65+ hist_central_effnp_name = cfg ["central_efficiency" ]["histonames" ]["nonprompt" ]
66+ hist_central_effnp = infile_central_eff .Get (hist_central_effnp_name )
67+ if (hist_central_effnp is None ):
68+ sys .exit (f"Fatal error: Histogram with central efficiency for nonprompt \" { hist_central_effnp_name } \" is absent. Exit." )
5369 hist_central_effp .SetDirectory (0 )
5470 hist_central_effnp .SetDirectory (0 )
5571 infile_central_eff .Close ()
@@ -115,18 +131,19 @@ def main(config):
115131
116132 output = ROOT .TFile (os .path .join (cfg ["output" ]["directory" ], cfg ["output" ]["file" ]), "recreate" )
117133 n_sets = len (hist_rawy )
134+ pt_axis_title = hist_rawy [0 ].GetXaxis ().GetTitle ()
118135 for ipt in range (hist_rawy [0 ].GetNbinsX ()):
119136 pt_min = hist_rawy [0 ].GetXaxis ().GetBinLowEdge (ipt + 1 )
120137 pt_max = hist_rawy [0 ].GetXaxis ().GetBinUpEdge (ipt + 1 )
121138
122139 rawy , effp , effnp , unc_rawy , unc_effp , unc_effnp = (np .zeros (n_sets ) for _ in range (6 ))
123140 for iset , (hrawy , heffp , heffnp ) in enumerate (zip (hist_rawy , hist_effp , hist_effnp )):
124- rawy . itemset ( iset , hrawy .GetBinContent (ipt + 1 ) )
125- effp . itemset ( iset , heffp .GetBinContent (ipt + 1 ) )
126- effnp . itemset ( iset , heffnp .GetBinContent (ipt + 1 ) )
127- unc_rawy . itemset ( iset , hrawy .GetBinError (ipt + 1 ) )
128- unc_effp . itemset ( iset , heffp .GetBinError (ipt + 1 ) )
129- unc_effnp . itemset ( iset , heffnp .GetBinError (ipt + 1 ) )
141+ rawy [ iset ] = hrawy .GetBinContent (ipt + 1 )
142+ effp [ iset ] = heffp .GetBinContent (ipt + 1 )
143+ effnp [ iset ] = heffnp .GetBinContent (ipt + 1 )
144+ unc_rawy [ iset ] = hrawy .GetBinError (ipt + 1 )
145+ unc_effp [ iset ] = heffp .GetBinError (ipt + 1 )
146+ unc_effnp [ iset ] = heffnp .GetBinError (ipt + 1 )
130147
131148 minimiser = CutVarMinimiser (rawy , effp , effnp , unc_rawy , unc_effp , unc_effnp )
132149 minimiser .minimise_system (cfg ["minimisation" ]["correlated" ])
@@ -155,30 +172,32 @@ def main(config):
155172 hist_frac_raw_nonprompt .SetBinContent (ipt + 1 , raw_frac_nonprompt [0 ])
156173 hist_frac_raw_nonprompt .SetBinError (ipt + 1 , raw_frac_nonprompt [1 ])
157174
158- canv_rawy , histos_rawy , leg_r = minimiser .plot_result (f"_pt{ pt_min :.0f} _{ pt_max :.0f} " )
175+ hist_bin_title = f"bin # { ipt + 1 } ; { pt_axis_title } #in ({ pt_min } ; { pt_max } )"
176+
177+ canv_rawy , histos_rawy , leg_r = minimiser .plot_result (f"_pt{ pt_min } _{ pt_max } " , hist_bin_title )
159178 output .cd ()
160179 canv_rawy .Write ()
161180 for _ , hist in histos_rawy .items ():
162181 hist .Write ()
163182
164- canv_eff , histos_eff , leg_e = minimiser .plot_efficiencies (f"_pt{ pt_min :.0f } _{ pt_max :.0f } " )
183+ canv_eff , histos_eff , leg_e = minimiser .plot_efficiencies (f"_pt{ pt_min } _{ pt_max } " , hist_bin_title )
165184 output .cd ()
166185 canv_eff .Write ()
167186 for _ , hist in histos_eff .items ():
168187 hist .Write ()
169188
170- canv_frac , histos_frac , leg_f = minimiser .plot_fractions (f"_pt{ pt_min :.0f } _{ pt_max :.0f } " )
189+ canv_frac , histos_frac , leg_f = minimiser .plot_fractions (f"_pt{ pt_min } _{ pt_max } " , hist_bin_title )
171190 output .cd ()
172191 canv_frac .Write ()
173192 for _ , hist in histos_frac .items ():
174193 hist .Write ()
175194
176- canv_cov , histo_cov = minimiser .plot_cov_matrix (True , f"_pt{ pt_min :.0f } _{ pt_max :.0f } " )
195+ canv_cov , histo_cov = minimiser .plot_cov_matrix (True , f"_pt{ pt_min } _{ pt_max } " , hist_bin_title )
177196 output .cd ()
178197 canv_cov .Write ()
179198 histo_cov .Write ()
180199
181- canv_combined = ROOT .TCanvas ("canv_combined " , "" , 1000 , 1000 )
200+ canv_combined = ROOT .TCanvas (f"canv_combined_ { ipt } " , "" , 1000 , 1000 )
182201 canv_combined .Divide (2 , 2 )
183202 canv_combined .cd (1 )
184203 canv_rawy .DrawClonePad ()
@@ -194,23 +213,20 @@ def main(config):
194213 output_name_frac_pdf = f"Frac_{ cfg ['output' ]['file' ].replace ('.root' , '.pdf' )} "
195214 output_name_covmat_pdf = f"CovMatrix_{ cfg ['output' ]['file' ].replace ('.root' , '.pdf' )} "
196215 output_name_pdf = f"{ cfg ['output' ]['file' ].replace ('.root' , '.pdf' )} "
197- if ipt == 0 :
198- canv_rawy .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_rawy_pdf )} [" )
199- canv_eff .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_eff_pdf )} [" )
200- canv_frac .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_frac_pdf )} [" )
201- 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 )} [" )
203- canv_rawy .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_rawy_pdf )} " )
204- canv_eff .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_eff_pdf )} " )
205- canv_frac .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_frac_pdf )} " )
206- 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 )} " )
208- if ipt == hist_rawy [0 ].GetNbinsX () - 1 :
209- canv_rawy .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_rawy_pdf )} ]" )
210- canv_eff .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_eff_pdf )} ]" )
211- canv_frac .SaveAs (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_frac_pdf )} ]" )
212- 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 )} ]" )
216+
217+ if hist_rawy [0 ].GetNbinsX () == 1 :
218+ print_bracket = ""
219+ elif ipt == 0 :
220+ print_bracket = "("
221+ elif ipt == hist_rawy [0 ].GetNbinsX () - 1 :
222+ print_bracket = ")"
223+ else :
224+ print_bracket = ""
225+ canv_rawy .Print (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_rawy_pdf )} { print_bracket } " )
226+ canv_eff .Print (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_eff_pdf )} { print_bracket } " )
227+ canv_frac .Print (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_frac_pdf )} { print_bracket } " )
228+ canv_cov .Print (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_covmat_pdf )} { print_bracket } " )
229+ canv_combined .Print (f"{ os .path .join (cfg ['output' ]['directory' ], output_name_pdf )} { print_bracket } " )
214230
215231 output .cd ()
216232 hist_corry_prompt .Write ()
0 commit comments