Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gemc/gstreamer/factories/ROOT/event/event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Emit lightweight lifecycle logs around event publication for the ROOT backend.

bool GstreamerRootFactory::startEventImpl([[maybe_unused]] const std::shared_ptr<GEventDataCollection>& event_data) {
current_event_number = event_data->getHeader()->getG4LocalEvn();
current_thread_id = event_data->getHeader()->getThreadID();
log->info(2, "Start of event %1 in %2", event_data->getHeader()->getG4LocalEvn(), filename());

return true;
Expand Down
2 changes: 1 addition & 1 deletion gemc/gstreamer/factories/ROOT/event/publishDigitized.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool GstreamerRootFactory::publishEventDigitizedDataImpl(const std::string&
if (!digitizedData.empty()) {
const auto& digitizedDataTree = getOrInstantiateDigitizedDataTree(detectorName, digitizedData.front());

return digitizedDataTree->fillTree(digitizedData);
return digitizedDataTree->fillTree(digitizedData, current_event_number, current_thread_id);
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion gemc/gstreamer/factories/ROOT/event/publishTrueInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool GstreamerRootFactory::publishEventTrueInfoDataImpl(const std::string&
if (!trueInfoData.empty()) {
const auto& trueInforDataTree = getOrInstantiateTrueInfoDataTree(detectorName, trueInfoData.front());

return trueInforDataTree->fillTree(trueInfoData);
return trueInforDataTree->fillTree(trueInfoData, current_event_number, current_thread_id);
}

return false;
Expand Down
22 changes: 20 additions & 2 deletions gemc/gstreamer/factories/ROOT/gRootTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ GRootTree::GRootTree(const std::string& detectorName,


// fill the True Info Tree
bool GRootTree::fillTree(const std::vector<const GTrueInfoData*>& trueInfoData) {
bool GRootTree::fillTree(const std::vector<const GTrueInfoData*>& trueInfoData, int event_number, int thread_id) {
// Reset all branch vectors before repopulating them for this detector collection.
for (auto& [varname, values] : intVarsMap) { values.clear(); }
for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
Expand All @@ -174,13 +174,14 @@ bool GRootTree::fillTree(const std::vector<const GTrueInfoData*>& trueInfoData)
for (auto& [varname, value] : dataHits->getStringVariablesMap()) { stringVarsMap[varname].push_back(value); }
}

fillEventIdentity(event_number, thread_id);
root_tree->Fill();

return true;
}

// fill the Digitized Data Tree
bool GRootTree::fillTree(const std::vector<const GDigitizedData*>& digitizedData) {
bool GRootTree::fillTree(const std::vector<const GDigitizedData*>& digitizedData, int event_number, int thread_id) {
// Reset all branch vectors before repopulating them for this detector collection.
for (auto& [varname, values] : intVarsMap) { values.clear(); }
for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
Expand All @@ -198,6 +199,7 @@ bool GRootTree::fillTree(const std::vector<const GDigitizedData*>& digitizedData
for (auto& [varname, value] : dataHits->getIntObservablesMap(0)) { intVarsMap[varname].push_back(value); }
for (auto& [varname, value] : dataHits->getDblObservablesMap(0)) { doubleVarsMap[varname].push_back(value); }
}
fillEventIdentity(event_number, thread_id);
root_tree->Fill();

return true;
Expand Down Expand Up @@ -292,3 +294,19 @@ void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] co
root_tree->GetName());
}
}

// Event trees remain sparse; these keys identify the matching header/generated entry.
void GRootTree::fillEventIdentity(int event_number, int thread_id) {
if (event_number < 0) { return; } // Run-integrated trees have no event identity.
if (!event_identity_registered) {
for (const auto* key : {"g4localEventNumber", "threadID"}) {
if (intVarsMap.contains(key) || doubleVarsMap.contains(key) || stringVarsMap.contains(key)) {
log->error(gstreamer::ERR_PUBLISH_ERROR, "Detector variable %1 conflicts with reserved ROOT event metadata.", key);
}
registerVariable(key, 0);
}
event_identity_registered = true;
}
intVarsMap["g4localEventNumber"] = {event_number};
intVarsMap["threadID"] = {thread_id};
}
6 changes: 4 additions & 2 deletions gemc/gstreamer/factories/ROOT/gRootTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class GRootTree
* \param trueInfoData Raw-pointer view of the detector true-information hits.
* \return \c true on success.
*/
bool fillTree(const std::vector<const GTrueInfoData*>& trueInfoData);
bool fillTree(const std::vector<const GTrueInfoData*>& trueInfoData, int event_number = -1, int thread_id = -1);

/**
* \brief Fill a digitized detector tree with one detector collection.
Expand All @@ -159,7 +159,7 @@ class GRootTree
* \param digitizedData Raw-pointer view of the detector digitized hits.
* \return \c true on success.
*/
bool fillTree(const std::vector<const GDigitizedData*>& digitizedData);
bool fillTree(const std::vector<const GDigitizedData*>& digitizedData, int event_number = -1, int thread_id = -1);

/**
* \brief Fill a generated-particle tree with one event bank.
Expand All @@ -175,6 +175,8 @@ class GRootTree
bool fillTree(const GAncestorBank& ancestors);

private:
bool event_identity_registered = false;
void fillEventIdentity(int event_number, int thread_id);
/// \brief Owned ROOT tree instance receiving all branch data.
std::unique_ptr<TTree> root_tree;

Expand Down
2 changes: 2 additions & 0 deletions gemc/gstreamer/factories/ROOT/gstreamerROOTFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class GstreamerRootFactory : public GStreamer

/// \brief ROOT file owning all trees written by this plugin instance.
std::unique_ptr<TFile> rootfile;
int current_event_number = -1;
int current_thread_id = -1;

/**
* \brief Return the final ROOT filename for this plugin instance.
Expand Down
13 changes: 13 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ material_optics = executable('test_material_optics', 'material_optics.cc',
dependencies: gui_probe_dependencies,
link_with: all_libs)
test('material_optics', material_optics, suite: 'gsystem')

if root_dep.found()
root_sparse_fixture = executable('test_root_sparse_fixture',
files('root_sparse_fixture.cc', '../gemc_options.cc') + qrc_gemc_sources,
include_directories: gemc_includes,
dependencies: gui_probe_dependencies,
link_with: all_libs,
export_dynamic: true)
test('root_event_association', python_exe,
args: [files('root_event_association.py'), gemc, get_option('prefix'), root_sparse_fixture, '--plugin-path=' + build_root],
depends: plugin_target_maps.values(),
env: project_test_env, suite: 'gstreamer', timeout: 180)
endif
92 changes: 92 additions & 0 deletions tests/root_event_association.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Sparse ROOT detector entries join the exact hit-bearing events in parallel JSON output."""
import json
from pathlib import Path
import re
import subprocess
import sys
import os
import tempfile

for argument in sys.argv[2:]:
if argument.startswith("--plugin-path="):
os.environ["GEMC_PLUGIN_PATH"] = argument.split("=", 1)[1]

binary=str(Path(sys.argv[1]).resolve());source=Path(__file__).resolve().parents[1]
assert len(sys.argv)>3 and not sys.argv[3].startswith("--"), "ROOT sparse fixture executable is required"
with tempfile.TemporaryDirectory() as directory:
work=Path(directory)
database=work/'geometry.db'
setup=subprocess.run([sys.executable,str(source/'examples/basic/simple_flux/simple_flux.py'),'-f','sqlite','-sql',str(database)],cwd=work,capture_output=True,text=True)
assert setup.returncode==0,setup.stdout+setup.stderr
run=subprocess.run([binary,str(source/'examples/basic/simple_flux/simple_flux.yaml'),'-sql='+str(database),'-n=30','-nthreads=1','-seed=12345','-gparticle=[{name: e-, p: 1*GeV, theta: 0*deg, delta_theta: 89*deg}]','-gstreamer=[{format: root, filename: flux}, {format: json, filename: reference}]'],cwd=work,capture_output=True,text=True)
assert run.returncode==0,run.stdout+run.stderr
events=json.loads((work/'reference_t0.json').read_text())['events']
expected=[e['header']['g4local_event'] for e in events if e.get('detectors',{}).get('digitized_by_detector',{}).get('flux')]
assert 0<len(expected)<30,expected
(work/'inspect.C').write_text(r'''
#include "TFile.h"
#include "TTree.h"
#include "TSystem.h"
#include <vector>
#include <iostream>
void inspect() {
TFile file("flux_t0.root");
for (auto name : {"digitized_flux", "true_info_flux"}) {
auto* tree=dynamic_cast<TTree*>(file.Get(name));
if (!tree || !tree->GetBranch("g4localEventNumber") || !tree->GetBranch("threadID")) {
std::cerr<<"Missing detector event keys in "<<name<<std::endl; gSystem->Exit(2); return;
}
std::vector<int>* events=nullptr; std::vector<int>* threads=nullptr;
tree->SetBranchAddress("g4localEventNumber",&events);
tree->SetBranchAddress("threadID",&threads);
for(Long64_t i=0;i<tree->GetEntries();++i) {
tree->GetEntry(i);
if(events->size()!=1 || threads->size()!=1 || threads->at(0)!=0) {gSystem->Exit(3);return;}
std::cout<<"KEY "<<name<<" "<<events->at(0)<<std::endl;
}
}
}
''')
result=subprocess.run(['root','-l','-b','-q','inspect.C'],cwd=work,capture_output=True,text=True)
assert result.returncode==0,result.stdout+result.stderr
for tree in ['digitized_flux','true_info_flux']:
actual=[int(x) for x in re.findall(r'KEY '+tree+r' (\d+)',result.stdout)]
assert actual==expected,(tree,expected,actual)
if len(sys.argv)>3 and not sys.argv[3].startswith('--'):
fixture=str(Path(sys.argv[3]).resolve())
result=subprocess.run([fixture,'-gstreamer=[{format: root, filename: deterministic}]'],cwd=work,capture_output=True,text=True)
assert result.returncode==0,result.stdout+result.stderr
(work/'deterministic.C').write_text(r'''
#include "TFile.h"
#include "TTree.h"
#include "TSystem.h"
#include <vector>
void deterministic() {
for(int thread=0;thread<2;++thread) {
TFile file(Form("deterministic_t%d.root",thread));
for(auto name : {"true_info_A","digitized_A","true_info_B","digitized_B"}) {
auto* tree=dynamic_cast<TTree*>(file.Get(name));
if(!tree){gSystem->Exit(11);return;}
std::vector<int>* events=nullptr;std::vector<int>* threads=nullptr;
tree->SetBranchAddress("g4localEventNumber",&events);
tree->SetBranchAddress("threadID",&threads);
const bool truthA=std::string(name)=="true_info_A";
const int expectedCount=truthA?2:1;
if(tree->GetEntries()!=expectedCount){gSystem->Exit(12);return;}
for(int i=0;i<expectedCount;++i) {
tree->GetEntry(i);
const int expected=truthA?(i==0?17:19):(std::string(name)=="digitized_A"?19:18);
if(!events||!threads||events->size()!=1||threads->size()!=1||events->at(0)!=expected||threads->at(0)!=thread){gSystem->Exit(13);return;}
}
}
}
TFile run("deterministic.root");auto* tree=dynamic_cast<TTree*>(run.Get("digitized_dose"));
if(!tree||tree->GetEntries()!=1||tree->GetBranch("g4localEventNumber")||tree->GetBranch("threadID")){gSystem->Exit(14);return;}
gSystem->Exit(0);
}
''')
result=subprocess.run(['root','-l','-b','-q','deterministic.C'],cwd=work,capture_output=True,text=True)
assert result.returncode==0,result.stdout+result.stderr
result=subprocess.run([fixture,'-recordZeroEdep','-gstreamer=[{format: root, filename: collision}]'],cwd=work,capture_output=True,text=True)
assert result.returncode!=0 and 'reserved' in (result.stdout+result.stderr).lower(),result.stdout+result.stderr
print('Sparse ROOT joins, deterministic occupancy, worker IDs, rejected digitization, run schema, and collision checks passed.')
41 changes: 41 additions & 0 deletions tests/root_sparse_fixture.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "gemc_options.h"
#include <gemc/gstreamer/gstreamer.h>
#include <gemc/gtouchable/gtouchable.h>

int main(int argc, char* argv[]) {
auto options = std::make_shared<GOptions>(argc, argv, gemc::defineOptions());
const bool collision = options->getSwitch("recordZeroEdep");
auto touchable = GTouchable::create(options);
GHit hit(touchable);
// Deterministic sparse banks in two worker files: A hit/miss/hit, B late first hit.
for (int thread = 0; thread < 2; ++thread) {
auto streamers = gstreamer::gstreamersMapPtr(options, thread);
for (auto& [name, streamer] : *streamers) streamer->openConnection();
for (int event = 17; event <= 19; ++event) {
auto data = std::make_shared<GEventDataCollection>(
options, std::make_unique<GEventHeader>(options, event, thread));
const std::string detector = event == 18 ? "B" : "A";
auto truth = std::make_unique<GTrueInfoData>(options, &hit);
truth->includeVariable("energy", 1.0);
data->addDetectorTrueInfoData(detector, std::move(truth));
// Reject A's first digitization while retaining its truth.
if (event != 17) {
auto digitized = std::make_unique<GDigitizedData>(options, &hit);
digitized->includeVariable(collision ? "threadID" : "energy", 1.0);
data->addDetectorDigitizedData(detector, std::move(digitized));
}
for (auto& [name, streamer] : *streamers) streamer->publishEventData(data);
}
for (auto& [name, streamer] : *streamers) streamer->closeConnection();
}
auto streamers = gstreamer::gstreamersMapPtr(options);
auto run = std::make_shared<GRunDataCollection>(options, std::make_unique<GRunHeader>(options, 4));
auto digitized = std::make_unique<GDigitizedData>(options, &hit);
digitized->includeVariable("dose", 2.0);
run->collect_event_data_collections("dose", std::move(digitized));
for (auto& [name, streamer] : *streamers) {
streamer->openConnection();
streamer->publishRunData(run);
streamer->closeConnection();
}
}
Loading