Skip to content

Commit

Permalink
Merge pull request #1 from lobis/variadic
Browse files Browse the repository at this point in the history
use variadic templates to insert data
  • Loading branch information
lobis authored Nov 30, 2023
2 parents 46f82c4 + 4babc29 commit 2defa4f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 70 deletions.
4 changes: 4 additions & 0 deletions application/include/geant4/DataModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ Builder MakeBuilder();

void InsertEventBegin(const G4Event* event, Builder& builder);
void InsertEventEnd(const G4Event* event, Builder& builder);

void InsertTrackBegin(const G4Track* track, Builder& builder);
void InsertTrackEnd(const G4Track* track, Builder& builder);

void InsertEvent(const G4Event* event, Builder& builder);
void InsertTrack(const G4Track* track, Builder& builder);
void InsertStep(const G4Step* step, Builder& builder);

py::object SnapshotBuilder(Builder& builder);
Expand Down
111 changes: 42 additions & 69 deletions application/src/geant4/DataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,38 @@ using namespace std;

namespace geant4::data {

void InsertEventBegin(const G4Event* event, Builder& builder) {
void InsertEvent(const G4Event* event, Builder& builder) {
builder.content<Field::runId>().append(G4RunManager::GetRunManager()->GetCurrentRun()->GetRunID());
builder.content<Field::eventId>().append(event->GetEventID());
//
builder.content<Field::trackId>().begin_list();
builder.content<Field::trackParentId>().begin_list();
builder.content<Field::trackParticle>().begin_list();
builder.content<Field::trackInitialEnergy>().begin_list();
builder.content<Field::trackInitialTime>().begin_list();
builder.content<Field::trackInitialPositionX>().begin_list();
builder.content<Field::trackInitialPositionY>().begin_list();
builder.content<Field::trackInitialPositionZ>().begin_list();
builder.content<Field::trackInitialMomentumX>().begin_list();
builder.content<Field::trackInitialMomentumY>().begin_list();
builder.content<Field::trackInitialMomentumZ>().begin_list();
builder.content<Field::trackWeight>().begin_list();
//
builder.content<Field::stepEnergy>().begin_list();
builder.content<Field::stepTime>().begin_list();
builder.content<Field::stepPositionX>().begin_list();
builder.content<Field::stepPositionY>().begin_list();
builder.content<Field::stepPositionZ>().begin_list();
builder.content<Field::stepTrackKineticEnergy>().begin_list();

// builder.content<Field::stepProcess>().begin_list();
}

template<std::size_t... I>
void InsertEventBeginHelper(Builder& builder) {
(builder.content<I>().begin_list(), ...);
}

void InsertEventBegin(const G4Event* event, Builder& builder) {
InsertEventBeginHelper<Field::trackId, Field::trackParentId, Field::trackInitialEnergy, Field::trackInitialTime,
Field::trackParticle, Field::trackInitialPositionX, Field::trackInitialPositionY, Field::trackInitialPositionZ,
Field::trackInitialMomentumX, Field::trackInitialMomentumY, Field::trackInitialMomentumZ, Field::trackWeight,
Field::stepEnergy, Field::stepTime, Field::stepPositionX, Field::stepPositionY, Field::stepPositionZ,
Field::stepTrackKineticEnergy>(builder);
}

template<std::size_t... I>
void InsertEventEndHelper(Builder& builder) {
(builder.content<I>().end_list(), ...);
}

void InsertEventEnd(const G4Event*, Builder& builder) {
builder.content<Field::trackId>().end_list();
builder.content<Field::trackParentId>().end_list();
builder.content<Field::trackInitialEnergy>().end_list();
builder.content<Field::trackInitialTime>().end_list();
builder.content<Field::trackParticle>().end_list();
// builder.content<Field::trackCreatorProcess>().end_list();
// builder.content<Field::trackCreatorProcessType>().end_list();
builder.content<Field::trackInitialPositionX>().end_list();
builder.content<Field::trackInitialPositionY>().end_list();
builder.content<Field::trackInitialPositionZ>().end_list();
builder.content<Field::trackInitialMomentumX>().end_list();
builder.content<Field::trackInitialMomentumY>().end_list();
builder.content<Field::trackInitialMomentumZ>().end_list();
builder.content<Field::trackWeight>().end_list();
//
builder.content<Field::stepEnergy>().end_list();
builder.content<Field::stepTime>().end_list();
// builder.content<Field::stepProcess>().end_list();
// builder.content<Field::stepProcessType>().end_list();
// builder.content<Field::stepVolume>().end_list();
builder.content<Field::stepPositionX>().end_list();
builder.content<Field::stepPositionY>().end_list();
builder.content<Field::stepPositionZ>().end_list();
builder.content<Field::stepTrackKineticEnergy>().end_list();
InsertEventEndHelper<Field::trackId, Field::trackParentId, Field::trackInitialEnergy, Field::trackInitialTime,
Field::trackParticle, Field::trackInitialPositionX, Field::trackInitialPositionY, Field::trackInitialPositionZ,
Field::trackInitialMomentumX, Field::trackInitialMomentumY, Field::trackInitialMomentumZ, Field::trackWeight,
Field::stepEnergy, Field::stepTime, Field::stepPositionX, Field::stepPositionY, Field::stepPositionZ,
Field::stepTrackKineticEnergy>(builder);
}

void InsertTrackBegin(const G4Track* track, Builder& builder) {
void InsertTrack(const G4Track* track, Builder& builder) {
builder.content<Field::trackId>().content().append(track->GetTrackID());
builder.content<Field::trackParentId>().content().append(track->GetParentID());
builder.content<Field::trackParticle>().content().append({});
Expand All @@ -90,30 +67,26 @@ void InsertTrackBegin(const G4Track* track, Builder& builder) {
builder.content<Field::trackInitialMomentumY>().content().append(track->GetMomentum().y() / units::momentum);
builder.content<Field::trackInitialMomentumZ>().content().append(track->GetMomentum().z() / units::momentum);
builder.content<Field::trackWeight>().content().append(track->GetWeight());
//
builder.content<Field::stepEnergy>().content().begin_list();
builder.content<Field::stepTime>().content().begin_list();
// builder.content<Field::stepProcess>().content().begin_list();
// builder.content<Field::stepProcessType>().content().begin_list();
// builder.content<Field::stepVolume>().content().begin_list();
builder.content<Field::stepPositionX>().content().begin_list();
builder.content<Field::stepPositionY>().content().begin_list();
builder.content<Field::stepPositionZ>().content().begin_list();
builder.content<Field::stepTrackKineticEnergy>().content().begin_list();

// we should probably insert a step here to mark the beginning of the track / use stepping verbose
}

template<std::size_t... I>
void InsertTrackBeginHelper(Builder& builder) {
(builder.content<I>().content().begin_list(), ...);
}

void InsertTrackBegin(const G4Track* track, Builder& builder) {
InsertTrackBeginHelper<Field::stepEnergy, Field::stepTime, Field::stepPositionX, Field::stepPositionY, Field::stepPositionZ,
Field::stepTrackKineticEnergy>(builder);
}

template<std::size_t... I>
void InsertTrackEndHelper(Builder& builder) {
(builder.content<I>().content().end_list(), ...);
}

void InsertTrackEnd(const G4Track*, Builder& builder) {
builder.content<Field::stepEnergy>().content().end_list();
builder.content<Field::stepTime>().content().end_list();
// builder.content<Field::stepProcess>().content().end_list();
// builder.content<Field::stepProcessType>().content().end_list();
// builder.content<Field::stepVolume>().content().end_list();
builder.content<Field::stepPositionX>().content().end_list();
builder.content<Field::stepPositionY>().content().end_list();
builder.content<Field::stepPositionZ>().content().end_list();
builder.content<Field::stepTrackKineticEnergy>().content().end_list();
InsertTrackEndHelper<Field::stepEnergy, Field::stepTime, Field::stepPositionX, Field::stepPositionY, Field::stepPositionZ,
Field::stepTrackKineticEnergy>(builder);
}

void InsertStep(const G4Step* step, Builder& builder) {
Expand Down
1 change: 1 addition & 0 deletions application/src/geant4/EventAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ EventAction::EventAction() : G4UserEventAction() {}

void EventAction::BeginOfEventAction(const G4Event* event) {
geant4::data::InsertEventBegin(event, RunAction::GetBuilder());
geant4::data::InsertEvent(event, RunAction::GetBuilder());
}

void EventAction::EndOfEventAction(const G4Event* event) {
Expand Down
1 change: 1 addition & 0 deletions application/src/geant4/TrackingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TrackingAction::TrackingAction() : G4UserTrackingAction() {}

void TrackingAction::PreUserTrackingAction(const G4Track* track) {
geant4::data::InsertTrackBegin(track, RunAction::GetBuilder());
geant4::data::InsertTrack(track, RunAction::GetBuilder());
}

void TrackingAction::PostUserTrackingAction(const G4Track* track) {
Expand Down
4 changes: 3 additions & 1 deletion tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def test_setup_and_run():
app.setup_physics()
app.setup_action()
app.initialize()
app.run()
events = app.run(100)
assert len(events) == 100


def test_missing_setup():
Expand All @@ -28,6 +29,7 @@ def test_missing_manager():
app.setup_detector()


@pytest.mark.skip(reason="TODO")
def test_multiple_apps():
# Could this work somehow?
with pytest.raises(RuntimeError):
Expand Down

0 comments on commit 2defa4f

Please sign in to comment.