diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp index ea62694d1da2ccee06253d647dcf3599c7398d36..b293022710c329ac570cadbfb5c29499db213434 100644 --- a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp +++ b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp @@ -26,6 +26,10 @@ Edm4hepWriterAnaElemTool::EndOfRunAction(const G4Run*) { void Edm4hepWriterAnaElemTool::BeginOfEventAction(const G4Event* anEvent) { msg() << "Event " << anEvent->GetEventID() << endmsg; + + // reset + m_track2primary.clear(); + } void @@ -192,7 +196,15 @@ Edm4hepWriterAnaElemTool::EndOfEventAction(const G4Event* anEvent) { edm_calo_contrib.setEnergy(c.deposit/CLHEP::GeV); edm_calo_contrib.setTime(c.time/CLHEP::ns); edm_calo_contrib.setStepPosition(edm4hep::Vector3f(pos)); - edm_calo_contrib.setParticle(mcCol->at(0)); // todo + + // from the track id, get the primary track + int pritrkid = m_track2primary[c.trackID]; + if (pritrkid<=0) { + error() << "Failed to find the primary track for trackID #" << c.trackID << endmsg; + pritrkid = 1; + } + + edm_calo_contrib.setParticle(mcCol->at(pritrkid-1)); // todo edm_calo_hit.addToContributions(edm_calo_contrib); } } @@ -214,8 +226,26 @@ Edm4hepWriterAnaElemTool::EndOfEventAction(const G4Event* anEvent) { } void -Edm4hepWriterAnaElemTool::PreUserTrackingAction(const G4Track*) { +Edm4hepWriterAnaElemTool::PreUserTrackingAction(const G4Track* track) { + int curtrkid = track->GetTrackID(); + int curparid = track->GetParentID(); + int pritrkid = curparid; + + // try to find the primary track id from the parent track id. + if (curparid) { + auto it = m_track2primary.find(curparid); + if (it == m_track2primary.end()) { + error() << "Failed to find primary track for track id " << curparid << endmsg; + } else { + pritrkid = it->second; + } + } else { + // curparid is 0, it is primary + pritrkid = curtrkid; + } + + m_track2primary[curtrkid] = pritrkid; } void diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h index a0b48497e4946cc64560cb5f7b01b7803f82cd3a..365078aca348bd88893516d99bd5504b798716a2 100644 --- a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h +++ b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.h @@ -1,6 +1,8 @@ #ifndef Edm4hepWriterAnaElemTool_h #define Edm4hepWriterAnaElemTool_h +#include <map> + #include "GaudiKernel/AlgTool.h" #include "FWCore/DataHandle.h" #include "DetSimInterface/IAnaElemTool.h" @@ -62,6 +64,20 @@ private: DataHandle<edm4hep::SimTrackerHitCollection> m_SETCol{"SETCollection", Gaudi::DataHandle::Writer, this}; +private: + // in order to associate the hit contribution with the primary track, + // we have a bookkeeping of every track. + // The primary track will assign the same key/value. + + // Following is an example: + // 1 -> 1, + // 2 -> 2, + // 3 -> 1, + // Now, if parent of trk #4 is trk #3, using the mapping {3->1} could + // locate the primary trk #1. + + std::map<int, int> m_track2primary; + }; #endif