Jpp  17.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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) override
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) override
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) override
289  {
290  actionReset(0, NULL);
291  }
292 
293 
294  virtual void actionStart(int length, const char* buffer) override
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) override
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() override
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 
389  in.seekg(getSizeof<JDAQPreamble>());
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 
430  in.seekg(getSizeof<JDAQPreamble>());
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 }
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:207
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
std::vector< value_type >::const_iterator const_iterator
Definition: JTimeslice.hh:34
int getSendBufferSize() const
Get send buffer size.
Definition: JSocket.hh:284
bool read(const JEquation &equation)
Read equation.
Definition: JProperties.hh:677
Exceptions.
int main(int argc, char *argv[])
Definition: Main.cc:15
Target.
Definition: JHead.hh:296
ROOT TTree parameter settings of various packages.
bool getReuseAddress() const
Get reuse address.
Definition: JSocket.hh:218
JTarget()
Default constructor.
void close()
Close file.
Definition: JFile.hh:55
void setRunNumber(int run_number)
Set run number.
JData()
Default constructor.
int write(const char *buffer, const int length)
Write data to socket.
virtual void actionQuit(int length, const char *buffer) override
std::vector< JFrame > JTimeslice
Message logging based on std::ostream.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
void setSendBufferSize(const int size)
Set send buffer size.
Definition: JSocket.hh:273
int getReceiveBufferSize() const
Set receive buffer size.
Definition: JSocket.hh:262
virtual void actionConfigure(int length, const char *buffer) override
void next()
Increment internal iterator.
bool getNonBlocking() const
Get non-blocking of I/O.
Definition: JSocket.hh:134
Interface for logging messages.
Definition: JLogger.hh:22
Message logging based on ControlHost.
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
const_iterator operator->()
Smart pointer operator.
Utility class to parse parameter values.
Definition: JProperties.hh:496
std::vector< JTarget > target
std::vector< value_type >::iterator iterator
Definition: JTimeslice.hh:33
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Definition of random value generator.
Runcontrol client to simulate DOM.
virtual void actionRunning() override
This method is repeatedly called when this client machine is in state Running and the clock interval ...
void run()
Run as run control client following command messages via JNET::JControlHost.
Definition: JDAQClient.hh:661
Auxiliary data structure for hostname and port number.
Definition: JHostname.hh:30
JSource()
Default constructor.
Utility class to parse parameter values.
bool getKeepAlive() const
Get keep alive of socket.
Definition: JSocket.hh:163
friend std::istream & operator>>(std::istream &in, JSource &source)
Read JSource from input stream.
Scheduling of actions via fixed latency intervals.
JIO::JByteArrayWriter JFrame
Memory management for sending of raw data.
Hit data structure.
Definition: JDAQHit.hh:34
event< ev_daq > ev_configure
Definition: JDAQCHSM.chsm:175
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
Byte array binary input.
Definition: JByteArrayIO.hh:25
std::vector< JSource > source
const_iterator begin() const
Definition: JDAQFrame.hh:164
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
int getRunNumber(const std::string &file_name)
Get run number for given file name of data taking run.
Auxiliary class for CPU timing and usage.
Definition: JTimer.hh:32
Data time slice.
Data structure for configuration of JDOMSimulator.
Level specific message streamers.
int debug
debug level
Definition: JSirene.cc:66
void setReceiveBufferSize(const int size)
Set receive buffer size.
Definition: JSocket.hh:251
Data structure for configuration of JDataFilter.
#define FATAL(A)
Definition: JMessage.hh:67
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Blocking socket I/O.
virtual void actionReset(int length, const char *buffer) override
JDOMSimulator(const std::string &name, const std::string &server, JLogger *logger, const int level)
Constructor.
void reset(T &value)
Reset value.
Control unit client base class.
Definition: JDAQClient.hh:272
General purpose class for object reading from a list of file names.
Utility class to parse command line options.
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:385
friend std::ostream & operator<<(std::ostream &out, const JSource &source)
Write JSource to output stream.
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition: JSocket.hh:110
virtual bool enter(const JArgs &args)
Enter the state machine.
Definition: JDAQClient.hh:363
bool getTcpNoDelay() const
Get TCP no-delay.
Definition: JSocket.hh:240
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:57
size_t getSizeof< JDAQSuperFrameHeader >()
Get size of type.
static const JNET::JTag RC_CMD
Definition: JDAQTags.hh:44
void setKeepAlive(const bool on)
Set keep alive of socket.
Definition: JSocket.hh:152
void setRunNumber(const int run)
Set run number.
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JSocket.hh:229
Byte array binary output.
friend std::istream & operator>>(std::istream &in, JTarget &target)
Read JTarget from input stream.
size_t getSizeof< JDAQPreamble >()
Get size of type.
KM3NeT DAQ constants, bit handling, etc.
virtual void actionStop(int length, const char *buffer) override
void setFrameIndex(const int frame_index)
Set frame index.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
JData::const_iterator page
friend std::ostream & operator<<(std::ostream &out, const JTarget &target)
Write JTarget to output stream.
Data frame of one optical module.
virtual void actionStart(int length, const char *buffer) override
void reset()
Reset internal iterator to begin.
long long int numberOfSlices
static const JNET::JTag RC_DOMSIMULATOR
Definition: JDAQTags.hh:47
then $DIR JPlotNPE PDG P
Definition: JPlotNPE-PDG.sh:62
const_iterator end() const
Definition: JDAQFrame.hh:165