Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 08:58:15

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