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 __COUT__ << "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 __COUT__ << "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 __COUT__ << std::endl;
00043 __COUT__ << std::endl;
00044 __COUT__ << "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 __COUT__ << "\n" << ss.str();
00072 throw std::runtime_error(ss.str());
00073 }
00074
00075
00076 __COUT__ << "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 __COUT__ << 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 __COUT__ << "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 fragment_receiver_ptr_.reset(nullptr);
00152 std::cout << __COUT_HDR_FL__ << "\tNew core" << std::endl;
00153 my_rank = rank;
00154 app_name = name_;
00155 fragment_receiver_ptr_.reset(new artdaq::BoardReaderApp());
00156
00157 uint64_t timeout = 45;
00158
00159 uint64_t timestamp = 184467440737095516;
00160 std::cout << __COUT_HDR_FL__ << "\tInitialize: " << std::endl;
00161 external_request_status_ = fragment_receiver_ptr_->initialize(fhiclConfiguration_, timeout, timestamp);
00162 std::cout << __COUT_HDR_FL__ << "\tDone Initialize" << std::endl;
00163 if (! external_request_status_)
00164 {
00165 report_string_ = "Error initializing ";
00166 report_string_.append(name_ + " ");
00167 report_string_.append("with ParameterSet = \"" + fhiclConfiguration_.to_string() + "\".");
00168 }
00169 std::cout << __COUT_HDR_FL__ << "\tDone Configure" << std::endl;
00170 }
00171
00172
00173 void ARTDAQConsumer::halt(void)
00174 {
00175 std::cout << __COUT_HDR_FL__ << "\tHalt" << std::endl;
00176
00177 uint64_t timeout = 45;
00178
00179 report_string_ = "";
00180 external_request_status_ = fragment_receiver_ptr_->shutdown(timeout);
00181 if (! external_request_status_)
00182 {
00183 report_string_ = "Error shutting down ";
00184 report_string_.append(name_ + ".");
00185 }
00186 }
00187
00188
00189 void ARTDAQConsumer::pauseProcessingData(void)
00190 {
00191 std::cout << __COUT_HDR_FL__ << "\tPause" << std::endl;
00192
00193 uint64_t timeout = 45;
00194 uint64_t timestamp = 184467440737095516;
00195 report_string_ = "";
00196 external_request_status_ = fragment_receiver_ptr_->pause(timeout, timestamp);
00197 if (! external_request_status_)
00198 {
00199 report_string_ = "Error pausing ";
00200 report_string_.append(name_ + ".");
00201 }
00202 }
00203
00204
00205 void ARTDAQConsumer::resumeProcessingData(void)
00206 {
00207 std::cout << __COUT_HDR_FL__ << "\tResume" << std::endl;
00208
00209 uint64_t timeout = 45;
00210 uint64_t timestamp = 184467440737095516;
00211 report_string_ = "";
00212 external_request_status_ = fragment_receiver_ptr_->resume(timeout, timestamp);
00213 if (! external_request_status_)
00214 {
00215 report_string_ = "Error resuming ";
00216 report_string_.append(name_ + ".");
00217 }
00218 }
00219
00220
00221 void ARTDAQConsumer::startProcessingData(std::string runNumber)
00222 {
00223 std::cout << __COUT_HDR_FL__ << "\tStart" << std::endl;
00224
00225 art::RunID runId((art::RunNumber_t)boost::lexical_cast<art::RunNumber_t>(runNumber));
00226
00227
00228 uint64_t timeout = 45;
00229 uint64_t timestamp = 184467440737095516;
00230
00231 report_string_ = "";
00232 std::cout << __COUT_HDR_FL__ << "\tStart run: " << runId << std::endl;
00233 external_request_status_ = fragment_receiver_ptr_->start(runId, timeout, timestamp);
00234 std::cout << __COUT_HDR_FL__ << "\tStart already crashed "<< std::endl;
00235 if (! external_request_status_)
00236 {
00237 report_string_ = "Error starting ";
00238 report_string_.append(name_ + " ");
00239 report_string_.append("for run number ");
00240 report_string_.append(boost::lexical_cast<std::string>(runId.run()));
00241 report_string_.append(", timeout ");
00242 report_string_.append(boost::lexical_cast<std::string>(timeout));
00243 report_string_.append(", timestamp ");
00244 report_string_.append(boost::lexical_cast<std::string>(timestamp));
00245 report_string_.append(".");
00246 }
00247
00248 std::cout << __COUT_HDR_FL__ << "STARTING BOARD READER THREAD" << std::endl;
00249 }
00250
00251
00252 void ARTDAQConsumer::stopProcessingData(void)
00253 {
00254 std::cout << __COUT_HDR_FL__ << "\tStop" << std::endl;
00255
00256 uint64_t timeout = 45;
00257 uint64_t timestamp = 184467440737095516;
00258 report_string_ = "";
00259 external_request_status_ = fragment_receiver_ptr_->stop(timeout, timestamp);
00260 if (! external_request_status_)
00261 {
00262 report_string_ = "Error stopping ";
00263 report_string_.append(name_ + ".");
00264
00265 }
00266 }
00267
00268 DEFINE_OTS_PROCESSOR(ARTDAQConsumer)