00001 #include "otsdaq-core/DataProcessorPlugins/ARTDAQConsumer.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004 #include "artdaq/Application/Commandable.hh"
00005 #include "otsdaq-core/Macros/ProcessorPluginMacros.h"
00006 #include "fhiclcpp/make_ParameterSet.h"
00007 #include "otsdaq-core/DataManager/DataManagerSingleton.h"
00008 #include "otsdaq-core/DataManager/DataManager.h"
00009
00010
00011 #include <fstream>
00012 #include <iostream>
00013 #include <cstdint>
00014 #include <set>
00015
00016 using namespace ots;
00017
00018 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
00019 #define ARTDAQ_FILE_PREAMBLE "boardReader"
00020
00021
00022 ARTDAQConsumer::ARTDAQConsumer (std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
00023 : WorkLoop (processorUID)
00024 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, LowConsumerPriority)
00025 , Configurable (theXDAQContextConfigTree, configurationPath)
00026 {
00027 __MOUT__ << "ARTDAQ CONSUMER CONSTRUCTOR!!!" << std::endl;
00028
00029
00030
00031 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE + "-";
00032 std::string uid = theXDAQContextConfigTree.getNode(configurationPath).getValue();
00033
00034 __MOUT__ << "uid: " << uid << std::endl;
00035 for(unsigned int i=0;i<uid.size();++i)
00036 if((uid[i] >= 'a' && uid[i] <= 'z') ||
00037 (uid[i] >= 'A' && uid[i] <= 'Z') ||
00038 (uid[i] >= '0' && uid[i] <= '9'))
00039 filename += uid[i];
00040 filename += ".fcl";
00041
00042 __MOUT__ << std::endl;
00043 __MOUT__ << std::endl;
00044 __MOUT__ << "filename: " << filename << std::endl;
00045
00046 std::string fileFclString;
00047 {
00048 std::ifstream in(filename, std::ios::in | std::ios::binary);
00049 if (in)
00050 {
00051 std::string contents;
00052 in.seekg(0, std::ios::end);
00053 fileFclString.resize(in.tellg());
00054 in.seekg(0, std::ios::beg);
00055 in.read(&fileFclString[0], fileFclString.size());
00056 in.close();
00057 }
00058 }
00059
00060
00061
00062
00063
00064
00065
00066 size_t fcli = fileFclString.find("fragment_receiver: {") +
00067 +strlen("fragment_receiver: {");
00068 if(fcli == std::string::npos)
00069 {
00070 __SS__ << "Could not find 'fragment_receiver: {' in Board Reader fcl string!" << std::endl;
00071 __MOUT__ << "\n" << ss.str();
00072 throw std::runtime_error(ss.str());
00073 }
00074
00075
00076 __MOUT__ << "configurationPath " << configurationPath << std::endl;
00077
00078 std::string consumerID, bufferID, appID;
00079 unsigned int backSteps;
00080 size_t backi = -1, backj;
00081 backSteps = 7;
00082 for(unsigned int i=0; i<backSteps; i++)
00083 {
00084
00085
00086 backj = backi;
00087 backi = configurationPath.rfind('/',backi-1);
00088
00089
00090
00091
00092 if(i+1 == 2)
00093 consumerID = configurationPath.substr(backi+1,backj-backi-1);
00094 else if(i+1 == 4)
00095 bufferID = configurationPath.substr(backi+1,backj-backi-1);
00096 else if(i+1 == 7)
00097 appID = configurationPath.substr(backi+1,backj-backi-1);
00098 }
00099
00100
00101 fileFclString = fileFclString.substr(0,fcli) + "\n\t\t" +
00102 "SupervisorApplicationUID: \"" + appID + "\"\n\t\t" +
00103 "BufferUID: \"" + bufferID + "\"\n\t\t" +
00104 "ProcessorUID: \"" + consumerID + "\"\n" +
00105 fileFclString.substr(fcli);
00106
00107 __MOUT__ << fileFclString << std::endl;
00108
00109 fhicl::make_ParameterSet(fileFclString, fhiclConfiguration_);
00110
00111
00112
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 ARTDAQConsumer::~ARTDAQConsumer(void)
00124 {
00125 halt();
00126 __MOUT__ << "DONE DELETING!" << std::endl;
00127 }
00128
00129
00130 void ARTDAQConsumer::initLocalGroup(int rank)
00131 {
00132 name_ = "BoardReader_" + DataConsumer::processorUID_;
00133 configure(rank);
00134 }
00135
00136 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
00137 #define ARTDAQ_FILE_PREAMBLE "boardReader"
00138
00139
00140 void ARTDAQConsumer::configure(int rank)
00141 {
00142 std::cout << __COUT_HDR_FL__ << "\tConfigure" << std::endl;
00143
00144 report_string_ = "";
00145 external_request_status_ = true;
00146
00147
00148
00149
00150
00151 artdaq::Commandable tmpCommandable;
00152 fragment_receiver_ptr_.reset(nullptr);
00153 std::cout << __COUT_HDR_FL__ << "\tNew core" << std::endl;
00154 fragment_receiver_ptr_.reset(new artdaq::BoardReaderCore(tmpCommandable, rank, name_));
00155
00156 uint64_t timeout = 45;
00157
00158 uint64_t timestamp = 184467440737095516;
00159 std::cout << __COUT_HDR_FL__ << "\tInitialize: " << std::endl;
00160 external_request_status_ = fragment_receiver_ptr_->initialize(fhiclConfiguration_, timeout, timestamp);
00161 std::cout << __COUT_HDR_FL__ << "\tDone Initialize" << std::endl;
00162 if (! external_request_status_)
00163 {
00164 report_string_ = "Error initializing ";
00165 report_string_.append(name_ + " ");
00166 report_string_.append("with ParameterSet = \"" + fhiclConfiguration_.to_string() + "\".");
00167 }
00168 std::cout << __COUT_HDR_FL__ << "\tDone Configure" << std::endl;
00169 }
00170
00171
00172 void ARTDAQConsumer::halt(void)
00173 {
00174 std::cout << __COUT_HDR_FL__ << "\tHalt" << std::endl;
00175
00176 uint64_t timeout = 45;
00177
00178 report_string_ = "";
00179 external_request_status_ = fragment_receiver_ptr_->shutdown(timeout);
00180 if (! external_request_status_)
00181 {
00182 report_string_ = "Error shutting down ";
00183 report_string_.append(name_ + ".");
00184 }
00185 }
00186
00187
00188 void ARTDAQConsumer::pauseProcessingData(void)
00189 {
00190 std::cout << __COUT_HDR_FL__ << "\tPause" << std::endl;
00191
00192 uint64_t timeout = 45;
00193 uint64_t timestamp = 184467440737095516;
00194 report_string_ = "";
00195 external_request_status_ = fragment_receiver_ptr_->pause(timeout, timestamp);
00196 if (! external_request_status_)
00197 {
00198 report_string_ = "Error pausing ";
00199 report_string_.append(name_ + ".");
00200 }
00201
00202 if (fragment_processing_future_.valid())
00203 {
00204 int number_of_fragments_sent = fragment_processing_future_.get();
00205 mf::LogDebug(name_+"App::do_pause(uint64_t, uint64_t)")
00206 << "Number of fragments sent = " << number_of_fragments_sent
00207 << ".";
00208 }
00209 }
00210
00211
00212 void ARTDAQConsumer::resumeProcessingData(void)
00213 {
00214 std::cout << __COUT_HDR_FL__ << "\tResume" << std::endl;
00215
00216 uint64_t timeout = 45;
00217 uint64_t timestamp = 184467440737095516;
00218 report_string_ = "";
00219 external_request_status_ = fragment_receiver_ptr_->resume(timeout, timestamp);
00220 if (! external_request_status_)
00221 {
00222 report_string_ = "Error resuming ";
00223 report_string_.append(name_ + ".");
00224 }
00225
00226 fragment_processing_future_ = std::async(std::launch::async, &artdaq::BoardReaderCore::process_fragments, fragment_receiver_ptr_.get());
00227
00228 }
00229
00230
00231 void ARTDAQConsumer::startProcessingData(std::string runNumber)
00232 {
00233 std::cout << __COUT_HDR_FL__ << "\tStart" << std::endl;
00234
00235 art::RunID runId((art::RunNumber_t)boost::lexical_cast<art::RunNumber_t>(runNumber));
00236
00237
00238 uint64_t timeout = 45;
00239 uint64_t timestamp = 184467440737095516;
00240
00241 report_string_ = "";
00242 std::cout << __COUT_HDR_FL__ << "\tStart run: " << runId << std::endl;
00243 external_request_status_ = fragment_receiver_ptr_->start(runId, timeout, timestamp);
00244 std::cout << __COUT_HDR_FL__ << "\tStart already crashed "<< std::endl;
00245 if (! external_request_status_)
00246 {
00247 report_string_ = "Error starting ";
00248 report_string_.append(name_ + " ");
00249 report_string_.append("for run number ");
00250 report_string_.append(boost::lexical_cast<std::string>(runId.run()));
00251 report_string_.append(", timeout ");
00252 report_string_.append(boost::lexical_cast<std::string>(timeout));
00253 report_string_.append(", timestamp ");
00254 report_string_.append(boost::lexical_cast<std::string>(timestamp));
00255 report_string_.append(".");
00256 }
00257
00258 std::cout << __COUT_HDR_FL__ << "STARTING BOARD READER THREAD" << std::endl;
00259 fragment_processing_future_ = std::async(std::launch::async, &artdaq::BoardReaderCore::process_fragments, fragment_receiver_ptr_.get());
00260
00261 }
00262
00263
00264 void ARTDAQConsumer::stopProcessingData(void)
00265 {
00266 std::cout << __COUT_HDR_FL__ << "\tStop" << std::endl;
00267
00268 uint64_t timeout = 45;
00269 uint64_t timestamp = 184467440737095516;
00270 report_string_ = "";
00271 external_request_status_ = fragment_receiver_ptr_->stop(timeout, timestamp);
00272 if (! external_request_status_)
00273 {
00274 report_string_ = "Error stopping ";
00275 report_string_.append(name_ + ".");
00276
00277 }
00278
00279 if (fragment_processing_future_.valid())
00280 {
00281 int number_of_fragments_sent = fragment_processing_future_.get();
00282 mf::LogDebug(name_ + "App::do_stop(uint64_t, uint64_t)")
00283 << "Number of fragments sent = " << number_of_fragments_sent
00284 << ".";
00285 }
00286
00287 }
00288
00289 DEFINE_OTS_PROCESSOR(ARTDAQConsumer)