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
JSupernovaMonitor.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <queue>
4 #include <string>
5 
6 #include "json/json.hpp"
7 
9 #include "JDAQ/JDAQEventIO.hh"
11 
12 #include "JDAQ/JDAQTags.hh"
13 
14 #include "JDetector/JDetector.hh"
16 
18 
19 #include "Jeep/JParser.hh"
20 #include "Jeep/JMessage.hh"
21 
23 
24 #include "JSupport/JSupport.hh"
25 
26 #include "JSupernova.hh"
27 
29 
30 /**
31  * \file
32  *
33  * Online supernova monitor
34  * \author mlincett
35  */
36 int main(int argc, char* argv[]) {
37  using namespace std;
38  using namespace JPP;
39  using namespace KM3NETDAQ;
40 
41  string controlhost;
42  string ligier;
43  JTimeval timeout_us;
44  int numberOfTimeouts;
45  int debug;
46  int queueLength;
47  int windowLength;
48  int statPrintInterval_s;
49 
50  string detectorFile;
51  JROOTClassSelector selector;
52  string summaryFile;
53 
54  int TMax_ns;
55  int preTriggerThreshold;
56  JRange<int> M;
57  double TVeto_ns;
58 
59  const string outputTag = "SNT";
60 
61  try {
62 
63  JParser<> zap("Supernova realtime monitor");
64 
65  zap['H'] = make_field(controlhost, "CH server (input)") = "localhost";
66  zap['L'] = make_field(ligier, "Ligier server (output)") = "";
67  zap['t'] = make_field(timeout_us) = 1e6;
68  zap['n'] = make_field(numberOfTimeouts) = 1e3;
69  zap['a'] = make_field(detectorFile);
70  zap['C'] = make_field(selector) = getROOTClassSelection<JDAQTimesliceTypes_t>();
71  zap['Q'] = make_field(queueLength, "number of timeslices of trigger queue") = 100;
72  zap['W'] = make_field(windowLength, "number of timeslices of trigger sliding window") = 5;
73  zap['T'] = make_field(TMax_ns, "coincidence time window [ns]") = 10;
74  zap['M'] = make_field(M, "multiplicity range for SN coincidences") = JRange<int>(6,10);
75  zap['S'] = make_field(preTriggerThreshold, "muon veto multiplicity threshold") = 4;
76  zap['V'] = make_field(TVeto_ns, "muon veto time interval") = 1000;
77  zap['s'] = make_field(summaryFile, "summary output file") = "";
78  zap['P'] = make_field(statPrintInterval_s, "statistics & file print interval [s]") = 30;
79  zap['d'] = make_field(debug) = 1;
80 
81  zap(argc, argv);
82 
83  }
84 
85  catch(const exception &error) {
86  FATAL(error.what() << endl);
87  }
88 
89  if (queueLength < windowLength) {
90  FATAL("Length of the trigger window must be smaller than the queue.");
91  }
92 
93 
95 
96  using namespace JSUPERNOVA;
97 
98  // -------------------------------
99  // load detector and build routers
100  // -------------------------------
101 
103 
104  try {
105  load(detectorFile, detector);
106  }
107  catch(const JException& error) {
108  FATAL(error);
109  }
110 
111  const JDAQHitRouter hitRouter(detector);
112 
113  const JModuleRouter& moduleRouter = hitRouter;
114 
115  const int DETID = detector.getID();
116 
117  const int detectorSize = detector.size();
118 
119  JSNFilterM F_M1(M, 1);
120 
121  // -------------------------------
122  // initialize processing queue
123  // -------------------------------
124 
125  typedef JTriggerSN trigger_type;
126 
127  typedef priority_queue<trigger_type, vector<trigger_type>, greater<trigger_type> > queue_type;
128 
129  typedef deque<trigger_type> window_type;
130 
131 
132 
133  typedef map<int, map<int, double> > rates_type; // (frameindex, DOMID) -> DOM rate [kHz]
134  typedef map<int, int> npmt_type ; // (frameindex) -> # active PMTs
135  typedef map<int, JVetoSet> veto_type ; // (frameindex) -> vetoes
136 
137  queue_type trgQueue;
138  window_type trgWindow;
139  rates_type rates;
140  npmt_type pmts;
141  veto_type veto;
142 
143  JTriggerSNStats stats(detectorSize);
144 
145  long int counter_live_ts = 0;
146  long int counter_lost_ts = 0;
147 
148  double frameTime_s = getFrameTime() / 1.0e9;
149 
150  // -------------------------------
151  // main processing
152  // -------------------------------
153 
154  try {
155 
156  // setup input
157 
158  typedef JDAQTimesliceSN data_type;
159  typedef JDAQSummaryslice summary_type;
160  typedef JDAQEvent event_type;
161 
162  JControlHostObjectIterator<data_type> in(controlhost, timeout_us, true);
163 
164  // timeout for the asynchronous reading of summary and event data
165  // needs to be smaller than the timeslice duration
166  const double asyncTimeout_us = 1000.0;
167 
168  JControlHostObjectIterator<summary_type> sm(controlhost, asyncTimeout_us, true);
169  JControlHostObjectIterator<event_type> ev(controlhost, asyncTimeout_us, true);
170 
172 
173  if (ligier != "") {
174  out.reset(new JControlHost(ligier));
175  out->MyId(argv[0]); // pid
176  }
177 
178  // setup state
179 
180  int RUN = 0;
181 
182  for (int i = 0; i != numberOfTimeouts; ) {
183 
184  if (in.hasNext()) {
185 
186  data_type* timeslice = in.next();
187 
188  DEBUG(timeslice->getDAQHeader() << endl);
189 
190  int timesliceSize = timeslice->size();
191 
192  // --------------------------------
193  // run number initialise and update
194  // --------------------------------
195 
196  const int r = timeslice->getRunNumber();
197 
198  if (r != RUN) {
199 
200  if (RUN != 0) {
201 
202  NOTICE("RUN CHANGE" << endl);
203 
204  while (trgQueue.size() > 0) { trgQueue.pop(); }
205 
206  trgWindow.clear();
207 
208  rates.clear();
209 
210  pmts.clear();
211 
212  veto.clear();
213 
214  }
215 
216  RUN = r;
217 
218  }
219 
220  // ------------------------------
221  // process pending summary slices
222  // ------------------------------
223 
224  while ( sm.hasNext() ) {
225 
226  JDAQSummaryslice* summary = sm.next();
227 
228  DEBUG("SUM " << summary->getDAQHeader() << endl);
229 
230  int frame_index = summary->getFrameIndex();
231 
232  for (JDAQSummaryslice::const_iterator summary_frame = summary->begin();
233  summary_frame != summary->end();
234  ++summary_frame) {
235 
236  int DOMID = summary_frame->getModuleID();
237 
238  for (int ipmt = 0 ; ipmt < NUMBER_OF_PMTS ; ipmt++) {
239  rates[frame_index][DOMID] += summary_frame->getRate(ipmt, 1.0/1000);
240  }
241 
242  pmts[frame_index] += summary_frame->countActiveChannels();
243  }
244 
245  }
246 
247  DEBUG("LOADING EVENTS" << endl);
248 
249  while ( ev.hasNext() ) {
250 
251  JDAQEvent* event = ev.next();
252 
253  DEBUG("EVT " << event->getDAQHeader() << endl);
254 
255  int frame_index = event->getFrameIndex();
256 
257  veto[frame_index].push_back(JVeto(*event, hitRouter));
258 
259  }
260 
261  // -----------------
262  // process timeslice
263  // -----------------
264 
265  JDataSN preTrigger(TMax_ns, preTriggerThreshold);
266 
267  preTrigger(timeslice, moduleRouter);
268 
269  JTriggerSN trigger(TVeto_ns);
270 
271  trigger(preTrigger);
272 
273  trgQueue.push(trigger);
274 
275  //----------------
276  // compute trigger
277  //----------------
278 
279  if (trgQueue.size() >= (unsigned) queueLength) {
280 
281  while (trgWindow.size() <= (unsigned) windowLength) {
282 
283  trigger_type pending = trgQueue.top();
284 
285  if ( trgWindow.size() == 0 || pending > trgWindow.back() ) {
286 
287  trgWindow.push_back( pending );
288 
289  counter_live_ts++;
290 
291  } else {
292  // latecoming (out of order) timeslice
293  counter_lost_ts++;
294  }
295 
296  trgQueue.pop();
297 
298  }
299 
300  // build triggered modules
301 
302  int trg_cc_counts = 0;
303  int trg_cc_modules = 0;
304  set<int> cc_modules;
305 
306  int trg_ev_counts = 0;
307  int trg_ev_modules = 0;
308  set<int> ev_modules;
309 
310  // loop over the trigger window and count the triggers
311  for (int its = 0; its < windowLength; its++) {
312 
313  const int frame_index = trgWindow[its].frameIndex;
314 
315  JVetoSet vetoSet;
316  if (veto.count(frame_index)) {
317  vetoSet = veto.at(frame_index);
318  }
319 
320  JSNFilterMV F_MV(M, vetoSet);
321 
322  set<int> cc_vec = trgWindow[its].getModules(F_M1);
323  set<int> ev_vec = trgWindow[its].getModules(F_MV);
324 
325  cc_modules.insert(cc_vec.begin(), cc_vec.end());
326  ev_modules.insert(ev_vec.begin(), ev_vec.end());
327 
328  trg_cc_counts += count_if(trgWindow[its].begin(), trgWindow[its].end(), F_M1);
329  trg_ev_counts += count_if(trgWindow[its].begin(), trgWindow[its].end(), F_MV);
330 
331  }
332 
333  trg_cc_modules = cc_modules.size();
334  trg_ev_modules = ev_modules.size();
335 
336  // trigger window slide of one element
337 
338  int currentFrame = trgWindow[0].frameIndex;
339  JDAQUTCExtended currentTime = trgWindow[0].timeUTC;
340 
341  trgWindow.pop_front();
342 
343  // calculate trigger
344 
345  ++stats[trg_cc_counts];
346 
347  // calculate active modules
348 
349  int activeModules = -1;
350  double detectorRate = 0.0;
351 
352  if (!rates.empty() &&
353  rates.count(currentFrame)) {
354 
355  activeModules = 0;
356 
357  for (map<int, double>::const_iterator p = rates.at(currentFrame).begin();
358  p != rates.at(currentFrame).end(); p++ ) {
359 
360  detectorRate += p->second;
361 
362  activeModules += (p->second > 0);
363 
364  }
365  } else {
366 
367  activeModules = timesliceSize;
368 
369  }
370 
371  // build summary message
372 
373  json jd;
374 
375  jd["detid"] = DETID;
376  jd["active_doms"] = activeModules;
377  jd["detector_rate_MHz"] = int(detectorRate / 1000.0);
378  jd["run_number"] = RUN;
379  jd["frame_index"] = currentFrame;
380  jd["daq_time"] = to_string(currentTime);
381  jd["trigger"]["cc"]["c"] = trg_cc_counts;
382  jd["trigger"]["cc"]["m"] = trg_cc_modules;
383  jd["trigger"]["ev"]["c"] = trg_ev_counts;
384  jd["trigger"]["ev"]["m"] = trg_ev_modules;
385  jd["active_pmts"] = pmts[currentFrame];
386 
387  string msg = jd.dump();
388 
389  DEBUG(msg << endl);
390 
391  // send summary information to output ligier
392 
393  if (out != NULL) {
394  out->PutFullString(outputTag, msg);
395  }
396 
397  // print stats
398 
399  if ( (counter_live_ts % ((int)(statPrintInterval_s / frameTime_s)) == 0 ) ) {
400 
401  double livetime = counter_live_ts * frameTime_s;
402 
403  stats.setLiveTime(livetime);
404 
405  NOTICE(endl);
406  NOTICE(stats.toString());
407  NOTICE("=> discarded out-of-order timeslices = " << counter_lost_ts << endl);
408 
409  if (summaryFile != "") {
410  ofstream of(summaryFile.c_str());
411  of << stats.toSummaryFile();
412  of.close();
413  }
414 
415  }
416 
417  } else {
418  NOTICE("Filling trigger queue: " << trgQueue.size() << "/" << queueLength << '\r');
419  }
420 
421  } else {
422 
423  NOTICE("timeout " << setw(3) << i << endl);
424 
425  ++i;
426  }
427  }
428  }
429 
430  catch(const JSocketException& error) {
431  ERROR(error.what() << endl);
432  }
433 
434 }
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
int MyId(const std::string &nick_name)
Identify.
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
ControlHost class.
Auxiliary class to define a veto time window on a set of optical modules.
Definition: JSupernova.hh:80
virtual bool hasNext() override
Check availability of next element.
Detector data structure.
Definition: JDetector.hh:89
virtual const pointer_type & next() override
Get next element.
Auxiliary class to select ROOT class based on class name.
SN filter based on veto window.
Definition: JSupernova.hh:364
Router for direct addressing of module data in detector data structure.
virtual void reset() override
Reset pointer.
Definition: JStorage.hh:42
data_type r[M+1]
Definition: JPolint.hh:758
Data structure for UTC time.
Data structure for detector geometry and calibration.
Simple wrapper around JModuleRouter class for direct addressing of PMT data in detector data structur...
int getFrameIndex() const
Get frame index.
string toSummaryFile()
put statistics into printable form outputs trigger level - rate - error
Definition: JSupernova.hh:594
Auxiliary class for time values.
Definition: JTimeval.hh:26
void setDAQLongprint(const bool option)
Set DAQ print option.
Definition: JDAQPrint.hh:28
The template JSinglePointer class can be used to hold a pointer to an object.
then set_variable DETID
Definition: JEditTuneHV.sh:63
Detector file.
Definition: JHead.hh:224
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
string toString()
put statistics into printable form outputs trigger level - rate - error
Definition: JSupernova.hh:561
SN trigger statistics, the information is stored in the form of a count as a function of the trigger ...
Definition: JSupernova.hh:540
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
then rm i $OUTPUT_FILE fi let RUN
Auxiliary class to apply the supernova trigger to SN data.
Definition: JSupernova.hh:391
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
SN filter based on multiplicity selection optional suppression of multi-module coincidences WARNING: ...
Definition: JSupernova.hh:329
int debug
debug level
Definition: JSirene.cc:66
Timeslice data structure for SN data.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Object iteration through ControlHost.
Auxiliary class to build the supernova trigger dataset.
Definition: JSupernova.hh:134
virtual const char * what() const override
Get error message.
Definition: JException.hh:48
Normalisation of MUPAGE events.
Definition: JHead.hh:820
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Utility class to parse command line options.
const JDAQHeader & getDAQHeader() const
Get DAQ header.
Definition: JDAQHeader.hh:49
nlohmann::json json
std::string to_string(const T &value)
Convert value to string.
Exception for socket.
Definition: JException.hh:450
Fixed parameters andd ControlHost tags for KM3NeT DAQ.
void setLiveTime(const double lt)
Definition: JSupernova.hh:553
do set_variable DETECTOR_TXT $WORKDIR detector
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
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
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Auxiliary class to manage a set of vetoes.
Definition: JSupernova.hh:257