From b85b7fe84b67698ea4633867ae1fb3c74b01ce96 Mon Sep 17 00:00:00 2001
From: Eric Torrence <eric.torrence@cern.ch>
Date: Tue, 9 Mar 2021 16:28:37 -0800
Subject: [PATCH] Writing WaveformClock as xAOD object works

---
 Scintillator/ScintRecAlgs/CMakeLists.txt      |  2 +-
 .../ScintRecAlgs/src/ScintClockRecAlg.cxx     |  7 +++--
 .../ScintRecAlgs/src/ScintClockRecAlg.h       |  7 +++--
 Scintillator/ScintRecTools/CMakeLists.txt     |  2 +-
 .../ScintRecTools/IClockReconstructionTool.h  |  5 +--
 .../src/ClockReconstructionTool.cxx           | 31 +++++++++----------
 .../src/ClockReconstructionTool.h             |  5 +--
 .../Root/WaveformClock_v1.cxx                 |  3 ++
 .../versions/WaveformClock_v1.h               |  4 ++-
 .../xAODFaserWaveform/xAODFaserWaveformDict.h |  2 +-
 .../CMakeLists.txt                            | 14 +++++++++
 .../src/xAODWaveformClockAuxInfoCnv.cxx       |  5 +++
 .../src/xAODWaveformClockAuxInfoCnv.h         | 15 +++++++++
 .../src/xAODWaveformClockCnv.cxx              |  5 +++
 .../src/xAODWaveformClockCnv.h                | 15 +++++++++
 15 files changed, 93 insertions(+), 29 deletions(-)
 create mode 100644 xAOD/xAODFaserWaveformAthenaPool/CMakeLists.txt
 create mode 100644 xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.cxx
 create mode 100644 xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.h
 create mode 100644 xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.cxx
 create mode 100644 xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.h

diff --git a/Scintillator/ScintRecAlgs/CMakeLists.txt b/Scintillator/ScintRecAlgs/CMakeLists.txt
index e4f81b4b..1cc6650e 100644
--- a/Scintillator/ScintRecAlgs/CMakeLists.txt
+++ b/Scintillator/ScintRecAlgs/CMakeLists.txt
@@ -9,7 +9,7 @@ atlas_subdir( ScintRecAlgs )
 atlas_add_component( ScintRecAlgs
                      src/*.cxx src/*.h
                      src/components/*.cxx
-                     LINK_LIBRARIES AthenaBaseComps StoreGateLib ScintRawEvent ScintRecEvent ScintRecToolsLib )
+                     LINK_LIBRARIES AthenaBaseComps StoreGateLib ScintRawEvent ScintRecEvent xAODFaserWaveform ScintRecToolsLib )
 
 #atlas_install_python_modules( python/*.py )
 
diff --git a/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.cxx b/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.cxx
index cbdb8521..45ceaa30 100644
--- a/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.cxx
+++ b/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.cxx
@@ -53,8 +53,11 @@ ScintClockRecAlg::execute(const EventContext& ctx) const {
   }
 
   // Create the output clock container
-  SG::WriteHandle<WaveformClock> clockHandle(m_waveformClockKey, ctx);
-  ATH_CHECK( clockHandle.record( std::make_unique<WaveformClock>() ) );
+  SG::WriteHandle<xAOD::WaveformClock> clockHandle(m_waveformClockKey, ctx);
+  //ATH_CHECK( clockHandle.record( std::make_unique<WaveformClock>() ) );
+  ATH_CHECK( clockHandle.record( std::make_unique<xAOD::WaveformClock>(),
+				 std::make_unique<xAOD::WaveformClockAuxInfo>() ) );
+
   auto clock = clockHandle.ptr();
 
   // Reconstruct first element
diff --git a/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.h b/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.h
index ea0ac1db..8fb35555 100644
--- a/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.h
+++ b/Scintillator/ScintRecAlgs/src/ScintClockRecAlg.h
@@ -8,8 +8,9 @@
 #include "ScintRawEvent/ScintWaveformContainer.h"
 
 // Output data
-#include "ScintRecEvent/WaveformClock.h"
-
+// #include "ScintRecEvent/WaveformClock.h"
+#include "xAODFaserWaveform/WaveformClock.h"
+#include "xAODFaserWaveform/WaveformClockAuxInfo.h"
 #include "ScintRecTools/IClockReconstructionTool.h"
 
 #include "StoreGate/ReadHandleKey.h"
@@ -62,7 +63,7 @@ class ScintClockRecAlg : public AthReentrantAlgorithm {
    * @name Output data using SG::WriteHandleKey
    */
   //@{
-  SG::WriteHandleKey<WaveformClock> m_waveformClockKey
+  SG::WriteHandleKey<xAOD::WaveformClock> m_waveformClockKey
     {this, "WaveformClockKey", "LHCClockData"};
   //@}
 
diff --git a/Scintillator/ScintRecTools/CMakeLists.txt b/Scintillator/ScintRecTools/CMakeLists.txt
index 7befc0e4..285298d6 100644
--- a/Scintillator/ScintRecTools/CMakeLists.txt
+++ b/Scintillator/ScintRecTools/CMakeLists.txt
@@ -13,7 +13,7 @@ atlas_add_library( ScintRecToolsLib
                    ScintRecTools/*.h src/*.cxx src/*.h
                    PUBLIC_HEADERS ScintRecTools
                    PRIVATE_INCLUDE_DIRS ${ROOT_INCLUDE_DIRS}
-                   LINK_LIBRARIES AthenaBaseComps AthenaKernel GeoPrimitives ScintRawEvent ScintRecEvent
+                   LINK_LIBRARIES AthenaBaseComps AthenaKernel GeoPrimitives ScintRawEvent ScintRecEvent xAODFaserWaveform
                    PRIVATE_LINK_LIBRARIES ${ROOT_LIBRARIES}
 		   )
 
diff --git a/Scintillator/ScintRecTools/ScintRecTools/IClockReconstructionTool.h b/Scintillator/ScintRecTools/ScintRecTools/IClockReconstructionTool.h
index 4ed97e93..46712a75 100644
--- a/Scintillator/ScintRecTools/ScintRecTools/IClockReconstructionTool.h
+++ b/Scintillator/ScintRecTools/ScintRecTools/IClockReconstructionTool.h
@@ -16,8 +16,9 @@
 #include "GaudiKernel/IAlgTool.h"
 #include "GaudiKernel/ToolHandle.h"
 
+#include "xAODFaserWaveform/WaveformClock.h"
+
 class ScintWaveform;
-class WaveformClock;
 
 ///Interface for Clock reco algorithms
 class IClockReconstructionTool : virtual public IAlgTool 
@@ -31,7 +32,7 @@ class IClockReconstructionTool : virtual public IAlgTool
 
   // Reconstruct all peaks in a raw waveform
   virtual StatusCode reconstruct(const ScintWaveform& wave, 
-				 WaveformClock* clockdata) const = 0;
+				 xAOD::WaveformClock* clockdata) const = 0;
 
 };
 
diff --git a/Scintillator/ScintRecTools/src/ClockReconstructionTool.cxx b/Scintillator/ScintRecTools/src/ClockReconstructionTool.cxx
index 9e9743d9..4c065eff 100644
--- a/Scintillator/ScintRecTools/src/ClockReconstructionTool.cxx
+++ b/Scintillator/ScintRecTools/src/ClockReconstructionTool.cxx
@@ -31,7 +31,7 @@ ClockReconstructionTool::initialize() {
 // Reconstruction step
 StatusCode
 ClockReconstructionTool::reconstruct(const ScintWaveform& raw_wave,
-				     WaveformClock* clockdata) const {
+				     xAOD::WaveformClock* clockdata) const {
 
   ATH_MSG_DEBUG("Clock reconstruct called ");
 
@@ -42,8 +42,8 @@ ClockReconstructionTool::reconstruct(const ScintWaveform& raw_wave,
   }
 
   // Set the trigger time
-  clockdata->setTriggerTime(raw_wave.trigger_time_tag());
-  ATH_MSG_DEBUG("Trigger time: " << raw_wave.trigger_time_tag());
+  //clockdata->setTriggerTime(raw_wave.trigger_time_tag());
+  //ATH_MSG_DEBUG("Trigger time: " << raw_wave.trigger_time_tag());
 
   // Digitized clock data, sampled at 500 MHz (2 ns)
   auto counts = raw_wave.adc_counts();
@@ -54,8 +54,8 @@ ClockReconstructionTool::reconstruct(const ScintWaveform& raw_wave,
   wave.assign(counts.begin(), counts.end());
 
   ATH_MSG_DEBUG("Created double array with length " << wave.size() );
-  ATH_MSG_DEBUG("First 20 elements:");
-  for (unsigned int i=0; i< 20; i++)
+  ATH_MSG_DEBUG("First 10 elements:");
+  for (unsigned int i=0; i< 10; i++)
     ATH_MSG_DEBUG(" " << i << " " << wave[i]);
 
   // delta_nu = 1/T where T is the total waveform length
@@ -93,12 +93,11 @@ ClockReconstructionTool::reconstruct(const ScintWaveform& raw_wave,
   }
 
   // Store results
-  double avg = magnitude[0];
-  double freq = imax * freqmult;
-  double amp = magnitude[imax];
-  double phase = atan2(im_full[imax], re_full[imax]);
-  clockdata->setParams(avg, freq, amp, phase); 
- 
+  clockdata->set_offset(magnitude[0]);
+  clockdata->set_frequency(imax * freqmult);
+  clockdata->set_amplitude(magnitude[imax]);
+  clockdata->set_phase(atan2(im_full[imax], re_full[imax]));
+
   ATH_MSG_DEBUG("Before correcting for finite resolution:");
   ATH_MSG_DEBUG(*clockdata);
 
@@ -115,19 +114,19 @@ ClockReconstructionTool::reconstruct(const ScintWaveform& raw_wave,
   ATH_MSG_DEBUG("Found shift in frequency index: " << dm);
 
   // Improved values
-  freq = (imax+dm) * freqmult;
-  phase = atan2(im_full[imax], re_full[imax]) - dm * M_PI;
+
+  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);
 
-  amp = 2*M_PI*dm*magnitude[imax] / sin(M_PI * dm);
-  clockdata->setParams(avg, freq, amp, phase); 
+  clockdata->set_frequency( (imax+dm) * freqmult );
+  clockdata->set_phase (phase);
+  clockdata->set_amplitude( 2*M_PI*dm*magnitude[imax] / sin(M_PI * dm) );
 
   ATH_MSG_DEBUG("After correcting for finite resolution:");
   ATH_MSG_DEBUG(*clockdata);
 
-
   delete fftr2c;
 
   return StatusCode::SUCCESS;
diff --git a/Scintillator/ScintRecTools/src/ClockReconstructionTool.h b/Scintillator/ScintRecTools/src/ClockReconstructionTool.h
index 3d04d5ef..c0f66752 100644
--- a/Scintillator/ScintRecTools/src/ClockReconstructionTool.h
+++ b/Scintillator/ScintRecTools/src/ClockReconstructionTool.h
@@ -14,7 +14,8 @@
 #include "ScintRecTools/IClockReconstructionTool.h"
 
 #include "ScintRawEvent/ScintWaveform.h"
-#include "ScintRecEvent/WaveformClock.h"
+//#include "ScintRecEvent/WaveformClock.h"
+#include "xAODFaserWaveform/WaveformClock.h"
 
 //Gaudi
 #include "GaudiKernel/ToolHandle.h"
@@ -35,7 +36,7 @@ class ClockReconstructionTool: public extends<AthAlgTool, IClockReconstructionTo
 
   /// Reconstruct hits from clock  
   virtual StatusCode reconstruct(const ScintWaveform& wave,
-				 WaveformClock* clockdata) const;
+				 xAOD::WaveformClock* clockdata) const;
 
  private:
 
diff --git a/xAOD/xAODFaserWaveform/Root/WaveformClock_v1.cxx b/xAOD/xAODFaserWaveform/Root/WaveformClock_v1.cxx
index 6a3128d0..b875b468 100644
--- a/xAOD/xAODFaserWaveform/Root/WaveformClock_v1.cxx
+++ b/xAOD/xAODFaserWaveform/Root/WaveformClock_v1.cxx
@@ -27,6 +27,9 @@ namespace xAOD {
 
   std::ostream& operator<<(std::ostream& s, const xAOD::WaveformClock_v1& clk) {
     s << "xAODWaveformClock: frequency=" << clk.frequency()
+      << " phase=" << clk.phase()
+      << " amplitude=" << clk.amplitude()
+      << " offset=" << clk.offset()
       << std::endl;
 
     return s;
diff --git a/xAOD/xAODFaserWaveform/xAODFaserWaveform/versions/WaveformClock_v1.h b/xAOD/xAODFaserWaveform/xAODFaserWaveform/versions/WaveformClock_v1.h
index 2d70b976..3fffe41c 100644
--- a/xAOD/xAODFaserWaveform/xAODFaserWaveform/versions/WaveformClock_v1.h
+++ b/xAOD/xAODFaserWaveform/xAODFaserWaveform/versions/WaveformClock_v1.h
@@ -46,7 +46,9 @@ namespace xAOD {
 
   }; // class WaveformClock_v1
 
-}
+  std::ostream& operator<<(std::ostream& s, const xAOD::WaveformClock_v1& clk);
+
+} // namespace xAOD
 
 // Declare the inheritance of the type:
 #include "xAODCore/BaseInfo.h"
diff --git a/xAOD/xAODFaserWaveform/xAODFaserWaveform/xAODFaserWaveformDict.h b/xAOD/xAODFaserWaveform/xAODFaserWaveform/xAODFaserWaveformDict.h
index 6ba4a1ab..5bb2eceb 100644
--- a/xAOD/xAODFaserWaveform/xAODFaserWaveform/xAODFaserWaveformDict.h
+++ b/xAOD/xAODFaserWaveform/xAODFaserWaveform/xAODFaserWaveformDict.h
@@ -27,7 +27,7 @@ namespace {
   struct GCCXML_DUMMY_INSTANTIATION_XAODFASERWAVEFORM {
     XAOD_INSTANTIATE_NS_CONTAINER_TYPES( xAOD, WaveformHitContainer_v1 );
     XAOD_INSTANTIATE_NS_OBJECT_TYPES( xAOD, WaveformClock_v1 );
-  }
+  };
 }
 
 #endif // XAODFASERWAVEFORM_XAODFASERWAVEFORMDICT_H
diff --git a/xAOD/xAODFaserWaveformAthenaPool/CMakeLists.txt b/xAOD/xAODFaserWaveformAthenaPool/CMakeLists.txt
new file mode 100644
index 00000000..3df3443a
--- /dev/null
+++ b/xAOD/xAODFaserWaveformAthenaPool/CMakeLists.txt
@@ -0,0 +1,14 @@
+# Copyright (C) 2020 CERN for the benefit of the FASER collaboration
+
+# Declare the package name.
+atlas_subdir( xAODFaserWaveformAthenaPool )
+
+# Component(s) in the package:
+atlas_add_poolcnv_library( xAODFaserWaveformAthenaPoolPoolCnv
+   src/*.h src/*.cxx
+   FILES xAODFaserWaveform/WaveformClock.h xAODFaserWaveform/WaveformClockAuxInfo.h
+   TYPES_WITH_NAMESPACE xAOD::WaveformClock xAOD::WaveformClockAuxInfo
+   CNV_PFX xAOD
+   LINK_LIBRARIES AthenaPoolCnvSvcLib AthenaPoolUtilities xAODFaserWaveform )
+
+
diff --git a/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.cxx b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.cxx
new file mode 100644
index 00000000..2ae94670
--- /dev/null
+++ b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.cxx
@@ -0,0 +1,5 @@
+/*
+  Copyright (C) 2020 CERN for the benefit of the FASER collaboration
+*/
+
+// Dummy source file so that cmake will know this is a custom converter.
diff --git a/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.h b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.h
new file mode 100644
index 00000000..5ac8b4c0
--- /dev/null
+++ b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockAuxInfoCnv.h
@@ -0,0 +1,15 @@
+// Dear emacs, this is -*- c++ -*-
+
+/*
+  Copyright (C) 2020 CERN for the benefit of the FASER collaboration
+*/
+
+#ifndef XAODFASERWAVEFORMATHENAPOOL_XAODWAVEFORMCLOCKAUXINFOCNV_H
+#define XAODFASERWAVEFORMATHENAPOOL_XAODWAVEFORMCLOCKAUXINFOCNV_H
+
+#include "xAODFaserWaveform/WaveformClockAuxInfo.h"
+#include "AthenaPoolCnvSvc/T_AthenaPoolAuxContainerCnv.h"
+
+typedef T_AthenaPoolAuxContainerCnv<xAOD::WaveformClockAuxInfo> xAODWaveformClockAuxInfoCnv;
+
+#endif // XAODFASERWAVEFORMATHENAPOOL_XAODFASERWAVEFORMCLOCKAUXINFOCNV_H
diff --git a/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.cxx b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.cxx
new file mode 100644
index 00000000..2ae94670
--- /dev/null
+++ b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.cxx
@@ -0,0 +1,5 @@
+/*
+  Copyright (C) 2020 CERN for the benefit of the FASER collaboration
+*/
+
+// Dummy source file so that cmake will know this is a custom converter.
diff --git a/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.h b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.h
new file mode 100644
index 00000000..2f591d16
--- /dev/null
+++ b/xAOD/xAODFaserWaveformAthenaPool/src/xAODWaveformClockCnv.h
@@ -0,0 +1,15 @@
+// Dear emacs, this is -*- c++ -*-
+
+/*
+  Copyright (C) 2020 CERN for the benefit of the FASER collaboration
+*/
+
+#ifndef XAODFASERWAVEFORMATHENAPOOL_XAODWAVEFORMCLOCKCNV_H
+#define XAODFASERWAVEFORMATHENAPOOL_XAODWAVEFORMCLOCKCNV_H
+
+#include "xAODFaserWaveform/WaveformClock.h"
+#include "AthenaPoolCnvSvc/T_AthenaPoolxAODCnv.h"
+
+typedef T_AthenaPoolxAODCnv<xAOD::WaveformClock> xAODWaveformClockCnv;
+
+#endif // XAODFASERWAVEFORMATHENAPOOL_XAODWAVEFORMCLOCKCNV_H
-- 
GitLab