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