From b3aeeaaf885010ea95cae4499e706ee2a05b3715 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Wed, 16 Dec 2020 23:04:57 +0800
Subject: [PATCH] WIP: store the primary particle's position and momentum at
 endpoint in detector simulation.

---
 .../src/Edm4hepWriterAnaElemTool.cpp          | 33 ++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp
index b5b2ae7f..0a266b80 100644
--- a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp
+++ b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp
@@ -283,7 +283,38 @@ Edm4hepWriterAnaElemTool::PreUserTrackingAction(const G4Track* track) {
 }
 
 void
-Edm4hepWriterAnaElemTool::PostUserTrackingAction(const G4Track*) {
+Edm4hepWriterAnaElemTool::PostUserTrackingAction(const G4Track* track) {
+    int curtrkid = track->GetTrackID(); // starts from 1
+    int curparid = track->GetParentID();
+
+    if (curparid == 0) {
+        // select the primary tracks (parentID == 0)
+        auto mcCol = m_mcParCol.get();
+
+        if (curtrkid-1>=mcCol->size()) {
+            error() << "out of range: curtrkid is " << curtrkid
+                    << " while the MCParticle size is " << mcCol->size() << endmsg;
+            return;
+        }
+        auto primary_particle = mcCol->at(curtrkid-1);
+
+        const G4ThreeVector& stop_pos  = track->GetPosition();
+        edm4hep::Vector3d endpoint(stop_pos.x()/CLHEP::mm,
+                                   stop_pos.y()/CLHEP::mm,
+                                   stop_pos.z()/CLHEP::mm);
+        primary_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);
+        primary_particle.setMomentumAtEndpoint(mom_endpoint);
+
+
+    } else {
+        // TODO: select other interested tracks
+    }
 
 }
 
-- 
GitLab