From a9bce30dcf433ca3ad5d4dacc81b965cdc7d4cb8 Mon Sep 17 00:00:00 2001
From: Eric Torrence <eric.torrence@cern.ch>
Date: Sun, 23 Jan 2022 10:39:40 -0800
Subject: [PATCH] Fix minor bug, add IFT geometry to waveform channel mapping

---
 .../WaveByteStream/python/WaveByteStreamConfig.py   | 11 +++++++++++
 .../WaveRecTools/src/ClockReconstructionTool.cxx    | 13 +++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/Waveform/WaveEventCnv/WaveByteStream/python/WaveByteStreamConfig.py b/Waveform/WaveEventCnv/WaveByteStream/python/WaveByteStreamConfig.py
index cc13137d..840a4d0f 100644
--- a/Waveform/WaveEventCnv/WaveByteStream/python/WaveByteStreamConfig.py
+++ b/Waveform/WaveEventCnv/WaveByteStream/python/WaveByteStreamConfig.py
@@ -37,6 +37,17 @@ def WaveByteStreamCfg(configFlags, **kwargs):
         waveform_tool.PreshowerChannels = [12, 13]
         acc.addPublicTool(waveform_tool)
 
+    elif configFlags.GeoModel.FaserVersion == "FASER-02":
+        print(" - setting up TI12 with IFT detector")
+
+        # Need to fix this!
+        waveform_tool = CompFactory.RawWaveformDecoderTool("RawWaveformDecoderTool")
+        waveform_tool.CaloChannels = [1, 0, 3, 2]
+        waveform_tool.VetoChannels = [4, 5, 6, 7]
+        waveform_tool.TriggerChannels = [9, 8, 11, 10]
+        waveform_tool.PreshowerChannels = [12, 13]
+        acc.addPublicTool(waveform_tool)
+
     else:
         print(" - unknown version: user must set up Waveform channel mapping by hand!")
 
diff --git a/Waveform/WaveRecTools/src/ClockReconstructionTool.cxx b/Waveform/WaveRecTools/src/ClockReconstructionTool.cxx
index aaae0861..c45ea510 100644
--- a/Waveform/WaveRecTools/src/ClockReconstructionTool.cxx
+++ b/Waveform/WaveRecTools/src/ClockReconstructionTool.cxx
@@ -79,8 +79,8 @@ ClockReconstructionTool::reconstruct(const RawWaveform& raw_wave,
   // Get the coefficients
   std::vector<double> re_full(N);
   std::vector<double> im_full(N);
-  std::vector<double> magnitude(N/2); 
-  fftr2c->GetPointsComplex(&re_full[0],&im_full[0]);
+  std::vector<double> magnitude(N/2+1);  // From fftw manual, output array is N/2+1 long 
+  fftr2c->GetPointsComplex(&re_full[0], &im_full[0]);
 
   // Normalize the magnitude (just using the positive complex frequencies)
   unsigned int i=0;
@@ -106,7 +106,7 @@ ClockReconstructionTool::reconstruct(const RawWaveform& raw_wave,
   clockdata->set_dc_offset(magnitude[0]);
   clockdata->set_frequency(imax * freqmult);
   clockdata->set_amplitude(magnitude[imax]);
-  clockdata->set_phase(atan2(im_full[imax], re_full[imax]));
+  clockdata->set_phase(atan2(im_full[imax], re_full[imax])); // Not a bug, atan2(y,x)!
 
   ATH_MSG_DEBUG("Before correcting for finite resolution:");
   ATH_MSG_DEBUG(*clockdata);
@@ -125,10 +125,11 @@ ClockReconstructionTool::reconstruct(const RawWaveform& raw_wave,
 
   // Improved values
 
+  // Not a bug, atan2(y,x)!
   double phase = atan2(im_full[imax], re_full[imax]) - dm * M_PI;
-  // Fix any overflows
-  if (phase < M_PI) phase += (2*M_PI);
-  if (phase > M_PI) phase -= (2*M_PI);
+  // Fix any overflows caused by adding dm
+  if (phase < -M_PI) phase += (2*M_PI);
+  if (phase > +M_PI) phase -= (2*M_PI);
 
   clockdata->set_frequency( (imax+dm) * freqmult );
   clockdata->set_phase (phase);
-- 
GitLab