Jpp test-rotations-old-533-g2bdbdb559
the software that should make you happy
Loading...
Searching...
No Matches
JPerth.cc File Reference

Program to determine string or optical module time calibrations. 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/JLine3ZRegressor.hh"
#include "JFit/JGradient.hh"
#include "JLang/JSTDObjectReader.hh"
#include "JLang/JVectorize.hh"
#include "JGeometry3D/JRotation3D.hh"
#include "JMath/JMath.hh"
#include "JMath/JQuantile_t.hh"
#include "JTools/JRange.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::JEditor
 Auxiliary class for editing time offset. More...
 
struct  JRECONSTRUCTION::JPerth
 Thread pool for fits to data. More...
 
struct  JRECONSTRUCTION::JPerth_t
 Auxiliary data structure for chi2 function object. More...
 

Namespaces

namespace  JRECONSTRUCTION
 Support.
 

Typedefs

typedef std::vector< JHitW0JRECONSTRUCTION::buffer_type
 hits
 
typedef std::map< int, buffer_typeJRECONSTRUCTION::map_type
 identifier -> hits
 
typedef std::vector< event_typeJRECONSTRUCTION::data_type
 
typedef JLANG::JSTDObjectReader< const event_typeJRECONSTRUCTION::input_type
 
typedef JFIT::JRegressorStorage< JFIT::JLine3Z, JFIT::JGandalfJRECONSTRUCTION::JRegressorStorage_t
 
typedef JFIT::JRegressor< JFIT::JLine3Z, JFIT::JGandalfJRECONSTRUCTION::JRegressor_t
 

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to determine string or optical module time calibrations.

Author
mdejong

Definition in file JPerth.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 290 of file JPerth.cc.

291{
292 using namespace std;
293 using namespace JPP;
294 using namespace KM3NETDAQ;
295
296 typedef JParallelFileScanner< JTypeList<JDAQEvent, JFIT::JEvt> > JParallelFileScanner_t;
297 typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
299 JACOUSTICS::JEvt>::typelist calibration_types;
300 typedef JMultipleFileScanner<calibration_types> JCalibration_t;
301
302 JMultipleFileScanner<> inputFile;
303 JLimit_t& numberOfEvents = inputFile.getLimit();
304 string detectorFile;
305 JCalibration_t calibrationFile;
306 double Tmax_s;
307 string pdfFile;
308 JMuonGandalfParameters_t parameters;
309 bool overwriteDetector;
310 set<int> strings; // string identifiers
311 set<int> modules; // module identifiers
312 int number_of_iterations = 1000;
313 int number_of_extra_steps = 0;
314 double epsilon = 1.0e-4;
315 double T_ns = 2.5; // step size
316 size_t threads; // number of threads
317 int debug;
318
319 const int DEFAULT_ID = -1;
320
321 try {
322
323 JProperties properties;
324
325 properties.insert(gmake_property(number_of_iterations));
326 properties.insert(gmake_property(number_of_extra_steps));
327 properties.insert(gmake_property(epsilon));
328 properties.insert(gmake_property(T_ns));
329
330 JParser<> zap("Program to determine string or optical module time calibrations.");
331
332 zap['f'] = make_field(inputFile);
333 zap['a'] = make_field(detectorFile);
334 zap['+'] = make_field(calibrationFile) = JPARSER::initialised();
335 zap['T'] = make_field(Tmax_s) = 100.0;
336 zap['n'] = make_field(numberOfEvents) = JLimit::max();
337 zap['F'] = make_field(pdfFile);
338 zap['@'] = make_field(parameters) = JPARSER::initialised();
339 zap['A'] = make_field(overwriteDetector, "overwrite detector file provided through '-a' with fitted time offsets.");
340 zap['S'] = make_field(strings, "string identifiers (" << DEFAULT_ID << " => all)") = JPARSER::initialised();
341 zap['M'] = make_field(modules, "module identifiers (" << DEFAULT_ID << " => all)") = JPARSER::initialised();
342 zap['%'] = make_field(properties, "fit parameters") = JPARSER::initialised();
343 zap['N'] = make_field(threads, "number of threads") = 1;
344 zap['d'] = make_field(debug) = 1;
345
346 zap(argc, argv);
347 }
348 catch(const exception& error) {
349 FATAL(error.what() << endl);
350 }
351
352
353 if (strings.empty() == modules.empty()) {
354 FATAL("Set either strings (option -S) or modules (option -M)." << endl);
355 }
356
358
359 try {
360 load(detectorFile, detector);
361 }
362 catch(const JException& error) {
363 FATAL(error);
364 }
365
366 unique_ptr<JDynamics> dynamics;
367
368 try {
369
370 dynamics.reset(new JDynamics(detector, Tmax_s));
371
372 dynamics->load(calibrationFile);
373 }
374 catch(const exception& error) {
375 if (!calibrationFile.empty()) {
376 FATAL(error.what());
377 }
378 }
379
380 const JModuleRouter router(dynamics ? dynamics->getDetector() : detector);
381
382 NOTICE("Reading PDFs... " << flush);
383
384 const JRegressorStorage_t storage(pdfFile, JTimeRange(parameters.TMin_ns, parameters.TMax_ns), parameters.TTS_ns);
385
386 NOTICE("OK" << endl);
387
388 JRegressor_t::debug = debug;
389 JRegressor_t::Vmax_npe = parameters.VMax_npe;
390 JRegressor_t::MAXIMUM_ITERATIONS = parameters.NMax;
391
392 // preprocess input data
393
394 NOTICE("Reading data" << endl);
395
396 const JBuildL0<JHitL0> buildL0;
397
399
400 counter_type skip = inputFile.getLowerLimit();
401 counter_type counter = inputFile.getLowerLimit();
402
403 for (JMultipleFileScanner<>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
404
405 JSummaryFileRouter summary(*i);
406
407 for (JParallelFileScanner_t in(*i); (skip -= in.skip(skip)) == 0 && in.hasNext() && counter != numberOfEvents; ++counter) {
408
409 STATUS("event: " << setw(10) << counter << '\r'); DEBUG(endl);
410
411 multi_pointer_type ps = in.next();
412
413 const JDAQEvent* tev = ps;
414 JFIT::JEvt* evt = ps;
415
416 summary.update(*tev);
417
418 if (dynamics) {
419 dynamics->update(*tev);
420 }
421
422 JFIT::JEvt::iterator __end = partition(evt->begin(), evt->end(), JHistory::is_application(JMUONGANDALF));
423
424 if (evt->begin() != __end) {
425
426 sort(evt->begin(), __end, qualitySorter);
427
428 vector<JHitL0> dataL0;
429
430 buildL0(*tev, router, true, back_inserter(dataL0));
431
432 const JFIT::JFit& fit = (*evt)[0];
433
434 const JRotation3D R (getDirection(fit));
435 const JLine1Z tz(getPosition (fit).rotate(R), fit.getT());
436 JRange<double> Z_m;
437
438 if (fit.hasW(JSTART_LENGTH_METRES) &&
439 fit.getW(JSTART_LENGTH_METRES) > 0.0) {
440 Z_m = JZRange(parameters.ZMin_m, parameters.ZMax_m + fit.getW(JSTART_LENGTH_METRES));
441 }
442
443 const JFIT::JModel<JLine1Z> match(tz, parameters.roadWidth_m, JTimeRange(parameters.TMin_ns, parameters.TMax_ns), Z_m);
444
445 // hit selection based on start value
446
447 buffer_type buffer;
448
449 for (vector<JHitL0>::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
450
451 JHitW0 hit(*i, summary.getRate(i->getPMTIdentifier(), parameters.R_Hz));
452
453 hit.rotate(R);
454
455 if (match(hit)) {
456 buffer.push_back(hit);
457 }
458 }
459
460 // select first hit
461
462 sort(buffer.begin(), buffer.end(), JHitL0::compare);
463
464 buffer_type::const_iterator __end = unique(buffer.begin(), buffer.end(), equal_to<JDAQPMTIdentifier>());
465
466 // re-organise data
467
468 map_type map;
469
470 for (buffer_type::const_iterator hit = buffer.begin(); hit != __end; ++hit) {
471
472 const JModule& module = router.getModule(hit->getModuleID());
473
474 if (!strings.empty()) { map[module.getString()].push_back(*hit); }
475 if (!modules.empty()) { map[module.getID()] .push_back(*hit); }
476 }
477
478 data.push_back({map, tz, true});
479 }
480 }
481 }
482 STATUS(endl);
483 NOTICE("OK" << endl);
484
485
486 // fit
487
488 JGradient fit(number_of_iterations, number_of_extra_steps, epsilon, 3);
489
490 if (strings.count(DEFAULT_ID) != 0) { strings = getStringIDs(detector); }
491 if (modules.count(DEFAULT_ID) != 0) { modules = getModuleIDs(detector); }
492
493 for (int id : strings) { fit.push_back(JModifier_t(MAKE_STRING("string: " << FILL(4,'0') << id << FILL()), new JEditor(data, id), T_ns)); }
494 for (int id : modules) { fit.push_back(JModifier_t(MAKE_STRING("module: " << setw(8) << id), new JEditor(data, id), T_ns)); }
495
496 const JPerth_t perth = {storage, data, threads};
497
498 const double chi2 = fit(perth);
499
500 STATUS("result: " << FIXED(12,6) << chi2 << ' ' << setw(6) << fit.numberOfIterations << endl);
501
502 for (size_t i = 0; i != fit.size(); ++i) {
503 {
504 const JEditor* p = dynamic_cast<const JEditor*>(fit[i].get());
505
506 if (p != NULL) { STATUS(fit[i].name << ' ' << FIXED(9,3) << p->t0 << " [ns]" << endl); }
507 }
508 }
509
510
511 if (overwriteDetector) {
512
513 detector.comment.add(JMeta(argc, argv));
514
516
517 for (size_t i = 0; i != fit.size(); ++i) {
518
519 const JEditor* p = dynamic_cast<const JEditor*>(fit[i].get());
520
521 if (p != NULL) {
522 calibration[p->id] = p->t0;
523 }
524 }
525
526 const double t0 = getAverage(get_values(calibration), 0.0);
527
528 for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
529
530 if (!module->empty()) {
531
532 map<int, double>::const_iterator p = (!strings.empty() ? calibration.find(module->getString()) :
533 !modules.empty() ? calibration.find(module->getID()) :
534 calibration.end());
535 if (p != calibration.end()) {
536 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
537 module->getPMT(pmt).addT0(p->second - t0);
538 }
539 }
540 }
541 }
542
543 NOTICE("Store calibration data on file " << detectorFile << endl);
544
545 store(detectorFile, detector);
546 }
547}
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define STATUS(A)
Definition JMessage.hh:63
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
Detector data structure.
Definition JDetector.hh:96
int getString() const
Get string number.
Definition JLocation.hh:135
Router for direct addressing of module data in detector data structure.
Data structure for a composite optical module.
Definition JModule.hh:75
Utility class to parse parameter values.
Data structure for set of track fit results.
Data structure for track fit results with history and optional associated values.
const std::vector< double > & getW() const
Get associated values.
double getT() const
Get time.
bool hasW(const int i) const
Check availability of value.
Data structure for fit of straight line paralel to z-axis.
Definition JLine1Z.hh:29
General exception.
Definition JException.hh:24
int getID() const
Get identifier.
Definition JObjectID.hh:50
Utility class to parse command line options.
Definition JParser.hh:1698
Auxiliary class for a hit with background rate value.
Definition JHitW0.hh:23
General purpose class for object reading from a list of file names.
General purpose class for parallel reading of objects from a single file or multiple files.
File router for fast addressing of summary data.
Range of values.
Definition JRange.hh:42
Template L0 hit builder.
Definition JBuildL0.hh:38
const char * map
Definition elog.cc:87
static const int JSTART_LENGTH_METRES
distance between projected positions on the track of optical modules for which the response does not ...
JDirection3D getDirection(const Vec &dir)
Get direction.
JPosition3D getPosition(const Vec &pos)
Get position.
JTOOLS::JRange< double > JTimeRange
Type definition for time range (unit [s]).
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
std::set< int > getStringIDs(const JDetector &detector)
Get list of strings identifiers.
std::set< int > getModuleIDs(const JDetector &detector, const bool option=false)
Get list of modules identifiers.
JTOOLS::JRange< double > JZRange
const array_type< JValue_t > & get_values(const std::map< JKey_t, JValue_t, JComparator_t, JAllocator_t > &data)
Method to create array of values of map.
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition JMath.hh:494
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool qualitySorter(const JFit &first, const JFit &second)
Comparison of fit results.
Long64_t counter_type
Type definition for counter.
KM3NeT DAQ data structures and auxiliaries.
Definition DataQueue.cc:39
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition JDAQ.hh:26
Auxiliary data structure for sequence of same character.
Definition JManip.hh:330
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Calibration.
Definition JHead.hh:330
Detector file.
Definition JHead.hh:227
Acoustic event fit.
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Definition JFitK40.hh:103
Orientation of module.
Dynamic detector calibration.
Definition JDynamics.hh:86
Conjugate gradient fit.
Definition JGradient.hh:75
Auxiliary class to test history.
Definition JHistory.hh:188
Auxiliary class to match data points with given model.
Auxiliary data structure for editable parameter.
Definition JGradient.hh:49
Template data structure for storage of internal data.
Auxiliary class for recursive type list generation.
Definition JTypeList.hh:351
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Auxiliary class for editing time offset.
Definition JPerth.cc:91
double t0
time offset [ns]
Definition JPerth.cc:128
double TMin_ns
minimal time w.r.t. Cherenkov hypothesis [ns]
double TMax_ns
maximal time w.r.t. Cherenkov hypothesis [ns]
double VMax_npe
maximum number of of photo-electrons
Auxiliary data structure for chi2 function object.
Definition JPerth.cc:262
Auxiliary class for defining the range of iterations of objects.
Definition JLimit.hh:45
static counter_type max()
Get maximum counter value.
Definition JLimit.hh:128
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72
Auxiliary data structure for sorting of hits.
Definition JHitL0.hh:85