From b856dd5ded4c1beb1426bac9d38e90d1436a27c6 Mon Sep 17 00:00:00 2001
From: "lintao@ihep.ac.cn" <lintao@ihep.ac.cn>
Date: Wed, 6 Nov 2024 11:41:26 +0000
Subject: [PATCH] GtPythiaTool: add mother and daughter.

---
 Generator/src/GtPythiaTool.cpp | 146 +++++++++++++++++++--------------
 1 file changed, 85 insertions(+), 61 deletions(-)

diff --git a/Generator/src/GtPythiaTool.cpp b/Generator/src/GtPythiaTool.cpp
index b34c92ef..d739cb76 100755
--- a/Generator/src/GtPythiaTool.cpp
+++ b/Generator/src/GtPythiaTool.cpp
@@ -1,62 +1,86 @@
-#include "GtPythiaTool.hh"
-
-DECLARE_COMPONENT(GtPythiaTool)
-
-StatusCode GtPythiaTool::initialize() {
-    StatusCode sc;
-    m_pythia = std::make_unique<Pythia8::Pythia>();
-    // configure with the card file first
-    m_pythia->readFile(m_card.value());
-    // tune with the additional commands
-    for (auto cmd: m_cmds.value()) {
-        m_pythia->readString(cmd);
-    }
-    // initialize pythia
-    m_pythia->init();
-    return sc;
-}
-
-StatusCode GtPythiaTool::finalize() {
-    StatusCode sc;
-    return sc;
-}
-
-bool GtPythiaTool::mutate(Gen::GenEvent& event) {
-    // generate the event
-    while(!m_pythia->next()) {
-        // if failed, try again
-    }
-
-    // get the particles
-    auto& pythia_particles = m_pythia->event;
-    // loop over the particles
-    for (int i = 0; i < pythia_particles.size(); ++i) {
-        auto& p = pythia_particles[i];
-        // create the MCParticle
-        auto mcp = event.getMCVec().create();
-        // set the properties
-        mcp.setPDG(p.id());
-        int status = 0;
-        if (p.isFinal()) {
-            status = 1;
-        } else {
-            status = 0;
-        }
-        mcp.setGeneratorStatus(status);
-        mcp.setCharge(p.charge());
-        mcp.setTime(p.tau());
-        mcp.setMass(p.m());
-        mcp.setVertex(edm4hep::Vector3d(p.xProd(), p.yProd(), p.zProd()));
-        mcp.setEndpoint(edm4hep::Vector3d(p.xDec(), p.yDec(), p.zDec()));
-        mcp.setMomentum(edm4hep::Vector3f(p.px(), p.py(), p.pz()));
-    }
-    return true;
-}
-
-bool GtPythiaTool::finish() {
-    return true;
-}
-
-bool GtPythiaTool::configure_gentool() {
-    return true;
+#include "GtPythiaTool.hh"
+
+DECLARE_COMPONENT(GtPythiaTool)
+
+StatusCode GtPythiaTool::initialize() {
+    StatusCode sc;
+    m_pythia = std::make_unique<Pythia8::Pythia>();
+    // configure with the card file first
+    m_pythia->readFile(m_card.value());
+    // tune with the additional commands
+    for (auto cmd: m_cmds.value()) {
+        m_pythia->readString(cmd);
+    }
+    // initialize pythia
+    m_pythia->init();
+    return sc;
+}
+
+StatusCode GtPythiaTool::finalize() {
+    StatusCode sc;
+    return sc;
+}
+
+bool GtPythiaTool::mutate(Gen::GenEvent& event) {
+    // generate the event
+    while(!m_pythia->next()) {
+        // if failed, try again
+    }
+
+    // get the particles
+    auto& pythia_particles = m_pythia->event;
+    // loop over the particles
+    for (int i = 0; i < pythia_particles.size(); ++i) {
+        auto& p = pythia_particles[i];
+        // create the MCParticle
+        auto mcp = event.getMCVec().create();
+        // set the properties
+        mcp.setPDG(p.id());
+        int status = 0;
+        if (p.isFinal()) {
+            status = 1;
+        } else {
+            status = 0;
+        }
+        mcp.setGeneratorStatus(status);
+        mcp.setCharge(p.charge());
+        mcp.setTime(p.tProd());
+        mcp.setMass(p.m());
+        mcp.setVertex(edm4hep::Vector3d(p.xProd(), p.yProd(), p.zProd()));
+        // update the endpoint later
+        mcp.setEndpoint(edm4hep::Vector3d(p.xProd(), p.yProd(), p.zProd()));
+        mcp.setMomentum(edm4hep::Vector3f(p.px(), p.py(), p.pz()));
+    }
+    // setup the relationships (mother and daughter)
+    for (int i = 0; i < pythia_particles.size(); ++i) {
+        auto& p = pythia_particles[i];
+        auto mcp = event.getMCVec()[i];
+
+        auto mother_list = p.motherList();
+        for (auto idx: mother_list) {
+            auto mother = event.getMCVec()[idx];
+            mcp.addToParents(mother);
+        }
+
+        auto daughter_list = p.daughterList();
+        bool need_endpoint = true;
+        for (auto idx: daughter_list) {
+            auto daughter = event.getMCVec()[idx];
+            mcp.addToDaughters(daughter);
+            // Update the endpoint to the daughter's vertex
+            if (need_endpoint) {
+                mcp.setEndpoint(daughter.getVertex());
+                need_endpoint = false;
+            }
+        }
+    }
+    return true;
+}
+
+bool GtPythiaTool::finish() {
+    return true;
+}
+
+bool GtPythiaTool::configure_gentool() {
+    return true;
 }
\ No newline at end of file
-- 
GitLab