From a9e39969032b4b9aa434f347de7564779744db88 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 8 Aug 2025 13:00:45 -0400 Subject: [PATCH 1/4] Create test cases for each bug --- src/programs/unit_tests/CMakeLists.txt | 1 + .../Components/JOmniFactoryTests.cc | 150 ++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/programs/unit_tests/Components/JOmniFactoryTests.cc diff --git a/src/programs/unit_tests/CMakeLists.txt b/src/programs/unit_tests/CMakeLists.txt index ae0743085657181143d8b0f6a2888f2becc09924..39ca7906e2a4e6b14c04a7d89f7261c5345d4095 100644 --- a/src/programs/unit_tests/CMakeLists.txt +++ b/src/programs/unit_tests/CMakeLists.txt @@ -23,6 +23,7 @@ set(TEST_SOURCES Components/JFactoryGeneratorTests.cc Components/JHasInputsTests.cc Components/JMultiFactoryTests.cc + Components/JOmniFactoryTests.cc Components/JServiceTests.cc Components/UnfoldTests.cc Components/UserExceptionTests.cc diff --git a/src/programs/unit_tests/Components/JOmniFactoryTests.cc b/src/programs/unit_tests/Components/JOmniFactoryTests.cc new file mode 100644 index 0000000000000000000000000000000000000000..c96d0b51ad00441df1879b620b2d35a41048651c --- /dev/null +++ b/src/programs/unit_tests/Components/JOmniFactoryTests.cc @@ -0,0 +1,150 @@ + +#include +#include +#include +#include +#include + +struct MyHit {int e; }; + +#if JANA2_HAVE_PODIO + +#include + +struct MyFac : public JOmniFactory { + Input hits_in {this}; + VariadicPodioInput variadic_podio_hits_in {this}; + PodioInput podio_hits_in {this}; + + Output hits_out {this}; + VariadicPodioOutput variadic_podio_hits_out {this}; + PodioOutput podio_hits_out {this}; + + MyFac() { + hits_out.SetNotOwnerFlag(true); + } + void Configure() { } + void ChangeRun(int32_t /*run_nr*/) { } + void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) { + + REQUIRE(hits_out().size() == 0); + REQUIRE(podio_hits_out()->size() == 0); + REQUIRE(variadic_podio_hits_out().size() == 2); + REQUIRE(variadic_podio_hits_out().at(0)->size() == 0); + REQUIRE(variadic_podio_hits_out().at(1)->size() == 0); + + for (auto hit : *hits_in) { + hits_out().push_back(const_cast(hit)); + } + + podio_hits_out()->setSubsetCollection(); + for (auto hit : *podio_hits_in) { + podio_hits_out()->push_back(hit); + variadic_podio_hits_out().at(0)->push_back(hit); + } + + variadic_podio_hits_out().at(1)->push_back(ExampleHit(22, 1.1, 1.1, 1.1, 10, 0)); + } +}; + +void test_single_event(JEvent& event) { + event.Insert(new MyHit{99}, "lw"); + ExampleHitCollection coll; + coll.push_back(MutableExampleHit{14,0.0,0.0,0.0,100,0}); + coll.push_back(MutableExampleHit{21,0.0,0.0,0.0,100,0}); + event.InsertCollection(std::move(coll), "podio"); + + ExampleHitCollection coll2; + coll2.push_back(MutableExampleHit{30,0.0,0.0,0.0,100,0}); + event.InsertCollection(std::move(coll2), "v_podio_0"); + + ExampleHitCollection coll3; + coll3.push_back(MutableExampleHit{10101,0.0,0.0,0.0,100,0}); + event.InsertCollection(std::move(coll3), "v_podio_1"); + + auto hits = event.Get("lw2"); + REQUIRE(hits.size() == 1); + REQUIRE(hits.at(0)->e == 99); + + auto podio_hits = event.GetCollection("podio2"); + REQUIRE(podio_hits->size() == 2); + REQUIRE(podio_hits->at(0).cellID() == 14); + REQUIRE(podio_hits->at(1).cellID() == 21); + + auto podio_hits_v2 = event.GetCollection("v_podio_2"); + REQUIRE(podio_hits_v2 ->size() == 2); + REQUIRE(podio_hits_v2->at(0).cellID() == 14); + REQUIRE(podio_hits_v2->at(1).cellID() == 21); + + auto podio_hits_v3 = event.GetCollection("v_podio_3"); + REQUIRE(podio_hits_v3 ->size() == 1); + REQUIRE(podio_hits_v3->at(0).cellID() == 22); +} + +TEST_CASE("JOmniFactoryTests_VariadicWiring") { + JApplication app; + app.Add(new JOmniFactoryGeneratorT( + "sut", + {"lw", "v_podio_0", "v_podio_1", "podio"}, + {"lw2", "v_podio_2", "v_podio_3", "podio2"})); + + auto event = std::make_shared(&app); + test_single_event(*event); + event->Clear(); + test_single_event(*event); +} + +#endif + + +struct MyFac2 : public JOmniFactory { + Output hits_out {this}; + + void Configure() { } + void ChangeRun(int32_t /*run_nr*/) { } + void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) { + REQUIRE(hits_out().size() == 0); + hits_out().push_back(new MyHit{22}); + } +}; + + +TEST_CASE("JOmniFactoryTests_OutputsCleared") { + JApplication app; + app.Add(new JOmniFactoryGeneratorT( + "sut", + {}, + {"huegelgrab"})); + + auto event = std::make_shared(&app); + REQUIRE(event->GetSingleStrict("huegelgrab")->e == 22); + + event->Clear(); + REQUIRE(event->GetSingleStrict("huegelgrab")->e == 22); +} + +struct MyFac3 : public JOmniFactory { + Input hits_in {this}; + Output hits_out {this}; + + void Configure() { } + void ChangeRun(int32_t /*run_nr*/) { } + void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) { + REQUIRE(hits_in().at(0)->e == 123); + hits_out().push_back(new MyHit{1234}); + } +}; + + +TEST_CASE("JOmniFactoryTests_LightweightInputTag") { + JApplication app; + app.Add(new JOmniFactoryGeneratorT( + "sut", + {"huegelgrab"}, + {"schlafen"})); + + auto event = std::make_shared(&app); + event->Insert(new MyHit{123}, "huegelgrab"); + REQUIRE(event->GetSingleStrict("schlafen")->e == 1234); + +} From f18b66c6004f636a6c9108c6c9b630cc71ac4c6a Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 8 Aug 2025 11:19:27 -0400 Subject: [PATCH 2/4] Bugfix: Input loses databundle name set via JOmniFactoryGenerator --- src/libraries/JANA/Components/JHasInputs.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libraries/JANA/Components/JHasInputs.h b/src/libraries/JANA/Components/JHasInputs.h index d0ccd21f1b7afa55cee91c3c296b0cdca3c07f52..23e10274281964aa0dbfca2bb7a3bea9b1506f70 100644 --- a/src/libraries/JANA/Components/JHasInputs.h +++ b/src/libraries/JANA/Components/JHasInputs.h @@ -146,14 +146,12 @@ struct JHasInputs { class Input : public InputBase { std::vector m_data; - std::string m_tag; public: Input(JHasInputs* owner) { owner->RegisterInput(this); m_type_name = JTypeInfo::demangle(); - m_databundle_name = m_type_name; m_level = JEventLevel::None; } @@ -164,8 +162,7 @@ struct JHasInputs { } void SetTag(std::string tag) { - m_tag = tag; - m_databundle_name = m_type_name + ":" + tag; + m_databundle_name = tag; } const std::vector& operator()() { return m_data; } @@ -180,23 +177,23 @@ struct JHasInputs { auto& level = m_level; m_data.clear(); if (level == event.GetLevel() || level == JEventLevel::None) { - event.Get(m_data, m_tag, !m_is_optional); + event.Get(m_data, m_databundle_name, !m_is_optional); } else { if (m_is_optional && !event.HasParent(level)) return; - event.GetParent(level).template Get(m_data, m_tag, !m_is_optional); + event.GetParent(level).template Get(m_data, m_databundle_name, !m_is_optional); } } void PrefetchCollection(const JEvent& event) { if (m_level == event.GetLevel() || m_level == JEventLevel::None) { - auto fac = event.GetFactory(m_tag, !m_is_optional); + auto fac = event.GetFactory(m_databundle_name, !m_is_optional); if (fac != nullptr) { fac->Create(event); } } else { if (m_is_optional && !event.HasParent(m_level)) return; - auto fac = event.GetParent(m_level).template GetFactory(m_tag, !m_is_optional); + auto fac = event.GetParent(m_level).template GetFactory(m_databundle_name, !m_is_optional); if (fac != nullptr) { fac->Create(event); } From 05693e9bcda45793ed992bd5926b29e86777e20f Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 8 Aug 2025 11:21:12 -0400 Subject: [PATCH 3/4] Bugfix: Ouput not clearing the output before Process() --- src/libraries/JANA/Components/JHasOutputs.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/JANA/Components/JHasOutputs.h b/src/libraries/JANA/Components/JHasOutputs.h index c52ca247d022e60c96e2cddc8d28dfd3c62d4918..d3f1f13f83d91ef4596f0aa918af18fb44f54d25 100644 --- a/src/libraries/JANA/Components/JHasOutputs.h +++ b/src/libraries/JANA/Components/JHasOutputs.h @@ -72,7 +72,9 @@ struct JHasOutputs { auto fac = event.Insert(m_data, this->collection_names[0]); fac->SetNotOwnerFlag(is_not_owner); } - void Reset() override { } + void Reset() override { + m_data.clear(); + } }; @@ -174,7 +176,7 @@ struct JHasOutputs { void Reset() override { m_data.clear(); - for (auto& coll_name : this->collection_names) { + for (size_t i=0; i()); } } From 03fc16eea670884e01760c6a93e54bcfcc38f88c Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 8 Aug 2025 11:25:30 -0400 Subject: [PATCH 4/4] Reestablish compatibility with EICrecon's JOmniFactory --- src/libraries/JANA/Components/JHasInputs.h | 49 +++++++++++++++++++ src/libraries/JANA/Components/JHasOutputs.h | 43 +++++++++++++--- .../Components/JOmniFactoryTests.cc | 4 +- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/libraries/JANA/Components/JHasInputs.h b/src/libraries/JANA/Components/JHasInputs.h index 23e10274281964aa0dbfca2bb7a3bea9b1506f70..92bcb3680d2345545d2cb3893f26fc0346de419e 100644 --- a/src/libraries/JANA/Components/JHasInputs.h +++ b/src/libraries/JANA/Components/JHasInputs.h @@ -20,13 +20,16 @@ struct JHasInputs { std::vector m_inputs; std::vector m_variadic_inputs; + std::vector> m_ordered_inputs; void RegisterInput(InputBase* input) { m_inputs.push_back(input); + m_ordered_inputs.push_back({input, nullptr}); } void RegisterInput(VariadicInputBase* input) { m_variadic_inputs.push_back(input); + m_ordered_inputs.push_back({nullptr, input}); } struct InputOptions { @@ -464,6 +467,11 @@ struct JHasInputs { const std::vector& variadic_input_levels, const std::vector>& variadic_input_databundle_names) { + if (m_variadic_inputs.size() == 1 && variadic_input_databundle_names.size() == 0) { + WireInputsCompatibility(component_level, single_input_levels, single_input_databundle_names); + return; + } + // Validate that we have the correct number of input databundle names if (single_input_databundle_names.size() != m_inputs.size()) { throw JException("Wrong number of (nonvariadic) input databundle names! Expected %d, found %d", m_inputs.size(), single_input_databundle_names.size()); @@ -498,6 +506,47 @@ struct JHasInputs { } } + void WireInputsCompatibility(JEventLevel component_level, + const std::vector& single_input_levels, + const std::vector& single_input_databundle_names) { + + // Figure out how many collection names belong to the variadic input + int variadic_databundle_count = single_input_databundle_names.size() - m_inputs.size(); + int databundle_name_index = 0; + int databundle_level_index = 0; + + for (auto& pair : m_ordered_inputs) { + auto* single_input = pair.first; + auto* variadic_input = pair.second; + if (single_input != nullptr) { + single_input->SetDatabundleName(single_input_databundle_names.at(databundle_name_index)); + if (single_input_levels.empty()) { + single_input->SetLevel(component_level); + } + else { + single_input->SetLevel(single_input_levels.at(databundle_level_index)); + } + databundle_name_index += 1; + databundle_level_index += 1; + } + else { + std::vector variadic_databundle_names; + for (int i=0; iSetRequestedDatabundleNames(variadic_databundle_names); + if (single_input_levels.empty()) { + variadic_input->SetLevel(component_level); + } + else { + variadic_input->SetLevel(single_input_levels.at(databundle_level_index)); // Last one wins! + } + databundle_name_index += variadic_databundle_count; + databundle_level_index += 1; + } + } + } + void SummarizeInputs(JComponentSummary::Component& summary) const { for (const auto* input : m_inputs) { summary.AddInput(new JComponentSummary::Collection("", input->GetDatabundleName(), input->GetTypeName(), input->GetLevel())); diff --git a/src/libraries/JANA/Components/JHasOutputs.h b/src/libraries/JANA/Components/JHasOutputs.h index d3f1f13f83d91ef4596f0aa918af18fb44f54d25..9f129ca65d922b6e41a26d44bcb0e814428929ef 100644 --- a/src/libraries/JANA/Components/JHasOutputs.h +++ b/src/libraries/JANA/Components/JHasOutputs.h @@ -61,7 +61,7 @@ struct JHasOutputs { protected: void CreateHelperFactory(JMultifactory& fac) override { - fac.DeclareOutput(this->collection_names[0]); + fac.DeclareOutput(this->collection_names[0], !is_not_owner); } void SetCollection(JMultifactory& fac) override { @@ -188,14 +188,45 @@ struct JHasOutputs { size_t single_output_index = 0; size_t variadic_output_index = 0; + size_t variadic_output_count = 0; for (auto* output : m_outputs) { - output->collection_names.clear(); - output->level = component_level; if (output->is_variadic) { - output->collection_names = variadic_output_databundle_names.at(variadic_output_index++); + variadic_output_count += 1; } - else { - output->collection_names.push_back(single_output_databundle_names.at(single_output_index++)); + } + if (variadic_output_count == 1 && variadic_output_databundle_names.size() == 0) { + // Obtain variadic databundle names from excess single-output databundle names + int variadic_databundle_count = single_output_databundle_names.size() - m_outputs.size() + 1; + int current_databundle_index = 0; + + for (auto* output : m_outputs) { + output->collection_names.clear(); + output->level = component_level; + if (output->is_variadic) { + std::vector variadic_names; + for (int i=0; icollection_names = variadic_names; + current_databundle_index += variadic_databundle_count; + } + else { + output->collection_names.push_back(single_output_databundle_names.at(current_databundle_index)); + current_databundle_index += 1; + } + } + } + else { + // Do the obvious, sensible thing instead + for (auto* output : m_outputs) { + output->collection_names.clear(); + output->level = component_level; + if (output->is_variadic) { + output->collection_names = variadic_output_databundle_names.at(variadic_output_index++); + } + else { + output->collection_names.push_back(single_output_databundle_names.at(single_output_index++)); + } } } } diff --git a/src/programs/unit_tests/Components/JOmniFactoryTests.cc b/src/programs/unit_tests/Components/JOmniFactoryTests.cc index c96d0b51ad00441df1879b620b2d35a41048651c..45031acae3d3965784472286278a1e5858dc55fa 100644 --- a/src/programs/unit_tests/Components/JOmniFactoryTests.cc +++ b/src/programs/unit_tests/Components/JOmniFactoryTests.cc @@ -38,12 +38,14 @@ struct MyFac : public JOmniFactory { } podio_hits_out()->setSubsetCollection(); + variadic_podio_hits_out().at(0)->setSubsetCollection(); + for (auto hit : *podio_hits_in) { podio_hits_out()->push_back(hit); variadic_podio_hits_out().at(0)->push_back(hit); } - variadic_podio_hits_out().at(1)->push_back(ExampleHit(22, 1.1, 1.1, 1.1, 10, 0)); + variadic_podio_hits_out().at(1)->push_back(MutableExampleHit(22, 1.1, 1.1, 1.1, 10, 0)); } };