File indexing completed on 2025-01-18 10:17:42
0001
0002 #include <catch.hpp>
0003
0004 #include <type_traits>
0005 #include <PodioDatamodel/ExampleClusterCollection.h>
0006 #include <JANA/JEvent.h>
0007 #include <JANA/JFactoryGenerator.h>
0008
0009 namespace podiotests {
0010
0011
0012 TEST_CASE("PodioTestsInsertAndRetrieve") {
0013 ExampleClusterCollection clusters_to_insert;
0014 clusters_to_insert.push_back(MutableExampleCluster(16.0));
0015 clusters_to_insert.push_back(MutableExampleCluster(128.0));
0016
0017 auto event = std::make_shared<JEvent>();
0018 event->InsertCollection<ExampleCluster>(std::move(clusters_to_insert), "clusters");
0019
0020 SECTION("Retrieve using JEvent::GetCollection()") {
0021 auto* collection_retrieved = event->GetCollection<ExampleCluster>("clusters");
0022 REQUIRE(collection_retrieved->size() == 2);
0023 REQUIRE((*collection_retrieved)[0].energy() == 16.0);
0024 }
0025
0026 SECTION("Retrieve using JEvent::GetCollectionBase()") {
0027 auto* collection_retrieved_untyped = event->GetCollectionBase("clusters");
0028 REQUIRE(collection_retrieved_untyped->size() == 2);
0029 auto* collection_retrieved = dynamic_cast<const ExampleClusterCollection*>(collection_retrieved_untyped);
0030 REQUIRE(collection_retrieved != nullptr);
0031 REQUIRE((*collection_retrieved)[0].energy() == 16.0);
0032 }
0033
0034 SECTION("Retrieve using JEvent::Get()") {
0035 std::vector<const ExampleCluster*> clusters_retrieved = event->Get<ExampleCluster>("clusters");
0036 REQUIRE(clusters_retrieved.size() == 2);
0037 REQUIRE(clusters_retrieved[0]->energy() == 16.0);
0038 }
0039
0040 SECTION("Retrieve directly from podio::Frame") {
0041 auto frame = event->GetSingle<podio::Frame>();
0042 auto* collection_retrieved = dynamic_cast<const ExampleClusterCollection*>(frame->get("clusters"));
0043 REQUIRE(collection_retrieved->size() == 2);
0044 REQUIRE((*collection_retrieved)[0].energy() == 16.0);
0045 }
0046
0047 }
0048
0049 template <typename T, typename = void>
0050 struct MyWrapper {
0051 bool have_podio() {
0052 return false;
0053 }
0054 };
0055
0056 template <typename T>
0057 struct MyWrapper<T, std::void_t<typename T::collection_type>> {
0058 int x = 2;
0059 bool have_podio() {
0060 return true;
0061 }
0062 };
0063
0064 TEST_CASE("SFINAE for JFactoryT || JFactoryPodioT") {
0065
0066 MyWrapper<int> w;
0067 REQUIRE(w.have_podio() == false);
0068
0069 MyWrapper<ExampleCluster> ww;
0070 REQUIRE(ww.have_podio() == true);
0071
0072 ww.x = 22;
0073
0074 }
0075
0076 template <typename, typename=void>
0077 struct is_podio : std::false_type {};
0078
0079 template <typename T>
0080 struct is_podio<T, std::void_t<typename T::collection_type>> : std::true_type {};
0081
0082 template <typename T>
0083 static constexpr bool is_podio_v = is_podio<T>::value;
0084
0085 struct FakeMultifactory {
0086
0087 template <typename T, typename std::enable_if_t<is_podio_v<T>>* = nullptr>
0088 bool DeclareOutput(std::string ) {
0089 return true;
0090 }
0091
0092 template <typename T, typename std::enable_if_t<!is_podio_v<T>>* = nullptr>
0093 bool DeclareOutput(std::string ) {
0094 return false;
0095 }
0096 };
0097
0098 TEST_CASE("SFINAE for JMultifactory::SetData") {
0099 FakeMultifactory sut;
0100 REQUIRE(sut.DeclareOutput<int>("asdf") == false);
0101 REQUIRE(sut.DeclareOutput<ExampleCluster>("asdf") == true);
0102 }
0103
0104
0105 TEST_CASE("PODIO 'subset' collections handled correctly (not involving factories yet") {
0106 auto event = std::make_shared<JEvent>();
0107
0108 auto a1 = MutableExampleCluster(22.2);
0109 auto a2 = MutableExampleCluster(4.0);
0110 ExampleClusterCollection clusters_to_insert;
0111 clusters_to_insert.push_back(a1);
0112 clusters_to_insert.push_back(a2);
0113 event->InsertCollection<ExampleCluster>(std::move(clusters_to_insert), "original_clusters");
0114
0115 auto* retrieved_clusters = event->GetCollection<ExampleCluster>("original_clusters");
0116 auto b = (*retrieved_clusters)[1];
0117 REQUIRE(b.energy() == 4.0);
0118
0119 ExampleClusterCollection subset_clusters;
0120 subset_clusters.setSubsetCollection(true);
0121 subset_clusters.push_back(b);
0122 event->InsertCollection<ExampleCluster>(std::move(subset_clusters), "subset_clusters");
0123
0124
0125
0126 const ExampleClusterCollection* retrieved_subset_clusters = event->GetCollection<ExampleCluster>("subset_clusters");
0127 const ExampleCluster& c = (*retrieved_subset_clusters)[0];
0128 REQUIRE(c.energy() == 4.0);
0129 REQUIRE(c.id() == b.id());
0130
0131 }
0132
0133 namespace jana2_tests_podiotests_init {
0134
0135 struct TestFac : public JFactoryPodioT<ExampleCluster> {
0136 TestFac() {
0137 SetTag("clusters");
0138 }
0139 bool init_called = false;
0140 void Init() override {
0141 init_called = true;
0142 }
0143 void Process(const std::shared_ptr<const JEvent>&) override {
0144 ExampleClusterCollection c;
0145 c.push_back(MutableExampleCluster(16.0));
0146 SetCollection(std::move(c));
0147 }
0148 };
0149 }
0150
0151 TEST_CASE("JFactoryPodioT::Init gets called") {
0152
0153 JApplication app;
0154 app.Add(new JFactoryGeneratorT<jana2_tests_podiotests_init::TestFac>());
0155 auto event = std::make_shared<JEvent>(&app);
0156 event->Clear();
0157
0158 auto r = event->GetCollectionBase("clusters");
0159 REQUIRE(r != nullptr);
0160 const auto* res = dynamic_cast<const ExampleClusterCollection*>(r);
0161 REQUIRE(res != nullptr);
0162 REQUIRE((*res)[0].energy() == 16.0);
0163 auto fac = dynamic_cast<jana2_tests_podiotests_init::TestFac*>(event->GetFactory<ExampleCluster>("clusters"));
0164 REQUIRE(fac != nullptr);
0165 REQUIRE(fac->init_called == true);
0166 }
0167 }
0168