Coverage for src/km3dq_common/grl_library.py: 85%
40 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 14:13 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 14:13 +0000
1#! /usr/bin/env python
2########################################################################################
4from .config_library import configure_dataquality_tag
5from .config_library import configure_var_name
6from .config_library import configure_var_bit
7from .config_library import configure_var_thresholds
8from .common_library import create_ttree_from_qaqc
9from .lw_db_defect_library import decode_defect_diag_byte
10import numpy as np
13########################################################################################
14def get_good_run_list(det, dq_tag_name, add_info=[], qaqc_input="qaqc_sftp"):
15 """
16 Production of good run lists
18 === Arguments ===
19 - det : detector name - [string] - Ex: "D0ARCA021", "D0ORCA018"...
20 - dq_tag_name : data-quality tag name - [string]
21 - add_info : list of additional informations (qaqc variables or defect)
22 to be retrieved in the same time as the GRL -
23 [array of strings] -
24 Ex: ["livetime", "def_operation/partly", "sign_off/run_coordinator"]
25 - source : QAQC source, a priori "qaqc_sftp" - [string]
27 === Output ===
28 - GRL dictionnary including the additional info.
30 """
32 dq_tag_0 = configure_dataquality_tag(dq_tag_name)
34 grl_0 = {
35 "var_prop": {},
36 "good": {},
37 "poor": {},
38 "bad": {},
39 "discard": {},
40 "add_info": {},
41 }
43 configure_var_name(grl_0["var_prop"])
44 configure_var_bit(grl_0["var_prop"])
45 configure_var_thresholds(grl_0["var_prop"], det, dq_tag_0)
47 # The TTree must contain:
48 # - the run number and livetime
49 # - the variables (QAQC + defects) used in the Veto / Qscore computation
50 # (required by create_ttree_from_qaqc)
51 # - the variables (QAQC + defects) used in the GRL generation
52 # - the additional variables to be reported
54 var_to_store = ["run", "livetime_s"]
55 var_to_store += list(grl_0["var_prop"]["veto_thresholds"].keys())
56 var_to_store += list(grl_0["var_prop"]["qsco_thresholds"].keys())
58 for i_q in ("good", "poor", "bad", "discard"):
59 var_to_store += list(grl_0["var_prop"][f"{i_q}_thresholds"].keys())
61 for i_var in add_info:
62 if i_var.startswith("def_"):
63 # Extract the defect type to be included in the tree
64 i_var_def_type = i_var.split("/")[0]
65 var_to_store.append(i_var_def_type)
66 grl_0["add_info"][i_var] = []
67 else:
68 grl_0["add_info"][i_var] = {}
70 var_to_store = set(var_to_store)
71 var_to_store.discard("veto")
72 var_to_store.discard("qsco")
74 (n1, _) = create_ttree_from_qaqc(det, var_to_store, qaqc_input, dq_tag_0, True)
76 masks = {}
77 for quality in ("good", "poor", "bad", "discard"):
78 mask = np.ones(len(n1), dtype=bool)
80 for variable, (lower, upper) in grl_0["var_prop"][
81 f"{quality}_thresholds"
82 ].items():
83 mask = mask & (n1[variable] >= lower) & (n1[variable] <= upper)
85 if quality == "discard": # Discard is True when excluded
86 mask = ~mask
88 grl_0[quality]["run_list"] = n1.run[mask].values
89 grl_0[quality]["livetime_s"] = np.sum(n1.livetime_s[mask])
91 for variable in add_info:
92 grl_0["add_info"][variable] = dict(zip(n1.run, n1[variable]))
94 return grl_0