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

Program to monitor hit time residuals from reconstructed tracks using JPrefit+JSimplex. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include "evt/Head.hh"
#include "evt/Evt.hh"
#include "JDAQ/JDAQEvent.hh"
#include "JDAQ/JDAQTimeslice.hh"
#include "JDAQ/JDAQSummaryslice.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JTrigger/JHit.hh"
#include "JTrigger/JFrame.hh"
#include "JTrigger/JTimeslice.hh"
#include "JTrigger/JSuperFrame2D.hh"
#include "JTrigger/JHitL0.hh"
#include "JTrigger/JHitL1.hh"
#include "JTrigger/JHitR1.hh"
#include "JTrigger/JBuildL0.hh"
#include "JTrigger/JBuildL1.hh"
#include "JTrigger/JBuildL2.hh"
#include "JTrigger/JAlgorithm.hh"
#include "JTrigger/JMatch1D.hh"
#include "JTrigger/JMatch3B.hh"
#include "JTrigger/JBind2nd.hh"
#include "JTrigger/JTriggerParameters.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JParallelFileScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JMeta.hh"
#include "JTools/JConstants.hh"
#include "JTools/JPermutation.hh"
#include "JFit/JLine1Z.hh"
#include "JFit/JLine3Z.hh"
#include "JFit/JSimplex.hh"
#include "JFit/JLine1ZEstimator.hh"
#include "JFit/JMatrixNZ.hh"
#include "JFit/JVectorNZ.hh"
#include "JFit/JLine3ZRegressor.hh"
#include "JFit/JFitToolkit.hh"
#include "JFit/JEvt.hh"
#include "JFit/JEvtToolkit.hh"
#include "JFit/JFitParameters.hh"
#include "JFit/JModel.hh"
#include "JGeometry3D/JOmega3D.hh"
#include "JGeometry3D/JRotation3D.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to monitor hit time residuals from reconstructed tracks using JPrefit+JSimplex.

The option -U has the following consequences:

Definition in file JPreSim_HTR.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 96 of file JPreSim_HTR.cc.

97 {
98  using namespace std;
99  using namespace JPP;
100  using namespace KM3NETDAQ;
101 
103  JLimit_t& numberOfEvents = inputFile.getLimit();
104  string outputFileName;
105  string detectorFile;
106  double Tmax_ns;
107  double roadWidth_m;
108  double ctMin;
109  int numberOfOutliers;
110  double gridAngle_deg;
111  size_t numberOfPrefits;
112  double sigma_ns;
113  bool useL0;
114  bool noInterDU;
115  int minHits;
116  int debug;
117 
118  try {
119 
120  JParser<> zap("Program to monitor hit time residuals from reconstructed tracks using JPrefit+JSimplex");
121 
122  zap['f'] = make_field(inputFile);
123  zap['o'] = make_field(outputFileName) = "HTR_monitor.root";
124  zap['a'] = make_field(detectorFile);
125  zap['n'] = make_field(numberOfEvents) = JLimit::max();
126  zap['T'] = make_field(Tmax_ns) = 20.0; // [ns]
127  zap['R'] = make_field(roadWidth_m) = 150.0; // [m]
128  zap['C'] = make_field(ctMin) = 0.0; //
129  zap['S'] = make_field(sigma_ns);
130  zap['O'] = make_field(numberOfOutliers) = 0;
131  zap['g'] = make_field(gridAngle_deg); // [deg]
132  zap['N'] = make_field(numberOfPrefits) = 1;
133  zap['U'] = make_field(useL0);
134  zap['N'] = make_field(numberOfPrefits) = numeric_limits<size_t>::max();
135  zap['L'] = make_field(minHits) = 4;
136  zap['I'] = make_field(noInterDU);
137  zap['d'] = make_field(debug) = 1;
138 
139  zap(argc, argv);
140  }
141  catch(const exception& error) {
142  FATAL(error.what() << endl);
143  }
144 
145 
146  cout.tie(&cerr);
147 
148 
149  const int FACTORY_LIMIT = 8; // [unit]
150  const double STANDARD_DEVIATIONS = 3.0; // [unit]
151  const double HIT_OFF = 1.0e3 * sigma_ns * sigma_ns; // [ns^2]
152  //const int NUMBER_OF_L1HITS = (useL0 ? 1 : 4);
153  const int MAXIMUM_NUMBER_OF_HITS = 50; //numeric_limits<int>::max();
154  const double TMAX_NS = 50.0; // [ns]
155 
157 
158  try {
159  load(detectorFile, detector);
160  }
161  catch(const JException& error) {
162  FATAL(error);
163  }
164 
165  const JModuleRouter router(detector);
166 
167 
168  typedef vector<JHitL0> JDataL0_t;
169  typedef vector<JHitL1> JDataL1_t;
170  typedef vector<JHitR1> JDataR1_t;
171 
172  JSuperFrame2D <JHit> buffer;
173  const JBuildL0<JHitL0> buildL0;
174  const JBuildL2<JHitL1> buildL2(2, Tmax_ns, ctMin);
175  const JMatch1D<JHitR1> match1D(roadWidth_m, Tmax_ns);
176  const JMatch3B<JHitL1> match3B(roadWidth_m, Tmax_ns);
177  const JHitL1Comparator compare;
178 
179  JOmega3D_t omega ;
180  if (noInterDU) {
181 
182  const double thetaMin = 0.75*PI ;
183  const double thetaMax = PI ;
184  const double rad = thetaMax - thetaMin;
185  const double bin = rad / floor(rad/(gridAngle_deg*PI/180.0) + 0.5); // polar angle step size
186 
187  for (double theta = thetaMin; theta < thetaMax + 0.5*bin; theta += bin) {
188  omega.push_back(JAngle3D(theta,0.0));
189  }
190 
191  } else {
192 
193  omega = JOmega3D(gridAngle_deg * PI/180.0) ;
194 
195  }
196 
197  NOTICE( "Scanning over " << omega.size() << "JPrefit directions" <<endl ) ;
198 
199  typedef JRegressor<JLine3Z, JSimplex> JRegressor_t;
200 
201  JRegressor_t fit(sigma_ns);
202 
203  fit.estimator.reset(new JMEstimatorLorentzian());
204 
206  JRegressor_t::MAXIMUM_ITERATIONS = 10000;
207 
208  TFile outputFile(outputFileName.c_str(),"recreate");
209 
210  // exclude one DOM from the fit
211  for (JModuleRouter::const_iterator excl_mod = detector.begin(); excl_mod != detector.end(); ++excl_mod) {
212 
213  // output tree (one per excluded DOM)
214  double htr_dt ;
215  JFit bestfit ;
216  TTree htr_tree(Form("%d.htr",excl_mod->getID()),Form("%d.htr",excl_mod->getID())) ;
217  htr_tree.Branch("dt",&htr_dt) ;
218  htr_tree.Branch("bestfit",&bestfit) ;
219 
220  STATUS( "Excluding DOM "<<excl_mod->getID()<<endl ) ;
221 
222  while (inputFile.hasNext()) {
223 
224  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
225 
226  JDAQEvent* tev = inputFile.next();
227  JEvt out;
228  JEvt out2 ;
229 
230 
231  // L1 and L0 data
232 
233  JDataL0_t dataL0;
234  JDataL1_t dataL1;
235  JDataL1_t dataL1_excluded;
236  JDataR1_t data;
237 
238  JDAQTimeslice timeslice(*tev, true);
239 
240  for (JDAQTimeslice::const_iterator i = timeslice.begin(); i != timeslice.end(); ++i) {
241 
242  JModule module = router.getModule(i->getModuleID()) ;
243  buffer(*i, module);
244 
245  if (module.getID() != excl_mod->getID()) {
246 
247  if (!noInterDU || module.getString() == excl_mod->getString()) {
248 
249  buildL0(buffer, back_inserter(dataL0));
250  buildL2(buffer, back_inserter(dataL1));
251 
252  }
253 
254  } else {
255 
256  buildL2(buffer, back_inserter(dataL1_excluded));
257 
258  }
259  }
260 
261 
262  // 3D cluster of unique optical modules
263 
264  JDataL1_t::iterator __end = unique(dataL1.begin(), dataL1.end(), equal_to<JDAQModuleIdentifier>());
265 
266  __end = clusterizeWeight(dataL1.begin(), __end, match3B);
267 
268 
269  if (distance(dataL1.begin(), __end) >= minHits && dataL1_excluded.size() > 0) {
270 
271  for (JOmega3D_t::const_iterator dir = omega.begin(); dir != omega.end(); ++dir) {
272 
273  const JRotation3D R(*dir);
274 
275  data.clear();
276 
277  copy(dataL1.begin(), __end, back_inserter(data));
278 
279  for (JDataR1_t::iterator i = data.begin(); i != data.end(); ++i) {
280  i->rotate(R);
281  }
282 
283 
284  JDataR1_t::iterator __end1 = data.end();
285 
286 
287  // reduce data
288 
289  if (distance(data.begin(), __end1) > MAXIMUM_NUMBER_OF_HITS) {
290 
291  advance(__end1 = data.begin(), MAXIMUM_NUMBER_OF_HITS);
292 
293  partial_sort(data.begin(), __end1, data.end(), cmz);
294  }
295 
296 
297  // 1D cluster
298 
299  __end1 = clusterizeWeight(data.begin(), __end1, match1D);
300 
301  if (distance(data.begin(), __end1) < minHits) {
302  continue;
303  }
304 
305 
306  if (useL0) {
307 
308  data.erase(__end1, data.end());
309 
310  data.reserve(data.size() + dataL0.size());
311 
312  __end1 = data.end();
313 
314  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
315 
316  if (find_if(data.begin(), __end1, bind2nd(equal_to<JDAQModuleIdentifier>(), i->getModuleID())) == __end1) {
317 
318  JHitR1 hit(*i);
319 
320  hit.rotate(R);
321 
322  if (count_if(data.begin(), __end1, JBind2nd(match1D,hit)) >= minHits) {
323  data.push_back(hit);
324  }
325  }
326  }
327 
328  __end1 = clusterize(__end1, data.end(), match1D);
329  }
330 
331 
332  if (distance(data.begin(), __end1) <= JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS) {
333  continue;
334  }
335 
336  // 1D fit
337 
338  JLine1Z tz;
339  double chi2 = numeric_limits<double>::max();
340  int NDF = 0;
341  int N = 0;
342 
343  JMatrixNZ V;
344  JVectorNZ Y;
345 
346  if (distance(data.begin(), __end1) <= FACTORY_LIMIT) {
347 
348  double ymin = numeric_limits<double>::max();
349 
350  JDataR1_t::iterator __end2 = __end1;
351 
352  for (int n = 0; n <= numberOfOutliers && distance(data.begin(), __end2) > JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS; ++n, --__end2) {
353 
354  sort(data.begin(), __end1, compare);
355 
356  do {
357 
358  try {
359 
360  JEstimator<JLine1Z> fit(data.begin(), __end2);
361 
362  V.set(fit, data.begin(), __end2, gridAngle_deg, sigma_ns);
363  Y.set(fit, data.begin(), __end2);
364 
365  V.invert();
366 
367  double y = getChi2(Y, V);
368 
369  if (y <= 0.0) {
370  WARNING(endl << "chi2(1) " << y << endl);
371  } else if (y < ymin) {
372  ymin = y;
373  tz = fit;
374  NDF = distance(data.begin(), __end2) - JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS;
375  N = getCount(data.begin(), __end2);
376  chi2 = y;
377  }
378  }
379  catch(const JException& error) {}
380 
381  } while (next_permutation(data.begin(), __end2, __end1, compare));
382 
383  ymin -= STANDARD_DEVIATIONS * STANDARD_DEVIATIONS;
384  }
385 
386  } else {
387 
388  try {
389 
390  const int number_of_outliers = distance(data.begin(), __end1) - JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS - 1;
391 
392  JEstimator<JLine1Z> fit(data.begin(), __end1);
393 
394  V.set(fit, data.begin(), __end1, gridAngle_deg, sigma_ns);
395  Y.set(fit, data.begin(), __end1);
396 
397  V.invert();
398 
399  NDF = distance(data.begin(), __end1) - JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS;
400  N = getCount(data.begin(), __end1);
401 
402  for (int n = 0; n <= number_of_outliers && NDF > 0; ++n, --NDF) {
403 
404  double ymax = 0.0;
405  int kill = -1;
406 
407  for (unsigned int i = 0; i != Y.size(); ++i) {
408 
409  double y = getChi2(Y, V, i);
410 
411  if (y <= 0.0) {
412  WARNING(endl << "chi2(2) " << y << endl);
413  } else if (y > ymax) {
414  ymax = y;
415  kill = i;
416  }
417  }
418 
419  if (ymax < STANDARD_DEVIATIONS * STANDARD_DEVIATIONS) {
420  break;
421  }
422 
423  V.update(kill, HIT_OFF);
424 
425  fit.update(data.begin(), __end1, V);
426 
427  Y.set(fit, data.begin(), __end1);
428 
429  N -= getCount(data[kill]);
430  }
431 
432  chi2 = getChi2(Y, V);
433  tz = fit;
434  }
435  catch(const JException& error) {}
436  }
437 
438  if (NDF != 0) {
439 
440  tz.rotate_back(R);
441 
442  out.push_back(getFit(JPRESIM, tz, *dir, getQuality(chi2, (useL0 ? N : NDF)), NDF));
443  }
444  }
445 
446  // apply default sorter
447 
448  if (numberOfPrefits > 0) {
449 
450  JEvt::iterator __end = out.end();
451 
452  advance(__end = out.begin(), min(numberOfPrefits, out.size()));
453 
454  partial_sort(out.begin(), __end, out.end(), qualitySorter);
455 
456  out.erase(__end, out.end());
457 
458  } else {
459 
460  sort(out.begin(), out.end(), qualitySorter);
461  }
462 
463 // JSimplex
464 
465  if (!out.empty()) {
466 
467  JEvt::iterator __end = out.end();
468 
469  if (numberOfPrefits > 0) {
470  advance(__end = out.begin(), min(numberOfPrefits, out.size()));
471  }
472 
473  for (JEvt::const_iterator track = out.begin(); track != __end; ++track) {
474 
475  const JRotation3D R (getDirection(*track));
476  const JLine1Z tz(getPosition (*track).rotate(R), track->getT());
477  const JModel<JLine1Z> match(tz, roadWidth_m, JTimeRange(-TMAX_NS, +TMAX_NS));
478 
479  data.clear();
480 
481  for (JDataL1_t::const_iterator i = dataL1.begin(); i != dataL1.end(); ++i) {
482 
483  JHitR1 hit(*i);
484 
485  hit.rotate(R);
486 
487  if (match(hit)) {
488  data.push_back(hit);
489  }
490  }
491 
492  if (useL0) {
493 
494  data.reserve(data.size() + dataL0.size());
495 
496  JDataR1_t::iterator __end1 = data.end();
497 
498  for (JDataL0_t::iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
499 
500  if (find_if(data.begin(), __end1, bind2nd(equal_to<JDAQModuleIdentifier>(), i->getModuleID())) == __end1) {
501 
502  JHitR1 hit(*i);
503 
504  hit.rotate(R);
505 
506  if (match(hit)) {
507  data.push_back(hit);
508  }
509  }
510  }
511  }
512 
513 
514  fit.step.resize(5);
515 
516  fit.step[0] = JLine3Z(JLine1Z(JVector3D(0.5, 0.0, 0.0), 0.0));
517  fit.step[1] = JLine3Z(JLine1Z(JVector3D(0.0, 0.5, 0.0), 0.0));
518  fit.step[2] = JLine3Z(JLine1Z(JVector3D(0.0, 0.0, 0.0), 1.0));
519  fit.step[3] = JLine3Z(JLine1Z(JVector3D(), 0.0), JVersor3Z(0.005, 0.0));
520  fit.step[4] = JLine3Z(JLine1Z(JVector3D(), 0.0), JVersor3Z(0.0, 0.005));
521 
522  const int NDF = getCount(data.begin(), data.end()) - fit.step.size();
523 
524  if (NDF >= 0) {
525 
526  const double chi2 = fit(JLine3Z(tz), data.begin(), data.end());
527 
528  JTrack3D tb(fit.value);
529 
530  tb.rotate_back(R);
531 
532  out2.push_back(getFit(JPRESIM, tb, getQuality(chi2, NDF), NDF));
533  }
534  }
535 
536  // apply default sorter
537 
538  sort(out2.begin(), out2.end(), qualitySorter);
539 
540  } // !out.empty()
541 
542  } // distance(dataL1.begin(), __end) >= minHits
543 
544  if (!out2.empty()) {
545 
546  bestfit = *out2.begin() ;
547 
548  const double t_exp = getTrack(bestfit).getT(excl_mod->getPosition()) ;
549 
550  sort(dataL1_excluded.begin(), dataL1_excluded.end()) ;
551 
552  htr_dt = dataL1_excluded.begin()->getT()-t_exp ;
553 
554  htr_tree.Fill() ;
555 
556  }
557 
558  } // inputFile.hasNext()
559 
560  inputFile.rewind() ;
561 
562  outputFile.cd() ;
563  htr_tree.Write() ;
564 
565  } // excluded DOM
566 
567  STATUS(endl);
568 
569  outputFile.Write() ;
570 
571 }
Data structure for angles in three dimensions.
Definition: JAngle3D.hh:30
Utility class to parse command line options.
Definition: JParser.hh:1410
General exception.
Definition: JException.hh:40
#define WARNING(A)
Definition: JMessage.hh:63
1D match criterion.
Definition: JMatch1D.hh:31
Linear fit of straight line parallel to z-axis to set of hits (objects with position and time)...
Data structure for a composite optical module.
Definition: JModule.hh:47
JBinder2nd< JHit_t > JBind2nd(const JMatch< JHit_t > &match, const JHit_t &second)
Auxiliary method to create JBinder2nd object.
Definition: JBind2nd.hh:70
void set(const JVector3D &pos, T __begin, T __end, const double alpha, const double sigma)
Set co-variance matrix.
Definition: JMatrixNZ.hh:81
JPosition3D & rotate_back(const JRotation3D &R)
Rotate back.
Definition: JPosition3D.hh:199
JTrack3E getTrack(const Trk &track)
Get track.
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:102
double getCount(TH1D *hptr, int muon_threshold)
#define STATUS(A)
Definition: JMessage.hh:61
Detector data structure.
Definition: JDetector.hh:77
Router for direct addressing of module data in detector data structure.
Rotation matrix.
Definition: JRotation3D.hh:108
static const double PI
Constants.
Definition: JConstants.hh:20
Template specialisation of class JModel to match hit with muon trajectory along z-axis.
Definition: JModel.hh:31
Data structure for fit of straight line in positive z-direction.
Definition: JLine3Z.hh:35
Direction set covering (part of) solid angle.
Definition: JOmega3D.hh:62
Template definition of linear fit.
Definition: JEstimator.hh:25
string outputFile
void update(const unsigned int k, const double extra)
Update inverted matrix at given diagonal element.
Definition: JMatrixNS.hh:208
JHitIterator_t clusterizeWeight(JHitIterator_t __begin, JHitIterator_t __end, const JMatch< JHit_t > &match)
Partition data according given binary match operator.
Definition: JAlgorithm.hh:310
JRange< double > JTimeRange
Type definition for time range.
JFit getFit(const JHistory &history, const JTrack3D &track, const double Q, const int NDF, const double energy=0.0, const int status=0)
Get fit.
Definition: JEvtToolkit.hh:116
Data structure for track fit results.
Definition: JEvt.hh:32
Auxiliary class for permutations of L1 hits.
Definition: JEvtToolkit.hh:501
JDetector::const_iterator const_iterator
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
int getString() const
Get string number.
JHitIterator_t clusterize(JHitIterator_t __begin, JHitIterator_t __end, const JMatch< JHit_t > &match, const int Nmin=1)
Partition data according given binary match operator.
Definition: JAlgorithm.hh:45
Template L2 builder.
Definition: JBuildL2.hh:47
Determination of the time residual vector of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JVectorNZ.hh:25
Detector file.
Definition: JHead.hh:126
Data structure for vector in three dimensions.
Definition: JVector3D.hh:32
Determination of the co-variance matrix of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JMatrixNZ.hh:28
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
int getID() const
Get identifier.
Definition: JObjectID.hh:54
Regressor function object for JLine3Z fit using JSimplex minimiser.
#define NOTICE(A)
Definition: JMessage.hh:62
Data time slice.
void set(const JLine1Z &track, T __begin, T __end)
Set time residual vector.
Definition: JVectorNZ.hh:74
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:59
double getT(const JVector3D &pos) const
Get arrival time of Cherenkov light at given position.
Definition: JTrack3D.hh:126
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Definition: JCounter.hh:35
#define FATAL(A)
Definition: JMessage.hh:65
double getQuality(const double chi2, const int NDF)
Get quality of fit.
Definition: JEvtToolkit.hh:195
Data structure for set of track fit results.
Definition: JEvt.hh:312
General purpose class for object reading from a list of file names.
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
bool qualitySorter(const JFIT::JFit &first, const JFIT::JFit &second)
Comparison of fit results.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:40
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
2-dimensional frame with time calibrated data from one optical module.
JDirection3D getDirection(const Vec &v)
Get direction.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:72
bool next_permutation(T __begin, T __last, T __end, JComparator_t compare, std::bidirectional_iterator_tag)
Implementation of method next_permutation for bidirectional iterators.
Definition: JPermutation.hh:20
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:80
Data structure for normalised vector in positive z-direction.
Definition: JVersor3Z.hh:36
Base class for direction set.
Definition: JOmega3D.hh:30
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
JPreSim_HTR.cc.
void invert()
Invert matrix.
Definition: JMatrixNS.hh:61
Lorentzian M-estimator.
Definition: JMEstimator.hh:79
JTriggerCounter_t next()
Increment trigger counter.
3D match criterion with road width.
Definition: JMatch3B.hh:34
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
JPosition3D getPosition(const Vec &v)
Get position.