SND@LHC Software
Loading...
Searching...
No Matches
SiPMqdcConstantLoader Namespace Reference

Functions

 loadConstants (runNumberOrJSONpath=None, csvPath=None)
 

Function Documentation

◆ loadConstants()

SiPMqdcConstantLoader.loadConstants (   runNumberOrJSONpath = None,
  csvPath = None 
)
Load SiPM QDC calibration constants into ROOT global objects.

The constants can be loaded either from a JSON file supplied directly or
by mapping a run number to a JSON file using a CSV file. In Monte Carlo
mode, the global calibration offset is set to zero.

Parameters
----------
runNumberOrJSONpath : int or str, optional
    If an integer is provided, it is interpreted as a run number and used
    to select the corresponding JSON file from ``csvPath`` (or the default
    mapping). Any negative run number loads the default constants for MC.
    If a string is provided, it is interpreted as the path to a JSON
    constants file, and ``csvPath`` is ignored. The JSON constants files
    contain subsystem-wide constant offsets (for VS, US, DS) as well as
    calibration constants for each individual SiPM. For the default files,
    see their paths in
    /eos/experiment/sndlhc/calibration/MuFilter/SiPMqdcCalibration/SiPMqdcCalibrationConstantsPaths.csv.
csvPath : str, optional
    Path to a CSV file containing run-number ranges and JSON file paths.
    The CSV is expected to contain a header followed by rows of the form::

        minimum_run_number,maximum_run_number,json_path

    The lower and upper limits on the run number are BOTH inclusive.
    If omitted, when a run number is provided, the default mapping CSV
    file is used.

Raises
------
ValueError
    If ``runNumberOrJSONpath`` is neither an integer nor a string, or if
    the supplied run number cannot be found in the CSV mapping.
FileNotFoundError
    If the JSON or CSV file cannot be opened.
json.JSONDecodeError
    If the selected JSON file contains invalid JSON.

Notes
-----
This function declares the required C++ header through ROOT and populates
``ROOT.SiPM_qdc_calibration_constants`` with the values from the selected
JSON file.

Definition at line 5 of file SiPMqdcConstantLoader.py.

5def loadConstants(runNumberOrJSONpath=None, csvPath=None):
6 """Load SiPM QDC calibration constants into ROOT global objects.
7
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.
11
12 Parameters
13 ----------
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,
22 see their paths in
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::
27
28 minimum_run_number,maximum_run_number,json_path
29
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
32 file is used.
33
34 Raises
35 ------
36 ValueError
37 If ``runNumberOrJSONpath`` is neither an integer nor a string, or if
38 the supplied run number cannot be found in the CSV mapping.
39 FileNotFoundError
40 If the JSON or CSV file cannot be opened.
41 json.JSONDecodeError
42 If the selected JSON file contains invalid JSON.
43
44 Notes
45 -----
46 This function declares the required C++ header through ROOT and populates
47 ``ROOT.SiPM_qdc_calibration_constants`` with the values from the selected
48 JSON file.
49 """
50
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
57
58 # any negative run number is treated as MC the same way,
59 # unless a csv file (which might contain constants for different
60 # negative run numbers, e.g. different MC) is provided
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.")
65 runNumber = -1
66
67 if csvPath is None: # use default mapping
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 + ".")
72 lines = f.readlines()
73 for line in lines[1:]:
74 min_run, max_run, path = line.strip().split(',')
75 if int(min_run) <= runNumber <= int(max_run):
76 JSONpath = path
77 break
78 else:
79 raise ValueError(f"Run number {runNumber} not found in CSV file {csvPath}.")
80 else:
81 raise ValueError("Error: runNumberOrJSONpath must be either an integer "
82 "(run number) or a string (path to constants JSON file).")
83
84 ROOT.gInterpreter.Declare(r"""
85 #include <map>
86 #include "SiPMqdcCalibrationConstants.h"
87 """)
88
89 # Load constants and store in root object
90 with open(JSONpath) as f:
91 data = json.load(f)
92
93 for k, v in data.items():
94 ROOT.SiPM_qdc_calibration_constants[int(k)] = float(v)