Jpp  test_elongated_shower_pde
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Enumerations | Functions
JTurbot.cc File Reference

Example program to search for correlations between triggered events and time slice data. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <vector>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TF1.h"
#include "TMath.h"
#include "km3net-dataformat/online/JDAQ.hh"
#include "km3net-dataformat/definitions/pmt_status.hh"
#include "km3net-dataformat/definitions/module_status.hh"
#include "JDAQ/JDAQEventIO.hh"
#include "JDAQ/JDAQTimesliceIO.hh"
#include "JDAQ/JDAQEvaluator.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JDAQHitRouter.hh"
#include "JTrigger/JBuildL0.hh"
#include "JTrigger/JBuildL1.hh"
#include "JLang/JSinglePointer.hh"
#include "JROOT/JManager.hh"
#include "JTools/JWeight.hh"
#include "JROOT/JROOTClassSelector.hh"
#include "JSupport/JSingleFileScanner.hh"
#include "JSupport/JTreeScannerInterface.hh"
#include "JSupport/JTreeScanner.hh"
#include "JSupport/JAutoTreeScanner.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JMeta.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Enumerations

enum  JStatus_t
 

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to search for correlations between triggered events and time slice data.


This application can be used to identify optical modules that are out-of-sync with respect to the central White Rabbit clock.
To this end, time correlations are searched for between triggered events and local coincidences, referred to as L1 hits. The local coincidences are independently obtained from time slice data and can thereby cross the time slice border of a triggered event.
The MODULE_OUT_OF_SYNC of status the modules that are found to be out-of-sync are set.

The number of time slices to look for L1 hits away from the triggered event can be set via command line option -N <number of time slices>.
The larger this value, the bigger time offsets can be covered. To identify the modules that are out-of-sync as fast as possible (without determining the time offset), this value can be set to 0 with the caveat that a out-of-sync occurring during the data taking run may then be missed.

Author
mdejong

Definition in file JTurbot.cc.

Enumeration Type Documentation

enum JStatus_t

Definition at line 48 of file JTurbot.cc.

48  {
49  DEFAULT = 0, //!< module exists in detector
50  NODATA = -2, //!< module not present in data or no entries
51  OUT_SYNC = -3, //!< module is out-of-sync
52  ERROR = -4, //!< module is out-of-sync, possibly multiple time offsets
53  IN_SYNC = +1 //!< module is in-sync
54  };
#define ERROR(A)
Definition: JMessage.hh:66

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 86 of file JTurbot.cc.

87 {
88  using namespace std;
89  using namespace JPP;
90  using namespace KM3NETDAQ;
91 
92  JSingleFileScanner<> inputFile;
93  JLimit_t& numberOfEvents = inputFile.getLimit();
94  string outputFile;
95  string detectorFile;
96  bool overwriteDetector;
97  double TMaxLocal_ns;
98  int numberOfTimeslices;
99  double binWidth_ns;
100  double deadTime_us;
101  double Pmin;
102  JROOTClassSelector selector;
103  int qaqc;
104  int debug;
105 
106  try {
107 
108  JParser<> zap("Example program to search for correlations between triggered events and timeslice data.");
109 
110  zap['f'] = make_field(inputFile, "input file.");
111  zap['o'] = make_field(outputFile, "optional output file containing ROOT histograms.") = "";
112  zap['a'] = make_field(detectorFile, "detector file.");
113  zap['A'] = make_field(overwriteDetector, "overwrite detector file provided through '-a' with module (PMT) status.");
114  zap['n'] = make_field(numberOfEvents) = JLimit::max();
115  zap['T'] = make_field(TMaxLocal_ns, "time window for local coincidences (L1),") = 20.0;
116  zap['N'] = make_field(numberOfTimeslices, "time slice difference between triggered event and L1.") = 100;
117  zap['W'] = make_field(binWidth_ns, "bin width for output histograms." ) = 10e3;
118  zap['D'] = make_field(deadTime_us, "L1 dead time (us)") = 200.0;
119  zap['p'] = make_field(Pmin, "minimal probability for background to be signal.") = 1.0e-7;
120  zap['C'] = make_field(selector, "data type selection (default is all).") = "", getROOTClassSelection<JDAQTimesliceTypes_t>();
121  zap['Q'] = make_field(qaqc) = 0;
122  zap['d'] = make_field(debug) = 2;
123 
124  zap(argc, argv);
125  }
126  catch(const exception& error) {
127  FATAL(error.what() << endl);
128  }
129 
130 
131  cout.tie(&cerr);
132 
134 
135  try {
136  load(detectorFile, detector);
137  }
138  catch(const JException& error) {
139  FATAL(error);
140  }
141 
142  if (detector.empty()) {
143  FATAL("Empty detector " << detectorFile << endl);
144  }
145 
146  const JDAQHitRouter router(detector);
147 
148  const double TMax_ns = max(binWidth_ns, getMaximalTime(detector)); // signal time window
149  const double BOOST = 20.0; // scaling of time window for background
150  const double deadTime_ns = deadTime_us * 1e3;
151 
152  NOTICE("Time window " << FIXED(7,1) << TMax_ns << " [ns]" << endl);
153 
154  typedef double hit_type;
155 
156  JBuildL0<hit_type> buildL0;
158  vector <hit_type> data;
159 
160  typedef JManager<int, TH1D> JManager_t;
161 
162  const Double_t xmin = -(numberOfTimeslices + 1) * getFrameTime() - 0.5*binWidth_ns;
163  const Double_t xmax = +(numberOfTimeslices + 1) * getFrameTime() + 0.5*binWidth_ns;
164  const Int_t nx = (Int_t) ((xmax - xmin) / binWidth_ns);
165 
166  JManager_t manager(new TH1D("M_%", NULL, nx, xmin, xmax));
167 
169 
171 
173 
174  if (selector == "") {
175 
176  if ((ps = new JTreeScanner< JAssertConversion<JDAQTimesliceSN, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
177  (ps = new JTreeScanner< JAssertConversion<JDAQTimesliceL2, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
178  (ps = new JTreeScanner< JAssertConversion<JDAQTimesliceL1, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
179  (ps = new JTreeScanner< JAssertConversion<JDAQTimesliceL0, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
180  (ps = new JTreeScanner< JAssertConversion<JDAQTimeslice, JDAQTimeslice> >(inputFile))->getEntries() != 0) {
181  } else {
182  FATAL("No timeslice data." << endl);
183  }
184 
185  NOTICE("Selected class " << ps->getClassName() << endl);
186 
187  } else {
188 
189  ps = zmap[selector];
190 
191  ps->configure(inputFile);
192  }
193 
194  ps->setLimit(inputFile.getLimit());
195 
196  typedef map<int, vector<double> > map_type; // frame index -> event times
197 
198  map_type buffer;
199 
200  for (JTreeScanner<JDAQEvent> in(inputFile.getFilename()); in.hasNext(); ) {
201 
202  STATUS("event: " << setw(10) << in.getCounter() << '\r'); DEBUG(endl);
203 
204  JDAQEvent* event = in.next();
205 
206  // Average time of triggered hits
207 
208  double t0 = 0.0;
209 
210  for (JDAQEvent::const_iterator<JDAQTriggeredHit> hit = event->begin<JDAQTriggeredHit>(); hit != event->end<JDAQTriggeredHit>(); ++hit) {
211  t0 += getTime(*hit, router.getPMT(*hit));
212  }
213 
214  t0 /= event->size<JDAQTriggeredHit>();
215  t0 += event->getFrameIndex() * getFrameTime();
216 
217  buffer[event->getFrameIndex()].push_back(t0);
218  }
219  STATUS(endl);
220 
221 
222  while (ps->hasNext()) {
223 
224  STATUS("event: " << setw(10) << ps->getCounter() << '\r'); DEBUG(endl);
225 
226  JDAQTimeslice* timeslice = ps->next();
227 
228  map_type::const_iterator p = buffer.lower_bound(timeslice->getFrameIndex() - numberOfTimeslices);
229  map_type::const_iterator q = buffer.upper_bound(timeslice->getFrameIndex() + numberOfTimeslices);
230 
231  int number_of_events = 0;
232 
233  for (map_type::const_iterator i = p; i != q; ++i) {
234  number_of_events += i->second.size();
235  }
236 
237  if (number_of_events == 0) {
238  continue;
239  }
240 
241  for (JDAQTimeslice::const_iterator frame = timeslice->begin(); frame != timeslice->end(); ++frame) {
242 
243  data.clear();
244 
245  buildL1(*frame, router.getModule(frame->getModuleID()), back_inserter(data));
246 
247  TH1D* h1 = manager[frame->getModuleID()];
248 
249  double t1 = numeric_limits<double>::lowest();
250 
251  for (vector<hit_type>::const_iterator hit = data.begin(); hit != data.end(); ++hit) {
252 
253  const double t2 = *hit + frame->getFrameIndex() * getFrameTime();
254 
255  if (t2 > t1 + deadTime_ns) {
256 
257  for (map_type::const_iterator i = p; i != q; ++i) {
258  for (map_type::mapped_type::const_iterator t0 = i->second.begin(); t0 != i->second.end(); ++t0) {
259  h1->Fill(t2 - *t0);
260  }
261  }
262 
263  t1 = t2;
264  }
265  }
266  }
267  }
268  STATUS(endl);
269 
270 
271  // fit function
272 
273  TF1 f1("f1", "[0]*exp(-0.5*(x-[1])*(x-[1])/([2]*[2])) + [3]");
274 
275  string option = "L";
276 
277  if (debug < debug_t) {
278  option += "Q";
279  }
280 
281 
282  map<int, JStatus_t> status;
283 
284  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
285 
286  if (module->getFloor() != 0) {
287 
288  status[module->getID()] = DEFAULT;
289 
290  JManager_t::iterator p = manager.find(module->getID());
291 
292  if (p == manager.end() || p->second->GetEntries() == 0) {
293 
294  status[module->getID()] = NODATA;
295 
296  continue;
297  }
298 
299  TH1D* h1 = p->second;
300 
301  // start values
302 
303  Double_t ymax = 0.0;
304  Double_t x0 = 0.0; // [ns]
305  Double_t sigma = 250.0; // [ns]
306  Double_t ymin = 0.0;
307 
308  for (int i = 1; i != h1->GetNbinsX(); ++i) {
309 
310  const Double_t x = h1->GetBinCenter (i);
311  const Double_t y = h1->GetBinContent(i);
312 
313  if (y > ymax) {
314  ymax = y;
315  x0 = x;
316  }
317 
318  ymin += y;
319  }
320 
321  ymin /= h1->GetNbinsX();
322 
323  f1.SetParameter(0, ymax);
324  f1.SetParameter(1, x0);
325  if (binWidth_ns < sigma)
326  f1.SetParameter(2, sigma);
327  else
328  f1.FixParameter(2, binWidth_ns/sqrt(12.0));
329  f1.SetParameter(3, ymin);
330 
331  // fit
332 
333  h1->Fit(&f1, option.c_str(), "same", x0 - 5 * sigma, x0 + 5 * sigma);
334 
335 
336  if (fabs(f1.GetParameter(1)) <= 0.5*getFrameTime() && // position
337  f1.GetParameter(0) >= f1.GetParameter(3)) { // S/N
338 
339  status[module->getID()] = IN_SYNC;
340  }
341 
342 
343  // look for peaks at time offsets equal to a multiple of frame time
344 
345  map<int, JWeight> bg; // background
346  map<int, JWeight> sn; // signal(s)
347 
348  for (Int_t i = 1, ns = -(numberOfTimeslices + 1); i <= h1->GetNbinsX(); ++i) {
349 
350  const Double_t x = h1->GetBinCenter (i);
351  const Double_t y = h1->GetBinContent(i);
352 
353  while (x > (ns + 1) * getFrameTime() - BOOST * TMax_ns) {
354  ++ns;
355  }
356 
357  if (fabs(x - ns * getFrameTime()) < TMax_ns)
358  sn[ns].put(y);
359  else if (fabs(x - ns * getFrameTime()) < BOOST * TMax_ns)
360  bg[ns].put(y);
361  }
362 
363  NOTICE("Module " << setw(8) << module->getID() << endl);
364 
365  for (map<int, JWeight>::iterator i = sn.begin(); i != sn.end(); ) {
366 
367  const Double_t y = getBackground(i->second, bg[i->first]);
368  const Double_t P = TMath::PoissonI(i->second.getTotal(), y);
369 
370  if (debug >= debug_t || i->first == 0) {
371  cout << "Peak at " << setw(4) << i->first << " [frame time] "
372  << "S/N = "
373  << FIXED(7,1) << i->second.getTotal() << " / "
374  << FIXED(7,1) << y << ' '
375  << SCIENTIFIC(12,3) << P << endl;
376  }
377 
378  if (i->second.getTotal() > y && P < Pmin)
379  ++i; // keep
380  else
381  sn.erase(i++); // remove
382  }
383 
384  if (!(sn.size() == 1 &&
385  sn.begin()->first == 0)) {
386 
387  ERROR("Module " << setw(8) << module->getID() << " number of peaks " << sn.size() << endl);
388 
389  for (map<int, JWeight>::const_iterator i = sn.begin(); i != sn.end(); ++i) {
390 
391  ERROR("Peak at " << setw(4) << i->first << " [frame time] "
392  << "S/N = "
393  << FIXED(7,1) << i->second.getTotal() << " / "
394  << FIXED(7,1) << getBackground(i->second, bg[i->first]) << endl);
395  }
396 
397  status[module->getID()] = (sn.size() == 1 ? OUT_SYNC : ERROR);
398  }
399  }
400  }
401 
402  if (overwriteDetector) {
403 
404  detector.comment.add(JMeta(argc, argv));
405 
406  for (map<int, JStatus_t>::const_iterator i = status.begin(); i != status.end(); ++i) {
407 
408  if (i->second != IN_SYNC &&
409  i->second != NODATA) {
410 
411  NOTICE("Module " << setw(8) << i->first << " set out-of-sync." << endl);
412 
413  JModule& module = detector.getModule(router.getAddress(i->first));
414 
415  module.getStatus().set(MODULE_OUT_OF_SYNC);
416 
417  for (JModule::iterator pmt = module.begin(); pmt != module.end(); ++pmt) {
418  pmt->getStatus().set(OUT_OF_SYNC);
419  }
420  }
421  }
422 
423  store(detectorFile.c_str(), detector);
424  }
425 
426  if (outputFile != "") {
427  manager.Write(outputFile.c_str());
428  }
429 
430  int nin = 0;
431  int nout = 0;
432 
433  for (map<int, JStatus_t>::const_iterator i = status.begin(); i != status.end(); ++i) {
434  if (i->second == IN_SYNC) {
435  ++nin;
436  }
437  if (i->second != IN_SYNC &&
438  i->second != NODATA) {
439  ++nout;
440  }
441  }
442 
443  NOTICE("Number of modules out/in sync " << nout << '/' << nin << endl);
444 
445  QAQC(nin << ' ' << nout << endl);
446 
447  return 0;
448 }
JDetector detector
Definition: JRunAnalyzer.hh:23
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
void clear()
Clear data.
debug
Definition: JMessage.hh:29
Data structure for a composite optical module.
Definition: JModule.hh:68
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:89
Template const_iterator.
Definition: JDAQEvent.hh:68
Auxiliary class to select ROOT class based on class name.
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
Auxialiary class to assert type conversion.
Definition: JConversion.hh:66
Auxiliary class for a type holder.
Definition: JType.hh:19
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
double getTime(const Hit &hit)
Get true time of hit.
string outputFile
Template definition for direct access of elements in ROOT TChain.
Definition: JTreeScanner.hh:91
Simple wrapper around JModuleRouter class for direct addressing of PMT data in detector data structur...
int getFrameIndex() const
Get frame index.
The template JSinglePointer class can be used to hold a pointer to an object.
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
Detector file.
Definition: JHead.hh:224
void set(const int bit)
Set PMT status.
Definition: JStatus.hh:131
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
Data time slice.
int debug
debug level
Definition: JSirene.cc:68
double getMaximalTime(const double R_Hz)
Get maximal time for given rate.
Auxiliary class to select JTreeScanner based on ROOT class name.
Template L1 hit builder.
Definition: JBuildL1.hh:85
#define FATAL(A)
Definition: JMessage.hh:67
const JStatus & getStatus() const
Get status.
Definition: JStatus.hh:63
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
#define QAQC(A)
QA/QC output macro.
Definition: JMessage.hh:100
static const int OUT_OF_SYNC
Enable (disable) synchronous signal from this PMT if this status bit is 0 (1);.
Definition: pmt_status.hh:17
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
static const int MODULE_OUT_OF_SYNC
Enable (disable) synchronous signal from this module if this status bit is 0 (1);.
Auxiliary data structure for L1 build parameters.
Definition: JBuildL1.hh:37
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
Template L0 hit builder.
Definition: JBuildL0.hh:35
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 source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
int qaqc
QA/QC file descriptor.
then $DIR JPlotNPE PDG P
Definition: JPlotNPE-PDG.sh:62