Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright 2020, Jefferson Science Associates, LLC.
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 
0004 
0005 #include "JControlEventProcessor.h"
0006 #include <JANA/JLogger.h>
0007 
0008 #include <sstream>
0009 
0010 
0011 //-------------------------------------------------------------
0012 // JControlEventProcessor
0013 //-------------------------------------------------------------
0014 JControlEventProcessor::JControlEventProcessor(JApplication *japp):JEventProcessor(japp),jstringification(new JStringification) {
0015     SetTypeName(NAME_OF_THIS); // Provide JANA with this class's name
0016 }
0017 
0018 //-------------------------------------------------------------
0019 // Init
0020 //-------------------------------------------------------------
0021 void JControlEventProcessor::Init() {
0022     LOG << "JControlEventProcessor::Init" << LOG_END;
0023 
0024     GetApplication()->GetJParameterManager()->SetDefaultParameter("jana:debug_mode", _debug_mode, "Turn on JANA debug mode for janacontrol plugin. (Will require jana-control.py GUI to step through events)");
0025 }
0026 
0027 //-------------------------------------------------------------
0028 // Process
0029 //-------------------------------------------------------------
0030 void JControlEventProcessor::Process(const std::shared_ptr<const JEvent> &event) {
0031 
0032     // If _debug_mode is on then stall here until told to move on to next event
0033     if( _debug_mode ){
0034 
0035         // Serialize threads so setting _wait to true only lets one event through
0036         static std::mutex mtx;
0037         std::lock_guard<std::mutex> lck(mtx);
0038 
0039         // Copy pointer to JEvent to internal member so it can be used to probe
0040         // the event via other methods.
0041         _jevent = event;
0042 
0043         while(_wait && _debug_mode){
0044             std::chrono::milliseconds wait_time {100};
0045             std::this_thread::sleep_for(wait_time);
0046         }
0047         _wait=true; // set up to stall on next event
0048 
0049         // Release shared_ptr to JEvent since we are done with it.
0050         _jevent.reset();
0051     }
0052 
0053     if( _fetch_flag ) FetchObjects( event );
0054  }
0055 
0056 //-------------------------------------------------------------
0057 // Finish
0058 //-------------------------------------------------------------
0059 void JControlEventProcessor::Finish() {
0060     // Close any resources
0061     LOG << "JControlEventProcessor::Finish" << LOG_END;
0062 }
0063 
0064 //-------------------------------------------------------------
0065 // SetDebugMode
0066 //-------------------------------------------------------------
0067 void JControlEventProcessor::SetDebugMode(bool debug_mode){
0068     _debug_mode = debug_mode;
0069     GetApplication()->SetTimeoutEnabled( !_debug_mode );
0070 }
0071 
0072 //-------------------------------------------------------------
0073 // NextEvent
0074 //-------------------------------------------------------------
0075 void JControlEventProcessor::NextEvent(void){
0076     _wait = false;
0077 }
0078 
0079 //-------------------------------------------------------------
0080 // GetObjectStatus
0081 //
0082 // Get count of objects already created for the current event
0083 // for each type of factory.
0084 //-------------------------------------------------------------
0085 void JControlEventProcessor::GetObjectStatus( std::map<JFactorySummary, std::size_t> &factory_object_counts ){
0086 
0087     // bombproof against getting called with no active JEvent
0088     if(_jevent.get() == nullptr ) return;
0089 
0090     // Get list of all factories associated with this event.
0091     // n.b. This will also list objects inserted by the event source
0092     // unlike _jevent->GetJApplication()->GetComponentSummary()
0093     // which only lists registered factories.
0094     auto factories = _jevent->GetAllFactories();
0095     for( auto fac : factories ){
0096         JFactorySummary fac_info;
0097         fac_info.plugin_name = fac->GetPluginName();
0098         fac_info.factory_name = fac->GetFactoryName();
0099         fac_info.factory_tag = fac->GetTag();
0100         fac_info.object_name = fac->GetObjectName();
0101         if(fac_info.factory_name == "") fac_info.factory_name = "JFactoryT<" + fac_info.object_name + ">";
0102         if( fac ) factory_object_counts[fac_info] = fac->GetNumObjects();
0103     }
0104 }
0105 
0106 //-------------------------------------------------------------
0107 // GetObjects
0108 //
0109 // Get objects for the specified factory in the form of strings.
0110 //-------------------------------------------------------------
0111 void JControlEventProcessor::GetObjects(const std::string &/* factory_name */, const std::string &factory_tag, const std::string &object_name, std::map<std::string, JObjectSummary> &objects){
0112     // bombproof against getting called with no active JEvent
0113     if(_jevent.get() == nullptr ) return;
0114 
0115     jstringification->GetObjectSummaries(objects, _jevent, object_name, factory_tag);
0116 }
0117 
0118 //-------------------------------------------------------------
0119 // SetFetch
0120 //
0121 // Set list of factories whose objects should be retrieved and packaged
0122 // into strings during the next event.
0123 //-------------------------------------------------------------
0124 void JControlEventProcessor::SetFetchFactories(std::set<std::string> &factorytags){
0125 
0126     _fetch_object_summaries.clear();
0127     _fetch_metadata.clear();
0128     _fetch_factorytags = factorytags;
0129     _fetch_flag = true;
0130 }
0131 
0132 //-------------------------------------------------------------
0133 // FetchObjects
0134 //
0135 /// Fetch the objects set in the fetch list by an earlier call to
0136 /// SetFetch(). Objects will be retrieved from the given event
0137 /// and JObjectSummary objects created for them and stored in
0138 /// the _fetch_object_summaries container. The _fetch_flag
0139 /// will be set to false once the objects in _fetch_object_summaries
0140 /// are ready.
0141 //-------------------------------------------------------------
0142 void JControlEventProcessor::FetchObjects(std::shared_ptr<const JEvent> jevent){
0143 
0144     _fetch_object_summaries.clear(); // should be redundant with clear in SetFetchFactories()
0145     _fetch_metadata.clear();
0146 
0147     _fetch_metadata["run_number"]   = ToString( GetRunNumber());
0148     _fetch_metadata["event_number"] = ToString( GetEventNumber());
0149 
0150     // Loop over factories set in a previous call to SetFetchFactories()
0151     for( const auto& factorytag : _fetch_factorytags){
0152         // Add entry to _fetch_object_summaries and get reference to it
0153         auto &objects = _fetch_object_summaries[factorytag];
0154 
0155         // Split factorytag string into factory part and tag part
0156         auto pos = factorytag.find(":");
0157         std::string factory_name = factorytag.substr(0, pos);
0158         std::string tag = (pos==std::string::npos ? "":factorytag.substr(pos+1));
0159 
0160         jstringification->GetObjectSummaries(objects, jevent, factory_name, tag);
0161     }
0162 
0163     // All done. Notify JControlZMQ::FetchJANAObjectsJSON
0164     _fetch_flag = false;
0165 }
0166 
0167 //-------------------------------------------------------------
0168 // FetchObjectsNow
0169 //
0170 /// This is a wrapper to FetchObjects that can be called while in
0171 /// debug mode (i.e. when event processing is stalled). It allows
0172 /// JControlZMQ::FetchJANAObjectsJSON to get objects from the
0173 /// current event while in debug_mode. Like FetchObjects(), this
0174 /// assumes SetFetch() has already been called. This will set the
0175 /// _fetch_flag to false when done and should be followed by a
0176 /// call to GetLastFetchResult() to get the actual results.
0177 //-------------------------------------------------------------
0178 void JControlEventProcessor::FetchObjectsNow(void){
0179     // bombproof against getting called with no active JEvent
0180     if(_jevent.get() == nullptr ) return;
0181 
0182     FetchObjects( _jevent );
0183 }
0184 
0185 //-------------------------------------------------------------
0186 // GetRunNumber
0187 //-------------------------------------------------------------
0188 uint32_t JControlEventProcessor::GetRunNumber(void){
0189     if(_jevent.get() == nullptr ) return 0;
0190     return _jevent->GetRunNumber();
0191 }
0192 
0193 //-------------------------------------------------------------
0194 // GetEventNumber
0195 //-------------------------------------------------------------
0196 uint64_t JControlEventProcessor::GetEventNumber(void){
0197     if(_jevent.get() == nullptr ) return 0;
0198     return _jevent->GetEventNumber();
0199 }