Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-16 09:15:21

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 
0006 #include "JComponentManager.h"
0007 #include <JANA/JEventProcessor.h>
0008 #include <JANA/JMultifactory.h>
0009 #include <JANA/JFactoryGenerator.h>
0010 #include <JANA/JEventUnfolder.h>
0011 #include <JANA/Utils/JAutoActivator.h>
0012 
0013 JComponentManager::JComponentManager() {
0014     SetPrefix("jana");
0015 }
0016 
0017 JComponentManager::~JComponentManager() {
0018 
0019     for (auto* src_gen : m_src_gens) {
0020         delete src_gen;
0021     }
0022     for (auto* fac_gen : m_fac_gens) {
0023         delete fac_gen;
0024     }
0025 
0026     for (auto* src : m_evt_srces) {
0027         LOG_TRACE(GetLogger()) << "Destroying source with type=" << src->GetTypeName();
0028         delete src;
0029     }
0030     for (auto* unfolder : m_unfolders) {
0031         LOG_TRACE(GetLogger()) << "Destroying unfolder with type=" << unfolder->GetTypeName();
0032         delete unfolder;
0033     }
0034 
0035     // The order of deletion here sadly matters, because GlueX likes to fill
0036     // histograms inside component destructors. Thus event processors must be destroyed after 
0037     // all other components, and the last component to be destroyed should be the _first_ event 
0038     // processor, because that's the one that is usually provided by the main program and handles 
0039     // the ROOT output file. We may eventually want to move all ROOT file operations to a separate
0040     // JService which would be closed and destroyed after all other components.
0041 
0042     for (int i=m_evt_procs.size()-1; i >= 0; --i) {
0043         LOG_TRACE(GetLogger()) << "Destroying processor with type=" << m_evt_procs[i]->GetTypeName();
0044         delete m_evt_procs[i];
0045     }
0046 }
0047 
0048 void JComponentManager::Init() {
0049     // Because this is an 'internal' JService, it works a little differently than user-provided
0050     // JServices. Specifically, we have to fetch parameters _after_ plugin loading because some 
0051     // plugins want to set parameter values that JComponentManager needs (most notably janadot). 
0052     // We handle parameters in configure_components() instead.
0053 }
0054 
0055 void JComponentManager::configure_components() {
0056 
0057     m_params->SetDefaultParameter("event_source_type", m_user_evt_src_typename, "Manually specifies which JEventSource should open the input file");
0058     m_params->SetDefaultParameter("record_call_stack", 
0059                                   m_enable_call_graph_recording,
0060                                   "Records a trace of who called each factory. Reduces performance but necessary for plugins such as janadot.")
0061             ->SetIsAdvanced(true);
0062     m_params->SetDefaultParameter("jana:nevents", m_nevents, "Max number of events that sources can emit");
0063     m_params->SetDefaultParameter("jana:nskip", m_nskip, "Number of events that sources should skip before starting emitting");
0064     m_params->SetDefaultParameter("autoactivate", m_autoactivate, "List of factories to activate regardless of what the event processors request. Format is typename:tag,typename:tag");
0065 
0066 
0067     bool output_processed_event_numbers = false;
0068     m_params->SetDefaultParameter("jana:output_processed_event_numbers", output_processed_event_numbers, "Write the sequence of processed event numbers to file");
0069     m_params->FilterParameters(m_default_tags, "DEFTAG:");
0070 
0071     // Look for factories to auto-activate
0072     if (!m_autoactivate.empty() || output_processed_event_numbers) {
0073         m_evt_procs.insert(m_evt_procs.begin(), new JAutoActivator);
0074         // We add this to the _front_ of the evt_procs vector so that it is run _first_
0075         // JAutoActivator will re-parse the autoactivate list by itself
0076     }
0077 
0078     // Give all components a JApplication pointer and a logger
0079     preinitialize_components();
0080 
0081     // Resolve all event sources now that all plugins have been loaded
0082     // (Note that we need to wire the event source generators beforehand)
0083     resolve_event_sources();
0084 
0085     // Call Summarize() and Init() in order to populate JComponentSummary and JParameterManager, respectively
0086     initialize_components();
0087 }
0088 
0089 void JComponentManager::preinitialize_components() {
0090     for (auto* src : m_evt_srces) {
0091         src->Wire(GetApplication());
0092     }
0093     for (auto* proc : m_evt_procs) {
0094         proc->Wire(GetApplication());
0095     }
0096     for (auto* fac_gen : m_fac_gens) {
0097         fac_gen->SetApplication(GetApplication());
0098         //fac_gen->SetLogger(m_logging->get_logger(fac_gen->GetLoggerName()));
0099     }
0100     for (auto* src_gen : m_src_gens) {
0101         src_gen->SetJApplication(GetApplication());
0102         //src_gen->SetLogger(m_logging->get_logger(src_gen->GetLoggerName()));
0103     }
0104     for (auto* unfolder : m_unfolders) {
0105         unfolder->Wire(GetApplication());
0106     }
0107 }
0108 
0109 void JComponentManager::initialize_components() {
0110     // For now, this only computes the summary for all components except factories.
0111     // However, we are likely to eventually want summaries to access information only
0112     // available after component initialization, specifically parameters. In this case, 
0113     // we would move responsibility for source, unfolder, and processor initialization 
0114     // out from the JArrowTopology and into here.
0115 
0116     // Event sources
0117     for (auto * src : m_evt_srces) {
0118         src->Summarize(m_summary);
0119     }
0120 
0121     // Event processors
0122     for (auto * evt_proc : m_evt_procs) {
0123         evt_proc->Summarize(m_summary);
0124     }
0125 
0126     // Unfolders
0127     for (auto * unfolder : m_unfolders) {
0128         unfolder->Summarize(m_summary);
0129     }
0130 
0131     JFactorySet dummy_fac_set;
0132     for (auto* fac_gen : m_fac_gens) {
0133         // TODO: Get rid of this
0134         fac_gen->GenerateFactories(&dummy_fac_set);
0135     }
0136 
0137     // Factories
0138     for (auto* fac : dummy_fac_set.GetAllFactories()) {
0139         try {
0140             // Run Init() on each factory in order to capture any parameters 
0141             // (and eventually services) that are retrieved via GetApplication().
0142             fac->SetApplication(GetApplication());
0143             fac->DoInit();
0144         }
0145         catch (...) {
0146             // Swallow any exceptions!
0147             // Everything in dummy_fac_set will be destroyed immediately after this.
0148             // The same exception will be thrown from a fresh factory
0149             // set once processing begins, unless the factory is never used.
0150         }
0151         fac->Summarize(m_summary);
0152     }
0153 }
0154 
0155 void JComponentManager::next_plugin(std::string plugin_name) {
0156     // We defer resolving event sources until we have finished loading all plugins
0157     m_current_plugin_name = plugin_name;
0158 }
0159 
0160 void JComponentManager::add(std::string event_source_name) {
0161     m_src_names.push_back(event_source_name);
0162 }
0163 
0164 void JComponentManager::add(JEventSourceGenerator *source_generator) {
0165     source_generator->SetPluginName(m_current_plugin_name);
0166     m_src_gens.push_back(source_generator);
0167 }
0168 
0169 void JComponentManager::add(JFactoryGenerator *factory_generator) {
0170     factory_generator->SetPluginName(m_current_plugin_name);
0171     m_fac_gens.push_back(factory_generator);
0172 }
0173 
0174 void JComponentManager::add(JEventSource *event_source) {
0175     event_source->SetPluginName(m_current_plugin_name);
0176     m_evt_srces.push_back(event_source);
0177 }
0178 
0179 void JComponentManager::add(JEventProcessor *processor) {
0180     processor->SetPluginName(m_current_plugin_name);
0181     m_evt_procs.push_back(processor);
0182 }
0183 
0184 void JComponentManager::add(JEventUnfolder* unfolder) {
0185     unfolder->SetPluginName(m_current_plugin_name);
0186     m_unfolders.push_back(unfolder);
0187 }
0188 
0189 void JComponentManager::configure_event(JEvent& event) {
0190     auto* factory_set = event.GetFactorySet();
0191     for (auto gen : m_fac_gens) {
0192         gen->GenerateFactories(factory_set);
0193     }
0194     event.SetDefaultTags(m_default_tags);
0195     event.GetJCallGraphRecorder()->SetEnabled(m_enable_call_graph_recording);
0196     event.SetJApplication(GetApplication());
0197 }
0198 
0199 
0200 
0201 void JComponentManager::resolve_event_sources() {
0202 
0203     m_user_evt_src_gen = resolve_user_event_source_generator();
0204     for (auto& source_name : m_src_names) {
0205         auto* generator = resolve_event_source(source_name);
0206         auto source = generator->MakeJEventSource(source_name);
0207         source->SetPluginName(generator->GetPluginName());
0208         source->Wire(GetApplication());
0209         m_evt_srces.push_back(source);
0210     }
0211 
0212     for (auto source : m_evt_srces) {
0213         // If nskip/nevents are set individually on JEventSources, respect those. Otherwise use global values.
0214         // Note that this is not what we usually want when we have multiple event sources. It would make more sense to
0215         // take the nskip/nevent slice across the stream of events emitted by each JEventSource in turn.
0216         if (source->GetNSkip() == 0) source->SetNSkip(m_nskip);
0217         if (source->GetNEvents() == 0) source->SetNEvents(m_nevents);
0218     }
0219 }
0220 
0221 JEventSourceGenerator *JComponentManager::resolve_event_source(std::string source_name) const {
0222 
0223     // Always use the user override if they provided one
0224     if (m_user_evt_src_gen != nullptr) {
0225         return m_user_evt_src_gen;
0226     }
0227 
0228     // Otherwise determine the best via CheckOpenable()
0229     JEventSourceGenerator* best_gen = nullptr;
0230     double best_likelihood = 0.0;
0231     for (auto* gen : m_src_gens) {
0232         auto likelihood = gen->CheckOpenable(source_name);
0233         if (best_likelihood < likelihood) {
0234             best_likelihood = likelihood;
0235             best_gen = gen;
0236         }
0237     }
0238 
0239     // If we found the best, use that
0240     if (best_gen != nullptr) {
0241         return best_gen;
0242     }
0243 
0244     // Otherwise, report the problem and throw
0245     throw JException("Unable to open event source \"%s\": No suitable generator found!", source_name.c_str());
0246 }
0247 
0248 
0249 JEventSourceGenerator* JComponentManager::resolve_user_event_source_generator() const {
0250 
0251     // If the user didn't specify an EVENT_SOURCE_TYPE, do nothing
0252     if (m_user_evt_src_typename == "") return nullptr;
0253 
0254     // Attempt to find the user event source generator
0255     for (auto* evt_src_gen : m_src_gens) {
0256         if (evt_src_gen->GetType() == m_user_evt_src_typename) {
0257             // Found! Save and exit
0258             return evt_src_gen;
0259         }
0260     }
0261 
0262     // No user event source generator found; generate a message and throw
0263     std::ostringstream os;
0264     os << "You specified event source type \"" << m_user_evt_src_typename << "\"" << std::endl;
0265     os << "be used to read the event sources but no such type exists." << std::endl;
0266     os << "Here is a list of available source types:" << std::endl << std::endl;
0267     for (auto * evt_src_gen : m_src_gens) {
0268         os << "    " << evt_src_gen->GetType() << std::endl;
0269     }
0270     os << std::endl;
0271     throw JException(os.str());
0272 
0273 }
0274 
0275 std::vector<JEventSourceGenerator*>& JComponentManager::get_evt_src_gens() {
0276     return m_src_gens;
0277 }
0278 
0279 std::vector<JEventSource*>& JComponentManager::get_evt_srces() {
0280     return m_evt_srces;
0281 }
0282 
0283 std::vector<JEventProcessor*>& JComponentManager::get_evt_procs() {
0284     return m_evt_procs;
0285 }
0286 
0287 std::vector<JFactoryGenerator*>& JComponentManager::get_fac_gens() {
0288     return m_fac_gens;
0289 }
0290 
0291 std::vector<JEventUnfolder*>& JComponentManager::get_unfolders() {
0292     return m_unfolders;
0293 }
0294 
0295 const JComponentSummary& JComponentManager::get_component_summary() {
0296     return m_summary;
0297 }
0298