From 18d2a3026717a90585fee5fd1733a54ad2e2e126 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Tue, 23 Aug 2022 22:34:18 +0800
Subject: [PATCH] add GuineaPigPairsFileParser.

---
 Generator/CMakeLists.txt                   |  1 +
 Generator/src/GuineaPigPairsFileParser.cpp | 62 ++++++++++++++++++++++
 Generator/src/GuineaPigPairsFileParser.h   | 32 +++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 Generator/src/GuineaPigPairsFileParser.cpp
 create mode 100644 Generator/src/GuineaPigPairsFileParser.h

diff --git a/Generator/CMakeLists.txt b/Generator/CMakeLists.txt
index 6169e415..e411a482 100644
--- a/Generator/CMakeLists.txt
+++ b/Generator/CMakeLists.txt
@@ -18,6 +18,7 @@ gaudi_add_module(GenAlgo
                          # ------- Beam Background -------
                          src/GtBeamBackgroundTool.cpp
                          src/BeamBackgroundFileParserV0.cpp
+                         src/GuineaPigPairsFileParser.cpp
                  LINK ${ROOT_LIBRARIES}
                       k4FWCore::k4FWCore 
                       Gaudi::GaudiAlgLib
diff --git a/Generator/src/GuineaPigPairsFileParser.cpp b/Generator/src/GuineaPigPairsFileParser.cpp
new file mode 100644
index 00000000..13df7f23
--- /dev/null
+++ b/Generator/src/GuineaPigPairsFileParser.cpp
@@ -0,0 +1,62 @@
+#include "GuineaPigPairsFileParser.h"
+#include <sstream>
+#include <cmath>
+
+GuineaPigPairsFileParser::GuineaPigPairsFileParser(const std::string& filename) {
+    m_input.open(filename.c_str());
+}
+
+bool GuineaPigPairsFileParser::load(IBeamBackgroundFileParser::BeamBackgroundData& result) {
+    if (not m_input.good()) {
+        return false;
+    }
+
+    // read one record
+    std::string tmpline;
+    // the format
+    double energy; // unit: GeV
+    double vx; // unit: c
+    double vy; // unit: c
+    double vz; // unit: c
+    double x; // unit: nm
+    double y; // unit: nm
+    double z; // unit: nm
+    int process;
+
+    while(m_input.good()) {
+        std::getline(m_input, tmpline);
+        std::stringstream ss;
+        ss << tmpline;
+        ss >> energy;           if (ss.fail()) { continue; }
+        ss >> vx;               if (ss.fail()) { continue; }
+        ss >> vy;               if (ss.fail()) { continue; }
+        ss >> vz;               if (ss.fail()) { continue; }
+        ss >> x;                if (ss.fail()) { continue; }
+        ss >> y;                if (ss.fail()) { continue; }
+        ss >> z;                if (ss.fail()) { continue; }
+        ss >> process;          if (ss.fail()) { continue; }
+
+        int pdgid = 11; // 11: electron; -11: positron
+        if (energy<0) pdgid = -11;
+
+        double p = std::fabs(energy);
+        double v = sqrt(vx*vx+vy*vy+vz*vz);
+
+        // Now, we get a almost valid data
+        const double nm2mm = 1e-6; // convert from nm to mm
+        result.pdgid = pdgid;
+        result.x     = x * nm2mm; 
+        result.y     = y * nm2mm; 
+        result.z     = z * nm2mm;
+
+        result.px    = p * vx/v;
+        result.py    = p * vy/v;
+        result.pz    = p * vz/v;
+
+        result.mass  = 0.000511; // assume e-/e+, mass is 0.511 MeV
+
+        return true;
+    }
+    return false;
+
+}
diff --git a/Generator/src/GuineaPigPairsFileParser.h b/Generator/src/GuineaPigPairsFileParser.h
new file mode 100644
index 00000000..078a38a0
--- /dev/null
+++ b/Generator/src/GuineaPigPairsFileParser.h
@@ -0,0 +1,32 @@
+#ifndef GuineaPigPairsFileParser_h
+#define GuineaPigPairsFileParser_h
+
+#include "IBeamBackgroundFileParser.h"
+#include <fstream>
+
+/* Format of Guinea-Pig Pairs:
+ *
+ *   E vx vy vz x y z process
+ *
+ * Notes:
+ *   - E (GeV). If E>0, it is electron. If E<0, it is positron
+ *   - vx/vy/vz (speed of light)
+ *   - x/y/z (nm)
+ *   - process
+ *     - 0: Breit-Wheeler
+ *     - 1: Bethe-Heitler
+ *     - 2: Landau-Lifschitz
+ *
+ */
+
+class GuineaPigPairsFileParser: public IBeamBackgroundFileParser {
+public:
+    GuineaPigPairsFileParser(const std::string& filename);
+
+    bool load(IBeamBackgroundFileParser::BeamBackgroundData&);
+
+private:
+    std::ifstream m_input;
+};
+
+#endif
-- 
GitLab