Back to home page

EIC code displayed by LXR

 
 

    


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

0001 
0002 // Copyright 2023, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #include <JANA/JFactory.h>
0006 #include <JANA/JEvent.h>
0007 #include <JANA/JEventSource.h>
0008 #include <JANA/Utils/JTypeInfo.h>
0009 
0010 
0011 void JFactory::Create(const std::shared_ptr<const JEvent>& event) {
0012 
0013     if (m_app == nullptr && event->GetJApplication() != nullptr) {
0014         // These are usually set by JFactoryGeneratorT, but some user code has custom JFactoryGenerators which don't!
0015         // The design of JFactoryGenerator doesn't give us a better place to inject things
0016         m_app = event->GetJApplication();
0017         m_logger = m_app->GetJParameterManager()->GetLogger(GetLoggerName());
0018     }
0019 
0020     if (mStatus == Status::Uninitialized) {
0021         DoInit();
0022     }
0023 
0024     // How do we obtain our data? The priority is as follows:
0025     // 1. JFactory::Process() if REGENERATE flag is set
0026     // 2. JEvent::Insert()
0027     // 3. JEventSource::GetObjects() if source has GetObjects() enabled
0028     // 4. JFactory::Process()
0029 
0030     // ---------------------------------------------------------------------
0031     // 1. JFactory::Process() if REGENERATE flag is set
0032     // ---------------------------------------------------------------------
0033 
0034     if (TestFactoryFlag(REGENERATE)) {
0035         if (mStatus == Status::Inserted) {
0036             // Status::Inserted indicates that the data came from either src->GetObjects() or evt->Insert()
0037             ClearData(); 
0038             // ClearData() resets mStatus to Unprocessed so that the data will be regenerated exactly once.
0039         }
0040         // After this point, control flow falls through to "4. JFactory::Process"
0041     }
0042     else {
0043 
0044         // ---------------------------------------------------------------------
0045         // 2. JEvent::Insert()
0046         // ---------------------------------------------------------------------
0047 
0048         if (mStatus == Status::Inserted) {
0049             // This may include data cached from eventsource->GetObjects().
0050             // Either way, short-circuit here, because the data is present.
0051             return;
0052         }
0053 
0054         // ---------------------------------------------------------------------
0055         // 3. JEventSource::GetObjects() if source has GetObjects() enabled
0056         // ---------------------------------------------------------------------
0057 
0058         auto src = event->GetJEventSource();
0059         if (src != nullptr && src->IsGetObjectsEnabled()) {
0060             bool found_data = false;
0061 
0062             CallWithJExceptionWrapper("JEventSource::GetObjects", [&](){ 
0063                 found_data = src->GetObjects(event, this); });
0064 
0065             if (found_data) {
0066                 mStatus = Status::Inserted;
0067                 mCreationStatus = CreationStatus::InsertedViaGetObjects;
0068                 return;
0069             }
0070         }
0071         // If neither "2. JEvent::Insert()" nor "3. JEventSource::GetObjects()" succeeded, fall through to "4. JFactory::Process()"
0072     }
0073 
0074     // ---------------------------------------------------------------------
0075     // 4. JFactory::Process()
0076     // ---------------------------------------------------------------------
0077 
0078     // If the data was Processed (instead of Inserted), it will be in cache, and we can just exit.
0079     // Otherwise we call Process() to create the data in the first place.
0080     if (mStatus == Status::Unprocessed) {
0081         auto run_number = event->GetRunNumber();
0082         if (mPreviousRunNumber != run_number) {
0083             if (mPreviousRunNumber != -1) {
0084                 CallWithJExceptionWrapper("JFactory::EndRun", [&](){ EndRun(); });
0085             }
0086             CallWithJExceptionWrapper("JFactory::ChangeRun", [&](){ ChangeRun(event); });
0087             CallWithJExceptionWrapper("JFactory::BeginRun", [&](){ BeginRun(event); });
0088             mPreviousRunNumber = run_number;
0089         }
0090         CallWithJExceptionWrapper("JFactory::Process", [&](){ Process(event); });
0091         mStatus = Status::Processed;
0092         mCreationStatus = CreationStatus::Created;
0093     }
0094 }
0095 
0096 void JFactory::DoInit() {
0097     if (mStatus != Status::Uninitialized) {
0098         return;
0099     }
0100     for (auto* parameter : m_parameters) {
0101         parameter->Configure(*(m_app->GetJParameterManager()), m_prefix);
0102     }
0103     for (auto* service : m_services) {
0104         service->Fetch(m_app);
0105     }
0106     CallWithJExceptionWrapper("JFactory::Init", [&](){ Init(); });
0107     mStatus = Status::Unprocessed;
0108 }
0109 
0110 void JFactory::DoFinish() {
0111     if (mStatus == Status::Unprocessed || mStatus == Status::Processed) {
0112         if (mPreviousRunNumber != -1) {
0113             CallWithJExceptionWrapper("JFactory::EndRun", [&](){ EndRun(); });
0114         }
0115         CallWithJExceptionWrapper("JFactory::Finish", [&](){ Finish(); });
0116         mStatus = Status::Finished;
0117     }
0118 }
0119 
0120 void JFactory::Summarize(JComponentSummary& summary) const {
0121 
0122     auto fs = new JComponentSummary::Component(
0123             "Factory",
0124             GetPrefix(),
0125             GetTypeName(),
0126             GetLevel(),
0127             GetPluginName());
0128 
0129     auto coll = new JComponentSummary::Collection(
0130             GetTag(), 
0131             GetTag(),
0132             GetObjectName(), 
0133             GetLevel());
0134 
0135     fs->AddOutput(coll);
0136     summary.Add(fs);
0137 }
0138 
0139