Coverage for src/km3dq_common/lw_db_signoff_library.py: 0%
46 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-25 11:58 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-25 11:58 +0000
1#! /usr/bin/env python
2###############################################################################
3import re
4import urllib.request
6from km3dq_common.common_library import create_ttree_from_qaqc
8from km3dq_common.config_library import configure_dataquality_tag
9from km3dq_common.config_library import configure_defect
11from km3dq_common.lw_db_library import decode_defect_diag_byte
14###############################################################################
15def get_unsignedoff_runs(det, dq_tag_name="default"):
16 """
17 Return the unsignedoff PHYS runs based on the defect files stored on SFTP
18 ("signoff" defect type)
20 === Arguments ===
21 - det : detector name - [string] - Ex: "D0ARCA021", "D0ORCA018"...
22 - dq_tag_name : data quality tag name - [string] - Ex: "default", "neutrino2024"
24 === Output ===
25 - Dictionnary with def_[defect] as keys and a value for each affected run.
26 This value will be decoded by the decode_defect_diag_byte function.
27 Ex: {'def_daq': {}, 'def_operation': {16501: 1, 16503: 2}...}
29 """
30 # The DQ tag is needed as the signoff depends on the QAQC file that
31 # depends itself on the DQ tag
32 dq_tag = configure_dataquality_tag(dq_tag_name)
33 defects0 = configure_defect()
35 if "ORCA" in det:
36 site = "ORCA"
37 else:
38 site = "ARCA"
40 (tree_qaqc, _) = create_ttree_from_qaqc(det,
41 ["run"],
42 "qaqc_sftp",
43 dq_tag)
45 re_line = re.compile(r"\s*([0-9]+)\s*\|.*")
47 entries = tree_qaqc.GetEntriesFast()
48 res = {}
49 # Initialize the output for all runs as not yet signed off
50 for irun in range(entries): # One entry per run
51 tree_qaqc.GetEntry(irun) # Load event
52 res[tree_qaqc.run] = []
53 for i_diag in defects0['bit']["signoff"].keys():
54 res[tree_qaqc.run].append(i_diag)
56 # Now scan the defect files and removed the run signed off
57 for i_diag in defects0['bit']["signoff"].keys():
58 lines = []
59 defect_url = ("https://sftp.km3net.de/data/km3dq_lw_db/"
60 f"{site}/{det}/Defects/{dq_tag['def_tag']}/"
61 f"signoff_{i_diag}.txt")
62 try:
63 with urllib.request.urlopen(defect_url) as def_file:
64 tmp = ((def_file.read()).split(b"\n"))
65 for i_line in tmp:
66 if i_line != "":
67 lines.append(i_line.decode("utf-8"))
68 except urllib.error.URLError:
69 # No defect file found
70 continue
72 for i_line in lines:
73 r_m = re_line.match(i_line)
74 if r_m is not None:
75 run_nb = int(r_m.group(1))
76 # Run signed off not (yet?) in QAQC
77 if run_nb not in res.keys():
78 continue
79 signoff_nature_bit = 1 << \
80 defects0['bit']["signoff"][i_diag]
81 signoff_nature = decode_defect_diag_byte(signoff_nature_bit, "signoff")
82 for i_nature in signoff_nature.split(" / "):
83 if i_nature != "":
84 res[run_nb].remove(i_nature)
86 if len(res[run_nb]) == 0:
87 res.pop(run_nb)
89 return res