Coverage for src/km3dq_common/grl_library.py: 0%

91 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-25 11:58 +0000

1#! /usr/bin/env python 

2######################################################################################## 

3 

4from km3dq_common.config_library import configure_dataquality_tag 

5from km3dq_common.config_library import configure_var_name 

6from km3dq_common.config_library import configure_var_bit 

7from km3dq_common.config_library import configure_var_thresholds 

8from km3dq_common.common_library import create_ttree_from_qaqc 

9from km3dq_common.lw_db_library import decode_defect_diag_byte 

10 

11 

12######################################################################################## 

13def get_good_run_list(det, dq_tag_name, 

14 add_info=[], 

15 qaqc_input="qaqc_sftp"): 

16 ''' 

17 Production of good run lists 

18 

19 === Arguments === 

20 - det : detector name - [string] - Ex: "D0ARCA021", "D0ORCA018"... 

21 - dq_tag_name : data-quality tag name - [string] 

22 - add_info : list of additional informations (qaqc variables or defect) 

23 to be retrieved in the same time as the GRL - 

24 [array of strings] - 

25 Ex: ["livetime", "def_operation/partly", "sign_off/run_coordinator"] 

26 - source : QAQC source, a priori "qaqc_sftp" - [string] 

27 

28 === Output === 

29 - GRL dictionnary including the additional info. 

30 

31 ''' 

32 

33 dq_tag_0 = configure_dataquality_tag(dq_tag_name) 

34 

35 grl_0 = {"var_prop": {}, 

36 "good": {}, 

37 "poor": {}, 

38 "bad": {}, 

39 "discard": {}, 

40 "add_info": {}} 

41 

42 configure_var_name(grl_0['var_prop']) 

43 configure_var_bit(grl_0['var_prop']) 

44 configure_var_thresholds(grl_0['var_prop'], det, dq_tag_0) 

45 

46 # The TTree must contain: 

47 # - the run number and livetime 

48 # - the variables (QAQC + defects) used in the Veto / Qscore computation 

49 # (required by create_ttree_from_qaqc) 

50 # - the variables (QAQC + defects) used in the GRL generation 

51 # - the additional variables to be reported 

52 var_to_store = ["run", "livetime"] 

53 for i_var in (list(grl_0['var_prop']['veto_thresholds'].keys()) + 

54 list(grl_0['var_prop']['qsco_thresholds'].keys())): 

55 if i_var not in var_to_store: 

56 var_to_store.append(i_var) 

57 for i_q in ("good", "poor", "bad", "discard"): 

58 for i_var in list(grl_0['var_prop'][f'{i_q}_thresholds'].keys()): 

59 if all((i_var not in var_to_store, 

60 i_var != "veto", 

61 i_var != "qsco")): 

62 var_to_store.append(i_var) 

63 for i_var in add_info: 

64 if i_var.startswith("def_"): 

65 # Extract the defect type to be included in the tree 

66 i_var_def_type = i_var.split("/")[0] 

67 if i_var_def_type not in var_to_store: 

68 var_to_store.append(i_var_def_type) 

69 grl_0['add_info'][i_var] = [] 

70 else: 

71 if i_var not in var_to_store: 

72 var_to_store.append(i_var) 

73 grl_0['add_info'][i_var] = {} 

74 

75 (n1, _) = create_ttree_from_qaqc(det, 

76 var_to_store, 

77 qaqc_input, 

78 dq_tag_0, 

79 True) 

80 

81 for i_q in ("good", "poor", "bad", "discard"): 

82 grl_0[i_q]["run_list"] = [] 

83 grl_0[i_q]["livetime"] = 0. 

84 

85 # Loop on runs and extract the grl 

86 entries = n1.GetEntriesFast() 

87 for irun in range(entries): # One entry per run 

88 n1.GetEntry(irun) # Load event 

89 

90 this_run_grl = {} 

91 

92 # First treat discarded run. 

93 # A single failing criteria triggers a run discard 

94 this_run_grl['discard'] = False 

95 # Loop on all variables (QAQC variables, veto, qscore, defect) 

96 # used for this run list 

97 for i_var in grl_0['var_prop']['discard_thresholds']: 

98 if i_var.startswith("def_"): 

99 val = n1.GetLeaf(i_var).GetValue() 

100 decoded_defects = decode_defect_diag_byte(val, i_var) 

101 for i_def in grl_0['var_prop']['discard_thresholds'][i_var]: 

102 if i_def.startswith("KEEP_"): 

103 if i_def.replace("KEEP_", "") not in decoded_defects: 

104 this_run_grl['discard'] = True 

105 else: 

106 if i_def in decoded_defects: 

107 this_run_grl['discard'] = True 

108 else: 

109 val = n1.GetLeaf(i_var).GetValue() 

110 if any((val < grl_0['var_prop']['discard_thresholds'][i_var][0], 

111 val > grl_0['var_prop']['discard_thresholds'][i_var][1])): 

112 this_run_grl['discard'] = True 

113 

114 # GRL generation 

115 # All criteria must be fullfiled to be in a GRL 

116 for i_q in ("good", "poor", "bad"): 

117 this_run_grl[i_q] = True 

118 if this_run_grl['discard']: 

119 this_run_grl[i_q] = False 

120 continue 

121 # Loop on all variables (QAQC variables, veto, qscore, defect) 

122 # used for this run list 

123 for i_var in grl_0['var_prop'][f"{i_q}_thresholds"]: 

124 if i_var.startswith("def_"): 

125 val = n1.GetLeaf(i_var).GetValue() 

126 decoded_defects = decode_defect_diag_byte(val, i_var) 

127 for i_def in grl_0['var_prop'][f"{i_q}_thresholds"][i_var]: 

128 if i_def.startswith("KEEP_"): 

129 if i_def.replace("KEEP_", "") not in decoded_defects: 

130 this_run_grl[i_q] = False 

131 else: 

132 if i_def in decoded_defects: 

133 this_run_grl[i_q] = False 

134 else: 

135 val = n1.GetLeaf(i_var).GetValue() 

136 if any((val < grl_0['var_prop'][f"{i_q}_thresholds"][i_var][0], 

137 val > grl_0['var_prop'][f"{i_q}_thresholds"][i_var][1])): 

138 this_run_grl[i_q] = False 

139 

140 this_run_nb_grl = 0 

141 for i_q in ("good", "poor", "bad", "discard"): 

142 if this_run_grl[i_q]: 

143 this_run_nb_grl += 1 

144 

145 if this_run_nb_grl == 0: 

146 print(f"Run {n1.run} in no GRL") 

147 if this_run_nb_grl > 1: 

148 print(f"Run {n1.run} in >=1 GRL: {this_run_grl}") 

149 for i_q in ("good", "poor", "bad", "discard"): 

150 if this_run_grl[i_q]: 

151 grl_0[i_q]['run_list'].append(n1.run) 

152 grl_0[i_q]['livetime'] += n1.livetime 

153 

154 # Additional info to be displayed in the results 

155 for i_var in add_info: 

156 if i_var.startswith("def_"): 

157 i_var_def_type = i_var.split("/")[0] 

158 val = n1.GetLeaf(i_var_def_type).GetValue() 

159 decoded_defects = decode_defect_diag_byte(val, i_var_def_type) 

160 if i_var.split("/")[1] in decoded_defects: 

161 grl_0['add_info'][i_var].append(n1.run) 

162 else: 

163 val = n1.GetLeaf(i_var).GetValue() 

164 grl_0['add_info'][i_var][n1.run] = val 

165 

166 return grl_0