Commit 59261879 authored by Aidan Richard Wiederhold's avatar Aidan Richard Wiederhold
Browse files

Merge branch 'djwhite/checks-root-naming' into 'master'

Store all histograms in one root file + add hists to JSON

See merge request !45
parents 6e27ff88 c76ff50f
Pipeline #3451601 passed with stages
in 2 minutes and 13 seconds
......@@ -13,24 +13,27 @@ Contains utility functions used to display and save the output of the checks.
"""
import copy
import json
from os.path import join
import uproot
from uproot.writing.identify import to_writable
def hist_to_root(job_name, check_result, output_path):
def hist_to_root(job_name, check_results, output_path):
"""
Save histograms in a root file.
"""
# Create the file only if the check produce histograms in output
checks_with_histo = ["range", "range_nd", "range_bkg_subtracted"]
if check_result.check_type in checks_with_histo:
file_name = f"{job_name}_{check_result.check_type}_histograms.root"
with uproot.recreate(join(output_path, file_name)) as file_root:
for key, data in check_result.tree_data.items():
for hist_counter, _histo in enumerate(data.get("histograms", [])):
histo_name = f"{key}/{hist_counter-1}"
file_root[histo_name] = _histo
with uproot.recreate(output_path) as file_root:
for cr in check_results:
if (
check_results[cr].passed
and check_results[cr].check_type in checks_with_histo
):
for key, data in check_results[cr].tree_data.items():
for hist_counter, _histo in enumerate(data.get("histograms", [])):
histo_name = f"{key}/{cr}_{hist_counter}"
file_root[histo_name] = _histo
def checks_to_JSON(
......@@ -54,13 +57,16 @@ def checks_to_JSON(
result[job][check]["input"] = checks_data[check]
result[job][check]["output"] = all_check_results_copy[job][check].tree_data
# Temporary: remove histograms until a good way to serialise them is found
# Convert histograms to ROOT's JSON output by converting using uproot's to_writable()
for tree in result[job][check]["output"]:
if "histograms" in result[job][check]["output"][tree]:
hist_placeholder = ["placeholder"] * len(
result[job][check]["output"][tree]["histograms"]
)
result[job][check]["output"][tree]["histograms"] = hist_placeholder
n_hists = len(result[job][check]["output"][tree]["histograms"])
for n in range(n_hists):
result[job][check]["output"][tree]["histograms"][
n
] = to_writable(
result[job][check]["output"][tree]["histograms"][n]
).tojson()
if json_output_path is not None:
with open(json_output_path, "w", encoding="utf8") as json_file:
......
......@@ -177,7 +177,185 @@ def test_range_parsing_to_JSON():
"output": {
"DecayTree": {
"histograms": [
"placeholder"
{
"_typename": "TH1D",
"fUniqueID": 0,
"fBits": 0,
"fName": null,
"fTitle": "",
"fLineColor": 602,
"fLineStyle": 1,
"fLineWidth": 1,
"fFillColor": 0,
"fFillStyle": 1001,
"fMarkerColor": 1,
"fMarkerStyle": 1,
"fMarkerSize": 1.0,
"fNcells": 52,
"fXaxis": {
"_typename": "TAxis",
"fUniqueID": 0,
"fBits": 0,
"fName": "H1_PZ",
"fTitle": "H1_PZ",
"fNdivisions": 510,
"fAxisColor": 1,
"fLabelColor": 1,
"fLabelFont": 42,
"fLabelOffset": 0.005,
"fLabelSize": 0.035,
"fTickLength": 0.03,
"fTitleOffset": 1.0,
"fTitleSize": 0.035,
"fTitleColor": 1,
"fTitleFont": 42,
"fNbins": 50,
"fXmin": 0.0,
"fXmax": 500000.0,
"fXbins": [],
"fFirst": 0,
"fLast": 0,
"fBits2": 0,
"fTimeDisplay": false,
"fTimeFormat": "",
"fLabels": null,
"fModLabs": null
},
"fYaxis": {
"_typename": "TAxis",
"fUniqueID": 0,
"fBits": 0,
"fName": "yaxis",
"fTitle": "",
"fNdivisions": 510,
"fAxisColor": 1,
"fLabelColor": 1,
"fLabelFont": 42,
"fLabelOffset": 0.005,
"fLabelSize": 0.035,
"fTickLength": 0.03,
"fTitleOffset": 1.0,
"fTitleSize": 0.035,
"fTitleColor": 1,
"fTitleFont": 42,
"fNbins": 1,
"fXmin": 0.0,
"fXmax": 1.0,
"fXbins": [],
"fFirst": 0,
"fLast": 0,
"fBits2": 0,
"fTimeDisplay": false,
"fTimeFormat": "",
"fLabels": null,
"fModLabs": null
},
"fZaxis": {
"_typename": "TAxis",
"fUniqueID": 0,
"fBits": 0,
"fName": "zaxis",
"fTitle": "",
"fNdivisions": 510,
"fAxisColor": 1,
"fLabelColor": 1,
"fLabelFont": 42,
"fLabelOffset": 0.005,
"fLabelSize": 0.035,
"fTickLength": 0.03,
"fTitleOffset": 1.0,
"fTitleSize": 0.035,
"fTitleColor": 1,
"fTitleFont": 42,
"fNbins": 1,
"fXmin": 0.0,
"fXmax": 1.0,
"fXbins": [],
"fFirst": 0,
"fLast": 0,
"fBits2": 0,
"fTimeDisplay": false,
"fTimeFormat": "",
"fLabels": null,
"fModLabs": null
},
"fBarOffset": 0,
"fBarWidth": 1000,
"fEntries": 4776546.0,
"fTsumw": 4776546.0,
"fTsumw2": 4776546.0,
"fTsumwx": 214617100000.0,
"fTsumwx2": 2.245448605e+16,
"fMaximum": -1111.0,
"fMinimum": -1111.0,
"fNormFactor": 0.0,
"fContour": [],
"fSumw2": [
0.0,
1068515.0,
911171.0,
630749.0,
449908.0,
336446.0,
265471.0,
219142.0,
186119.0,
0.0,
0.0,
122888.0,
107285.0,
92548.0,
78798.0,
65261.0,
53400.0,
44413.0,
36398.0,
0.0,
0.0,
19816.0,
16119.0,
12874.0,
10512.0,
8506.0,
6869.0,
5591.0,
4593.0,
3772.0,
3049.0,
2522.0,
2166.0,
1754.0,
1520.0,
1286.0,
1033.0,
932.0,
822.0,
720.0,
610.0,
474.0,
443.0,
391.0,
347.0,
310.0,
262.0,
210.0,
186.0,
171.0,
174.0,
0.0
],
"fOption": "",
"fFunctions": {
"_typename": "TList",
"name": "TList",
"arr": [],
"opt": []
},
"fBufferSize": 0,
"fBuffer": [],
"fBinStatErrOpt": 0,
"fStatOverflows": 2
}
],
"num_entries": 4776546,
"mean": 44931.44209225662,
......
Markdown is supported
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