File indexing completed on 2026-11-30 10:44:32
0001
0002 #include <catch.hpp>
0003 #include <chrono>
0004 #include <thread>
0005 #include <JANA/JApplication.h>
0006 #include <JANA/JEventSource.h>
0007 #include <JANA/JEventUnfolder.h>
0008 #include <JANA/JEventProcessor.h>
0009 #include <JANA/Engine/JExecutionEngine.h>
0010
0011 namespace jana::integration_tests::timeslice_unfolder {
0012
0013 struct Hit {
0014 int ts_nr;
0015 int hit_nr;
0016 };
0017
0018
0019 class MyTimesliceSource : public JEventSource {
0020 private:
0021 Output<Hit> m_hits_out {this};
0022 int m_total_timeslices;
0023 int m_hits_per_timeslice;
0024 int m_ts_emitted = 0;
0025
0026 public:
0027 MyTimesliceSource(int total_timeslices, int hits_per_timeslice)
0028 : m_total_timeslices(total_timeslices), m_hits_per_timeslice(hits_per_timeslice)
0029 {
0030 SetCallbackStyle(CallbackStyle::ExpertMode);
0031 SetLevel(JEventLevel::Timeslice);
0032 }
0033
0034 Result Emit(JEvent& event) override {
0035 if (m_ts_emitted >= m_total_timeslices) {
0036 return Result::FailureFinished;
0037 }
0038 event.SetEventNumber(m_ts_emitted);
0039 for (int i = 0; i < m_hits_per_timeslice; ++i) {
0040 m_hits_out().push_back(new Hit{m_ts_emitted, i});
0041 }
0042 m_ts_emitted++;
0043 std::this_thread::sleep_for(std::chrono::milliseconds(10));
0044 return Result::Success;
0045 }
0046 };
0047
0048
0049 class MyTimesliceUnfolder : public JEventUnfolder {
0050 private:
0051 Input<Hit> m_hits_in {this};
0052 Output<Hit> m_hit_out {this};
0053 int m_hits_per_timeslice;
0054
0055 public:
0056 explicit MyTimesliceUnfolder(int hits_per_timeslice)
0057 : m_hits_per_timeslice(hits_per_timeslice)
0058 {
0059 SetParentLevel(JEventLevel::Timeslice);
0060 SetChildLevel(JEventLevel::PhysicsEvent);
0061 }
0062
0063 Result Unfold(const JEvent& parent, JEvent& child, int child_idx) override {
0064 std::this_thread::sleep_for(std::chrono::milliseconds(5));
0065 child.SetEventNumber(parent.GetEventNumber() * m_hits_per_timeslice + child_idx);
0066 const Hit* src = m_hits_in->at(child_idx);
0067 m_hit_out().push_back(new Hit{src->ts_nr, src->hit_nr});
0068 if (child_idx == m_hits_per_timeslice - 1) {
0069 return Result::NextChildNextParent;
0070 }
0071 return Result::NextChildKeepParent;
0072 }
0073 };
0074
0075
0076 class MyPhysicsEventProcessor : public JEventProcessor {
0077 private:
0078 Input<Hit> m_hit_in {this};
0079 int m_hits_per_timeslice;
0080
0081 public:
0082 explicit MyPhysicsEventProcessor(int hits_per_timeslice)
0083 : m_hits_per_timeslice(hits_per_timeslice)
0084 {
0085 SetCallbackStyle(CallbackStyle::ExpertMode);
0086 }
0087
0088 void ProcessSequential(const JEvent& event) override {
0089 int evt_nr = (int) event.GetEventNumber();
0090 int ts_nr = (int) event.GetParentNumber(JEventLevel::Timeslice);
0091 int expected_ts_nr = evt_nr / m_hits_per_timeslice;
0092 int expected_hit_nr = evt_nr % m_hits_per_timeslice;
0093 REQUIRE(ts_nr == expected_ts_nr);
0094 REQUIRE(m_hit_in->size() == 1);
0095 REQUIRE(m_hit_in->at(0)->ts_nr == expected_ts_nr);
0096 REQUIRE(m_hit_in->at(0)->hit_nr == expected_hit_nr);
0097 std::this_thread::sleep_for(std::chrono::milliseconds(10));
0098 }
0099 };
0100
0101
0102 TEST_CASE("TimesliceUnfolder") {
0103 const int n_timeslices = 5;
0104 int n_hits_per_timeslice;
0105
0106 SECTION("FourEventsPerTimeslice") {
0107 n_hits_per_timeslice = 4;
0108 }
0109 SECTION("ThreeEventsPerTimeslice") {
0110 n_hits_per_timeslice = 3;
0111 }
0112
0113 JApplication app;
0114 app.SetParameterValue("jana:loglevel", "info");
0115 app.Add(new MyTimesliceSource(n_timeslices, n_hits_per_timeslice));
0116 app.Add(new MyTimesliceUnfolder(n_hits_per_timeslice));
0117 app.Add(new MyPhysicsEventProcessor(n_hits_per_timeslice));
0118 app.Run();
0119
0120 auto ee = app.GetService<JExecutionEngine>();
0121 auto perf = ee->GetPerf();
0122
0123 REQUIRE(perf.runstatus == JExecutionEngine::RunStatus::Finished);
0124
0125
0126 REQUIRE(perf.throughputs_hz.count(JEventLevel::Timeslice) == 1);
0127 REQUIRE(perf.throughputs_hz.count(JEventLevel::PhysicsEvent) == 1);
0128 REQUIRE(perf.throughputs_hz.at(JEventLevel::Timeslice) > 0);
0129 REQUIRE(perf.throughputs_hz.at(JEventLevel::PhysicsEvent) > 0);
0130
0131
0132
0133 REQUIRE(perf.throughputs_hz.at(JEventLevel::PhysicsEvent) ==
0134 Approx(perf.throughputs_hz.at(JEventLevel::Timeslice) * n_hits_per_timeslice));
0135 }
0136
0137 }