From fe3b8e8abb8554812331798070173da2e4ce3378 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Tue, 12 Oct 2021 21:16:37 +0800
Subject: [PATCH] WIP: create MC particle from the Beam Background Data.

---
 Generator/src/GtBeamBackgroundTool.cpp | 40 ++++++++++++++++++++++++++
 Generator/src/GtBeamBackgroundTool.h   |  2 +-
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/Generator/src/GtBeamBackgroundTool.cpp b/Generator/src/GtBeamBackgroundTool.cpp
index 8eea3cf2..a2d3d983 100644
--- a/Generator/src/GtBeamBackgroundTool.cpp
+++ b/Generator/src/GtBeamBackgroundTool.cpp
@@ -1,5 +1,6 @@
 #include "GtBeamBackgroundTool.h"
 #include "IBeamBackgroundFileParser.h"
+#include "BeamBackgroundFileParserV0.h"
 DECLARE_COMPONENT(GtBeamBackgroundTool)
 
 StatusCode GtBeamBackgroundTool::initialize() {
@@ -7,6 +8,17 @@ StatusCode GtBeamBackgroundTool::initialize() {
 
     // create the instances of the background parsers
 
+    for (auto& [label, inputfn]: m_inputmaps) {
+        
+        m_beaminputs[label] = std::make_shared<BeamBackgroundFileParserV0>(inputfn, 11, 125.);
+    }
+
+    // check the size
+    if (m_beaminputs.empty()) {
+        error() << "Empty Beam Background File Parser. " << endmsg;
+        return StatusCode::FAILURE;
+    }
+
     return StatusCode::SUCCESS;
 }
 
@@ -16,6 +28,34 @@ StatusCode GtBeamBackgroundTool::finalize() {
 
 
 bool GtBeamBackgroundTool::mutate(MyHepMC::GenEvent& event) {
+    if (m_beaminputs.empty()) {
+        return false;
+    }
+    // TODO: should sample according to the rates
+    // dummy: get one and stop to generate
+    for (auto& [label, parser]: m_beaminputs) {
+        IBeamBackgroundFileParser::BeamBackgroundData beamdata;
+        auto isok = parser->load(beamdata);
+        if (not isok) {
+            error() << "Failed to load beam background data from the parser " << label << endmsg;
+            return false;
+        }
+        // fill the value
+        float charge = beamdata.pdgid == 11 ? -1: 1;
+
+        // create the MC particle
+        edm4hep::MCParticle mcp = event.m_mc_vec.create();
+        mcp.setPDG(beamdata.pdgid);
+        mcp.setGeneratorStatus(1);
+        mcp.setSimulatorStatus(1);
+        mcp.setCharge(static_cast<float>(charge));
+        mcp.setTime(beamdata.t);
+        mcp.setMass(beamdata.mass);
+        mcp.setVertex(edm4hep::Vector3d(beamdata.x,beamdata.y,beamdata.z)); 
+        mcp.setMomentum(edm4hep::Vector3f(beamdata.px,beamdata.py,beamdata.pz));
+
+    }
+    
     return true;
 }
 
diff --git a/Generator/src/GtBeamBackgroundTool.h b/Generator/src/GtBeamBackgroundTool.h
index 4750a96a..90fa37f8 100644
--- a/Generator/src/GtBeamBackgroundTool.h
+++ b/Generator/src/GtBeamBackgroundTool.h
@@ -27,11 +27,11 @@
 #include <GaudiKernel/AlgTool.h>
 #include <Gaudi/Property.h>
 #include "IGenTool.h"
+#include "IBeamBackgroundFileParser.h"
 
 #include <vector>
 #include <map>
 
-class IBeamBackgroundFileParser;
 
 class GtBeamBackgroundTool: public extends<AlgTool, IGenTool> {
 public:
-- 
GitLab