Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Typedefs | Functions
JSupernovaMonitor.cc File Reference

Online supernova monitor. More...

#include <iostream>
#include <iomanip>
#include <queue>
#include <string>
#include "json/json.hpp"
#include "JDAQ/JDAQTimesliceIO.hh"
#include "JDAQ/JDAQEventIO.hh"
#include "JDAQ/JDAQSummarysliceIO.hh"
#include "JDAQ/JDAQTags.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JNet/JControlHostObjectIterator.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "JROOT/JROOTClassSelector.hh"
#include "JSupport/JSupport.hh"
#include "JSupernova.hh"

Go to the source code of this file.

Typedefs

using json = nlohmann::json
 

Functions

int main (int argc, char *argv[])
 

Detailed Description

Online supernova monitor.

Author
mlincett

Definition in file JSupernovaMonitor.cc.

Typedef Documentation

using json = nlohmann::json

Definition at line 28 of file JSupernovaMonitor.cc.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 36 of file JSupernovaMonitor.cc.

36  {
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  try {
60 
61  JParser<> zap("Example program to test receiving of objects from ControlHost server.");
62 
63  zap['H'] = make_field(controlhost, "CH server (input)") = "localhost";
64  zap['L'] = make_field(ligier, "Ligier server (output)") = "";
65  zap['t'] = make_field(timeout_us) = 100000;
66  zap['n'] = make_field(numberOfTimeouts) = 100;
67  zap['a'] = make_field(detectorFile);
68  zap['C'] = make_field(selector) = getROOTClassSelection<JDAQTimesliceTypes_t>();
69  zap['Q'] = make_field(queueLength, "number of timeslices of trigger queue") = 100;
70  zap['W'] = make_field(windowLength, "number of timeslices of trigger sliding window") = 5;
71  zap['T'] = make_field(TMax_ns, "coincidence time window [ns]") = 10;
72  zap['M'] = make_field(M, "multiplicity range for SN coincidences") = JRange<int>(6,10);
73  zap['S'] = make_field(preTriggerThreshold, "muon veto multiplicity threshold") = 4;
74  zap['V'] = make_field(TVeto_ns, "muon veto time interval") = 1000;
75  zap['s'] = make_field(summaryFile, "summary output file");
76  zap['P'] = make_field(statPrintInterval_s, "statistics & file print interval [s]") = 30;
77  zap['d'] = make_field(debug) = 1;
78 
79  zap(argc, argv);
80 
81  }
82 
83  catch(const exception &error) {
84  FATAL(error.what() << endl);
85  }
86 
87  if (queueLength < windowLength) {
88  FATAL("Length of the trigger window must be smaller than the queue.");
89  }
90 
91 
93 
94  using namespace JSUPERNOVA;
95 
96  // -------------------------------
97  // load detector and build routers
98  // -------------------------------
99 
101 
102  try {
103  load(detectorFile, detector);
104  }
105  catch(const JException& error) {
106  FATAL(error);
107  }
108 
109  const JModuleRouter moduleRouter(detector);
110 
111  const int DETID = detector.getID();
112 
113  const int detectorSize = detector.size();
114 
115  JSNFilterM F_M1(M, 1);
116 
117  // -------------------------------
118  // initialize processing queue
119  // -------------------------------
120 
121  typedef JTriggerSN trigger_type;
122 
123  typedef priority_queue<trigger_type, vector<trigger_type>, greater<trigger_type> > queue_type;
124 
125  typedef deque<trigger_type> window_type;
126 
127  // (frameindex, DOMID) -> DOM rate [kHz]
128  typedef map<int, map<int, double> > rates_type;
129 
130  // frameindex -> N. active PMTs
131  typedef map<int, int> npmt_type;
132 
133  queue_type trgQueue;
134  window_type trgWindow;
135  rates_type moduleRates;
136  npmt_type activeChannels;
137 
138  JTriggerSNStats stats(detectorSize);
139 
140  long int counter_live_ts = 0;
141  long int counter_lost_ts = 0;
142 
143  double frameTime_s = getFrameTime() / 1.0e9;
144 
145  // -------------------------------
146  // main processing
147  // -------------------------------
148 
149  try {
150 
151  // setup input
152 
153  typedef JDAQTimesliceSN data_type;
154  typedef JDAQSummaryslice summary_type;
155 
156  JControlHostObjectIterator<data_type> in(controlhost, timeout_us, true);
157  JControlHostObjectIterator<summary_type> sm(controlhost, 1000.0, true);
158 
159  // setup output
160  JControlHost* out = NULL;
161 
162  if (ligier != "") {
163  out = new JControlHost(ligier);
164  out->MyId(argv[0]); // pid
165  }
166 
167  const string outputTag = "SNT";
168 
169  // setup state
170 
171  int RUN = 0;
172 
173  for (int i = 0; i != numberOfTimeouts; ) {
174 
175  if (in.hasNext()) {
176 
177  data_type* timeslice = in.next();
178 
179  DEBUG(timeslice->getDAQHeader() << endl);
180 
181  int timesliceSize = timeslice->size();
182 
183  // --------------------------------
184  // run number initialise and update
185  // --------------------------------
186 
187  const int r = timeslice->getRunNumber();
188 
189  if (r != RUN) {
190 
191  if (RUN != 0) {
192 
193  NOTICE("RUN CHANGE" << endl);
194 
195  while (trgQueue.size() > 0) { trgQueue.pop(); }
196 
197  trgWindow.clear();
198 
199  moduleRates.clear();
200 
201  activeChannels.clear();
202 
203  }
204 
205  RUN = r;
206 
207  }
208 
209  // ------------------------------
210  // process pending summary slices
211  // ------------------------------
212 
213  while ( sm.hasNext() ) {
214 
215  JDAQSummaryslice* summary = sm.next();
216 
217  int frame_index = summary->getFrameIndex();
218 
219  for (JDAQSummaryslice::const_iterator summary_frame = summary->begin(); summary_frame != summary->end(); ++summary_frame) {
220 
221  int DOMID = summary_frame->getModuleID();
222 
223  for (int ipmt = 0 ; ipmt < NUMBER_OF_PMTS ; ipmt++) {
224  moduleRates[frame_index][DOMID] += summary_frame->getRate(ipmt, 1.0/1000);
225  }
226 
227  activeChannels[frame_index] += summary_frame->countActiveChannels();
228  }
229  }
230 
231  // -----------------
232  // process timeslice
233  // -----------------
234 
235  JDataSN preTrigger(TMax_ns, preTriggerThreshold);
236 
237  preTrigger(timeslice, moduleRouter);
238 
239  JTriggerSN trigger(TVeto_ns);
240 
241  trigger(preTrigger);
242 
243  trgQueue.push(trigger);
244 
245  //----------------
246  // compute trigger
247  //----------------
248 
249  if (trgQueue.size() >= (unsigned) queueLength) {
250 
251  while (trgWindow.size() <= (unsigned) windowLength) {
252 
253  trigger_type pending = trgQueue.top();
254 
255  if ( trgWindow.size() == 0 || pending > trgWindow.back() ) {
256 
257  trgWindow.push_back( pending );
258 
259  counter_live_ts++;
260 
261  } else {
262  // latecoming (out of order) timeslice
263  counter_lost_ts++;
264  }
265 
266  trgQueue.pop();
267 
268  }
269 
270  // build triggered modules
271 
272  int cTrigger = 0;
273  int mTrigger = 0;
274 
275  set<int> triggeredModules;
276 
277  for (int its = 0; its < windowLength; its++) {
278 
279  set<int> current = trgWindow[its].getModules(F_M1);
280 
281  triggeredModules.insert(current.begin(), current.end());
282 
283  cTrigger += count_if(trgWindow[its].begin(), trgWindow[its].end(), F_M1);
284 
285  }
286 
287  mTrigger = triggeredModules.size();
288 
289  // trigger window slide of one element
290 
291  int currentFrame = trgWindow[0].frameIndex;
292  JDAQUTCExtended currentTime = trgWindow[0].timeUTC;
293 
294  trgWindow.pop_front();
295 
296  // calculate trigger
297 
298  ++stats[cTrigger];
299 
300  // calculate active modules
301 
302  int activeModules = -1;
303  double detectorRate = 0.0;
304 
305  if (!moduleRates.empty() &&
306  moduleRates.count(currentFrame)) {
307 
308  activeModules = 0;
309 
310  for (map<int, double>::const_iterator p = moduleRates.at(currentFrame).begin();
311  p != moduleRates.at(currentFrame).end(); p++ ) {
312 
313  detectorRate += p->second;
314 
315  activeModules += (p->second > 0);
316 
317  }
318  } else {
319 
320  activeModules = timesliceSize;
321 
322  }
323 
324  // build summary message
325 
326  json jd;
327 
328  jd["detid"] = DETID;
329  jd["active_doms"] = activeModules;
330  jd["detector_rate"] = int(detectorRate);
331  jd["run_number"] = RUN;
332  jd["frame_index"] = currentFrame;
333  jd["daq_time"] = to_string(currentTime);
334  jd["trigger_level"] = cTrigger;
335  jd["trigger_ndoms"] = mTrigger;
336  jd["active_pmts"] = activeChannels[currentFrame];
337 
338  string msg = jd.dump();
339 
340  DEBUG(msg << endl);
341 
342  // send summary information to output ligier
343 
344  if (out != NULL) {
345  out->PutFullString(outputTag, msg);
346  }
347 
348  // print stats
349 
350  if ( (counter_live_ts % ((int)(statPrintInterval_s / frameTime_s)) == 0 ) ) {
351 
352  double livetime = counter_live_ts * frameTime_s;
353 
354  stats.setLiveTime(livetime);
355 
356  NOTICE(endl);
357  NOTICE(stats.toString());
358  NOTICE("=> discarded out-of-order timeslices = " << counter_lost_ts << endl);
359 
360  if (summaryFile != "") {
361  ofstream of(summaryFile.c_str());
362  of << stats.toSummaryFile();
363  of.close();
364  }
365 
366  }
367 
368  } else {
369  NOTICE("Filling trigger queue: " << trgQueue.size() << "/" << queueLength << '\r');
370  }
371 
372  } else {
373 
374  NOTICE("timeout " << setw(3) << i << endl);
375 
376  ++i;
377  }
378  }
379  }
380 
381  catch(const JSocketException& error) {
382  ERROR(error.what() << endl);
383  }
384 
385 }
Utility class to parse command line options.
Definition: JParser.hh:1493
General exception.
Definition: JException.hh:23
debug
Definition: JMessage.hh:29
int MyId(const std::string &nick_name)
Identify.
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
ControlHost class.
Detector data structure.
Definition: JDetector.hh:80
Auxiliary class to select ROOT class based on class name.
Router for direct addressing of module data in detector data structure.
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
data_type r[M+1]
Definition: JPolint.hh:709
Data structure for UTC time.
int getFrameIndex() const
Get frame index.
Auxiliary class for time values.
Definition: JTimeval.hh:26
void setDAQLongprint(const bool option)
Set DAQ print option.
Definition: JDAQPrint.hh:28
Detector file.
Definition: JHead.hh:130
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
SN trigger statistics, the information is stored in the form of a count as a function of the trigger ...
Definition: JSupernova.hh:560
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:411
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
SN filter based on multiplicity selection optional suppression of multi-module coincidences WARNING: ...
Definition: JSupernova.hh:349
int debug
debug level
Definition: JSirene.cc:61
Timeslice data structure for SN data.
#define FATAL(A)
Definition: JMessage.hh:67
Object iteration through ControlHost.
Auxiliary class to build the supernova trigger dataset.
Definition: JSupernova.hh:158
Normalisation of MUPAGE events.
Definition: JHead.hh:604
nlohmann::json json
std::string to_string(const T &value)
Convert value to string.
Exception for socket.
Definition: JException.hh:432
virtual const char * what() const
Get error message.
Definition: JException.hh:48
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62