Skip to content
Snippets Groups Projects
G4PrimaryCnvTool.cpp 2.39 KiB
Newer Older
#include "G4PrimaryCnvTool.h"

#include "G4Event.hh"
lintao@ihep.ac.cn's avatar
lintao@ihep.ac.cn committed
#include "G4ParticleTable.hh"
#include "G4IonTable.hh"
#include "G4ParticleDefinition.hh"

DECLARE_COMPONENT(G4PrimaryCnvTool)

bool G4PrimaryCnvTool::mutate(G4Event* anEvent) {

    auto mcCol = m_mcParCol.get();
    info() << "Start a new event: " << endmsg;
    for ( auto p : *mcCol ) {
        info() << p.getObjectID().index << " : [";
        for ( auto it = p.daughters_begin(), end = p.daughters_end(); it != end; ++it ) {
            info() << " " << it->getObjectID().index;
        }
        info() << " ]; " << endmsg;
lintao@ihep.ac.cn's avatar
lintao@ihep.ac.cn committed

        // only the GeneratorStatus == 1 is used.
        if (p.getGeneratorStatus() != 1) {
            continue;
        }

        // vertex
        const edm4hep::Vector3d& vertex = p.getVertex();
        double t = p.getTime()*CLHEP::ns;
        G4PrimaryVertex* g4vtx = new G4PrimaryVertex(vertex.x*CLHEP::mm,
                                                     vertex.y*CLHEP::mm,
                                                     vertex.z*CLHEP::mm,
                                                     t);

        info() << "Geant4 Primary Vertex: ("
               << vertex.x*CLHEP::mm << ","
               << vertex.y*CLHEP::mm << ","
               << vertex.z*CLHEP::mm << ")"
               << endmsg;

lintao@ihep.ac.cn's avatar
lintao@ihep.ac.cn committed
        // pdg/particle
        int pdgcode = p.getPDG();
        G4ParticleTable* particletbl = G4ParticleTable::GetParticleTable();
        G4ParticleDefinition* particle_def = nullptr;
lintao@ihep.ac.cn's avatar
lintao@ihep.ac.cn committed

        // handle the several exceptions
        if (pdgcode == 0 && p.getCharge() == 0) {
            // this is geantino
            particle_def = particletbl->FindParticle("geantino");
        } else if (pdgcode == 0 && p.getCharge() == 1) {
            // this is chargedgeantino
            particle_def = particletbl->FindParticle("chargedgeantino");
        } else {
            particle_def = particletbl->FindParticle(pdgcode);
        }
lintao@ihep.ac.cn's avatar
lintao@ihep.ac.cn committed
        // momentum
        const edm4hep::Vector3f& momentum = p.getMomentum();
lintao@ihep.ac.cn's avatar
lintao@ihep.ac.cn committed
        G4PrimaryParticle* g4prim = new G4PrimaryParticle(particle_def,
                                                          momentum.x*CLHEP::GeV,
                                                          momentum.y*CLHEP::GeV,
                                                          momentum.z*CLHEP::GeV);
lintao@ihep.ac.cn's avatar
lintao@ihep.ac.cn committed

        g4vtx->SetPrimary(g4prim);

        anEvent->AddPrimaryVertex(g4vtx);