From 5d9800984bdc5b4df88f8327517b7c03b5b095a1 Mon Sep 17 00:00:00 2001
From: Chris Jones <jonesc@hep.phy.cam.ac.uk>
Date: Wed, 1 Nov 2023 16:39:51 +0000
Subject: [PATCH] DecodedDataFromMCRichHits: Allow posibility to create more
 than one hit per channel

---
 .../component/DecodedDataFromMCRichHits.cpp   | 58 ++++++++++++++-----
 1 file changed, 43 insertions(+), 15 deletions(-)

diff --git a/Rich/RichFutureMCUtils/src/component/DecodedDataFromMCRichHits.cpp b/Rich/RichFutureMCUtils/src/component/DecodedDataFromMCRichHits.cpp
index b2586edf26c..d66b3851be4 100644
--- a/Rich/RichFutureMCUtils/src/component/DecodedDataFromMCRichHits.cpp
+++ b/Rich/RichFutureMCUtils/src/component/DecodedDataFromMCRichHits.cpp
@@ -159,9 +159,6 @@ namespace Rich::Future::MC {
         // Should never fail but just in case...
         if ( hits.empty() ) { continue; }
 
-        // Apply random inefficiency for each RICH
-        if ( m_randZeroOne.shoot() > m_detEff[id.rich()] ) { continue; }
-
         // temporary copy of SmartID
         auto newID = id;
 
@@ -179,21 +176,49 @@ namespace Rich::Future::MC {
         }
 #endif
 
-        // Add best time for this ID
-        if ( m_includeTimeInfo ) {
-          // attempt to select the 'best' time for this ID ...
-          const auto* mch = &hits.front();
-          for ( const auto& hit : hits ) {
-            if ( hit.first->isSignal() ) {
-              mch = &hit;
-              break;
+        // Are we allowing more than one decoded hit per channel ?
+        if ( !m_allowMultiHits ) {
+          // 'Normal' mode. Only allow a single decoded hit per channel
+
+          // Apply random inefficiency for each RICH
+          if ( m_randZeroOne.shoot() > m_detEff[id.rich()] ) { continue; }
+
+          // Add best time for this ID
+          if ( m_includeTimeInfo ) {
+            // attempt to select the 'best' time for this ID ...
+            const auto* mch = &hits.front();
+            if ( hits.size() > 1 ) {
+              for ( const auto& hit : hits ) {
+                if ( hit.first->isSignal() ) {
+                  mch = &hit;
+                  break;
+                }
+              }
             }
+            newID.setTime( mch->second );
           }
-          newID.setTime( mch->second );
-        }
 
-        // Add (dd4hep corrected) ID to decoded list
-        ids.emplace_back( newID );
+          // Add (dd4hep corrected) ID to decoded list
+          ids.emplace_back( newID );
+
+        } else {
+          // Allow more than one hit per channel, one per MC hit
+          // Useful for granularity studies.
+
+          // loop over all MC hits
+          for ( const auto& hit : hits ) {
+
+            // Apply random inefficiency for each RICH
+            if ( m_randZeroOne.shoot() > m_detEff[id.rich()] ) { continue; }
+
+            // Clone channel ID before adding time
+            auto idWithT = newID;
+            idWithT.setTime( hit.second );
+
+            // Add to decoded list
+            ids.emplace_back( idWithT );
+          }
+        }
 
         // Form MC summaries for each MCHit
         for ( const auto& hit : hits ) {
@@ -257,6 +282,9 @@ namespace Rich::Future::MC {
     /// Include spillover events
     Gaudi::Property<bool> m_enableSpillover{this, "EnableSpillover", true};
 
+    /// Allow multiple hits per channel
+    Gaudi::Property<bool> m_allowMultiHits{this, "AllowMultipleHits", false};
+
     /// Time window shift for each RICH
     Gaudi::Property<DetectorArray<double>> m_timeShift{
         this, "TimeCalib", {0., 40.}, "Global time shift for each RICH, to get both to same calibrated point"};
-- 
GitLab