Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-09 08:51:49

0001 // Copyright 2023, Jefferson Science Associates, LLC.
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 // Created by Nathan Brei
0004 
0005 #pragma once
0006 
0007 /**
0008  * Omnifactories are a lightweight layer connecting JANA to generic algorithms
0009  * It is assumed multiple input data (controlled by input tags)
0010  * which might be changed by user parameters.
0011  */
0012 
0013 #include "JANA/Services/JParameterManager.h"
0014 #include "JANA/Utils/JEventLevel.h"
0015 #include <JANA/JEvent.h>
0016 #include <JANA/JMultifactory.h>
0017 #include <JANA/JVersion.h>
0018 #include <JANA/Components/JHasInputs.h>
0019 #include <JANA/Components/JHasOutputs.h>
0020 
0021 #include <string>
0022 #include <vector>
0023 
0024 namespace jana::components {
0025 
0026 struct EmptyConfig {};
0027 
0028 template <typename AlgoT, typename ConfigT=EmptyConfig>
0029 class JOmniFactory : public JMultifactory, 
0030                      public jana::components::JHasInputs,
0031                      public jana::components::JHasOutputs {
0032 private:
0033 
0034     ConfigT m_config;
0035 
0036 public:
0037 
0038     inline void PreInit(std::string tag,
0039                         JEventLevel level,
0040                         std::vector<std::string> input_collection_names,
0041                         std::vector<JEventLevel> input_collection_levels,
0042                         std::vector<std::vector<std::string>> variadic_input_collection_names,
0043                         std::vector<JEventLevel> variadic_input_collection_levels, 
0044                         std::vector<std::string> output_collection_names,
0045                         std::vector<std::vector<std::string>> variadic_output_collection_names
0046                         ) {
0047 
0048         m_prefix = (this->GetPluginName().empty()) ? tag : this->GetPluginName() + ":" + tag;
0049         m_level = level;
0050 
0051         // Obtain logger
0052         m_logger = m_app->GetService<JParameterManager>()->GetLogger(m_prefix);
0053 
0054         // Obtain collection name overrides if provided.
0055         // Priority = [JParameterManager, JOmniFactoryGenerator]
0056         m_app->SetDefaultParameter(m_prefix + ":InputTags", input_collection_names, "Input collection names");
0057         m_app->SetDefaultParameter(m_prefix + ":OutputTags", output_collection_names, "Output collection names");
0058 
0059         WireInputs(level, input_collection_levels, input_collection_names, variadic_input_collection_levels, variadic_input_collection_names);
0060         WireOutputs(level, output_collection_names, variadic_output_collection_names);
0061 
0062         // Handle the JMultifactory-specific wiring details
0063         for (auto* output: m_outputs) {
0064             output->CreateHelperFactory(*this);
0065         }
0066 
0067         // Configure logger. Priority = [JParameterManager, system log level]
0068         // std::string default_log_level = eicrecon::LogLevelToString(m_logger->level());
0069         // m_app->SetDefaultParameter(m_prefix + ":LogLevel", default_log_level, "LogLevel: trace, debug, info, warn, err, critical, off");
0070         // m_logger->set_level(eicrecon::ParseLogLevel(default_log_level));
0071     }
0072 
0073     void Init() override {
0074         for (auto* parameter : m_parameters) {
0075             parameter->Init(*(m_app->GetJParameterManager()), m_prefix);
0076         }
0077         for (auto* service : m_services) {
0078             service->Fetch(m_app);
0079         }
0080         static_cast<AlgoT*>(this)->Configure();
0081     }
0082 
0083     void BeginRun(const std::shared_ptr<const JEvent>& event) override {
0084         for (auto* resource : m_resources) {
0085             resource->ChangeRun(event->GetRunNumber(), m_app);
0086         }
0087         static_cast<AlgoT*>(this)->ChangeRun(event->GetRunNumber());
0088     }
0089 
0090     void Process(const std::shared_ptr<const JEvent> &event) override {
0091         try {
0092             for (auto* input : m_inputs) {
0093                 input->GetCollection(*event);
0094             }
0095             for (auto* variadic_input : m_variadic_inputs) {
0096                 variadic_input->GetCollection(*event);
0097             }
0098             for (auto* output : m_outputs) {
0099                 output->Reset();
0100             }
0101             static_cast<AlgoT*>(this)->Execute(event->GetRunNumber(), event->GetEventNumber());
0102             for (auto* output : m_outputs) {
0103                 output->SetCollection(*this);
0104             }
0105         }
0106         catch(std::exception &e) {
0107             throw JException(e.what());
0108         }
0109     }
0110 
0111     using ConfigType = ConfigT;
0112 
0113     /// Retrieve reference to already-configured logger
0114     //std::shared_ptr<spdlog::logger> &logger() { return m_logger; }
0115     JLogger& logger() { return m_logger; }
0116 
0117     /// Retrieve reference to embedded config object
0118     ConfigT& config() { return m_config; }
0119 
0120 
0121     /// Generate summary for UI, inspector
0122     void Summarize(JComponentSummary& summary) const override {
0123 
0124         auto* mfs = new JComponentSummary::Component(
0125             "OmniFactory", GetPrefix(), GetTypeName(), GetLevel(), GetPluginName());
0126 
0127         SummarizeInputs(*mfs);
0128         SummarizeOutputs(*mfs);
0129         summary.Add(mfs);
0130     }
0131 
0132 };
0133 
0134 } // namespace jana::components
0135 
0136 using jana::components::JOmniFactory;
0137 
0138