Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /jana2/src/programs/unit_tests/Components/JDatabundleTests.cc was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 
0002 #include <catch.hpp>
0003 #include <JANA/Components/JDatabundle.h>
0004 #include <JANA/JApplicationFwd.h>
0005 #include <JANA/JFactoryT.h>
0006 #include <JANA/JFactoryGenerator.h>
0007 #include <JANA/JEvent.h>
0008 
0009 #if JANA2_HAVE_PODIO
0010 #include <PodioDatamodel/ExampleHitCollection.h>
0011 #include <JANA/Components/JPodioOutput.h>
0012 #endif
0013 
0014 namespace jana::databundletests::jfactoryt {
0015 
0016 struct Hit { double E; };
0017 
0018 class MyFacT : public JFactoryT<Hit> {
0019     void Process(const std::shared_ptr<const JEvent>& event) {
0020         mData.push_back(new Hit{21.2 + event->GetEventNumber()});
0021     }
0022 };
0023 
0024 TEST_CASE("JDatabundle_JFactoryT") {
0025 
0026     JApplication app;
0027     app.Add(new JFactoryGeneratorT<MyFacT>);
0028     auto event = std::make_shared<JEvent>(&app);
0029     event->SetEventNumber(1);
0030 
0031     SECTION("OldMechanism") {
0032         auto data = event->Get<Hit>();
0033         REQUIRE(data.size() == 1);
0034         REQUIRE(data.at(0)->E == 22.2);
0035     }
0036     SECTION("NewMechanism") {
0037 
0038         auto databundle = event->GetFactorySet()->GetDatabundle("jana::databundletests::jfactoryt::Hit");
0039 
0040         REQUIRE(databundle != nullptr); // Databundle should ALWAYS exist whether factory runs or not
0041         REQUIRE(databundle->GetStatus() == JDatabundle::Status::Empty);
0042         REQUIRE(databundle->GetFactory() != nullptr);
0043 
0044         databundle->GetFactory()->Create(*event);
0045 
0046         REQUIRE(databundle->GetStatus() == JDatabundle::Status::Created);
0047         REQUIRE(databundle->GetSize() == 1);
0048 
0049         auto typed_databundle = dynamic_cast<JLightweightDatabundleT<Hit>*>(databundle);
0050 
0051         REQUIRE(typed_databundle != nullptr);
0052         REQUIRE(typed_databundle->GetData().size() == 1);
0053         REQUIRE(typed_databundle->GetData().at(0)->E == 22.2);
0054     }
0055 }
0056 
0057 #if JANA2_HAVE_PODIO
0058 
0059 class MyFac : public JFactory {
0060 
0061     components::PodioOutput<ExampleHit> m_hits_out {this};
0062 
0063     void Process(const JEvent& event) {
0064         auto hit = MutableExampleHit();
0065         hit.time(event.GetEventNumber() + 11);
0066         m_hits_out->push_back(std::move(hit));
0067     }
0068 };
0069 
0070 void CheckPodioCollection(JEvent& event, std::string unique_name, size_t expected_value, JDatabundle::Status status = JDatabundle::Status::Created) {
0071 
0072     // Trigger creation via event->GetCollection
0073     auto coll = event.GetCollection<ExampleHit>(unique_name);
0074     REQUIRE(coll->size() == 1);
0075     REQUIRE(coll->at(0).time() == expected_value);
0076 
0077     // Check databundle contents directly as well
0078     auto databundle = event.GetFactorySet()->GetDatabundle(unique_name);
0079     REQUIRE(databundle->GetStatus() == status);
0080     REQUIRE(databundle->GetSize() == 1);
0081 
0082     auto typed_databundle = dynamic_cast<JPodioDatabundle*>(databundle);
0083 
0084     REQUIRE(typed_databundle != nullptr);
0085     REQUIRE(typed_databundle->GetCollection()->size() == 1);
0086     auto typed_collection = dynamic_cast<const ExampleHitCollection*>(typed_databundle->GetCollection());
0087     REQUIRE(typed_collection->at(0).time() == expected_value);
0088 }
0089 
0090 
0091 TEST_CASE("JDatabundle_JFactory") {
0092     JApplication app;
0093     app.Add(new JFactoryGeneratorT<MyFac>);
0094     auto event = std::make_shared<JEvent>(&app);
0095 
0096     auto databundle = event->GetFactorySet()->GetDatabundle("ExampleHit");
0097     REQUIRE(databundle != nullptr); // Databundle should ALWAYS exist whether factory runs or not
0098     REQUIRE(databundle->GetStatus() == JDatabundle::Status::Empty);
0099     REQUIRE(databundle->GetFactory() != nullptr);
0100 
0101     event->SetEventNumber(1);
0102     CheckPodioCollection(*event, "ExampleHit", 12);
0103 
0104     event->Clear();
0105     event->SetEventNumber(7);
0106     CheckPodioCollection(*event, "ExampleHit", 18);
0107 
0108     event->Clear();
0109     event->SetEventNumber(11);
0110     CheckPodioCollection(*event, "ExampleHit", 22);
0111 }
0112 
0113 
0114 void InsertOneHit(JEvent& event, std::string unique_name, size_t value) {
0115     ExampleHitCollection collection;
0116     auto hit = collection.create();
0117     hit.time(value);
0118     event.InsertCollection<ExampleHit>(std::move(collection), unique_name);
0119 }
0120 
0121 TEST_CASE("JDatabundle_InsertCollection") {
0122 
0123     JApplication app;
0124     auto event = std::make_shared<JEvent>(&app);
0125 
0126     auto databundle = event->GetFactorySet()->GetDatabundle("myhits");
0127     REQUIRE(databundle == nullptr); 
0128     // This databundle won't exist because there's no factory and nothing has been Inserted
0129 
0130     event->Clear();
0131     InsertOneHit(*event, "myhits", 8);
0132     CheckPodioCollection(*event, "myhits", 8, JDatabundle::Status::Inserted);
0133 
0134     event->Clear();
0135     InsertOneHit(*event, "myhits", 22);
0136     CheckPodioCollection(*event, "myhits", 22, JDatabundle::Status::Inserted);
0137 }
0138 
0139 
0140 #endif
0141 
0142 } // namespace jana::databundletests::jfactoryt
0143 
0144 struct InterestingHit {};
0145 
0146 TEST_CASE("JDatabundle_SetUniqueName"){
0147 
0148     JLightweightDatabundleT<InterestingHit> sut;
0149 
0150     // Default setup
0151     REQUIRE(sut.GetTypeName() == "InterestingHit");
0152     REQUIRE(sut.GetUniqueName() == "InterestingHit");
0153     REQUIRE(sut.HasShortName() == true);
0154     REQUIRE(sut.GetShortName() == "");
0155 
0156     // Set a unique name with NO short name
0157     sut.SetUniqueName("Scintillation");
0158 
0159     REQUIRE(sut.GetTypeName() == "InterestingHit");
0160     REQUIRE(sut.GetUniqueName() == "Scintillation");
0161     REQUIRE(sut.HasShortName() == false);
0162     REQUIRE(sut.GetShortName() == "");
0163 
0164     // Set a unique name with an implied nonempty short name
0165     sut.SetUniqueName("InterestingHit:Fascination");
0166 
0167     REQUIRE(sut.GetTypeName() == "InterestingHit");
0168     REQUIRE(sut.GetUniqueName() == "InterestingHit:Fascination");
0169     REQUIRE(sut.HasShortName() == true);
0170     REQUIRE(sut.GetShortName() == "Fascination");
0171 
0172     // Set a unique name with NO short name
0173     sut.SetUniqueName("InterestingHit");
0174 
0175     REQUIRE(sut.GetTypeName() == "InterestingHit");
0176     REQUIRE(sut.GetUniqueName() == "InterestingHit");
0177     REQUIRE(sut.HasShortName() == true);
0178     REQUIRE(sut.GetShortName() == "");
0179 }
0180 
0181