From 696250431eae1f1bd06baac708be0b78f6c76922 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Sat, 20 Jun 2020 10:54:32 +0800
Subject: [PATCH] WIP: bookkeeping of the current track and its primary track.

---
 .../src/Edm4hepWriterAnaElemTool.cpp          | 34 +++++++++++++++++--
 .../DetSimAna/src/Edm4hepWriterAnaElemTool.h  | 16 +++++++++
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp
index ea62694d..b2930227 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 a0b48497..365078ac 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
-- 
GitLab