Back to home page

EIC code displayed by LXR

 
 

    


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

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     m_params->FilterParameters(m_default_tags, "DEFTAG:");
0066 
0067     // Look for factories to auto-activate
0068     if (!m_autoactivate.empty()) {
0069         add(new JAutoActivator);
0070         // JAutoActivator will re-parse the autoactivate list by itself
0071     }
0072 
0073     // Give all components a JApplication pointer and a logger
0074     preinitialize_components();
0075 
0076     // Resolve all event sources now that all plugins have been loaded
0077     resolve_event_sources();
0078 
0079     // Call Summarize() and Init() in order to populate JComponentSummary and JParameterManager, respectively
0080     initialize_components();
0081 }
0082 
0083 void JComponentManager::preinitialize_components() {
0084     for (auto* src : m_evt_srces) {
0085         src->SetApplication(GetApplication());
0086         src->SetLogger(m_params->GetLogger(src->GetLoggerName()));
0087     }
0088     for (auto* proc : m_evt_procs) {
0089         proc->SetApplication(GetApplication());
0090         proc->SetLogger(m_params->GetLogger(proc->GetLoggerName()));
0091     }
0092     for (auto* fac_gen : m_fac_gens) {
0093         fac_gen->SetApplication(GetApplication());
0094         //fac_gen->SetLogger(m_logging->get_logger(fac_gen->GetLoggerName()));
0095     }
0096     for (auto* src_gen : m_src_gens) {
0097         src_gen->SetJApplication(GetApplication());
0098         //src_gen->SetLogger(m_logging->get_logger(src_gen->GetLoggerName()));
0099     }
0100     for (auto* unfolder : m_unfolders) {
0101         unfolder->SetApplication(GetApplication());
0102         unfolder->SetLogger(m_params->GetLogger(unfolder->GetLoggerName()));
0103     }
0104 }
0105 
0106 void JComponentManager::initialize_components() {
0107     // For now, this only computes the summary for all components except factories.
0108     // However, we are likely to eventually want summaries to access information only
0109     // available after component initialization, specifically parameters. In this case, 
0110     // we would move responsibility for source, unfolder, and processor initialization 
0111     // out from the JArrowTopology and into here.
0112 
0113     // Event sources
0114     for (auto * src : m_evt_srces) {
0115         src->Summarize(m_summary);
0116     }
0117 
0118     // Event processors
0119     for (auto * evt_proc : m_evt_procs) {
0120         evt_proc->Summarize(m_summary);
0121     }
0122 
0123     // Unfolders
0124     for (auto * unfolder : m_unfolders) {
0125         unfolder->Summarize(m_summary);
0126     }
0127 
0128     JFactorySet dummy_fac_set(m_fac_gens);
0129 
0130     // Factories
0131     for (auto* fac : dummy_fac_set.GetAllFactories()) {
0132         try {
0133             // Run Init() on each factory in order to capture any parameters 
0134             // (and eventually services) that are retrieved via GetApplication().
0135             fac->SetApplication(GetApplication());
0136             fac->DoInit();
0137         }
0138         catch (...) {
0139             // Swallow any exceptions!
0140             // Everything in dummy_fac_set will be destroyed immediately after this.
0141             // The same exception will be thrown from a fresh factory
0142             // set once processing begins, unless the factory is never used.
0143         }
0144         fac->Summarize(m_summary);
0145     }
0146 
0147     // Multifactories
0148     for (auto* fac : dummy_fac_set.GetAllMultifactories()) {
0149         try {
0150             fac->DoInit();
0151         }
0152         catch (...) {
0153             // Swallow any exceptions! See above.
0154         }
0155         fac->Summarize(m_summary);
0156     }
0157 }
0158 
0159 void JComponentManager::next_plugin(std::string plugin_name) {
0160     // We defer resolving event sources until we have finished loading all plugins
0161     m_current_plugin_name = plugin_name;
0162 }
0163 
0164 void JComponentManager::add(std::string event_source_name) {
0165     m_src_names.push_back(event_source_name);
0166 }
0167 
0168 void JComponentManager::add(JEventSourceGenerator *source_generator) {
0169     source_generator->SetPluginName(m_current_plugin_name);
0170     m_src_gens.push_back(source_generator);
0171 }
0172 
0173 void JComponentManager::add(JFactoryGenerator *factory_generator) {
0174     factory_generator->SetPluginName(m_current_plugin_name);
0175     m_fac_gens.push_back(factory_generator);
0176 }
0177 
0178 void JComponentManager::add(JEventSource *event_source) {
0179     event_source->SetPluginName(m_current_plugin_name);
0180     m_evt_srces.push_back(event_source);
0181 }
0182 
0183 void JComponentManager::add(JEventProcessor *processor) {
0184     processor->SetPluginName(m_current_plugin_name);
0185     m_evt_procs.push_back(processor);
0186 }
0187 
0188 void JComponentManager::add(JEventUnfolder* unfolder) {
0189     unfolder->SetPluginName(m_current_plugin_name);
0190     m_unfolders.push_back(unfolder);
0191 }
0192 
0193 void JComponentManager::configure_event(JEvent& event) {
0194     auto factory_set = new JFactorySet(m_fac_gens);
0195     event.SetFactorySet(factory_set);
0196     event.SetDefaultTags(m_default_tags);
0197     event.GetJCallGraphRecorder()->SetEnabled(m_enable_call_graph_recording);
0198     event.SetJApplication(GetApplication());
0199 }
0200 
0201 
0202 
0203 void JComponentManager::resolve_event_sources() {
0204 
0205     m_user_evt_src_gen = resolve_user_event_source_generator();
0206     for (auto& source_name : m_src_names) {
0207         auto* generator = resolve_event_source(source_name);
0208         auto source = generator->MakeJEventSource(source_name);
0209         source->SetPluginName(generator->GetPluginName());
0210         source->SetApplication(GetApplication());
0211         m_evt_srces.push_back(source);
0212     }
0213 
0214     for (auto source : m_evt_srces) {
0215         // If nskip/nevents are set individually on JEventSources, respect those. Otherwise use global values.
0216         // Note that this is not what we usually want when we have multiple event sources. It would make more sense to
0217         // take the nskip/nevent slice across the stream of events emitted by each JEventSource in turn.
0218         if (source->GetNSkip() == 0) source->SetNSkip(m_nskip);
0219         if (source->GetNEvents() == 0) source->SetNEvents(m_nevents);
0220     }
0221 }
0222 
0223 JEventSourceGenerator *JComponentManager::resolve_event_source(std::string source_name) const {
0224 
0225     // Always use the user override if they provided one
0226     if (m_user_evt_src_gen != nullptr) {
0227         return m_user_evt_src_gen;
0228     }
0229 
0230     // Otherwise determine the best via CheckOpenable()
0231     JEventSourceGenerator* best_gen = nullptr;
0232     double best_likelihood = 0.0;
0233     for (auto* gen : m_src_gens) {
0234         auto likelihood = gen->CheckOpenable(source_name);
0235         if (best_likelihood < likelihood) {
0236             best_likelihood = likelihood;
0237             best_gen = gen;
0238         }
0239     }
0240 
0241     // If we found the best, use that
0242     if (best_gen != nullptr) {
0243         return best_gen;
0244     }
0245 
0246     // Otherwise, report the problem and throw
0247     throw JException("Unable to open event source \"%s\": No suitable generator found!", source_name.c_str());
0248 }
0249 
0250 
0251 JEventSourceGenerator* JComponentManager::resolve_user_event_source_generator() const {
0252 
0253     // If the user didn't specify an EVENT_SOURCE_TYPE, do nothing
0254     if (m_user_evt_src_typename == "") return nullptr;
0255 
0256     // Attempt to find the user event source generator
0257     for (auto* evt_src_gen : m_src_gens) {
0258         if (evt_src_gen->GetType() == m_user_evt_src_typename) {
0259             // Found! Save and exit
0260             return evt_src_gen;
0261         }
0262     }
0263 
0264     // No user event source generator found; generate a message and throw
0265     std::ostringstream os;
0266     os << "You specified event source type \"" << m_user_evt_src_typename << "\"" << std::endl;
0267     os << "be used to read the event sources but no such type exists." << std::endl;
0268     os << "Here is a list of available source types:" << std::endl << std::endl;
0269     for (auto * evt_src_gen : m_src_gens) {
0270         os << "    " << evt_src_gen->GetType() << std::endl;
0271     }
0272     os << std::endl;
0273     throw JException(os.str());
0274 
0275 }
0276 
0277 std::vector<JEventSourceGenerator*>& JComponentManager::get_evt_src_gens() {
0278     return m_src_gens;
0279 }
0280 
0281 std::vector<JEventSource*>& JComponentManager::get_evt_srces() {
0282     return m_evt_srces;
0283 }
0284 
0285 std::vector<JEventProcessor*>& JComponentManager::get_evt_procs() {
0286     return m_evt_procs;
0287 }
0288 
0289 std::vector<JFactoryGenerator*>& JComponentManager::get_fac_gens() {
0290     return m_fac_gens;
0291 }
0292 
0293 std::vector<JEventUnfolder*>& JComponentManager::get_unfolders() {
0294     return m_unfolders;
0295 }
0296 
0297 const JComponentSummary& JComponentManager::get_component_summary() {
0298     return m_summary;
0299 }
0300