Commit ae8ab141 authored by Giulia Tuci's avatar Giulia Tuci
Browse files

Combine range_bkg_subtrcated checks in case of low stat

parent df0450bc
Pipeline #3915007 failed with stage
in 1 minute and 55 seconds
......@@ -216,6 +216,71 @@ def JSON_to_checks(check_results_json):
return checks_data, all_check_results
def _add_lowstat_range_bkg_subtracted(result_a, result_b):
"""Add results from two 'range_background_subtracted' checks on separate data,
in case one check is failing because of the lack of statistics.
Adds together the results of the 'range_background_subtracted' check on two different
datasets, in case one check (or both) is failing because of the low statistic in the sample.
The background subtraction is performed again on the full dataset.
Args:
result_a (CheckResult): Check result from dataset a.
result_b (CheckResult): Check result from dataset b.
Returns:
A CheckResult containing the a and b check results added. This should
be equivalent to if the check had been run once on the two datasets
combined.
"""
added_result = CheckResult(
check_type=result_a.check_type,
passed=True,
messages=result_a.messages + result_b.messages,
tree_data={tree: {} for tree in result_a.tree_data},
)
# Add data together and perform background subtraction
for tree in result_a.tree_data:
data_added = added_result.tree_data[tree]
data_a = result_a.tree_data[tree]
data_b = result_b.tree_data[tree]
data_added["histograms"] = []
data_added["error_type"] = "none"
data_added["background_range_low"] = data_a["background_range_low"]
data_added["background_range_high"] = data_a["background_range_high"]
data_added["signal_range"] = data_a["signal_range"]
for i in range(3):
data_added["histograms"].append(
data_a["histograms"][i] + data_b["histograms"][i]
)
if (data_added["histograms"][1].sum() == 0) or (
data_added["histograms"][2].sum() == 0
):
added_result.passed = False
data_added["error_type"] = "stat"
else:
alpha = (
2.0
* (
data_added["background_range_low"]["max"]
- data_added["background_range_low"]["min"]
)
/ (
data_added["signal_range"]["max"]
- data_added["signal_range"]["min"]
)
)
# Histogram subtraction
hsub = (
data_added["histograms"][1] + (-1 * alpha) * data_added["histograms"][2]
)
data_added["histograms"].append(hsub)
return added_result
def _add_check_results(result_a, result_b):
"""Add results from two of the same check on separate data.
......@@ -250,8 +315,20 @@ def _add_check_results(result_a, result_b):
tree_data={tree: {} for tree in result_a.tree_data},
)
# Can't combine checks where one or both failed
# In general, we can't combine checks where one or both failed
# However, we want to rerun background subtraction if the failing was due to the lack of statistics
if not added_result.passed:
if added_result.check_type == "range_bkg_subtracted":
result = any(
"not_stat" in tree.values() for tree in result_a.tree_data.values()
) or any(
"not_stat" in tree.values() for tree in result_b.tree_data.values()
)
if result:
raise ValueError("Cannot combine CheckResults if one (or both) failed")
else:
return _add_lowstat_range_bkg_subtracted(result_a, result_b)
else:
raise ValueError("Cannot combine CheckResults if one (or both) failed")
# Add data together
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment