6 """Load SiPM QDC calibration constants into ROOT global objects.
8 The constants can be loaded either from a JSON file supplied directly or
9 by mapping a run number to a JSON file using a CSV file. In Monte Carlo
10 mode, the global calibration offset is set to zero.
14 runNumberOrJSONpath : int or str, optional
15 If an integer is provided, it is interpreted as a run number and used
16 to select the corresponding JSON file from ``csvPath`` (or the default
17 mapping). Any negative run number loads the default constants for MC.
18 If a string is provided, it is interpreted as the path to a JSON
19 constants file, and ``csvPath`` is ignored. The JSON constants files
20 contain subsystem-wide constant offsets (for VS, US, DS) as well as
21 calibration constants for each individual SiPM. For the default files,
23 /eos/experiment/sndlhc/calibration/MuFilter/SiPMqdcCalibration/SiPMqdcCalibrationConstantsPaths.csv.
24 csvPath : str, optional
25 Path to a CSV file containing run-number ranges and JSON file paths.
26 The CSV is expected to contain a header followed by rows of the form::
28 minimum_run_number,maximum_run_number,json_path
30 The lower and upper limits on the run number are BOTH inclusive.
31 If omitted, when a run number is provided, the default mapping CSV
37 If ``runNumberOrJSONpath`` is neither an integer nor a string, or if
38 the supplied run number cannot be found in the CSV mapping.
40 If the JSON or CSV file cannot be opened.
42 If the selected JSON file contains invalid JSON.
46 This function declares the required C++ header through ROOT and populates
47 ``ROOT.SiPM_qdc_calibration_constants`` with the values from the selected
51 if type(runNumberOrJSONpath) == str:
52 JSONpath = runNumberOrJSONpath
53 if csvPath
is not None:
54 warnings.warn(
"csvPath argument is ignored when runNumberOrJSONpath is a string.")
55 elif type(runNumberOrJSONpath)==int:
56 runNumber = runNumberOrJSONpath
61 if runNumber <= -1
and csvPath
is None:
62 warnings.warn(
"Any negative run number loads the default constants for MC, "
63 "unless a csv file with a mapping to constants for other "
64 "negative values is provided in the argument csvPath.")
68 warnings.warn(
"Using default SiPM qdc calibration constant mapping.")
69 csvPath =
"/eos/experiment/sndlhc/calibration/MuFilter/SiPMqdcCalibration/SiPMqdcCalibrationConstantsPaths.csv"
70 with open(csvPath)
as f:
71 warnings.warn(
"Reading SiPM qdc calibration constant mapping from " + csvPath +
".")
73 for line
in lines[1:]:
74 min_run, max_run, path = line.strip().split(
',')
75 if int(min_run) <= runNumber <= int(max_run):
79 raise ValueError(f
"Run number {runNumber} not found in CSV file {csvPath}.")
81 raise ValueError(
"Error: runNumberOrJSONpath must be either an integer "
82 "(run number) or a string (path to constants JSON file).")
84 ROOT.gInterpreter.Declare(
r"""
86 #include "SiPMqdcCalibrationConstants.h"
90 with open(JSONpath)
as f:
93 for k, v
in data.items():
94 ROOT.SiPM_qdc_calibration_constants[int(k)] = float(v)