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
4 changes: 2 additions & 2 deletions gemc/actions/event/gEventAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void GEventAction::EndOfEventAction([[maybe_unused]] const G4Event* event) {

// Loop over every hit collection produced during this event and dispatch each
// collection to the digitization routine registered under its collection name.
for (G4int hci = 0; hci < hcs_this_event->GetNumberOfCollections(); ++hci) {
for (G4int hci = 0; hci < hcs_this_event->GetCapacity(); ++hci) {
auto* const this_ghc = static_cast<GHitsCollection*>(hcs_this_event->GetHC(hci));
if (this_ghc == nullptr) {
continue;
Expand Down Expand Up @@ -394,7 +394,7 @@ void GEventAction::stream_event(const G4Event* event) {
if (auto* collections = event->GetHCofThisEvent()) {
const auto routines = run_action->get_digitization_routines_map();
if (!routines) { throw std::runtime_error("SRO requires a digitization routine map"); }
for (int index = 0; index < collections->GetNumberOfCollections(); ++index) {
for (int index = 0; index < collections->GetCapacity(); ++index) {
auto* hits = static_cast<GHitsCollection*>(collections->GetHC(index));
if (!hits || detector_is_listed(goptions, NO_DIGITIZED_OPTION, hits->GetSDname())) { continue; }
const auto& routine = routines->at(hits->GetSDname());
Expand Down
10 changes: 10 additions & 0 deletions gemc/actions/examples/sro_runtime_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ void require(bool condition, const char* message) {
if (!condition) { throw std::runtime_error(message); }
}

// Reserve an inactive slot before the live collection, including empty-event runs.
class Inactive final : public G4VSensitiveDetector {
public:
Inactive() : G4VSensitiveDetector("unused") { collectionName.insert("unused"); }
G4bool ProcessHits(G4Step*, G4TouchableHistory*) override { return false; }
};

class Sensitive final : public G4VSensitiveDetector {
public:
Sensitive() : G4VSensitiveDetector("test_sro") { collectionName.insert("hits"); }
Expand Down Expand Up @@ -55,6 +62,9 @@ class Detector final : public G4VUserDetectorConstruction {
return new G4PVPlacement(nullptr, {}, logical, "world", nullptr, false, 0);
}
void ConstructSDandField() override {
auto* inactive = new Inactive;
G4SDManager::GetSDMpointer()->AddNewDetector(inactive);
inactive->Activate(false);
if (!empty) { G4SDManager::GetSDMpointer()->AddNewDetector(new Sensitive); }
}
private:
Expand Down
5 changes: 3 additions & 2 deletions gemc/dbselect/examples/test_reload_sequence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class EventAction : public G4UserEventAction {
// G4HCofThisEvent::GetHC / GetNumberOfCollections are not marked const in G4 11.4.
auto* hc = const_cast<G4HCofThisEvent*>(event->GetHCofThisEvent());
if (!hc) return;
for (G4int i = 0; i < hc->GetNumberOfCollections(); ++i) {
for (G4int i = 0; i < hc->GetCapacity(); ++i) {
auto* col = static_cast<GHitsCollection*>(hc->GetHC(i));
if (!col || col->GetSize() == 0) continue;
const std::string sdName = col->GetSDname();
Expand Down Expand Up @@ -165,7 +165,7 @@ int main(int argc, char* argv[]) {
if (stale_dosi_in_flux)
std::cout << "WARNING: unexpected dosimeter hits in flux-only phases (stale SD).\n";

bool passed = (flux1 > 0) && (flux3 > 0) && !stale_flux_in_b1 && !stale_dosi_in_flux;
bool passed = (flux1 > 0) && (dosi2 > 0) && (flux3 > 0) && !stale_flux_in_b1 && !stale_dosi_in_flux;

delete runManager;

Expand All @@ -175,6 +175,7 @@ int main(int argc, char* argv[]) {
}
std::cout << "FAILED"
<< (flux1 == 0 ? " (no flux hits in Phase 1)" : "")
<< (dosi2 == 0 ? " (no dosimeter hits in Phase 2)" : "")
<< (flux3 == 0 ? " (no flux hits in Phase 3)" : "")
<< (stale_flux_in_b1 ? " (stale flux SD in Phase 2)" : "")
<< (stale_dosi_in_flux ? " (stale dosimeter SD in flux phases)" : "")
Expand Down
11 changes: 11 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,14 @@ material_optics = executable('test_material_optics', 'material_optics.cc',
dependencies: gui_probe_dependencies,
link_with: all_libs)
test('material_optics', material_optics, suite: 'gsystem')

sparse_hit_collections = executable('test_sparse_hit_collections',
files('sparse_hit_collections.cc', '../gemc_options.cc') + qrc_gemc_sources,
include_directories: gemc_includes,
dependencies: gui_probe_dependencies,
link_with: all_libs,
export_dynamic: true)
test('sparse_hit_collections', python_exe,
args: [files('sparse_hit_collections.py'), sparse_hit_collections, get_option('prefix'), '--plugin-path=' + build_root],
depends: plugin_target_maps.values(),
env: project_test_env, suite: 'gactions', timeout: 180)
44 changes: 44 additions & 0 deletions tests/sparse_hit_collections.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "gemc_options.h"
#include <gemc/actions/gaction.h>
#include <gemc/gdetector/gdetectorConstruction.h>
#include <gemc/gphysics/gphysics.h>
#include "G4RunManagerFactory.hh"
#include "G4SDManager.hh"
#include "G4VSensitiveDetector.hh"

#include <memory>

namespace {
class InactiveDetector : public G4VSensitiveDetector {
public:
InactiveDetector() : G4VSensitiveDetector("unused") { collectionName.insert("unused"); }
G4bool ProcessHits(G4Step*, G4TouchableHistory*) override { return false; }
};

class SparseDetector : public GDetectorConstruction {
public:
using GDetectorConstruction::GDetectorConstruction;
void ConstructSDandField() override {
// Reserve collection ID 0 in every execution context, then leave its slot empty.
auto* inactive = new InactiveDetector;
G4SDManager::GetSDMpointer()->AddNewDetector(inactive);
inactive->Activate(false);
GDetectorConstruction::ConstructSDandField();
}
};
}

int main(int argc, char* argv[]) {
auto options = std::make_shared<GOptions>(argc, argv, gemc::defineOptions());
auto manager = std::unique_ptr<G4RunManager>(
G4RunManagerFactory::CreateRunManager(G4RunManagerType::MTOnly));
manager->SetNumberOfThreads(1);
auto* detector = new SparseDetector(options);
manager->SetUserInitialization(detector);
auto* physics = new GPhysics(options);
manager->SetUserInitialization(physics->getPhysList());
manager->SetUserInitialization(new GAction(options, detector->get_digitization_routines_map()));
manager->Initialize();
manager->BeamOn(1);
return 0;
}
24 changes: 24 additions & 0 deletions tests/sparse_hit_collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""A real production event action must publish the active collection after an inactive slot."""
import json
from pathlib import Path
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]
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,'-gsystem=[{name: simple_flux, factory: sqlite}]','-experiment=examples','-runno=1','-sql='+str(database),'-nthreads=1','-recordZeroEdep','-gparticle=[{name: geantino, p: 1*GeV, vz: -3*cm}]','-gstreamer=[{format: json, filename: sparse}]'],cwd=work,capture_output=True,text=True)
assert run.returncode==0,run.stdout+run.stderr
events=json.loads((work/'sparse_t0.json').read_text())['events']
assert len(events)==1,events
assert events[0].get('detectors',{}).get('digitized_by_detector',{}).get('flux'),events
print('Production event dispatch preserves hits at collection ID 1 with inactive ID 0.')
Loading