File indexing completed on 2025-05-12 09:14:03
0001 #include <catch.hpp>
0002 #include <JANA/JApplication.h>
0003 #include <JANA/JObject.h>
0004 #include <JANA/JEventSource.h>
0005 #include <JANA/JEventProcessor.h>
0006
0007 #if JANA2_HAVE_PODIO
0008 #include <PodioDatamodel/ExampleHitCollection.h>
0009 #endif
0010
0011 namespace jana2::components::hasinputstests {
0012
0013 struct TestHit : public JObject {
0014 int cell_row;
0015 int cell_col;
0016 double energy;
0017
0018 TestHit(int cell_row, int cell_col, double energy) : cell_row(cell_row), cell_col(cell_col), energy(energy) {}
0019 };
0020
0021 struct TestEventSource : public JEventSource {
0022
0023 TestEventSource() {
0024 SetCallbackStyle(CallbackStyle::ExpertMode);
0025 }
0026
0027 Result Emit(JEvent& event) override {
0028
0029 std::vector<TestHit*> det_a_hits;
0030 det_a_hits.push_back(new TestHit(5, 4, 100.5));
0031 det_a_hits.push_back(new TestHit(5, 5, 99.8));
0032 det_a_hits.push_back(new TestHit(6, 4, 70.1));
0033 event.Insert(det_a_hits, "detector_a_hits");
0034
0035 std::vector<TestHit*> det_b_hits;
0036 det_b_hits.push_back(new TestHit(1, 0, 50.5));
0037 det_b_hits.push_back(new TestHit(2, 0, 65.1));
0038 det_b_hits.push_back(new TestHit(3, 0, 22.8));
0039 event.Insert(det_b_hits, "detector_b_hits");
0040
0041 #if JANA2_HAVE_PODIO
0042 ExampleHitCollection det_c_hits;
0043 det_c_hits.push_back(MutableExampleHit(14, 0.0, 1.1, 2.2, 52.2, 0));
0044 event.InsertCollection<ExampleHit>(std::move(det_c_hits), "detector_c_hits");
0045
0046 ExampleHitCollection det_d_hits;
0047 det_d_hits.push_back(MutableExampleHit(0, 0.0, 0.0, 0.0, 1000.1, 0));
0048 det_d_hits.push_back(MutableExampleHit(0, 0.0, 0.0, 0.0, 500.5, 1));
0049 det_d_hits.push_back(MutableExampleHit(0, 0.0, 0.0, 0.0, 300.1, 2));
0050 event.InsertCollection<ExampleHit>(std::move(det_d_hits), "detector_d_hits");
0051
0052 ExampleHitCollection det_e_hits;
0053 det_e_hits.push_back(MutableExampleHit(4, 10.0, 10.0, 10.0, 77.0, 3));
0054 event.InsertCollection<ExampleHit>(std::move(det_e_hits), "detector_e_hits");
0055 #endif
0056
0057 return Result::Success;
0058 }
0059 };
0060
0061 struct TestProc : public JEventProcessor {
0062
0063 Input<TestHit> m_det_a_hits_in {this};
0064
0065
0066
0067 #if JANA2_HAVE_PODIO
0068 PodioInput<ExampleHit> m_det_c_hits_in {this};
0069 VariadicPodioInput<ExampleHit> m_det_de_hits_in {this};
0070
0071
0072 PodioInput<ExampleHit> m_det_f_hits_in {this};
0073 VariadicPodioInput<ExampleHit> m_det_def_hits_in {this};
0074 #endif
0075
0076 TestProc() {
0077 SetCallbackStyle(CallbackStyle::ExpertMode);
0078 m_det_a_hits_in.SetTag("detector_a_hits");
0079
0080 #if JANA2_HAVE_PODIO
0081 m_det_c_hits_in.SetCollectionName("detector_c_hits");
0082 m_det_de_hits_in.SetCollectionNames({"detector_d_hits", "detector_e_hits"});
0083
0084 m_det_f_hits_in.SetCollectionName("detector_f_hits");
0085 m_det_f_hits_in.SetOptional(true);
0086
0087 m_det_def_hits_in.SetCollectionNames({"detector_d_hits", "detector_e_hits", "detector_f_hits"});
0088 m_det_def_hits_in.SetOptional(true);
0089 #endif
0090 }
0091
0092 void Process(const JEvent&) override {
0093 REQUIRE(m_det_a_hits_in->size() == 3);
0094 REQUIRE(m_det_a_hits_in->at(2)->cell_col == 4);
0095
0096 #if JANA2_HAVE_PODIO
0097 REQUIRE(m_det_c_hits_in->size() == 1);
0098 REQUIRE(m_det_c_hits_in->at(0).energy() == 52.2);
0099
0100 REQUIRE(m_det_de_hits_in().at(0)->size() == 3);
0101 REQUIRE(m_det_de_hits_in().at(0)->at(0).energy() == 1000.1);
0102
0103 REQUIRE(m_det_de_hits_in().at(1)->size() == 1);
0104 REQUIRE(m_det_de_hits_in().at(1)->at(0).energy() == 77.0);
0105
0106 REQUIRE(m_det_f_hits_in() == nullptr);
0107
0108 REQUIRE(m_det_def_hits_in().at(1)->at(0).energy() == 77.0);
0109 REQUIRE(m_det_def_hits_in().at(2) == nullptr);
0110 #endif
0111 }
0112 };
0113
0114 TEST_CASE("JHasInputs_BasicTests") {
0115 JApplication app;
0116 app.Add(new TestEventSource);
0117 app.Add(new TestProc);
0118 app.SetParameterValue("jana:nevents", 1);
0119 app.Run();
0120 };
0121
0122 }
0123