From d7943b24fdd930d4adafa7a6c4cb133213ca8029 Mon Sep 17 00:00:00 2001
From: Eric Torrence <eric.torrence@cern.ch>
Date: Mon, 9 May 2022 23:12:03 -0700
Subject: [PATCH] Make some temp changes to get MC reco working

---
 .../WaveRecAlgs/python/WaveRecAlgsConfig.py   | 10 ++++++---
 .../WaveRecAlgs/src/RawWaveformRecAlg.cxx     |  4 +++-
 .../src/WaveformReconstructionTool.cxx        | 21 +++++++++++++++----
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/Waveform/WaveRecAlgs/python/WaveRecAlgsConfig.py b/Waveform/WaveRecAlgs/python/WaveRecAlgsConfig.py
index ad4421093..7d3219eb0 100644
--- a/Waveform/WaveRecAlgs/python/WaveRecAlgsConfig.py
+++ b/Waveform/WaveRecAlgs/python/WaveRecAlgsConfig.py
@@ -22,6 +22,7 @@ def WaveformReconstructionCfg(flags, naive = False):
     if flags.Input.isMC  and naive:
         if "TB" not in flags.GeoModel.FaserVersion:
             acc.merge(PseudoScintHitToWaveformRecCfg(flags, "PseudoTriggerHitWaveformRecAlg", "Trigger"))
+
         acc.merge(PseudoScintHitToWaveformRecCfg(flags, "PseudoVetoHitToWaveformRecAlg", "Veto"))
         acc.merge(PseudoScintHitToWaveformRecCfg(flags, "PseudoPresehowerHitWaveformRecAlg", "Preshower"))
         acc.merge(PseudoCaloHitToWaveformRecCfg(flags, "PseudoCaloHitWaveformRecAlg"))                
@@ -29,7 +30,10 @@ def WaveformReconstructionCfg(flags, naive = False):
 
     acc.merge(WaveformHitRecCfg(flags, "TriggerWaveformRecAlg", "Trigger"))
     acc.merge(WaveformHitRecCfg(flags, "VetoWaveformRecAlg", "Veto"))
-    acc.merge(WaveformHitRecCfg(flags, "VetoNuWaveformRecAlg", "VetoNu"))
+    if flags.Input.isMC:
+        print("Turning off VetoNu reco in MC!")
+    else:
+        acc.merge(WaveformHitRecCfg(flags, "VetoNuWaveformRecAlg", "VetoNu"))
     acc.merge(WaveformHitRecCfg(flags, "PreshowerWaveformRecAlg", "Preshower"))
     acc.merge(WaveformHitRecCfg(flags, "CaloWaveformRecAlg", "Calo"))
 
@@ -58,8 +62,8 @@ def WaveformHitRecCfg(flags, name="WaveformRecAlg", source="", **kwargs):
 
     acc = ComponentAccumulator()
 
-    if flags.Input.isMC:
-        kwargs.setdefault("PeakThreshold", 5)
+    #if flags.Input.isMC:
+    #    kwargs.setdefault("PeakThreshold", 5)
 
     tool = WaveformReconstructionTool(name=source+"WaveformRecTool", **kwargs)
 
diff --git a/Waveform/WaveRecAlgs/src/RawWaveformRecAlg.cxx b/Waveform/WaveRecAlgs/src/RawWaveformRecAlg.cxx
index 243b88189..8f8ac4809 100644
--- a/Waveform/WaveRecAlgs/src/RawWaveformRecAlg.cxx
+++ b/Waveform/WaveRecAlgs/src/RawWaveformRecAlg.cxx
@@ -44,6 +44,9 @@ StatusCode
 RawWaveformRecAlg::execute(const EventContext& ctx) const {
   ATH_MSG_DEBUG("Executing");
 
+  // Keep track of some statistics
+  m_numberOfEvents++;
+
   ATH_MSG_DEBUG("Run: " << ctx.eventID().run_number() 
 		<< " Event: " << ctx.eventID().event_number());
 
@@ -96,7 +99,6 @@ RawWaveformRecAlg::execute(const EventContext& ctx) const {
   ATH_MSG_DEBUG("WaveformsHitContainer '" << hitContainerHandle.name() << "' filled with "<< hitContainerHandle->size() <<" items");
 
   // Keep track of some statistics
-  m_numberOfEvents++;
   for (const auto& hit : *(hitContainerHandle.ptr())) {
     if (hit->status_bit(xAOD::WaveformStatus::THRESHOLD_FAILED)) continue;
 
diff --git a/Waveform/WaveRecTools/src/WaveformReconstructionTool.cxx b/Waveform/WaveRecTools/src/WaveformReconstructionTool.cxx
index 296b36d20..25cdc0a39 100644
--- a/Waveform/WaveRecTools/src/WaveformReconstructionTool.cxx
+++ b/Waveform/WaveRecTools/src/WaveformReconstructionTool.cxx
@@ -55,9 +55,6 @@ WaveformReconstructionTool::reconstructPrimary(
 
   ATH_MSG_DEBUG(" reconstructPrimary called");
 
-  // Get the nominal trigger time (in ns) from config
-  float trigger_time = m_timingTool->nominalTriggerTime();
-
   xAOD::WaveformHit* newhit = new xAOD::WaveformHit();
   hitContainer->push_back(newhit);
 
@@ -90,6 +87,9 @@ WaveformReconstructionTool::reconstructPrimary(
   if (newhit->status_bit(xAOD::WaveformStatus::BASELINE_FAILED)) 
     return StatusCode::SUCCESS;
 
+  // Get the nominal trigger time (in ns) from config
+  float trigger_time = m_timingTool->nominalTriggerTime();
+
   // Set range for windowed data in digitizer samples
   float offset = m_timingTool->triggerTimeOffset(wave.channel());
 
@@ -126,7 +126,10 @@ WaveformReconstructionTool::reconstructSecondary(
 
   for( const auto& hit : *hitContainer) {
 
-    if (hit->channel() == wave.channel()) {
+    // Use id rather than channel to make sure this works on MC
+    if (hit->identify() == wave.identify()) {
+      ATH_MSG_DEBUG("Found primary hit in channel "<< hit->channel() 
+		    << " with id 0x" << std::hex << hit->identify() << std::dec );
       primaryHit = hit;
       break;
     }
@@ -138,6 +141,16 @@ WaveformReconstructionTool::reconstructSecondary(
     return StatusCode::FAILURE;
   }
 
+  if (primaryHit->status_bit(xAOD::WaveformStatus::WAVEFORM_MISSING)) {
+    ATH_MSG_DEBUG("Found primary hit with waveform missing");
+    return StatusCode::SUCCESS;
+  }
+
+  if (primaryHit->status_bit(xAOD::WaveformStatus::WAVEFORM_INVALID)) {
+    ATH_MSG_DEBUG("Found primary hit with waveform invalid");
+    return StatusCode::SUCCESS;
+  }
+
   WaveformBaselineData baseline;
 
   baseline.mean = primaryHit->baseline_mean();
-- 
GitLab