otsdaq  v2_00_00
ARTDAQConsumer_processor.cc
1 #include "otsdaq-core/DataProcessorPlugins/ARTDAQConsumer.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
4 #include "artdaq/Application/Commandable.hh"
5 #include "otsdaq-core/Macros/ProcessorPluginMacros.h"
6 #include "fhiclcpp/make_ParameterSet.h"
7 #include "otsdaq-core/DataManager/DataManagerSingleton.h"
8 #include "otsdaq-core/DataManager/DataManager.h"
9 
10 
11 #include <fstream>
12 #include <iostream>
13 #include <cstdint>
14 #include <set>
15 
16 using namespace ots;
17 
18 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
19 #define ARTDAQ_FILE_PREAMBLE "boardReader"
20 
21 //========================================================================================================================
22 ARTDAQConsumer::ARTDAQConsumer (std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
23 : WorkLoop (processorUID)
24 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, LowConsumerPriority)
25 , Configurable (theXDAQContextConfigTree, configurationPath)
26 {
27  __COUT__ << "ARTDAQ CONSUMER CONSTRUCTOR!!!" << std::endl;
28  //__COUT__ << "Configuration string:-" << theXDAQContextConfigTree.getNode(configurationPath).getNode("ConfigurationString").getValue<std::string>() << "-" << std::endl;
29 
30 
31  std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE + "-";
32  std::string uid = theXDAQContextConfigTree.getNode(configurationPath).getValue();
33 
34  __COUT__ << "uid: " << uid << std::endl;
35  for(unsigned int i=0;i<uid.size();++i)
36  if((uid[i] >= 'a' && uid[i] <= 'z') ||
37  (uid[i] >= 'A' && uid[i] <= 'Z') ||
38  (uid[i] >= '0' && uid[i] <= '9')) //only allow alpha numeric in file name
39  filename += uid[i];
40  filename += ".fcl";
41 
42  __COUT__ << std::endl;
43  __COUT__ << std::endl;
44  __COUT__ << "filename: " << filename << std::endl;
45 
46  std::string fileFclString;
47  {
48  std::ifstream in(filename, std::ios::in | std::ios::binary);
49  if (in)
50  {
51  std::string contents;
52  in.seekg(0, std::ios::end);
53  fileFclString.resize(in.tellg());
54  in.seekg(0, std::ios::beg);
55  in.read(&fileFclString[0], fileFclString.size());
56  in.close();
57  }
58  }
59  //__COUT__ << fileFclString << std::endl;
60 
61  //find fragment_receiver {
62  // and insert e.g.,
63  // SupervisorApplicationUID:"ARTDataManager0"
64  // BufferUID:"ART_S0_DM0_DataBuffer0"
65  // ProcessorUID:"ART_S0_DM0_DB0_ARTConsumer0"
66  size_t fcli = fileFclString.find("fragment_receiver: {") +
67  +strlen("fragment_receiver: {");
68  if(fcli == std::string::npos)
69  {
70  __SS__ << "Could not find 'fragment_receiver: {' in Board Reader fcl string!" << std::endl;
71  __COUT__ << "\n" << ss.str();
72  throw std::runtime_error(ss.str());
73  }
74 
75  //get the parent IDs from configurationPath
76  __COUT__ << "configurationPath " << configurationPath << std::endl;
77 
78  std::string consumerID, bufferID, appID;
79  unsigned int backSteps; //at 2, 4, and 7 are the important parent IDs
80  size_t backi = -1, backj;
81  backSteps = 7;
82  for(unsigned int i=0; i<backSteps; i++)
83  {
84  //__COUT__ << "backsteps: " << i+1 << std::endl;
85 
86  backj = backi;
87  backi = configurationPath.rfind('/',backi-1);
88 
89  //__COUT__ << "backi:" << backi << " backj:" << backj << std::endl;
90  //__COUT__ << "substr: " << configurationPath.substr(backi+1,backj-backi-1) << std::endl;
91 
92  if(i+1 == 2)
93  consumerID = configurationPath.substr(backi+1,backj-backi-1);
94  else if(i+1 == 4)
95  bufferID = configurationPath.substr(backi+1,backj-backi-1);
96  else if(i+1 == 7)
97  appID = configurationPath.substr(backi+1,backj-backi-1);
98  }
99 
100  //insert parent IDs into fcl string
101  fileFclString = fileFclString.substr(0,fcli) + "\n\t\t" +
102  "SupervisorApplicationUID: \"" + appID + "\"\n\t\t" +
103  "BufferUID: \"" + bufferID + "\"\n\t\t" +
104  "ProcessorUID: \"" + consumerID + "\"\n" +
105  fileFclString.substr(fcli);
106 
107  __COUT__ << fileFclString << std::endl;
108 
109  fhicl::make_ParameterSet(fileFclString, fhiclConfiguration_);
110 
111 
112  //fhicl::make_ParameterSet(theXDAQContextConfigTree.getNode(configurationPath).getNode("ConfigurationString").getValue<std::string>(), fhiclConfiguration_);
113 }
114 
115 //========================================================================================================================
116 //ARTDAQConsumer::ARTDAQConsumer(std::string interfaceID, MPI_Comm local_group_comm, std::string name)
117 //:FEVInterface (feId, 0)
118 //,local_group_comm_(local_group_comm)
119 //,name_ (name)
120 //{}
121 
122 //========================================================================================================================
123 ARTDAQConsumer::~ARTDAQConsumer(void)
124 {
125  halt();
126  __COUT__ << "DONE DELETING!" << std::endl;
127 }
128 
129 //========================================================================================================================
130 void ARTDAQConsumer::initLocalGroup(int rank)
131 {
132  name_ = "BoardReader_" + DataConsumer::processorUID_;
133  configure(rank);
134 }
135 
136 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
137 #define ARTDAQ_FILE_PREAMBLE "boardReader"
138 
139 //========================================================================================================================
140 void ARTDAQConsumer::configure(int rank)
141 {
142  std::cout << __COUT_HDR_FL__ << "\tConfigure" << std::endl;
143 
144  report_string_ = "";
145  external_request_status_ = true;
146 
147  // in the following block, we first destroy the existing BoardReader
148  // instance, then create a new one. Doing it in one step does not
149  // produce the desired result since that creates a new instance and
150  // then deletes the old one, and we need the opposite order.
151  fragment_receiver_ptr_.reset(nullptr);
152  std::cout << __COUT_HDR_FL__ << "\tNew core" << std::endl;
153  my_rank = rank;
154  app_name = name_;
155  fragment_receiver_ptr_.reset(new artdaq::BoardReaderApp());
156  //FIXME These are passed as parameters
157  uint64_t timeout = 45;
158  //uint64_t timestamp = 184467440737095516;
159  uint64_t timestamp = 184467440737095516;
160  std::cout << __COUT_HDR_FL__ << "\tInitialize: " << std::endl;//<< fhiclConfiguration_.to_string() << std::endl;
161  external_request_status_ = fragment_receiver_ptr_->initialize(fhiclConfiguration_, timeout, timestamp);
162  std::cout << __COUT_HDR_FL__ << "\tDone Initialize" << std::endl;
163  if (! external_request_status_)
164  {
165  report_string_ = "Error initializing ";
166  report_string_.append(name_ + " ");
167  report_string_.append("with ParameterSet = \"" + fhiclConfiguration_.to_string() + "\".");
168  }
169  std::cout << __COUT_HDR_FL__ << "\tDone Configure" << std::endl;
170 }
171 
172 //========================================================================================================================
173 void ARTDAQConsumer::halt(void)
174 {
175  std::cout << __COUT_HDR_FL__ << "\tHalt" << std::endl;
176  //FIXME These are passed as parameters
177  uint64_t timeout = 45;
178  //uint64_t timestamp = 184467440737095516;
179  report_string_ = "";
180  external_request_status_ = fragment_receiver_ptr_->shutdown(timeout);
181  if (! external_request_status_)
182  {
183  report_string_ = "Error shutting down ";
184  report_string_.append(name_ + ".");
185  }
186 }
187 
188 //========================================================================================================================
189 void ARTDAQConsumer::pauseProcessingData(void)
190 {
191  std::cout << __COUT_HDR_FL__ << "\tPause" << std::endl;
192  //FIXME These are passed as parameters
193  uint64_t timeout = 45;
194  uint64_t timestamp = 184467440737095516;
195  report_string_ = "";
196  external_request_status_ = fragment_receiver_ptr_->pause(timeout, timestamp);
197  if (! external_request_status_)
198  {
199  report_string_ = "Error pausing ";
200  report_string_.append(name_ + ".");
201  }
202 }
203 
204 //========================================================================================================================
205 void ARTDAQConsumer::resumeProcessingData(void)
206 {
207  std::cout << __COUT_HDR_FL__ << "\tResume" << std::endl;
208  //FIXME These are passed as parameters
209  uint64_t timeout = 45;
210  uint64_t timestamp = 184467440737095516;
211  report_string_ = "";
212  external_request_status_ = fragment_receiver_ptr_->resume(timeout, timestamp);
213  if (! external_request_status_)
214  {
215  report_string_ = "Error resuming ";
216  report_string_.append(name_ + ".");
217  }
218 }
219 
220 //========================================================================================================================
221 void ARTDAQConsumer::startProcessingData(std::string runNumber)
222 {
223  std::cout << __COUT_HDR_FL__ << "\tStart" << std::endl;
224 
225  art::RunID runId((art::RunNumber_t)boost::lexical_cast<art::RunNumber_t>(runNumber));
226 
227  //FIXME These are passed as parameters
228  uint64_t timeout = 45;
229  uint64_t timestamp = 184467440737095516;
230 
231  report_string_ = "";
232  std::cout << __COUT_HDR_FL__ << "\tStart run: " << runId << std::endl;
233  external_request_status_ = fragment_receiver_ptr_->start(runId, timeout, timestamp);
234  std::cout << __COUT_HDR_FL__ << "\tStart already crashed "<< std::endl;
235  if (! external_request_status_)
236  {
237  report_string_ = "Error starting ";
238  report_string_.append(name_ + " ");
239  report_string_.append("for run number ");
240  report_string_.append(boost::lexical_cast<std::string>(runId.run()));
241  report_string_.append(", timeout ");
242  report_string_.append(boost::lexical_cast<std::string>(timeout));
243  report_string_.append(", timestamp ");
244  report_string_.append(boost::lexical_cast<std::string>(timestamp));
245  report_string_.append(".");
246  }
247 
248  std::cout << __COUT_HDR_FL__ << "STARTING BOARD READER THREAD" << std::endl;
249 }
250 
251 //========================================================================================================================
252 void ARTDAQConsumer::stopProcessingData(void)
253 {
254  std::cout << __COUT_HDR_FL__ << "\tStop" << std::endl;
255  //FIXME These are passed as parameters
256  uint64_t timeout = 45;
257  uint64_t timestamp = 184467440737095516;
258  report_string_ = "";
259  external_request_status_ = fragment_receiver_ptr_->stop(timeout, timestamp);
260  if (! external_request_status_)
261  {
262  report_string_ = "Error stopping ";
263  report_string_.append(name_ + ".");
264  //return false;
265  }
266 }
267 
268 DEFINE_OTS_PROCESSOR(ARTDAQConsumer)