Jpp  18.0.1-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Enumerations | Functions
JTurbotIter.cc File Reference

Example program to search for correlations between triggered events and timeslice 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 "JDAQ/JDAQEventIO.hh"
#include "JDAQ/JDAQTimesliceIO.hh"
#include "JDAQ/JDAQEvaluator.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JDAQHitRouter.hh"
#include "JDetector/JPMTParametersMap.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 timeslice data.

Author
mdejong

Definition in file JTurbotIter.cc.

Enumeration Type Documentation

enum JStatus_t

Definition at line 44 of file JTurbotIter.cc.

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

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 60 of file JTurbotIter.cc.

61 {
62  using namespace std;
63  using namespace JPP;
64  using namespace KM3NETDAQ;
65 
66  JSingleFileScanner<> inputFile;
67  JLimit_t& numberOfEvents = inputFile.getLimit();
68  string outputFile;
69  string detectorFile;
70  double TMaxLocal_ns;
71  string pmtFile;
72  int numberOfTimeslices;
73  double binWidth_ns;
74  double Pmin;
75  JROOTClassSelector selector;
76  int qaqc;
77  int debug;
78 
79  string peaks;
80 
81  try {
82 
83  JParser<> zap("Example program to search for correlations between triggered events and timeslice data.");
84 
85  zap['f'] = make_field(inputFile);
86  zap['o'] = make_field(outputFile) = "";
87  zap['a'] = make_field(detectorFile);
88  zap['n'] = make_field(numberOfEvents) = JLimit::max();
89  zap['T'] = make_field(TMaxLocal_ns) = 20.0;
90  zap['P'] = make_field(pmtFile) = "";
91  zap['N'] = make_field(numberOfTimeslices) = 100;
92  zap['W'] = make_field(binWidth_ns) = 10e3;
93  zap['p'] = make_field(Pmin) = 1.0e-6;
94  zap['C'] = make_field(selector) = "", getROOTClassSelection<JDAQTimesliceTypes_t>();
95  zap['Q'] = make_field(qaqc) = 0;
96  zap['d'] = make_field(debug) = 2;
97 
98  zap['O'] = make_field(peaks) = ""; //Make a small summary file, with the list of the peaks
99 
100  zap.read(argc, argv);
101  }
102  catch(const exception& error) {
103  FATAL(error.what() << endl);
104  }
105 
106 
108 
109  try {
110  load(detectorFile, detector);
111  }
112  catch(const JException& error) {
113  FATAL(error);
114  }
115 
116  const JDAQHitRouter router(detector);
117 
118  const double TMax_ns = max(binWidth_ns, getMaximalTime(detector));
119 
120  typedef double hit_type;
121 
122  JBuildL0<hit_type> buildL0;
125 
126  typedef JManager<int, TH1D> JManager_t;
127 
128  const Double_t xmin = -(numberOfTimeslices + 1) * getFrameTime() - 0.5*binWidth_ns;
129  const Double_t xmax = +(numberOfTimeslices + 1) * getFrameTime() + 0.5*binWidth_ns;
130  const Int_t nx = (Int_t) ((xmax - xmin) / binWidth_ns);
131 
132  JManager_t manager(new TH1D("M_%", NULL, nx, xmin, xmax));
133 
135 
137 
139 
140  if (selector == "") {
141 
142  if ((ps = new JTreeScanner< JAssertConversion<JDAQTimesliceSN, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
143  (ps = new JTreeScanner< JAssertConversion<JDAQTimesliceL2, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
144  (ps = new JTreeScanner< JAssertConversion<JDAQTimesliceL1, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
145  (ps = new JTreeScanner< JAssertConversion<JDAQTimesliceL0, JDAQTimeslice> >(inputFile))->getEntries() != 0 ||
146  (ps = new JTreeScanner< JAssertConversion<JDAQTimeslice, JDAQTimeslice> >(inputFile))->getEntries() != 0) {
147  } else {
148  FATAL("No timeslice data." << endl);
149  }
150 
151  NOTICE("Selected class " << ps->getClassName() << endl);
152 
153  } else {
154 
155  ps = zmap[selector];
156 
157  ps->configure(inputFile);
158  }
159 
160  /* Updated method :
161  * OOS DOMs can have an impact on the histogram of other DOMs
162  * especialy OOS DOMs on adjacent In Sync or OOS DOMs (presence of ghost peaks)
163  * To remove these ghost peaks, the idea is to iterate the method, removing
164  * at each loop the most OOS DOM, and recomputing the histograms without
165  * them (removing events where they are involved)
166  * Once there is no more OOS DOMs in the remaining DOMs, end of precedure.
167  * Steps of procedure (from J. Hofestaedt, code updated by R. Le Breton)
168  * 1) Check if any DOM shows a peak away from 0 (OOS)
169  * If no OOS -> finish procedure
170  * 2) If at least one OOS, then identify the DOM that is most OOS,
171  * ie. highest peak (other than delta time = 0)
172  * 3) Remove the most OOS DOM and calculate the histograms again but do not
173  * consider the OOS DOM (in later iterations more than one DOM is OOS and not
174  * considered for histogram filling).
175  * 4) start with 1) again
176  */
177 
178  ps->setLimit(inputFile.getLimit());
179 
180  typedef map<int, vector<double> > map_type; // frame index -> event times
181 
182  bool OOS = true; //Boolean: to continue or not the procedure
183  map_type buffer;
184 
185  map_type peaksPerDoms;
186 
187  map<int, JStatus_t> status2;
188 
189  //OOS DOMs IDs : if at least one OOS DOM, one ID is added into oosDomsId at each iteration
190  map<int, bool> oosDomsId;
191 
192  //Default value for all modules : false
193  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
194  oosDomsId[module->getID()] = false;
195  }
196 
197  do{
198  for (JTreeScanner<JDAQEvent> in(inputFile.getFilename()); in.hasNext(); ) {
199 
200  STATUS("event: " << setw(10) << in.getCounter() << '\r'); DEBUG(endl);
201 
202  JDAQEvent* event = in.next();
203 
204  // Average time of triggered hits
205 
206  double t0 = 0.0;
207  bool test_OOS = false; //If an OOS DOM is involved in an event, remove this event
208 
209  for (JDAQEvent::const_iterator<JDAQTriggeredHit> hit = event->begin<JDAQTriggeredHit>(); hit != event->end<JDAQTriggeredHit>(); ++hit) {
210  t0 += getTime(*hit, router.getPMT(*hit));
211 
212  if(oosDomsId[hit->getModuleID()]) {
213  test_OOS = true;
214  break;
215  }
216  }
217 
218  if(test_OOS) continue; //If true, event not taken into account
219 
220  t0 /= event->size<JDAQTriggeredHit>();
221  t0 += event->getFrameIndex() * getFrameTime();
222 
223  buffer[event->getFrameIndex()].push_back(t0);
224  }
225  STATUS(endl);
226 
227  //Events : start from the beginning
228  ps->rewind();
229 
230  while (ps->hasNext()) {
231 
232  STATUS("event: " << setw(10) << ps->getCounter() << '\r'); DEBUG(endl);
233 
234  JDAQTimeslice* timeslice = ps->next();
235 
236  map_type::const_iterator p = buffer.lower_bound(timeslice->getFrameIndex() - numberOfTimeslices);
237  map_type::const_iterator q = buffer.upper_bound(timeslice->getFrameIndex() + numberOfTimeslices);
238 
239  int number_of_events = 0;
240 
241  for (map_type::const_iterator i = p; i != q; ++i) {
242  number_of_events += i->second.size();
243  }
244 
245  if (number_of_events == 0) {
246  continue;
247  }
248 
249  for (JDAQTimeslice::const_iterator frame = timeslice->begin(); frame != timeslice->end(); ++frame) {
250 
251  data.clear();
252 
253  buildL1(*frame, router.getModule(frame->getModuleID()), back_inserter(data));
254 
255  TH1D* h1 = manager[frame->getModuleID()];
256 
257  for (vector<hit_type>::const_iterator hit = data.begin(); hit != data.end(); ++hit) {
258 
259  const double t1 = *hit + frame->getFrameIndex() * getFrameTime();
260 
261  for (map_type::const_iterator i = p; i != q; ++i) {
262  for (map_type::mapped_type::const_iterator j = i->second.begin(); j != i->second.end(); ++j) {
263 
264  const double t0 = *j;
265 
266  h1->Fill(t1 - t0);
267  }
268  }
269  }
270  }
271  }
272 
273  buffer.clear(); //Clear the buffer for the next loop, if any
274 
275  STATUS(endl);
276 
277  Double_t most_OOS_peak = 0;
278  Int_t most_OOS_ID = 0;
279 
280  TF1 f1("f1", "[0]*exp(-0.5*(x-[1])*(x-[1])/([2]*[2])) + [3]");
281 
282  string option = "L";
283 
284  if (debug < debug_t) {
285  option += "Q";
286  }
287 
288  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
289 
290  bool status = false;
291 
292  status2[module->getID()] = DEFAULT;
293 
294  JManager_t::iterator p = manager.find(module->getID());
295 
296  if (p == manager.end() || p->second->GetEntries() == 0) {
297 
298  status2[module->getID()] = NODATA;
299 
300  continue;
301  }
302 
303  TH1D* h1 = p->second;
304 
305  // start values
306 
307  Double_t ymax = 0.0;
308  Double_t x0 = 0.0; // [ns]
309  Double_t sigma = 250.0; // [ns]
310  Double_t ymin = 0.0;
311 
312  for (int i = 1; i != h1->GetNbinsX(); ++i) {
313 
314  const Double_t x = h1->GetBinCenter (i);
315  const Double_t y = h1->GetBinContent(i);
316 
317  if (y > ymax) {
318  ymax = y;
319  x0 = x;
320  }
321 
322  ymin += y;
323  }
324 
325  ymin /= h1->GetNbinsX();
326 
327  f1.SetParameter(0, ymax);
328  f1.SetParameter(1, x0);
329  if (binWidth_ns < sigma)
330  f1.SetParameter(2, sigma);
331  else
332  f1.FixParameter(2, binWidth_ns/sqrt(12.0));
333  f1.SetParameter(3, ymin);
334 
335  // fit
336 
337  h1->Fit(&f1, option.c_str(), "same", x0 - 5 * sigma, x0 + 5 * sigma);
338 
339  status = (fabs(f1.GetParameter(1)) <= 0.5*getFrameTime() && // position
340  f1.GetParameter(0) >= f1.GetParameter(3)); // S/N
341 
342  if (status) status2[module->getID()] = IN_SYNC;
343 
344 
345  // look for peaks at offsets equal to a multiple of frame time
346  JWeight bg; // background
347  map<int, JWeight> sn; // signal(s)
348 
349  for (Int_t i = 1, ns = -(numberOfTimeslices + 1); i <= h1->GetNbinsX(); ++i) {
350 
351  const Double_t x = h1->GetBinCenter (i);
352  const Double_t y = h1->GetBinContent(i);
353 
354  while (x > (ns + 1) * getFrameTime() - TMax_ns) { ++ns; }
355 
356  if (fabs(x - ns * getFrameTime()) < TMax_ns)
357  sn[ns].put(y);
358  else
359  bg .put(y);
360  }
361 
362  DEBUG("Module " << setw(8) << module->getID() << endl);
363 
364  for (map<int, JWeight>::iterator i = sn.begin(); i != sn.end(); ) {
365 
366  const Double_t y = bg.getTotal() * i->second.getCount() / bg.getCount();
367  const Double_t P = TMath::PoissonI(i->second.getTotal(), y);
368 
369  DEBUG("Peak at " << setw(4) << i->first << " [frame time] "
370  << "S/N = "
371  << FIXED(7,1) << i->second.getTotal() << " / "
372  << FIXED(7,1) << y << ' '
373  << SCIENTIFIC(12,3) << P << endl);
374 
375  if (P < Pmin && i->second.getTotal() > y) //Avoid edge effect (second bolean)
376  ++i; // keep
377  else
378  sn.erase(i++); // remove
379  }
380 
381  if (!(sn.size() == 1 &&
382  sn.begin()->first == 0)) {
383 
384  WARNING("Module " << setw(8) << module->getID() << " number of peaks " << sn.size() << endl);
385 
386  for (map<int, JWeight>::const_iterator i = sn.begin(); i != sn.end(); ++i) {
387 
388  const Double_t noise = bg.getTotal() * i->second.getCount() / bg.getCount();
389 
390  WARNING("Peak at " << setw(4) << i->first << " [frame time] "
391  << "S/N = "
392  << FIXED(7,1) << i->second.getTotal() << " / "
393  << FIXED(7,1) << noise << endl);
394 
395  peaksPerDoms[module->getID()].push_back(sn.size());
396  peaksPerDoms[module->getID()].push_back(i->first);
397  peaksPerDoms[module->getID()].push_back(i->second.getTotal());
398  peaksPerDoms[module->getID()].push_back(noise);
399  }
400 
401  status2[module->getID()] = (sn.size() == 1 ? OUT_SYNC : ERROR);
402  }
403 
404 
405  //if DOM is OOS
406  if (!status) {
407  //if OOS peak greater than the last and not in OOS DOMs vector
408  if ((f1.GetParameter(0) > most_OOS_peak) && !(oosDomsId[module->getID()])){
409 
410  most_OOS_peak = f1.GetParameter(0);
411  most_OOS_ID = module->getID();
412  }
413  }
414  }
415 
416  if (most_OOS_ID != 0) {
417  oosDomsId[most_OOS_ID] = true;//exclude the DOM to compute the histograms
418 
419  //Reseting all histograms
420  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
421  manager[module->getID()]->Reset("ICESM");
422  }
423  peaksPerDoms.clear();
424  }
425  else{
426  OOS = false;
427  }
428  }while(OOS);
429 
430  //To manage non working and OOS DOMs
431  NOTICE("Managing non-working and OOS DOMs." << endl);
432  if (pmtFile != "") {
433 
435 
436  try {
437  parameters.load(pmtFile.c_str());
438  }
439  catch(const JException& error) {}
440 
441  for (map<int, JStatus_t>::const_iterator i = status2.begin(); i != status2.end(); ++i) {
442 
443  if (i->second != IN_SYNC) {
444 
445  NOTICE("Module " << setw(8) << i->first << " set QEs of all PMTs to zero." << endl);
446 
447  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
448  parameters[JPMTIdentifier(i->first, pmt)].QE = 0.0;
449  }
450  }
451  }
452 
453  ofstream out(pmtFile.c_str());
454 
455  parameters.comment.add(JMeta(argc, argv));
456 
457  out << parameters << endl;
458 
459  out.close();
460  }
461 
462  if (outputFile != "") {
463  manager.Write(outputFile.c_str());
464  }
465 
466  //print small summary file of the peaks
467  if (peaks != ""){
468  ofstream stream(peaks);
469 
470  stream << "#DOM NB_PEAKS TIMESLICE_SHIFT SIGNAL NOISE\n";
471 
472  for (map_type::const_iterator dom = peaksPerDoms.begin(); dom != peaksPerDoms.end(); ++dom) {
473  stream << dom->first << " ";
474  int i = 1;
475  for (map_type::mapped_type::const_iterator data = dom->second.begin(); data != dom->second.end(); ++data) {
476  stream << *data;
477  ((i%4 == 0) ? stream << "\n" : stream << " ");
478  i++;
479  }
480  }
481  stream.close();
482  }
483 
484  //QAQC
485  int nin = 0;
486  int nout = 0;
487 
488  for (map<int, JStatus_t>::const_iterator i = status2.begin(); i != status2.end(); ++i) {
489  if (i->second == IN_SYNC) {
490  ++nin;
491  }
492  if (i->second != IN_SYNC &&
493  i->second != NODATA) {
494  ++nout;
495  }
496  }
497 
498  NOTICE("Number of modules out/in sync " << nout << '/' << nin << endl);
499 
500  QAQC(nin << ' ' << nout << endl);
501 
502  return 0;
503 
504 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
General exception.
Definition: JException.hh:23
debug
Definition: JMessage.hh:29
Auxiliary data structure to unify weights of acoustics data according to the number of pings per emit...
#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.
std::vector< size_t > ns
*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
Auxialiary class to assert type conversion.
Definition: JConversion.hh:43
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.
Simple wrapper around JModuleRouter class for direct addressing of PMT data in detector data structur...
int getFrameIndex() const
Get frame index.
const double sigma[]
Definition: JQuadrature.cc:74
The template JSinglePointer class can be used to hold a pointer to an object.
const JPolynome f1(1.0, 2.0, 3.0)
Function.
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:226
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
double getFrameTime()
Get frame time duration.
Definition: JDAQClock.hh:162
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
Data time slice.
Auxiliary class for map of PMT parameters.
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
void load(const char *file_name)
Load from input file.
const double xmin
Definition: JQuadrature.cc:23
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
Auxiliary data structure for L1 build parameters.
Definition: JBuildL1.hh:37
int j
Definition: JPolint.hh:703
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:84
JComment & add(const std::string &comment)
Add comment.
Definition: JComment.hh:100
Template L0 hit builder.
Definition: JBuildL0.hh:35
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
then echo WARNING
Definition: JTuneHV.sh:91
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
int qaqc
QA/QC file descriptor.
int debug
debug level
then $DIR JPlotNPE PDG P
Definition: JPlotNPE-PDG.sh:62
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62