#! /usr/bin/env python
########################################################################################
from km3dq_common.config_library import configure_dataquality_tag
from km3dq_common.config_library import configure_var_name
from km3dq_common.config_library import configure_var_bit
from km3dq_common.config_library import configure_var_thresholds
from km3dq_common.common_library import create_ttree_from_qaqc
from km3dq_common.defect_library import decode_defect_diag_byte
########################################################################################
[docs]
def get_good_run_list(det, dq_tag_name,
add_info=[],
qaqc_input="qaqc_sftp"):
'''
Production of good run lists
=== Arguments ===
- det : detector name - [string] - Ex: "D0ARCA021", "D0ORCA018"...
- dq_tag_name : data-quality tag name - [string]
- add_info : list of additional informations (qaqc variables or defect)
to be retrieved in the same time as the GRL -
[array of strings] -
Ex: ["livetime", "def_operation/partly", "sign_off/run_coordinator"]
- source : QAQC source, a priori "qaqc_sftp" - [string]
=== Output ===
- GRL dictionnary including the additional info.
'''
dq_tag_0 = configure_dataquality_tag(dq_tag_name)
grl_0 = {"var_prop": {},
"golden": {},
"silver": {},
"bronze": {},
"discard": {},
"add_info": {}}
configure_var_name(grl_0['var_prop'])
configure_var_bit(grl_0['var_prop'])
configure_var_thresholds(grl_0['var_prop'], det, dq_tag_0['grl'])
# The TTree must contain:
# - the run number and livetime
# - the variables (QAQC + defects) used in the Veto / Qscore computation
# (required by create_ttree_from_qaqc)
# - the variables (QAQC + defects) used in the GRL generation
# - the additional variables to be reported
var_to_store = ["run", "livetime"]
for i_var in (list(grl_0['var_prop']['veto_thresholds'].keys()) +
list(grl_0['var_prop']['qsco_thresholds'].keys())):
if i_var not in var_to_store:
var_to_store.append(i_var)
for i_q in ("golden", "silver", "bronze", "discard"):
for i_var in list(grl_0['var_prop'][f'{i_q}_thresholds'].keys()):
if all((i_var not in var_to_store,
i_var != "veto",
i_var != "qsco")):
var_to_store.append(i_var)
for i_var in add_info:
if i_var.startswith("def_"):
# Extract the defect type to be included in the tree
i_var_def_type = i_var.split("/")[0]
if i_var_def_type not in var_to_store:
var_to_store.append(i_var_def_type)
grl_0['add_info'][i_var] = []
else:
if i_var not in var_to_store:
var_to_store.append(i_var)
grl_0['add_info'][i_var] = {}
(n1, _) = create_ttree_from_qaqc(det,
var_to_store,
qaqc_input,
dq_tag_0,
True)
for i_q in ("golden", "silver", "bronze", "discard"):
grl_0[i_q]["run_list"] = []
grl_0[i_q]["livetime"] = 0.
# Loop on runs and extract the grl
entries = n1.GetEntriesFast()
for irun in range(entries): # One entry per run
n1.GetEntry(irun) # Load event
this_run_grl = {}
# First treat discarded run.
# A single failing criteria triggers a run discard
this_run_grl['discard'] = False
# Loop on all variables (QAQC variables, veto, qscore, defect)
# used for this run list
for i_var in grl_0['var_prop']['discard_thresholds']:
if i_var.startswith("def_"):
val = n1.GetLeaf(i_var).GetValue()
decoded_defects = decode_defect_diag_byte(val, i_var)
for i_def in grl_0['var_prop']['discard_thresholds'][i_var]:
if i_def.startswith("KEEP_"):
if i_def.replace("KEEP_", "") not in decoded_defects:
this_run_grl['discard'] = True
else:
if i_def in decoded_defects:
this_run_grl['discard'] = True
else:
val = n1.GetLeaf(i_var).GetValue()
if any((val < grl_0['var_prop']['discard_thresholds'][i_var][0],
val > grl_0['var_prop']['discard_thresholds'][i_var][1])):
this_run_grl['discard'] = True
# GRL generation
# All criteria must be fullfiled to be in a GRL
for i_q in ("golden", "silver", "bronze"):
this_run_grl[i_q] = True
if this_run_grl['discard']:
this_run_grl[i_q] = False
continue
# Loop on all variables (QAQC variables, veto, qscore, defect)
# used for this run list
for i_var in grl_0['var_prop'][f"{i_q}_thresholds"]:
if i_var.startswith("def_"):
val = n1.GetLeaf(i_var).GetValue()
decoded_defects = decode_defect_diag_byte(val, i_var)
for i_def in grl_0['var_prop'][f"{i_q}_thresholds"][i_var]:
if i_def.startswith("KEEP_"):
if i_def.replace("KEEP_", "") not in decoded_defects:
this_run_grl[i_q] = False
else:
if i_def in decoded_defects:
this_run_grl[i_q] = False
else:
val = n1.GetLeaf(i_var).GetValue()
if any((val < grl_0['var_prop'][f"{i_q}_thresholds"][i_var][0],
val > grl_0['var_prop'][f"{i_q}_thresholds"][i_var][1])):
this_run_grl[i_q] = False
this_run_nb_grl = 0
for i_q in ("golden", "silver", "bronze", "discard"):
if this_run_grl[i_q]:
this_run_nb_grl += 1
if this_run_nb_grl == 0:
print(f"Run {n1.run} in no GRL")
if this_run_nb_grl > 1:
print(f"Run {n1.run} in >=1 GRL: {this_run_grl}")
for i_q in ("golden", "silver", "bronze", "discard"):
if this_run_grl[i_q]:
grl_0[i_q]['run_list'].append(n1.run)
grl_0[i_q]['livetime'] += n1.livetime
# Additional info to be displayed in the results
for i_var in add_info:
if i_var.startswith("def_"):
i_var_def_type = i_var.split("/")[0]
val = n1.GetLeaf(i_var_def_type).GetValue()
decoded_defects = decode_defect_diag_byte(val, i_var_def_type)
if i_var.split("/")[1] in decoded_defects:
grl_0['add_info'][i_var].append(n1.run)
else:
val = n1.GetLeaf(i_var).GetValue()
grl_0['add_info'][i_var][n1.run] = val
return grl_0