diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp index 2736ac984c15b53f291dfac472a5e3307f823a60..f038658b5498ebd258ef8ffbed8070c79e7f3833 100644 --- a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp +++ b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp @@ -37,7 +37,26 @@ Edm4hepWriterAnaElemTool::BeginOfEventAction(const G4Event* anEvent) { void Edm4hepWriterAnaElemTool::EndOfEventAction(const G4Event* anEvent) { - auto mcCol = m_mcParCol.get(); + auto mcGenCol = m_mcParCol.get(); + mcCol = m_mcParCol.createAndPut(); + + // copy the MC particle first + for (auto mcGenParticle: *mcGenCol) { + auto newparticle = mcCol->create(); + newparticle.setPDG (mcGenParticle.getPDG()); + newparticle.setGeneratorStatus(mcGenParticle.getGeneratorStatus()); + newparticle.setSimulatorStatus(mcGenParticle.getSimulatorStatus()); + newparticle.setCharge (mcGenParticle.getCharge()); + newparticle.setTime (mcGenParticle.getTime()); + newparticle.setMass (mcGenParticle.getMass()); + newparticle.setVertex (mcGenParticle.getVertex()); + newparticle.setEndpoint (mcGenParticle.getEndpoint()); + newparticle.setMomentum (mcGenParticle.getMomentum()); + newparticle.setMomentumAtEndpoint(mcGenParticle.getMomentumAtEndpoint()); + newparticle.setSpin (mcGenParticle.getSpin()); + newparticle.setColorFlow (mcGenParticle.getColorFlow()); + } + msg() << "mcCol size: " << mcCol->size() << endmsg; // save all data @@ -328,7 +347,7 @@ Edm4hepWriterAnaElemTool::PostUserTrackingAction(const G4Track* track) { if (curparid == 0) { // select the primary tracks (parentID == 0) - auto mcCol = m_mcParCol.get(); + // auto mcCol = m_mcParCol.get(); if (curtrkid-1>=mcCol->size()) { error() << "out of range: curtrkid is " << curtrkid @@ -400,7 +419,7 @@ Edm4hepWriterAnaElemTool::PostUserTrackingAction(const G4Track* track) { // create secondaries in MC particles // todo: convert the const collection to non-const - auto mcCol = const_cast<edm4hep::MCParticleCollection*>(m_mcParCol.get()); + // auto mcCol = const_cast<edm4hep::MCParticleCollection*>(m_mcParCol.get()); edm4hep::MCParticle mcp = mcCol->create(); mcp.setPDG(secparticle->GetPDGEncoding()); mcp.setGeneratorStatus(0); // not created by Generator diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h index 26a1ba48a1ab00a05fa65a4fa011320d1770d988..7b992d3076c0ddfa7b7bd9f5d8639274ba0538b1 100644 --- a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h +++ b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h @@ -41,8 +41,13 @@ public: private: // In order to associate MCParticle with contribution, we need to access MC Particle. - DataHandle<edm4hep::MCParticleCollection> m_mcParCol{"MCParticle", + // - collection MCParticle: the particles in Generator + DataHandle<edm4hep::MCParticleCollection> m_mcParGenCol{"MCParticle", Gaudi::DataHandle::Writer, this}; + // - collection MCParticleG4: the simulated particles in Geant4 + DataHandle<edm4hep::MCParticleCollection> m_mcParCol{"MCParticleG4", + Gaudi::DataHandle::Writer, this}; + edm4hep::MCParticleCollection* mcCol; // Generic collections for Tracker and Calorimeter DataHandle<edm4hep::SimTrackerHitCollection> m_trackerCol{"SimTrackerCol", diff --git a/Simulation/DetSimCore/CMakeLists.txt b/Simulation/DetSimCore/CMakeLists.txt index 9c225fc7332999dc1bd7ca9088a33d8b6b0e46f9..439c50c615543484f6286685b19e78a504a6a9ff 100644 --- a/Simulation/DetSimCore/CMakeLists.txt +++ b/Simulation/DetSimCore/CMakeLists.txt @@ -21,6 +21,7 @@ gaudi_add_module(DetSimCore ${Geant4_LIBRARIES} ${DD4hep_COMPONENT_LIBRARIES} EDM4HEP::edm4hep EDM4HEP::edm4hepDict + k4FWCore::k4FWCore ) install(TARGETS DetSimCore