From f8ebfc2087eed6bedd8627033a53f36ce2753d3c Mon Sep 17 00:00:00 2001
From: ralfulrich <ralf.ulrich@kit.edu>
Date: Fri, 30 Nov 2018 07:59:14 +0100
Subject: [PATCH] fixed the canInteract, knownby flags for sibyll, and added
 the xs-type code

---
 Processes/Sibyll/ParticleConversion.h |  4 +++
 Processes/Sibyll/code_generator.py    | 37 +++++++++++++++++++--------
 Processes/Sibyll/testSibyll.cc        | 28 ++++++++++----------
 3 files changed, 45 insertions(+), 24 deletions(-)

diff --git a/Processes/Sibyll/ParticleConversion.h b/Processes/Sibyll/ParticleConversion.h
index e0ef48ecf..9d7227ce9 100644
--- a/Processes/Sibyll/ParticleConversion.h
+++ b/Processes/Sibyll/ParticleConversion.h
@@ -45,6 +45,10 @@ namespace corsika::process::sibyll {
   int ConvertToSibyllRaw(corsika::particles::Code pCode){
     return  (int)static_cast<corsika::process::sibyll::SibyllCodeIntType>( corsika::process::sibyll::ConvertToSibyll( pCode ) );
   }
+
+  int GetSibyllXSCode(corsika::particles::Code pCode) {
+    return corsika2sibyllXStype[static_cast<corsika::particles::CodeIntType>(pCode)];
+  }
   
 } // namespace corsika::process::sibyll
 
diff --git a/Processes/Sibyll/code_generator.py b/Processes/Sibyll/code_generator.py
index 4049bb9cd..77c1f5dd8 100755
--- a/Processes/Sibyll/code_generator.py
+++ b/Processes/Sibyll/code_generator.py
@@ -28,15 +28,11 @@ def read_sibyll_codes(filename, pythia_db):
             line = line.strip()
             if line[0] == '#':
                 continue            
-<<<<<<< HEAD
             identifier, sib_code, canInteractFlag, xsType = line.split()
-=======
-            identifier, sib_code, canInteractFlag, xsctnId = line.split()
->>>>>>> ca4ed4f568320603f81b9a4f0c19a45fb6dd1d58
             try:
                 pythia_db[identifier]["sibyll_code"] = int(sib_code)
                 pythia_db[identifier]["sibyll_canInteract"] = int(canInteractFlag)
-                pythia_db[identifier]["xsType"] = int(xsType)
+                pythia_db[identifier]["sibyll_xsType"] = int(xsType)
             except KeyError as e:
                 raise Exception("Identifier '{:s}' not found in pythia_db".format(identifier))
 
@@ -56,7 +52,7 @@ def generate_sibyll_enum(pythia_db):
 
 # generates the look-up table to convert corsika codes to sibyll codes
 def generate_corsika2sibyll(pythia_db):    
-    string = "std::array<SibyllCodeIntType, {:d}> constexpr corsika2sibyll = {{".format(len(pythia_db))
+    string = "std::array<SibyllCodeIntType, {:d}> constexpr corsika2sibyll = {{\n".format(len(pythia_db))
     for identifier, pData in pythia_db.items():
         sibCode = pData.get("sibyll_code", 0)
         string += "  {:d}, // {:s}\n".format(sibCode, identifier if sibCode else identifier + " (not implemented in SIBYLL)")
@@ -65,13 +61,27 @@ def generate_corsika2sibyll(pythia_db):
     
 
 
+# generates the look-up table to convert corsika codes to sibyll codes
+def generate_corsika2sibyll_xsType(pythia_db):    
+    string = "std::array<int, {:d}> constexpr corsika2sibyllXStype = {{\n".format(len(pythia_db))
+    for identifier, pData in pythia_db.items():
+        sibCodeXS = pData.get("sibyll_xsType", -1)
+        string += "  {:d}, // {:s}\n".format(sibCodeXS, identifier if sibCodeXS else identifier + " (not implemented in SIBYLL)")
+    string += "};\n"
+    return string
+
+
 # generates the look-up table to convert sibyll codes to corsika codes    
 def generate_sibyll2corsika(pythia_db) :
+    string = ""
+    
     minID = 0
     for identifier, pData in pythia_db.items() :
         if 'sibyll_code' in pData:
             minID = min(minID, pData['sibyll_code'])
-            
+
+    string += "SibyllCodeIntType constexpr minSibyll = {:d};\n\n".format(minID)
+
     pDict = {}
     for identifier, pData in pythia_db.items() :
         if 'sibyll_code' in pData:
@@ -79,7 +89,7 @@ def generate_sibyll2corsika(pythia_db) :
             pDict[sib_code] = identifier
     
     nPart = max(pDict.keys()) - min(pDict.keys()) + 1
-    string = "std::array<corsika::particles::Code, {:d}> sibyll2corsika = {{\n".format(nPart)
+    string += "std::array<corsika::particles::Code, {:d}> sibyll2corsika = {{\n".format(nPart)
     
     for iPart in range(nPart) :
         if iPart in pDict:
@@ -89,7 +99,6 @@ def generate_sibyll2corsika(pythia_db) :
         string += "  corsika::particles::Code::{:s}, \n".format(identifier)
     
     string += "};\n"
-    string += "SibyllCodeIntType constexpr minSibyll = {:d};\n".format(minID)
     return string
 
 
@@ -120,12 +129,15 @@ def generate_interacting_particle(pythia_db):
     num_particles = len(pythia_db)
     num_bytes = num_particles // 32 + 1
     string = "Bitset2::bitset2<{:d}> constexpr canInteract{{ std::array<uint32_t, {:d}>{{{{\n".format(num_particles, num_bytes)
+    #string = "std::array<bool, {:d}> constexpr corsika2sibyll = {{\n".format(num_particles)
     
     numeric = 0    
     for identifier, pData in reversed(pythia_db.items()):
+        can = 0
         if 'sibyll_canInteract' in pData:
-            can = int(pData["sibyll_canInteract"]) & 0x1
-            numeric = (numeric << 1) | can
+            if pData['sibyll_canInteract'] > 0:
+                can = 0x1
+        numeric = (numeric << 1) | can
     
     while numeric != 0:
         low = numeric & 0xFFFFFFFF
@@ -147,6 +159,8 @@ if __name__ == "__main__":
     pythia_db = load_pythiadb(sys.argv[1])
     read_sibyll_codes(sys.argv[2], pythia_db)
     
+    print (str(pythia_db))
+    
     with open("Generated.inc", "w") as f:
         print("// this file is automatically generated\n// edit at your own risk!\n", file=f)
         print(generate_sibyll_enum(pythia_db), file=f)
@@ -154,3 +168,4 @@ if __name__ == "__main__":
         print(generate_known_particle(pythia_db), file=f)
         print(generate_sibyll2corsika(pythia_db), file=f)
         print(generate_interacting_particle(pythia_db), file=f)
+        print(generate_corsika2sibyll_xsType(pythia_db), file=f)
diff --git a/Processes/Sibyll/testSibyll.cc b/Processes/Sibyll/testSibyll.cc
index 1f10c2f58..547de8f93 100644
--- a/Processes/Sibyll/testSibyll.cc
+++ b/Processes/Sibyll/testSibyll.cc
@@ -17,6 +17,8 @@
                           // cpp file
 #include <catch2/catch.hpp>
 
+#include <iostream>
+using namespace std;
 using namespace corsika;
 
 TEST_CASE("Sibyll", "[processes]") {
@@ -30,30 +32,30 @@ TEST_CASE("Sibyll", "[processes]") {
     REQUIRE(process::sibyll::ConvertToSibyll(corsika::particles::Electron::GetCode()) ==
             process::sibyll::SibyllCode::Electron);
     REQUIRE(process::sibyll::ConvertToSibyllRaw(corsika::particles::Proton::GetCode()) ==
-            13 );
+            13);
   }
 
-<<<<<<< HEAD
-  SECTION("handledBySibyll") {
-=======
   SECTION("KnownBySibyll") {
->>>>>>> ca4ed4f568320603f81b9a4f0c19a45fb6dd1d58
     REQUIRE(process::sibyll::KnownBySibyll(corsika::particles::Electron::GetCode()));
 
     REQUIRE_FALSE(
         process::sibyll::KnownBySibyll(corsika::particles::XiPrimeC0::GetCode()));
-<<<<<<< HEAD
   }
 
   SECTION("canInteractInSibyll") {
+
     REQUIRE(process::sibyll::CanInteract(corsika::particles::Proton::GetCode()));
-    REQUIRE(process::sibyll::CanInteract(corsika::particles::XiCPlus::GetCode()));
+    REQUIRE(process::sibyll::CanInteract(corsika::particles::Code::XiCPlus));
 
-    REQUIRE_FALSE(
-		  process::sibyll::CanInteract(corsika::particles::Electron::GetCode()));
-    REQUIRE_FALSE(
-		  process::sibyll::CanInteract(corsika::particles::SigmaC0::GetCode()));
-=======
->>>>>>> ca4ed4f568320603f81b9a4f0c19a45fb6dd1d58
+    REQUIRE_FALSE(process::sibyll::CanInteract(corsika::particles::Electron::GetCode()));
+    REQUIRE_FALSE(process::sibyll::CanInteract(corsika::particles::SigmaC0::GetCode()));
+  }
+  
+  SECTION("canInteractInSibyll") {
+
+    REQUIRE(process::sibyll::GetSibyllXSCode(corsika::particles::Code::Electron) == 0);
+    REQUIRE(process::sibyll::GetSibyllXSCode(corsika::particles::Code::K0Long) == 3);
+    REQUIRE(process::sibyll::GetSibyllXSCode(corsika::particles::Code::SigmaPlus) == 1);
+    REQUIRE(process::sibyll::GetSibyllXSCode(corsika::particles::Code::PiMinus) == 2);
   }
 }
-- 
GitLab