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