File indexing completed on 2025-07-12 08:56:12
0001
0002
0003
0004 #include <JANA/JEventSource.h>
0005 #include <JANA/JEventProcessor.h>
0006 #include <JANA/JApplication.h>
0007 #include "JANA/JException.h"
0008 #include "JANA/JFactoryGenerator.h"
0009 #include "catch.hpp"
0010
0011 namespace jana::components::getobjects_tests {
0012
0013 struct Obj : public JObject { int data; };
0014
0015 struct Src : public JEventSource {
0016
0017 bool emit_inserts_rawdata = true;
0018 bool getobjects_retrieves_rawdata = true;
0019
0020 Src() {
0021 EnableGetObjects();
0022 SetCallbackStyle(CallbackStyle::ExpertMode);
0023 }
0024 Result Emit(JEvent& event) override {
0025 if (emit_inserts_rawdata) {
0026 auto item_to_insert = new Obj;
0027 item_to_insert->data = 33 + event.GetEventNumber();
0028 event.Insert(item_to_insert, "raw");
0029 }
0030 return Result::Success;
0031 }
0032 bool GetObjects(const std::shared_ptr<const JEvent>& event, JFactory* fac) override {
0033
0034 LOG_INFO(GetLogger()) << "GetObjects: Fac has object name '" << fac->GetObjectName() << "' and type name '" << fac->GetTypeName() << "' and tag '" << fac->GetTag() << "'" << LOG_END;
0035
0036 if (getobjects_retrieves_rawdata) {
0037 event->Get<Obj>("raw");
0038 }
0039
0040
0041 auto typed_fac = dynamic_cast<JFactoryT<Obj>*>(fac);
0042 if (typed_fac != nullptr) {
0043 auto obj = new Obj;
0044 obj->data = 22;
0045 typed_fac->Insert(obj);
0046 return true;
0047 }
0048 return false;
0049 }
0050 };
0051
0052 struct Fac : public JFactoryT<Obj> {
0053 void Process(const std::shared_ptr<const JEvent>&) override {
0054 auto obj = new Obj;
0055 obj->data = 23;
0056 Insert(obj);
0057 }
0058 };
0059
0060 struct RFac : public JFactoryT<Obj> {
0061 RFac() {
0062 SetFactoryFlag(REGENERATE);
0063 }
0064 void Process(const std::shared_ptr<const JEvent>&) override {
0065 auto obj = new Obj;
0066 obj->data = 23;
0067 Insert(obj);
0068 }
0069 };
0070
0071 struct Proc : public JEventProcessor {
0072 bool from_getobjects=true;
0073 void Process(const std::shared_ptr<const JEvent>& event) override {
0074 auto objs = event->Get<Obj>();
0075 REQUIRE(objs.size() == 1);
0076 if (from_getobjects) {
0077 REQUIRE(objs[0]->data == 22);
0078 }
0079 else {
0080 REQUIRE(objs[0]->data == 23);
0081 }
0082 }
0083 };
0084
0085 TEST_CASE("GetObjectsTests_NoFac") {
0086 JApplication app;
0087 app.SetParameterValue("jana:loglevel", "warn");
0088 app.SetParameterValue("jana:nevents", 1);
0089 app.Add(new Src);
0090 auto proc = new Proc;
0091 proc->from_getobjects = true;
0092 app.Add(proc);
0093 app.Add(new JFactoryGeneratorT<JFactoryT<Obj>>);
0094 app.Run();
0095 }
0096
0097 TEST_CASE("GetObjectsTests_OverrideFac") {
0098 JApplication app;
0099 app.SetParameterValue("jana:loglevel", "warn");
0100 app.SetParameterValue("jana:nevents", 1);
0101 app.Add(new Src);
0102 app.Add(new JFactoryGeneratorT<Fac>);
0103 auto proc = new Proc;
0104 proc->from_getobjects = true;
0105 app.Add(proc);
0106 app.Run();
0107 }
0108
0109 TEST_CASE("GetObjectsTests_Regenerate") {
0110 JApplication app;
0111 app.SetParameterValue("jana:loglevel", "warn");
0112 app.SetParameterValue("jana:nevents", 1);
0113 app.Add(new Src);
0114 app.Add(new JFactoryGeneratorT<RFac>);
0115 auto proc = new Proc;
0116 proc->from_getobjects = false;
0117 app.Add(proc);
0118 app.Run();
0119 }
0120
0121 TEST_CASE("GetObjectsTests_GetObjectsNeedsMissingData") {
0122 JApplication app;
0123 app.SetParameterValue("jana:loglevel", "warn");
0124 app.SetParameterValue("jana:nevents", 1);
0125
0126 auto src = new Src;
0127 src->emit_inserts_rawdata = false;
0128 src->getobjects_retrieves_rawdata = true;
0129
0130 auto proc = new Proc;
0131 proc->from_getobjects = true;
0132
0133 app.Add(src);
0134 app.Add(proc);
0135 app.Add(new JFactoryGeneratorT<JFactoryT<Obj>>);
0136 app.Add(new JFactoryGeneratorT<JFactoryT<Obj>>("raw"));
0137 try {
0138 app.Run();
0139 REQUIRE(0 == 1);
0140 }
0141 catch (const JException& e){
0142 LOG << e;
0143 REQUIRE(e.message == "Encountered a cycle in the factory dependency graph! Hint: Maybe this data was supposed to be inserted in the JEventSource");
0144 REQUIRE(e.type_name == "JFactoryT<jana::components::getobjects_tests::Obj>");
0145 }
0146 }
0147
0148
0149 }