From 8e76fa37646516dbf7a74bb906d3f377773d087c Mon Sep 17 00:00:00 2001
From: scott snyder <snyder@bnl.gov>
Date: Mon, 14 Oct 2024 15:19:31 +0200
Subject: [PATCH] DataModelTestDataWrite: Update to support tests reading and
 writing to RNTuple.

Make ViewVector tests optional.
---
 .../src/xAODTestWrite.cxx                     | 107 ++++--------------
 .../src/xAODTestWrite.h                       |  30 ++---
 .../src/xAODTestWriteHVec.cxx                 |  46 ++------
 .../src/xAODTestWriteHVec.h                   |  25 ++--
 4 files changed, 49 insertions(+), 159 deletions(-)

diff --git a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.cxx b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.cxx
index 94436bbf1e5..c33d14ea949 100644
--- a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.cxx
+++ b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.cxx
@@ -1,8 +1,6 @@
 /*
-  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
 */
-
-// $Id$
 /**
  * @file DataModelTestDataWrite/src/xAODTestWrite.cxx
  * @author snyder@bnl.gov
@@ -21,43 +19,14 @@
 #include "DataModelTestDataWrite/GVec.h"
 #include "DataModelTestDataWrite/G.h"
 #include "DataModelTestDataWrite/GAuxContainer.h"
-//#include "DataModelTestDataWrite/H.h"
-//#include "DataModelTestDataWrite/HVec.h"
-//#include "DataModelTestDataWrite/HView.h"
-//#include "DataModelTestDataWrite/HAuxContainer.h"
 #include "AthContainersInterfaces/AuxDataOption.h"
 #include "AthLinks/ElementLink.h"
 #include "AthenaKernel/errorcheck.h"
 
 
-#define CHECK_OPTION(ret)                       \
-  do {                                          \
-    if (!ret) {                                 \
-      ATH_MSG_ERROR("setOption failed");        \
-      return StatusCode::FAILURE;               \
-    }                                           \
-  } while(0)
-
-
 namespace DMTest {
 
 
-/**
- * @brief Constructor.
- * @param name The algorithm name.
- * @param svc The service locator.
- */
-xAODTestWrite::xAODTestWrite (const std::string &name,
-                              ISvcLocator *pSvcLocator)
-  : AthReentrantAlgorithm (name, pSvcLocator)
-{
-  declareProperty ("CVecKey", m_cvecKey = "cvec");
-  declareProperty ("CTrigKey", m_ctrigKey = "ctrig");
-  declareProperty ("GVecKey", m_gvecKey = "gvec");
-  declareProperty ("CVecWDKey", m_cvecWDKey = "cvecWD");
-}
-  
-
 /**
  * @brief Algorithm initialization; called at the beginning of the job.
  */
@@ -65,7 +34,7 @@ StatusCode xAODTestWrite::initialize()
 {
   ATH_CHECK( m_cvecKey.initialize() );
   ATH_CHECK( m_ctrigKey.initialize() );
-  ATH_CHECK( m_gvecKey.initialize() );
+  ATH_CHECK( m_gvecKey.initialize (SG::AllowEmpty) );
   ATH_CHECK( m_cvecWDKey.initialize() );
   return StatusCode::SUCCESS;
 }
@@ -104,24 +73,26 @@ StatusCode xAODTestWrite::execute (const EventContext& ctx) const
   SG::WriteHandle<DMTest::CVec> ctrig (m_ctrigKey, ctx);
   CHECK( ctrig.record (std::move (trig_coll), std::move (trig_store)) );
 
-  auto gcont = std::make_unique<DMTest::GVec>();
-  auto gstore = std::make_unique<DMTest::GAuxContainer>();
-  gcont->setStore (gstore.get());
-
-  for (int i=0; i < 10; i++) {
-    gcont->push_back (new DMTest::G);
-    G& g = *gcont->back();
-    g.setAnInt (count * 700 + i+1);
-    g.setgFloat (count * 700 + 100 + i+0.5);
-    std::vector<double> v;
-    for (int j=0; j<i; j++) {
-      v.push_back (count * 700 + 100*j + i+0.5);
+  if (!m_gvecKey.empty()) {
+    auto gcont = std::make_unique<DMTest::GVec>();
+    auto gstore = std::make_unique<DMTest::GAuxContainer>();
+    gcont->setStore (gstore.get());
+
+    for (int i=0; i < 10; i++) {
+      gcont->push_back (new DMTest::G);
+      G& g = *gcont->back();
+      g.setAnInt (count * 700 + i+1);
+      g.setgFloat (count * 700 + 100 + i+0.5);
+      std::vector<double> v;
+      for (int j=0; j<i; j++) {
+        v.push_back (count * 700 + 100*j + i+0.5);
+      }
+      g.setgvFloat (v);
     }
-    g.setgvFloat (v);
-  }
 
-  SG::WriteHandle<DMTest::GVec> gvec (m_gvecKey, ctx);
-  CHECK( gvec.record (std::move (gcont), std::move (gstore)) );
+    SG::WriteHandle<DMTest::GVec> gvec (m_gvecKey, ctx);
+    CHECK( gvec.record (std::move (gcont), std::move (gstore)) );
+  }
 
   CHECK( write_cvec_with_data (count, ctx) );
 
@@ -153,43 +124,5 @@ StatusCode xAODTestWrite::write_cvec_with_data (unsigned int count,
 }
 
 
-#if 0
-/**
- * @brief Test schema evolution involving view container.
- */
-StatusCode xAODTestWrite::write_htest()
-{
-  auto hvec = std::make_unique<DMTest::HVec>();
-  auto store = std::make_unique<DMTest::HAuxContainer>();
-  hvec->setStore (store.get());
-  auto hview = std::make_unique<DMTest::HView>();
-
-  for (int i = 0; i < 20; i++) {
-    hvec->push_back (new DMTest::H);
-    hvec->back()->setAnInt (i+1 + count * 400);
-  }
-
-  for (int i = 0; i < 20; i++) {
-    hview->push_back (hvec->at (19-i));
-  }
-
-  ATH_CHECK( SG::makeHandle(m_hvecKey).record (std::move(hvec),
-                                               std::move(store)) );
-  CHECK( DMTest::recordView2 (&*evtStore(), std::move(hview), "hview") );
-
-  return StatusCode::SUCCESS;
-}
-#endif
-
-
-/**
- * @brief Algorithm finalization; called at the end of the job.
- */
-StatusCode xAODTestWrite::finalize()
-{
-  return StatusCode::SUCCESS;
-}
-
-
 } // namespace DMTest
 
diff --git a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.h b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.h
index 4bcfe9e2650..298fe1ad926 100644
--- a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.h
+++ b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWrite.h
@@ -1,10 +1,7 @@
 // This file's extension implies that it's C, but it's really -*- C++ -*-.
-
 /*
-  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
 */
-
-// $Id$
 /**
  * @file src/xAODTestWrite.h
  * @author scott snyder <snyder@bnl.gov>
@@ -35,12 +32,7 @@ class xAODTestWrite
   : public AthReentrantAlgorithm
 {
 public:
-  /**
-   * @brief Constructor.
-   * @param name The algorithm name.
-   * @param svc The service locator.
-   */
-  xAODTestWrite (const std::string &name, ISvcLocator *pSvcLocator);
+  using AthReentrantAlgorithm::AthReentrantAlgorithm;
   
 
   /**
@@ -55,21 +47,19 @@ public:
   virtual StatusCode execute (const EventContext& ctx) const override;
 
 
-  /**
-   * @brief Algorithm finalization; called at the end of the job.
-   */
-  virtual StatusCode finalize() override;
-
-
 private:
   /// Test writing container with additional data.
   StatusCode write_cvec_with_data (unsigned int count,
                                    const EventContext& ctx) const;
 
-  SG::ReadHandleKey<DMTest::CVec> m_cvecKey;
-  SG::WriteHandleKey<DMTest::CVec> m_ctrigKey;
-  SG::WriteHandleKey<DMTest::GVec> m_gvecKey;
-  SG::WriteHandleKey<DMTest::CVecWithData> m_cvecWDKey;
+  SG::ReadHandleKey<DMTest::CVec> m_cvecKey
+    { this, "CVecKey", "cvec", "" };
+  SG::WriteHandleKey<DMTest::CVec> m_ctrigKey
+    { this, "CTrigKey", "ctrig", "" };
+  SG::WriteHandleKey<DMTest::GVec> m_gvecKey
+    { this, "GVecKey", "gvec", "" };
+  SG::WriteHandleKey<DMTest::CVecWithData> m_cvecWDKey
+    { this, "CVecWDKey", "cvecWD", "" };
 };
 
 
diff --git a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.cxx b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.cxx
index 00332d7e2f5..5421e95d143 100644
--- a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.cxx
+++ b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.cxx
@@ -1,8 +1,6 @@
 /*
-  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
 */
-
-// $Id$
 /**
  * @file DataModelTestDataWrite/src/xAODTestWriteHVec.cxx
  * @author snyder@bnl.gov
@@ -22,29 +20,13 @@
 namespace DMTest {
 
 
-/**
- * @brief Constructor.
- * @param name The algorithm name.
- * @param svc The service locator.
- */
-xAODTestWriteHVec::xAODTestWriteHVec (const std::string &name,
-                                      ISvcLocator *pSvcLocator)
-  : AthReentrantAlgorithm (name, pSvcLocator),
-    m_hvecKey ("hvec"),
-    m_hviewKey ("hview")
-{
-  declareProperty ("HVecKey", m_hvecKey);
-  declareProperty ("HViewKey", m_hviewKey);
-}
-  
-
 /**
  * @brief Algorithm initialization; called at the beginning of the job.
  */
 StatusCode xAODTestWriteHVec::initialize()
 {
   ATH_CHECK( m_hvecKey.initialize() );
-  ATH_CHECK( m_hviewKey.initialize() );
+  ATH_CHECK( m_hviewKey.initialize( SG::AllowEmpty ) );
   return StatusCode::SUCCESS;
 }
 
@@ -59,32 +41,28 @@ StatusCode xAODTestWriteHVec::execute (const EventContext& ctx) const
   auto hvec = std::make_unique<DMTest::HVec>();
   auto store = std::make_unique<DMTest::HAuxContainer>();
   hvec->setStore (store.get());
-  auto hview = std::make_unique<DMTest::HView>();
 
   for (int i = 0; i < 20; i++) {
     hvec->push_back (new DMTest::H);
     hvec->back()->setAnInt (i+1 + count * 400);
   }
 
-  for (int i = 0; i < 20; i++) {
-    hview->push_back (hvec->at (19-i));
+  std::unique_ptr<DMTest::HView> hview;
+  if (!m_hviewKey.empty()) {
+    hview = std::make_unique<DMTest::HView>();
+    for (int i = 0; i < 20; i++) {
+      hview->push_back (hvec->at (19-i));
+    }
   }
 
   ATH_CHECK( SG::makeHandle(m_hvecKey, ctx).record (std::move(hvec),
                                                     std::move(store)) );
 
-  SG::WriteHandle<DMTest::HView> hviewH (m_hviewKey, ctx);
-  ATH_CHECK( DMTest::recordView2 (hviewH, std::move(hview)) );
-
-  return StatusCode::SUCCESS;
-}
-
+  if (!m_hviewKey.empty()) {
+    SG::WriteHandle<DMTest::HView> hviewH (m_hviewKey, ctx);
+    ATH_CHECK( DMTest::recordView2 (hviewH, std::move(hview)) );
+  }
 
-/**
- * @brief Algorithm finalization; called at the end of the job.
- */
-StatusCode xAODTestWriteHVec::finalize()
-{
   return StatusCode::SUCCESS;
 }
 
diff --git a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.h b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.h
index b3af6f9733a..cd9a46a2207 100644
--- a/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.h
+++ b/Control/DataModelTest/DataModelTestDataWrite/src/xAODTestWriteHVec.h
@@ -1,10 +1,8 @@
 // This file's extension implies that it's C, but it's really -*- C++ -*-.
 
 /*
-  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
 */
-
-// $Id$
 /**
  * @file DataModelTestDataWrite/src/xAODTestWriteHVec.h
  * @author scott snyder <snyder@bnl.gov>
@@ -34,13 +32,8 @@ class xAODTestWriteHVec
   : public AthReentrantAlgorithm
 {
 public:
-  /**
-   * @brief Constructor.
-   * @param name The algorithm name.
-   * @param svc The service locator.
-   */
-  xAODTestWriteHVec (const std::string &name, ISvcLocator *pSvcLocator);
-  
+  using AthReentrantAlgorithm::AthReentrantAlgorithm;
+
 
   /**
    * @brief Algorithm initialization; called at the beginning of the job.
@@ -54,15 +47,11 @@ public:
   virtual StatusCode execute (const EventContext& ctx) const override;
 
 
-  /**
-   * @brief Algorithm finalization; called at the end of the job.
-   */
-  virtual StatusCode finalize() override;
-
-
 private:
-  SG::WriteHandleKey<DMTest::HVec> m_hvecKey;
-  SG::WriteHandleKey<DMTest::HView> m_hviewKey;
+  SG::WriteHandleKey<DMTest::HVec> m_hvecKey
+  { this, "HVecKey", "hvec", "" };
+  SG::WriteHandleKey<DMTest::HView> m_hviewKey
+  { this, "HViewKey", "hview", "" };
 };
 
 
-- 
GitLab