File indexing completed on 2025-01-18 10:17:34
0001
0002
0003
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
0036
0037
0038
0039
0040
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
0050
0051
0052
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
0068 if (!m_autoactivate.empty()) {
0069 add(new JAutoActivator);
0070
0071 }
0072
0073
0074 preinitialize_components();
0075
0076
0077 resolve_event_sources();
0078
0079
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
0095 }
0096 for (auto* src_gen : m_src_gens) {
0097 src_gen->SetJApplication(GetApplication());
0098
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
0108
0109
0110
0111
0112
0113
0114 for (auto * src : m_evt_srces) {
0115 src->Summarize(m_summary);
0116 }
0117
0118
0119 for (auto * evt_proc : m_evt_procs) {
0120 evt_proc->Summarize(m_summary);
0121 }
0122
0123
0124 for (auto * unfolder : m_unfolders) {
0125 unfolder->Summarize(m_summary);
0126 }
0127
0128 JFactorySet dummy_fac_set(m_fac_gens);
0129
0130
0131 for (auto* fac : dummy_fac_set.GetAllFactories()) {
0132 try {
0133
0134
0135 fac->SetApplication(GetApplication());
0136 fac->DoInit();
0137 }
0138 catch (...) {
0139
0140
0141
0142
0143 }
0144 fac->Summarize(m_summary);
0145 }
0146
0147
0148 for (auto* fac : dummy_fac_set.GetAllMultifactories()) {
0149 try {
0150 fac->DoInit();
0151 }
0152 catch (...) {
0153
0154 }
0155 fac->Summarize(m_summary);
0156 }
0157 }
0158
0159 void JComponentManager::next_plugin(std::string plugin_name) {
0160
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
0216
0217
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
0226 if (m_user_evt_src_gen != nullptr) {
0227 return m_user_evt_src_gen;
0228 }
0229
0230
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
0242 if (best_gen != nullptr) {
0243 return best_gen;
0244 }
0245
0246
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
0254 if (m_user_evt_src_typename == "") return nullptr;
0255
0256
0257 for (auto* evt_src_gen : m_src_gens) {
0258 if (evt_src_gen->GetType() == m_user_evt_src_typename) {
0259
0260 return evt_src_gen;
0261 }
0262 }
0263
0264
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