From 7e5bc6f2ff90b2f5505f9373c1ade4cac440f35d Mon Sep 17 00:00:00 2001
From: Maximilian Reininghaus <maximilian.reininghaus@kit.edu>
Date: Fri, 24 May 2019 16:41:00 -0300
Subject: [PATCH] fixed handling of SwitchProcess inside ProcessSequence

---
 Framework/ProcessSequence/ProcessSequence.h | 23 +++++++++++++++------
 Processes/SwitchProcess/SwitchProcess.h     |  4 ++--
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/Framework/ProcessSequence/ProcessSequence.h b/Framework/ProcessSequence/ProcessSequence.h
index 55eb43ba7..e4108b95f 100644
--- a/Framework/ProcessSequence/ProcessSequence.h
+++ b/Framework/ProcessSequence/ProcessSequence.h
@@ -57,9 +57,15 @@ namespace corsika::process {
     class SwitchProcess; // fwd-decl.
   }
 
+  // to detect SwitchProcesses inside the ProcessSequence
+  template <typename T>
+  struct is_switch_process : std::false_type {};
+
   template <typename A, typename B>
-  struct is_process_sequence<switch_process::SwitchProcess<A, B>>
-      : std::true_type {};
+  struct is_switch_process<switch_process::SwitchProcess<A, B>> : std::true_type {};
+
+  template <typename T>
+  bool constexpr is_switch_process_v = is_switch_process<T>::value;
 
   /**
      T1 and T2 are both references if possible (lvalue), otherwise
@@ -75,6 +81,9 @@ namespace corsika::process {
     static bool constexpr t1ProcSeq = is_process_sequence_v<T1type>;
     static bool constexpr t2ProcSeq = is_process_sequence_v<T2type>;
 
+    static bool constexpr t1SwitchProc = is_switch_process_v<T1type>;
+    static bool constexpr t2SwitchProc = is_switch_process_v<T2type>;
+
   public:
     T1 A; // this is a reference, if possible
     T2 B; // this is a reference, if possible
@@ -185,10 +194,12 @@ namespace corsika::process {
 
       InverseGrammageType tot = 0 * meter * meter / gram;
 
-      if constexpr (std::is_base_of_v<InteractionProcess<T1type>, T1type> || t1ProcSeq) {
+      if constexpr (std::is_base_of_v<InteractionProcess<T1type>, T1type> || t1ProcSeq ||
+                    t1SwitchProc) {
         tot += A.GetInverseInteractionLength(vP);
       }
-      if constexpr (std::is_base_of_v<InteractionProcess<T2type>, T2type> || t2ProcSeq) {
+      if constexpr (std::is_base_of_v<InteractionProcess<T2type>, T2type> || t2ProcSeq ||
+                    t2SwitchProc) {
         tot += B.GetInverseInteractionLength(vP);
       }
       return tot;
@@ -200,7 +211,7 @@ namespace corsika::process {
         [[maybe_unused]] corsika::units::si::InverseGrammageType lambda_select,
         corsika::units::si::InverseGrammageType& lambda_inv_count) {
 
-      if constexpr (t1ProcSeq) {
+      if constexpr (t1ProcSeq || t1SwitchProc) {
         // if A is a process sequence --> check inside
         const EProcessReturn ret =
             A.SelectInteraction(vP, vS, lambda_select, lambda_inv_count);
@@ -216,7 +227,7 @@ namespace corsika::process {
         }
       } // end branch A
 
-      if constexpr (t2ProcSeq) {
+      if constexpr (t2ProcSeq || t2SwitchProc) {
         // if A is a process sequence --> check inside
         const EProcessReturn ret =
             B.SelectInteraction(vP, vS, lambda_select, lambda_inv_count);
diff --git a/Processes/SwitchProcess/SwitchProcess.h b/Processes/SwitchProcess/SwitchProcess.h
index 31e59599b..fd13021d2 100644
--- a/Processes/SwitchProcess/SwitchProcess.h
+++ b/Processes/SwitchProcess/SwitchProcess.h
@@ -12,8 +12,8 @@ namespace corsika::process::switch_process {
    * This process provides an energy-based switch between two interaction processes P1 and
    * P1. For energies below the threshold, P1 is invoked, otherwise P2. Both can be either
    * single interaction processes or multiple ones combined in a ProcessSequence. A
-   * SwitchProcess itself will always be regarded as a ProcessSequence rather than an
-   * InteractionProcess when assembled into a greater ProcessSequence.
+   * SwitchProcess itself will always be regarded as a distinct case when assembled into a
+   * (greater) ProcessSequence.
    */
 
   template <class TLowEProcess, class THighEProcess>
-- 
GitLab