Back to home page

EIC code displayed by LXR

 
 

    


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

0001 
0002 #include "catch.hpp"
0003 
0004 #include <JANA/JEventProcessor.h>
0005 #include <JANA/JEventSource.h>
0006 
0007 namespace jana::jeventprocessortests {
0008 
0009 
0010 struct MyEventProcessor : public JEventProcessor {
0011     int init_count = 0;
0012     int process_count = 0;
0013     int finish_count = 0;
0014     int* destroy_count = nullptr;
0015 
0016     MyEventProcessor() {
0017         SetCallbackStyle(CallbackStyle::ExpertMode);
0018         SetTypeName(NAME_OF_THIS);
0019     }
0020     ~MyEventProcessor() {
0021         (*destroy_count)++;
0022     }
0023     void Init() override {
0024         REQUIRE(GetApplication() != nullptr);
0025         LOG_INFO(GetLogger()) << "Init() called" << LOG_END;
0026         init_count++;
0027     }
0028     void Process(const JEvent&) override {
0029         REQUIRE(GetApplication() != nullptr);
0030         process_count++;
0031         LOG_INFO(GetLogger()) << "Process() called" << LOG_END;
0032     }
0033     void Finish() override {
0034         REQUIRE(GetApplication() != nullptr);
0035         LOG_INFO(GetLogger()) << "Finish() called" << LOG_END;
0036         finish_count++;
0037     }
0038 };
0039 
0040 TEST_CASE("JEventProcessor_ExpertMode_ProcessCount") {
0041 
0042     LOG << "Running test: JEventProcessor_ExpertMode_ProcessCount" << LOG_END;
0043 
0044     auto sut = new MyEventProcessor;
0045     int destroy_count = 0;
0046     sut->destroy_count = &destroy_count;
0047 
0048     {
0049         JApplication app;
0050         app.SetParameterValue("jana:loglevel", "off");
0051         app.SetParameterValue("jana:nevents", 5);
0052 
0053         app.Add(new JEventSource);
0054         app.Add(sut);
0055         app.Run();
0056 
0057         REQUIRE(sut->init_count == 1);
0058         REQUIRE(sut->process_count == 5);
0059         REQUIRE(sut->GetEventCount() == 5);
0060         REQUIRE(sut->finish_count == 1);
0061         REQUIRE(destroy_count == 0);
0062 
0063         // App goes out of scope; sut should be destroyed
0064     }
0065     REQUIRE(destroy_count == 1);
0066 }
0067 
0068 struct MyExceptingProcessor : public JEventProcessor {
0069     void Process(const std::shared_ptr<const JEvent>&) override {
0070         throw std::runtime_error("Mystery!");
0071     }
0072 };
0073 
0074 TEST_CASE("JEventProcessor_Exception") {
0075     JApplication app;
0076     app.Add(new JEventSource);
0077     app.Add(new MyExceptingProcessor);
0078     bool found_throw = false;
0079     try {
0080         app.Run();
0081     }
0082     catch(JException& ex) {
0083         REQUIRE(ex.function_name == "JEventProcessor::Process");
0084         REQUIRE(ex.message == "Mystery!");
0085         REQUIRE(ex.exception_type == "std::runtime_error");
0086         found_throw = true;
0087     }
0088     REQUIRE(found_throw == true);
0089 
0090 }
0091 
0092 
0093 struct SourceWithRunNumberChange : public JEventSource {
0094     SourceWithRunNumberChange() {
0095         SetCallbackStyle(CallbackStyle::ExpertMode);
0096     }
0097     Result Emit(JEvent& event) {
0098         if (GetEmittedEventCount() < 2) {
0099             event.SetRunNumber(48);
0100         }
0101         else {
0102             event.SetRunNumber(49);
0103         }
0104         return Result::Success;
0105     }
0106 };
0107 
0108 
0109 struct ProcWithExceptionsAndLogging : public JEventProcessor {
0110     Parameter<bool> except_on_init {this, "except_on_init", false, "Except on init"};
0111     Parameter<bool> except_on_beginrun {this, "except_on_beginrun", false, "Except on beginrun"};
0112     Parameter<bool> except_on_process {this, "except_on_process", false, "Except on process"};
0113     Parameter<bool> except_on_endrun {this, "except_on_endrun", false, "Except on endrun"};
0114     Parameter<bool> except_on_finish {this, "except_on_finish", false, "Except on finish"};
0115 
0116     std::vector<std::string> log;
0117 
0118     void Init() override {
0119         LOG_INFO(GetLogger()) << "ProcWithExceptionsAndLogging::Init" << LOG_END;
0120         log.push_back("init");
0121         if (*except_on_init) throw std::runtime_error("Mystery");
0122     }
0123     void BeginRun(const std::shared_ptr<const JEvent>&) override {
0124         LOG_INFO(GetLogger()) << "ProcWithExceptionsAndLogging::BeginRun" << LOG_END;
0125         log.push_back("beginrun");
0126         if (*except_on_beginrun) throw std::runtime_error("Mystery");
0127     }
0128     void Process(const std::shared_ptr<const JEvent>&) override {
0129         LOG_INFO(GetLogger()) << "ProcWithExceptionsAndLogging::Process" << LOG_END;
0130         log.push_back("process");
0131         if (*except_on_process) throw std::runtime_error("Mystery");
0132     }
0133     void EndRun() override {
0134         LOG_INFO(GetLogger()) << "ProcWithExceptionsAndLogging::EndRun" << LOG_END;
0135         log.push_back("endrun");
0136         if (*except_on_endrun) throw std::runtime_error("Mystery");
0137     }
0138     void Finish() override {
0139         LOG_INFO(GetLogger()) << "ProcWithExceptionsAndLogging::Finish" << LOG_END;
0140         log.push_back("finish");
0141         if (*except_on_finish) throw std::runtime_error("Mystery");
0142     }
0143 };
0144 
0145 TEST_CASE("JEventProcessor_CallbackSequence") {
0146     JApplication app;
0147     auto sut = new ProcWithExceptionsAndLogging;
0148     app.Add(sut);
0149     // JApplication takes ownership of sut, so our pointer will become invalid when JApplication is destroyed
0150 
0151     SECTION("NoRunNumber") {
0152         app.Add(new JEventSource);
0153         app.SetParameterValue("jana:nevents", 2);
0154         app.Run();
0155         REQUIRE(sut->log.size() == 6);
0156         REQUIRE(sut->log.at(0) == "init");
0157         REQUIRE(sut->log.at(1) == "beginrun");
0158         REQUIRE(sut->log.at(2) == "process");
0159         REQUIRE(sut->log.at(3) == "process");
0160         REQUIRE(sut->log.at(4) == "endrun");
0161         REQUIRE(sut->log.at(5) == "finish");
0162     }
0163     SECTION("ConstantRunNumber") {
0164         app.Add(new SourceWithRunNumberChange);
0165         app.SetParameterValue("jana:nevents", 2);
0166         app.Run();
0167         REQUIRE(sut->log.size() == 6);
0168         REQUIRE(sut->log.at(0) == "init");
0169         REQUIRE(sut->log.at(1) == "beginrun");
0170         REQUIRE(sut->log.at(2) == "process");
0171         REQUIRE(sut->log.at(3) == "process");
0172         REQUIRE(sut->log.at(4) == "endrun");
0173         REQUIRE(sut->log.at(5) == "finish");
0174     }
0175     SECTION("MultipleRunNumbers") {
0176         app.Add(new SourceWithRunNumberChange);
0177         app.SetParameterValue("jana:nevents", 5);
0178         app.Run();
0179         REQUIRE(sut->log.size() == 11);
0180         REQUIRE(sut->log.at(0) == "init");
0181         REQUIRE(sut->log.at(1) == "beginrun");
0182         REQUIRE(sut->log.at(2) == "process");
0183         REQUIRE(sut->log.at(3) == "process");
0184         REQUIRE(sut->log.at(4) == "endrun");
0185         REQUIRE(sut->log.at(5) == "beginrun");
0186         REQUIRE(sut->log.at(6) == "process");
0187         REQUIRE(sut->log.at(7) == "process");
0188         REQUIRE(sut->log.at(8) == "process");
0189         REQUIRE(sut->log.at(9) == "endrun");
0190         REQUIRE(sut->log.at(10) == "finish");
0191     }
0192 }
0193 
0194 
0195 
0196 
0197 } // namespace jana::jeventprocessortests