Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 08:57:00

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #include "catch.hpp"
0006 
0007 #include <JANA/JEventSource.h>
0008 
0009 struct EventData : public JObject {
0010     int event_nr=0;
0011     EventData(int event_nr) : event_nr(event_nr){}
0012 };
0013 
0014 struct NEventNSkipBoundedSource : public JEventSource {
0015 
0016     std::atomic_int event_count {0};
0017     int event_bound = 100;
0018     std::vector<int> events_emitted;
0019     std::atomic_int open_count{0};
0020     std::atomic_int close_count{0};
0021 
0022     NEventNSkipBoundedSource() {
0023         SetCallbackStyle(CallbackStyle::ExpertMode);
0024     }
0025 
0026     Result Emit(JEvent& event) override {
0027 
0028         REQUIRE(event.Get<EventData>("", false).size() == 0);
0029         if (event_count >= event_bound) {
0030             return Result::FailureFinished;
0031         }
0032         event_count += 1;
0033         event.Insert(new EventData {event_count});
0034         events_emitted.push_back(event_count);
0035         return Result::Success;
0036     }
0037 
0038     void Open() override {
0039         open_count++;
0040         LOG << "Opening source " << GetResourceName() << LOG_END;
0041     }
0042     void Close() override {
0043         close_count++;
0044         LOG << "Closing source " << GetResourceName() << LOG_END;
0045     }
0046 };
0047 
0048 
0049 TEST_CASE("NEventNSkipTests") {
0050 
0051     JApplication app;
0052     auto source = new NEventNSkipBoundedSource();
0053     app.Add(source);
0054 
0055     SECTION("[1..100] @ nskip=0, nevents=0 => [1..100]") {
0056 
0057         app.SetParameterValue("jana:nskip", 0);
0058         app.SetParameterValue("jana:nevents", 0);
0059         app.SetParameterValue("nthreads", 1);
0060         app.Run(true);
0061         REQUIRE(source->event_count == 100);
0062         REQUIRE(source->events_emitted.size() == 100);
0063         REQUIRE(source->events_emitted[0] == 1);
0064         REQUIRE(source->events_emitted[99] == 100);
0065     }
0066 
0067     SECTION("[1..100] @ nskip=0, nevents=22 => [1..22]") {
0068 
0069         app.SetParameterValue("jana:nskip", 0);
0070         app.SetParameterValue("jana:nevents", 22);
0071         app.SetParameterValue("nthreads", 1);
0072         app.Run(true);
0073         REQUIRE(source->event_count == 22);
0074         REQUIRE(source->events_emitted.size() == 22);
0075         REQUIRE(source->events_emitted[0] == 1);
0076         REQUIRE(source->events_emitted[21] == 22);
0077     }
0078 
0079     SECTION("[1..100] @ nskip=30, nevents=20 => [31..51]") {
0080 
0081         app.SetParameterValue("jana:nskip", 30);
0082         app.SetParameterValue("jana:nevents", 20);
0083         app.SetParameterValue("nthreads", 1);
0084         app.Run(true);
0085         REQUIRE(source->event_count == 50);
0086         // All 50 events get filled, but the first 30 are discarded without emitting
0087         // However, all 50 go into events_emitted
0088     }
0089 }
0090 
0091 TEST_CASE("JEventSourceArrow with multiple JEventSources") {
0092     JApplication app;
0093     auto source1 = new NEventNSkipBoundedSource();
0094     auto source2 = new NEventNSkipBoundedSource();
0095     auto source3 = new NEventNSkipBoundedSource();
0096     app.Add(source1);
0097     app.Add(source2);
0098     app.Add(source3);
0099 
0100     SECTION("All three event sources initialize, run, and finish") {
0101         source1->event_bound = 9;
0102         source2->event_bound = 13;
0103         source3->event_bound = 7;
0104 
0105         app.SetParameterValue("jana:nskip", 0);
0106         app.SetParameterValue("jana:nevents", 0);
0107         app.SetParameterValue("nthreads", 4);
0108         app.Run(true);
0109 
0110         REQUIRE(app.GetExitCode() == (int) JApplication::ExitCode::Success);
0111         REQUIRE(source1->GetStatus() == JEventSource::Status::Closed);
0112         REQUIRE(source2->GetStatus() == JEventSource::Status::Closed);
0113         REQUIRE(source3->GetStatus() == JEventSource::Status::Closed);
0114         REQUIRE(source1->open_count == 1);
0115         REQUIRE(source2->open_count == 1);
0116         REQUIRE(source3->open_count == 1);
0117         REQUIRE(source1->close_count == 1);
0118         REQUIRE(source2->close_count == 1);
0119         REQUIRE(source3->close_count == 1);
0120         REQUIRE(source1->GetEmittedEventCount() == 9);
0121         REQUIRE(source2->GetEmittedEventCount() == 13);
0122         REQUIRE(source3->GetEmittedEventCount() == 7);
0123         REQUIRE(app.GetNEventsProcessed() == 9+13+7);
0124     }
0125 
0126     SECTION("All three event sources initialize, run, and finish, each using the same nskip,nevents (self-terminated)") {
0127         source1->event_bound = 9;
0128         source2->event_bound = 13;
0129         source3->event_bound = 7;
0130 
0131         app.SetParameterValue("jana:nskip", 3);
0132         app.SetParameterValue("jana:nevents", 9);
0133         app.SetParameterValue("nthreads", 4);
0134         app.Run(true);
0135 
0136         REQUIRE(app.GetExitCode() == (int) JApplication::ExitCode::Success);
0137         REQUIRE(source1->GetStatus() == JEventSource::Status::Closed);
0138         REQUIRE(source2->GetStatus() == JEventSource::Status::Closed);
0139         REQUIRE(source3->GetStatus() == JEventSource::Status::Closed);
0140         REQUIRE(source1->open_count == 1);
0141         REQUIRE(source2->open_count == 1);
0142         REQUIRE(source3->open_count == 1);
0143         REQUIRE(source1->close_count == 1);
0144         REQUIRE(source2->close_count == 1);
0145         REQUIRE(source3->close_count == 1);
0146         REQUIRE(source1->GetEmittedEventCount() == 6);   // 3 dropped, 6 emitted
0147         REQUIRE(source2->GetEmittedEventCount() == 9);  // 3 dropped, 9 emitted
0148         REQUIRE(source3->GetEmittedEventCount() == 4);   // 3 dropped, 4 emitted
0149         REQUIRE(app.GetNEventsProcessed() == 19);
0150     }
0151 
0152 
0153     SECTION("All three event sources initialize, run, and finish, using individualized nskip,nevents (nevents-terminated)") {
0154         source1->event_bound = 9;
0155         source2->event_bound = 13;
0156         source3->event_bound = 7;
0157         source1->SetNSkip(2);
0158         source1->SetNEvents(4);
0159         source3->SetNEvents(4);
0160 
0161         app.SetParameterValue("nthreads", 4);
0162         app.Run(true);
0163 
0164         REQUIRE(app.GetExitCode() == (int) JApplication::ExitCode::Success);
0165         REQUIRE(source1->GetStatus() == JEventSource::Status::Closed);
0166         REQUIRE(source2->GetStatus() == JEventSource::Status::Closed);
0167         REQUIRE(source3->GetStatus() == JEventSource::Status::Closed);
0168         REQUIRE(source1->open_count == 1);
0169         REQUIRE(source2->open_count == 1);
0170         REQUIRE(source3->open_count == 1);
0171         REQUIRE(source1->close_count == 1);
0172         REQUIRE(source2->close_count == 1);
0173         REQUIRE(source3->close_count == 1);
0174         REQUIRE(source1->GetEmittedEventCount() == 4);   // 2 dropped, 4 emitted
0175         REQUIRE(source2->GetEmittedEventCount() == 13);  // 13 emitted
0176         REQUIRE(source3->GetEmittedEventCount() == 4);   // 4 emitted
0177         REQUIRE(app.GetNEventsProcessed() == 21);
0178     }
0179 
0180 }
0181