Jpp  15.0.1-rc.1-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEventGenerator.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <sstream>
4 #include <iomanip>
5 #include <vector>
6 #include <limits>
7 
8 #include "Jeep/JParser.hh"
9 #include "Jeep/JProperties.hh"
10 #include "Jeep/JTimer.hh"
11 #include "Jeep/JTimekeeper.hh"
12 #include "JLang/JSharedPointer.hh"
13 #include "JNet/JControlHost.hh"
15 #include "JDAQ/JDAQEventIO.hh"
18 #include "JSupport/JSupport.hh"
21 #include "JIO/JByteArrayIO.hh"
22 
23 
24 namespace KM3NETDAQ {
25 
26 
27  using namespace JPP;
28 
29 
30  /**
31  * Runcontrol client to simulate data filter(s).
32  * In state running, this application will send events to the data writer.
33  */
35  public JDAQClient
36  {
37  public:
38  /**
39  * Constructor.
40  *
41  * \param name name of client
42  * \param server name of command message server
43  * \param logger pointer to logger
44  * \param level debug level
45  */
46  JEventGenerator(const std::string& name,
47  const std::string& server,
48  JLogger* logger,
49  const int level) :
50  JDAQClient(name, server, logger, level),
51  datawriter()
52  {
53  replaceEvent(RC_CMD, RC_EVTGENERATOR, ev_configure);
54 
55  JControlHost::Throw(true);
56  }
57 
58 
59  virtual void actionConfigure(int length, const char* buffer)
60  {
61  using namespace std;
62  using namespace JPP;
63 
64  inputFile.clear();
65 
66  JString destination;
67 
68  JProperties properties(JEquationParameters("=", ";", "", ""));
69 
70  properties["datawriter"] = destination = "localhost";
71  properties["inputFile"] = inputFile;
72  properties["eventRate_Hz"] = eventRate_Hz = 1.0;
73 
74  properties.read(string(buffer, length));
75 
76  destination = destination.trim();
77 
78  try {
79  datawriter.reset(new JControlHost(destination));
80  }
81  catch(const JControlHostException& exception) {
82  JErrorStream(logger) << exception;
83  }
84 
85  if (eventRate_Hz > 0.0)
86  setClockInterval((long long int) (1.0e6 / eventRate_Hz));
87  else
88  setClockInterval(0ULL);
89 
90  parameters = getTriggerParameters(inputFile);
91 
92  JDebugStream(logger) << "Event interval time " << getClockInterval() << " us";
93  }
94 
95 
96  virtual void actionReset(int length, const char* buffer)
97  {
98  JDebugStream(logger) << "actionReset()";
99 
100  inputFile.rewind();
101 
102  datawriter.reset();
103  }
104 
105 
106  virtual void actionQuit(int length, const char* buffer)
107  {}
108 
109 
110  virtual void actionStart(int length, const char* buffer)
111  {
112  using namespace std;
113 
114  numberOfEvents = 0;
115  numberOfBytes = 0;
116 
117  ostringstream os;
118 
119  os << getRunNumber() << ' ' << parameters;
120 
121  datawriter->PutFullString(IO_TRIGGER_PARAMETERS, os.str());
122 
123  timer.reset();
124  }
125 
126 
127  virtual void actionStop(int length, const char* buffer)
128  {
129  JDebugStream(logger) << "actionStop()";
130 
131  if (timer.usec_wall > 0) JNoticeStream(logger) << "I/O [MB/s] " << numberOfBytes / timer.usec_wall;
132  if (numberOfEvents > 0) JNoticeStream(logger) << "Delay/event [ms] " << getClockDelay() / numberOfEvents / 1000;
133  }
134 
135 
136  virtual void actionRunning()
137  {
138  //JDebugStream(logger) << "actionRunning()";
139 
140  using namespace JPP;
141 
142  static JIO::JByteArrayWriter out;
143 
144  if (datawriter.is_valid()) {
145 
146  if (!inputFile.hasNext()) {
147  inputFile.rewind();
148  }
149 
150  if (inputFile.hasNext()) {
151 
152  timer.start();
153 
154  JDAQEvent* event = inputFile.next();
155 
156  event->setRunNumber(getRunNumber());
157 
158  out.clear();
159 
160  out << *event;
161 
162  try {
163 
164  datawriter->PutFullData(IO_EVENT, out.data(), out.size());
165 
166  numberOfEvents += 1;
167  numberOfBytes += out.size();
168  }
169  catch(const JControlHostException& exception) {
170  JErrorStream(logger) << exception;
171  }
172 
173  timer.stop();
174  }
175  }
176 
177  //JDebugStream(logger) << "actionRunning() return";
178  }
179 
180 
181  private:
183 
186 
187  Long64_t numberOfEvents;
188  double eventRate_Hz;
189 
190  JEEP::JTimer timer; // timer for I/O measurement
191  long long int numberOfBytes; // total number of bytes
192  };
193 }
194 
195 
196 /**
197  * \file
198  *
199  * Program for real-time simulation of data.
200  * \author mdejong
201  */
202 int main(int argc, char* argv[])
203 {
204  using namespace std;
205 
206  string server;
207  string logger;
208  string client_name;
209  bool use_cout;
210  int debug;
211 
212  try {
213 
214  JParser<> zap("Program for real-time simulation of data.");
215 
216  zap['H'] = make_field(server) = "localhost";
217  zap['M'] = make_field(logger) = "localhost";
218  zap['u'] = make_field(client_name) = "%";
219  zap['c'] = make_field(use_cout);
220  zap['d'] = make_field(debug) = 3;
221 
222  zap(argc, argv);
223  }
224  catch(const exception &error) {
225  FATAL(error.what() << endl);
226  }
227 
228 
229  using namespace KM3NETDAQ;
230  using namespace JPP;
231 
232  JLogger* out = NULL;
233 
234  if (use_cout)
235  out = new JStreamLogger(cout);
236  else
237  out = new JControlHostLogger(logger);
238 
239  JEventGenerator enigma(getProcessName(client_name, argv[0]), server, out, debug);
240 
241  enigma.enter();
242  enigma.run();
243 }
Utility class to parse command line options.
Definition: JParser.hh:1500
bool read(const JEquation &equation)
Read equation.
Definition: JProperties.hh:677
Data structure for all trigger parameters.
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
Wrapper class around STL string class.
Definition: JString.hh:27
virtual void actionStart(int length, const char *buffer)
ControlHost class.
JSharedPointer< JControlHost > datawriter
std::string getProcessName(const std::string &name, const std::string &process)
Get process name of run control client.
Message logging based on std::ostream.
JSUPPORT::JMultipleFileScanner< KM3NETDAQ::JDAQEvent > inputFile
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
Utility class to parse parameter values.
Definition: JProperties.hh:496
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Simple data structure to support I/O of equations (see class JLANG::JEquation).
static const JNET::JTag IO_TRIGGER_PARAMETERS
Definition: JDAQTags.hh:68
void run()
Run as run control client following command messages via JNET::JControlHost.
Definition: JDAQClient.hh:661
Utility class to parse parameter values.
JTRIGGER::JTriggerParameters parameters
Scheduling of actions via fixed latency intervals.
The template JSharedPointer class can be used to share a pointer to an object.
virtual void actionStop(int length, const char *buffer)
event< ev_daq > ev_configure
Definition: JDAQCHSM.chsm:175
virtual void actionReset(int length, const char *buffer)
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
int getRunNumber(const std::string &file_name)
Get run number for given file name of data taking run.
Runcontrol client to simulate data filter(s).
Auxiliary class for CPU timing and usage.
Definition: JTimer.hh:32
Level specific message streamers.
Exception for ControlHost.
Definition: JException.hh:450
static const JNET::JTag RC_EVTGENERATOR
Definition: JDAQTags.hh:48
int debug
debug level
Definition: JSirene.cc:63
#define FATAL(A)
Definition: JMessage.hh:67
Scanning of objects from multiple files according a format that follows from the extension of each fi...
JEventGenerator(const std::string &name, const std::string &server, JLogger *logger, const int level)
Constructor.
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.
virtual bool enter(const JArgs &args)
Enter the state machine.
Definition: JDAQClient.hh:363
static const JNET::JTag RC_CMD
Definition: JDAQTags.hh:44
static const JNET::JTag IO_EVENT
Definition: JDAQTags.hh:66
Byte array binary output.
KM3NeT DAQ constants, bit handling, etc.
virtual void actionQuit(int length, const char *buffer)
virtual void actionConfigure(int length, const char *buffer)
JTriggerParameters getTriggerParameters(const JMultipleFileScanner_t &file_list)
Get trigger parameters.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
virtual void actionRunning()
This method is repeatedly called when this client machine is in state Running and the clock interval ...
JTriggerCounter_t next()
Increment trigger counter.