Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:51:32

0001 
0002 // Copyright 2021, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 
0006 #include "JAutoActivator.h"
0007 #include <JANA/JEventSource.h>
0008 #include <ios>
0009 
0010 JAutoActivator::JAutoActivator() {
0011     SetTypeName("JAutoActivator");
0012     SetLoggerName("jana"); // We want this controlled by jana:loglevel
0013     SetCallbackStyle(CallbackStyle::ExpertMode);
0014 }
0015 
0016 void JAutoActivator::AddAutoActivatedFactory(string factory_name, string factory_tag) {
0017     m_auto_activated_factories.push_back({std::move(factory_name), std::move(factory_tag)});
0018 }
0019 
0020 /// Converts "ObjName:TagName" into ("ObjName", "TagName")
0021 ///      and "name::space::ObjName:TagName" into ("name::space::ObjName", "TagName")
0022 std::pair<std::string, std::string> JAutoActivator::Split(std::string factory_name) {
0023 
0024     std::string::size_type pos = -1;
0025     while (true) {
0026         // Find the next colon
0027         pos = factory_name.find(':', pos+1);
0028 
0029         if (pos == std::string::npos) {
0030             // There are no colons at all remaining. Everything is objname
0031             return std::make_pair(factory_name, "");
0032         }
0033         else if (factory_name[pos+1] == ':') {
0034             // Else we found a double colon, which is part of the objname namespace. Keep going.
0035             pos += 1;
0036         }
0037         else {
0038             // We found the first single colon, which has to delimit the objname from the tag. Stop.
0039             return std::make_pair(factory_name.substr(0, pos), factory_name.substr(pos+1));
0040         }
0041     }
0042 }
0043 
0044 void JAutoActivator::Init() {
0045 
0046     string autoactivate_conf;
0047     if (GetApplication()->GetParameter("autoactivate", autoactivate_conf)) {
0048         try {
0049             if (!autoactivate_conf.empty()) {
0050                 // Loop over comma separated list of factories to auto activate
0051                 vector <string> myfactories;
0052                 string &str = autoactivate_conf;
0053                 unsigned int cutAt;
0054                 while ((cutAt = str.find(",")) != (unsigned int) str.npos) {
0055                     if (cutAt > 0)myfactories.push_back(str.substr(0, cutAt));
0056                     str = str.substr(cutAt + 1);
0057                 }
0058                 if (str.length() > 0)myfactories.push_back(str);
0059 
0060                 // Loop over list of factory strings (which could be in factory:tag
0061                 // form) and parse the strings as needed in order to add them to
0062                 // the auto activate list.
0063                 for (unsigned int i = 0; i < myfactories.size(); i++) {
0064                     auto pair = Split(myfactories[i]);
0065                     AddAutoActivatedFactory(pair.first, pair.second);
0066                 }
0067             }
0068         }
0069         catch (...) {
0070             LOG_ERROR(GetLogger()) << "Error parsing parameter 'autoactivate'. Found: " << autoactivate_conf << LOG_END;
0071             throw JException("AutoActivator could not parse parameter 'autoactivate'");
0072         }
0073     }
0074     m_output_processed_event_numbers = GetApplication()->RegisterParameter("jana:output_processed_event_numbers", false);
0075     if (m_output_processed_event_numbers) {
0076         m_processed_event_numbers_file.open("processed_event_numbers.csv", std::ios_base::out);
0077         m_processed_event_numbers_file << "source,run_number,event_number" << std::endl;
0078     }
0079 }
0080 
0081 void JAutoActivator::ProcessParallel(const JEvent& event) {
0082     for (const auto &pair: m_auto_activated_factories) {
0083         auto name = pair.first;
0084         auto tag = pair.second;
0085         auto factory = event.GetFactory(name, tag);
0086         if (factory != nullptr) {
0087             LOG_DEBUG(GetLogger()) << "Autoactivating factory with typename=" << name << ", tag=" << tag << LOG_END;
0088             factory->Create(event); // This will do nothing if factory is already created
0089         }
0090         else {
0091             LOG_ERROR(GetLogger()) << "Could not find factory with typename=" << name << ", tag=" << tag << LOG_END;
0092             throw JException("AutoActivator could not find factory with typename=%s, tag=%s", name.c_str(), tag.c_str());
0093         }
0094     }
0095 }
0096 
0097 void JAutoActivator::ProcessSequential(const JEvent& event) {
0098     if (m_output_processed_event_numbers) {
0099         std::string name = event.GetJEventSource()->GetResourceName();
0100         if (name.empty()) {
0101             name = event.GetJEventSource()->GetTypeName();
0102         }
0103         m_processed_event_numbers_file << name << "," 
0104                                        << event.GetRunNumber() << "," 
0105                                        << event.GetEventNumber() << std::endl;
0106         m_processed_event_numbers_file.flush();
0107     }
0108 }
0109 
0110 void JAutoActivator::Finish() {
0111     if (m_output_processed_event_numbers) {
0112         m_processed_event_numbers_file.close();
0113     }
0114 }
0115