From 5932e329efdb288de97d2d6c9a6291277b8dce59 Mon Sep 17 00:00:00 2001
From: lintao <lintao@ihep.ac.cn>
Date: Fri, 1 Nov 2024 14:14:13 +0800
Subject: [PATCH] Update the endpoint information in EDM4hep for non-primary
 track

---
 .../src/Edm4hepWriterAnaElemTool.cpp          | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp
index e3728357..fd447fd4 100644
--- a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp
+++ b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp
@@ -415,6 +415,7 @@ Edm4hepWriterAnaElemTool::PostUserTrackingAction(const G4Track* track) {
     int curparid = track->GetParentID();
     int idxedm4hep = -1;
 
+    // note: only the primary track can get the idxedm4hep from Event level User Info.
     if (m_userinfo) {
         idxedm4hep =  m_userinfo->idxG4Track2Edm4hep(curtrkid);
     }
@@ -544,6 +545,28 @@ Edm4hepWriterAnaElemTool::PostUserTrackingAction(const G4Track* track) {
 
     } else {
         // TODO: select other interested tracks
+
+        // extract the track level user info
+        auto trackinfo = dynamic_cast<CommonUserTrackInfo*>(track->GetUserInformation());
+        if (trackinfo) {
+            idxedm4hep = trackinfo->idxEdm4hep();
+
+            auto mc_particle = mcCol->at(idxedm4hep);
+
+            const G4ThreeVector& stop_pos  = track->GetPosition();
+            edm4hep::Vector3d endpoint(stop_pos.x()/CLHEP::mm,
+                                       stop_pos.y()/CLHEP::mm,
+                                       stop_pos.z()/CLHEP::mm);
+            mc_particle.setEndpoint(endpoint);
+
+            const G4ThreeVector& stop_mom = track->GetMomentum();
+
+            edm4hep::Vector3f mom_endpoint(stop_mom.x()/CLHEP::GeV,
+                                           stop_mom.y()/CLHEP::GeV,
+                                           stop_mom.z()/CLHEP::GeV);
+            mc_particle.setMomentumAtEndpoint(mom_endpoint);
+
+        }
     }
 
 }
-- 
GitLab