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