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"
15 #include "JDAQ/JDAQHeaderIO.hh"
16 #include "JDAQ/JDAQTimesliceIO.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(hit->getPMT(), getRandom<JDAQHit::JTDC_t>(), hit->getToT());
251 
252  ++number_of_errors;
253  }
254  }
255  }
256 
257  JDebugStream(logger) << "Processing timeslice: " << inputFile.getCounter() << " [" << i1 << "," << i2 << "]";
258 
259  data.push_back(JTimeslice(i2 - i1));
260 
261  for (int i = i1; i != i2; ++i) {
262  data.rbegin()->at(i - i1) << timeslice->at(i);
263  }
264  }
265 
266  JNoticeStream(logger) << "Number of errors / hits " << number_of_errors << " / " << number_of_hits << " for P = " << P;
267 
268  } else {
269 
270  JErrorStream(logger) << "Source not found in configuration data: " << getName();
271  }
272 
273  setClockInterval((long long int) (1e-3 * getFrameTime()));
274  }
275 
276 
277  virtual void actionReset(int length, const char* buffer)
278  {
279  for (std::vector<JTarget>::iterator i = target.begin(); i != target.end(); ++i)
280  i->close();
281 
282  target.clear();
283  source.clear();
284  data .clear();
285  }
286 
287 
288  virtual void actionQuit(int length, const char* buffer)
289  {
290  actionReset(0, NULL);
291  }
292 
293 
294  virtual void actionStart(int length, const char* buffer)
295  {
296  numberOfSlices = 0;
297  numberOfBytes = 0;
298 
299  data.setRunNumber(getRunNumber());
300  data.reset();
301 
302  timer.reset();
303 
304  resetClock();
305  }
306 
307 
308  virtual void actionStop(int length, const char* buffer)
309  {
310  if (timer.usec_wall > 0) JNoticeStream(logger) << "I/O " << (int) (numberOfBytes / timer.usec_wall) << " MB/s";
311  if (numberOfSlices > 0) JNoticeStream(logger) << "Delay/slice " << (int) (getClockDelay() / numberOfSlices) << " us";
312  }
313 
314 
315  virtual void actionRunning()
316  {
317  if (!data.empty() && !target.empty()) {
318 
319  timer.start();
320 
321  try {
322 
323  JDAQPreamble preamble;
324  JDAQSuperFrameHeader header;
325 
326  for (JTimeslice::const_iterator frame = data->begin(); frame != data->end(); ++frame) {
327 
329 
330  in >> preamble;
331  in >> header;
332 
333  JSocketBlocking& socket = target[header.getFrameIndex() % target.size()];
334 
335  socket.write(frame->data(), frame->size());
336 
337  numberOfBytes += frame->size();
338  }
339 
340  numberOfSlices += 1;
341  }
342  catch(const JException& exception) {
343  JErrorStream(logger) << exception;
344  }
345 
346  data.next();
347 
348  timer.stop();
349  }
350  }
351 
352  private:
355 
356  /**
357  * Memory management for sending of raw data.
358  */
361 
362  class JData :
363  public std::vector<JTimeslice>
364  {
365  public:
366  /**
367  * Default constructor.
368  */
369  JData() :
370  std::vector<JTimeslice>()
371  {}
372 
373 
374  /**
375  * Set run number.
376  *
377  * \param run_number run number
378  */
379  void setRunNumber(int run_number)
380  {
381  JDAQSuperFrameHeader header;
382 
383  for (iterator timeslice = begin(); timeslice != end(); ++timeslice) {
384 
385  for (JTimeslice::iterator frame = timeslice->begin(); frame != timeslice->end(); ++frame) {
386 
388 
390 
391  in >> header;
392 
393  header.setRunNumber(run_number);
394 
395  frame->seekp(getSizeof<JDAQPreamble>());
396 
397  *frame << header;
398  }
399  }
400  }
401 
402 
403  /**
404  * Reset internal iterator to begin.
405  */
406  void reset()
407  {
408  page = begin();
409  }
410 
411 
412  /**
413  * Increment internal iterator.
414  * When the internal iterator reaches the end of the data,
415  * the frame indices of the data are increased and
416  * the internal iterator is reset to the begin of data.
417  */
418  void next()
419  {
420  if (page != end() && ++page == end()) {
421 
422  JDAQSuperFrameHeader header;
423 
424  for (iterator timeslice = begin(); timeslice != end(); ++timeslice) {
425 
426  for (JTimeslice::iterator frame = timeslice->begin(); frame != timeslice->end(); ++frame) {
427 
429 
431 
432  in >> header;
433 
434  header.setFrameIndex(header.getFrameIndex() + this->size());
435 
436  frame->seekp(getSizeof<JDAQPreamble>());
437 
438  *frame << header;
439  }
440  }
441 
442  reset();
443  }
444  }
445 
446 
447  /**
448  * Smart pointer operator.
449  *
450  * \return current iterator
451  */
452  const_iterator operator->()
453  {
454  return page;
455  }
456 
457 
458  private:
459  JData::const_iterator page;
460  };
461 
462 
464 
465  long long int numberOfSlices; // total number of timeslices
466  long long int numberOfBytes; // total number of bytes
467 
469  };
470 }
471 
472 
473 /**
474  * \file
475  *
476  * Program for real-time simulation of optical modules.
477  * \author rbruijn
478  */
479 int main(int argc, char* argv[])
480 {
481  using namespace std;
482 
483  string server;
484  string logger;
485  string client_name;
486  bool use_cout;
487  int debug;
488 
489  try {
490 
491  JParser<> zap("Program for real-time simulation of optical modules.");
492 
493  zap['H'] = make_field(server) = "localhost";
494  zap['M'] = make_field(logger) = "localhost";
495  zap['u'] = make_field(client_name) = "JDOMSimulator";
496  zap['c'] = make_field(use_cout);
497  zap['d'] = make_field(debug) = 3;
498 
499  zap(argc, argv);
500  }
501  catch(const exception &error) {
502  FATAL(error.what() << endl);
503  }
504 
505 
506  using namespace KM3NETDAQ;
507  using namespace JPP;
508 
509  JLogger* out = NULL;
510 
511  if (use_cout)
512  out = new JStreamLogger(cout);
513  else
514  out = new JControlHostLogger(logger);
515 
516  JDOMSimulator simbad(client_name, server, out, debug);
517 
518  simbad.enter();
519  simbad.run();
520 }
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:463
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:362
main
int main(int argc, char *argv[])
Definition: JDOMSimulator.cc:479
std::iterator
Definition: JSTDTypes.hh:18
KM3NETDAQ::JDOMSimulator::JData::next
void next()
Increment internal iterator.
Definition: JDOMSimulator.cc:418
KM3NETDAQ::JDOMSimulator::JData::operator->
const_iterator operator->()
Smart pointer operator.
Definition: JDOMSimulator.cc:452
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:406
KM3NETDAQ::JTarget::JTarget
JTarget()
Default constructor.
Definition: JDOMSimulator.cc:92
KM3NETDAQ::JDOMSimulator::source
std::vector< JSource > source
Definition: JDOMSimulator.cc:353
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:308
KM3NETDAQ::JDOMSimulator::numberOfSlices
long long int numberOfSlices
Definition: JDOMSimulator.cc:465
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:277
KM3NETDAQ::JDOMSimulator::target
std::vector< JTarget > target
Definition: JDOMSimulator.cc:354
KM3NETDAQ::JDAQTimeslice
Data time slice.
Definition: JDAQTimeslice.hh:30
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
JEEP::JProperties::read
bool read(const JEquation &equation)
Read equation.
Definition: JProperties.hh:677
JDAQHeaderIO.hh
JHostname.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:315
KM3NETDAQ::JDOMSimulator::JTimeslice
std::vector< JFrame > JTimeslice
Definition: JDOMSimulator.cc:360
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
JDAQTimesliceIO.hh
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:359
KM3NETDAQ::JDOMSimulator
Runcontrol client to simulate DOM.
Definition: JDOMSimulator.cc:166
JNET::JHostname::port
int port
Definition: JHostname.hh:155
JLOGGER::JStreamLogger
Message logging based on std::ostream.
Definition: JStreamLogger.hh:22
KM3NETDAQ::RC_DOMSIMULATOR
static const JNET::JTag RC_DOMSIMULATOR
Definition: JDAQTags.hh:47
KM3NETDAQ::JDAQSuperFrameHeader
DAQ super frame header.
Definition: JDAQSuperFrameHeader.hh:19
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
JLOGGER::JControlHostLogger
Message logging based on ControlHost.
Definition: JControlHostLogger.hh:26
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:468
KM3NETDAQ::JDOMSimulator::numberOfBytes
long long int numberOfBytes
Definition: JDOMSimulator.cc:466
KM3NETDAQ::JDOMSimulator::actionQuit
virtual void actionQuit(int length, const char *buffer)
Definition: JDOMSimulator.cc:288
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
KM3NETDAQ::getSizeof< JDAQSuperFrameHeader >
size_t getSizeof< JDAQSuperFrameHeader >()
Get size of type.
Definition: JDAQSuperFrameHeaderIO.hh:24
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:22
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:294
KM3NETDAQ::getSizeof< JDAQPreamble >
size_t getSizeof< JDAQPreamble >()
Get size of type.
Definition: JDAQPreambleIO.hh:19
KM3NETDAQ::JDAQHit
Hit data structure.
Definition: JDAQHit.hh:34
KM3NETDAQ::JDOMSimulator::JData::page
JData::const_iterator page
Definition: JDOMSimulator.cc:459
KM3NETDAQ::JDAQFrame::begin
const_iterator begin() const
Definition: JDAQFrame.hh:136
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:137
JLANG::JException
General exception.
Definition: JException.hh:23
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:369
KM3NETDAQ::JDOMSimulator::JData::setRunNumber
void setRunNumber(int run_number)
Set run number.
Definition: JDOMSimulator.cc:379
KM3NETDAQ::JDAQPreamble
DAQ preamble.
Definition: JDAQPreamble.hh:26
JIO::JByteArrayWriter
Byte array binary output.
Definition: JByteArrayIO.hh:157
JDAQClient.hh
KM3NETDAQ::JTarget
Data structure for configuration of JDataFilter.
Definition: JDOMSimulator.cc:85