Skip to content

Commit

Permalink
Putting scaling determination to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvbrt committed Sep 16, 2024
1 parent 30e340e commit 14d9437
Showing 1 changed file with 51 additions and 72 deletions.
123 changes: 51 additions & 72 deletions python/do_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,51 @@ def formatStatUncHist(hists, name, hstyle=3254):
return hist_tot


# _____________________________________________________________________________
def determine_lumi_scaling(config: dict[str, any],
infile: object,
initial_scale: float = 1.0) -> float:
'''
Determine whether to (re)scale histograms in the file to luminosity.
'''
scale: float = initial_scale

# Check if histograms were already scaled to lumi
try:
scaled: bool = infile.scaled.GetVal()
except AttributeError:
LOGGER.error('Input file does not contain scaling '
'information!\n %s\nAborting...', infile.GetName())
sys.exit(3)

if scaled:
try:
int_lumi_in_file: float = infile.intLumi.GetVal()
except AttributeError:
LOGGER.error('Can not load integrated luminosity '
'value from the input file!\n %s\n'
'Aborting...', infile.GetName())

if config['int_lumi'] != int_lumi_in_file:
LOGGER.warning(
'Histograms are already scaled to different '
'luminosity value!\n'
'Luminosity in the input file is %s pb-1 and '
'luminosity requested in plots script is %s pb-1.',
int_lumi_in_file, config['int_lumi'])
if config['do_scale']:
LOGGER.warning(
'Rescaling from %s pb-1 to %s pb-1...',
int_lumi_in_file, config['int_lumi'])
scale *= config['int_lumi'] / int_lumi_in_file

else:
if config['do_scale']:
scale = scale * config['int_lumi']

return scale


# _____________________________________________________________________________
def load_hists(var: str,
label: str,
Expand Down Expand Up @@ -83,43 +128,10 @@ def load_hists(var: str,
hist = copy.deepcopy(infile.Get(var))
hist.SetDirectory(0)

scale = config['scale_sig']

# Check if histograms were already scaled to lumi
try:
scaled = infile.scaled
except AttributeError:
LOGGER.error('Input file does not contain scaling '
'information!\n %s\nAborting...', infilepath)
sys.exit(3)

if scaled:
try:
int_lumi_in_file = infile.intLumi.GetVal()
except AttributeError:
LOGGER.error('Can not load integrated luminosity '
'value from the input file!\n %s\n'
'Aborting...', infilepath)

if config['int_lumi'] != int_lumi_in_file:
LOGGER.warning(
'Histograms are already scaled to different '
'luminosity value!\n'
'Luminosity in the input file is %s pb-1 and '
'luminosity requested in plots script is %s pb-1.',
int_lumi_in_file, config['int_lumi'])
if config['do_scale']:
LOGGER.warning(
'Rescaling from %s pb-1 to %s pb-1...',
int_lumi_in_file, config['int_lumi'])
scale *= config['int_lumi'] / int_lumi_in_file

else:
if config['do_scale']:
scale = scale * config['int_lumi']

scale = determine_lumi_scaling(config,
infile,
config['scale_sig'])
hist.Scale(scale)

hist.Rebin(rebin)

if len(hsignal[s]) == 0:
Expand All @@ -142,43 +154,10 @@ def load_hists(var: str,
hist = copy.deepcopy(infile.Get(var))
hist.SetDirectory(0)

scale = config['scale_bkg']

# Check if histograms were already scaled to lumi
try:
scaled = infile.scaled
except AttributeError:
LOGGER.error('Input file does not contain scaling '
'information!\n %s\nAborting...', infilepath)
sys.exit(3)

if scaled:
try:
int_lumi_in_file = infile.intLumi.GetVal()
except AttributeError:
LOGGER.error('Can not load integrated luminosity '
'value from the input file!\n %s\n'
'Aborting...', infilepath)

if config['int_lumi'] != int_lumi_in_file:
LOGGER.warning(
'Histograms are already scaled to different '
'luminosity value!\n'
'Luminosity in the input file is %s pb-1 and '
'luminosity requested in plots script is %s pb-1.',
int_lumi_in_file, config['int_lumi'])
if config['do_scale']:
LOGGER.warning(
'Rescaling from %s pb-1 to %s pb-1...',
int_lumi_in_file, config['int_lumi'])
scale *= config['int_lumi'] / int_lumi_in_file

else:
if config['do_scale']:
scale = scale * config['int_lumi']

scale = determine_lumi_scaling(config,
infile,
config['scale_bkg'])
hist.Scale(scale)

hist.Rebin(rebin)

if len(hbackgrounds[b]) == 0:
Expand Down

0 comments on commit 14d9437

Please sign in to comment.