Commit 8b8bbcaa authored by Simon Spannagel's avatar Simon Spannagel
Browse files

DepositionGen: move check for world volume position to main action

parent 90f8983b
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

#include <G4Event.hh>
#include <G4ParticleTable.hh>
#include <G4TransportationManager.hh>

#include "core/config/exceptions.h"
#include "core/utils/log.h"
@@ -30,6 +31,15 @@ PrimariesGeneratorAction::PrimariesGeneratorAction(const Configuration&, std::sh
    LOG(DEBUG) << "Setting up Geant4 generator action";
}

bool PrimariesGeneratorAction::check_vertex_inside_world(const G4ThreeVector& pos) const {
    auto* solid = G4TransportationManager::GetTransportationManager()
                      ->GetNavigatorForTracking()
                      ->GetWorldVolume()
                      ->GetLogicalVolume()
                      ->GetSolid();
    return solid->Inside(pos) == kInside;
}

/**
 * Called automatically for every event
 */
@@ -42,6 +52,12 @@ void PrimariesGeneratorAction::GeneratePrimaries(G4Event* event) {
    // Dispatch them to the Geant4 particle gun
    LOG(DEBUG) << "Primary particles generated:";
    for(const auto& particle : particles) {
        // Check world boundary of primary vertex
        if(!check_vertex_inside_world(particle.position())) {
            LOG(WARNING) << "Vertex at " << particle.position() << " outside world volume, skipping.";
            continue;
        }

        auto* pdg_table = G4ParticleTable::GetParticleTable();
        particle_gun_->SetParticleDefinition(pdg_table->FindParticle(particle.pdg()));
        particle_gun_->SetParticleEnergy(particle.energy());
+7 −0
Original line number Diff line number Diff line
@@ -39,6 +39,13 @@ namespace allpix {
        void GeneratePrimaries(G4Event*) override;

    private:
        /**
         * @brief helper method to check if particle is to be dispateched within the defined world volume
         * @param  pos Initial position of the primary particle
         * @return     True if within the world volume, false otherwise
         */
        bool check_vertex_inside_world(const G4ThreeVector& pos) const;

        std::unique_ptr<G4ParticleGun> particle_gun_;
        std::shared_ptr<PrimariesReader> reader_;
    };
+2 −18
Original line number Diff line number Diff line
@@ -14,8 +14,6 @@
#include "core/module/exceptions.h"
#include "core/utils/log.h"

#include <G4TransportationManager.hh>

#include <HepMC3/Print.h>
#include <HepMC3/ReaderAscii.h>
#include <HepMC3/ReaderAsciiHepMC2.h>
@@ -48,15 +46,6 @@ PrimariesReaderHepMC::PrimariesReaderHepMC(const Configuration& config) {
    LOG(INFO) << "Successfully opened data file " << file_path;
}

bool PrimariesReaderHepMC::check_vertex_inside_world(const G4ThreeVector& pos) const {
    auto* solid = G4TransportationManager::GetTransportationManager()
                      ->GetNavigatorForTracking()
                      ->GetWorldVolume()
                      ->GetLogicalVolume()
                      ->GetSolid();
    return solid->Inside(pos) == kInside;
}

std::vector<PrimariesReader::Particle> PrimariesReaderHepMC::getParticles() {

    // Read event from input file
@@ -91,13 +80,7 @@ std::vector<PrimariesReader::Particle> PrimariesReaderHepMC::getParticles() {
    std::vector<Particle> particles;
    for(const auto& v : evt.vertices()) {

        // Check world boundary of primary vertex
        auto pos = v->position();
        G4ThreeVector vertex_pos(pos.x(), pos.y(), pos.z());
        if(!check_vertex_inside_world(vertex_pos)) {
            LOG(WARNING) << "Vertex at " << vertex_pos << " outside world volume, skipping.";
            continue;
        }

        // Loop over all outgoing particles of this vertex:
        for(const auto& p : v->particles_out()) {
@@ -111,7 +94,8 @@ std::vector<PrimariesReader::Particle> PrimariesReaderHepMC::getParticles() {

            // Store particle
            auto momentum = G4ThreeVector(p->momentum().px(), p->momentum().py(), p->momentum().pz());
            particles.emplace_back(p->pdg_id(), p->momentum().e(), momentum, vertex_pos, pos.t());
            particles.emplace_back(
                p->pdg_id(), p->momentum().e(), momentum, G4ThreeVector(pos.x(), pos.y(), pos.z()), pos.t());
            LOG(DEBUG) << "Adding particle with ID " << p->pdg_id() << " energy " << p->momentum().e();
        }
    }
+0 −2
Original line number Diff line number Diff line
@@ -37,8 +37,6 @@ namespace allpix {
        std::vector<Particle> getParticles() override;

    private:
        bool check_vertex_inside_world(const G4ThreeVector& pos) const;

        std::shared_ptr<HepMC3::Reader> reader_;
    };
} // namespace allpix