From 2463f2699e58195cb86a2d2d3503632dfa05aa27 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 20 Sep 2024 15:54:20 -0400 Subject: [PATCH] Clear PodioStorage correctly --- src/libraries/JANA/Components/JPodioStorage.h | 4 ++ src/libraries/JANA/Components/JStorage.h | 1 + src/libraries/JANA/JFactory.h | 1 + .../unit_tests/Components/JStorageTests.cc | 4 +- .../unit_tests/Components/PodioTests.cc | 43 +++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/libraries/JANA/Components/JPodioStorage.h b/src/libraries/JANA/Components/JPodioStorage.h index 90a29ca86..ec5097c1f 100644 --- a/src/libraries/JANA/Components/JPodioStorage.h +++ b/src/libraries/JANA/Components/JPodioStorage.h @@ -18,12 +18,16 @@ class JPodioStorage : public JStorage { public: size_t GetSize() const override { + if (m_collection == nullptr) { + return 0; + } return m_collection->size(); } virtual void ClearData() override { m_collection = nullptr; m_frame = nullptr; + SetStatus(JStorage::Status::Empty); // Podio clears the data itself when the frame is destroyed. // Until then, the collection is immutable. // diff --git a/src/libraries/JANA/Components/JStorage.h b/src/libraries/JANA/Components/JStorage.h index 1b812ddc0..2d76574db 100644 --- a/src/libraries/JANA/Components/JStorage.h +++ b/src/libraries/JANA/Components/JStorage.h @@ -12,6 +12,7 @@ #include #include #include +#include class JFactory; diff --git a/src/libraries/JANA/JFactory.h b/src/libraries/JANA/JFactory.h index 7db60f2a2..d9fb08912 100644 --- a/src/libraries/JANA/JFactory.h +++ b/src/libraries/JANA/JFactory.h @@ -57,6 +57,7 @@ class JFactory : public jana::components::JComponent, : mObjectName(std::move(aName)), mTag(std::move(aTag)), mStatus(Status::Uninitialized) { + SetTypeName(mObjectName); SetPrefix(aTag.empty() ? mObjectName : mObjectName + ":" + mTag); }; diff --git a/src/programs/unit_tests/Components/JStorageTests.cc b/src/programs/unit_tests/Components/JStorageTests.cc index 178e5fed2..8a0cec486 100644 --- a/src/programs/unit_tests/Components/JStorageTests.cc +++ b/src/programs/unit_tests/Components/JStorageTests.cc @@ -44,7 +44,7 @@ struct TestFactory : public JFactory { } void Init() { } - void Process(const std::shared_ptr& event) { + void Process(const std::shared_ptr&) { LOG_WARN(GetLogger()) << "Calling TestFactory::Process" << LOG_END; m_clusters()->push_back(MutableExampleCluster(22.2)); m_clusters()->push_back(MutableExampleCluster(27)); @@ -61,7 +61,7 @@ struct RegeneratingTestFactory : public JFactory { } void Init() { } - void Process(const std::shared_ptr& event) { + void Process(const std::shared_ptr&) { LOG_WARN(GetLogger()) << "Calling TestFactory::Process" << LOG_END; m_clusters()->push_back(MutableExampleCluster(22.2)); m_clusters()->push_back(MutableExampleCluster(27)); diff --git a/src/programs/unit_tests/Components/PodioTests.cc b/src/programs/unit_tests/Components/PodioTests.cc index 920d2842d..ef8e3c15b 100644 --- a/src/programs/unit_tests/Components/PodioTests.cc +++ b/src/programs/unit_tests/Components/PodioTests.cc @@ -204,6 +204,49 @@ TEST_CASE("Podio JMultifactory::Init gets called") { REQUIRE(multifac_typed != nullptr); REQUIRE(multifac_typed->init_called == true); } +TEST_CASE("PodioTests_InsertMultiple") { + + JApplication app; + auto event = std::make_shared(&app); + + // Insert a cluster + + auto coll1 = ExampleClusterCollection(); + auto cluster1 = coll1.create(22.0); + auto storage = event->InsertCollection(std::move(coll1), "clusters"); + + REQUIRE(storage->GetSize() == 1); + REQUIRE(storage->GetStatus() == JStorage::Status::Inserted); + + // Retrieve and validate cluster + + auto cluster1_retrieved = event->GetCollection("clusters"); + REQUIRE(cluster1_retrieved->at(0).energy() == 22.0); + + // Clear event + + event->GetFactorySet()->Release(); // Simulate a trip to the JEventPool + + // After clearing, the JStorage will still exist, but it will be empty + auto storage2 = event->GetStorage("clusters", false); + REQUIRE(storage2->GetStatus() == JStorage::Status::Empty); + REQUIRE(storage2->GetSize() == 0); + + // Insert a cluster. If event isn't being cleared correctly, this will throw + + auto coll2 = ExampleClusterCollection(); + auto cluster2 = coll2.create(33.0); + auto storage3 = event->InsertCollection(std::move(coll2), "clusters"); + REQUIRE(storage3->GetStatus() == JStorage::Status::Inserted); + REQUIRE(storage3->GetSize() == 1); + + // Retrieve and validate cluster + + auto cluster2_retrieved = event->GetCollection("clusters"); + REQUIRE(cluster2_retrieved->at(0).energy() == 33.0); +} + + } // namespace multifactory } // namespace podiotests