Jpp  18.2.1-ARCA-DF-PATCH
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Namespaces | Typedefs | Functions
JPerth.cc File Reference

Program to determine inter-string time calibration. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <type_traits>
#include <functional>
#include <future>
#include <mutex>
#include <thread>
#include <vector>
#include <set>
#include <memory>
#include <algorithm>
#include "km3net-dataformat/online/JDAQEvent.hh"
#include "km3net-dataformat/definitions/fitparameters.hh"
#include "JDAQ/JDAQEventIO.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JDynamics/JDynamics.hh"
#include "JSupport/JMeta.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JParallelFileScanner.hh"
#include "JSupport/JSummaryFileRouter.hh"
#include "JReconstruction/JHitW0.hh"
#include "JReconstruction/JEvt.hh"
#include "JReconstruction/JEvtToolkit.hh"
#include "JReconstruction/JMuonGandalfParameters_t.hh"
#include "JTrigger/JHitL0.hh"
#include "JTrigger/JBuildL0.hh"
#include "JFit/JFitToolkit.hh"
#include "JFit/JLine1Z.hh"
#include "JFit/JLine3Z.hh"
#include "JFit/JModel.hh"
#include "JFit/JGandalf.hh"
#include "JFit/JRegressorHelper.hh"
#include "JFit/JLine3ZRegressor.hh"
#include "JFit/JGradient.hh"
#include "JLang/JSTDObjectReader.hh"
#include "JGeometry3D/JRotation3D.hh"
#include "JTools/JRange.hh"
#include "JTools/JQuantile.hh"
#include "Jeep/JProperties.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Classes

struct  JRECONSTRUCTION::event_type
 Auxiliary data structure to store data and fit in memory. More...
 
struct  JRECONSTRUCTION::JStringEditor
 Auxiliary class to edit time offset of data per string. More...
 
struct  JRECONSTRUCTION::JPerth
 Thread pool for fits to data. More...
 
struct  JRECONSTRUCTION::JPerth_t
 Auxiliary data structure for chi2 function object. More...
 

Namespaces

 JRECONSTRUCTION
 Model fits to data.
 

Typedefs

typedef std::vector< JHitW0JRECONSTRUCTION::buffer_type
 hits More...
 
typedef std::map< int,
buffer_type
JRECONSTRUCTION::map_type
 string -> hits More...
 
typedef std::vector< event_typeJRECONSTRUCTION::data_type
 
typedef
JLANG::JSTDObjectReader< const
event_type
JRECONSTRUCTION::input_type
 
typedef
JFIT::JRegressorStorage
< JFIT::JLine3Z,
JFIT::JGandalf
JRECONSTRUCTION::JRegressorStorage_t
 
typedef JFIT::JRegressor
< JFIT::JLine3Z,
JFIT::JGandalf
JRECONSTRUCTION::JRegressor_t
 

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to determine inter-string time calibration.

Author
mdejong

Definition in file JPerth.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 277 of file JPerth.cc.

278 {
279  using namespace std;
280  using namespace JPP;
281  using namespace KM3NETDAQ;
282 
283  typedef JParallelFileScanner< JTypeList<JDAQEvent, JFIT::JEvt> > JParallelFileScanner_t;
284  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
286  JACOUSTICS::JEvt>::typelist calibration_types;
287  typedef JMultipleFileScanner<calibration_types> JCalibration_t;
288 
289  JMultipleFileScanner<> inputFile;
290  JLimit_t& numberOfEvents = inputFile.getLimit();
291  string detectorFile;
292  JCalibration_t calibrationFile;
293  double Tmax_s;
294  string pdfFile;
296  bool overwriteDetector;
297  set<int> ids; // string identifiers
298  int number_of_iterations = 1000;
299  int number_of_extra_steps = 0;
300  double epsilon = 1.0e-4;
301  double T_ns = 2.5; // step size
302  size_t threads; // number of threads
303  int debug;
304 
305  try {
306 
307  JProperties properties;
308 
309  properties.insert(gmake_property(number_of_iterations));
310  properties.insert(gmake_property(number_of_extra_steps));
311  properties.insert(gmake_property(epsilon));
312  properties.insert(gmake_property(T_ns));
313 
314  JParser<> zap("Program to determine inter-string time calibration.");
315 
316  zap['f'] = make_field(inputFile);
317  zap['a'] = make_field(detectorFile);
318  zap['+'] = make_field(calibrationFile) = JPARSER::initialised();
319  zap['T'] = make_field(Tmax_s) = 100.0;
320  zap['n'] = make_field(numberOfEvents) = JLimit::max();
321  zap['P'] = make_field(pdfFile);
323  zap['A'] = make_field(overwriteDetector, "overwrite detector file provided through '-a' with fitted time offsets.");
324  zap['S'] = make_field(ids, "string identifiers (none = all)") = JPARSER::initialised();
325  zap['%'] = make_field(properties, "fit parameters") = JPARSER::initialised();
326  zap['N'] = make_field(threads, "number of threads") = 1;
327  zap['d'] = make_field(debug) = 1;
328 
329  zap(argc, argv);
330  }
331  catch(const exception& error) {
332  FATAL(error.what() << endl);
333  }
334 
335 
337 
338  try {
339  load(detectorFile, detector);
340  }
341  catch(const JException& error) {
342  FATAL(error);
343  }
344 
345  unique_ptr<JDynamics> dynamics;
346 
347  try {
348 
349  dynamics.reset(new JDynamics(detector, Tmax_s));
350 
351  dynamics->load(calibrationFile);
352  }
353  catch(const exception& error) {
354  if (!calibrationFile.empty()) {
355  FATAL(error.what());
356  }
357  }
358 
359  const JModuleRouter router(dynamics ? dynamics->getDetector() : detector);
360 
361  if (ids.empty()) {
362  ids = getStringIDs(detector);
363  }
364 
365  NOTICE("Reading PDFs... " << flush);
366 
367  const JRegressorStorage_t storage(pdfFile, parameters.TTS_ns);
368 
369  NOTICE("OK" << endl);
370 
372  JRegressor_t::T_ns.setRange(parameters.TMin_ns, parameters.TMax_ns);
373  JRegressor_t::Vmax_npe = parameters.VMax_npe;
374  JRegressor_t::MAXIMUM_ITERATIONS = parameters.NMax;
375 
376 
377  // preprocess input data
378 
379  const JBuildL0<JHitL0> buildL0;
380 
381  data_type data;
382 
383  counter_type skip = inputFile.getLowerLimit();
384  counter_type counter = inputFile.getLowerLimit();
385 
386  for (JMultipleFileScanner<>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
387 
388  JSummaryFileRouter summary(*i, parameters.R_Hz);
389 
390  for (JParallelFileScanner_t in(*i); (skip -= in.skip(skip)) == 0 && in.hasNext() && counter != numberOfEvents; ++counter) {
391 
392  STATUS("event: " << setw(10) << counter << '\r'); DEBUG(endl);
393 
394  multi_pointer_type ps = in.next();
395 
396  const JDAQEvent* tev = ps;
397  const JFIT::JEvt* evt = ps;
398 
399  summary.update(*tev);
400 
401  if (dynamics) {
402  dynamics->update(*tev);
403  }
404 
405  if (!evt->empty()) {
406 
407  vector<JHitL0> dataL0;
408 
409  buildL0(*tev, router, true, back_inserter(dataL0));
410 
411  const JFIT::JFit& fit = (*evt)[0];
412 
413  const JRotation3D R (getDirection(fit));
414  const JLine1Z tz(getPosition (fit).rotate(R), fit.getT());
415  JRange<double> Z_m;
416 
417  if (fit.hasW(JSTART_LENGTH_METRES) &&
418  fit.getW(JSTART_LENGTH_METRES) > 0.0) {
419  Z_m = JZRange(parameters.ZMin_m, parameters.ZMax_m + fit.getW(JSTART_LENGTH_METRES));
420  }
421 
422  const JFIT::JModel<JLine1Z> match(tz, parameters.roadWidth_m, JRegressor_t::T_ns, Z_m);
423 
424  // hit selection based on start value
425 
426  buffer_type buffer;
427 
428  for (vector<JHitL0>::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
429 
430  JHitW0 hit(*i, summary.getRate(i->getPMTIdentifier()));
431 
432  hit.rotate(R);
433 
434  if (match(hit)) {
435  buffer.push_back(hit);
436  }
437  }
438 
439  // select first hit
440 
441  sort(buffer.begin(), buffer.end(), JHitL0::compare);
442 
443  buffer_type::const_iterator __end = unique(buffer.begin(), buffer.end(), equal_to<JDAQPMTIdentifier>());
444 
445  // re-organise data
446 
447  map_type map;
448 
449  for (buffer_type::const_iterator hit = buffer.begin(); hit != __end; ++hit) {
450 
451  const JModule& module = router.getModule(hit->getModuleID());
452 
453  map[module.getString()].push_back(*hit);
454  }
455 
456  data.push_back({map, tz});
457  }
458  }
459  }
460  STATUS(endl);
461 
462 
463  // fit
464 
465  JGradient fit(number_of_iterations, number_of_extra_steps, epsilon, 3);
466 
467  for (int id : ids) {
468  fit.push_back(JModifier_t(MAKE_STRING("string: " << FILL(4,'0') << id << FILL()), new JStringEditor(data, id), T_ns));
469  }
470 
471  const JPerth_t perth = {storage, data, threads};
472 
473  const double chi2 = fit(perth);
474 
475  STATUS("result: " << FIXED(12,6) << chi2 << ' ' << setw(6) << fit.numberOfIterations << endl);
476 
477  for (size_t i = 0; i != fit.size(); ++i) {
478 
479  const JStringEditor* p = dynamic_cast<const JStringEditor*>(fit[i].get());
480 
481  if (p != NULL) {
482  STATUS("string: " << FILL(4,'0') << p->id << FILL() << ' ' << FIXED(9,3) << p->t0 << " [ns]" << endl);
483  }
484  }
485 
486 
487  if (overwriteDetector) {
488 
489  detector.comment.add(JMeta(argc, argv));
490 
492 
493  double t0 = 0.0;
494 
495  for (size_t i = 0; i != fit.size(); ++i) {
496 
497  const JStringEditor* p = dynamic_cast<const JStringEditor*>(fit[i].get());
498 
499  if (p != NULL) {
500 
501  calibration[p->id] = p->t0;
502 
503  t0 += p->t0;
504  }
505  }
506 
507  t0 /= calibration.size();
508 
509  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
510 
511  if (!module->empty()) {
512 
513  map<int, double>::const_iterator p = calibration.find(module->getString());
514 
515  if (p != calibration.end()) {
516  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
517  module->getPMT(pmt).addT0(p->second - t0);
518  }
519  }
520  }
521  }
522 
523  NOTICE("Store calibration data on file " << detectorFile << endl);
524 
525  store(detectorFile, detector);
526  }
527 }
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:1514
General exception.
Definition: JException.hh:24
Auxiliary data structure for chi2 function object.
Definition: JPerth.cc:249
Data structure for a composite optical module.
Definition: JModule.hh:68
std::map< int, buffer_type > map_type
string -&gt; hits
Definition: JPerth.cc:68
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:102
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
Auxiliary data structure for sorting of hits.
Definition: JHitL0.hh:85
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:89
int id
string identifier
Definition: JPerth.cc:123
Router for direct addressing of module data in detector data structure.
Rotation matrix.
Definition: JRotation3D.hh:111
std::set< int > getStringIDs(const JDetector &detector)
Get list of strings identifiers.
Utility class to parse parameter values.
Definition: JProperties.hh:497
*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
General purpose class for parallel reading of objects from a single file or multiple files...
Template specialisation of class JModel to match hit with muon trajectory along z-axis.
Definition: JFit/JModel.hh:34
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Long64_t counter_type
Type definition for counter.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
Template data structure for storage for PDF tables.
Auxiliary class to edit time offset of data per string.
Definition: JPerth.cc:85
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
Calibration.
Definition: JHead.hh:328
Data structure for track fit results with history and optional associated values. ...
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
Detector file.
Definition: JHead.hh:226
JDirection3D getDirection(const Vec &dir)
Get direction.
bool hasW(const int i) const
Check availability of value.
Acoustic event fit.
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
double getT() const
Get time.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
JPosition3D getPosition(const Vec &pos)
Get position.
#define NOTICE(A)
Definition: JMessage.hh:64
File router for fast addressing of summary data.
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:173
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
#define FATAL(A)
Definition: JMessage.hh:67
Auxiliary data structure for editable parameter.
Definition: JGradient.hh:47
Dynamic detector calibration.
Definition: JDynamics.hh:81
then JCookie sh JDataQuality D $DETECTOR_ID R
Definition: JDataQuality.sh:41
Auxiliary class for a hit with background rate value.
Definition: JHitW0.hh:21
int getString() const
Get string number.
Definition: JLocation.hh:134
static const int JSTART_LENGTH_METRES
distance between first and last hits in metres from JStart.cc
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Data structure for set of track fit results.
General purpose class for object reading from a list of file names.
double t0
time offset [ns]
Definition: JPerth.cc:124
then if[[!-f $DETECTOR]] then JDetector sh $DETECTOR fi cat $WORKDIR trigger_parameters txt<< EOFtrigger3DMuon.enabled=1;trigger3DMuon.numberOfHits=5;trigger3DMuon.gridAngle_deg=1;ctMin=0.0;TMaxLocal_ns=15.0;EOF set_variable TRIGGEREFFICIENCY_TRIGGERED_EVENTS_ONLY INPUT_FILES=() for((i=1;$i<=$NUMBER_OF_RUNS;++i));do JSirene.sh $DETECTOR $JPP_DATA/genhen.km3net_wpd_V2_0.evt.gz $WORKDIR/sirene_ ${i}.root JTriggerEfficiency.sh $DETECTOR $DETECTOR $WORKDIR/sirene_ ${i}.root $WORKDIR/trigger_efficiency_ ${i}.root $WORKDIR/trigger_parameters.txt $JPP_DATA/PMT_parameters.txt INPUT_FILES+=($WORKDIR/trigger_efficiency_ ${i}.root) done for ANGLE_DEG in $ANGLES_DEG[*];do set_variable SIGMA_NS 3.0 set_variable OUTLIERS 3 set_variable OUTPUT_FILE $WORKDIR/matrix\[${ANGLE_DEG}\deg\].root $JPP_DIR/examples/JReconstruction-f"$INPUT_FILES[*]"-o $OUTPUT_FILE-S ${SIGMA_NS}-A ${ANGLE_DEG}-O ${OUTLIERS}-d ${DEBUG}--!fiif[[$OPTION=="plot"]];then if((0));then for H1 in h0 h1;do JPlot1D-f"$WORKDIR/matrix["${^ANGLES_DEG}" deg].root:${H1}"-y"1 2e3"-Y-L TR-T""-\^"number of events [a.u.]"-> o chi2
Definition: JMatrixNZ.sh:106
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Definition: JFitK40.hh:99
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
void addT0(const double t0)
Add time offset.
Orientation of module.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:84
do set_variable DETECTOR_TXT $WORKDIR detector
const char * map
Definition: elog.cc:87
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
const std::vector< double > & getW() const
Get associated values.
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
Conjugate gradient fit.
Definition: JGradient.hh:73
JTOOLS::JRange< double > JZRange
Definition: JFit/JModel.hh:21
const double epsilon
Definition: JQuadrature.cc:21
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62