Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:42

0001 
0002 #include <catch.hpp>
0003 #include <JANA/Topology/JUnfoldArrow.h>
0004 #include <JANA/Topology/JFoldArrow.h>
0005 
0006 namespace jana {
0007 namespace unfoldtests {
0008 
0009 
0010 struct TestUnfolder : public JEventUnfolder {
0011     mutable std::vector<int> preprocessed_event_nrs;
0012     mutable std::vector<JEventLevel> preprocessed_event_levels;
0013     std::vector<int> unfolded_parent_nrs;
0014     std::vector<JEventLevel> unfolded_parent_levels;
0015     std::vector<int> unfolded_child_nrs;
0016     std::vector<JEventLevel> unfolded_child_levels;
0017 
0018     TestUnfolder() {
0019         SetParentLevel(JEventLevel::Timeslice);
0020         SetChildLevel(JEventLevel::PhysicsEvent);
0021     }
0022 
0023     void Preprocess(const JEvent& parent) const override {
0024         LOG << "Preprocessing " << parent.GetLevel() << " event " << parent.GetEventNumber() << LOG_END;
0025         preprocessed_event_nrs.push_back(parent.GetEventNumber());
0026         preprocessed_event_levels.push_back(parent.GetLevel());
0027     }
0028 
0029     Result Unfold(const JEvent& parent, JEvent& child, int iter) override {
0030         auto child_nr = iter + 100 + parent.GetEventNumber();
0031         unfolded_parent_nrs.push_back(parent.GetEventNumber());
0032         unfolded_parent_levels.push_back(parent.GetLevel());
0033         unfolded_child_nrs.push_back(child_nr);
0034         unfolded_child_levels.push_back(child.GetLevel());
0035         child.SetEventNumber(child_nr);
0036         LOG << "Unfolding " << parent.GetLevel() << " event " << parent.GetEventNumber() << " into " << child.GetLevel() << " " << child_nr << "; iter=" << iter << LOG_END;
0037         return (iter == 2 ? Result::NextChildNextParent : Result::NextChildKeepParent);
0038     }
0039 };
0040 
0041 TEST_CASE("UnfoldTests_Basic") {
0042 
0043     JApplication app;
0044     app.Initialize();
0045     auto jcm = app.GetService<JComponentManager>();
0046 
0047     JEventPool parent_pool {jcm, 5, 1, JEventLevel::Timeslice};
0048     JEventPool child_pool {jcm, 5, 1, JEventLevel::PhysicsEvent};
0049     JEventQueue parent_queue {3, 1};
0050     JEventQueue child_queue {3, 1};
0051 
0052     auto ts1 = parent_pool.Pop(0);
0053     ts1->SetEventNumber(17);
0054 
0055     auto ts2 = parent_pool.Pop(0);
0056     ts2->SetEventNumber(28);
0057 
0058     parent_queue.Push(ts1, 0);
0059     parent_queue.Push(ts2, 0);
0060 
0061     TestUnfolder unfolder;
0062     JUnfoldArrow arrow("sut", &unfolder);
0063     arrow.attach(&parent_queue, JUnfoldArrow::PARENT_IN);
0064     arrow.attach(&child_pool, JUnfoldArrow::CHILD_IN);
0065     arrow.attach(&child_queue, JUnfoldArrow::CHILD_OUT);
0066 
0067     arrow.initialize();
0068     arrow.execute( 0); // First call to execute() picks up the parent and exits early
0069     auto result = arrow.execute( 0); // Second call to execute() picks up the child, calls Unfold(), and emits the newly parented child
0070     REQUIRE(result == JArrow::FireResult::KeepGoing);
0071     REQUIRE(child_queue.GetSize(0) == 1);
0072     REQUIRE(unfolder.preprocessed_event_nrs.size() == 0);
0073     REQUIRE(unfolder.unfolded_parent_nrs.size() == 1);
0074     REQUIRE(unfolder.unfolded_parent_nrs[0] == 17);
0075     REQUIRE(unfolder.unfolded_parent_levels[0] == JEventLevel::Timeslice);
0076     REQUIRE(unfolder.unfolded_child_nrs.size() == 1);
0077     REQUIRE(unfolder.unfolded_child_nrs[0] == 117);
0078     REQUIRE(unfolder.unfolded_child_levels[0] == JEventLevel::PhysicsEvent);
0079 
0080 }
0081 
0082 TEST_CASE("FoldArrowTests") {
0083 
0084     JApplication app;
0085     app.Initialize();
0086     auto jcm = app.GetService<JComponentManager>();
0087 
0088     // We only use these to obtain preconfigured JEvents
0089     JEventPool parent_pool {jcm, 5, 1, JEventLevel::Timeslice};
0090     JEventPool child_pool {jcm, 5, 1, JEventLevel::PhysicsEvent};
0091 
0092     // We set up our test cases by putting events on these queues
0093     JEventQueue child_in(5, 1);
0094     JEventQueue child_out(5, 1);
0095     JEventQueue parent_out(5, 1);
0096 
0097     JFoldArrow arrow("sut", JEventLevel::Timeslice, JEventLevel::PhysicsEvent);
0098     arrow.attach(&child_in, JFoldArrow::CHILD_IN);
0099     arrow.attach(&child_out, JFoldArrow::CHILD_OUT);
0100     arrow.attach(&parent_out, JFoldArrow::PARENT_OUT);
0101     arrow.initialize();
0102 
0103     SECTION("One-to-one relationship between timeslices and events") {
0104 
0105         auto ts1 = parent_pool.Pop(0);
0106         ts1->SetEventNumber(17);
0107         REQUIRE(ts1->GetLevel() == JEventLevel::Timeslice);
0108 
0109         auto ts2 = parent_pool.Pop(0);
0110         ts2->SetEventNumber(28);
0111 
0112         auto evt1 = child_pool.Pop(0);
0113         evt1->SetEventNumber(111);
0114 
0115         auto evt2 = child_pool.Pop(0);
0116         evt2->SetEventNumber(112);
0117 
0118 
0119         evt1->SetParent(ts1);
0120         ts1->Release(); // One-to-one
0121         child_in.Push(evt1, 0);
0122 
0123         evt2->SetParent(ts2);
0124         ts2->Release(); // One-to-one
0125         child_in.Push(evt2, 0);
0126     
0127         arrow.execute(0);
0128 
0129         REQUIRE(child_in.GetSize(0) == 1);
0130         REQUIRE(child_out.GetSize(0) == 1);
0131         REQUIRE(parent_out.GetSize(0) == 1);
0132 
0133     }
0134 
0135 
0136     SECTION("One-to-two relationship between timeslices and events") {
0137 
0138         auto ts1 = parent_pool.Pop(0);
0139         ts1->SetEventNumber(17);
0140         REQUIRE(ts1->GetLevel() == JEventLevel::Timeslice);
0141 
0142         auto ts2 = parent_pool.Pop(0);
0143         ts2->SetEventNumber(28);
0144 
0145         auto evt1 = child_pool.Pop(0);
0146         evt1->SetEventNumber(111);
0147 
0148         auto evt2 = child_pool.Pop(0);
0149         evt2->SetEventNumber(112);
0150 
0151         auto evt3 = child_pool.Pop(0);
0152         evt3->SetEventNumber(113);
0153 
0154         auto evt4 = child_pool.Pop(0);
0155         evt4->SetEventNumber(114);
0156 
0157 
0158         evt1->SetParent(ts1);
0159         evt2->SetParent(ts1);
0160         ts1->Release(); // One-to-two
0161         
0162         evt3->SetParent(ts2);
0163         evt4->SetParent(ts2);
0164         ts2->Release(); // One-to-two
0165    
0166         child_in.Push(evt1, 0);
0167         child_in.Push(evt2, 0);
0168         child_in.Push(evt3, 0);
0169         child_in.Push(evt4, 0);
0170 
0171         arrow.execute(0);
0172 
0173         REQUIRE(child_in.GetSize(0) == 3);
0174         REQUIRE(child_out.GetSize(0) == 1);
0175         REQUIRE(parent_out.GetSize(0) == 0);
0176 
0177         arrow.execute(0);
0178 
0179         REQUIRE(child_in.GetSize(0) == 2);
0180         REQUIRE(child_out.GetSize(0) == 2);
0181         REQUIRE(parent_out.GetSize(0) == 1);
0182 
0183         arrow.execute(0);
0184 
0185         REQUIRE(child_in.GetSize(0) == 1);
0186         REQUIRE(child_out.GetSize(0) == 3);
0187         REQUIRE(parent_out.GetSize(0) == 1);
0188 
0189         arrow.execute(0);
0190 
0191         REQUIRE(child_in.GetSize(0) == 0);
0192         REQUIRE(child_out.GetSize(0) == 4);
0193         REQUIRE(parent_out.GetSize(0) == 2);
0194     }
0195 
0196 
0197 }
0198 
0199     
0200 } // namespace arrowtests
0201 } // namespace jana
0202 
0203 
0204 
0205