Jpp
JDOMSimulator.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <limits>
6 
7 #include "TRandom3.h"
8 #include "JMath/JRandom.hh"
9 
10 #include "Jeep/JParser.hh"
11 #include "Jeep/JProperties.hh"
12 #include "Jeep/JTimer.hh"
13 #include "Jeep/JTimekeeper.hh"
14 #include "JDAQ/JDAQ.hh"
15 #include "JDAQ/JDAQHeader.hh"
16 #include "JDAQ/JDAQTimeslice.hh"
18 #include "JSupport/JSupport.hh"
19 #include "JLang/JException.hh"
20 #include "JLang/JRedirectStream.hh"
22 #include "JIO/JByteArrayIO.hh"
23 #include "JNet/JSocket.hh"
24 #include "JNet/JHostname.hh"
25 #include "JNet/JSocketBlocking.hh"
26 
27 
28 namespace KM3NETDAQ {
29 
30 
31  using namespace JPP;
32 
33 
34  /**
35  * Data structure for configuration of JDOMSimulator.
36  */
37  class JSource :
38  public std::string
39  {
40  public:
41  /**
42  * Default constructor.
43  */
44  JSource() :
45  std::string()
46  {}
47 
48 
49  /**
50  * Read JSource from input stream.
51  *
52  * \param in input stream
53  * \param source JSource
54  * \return input stream
55  */
56  friend inline std::istream& operator>>(std::istream& in, JSource& source)
57  {
58  int index;
59 
60  in >> index >> static_cast<std::string&>(source);
61 
62  return in;
63  }
64 
65 
66  /**
67  * Write JSource to output stream.
68  *
69  * \param out output stream
70  * \param source JSource
71  * \return output stream
72  */
73  friend inline std::ostream& operator<<(std::ostream& out, const JSource& source)
74  {
75  out << static_cast<const std::string&>(source);
76 
77  return out;
78  }
79  };
80 
81 
82  /**
83  * Data structure for configuration of JDataFilter.
84  */
85  class JTarget :
86  public JSocketBlocking
87  {
88  public:
89  /**
90  * Default constructor.
91  */
92  JTarget() :
94  {}
95 
96 
97  /**
98  * Read JTarget from input stream.
99  *
100  * \param in input stream
101  * \param target JTarget
102  * \return input stream
103  */
104  friend inline std::istream& operator>>(std::istream& in, JTarget& target)
105  {
106  using namespace std;
107  using namespace JPP;
108 
109  int index;
110  JHostname hostname;
111 
112  if (in >> index >> hostname) {
113 
114  try {
115 
116  static_cast<JSocketBlocking&>(target) = JSocketBlocking(SOCK_STREAM);
117 
118  target.connect(hostname.hostname, hostname.port);
119 
120  target.setTcpNoDelay (true);
121  target.setReuseAddress(true);
122  target.setKeepAlive (true);
123  target.setReceiveBufferSize(1024);
124  target.setSendBufferSize (1024*1024);
125  //target.setNonBlocking (true);
126  target.setNonBlocking (false);
127  }
128  catch(const JException& error) {
129  cout << error << endl;
130  target.close();
131  }
132  }
133 
134  return in;
135  }
136 
137 
138  /**
139  * Write JTarget to output stream.
140  *
141  * \param out output stream
142  * \param target JTarget
143  * \return output stream
144  */
145  friend inline std::ostream& operator<<(std::ostream& out, const JTarget& target)
146  {
147  using namespace std;
148 
149  out << "TCP no-delay " << target.getTcpNoDelay() << endl;
150  out << "Reuse address " << target.getReuseAddress() << endl;
151  out << "Keep alive " << target.getKeepAlive() << endl;
152  out << "Receive buffer " << target.getReceiveBufferSize() << endl;
153  out << "Send buffer " << target.getSendBufferSize() << endl;
154  out << "Non blocking " << target.getNonBlocking() << endl;
155 
156  return out;
157  }
158  };
159 
160 
161  /**
162  * Runcontrol client to simulate DOM.
163  * In state running, this application will send raw data to the data filters
164  * in a round robin way, based on the frame index.
165  */
167  public JDAQClient
168  {
169  public:
170  /**
171  * Constructor.
172  *
173  * \param name name of client
174  * \param server name of command message server
175  * \param logger pointer to logger
176  * \param level debug level
177  */
178  JDOMSimulator(const std::string& name,
179  const std::string& server,
180  JLogger* logger,
181  const int level) :
182  JDAQClient(name, server, logger, level)
183  {
184  replaceEvent(RC_CMD, RC_DOMSIMULATOR, ev_configure);
185  }
186 
187 
188  virtual void actionConfigure(int length, const char* buffer)
189  {
190  using namespace std;
191  using namespace KM3NETDAQ;
192  using namespace JPP;
193 
194 
196  Long64_t numberOfEvents = 1;
197  int numberOfFrames = numeric_limits<int>::max();
198  double P = 0.0;
199 
200  JProperties properties(JEquationParameters("=", ";", "", ""));
201 
202  properties["source"] = source;
203  properties["target"] = target;
204  properties["inputFile"] = inputFile;
205  properties["numberOfEvents"] = numberOfEvents;
206  properties["numberOfFrames"] = numberOfFrames;
207  properties["probability"] = P;
208 
209  properties.read(string(buffer, length));
210 
211 
212  for (vector<JTarget>::iterator i = target.begin(); i != target.end(); ) {
213  if (i->is_open())
214  ++i;
215  else
216  i = target.erase(i);
217  }
218 
219  if (inputFile.empty()) JErrorStream(logger) << "No input files ";
220  if (target .empty()) JErrorStream(logger) << "No targets";
221 
222  const unsigned int index = distance(source.begin(), find(source.begin(), source.end(), getName()));
223 
224  int number_of_hits = 0;
225  int number_of_errors = 0;
226 
227  if (index < source.size()) {
228 
229  while (inputFile.hasNext()) {
230 
231  JDAQTimeslice* timeslice = inputFile.next();
232 
233  int i1 = (timeslice->size() * (index + 0)) / source.size();
234  int i2 = (timeslice->size() * (index + 1)) / source.size();
235 
236  if (i2 - i1 > numberOfFrames) {
237  i2 = i1 + numberOfFrames;
238  }
239 
240  for (int i = i1; i != i2; ++i) {
241 
242  JDAQSuperFrame& frame = timeslice->at(i);
243 
244  for (JDAQSuperFrame::iterator hit = frame.begin(); hit != frame.end(); ++hit) {
245 
246  ++number_of_hits;
247  /*
248  if (gRandom->Rndm() <= P) {
249 
250  *hit = JDAQHit(32, hit->getT(), hit->getToT());
251 
252  ++number_of_errors;
253  }
254  */
255  if (gRandom->Rndm() <= P) {
256 
257  *hit = JDAQHit(hit->getPMT(), getRandom<JDAQHit::JTDC_t>(), hit->getToT());
258 
259  ++number_of_errors;
260  }
261  }
262  }
263 
264  JDebugStream(logger) << "Processing timeslice: " << inputFile.getCounter() << " [" << i1 << "," << i2 << "]";
265 
266  data.push_back(JTimeslice(i2 - i1));
267 
268  for (int i = i1; i != i2; ++i) {
269  data.rbegin()->at(i - i1) << timeslice->at(i);
270  }
271  }
272 
273  JNoticeStream(logger) << "Number of errors / hits " << number_of_errors << " / " << number_of_hits << " for P = " << P;
274 
275  } else {
276 
277  JErrorStream(logger) << "Source not found in configuration data: " << getName();
278  }
279 
280  setClockInterval((long long int) (1e-3 * getFrameTime()));
281  }
282 
283 
284  virtual void actionReset(int length, const char* buffer)
285  {
286  for (std::vector<JTarget>::iterator i = target.begin(); i != target.end(); ++i)
287  i->close();
288 
289  target.clear();
290  source.clear();
291  data .clear();
292  }
293 
294 
295  virtual void actionQuit(int length, const char* buffer)
296  {
297  actionReset(0, NULL);
298  }
299 
300 
301  virtual void actionStart(int length, const char* buffer)
302  {
303  numberOfSlices = 0;
304  numberOfBytes = 0;
305 
306  data.setRunNumber(getRunNumber());
307  data.reset();
308 
309  timer.reset();
310 
311  resetClock();
312  }
313 
314 
315  virtual void actionStop(int length, const char* buffer)
316  {
317  if (timer.usec_wall > 0) JNoticeStream(logger) << "I/O " << (int) (numberOfBytes / timer.usec_wall) << " MB/s";
318  if (numberOfSlices > 0) JNoticeStream(logger) << "Delay/slice " << (int) (getClockDelay() / numberOfSlices) << " us";
319  }
320 
321 
322  virtual void actionRunning()
323  {
324  if (!data.empty() && !target.empty()) {
325 
326  timer.start();
327 
328  try {
329 
330  JDAQPreamble preamble;
331  JDAQSuperFrameHeader header;
332 
333  for (JTimeslice::const_iterator frame = data->begin(); frame != data->end(); ++frame) {
334 
336 
337  in >> preamble;
338  in >> header;
339 
340  JSocketBlocking& socket = target[header.getFrameIndex() % target.size()];
341 
342  socket.write(frame->data(), frame->size());
343 
344  numberOfBytes += frame->size();
345  }
346 
347  numberOfSlices += 1;
348  }
349  catch(const JException& exception) {
350  JErrorStream(logger) << exception;
351  }
352 
353  data.next();
354 
355  timer.stop();
356  }
357  }
358 
359  private:
362 
363  /**
364  * Memory management for sending of raw data.
365  */
368 
369  class JData :
370  public std::vector<JTimeslice>
371  {
372  public:
373  /**
374  * Default constructor.
375  */
376  JData() :
377  std::vector<JTimeslice>()
378  {}
379 
380 
381  /**
382  * Set run number.
383  *
384  * \param run_number run number
385  */
386  void setRunNumber(int run_number)
387  {
388  JDAQSuperFrameHeader header;
389 
390  for (iterator timeslice = begin(); timeslice != end(); ++timeslice) {
391 
392  for (JTimeslice::iterator frame = timeslice->begin(); frame != timeslice->end(); ++frame) {
393 
395 
397 
398  in >> header;
399 
400  header.setRunNumber(run_number);
401 
402  frame->seekp(JDAQPreamble::sizeOf());
403 
404  *frame << header;
405  }
406  }
407  }
408 
409 
410  /**
411  * Reset internal iterator to begin.
412  */
413  void reset()
414  {
415  page = begin();
416  }
417 
418 
419  /**
420  * Increment internal iterator.
421  * When the internal iterator reaches the end of the data,
422  * the frame indices of the data are increased and
423  * the internal iterator is reset to the begin of data.
424  */
425  void next()
426  {
427  if (page != end() && ++page == end()) {
428 
429  JDAQSuperFrameHeader header;
430 
431  for (iterator timeslice = begin(); timeslice != end(); ++timeslice) {
432 
433  for (JTimeslice::iterator frame = timeslice->begin(); frame != timeslice->end(); ++frame) {
434 
436 
438 
439  in >> header;
440 
441  header.setFrameIndex(header.getFrameIndex() + this->size());
442 
443  frame->seekp(JDAQPreamble::sizeOf());
444 
445  *frame << header;
446  }
447  }
448 
449  reset();
450  }
451  }
452 
453 
454  /**
455  * Smart pointer operator.
456  *
457  * \return current iterator
458  */
459  const_iterator operator->()
460  {
461  return page;
462  }
463 
464 
465  private:
466  JData::const_iterator page;
467  };
468 
469 
471 
472  long long int numberOfSlices; // total number of timeslices
473  long long int numberOfBytes; // total number of bytes
474 
476  };
477 }
478 
479 
480 /**
481  * \file
482  *
483  * Program for real-time simulation of optical modules.
484  * \author rbruijn
485  */
486 int main(int argc, char* argv[])
487 {
488  using namespace std;
489 
490  string server;
491  string logger;
492  string client_name;
493  bool use_cout;
494  int debug;
495 
496  try {
497 
498  JParser<> zap("Program for real-time simulation of optical modules.");
499 
500  zap['H'] = make_field(server) = "localhost";
501  zap['M'] = make_field(logger) = "localhost";
502  zap['u'] = make_field(client_name) = "JDOMSimulator";
503  zap['c'] = make_field(use_cout);
504  zap['d'] = make_field(debug) = 3;
505 
506  zap(argc, argv);
507  }
508  catch(const exception &error) {
509  FATAL(error.what() << endl);
510  }
511 
512 
513  using namespace KM3NETDAQ;
514  using namespace JPP;
515 
516  JLogger* out = NULL;
517 
518  if (use_cout)
519  out = new JStreamLogger(cout);
520  else
521  out = new JControlHostLogger(logger);
522 
523  JDOMSimulator simbad(client_name, server, out, debug);
524 
525  simbad.enter();
526  simbad.run();
527 }
JLOGGER::JDebugStream
Level specific message streamers.
Definition: JMessageStream.hh:113
JException.hh
JLOGGER::JLogger
Interface for logging messages.
Definition: JLogger.hh:22
KM3NETDAQ::JDOMSimulator::data
JData data
Definition: JDOMSimulator.cc:470
JRedirectStream.hh
KM3NETDAQ::JSource
Data structure for configuration of JDOMSimulator.
Definition: JDOMSimulator.cc:37
JLOGGER::JNoticeStream
Definition: JMessageStream.hh:116
JDAQ.hh
KM3NETDAQ::JDOMSimulator::JData
Definition: JDOMSimulator.cc:369
main
int main(int argc, char *argv[])
Definition: JDOMSimulator.cc:486
std::iterator
Definition: JSTDTypes.hh:18
KM3NETDAQ::JDOMSimulator::JData::next
void next()
Increment internal iterator.
Definition: JDOMSimulator.cc:425
KM3NETDAQ::JDOMSimulator::JData::operator->
const_iterator operator->()
Smart pointer operator.
Definition: JDOMSimulator.cc:459
KM3NETDAQ::JSource::operator<<
friend std::ostream & operator<<(std::ostream &out, const JSource &source)
Write JSource to output stream.
Definition: JDOMSimulator.cc:73
JEEP::JTimer
Auxiliary class for CPU timing and usage.
Definition: JTimer.hh:32
JAANET::target
Target.
Definition: JHead.hh:150
KM3NETDAQ::JDOMSimulator::JData::reset
void reset()
Reset internal iterator to begin.
Definition: JDOMSimulator.cc:413
KM3NETDAQ::JTarget::JTarget
JTarget()
Default constructor.
Definition: JDOMSimulator.cc:92
KM3NETDAQ::JDAQSuperFrameHeader::sizeOf
static int sizeOf()
Get size of object.
Definition: JDAQSuperFrameHeader.hh:132
KM3NETDAQ::JDOMSimulator::source
std::vector< JSource > source
Definition: JDOMSimulator.cc:360
JNET::JSocketBlocking::write
int write(const char *buffer, const int length)
Write data to socket.
Definition: JSocketBlocking.hh:78
JLOGGER::JErrorStream
Definition: JMessageStream.hh:115
KM3NETDAQ::JDOMSimulator::actionStop
virtual void actionStop(int length, const char *buffer)
Definition: JDOMSimulator.cc:315
KM3NETDAQ::JDOMSimulator::numberOfSlices
long long int numberOfSlices
Definition: JDOMSimulator.cc:472
JTimekeeper.hh
KM3NETDAQ::JDAQClient::run
void run()
Run as run control client following command messages via JNET::JControlHost.
Definition: JDAQClient.hh:661
std::vector
Definition: JSTDTypes.hh:12
KM3NETDAQ::JDOMSimulator::actionReset
virtual void actionReset(int length, const char *buffer)
Definition: JDOMSimulator.cc:284
KM3NETDAQ::JDOMSimulator::target
std::vector< JTarget > target
Definition: JDOMSimulator.cc:361
KM3NETDAQ::JDAQTimeslice
Data time slice.
Definition: JDAQTimeslice.hh:36
KM3NETDAQ::JDAQChronometer::setRunNumber
void setRunNumber(const int run)
Set run number.
Definition: JDAQChronometer.hh:155
KM3NETDAQ::JDOMSimulator::actionConfigure
virtual void actionConfigure(int length, const char *buffer)
Definition: JDOMSimulator.cc:188
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
KM3NETDAQ::JDAQPreamble::sizeOf
static int sizeOf()
Get size of object.
Definition: JDAQPreamble.hh:110
JEEP::JProperties::read
bool read(const JEquation &equation)
Read equation.
Definition: JProperties.hh:677
JHostname.hh
JDAQTimeslice.hh
KM3NETDAQ::JDOMSimulator::actionRunning
virtual void actionRunning()
This method is repeatedly called when this client machine is in state Running and the clock interval ...
Definition: JDOMSimulator.cc:322
KM3NETDAQ::JDOMSimulator::JTimeslice
std::vector< JFrame > JTimeslice
Definition: JDOMSimulator.cc:367
JNET::JHostname
Auxiliary data structure for hostname and port number.
Definition: JHostname.hh:30
JIO::JByteArrayReader
Byte array binary input.
Definition: JByteArrayIO.hh:25
KM3NETDAQ::getFrameTime
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
JSupport.hh
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JNET::JSocketBlocking
Blocking socket I/O.
Definition: JSocketBlocking.hh:22
JSUPPORT::JMultipleFileScanner::getCounter
counter_type getCounter() const
Get counter.
Definition: JMultipleFileScanner.hh:323
KM3NETDAQ::JDOMSimulator::JDOMSimulator
JDOMSimulator(const std::string &name, const std::string &server, JLogger *logger, const int level)
Constructor.
Definition: JDOMSimulator.cc:178
KM3NETDAQ::JDAQChronometer::setFrameIndex
void setFrameIndex(const int frame_index)
Set frame index.
Definition: JDAQChronometer.hh:166
debug
int debug
debug level
Definition: JSirene.cc:59
JSUPPORT::JMultipleFileScanner::next
virtual const pointer_type & next()
Get next element.
Definition: JMultipleFileScanner.hh:398
KM3NETDAQ::JDOMSimulator::JFrame
JIO::JByteArrayWriter JFrame
Memory management for sending of raw data.
Definition: JDOMSimulator.cc:366
KM3NETDAQ::JDOMSimulator
Runcontrol client to simulate DOM.
Definition: JDOMSimulator.cc:166
JNET::JHostname::port
int port
Definition: JHostname.hh:155
JDAQHeader.hh
KM3NETDAQ::RC_DOMSIMULATOR
static const JNET::JTag RC_DOMSIMULATOR
Definition: JDAQTags.hh:47
KM3NETDAQ::JDAQSuperFrameHeader
DAQ super frame header.
Definition: JDAQSuperFrameHeader.hh:25
KM3NETDAQ::JSource::JSource
JSource()
Default constructor.
Definition: JDOMSimulator.cc:44
JMultipleFileScanner.hh
JSUPPORT::JMultipleFileScanner::hasNext
virtual bool hasNext()
Check availability of next element.
Definition: JMultipleFileScanner.hh:350
JSocketBlocking.hh
KM3NETDAQ::JDAQClient
Control unit client base class.
Definition: JDAQClient.hh:272
JDETECTOR::reset
void reset(JCLBInput &data, size_t size)
Reset CLB buffers.
Definition: JCLBSimulator.hh:41
JParser.hh
KM3NETDAQ::JDOMSimulator::timer
JEEP::JTimer timer
Definition: JDOMSimulator.cc:475
KM3NETDAQ::JDOMSimulator::numberOfBytes
long long int numberOfBytes
Definition: JDOMSimulator.cc:473
KM3NETDAQ::JDOMSimulator::actionQuit
virtual void actionQuit(int length, const char *buffer)
Definition: JDOMSimulator.cc:295
JTRIGGER::JTimeslice::const_iterator
std::vector< value_type >::const_iterator const_iterator
Definition: JTimeslice.hh:34
JByteArrayIO.hh
JLANG::JEquationParameters
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Definition: JEquationParameters.hh:20
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JTRIGGER::JTimeslice::iterator
std::vector< value_type >::iterator iterator
Definition: JTimeslice.hh:33
JSUPPORT::JMultipleFileScanner
General purpose class for object reading from a list of file names.
Definition: JMultipleFileScanner.hh:167
JEEP::JProperties
Utility class to parse parameter values.
Definition: JProperties.hh:496
JROOT::getName
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:45
std
Definition: jaanetDictionary.h:36
KM3NETDAQ::JTarget::operator>>
friend std::istream & operator>>(std::istream &in, JTarget &target)
Read JTarget from input stream.
Definition: JDOMSimulator.cc:104
KM3NETDAQ
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
JSocket.hh
KM3NETDAQ::JDAQSuperFrame
Data frame of one optical module.
Definition: JDAQSuperFrame.hh:27
KM3NETDAQ::JSource::operator>>
friend std::istream & operator>>(std::istream &in, JSource &source)
Read JSource from input stream.
Definition: JDOMSimulator.cc:56
KM3NETDAQ::RC_CMD
static const JNET::JTag RC_CMD
Definition: JDAQTags.hh:44
JTimer.hh
JNET::JHostname::hostname
std::string hostname
Definition: JHostname.hh:154
JProperties.hh
KM3NETDAQ::JDOMSimulator::actionStart
virtual void actionStart(int length, const char *buffer)
Definition: JDOMSimulator.cc:301
KM3NETDAQ::JDAQHit
Hit data structure.
Definition: JDAQHit.hh:40
KM3NETDAQ::JDOMSimulator::JData::page
JData::const_iterator page
Definition: JDOMSimulator.cc:466
KM3NETDAQ::JDAQFrame::begin
const_iterator begin() const
Definition: JDAQFrame.hh:139
KM3NETDAQ::JTarget::operator<<
friend std::ostream & operator<<(std::ostream &out, const JTarget &target)
Write JTarget to output stream.
Definition: JDOMSimulator.cc:145
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
JRandom.hh
KM3NETDAQ::JDAQFrame::end
const_iterator end() const
Definition: JDAQFrame.hh:140
JLANG::JException
General exception.
Definition: JException.hh:40
JIO::JByteArrayReader::seekg
void seekg(const int pos)
Set read position.
Definition: JByteArrayIO.hh:112
KM3NETDAQ::JDAQClient::enter
virtual bool enter(const JArgs &args)
Enter the state machine.
Definition: JDAQClient.hh:363
KM3NETDAQ::JDOMSimulator::JData::JData
JData()
Default constructor.
Definition: JDOMSimulator.cc:376
KM3NETDAQ::JDOMSimulator::JData::setRunNumber
void setRunNumber(int run_number)
Set run number.
Definition: JDOMSimulator.cc:386
KM3NETDAQ::JDAQPreamble
DAQ preamble.
Definition: JDAQPreamble.hh:39
JIO::JByteArrayWriter
Byte array binary output.
Definition: JByteArrayIO.hh:157
JDAQClient.hh
KM3NETDAQ::JTarget
Data structure for configuration of JDataFilter.
Definition: JDOMSimulator.cc:85