Jpp
Functions
JPrefit.cc File Reference
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include "km3net-dataformat/offline/Head.hh"
#include "km3net-dataformat/offline/Evt.hh"
#include "JDAQ/JDAQEventIO.hh"
#include "JDAQ/JDAQTimesliceIO.hh"
#include "JDAQ/JDAQSummarysliceIO.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/JSingleFileScanner.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JMeta.hh"
#include "JFit/JLine1Z.hh"
#include "JFit/JLine1ZEstimator.hh"
#include "JFit/JMatrixNZ.hh"
#include "JFit/JVectorNZ.hh"
#include "JFit/JFitToolkit.hh"
#include "JFit/JEvt.hh"
#include "JFit/JEvtToolkit.hh"
#include "JFit/JFitParameters.hh"
#include "JTools/JConstants.hh"
#include "JTools/JPermutation.hh"
#include "JGeometry3D/JOmega3D.hh"
#include "JGeometry3D/JRotation3D.hh"
#include "JLang/JPredicate.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 perform linear fits JFIT::JEstimator<JLine1Z> covering the solid angle producing JFIT::JEvt data.

The option -U has the following consequences:

Definition in file JPrefit.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 91 of file JPrefit.cc.

92 {
93  using namespace std;
94  using namespace JPP;
95  using namespace KM3NETDAQ;
96 
98 
101  JLimit_t& numberOfEvents = inputFile.getLimit();
102  string detectorFile;
103  double Tmax_ns;
104  double roadWidth_m;
105  double ctMin;
106  double sigma_ns;
107  int numberOfOutliers;
108  double gridAngle_deg;
109  bool useL0;
110  size_t numberOfPrefits;
111  int debug;
112 
113  try {
114 
115  JParser<> zap("Program to perform pre-fit of muon trajectory to data.");
116 
117  zap['f'] = make_field(inputFile);
118  zap['o'] = make_field(outputFile) = "prefit.root";
119  zap['a'] = make_field(detectorFile);
120  zap['n'] = make_field(numberOfEvents) = JLimit::max();
121  zap['T'] = make_field(Tmax_ns) = 20.0; // [ns]
122  zap['R'] = make_field(roadWidth_m) = 150.0; // [m]
123  zap['C'] = make_field(ctMin) = 0.0; //
124  zap['S'] = make_field(sigma_ns);
125  zap['O'] = make_field(numberOfOutliers) = 0;
126  zap['g'] = make_field(gridAngle_deg); // [deg]
127  zap['U'] = make_field(useL0);
128  zap['N'] = make_field(numberOfPrefits) = numeric_limits<size_t>::max();
129  zap['d'] = make_field(debug) = 1;
130 
131  zap(argc, argv);
132  }
133  catch(const exception& error) {
134  FATAL(error.what() << endl);
135  }
136 
137 
138 
139  cout.tie(&cerr);
140 
141  const int FACTORY_LIMIT = 8; // [unit]
142  const double STANDARD_DEVIATIONS = 3.0; // [unit]
143  const double HIT_OFF = 1.0e3 * sigma_ns * sigma_ns; // [ns^2]
144  const int NUMBER_OF_L1HITS = (useL0 ? 1 : 4);
145  const int MAXIMUM_NUMBER_OF_HITS = 50; //numeric_limits<int>::max();
146 
147 
149 
150  try {
151  load(detectorFile, detector);
152  }
153  catch(const JException& error) {
154  FATAL(error);
155  }
156 
157  const JModuleRouter router(detector);
158 
159 
160  typedef vector<JHitL0> JDataL0_t;
161  typedef vector<JHitL1> JDataL1_t;
162  typedef vector<JHitR1> JDataR1_t;
163 
164  JSuperFrame2D <JHit> buffer;
165  const JBuildL0<JHitL0> buildL0;
166  const JBuildL2<JHitL1> buildL2(JL2Parameters(2, Tmax_ns, ctMin));
167  const JMatch1D<JHitR1> match1D(roadWidth_m, Tmax_ns);
168  const JMatch3B<JHitL1> match3B(roadWidth_m, Tmax_ns);
169  const JHitL1Comparator compare;
170  const JOmega3D omega(gridAngle_deg * PI/180.0);
171 
172 
173  outputFile.open();
174 
175  outputFile.put(JMeta(argc, argv));
176 
177  while (inputFile.hasNext()) {
178 
179  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
180 
181  JDAQEvent* tev = inputFile.next();
182  JEvt out;
183 
184 
185  // L1 and L0 data
186 
187  JDataL0_t dataL0;
188  JDataL1_t dataL1;
189 
190  buildL0(JDAQTimeslice(*tev, true), router, back_inserter(dataL0));
191  buildL2(JDAQTimeslice(*tev, !useL0), router, back_inserter(dataL1));
192 
193 
194  // 3D cluster of unique optical modules
195 
196  JDataL1_t::iterator __end = unique(dataL1.begin(), dataL1.end(), equal_to<JDAQModuleIdentifier>());
197 
198  __end = clusterizeWeight(dataL1.begin(), __end, match3B);
199 
200 
201  if (distance(dataL1.begin(), __end) >= NUMBER_OF_L1HITS) {
202 
203 
204  JDataR1_t data;
205 
206 
207  for (JOmega3D_t::const_iterator dir = omega.begin(); dir != omega.end(); ++dir) {
208 
209  const JRotation3D R(*dir);
210 
211  data.clear();
212 
213  copy(dataL1.begin(), __end, back_inserter(data));
214 
215  for (JDataR1_t::iterator i = data.begin(); i != data.end(); ++i) {
216  i->rotate(R);
217  }
218 
219 
220  JDataR1_t::iterator __end1 = data.end();
221 
222 
223  // reduce data
224 
225  if (distance(data.begin(), __end1) > MAXIMUM_NUMBER_OF_HITS) {
226 
227  advance(__end1 = data.begin(), MAXIMUM_NUMBER_OF_HITS);
228 
229  partial_sort(data.begin(), __end1, data.end(), cmz);
230  }
231 
232 
233  // 1D cluster
234 
235  __end1 = clusterizeWeight(data.begin(), __end1, match1D);
236 
237  if (distance(data.begin(), __end1) < NUMBER_OF_L1HITS) {
238  continue;
239  }
240 
241 
242  if (useL0) {
243 
244  data.erase(__end1, data.end());
245 
246  data.reserve(data.size() + dataL0.size());
247 
248  __end1 = data.end();
249 
250  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
251 
252  if (find_if(data.begin(), __end1, bind2nd(equal_to<JDAQModuleIdentifier>(), i->getModuleID())) == __end1) {
253 
254  JHitR1 hit(*i);
255 
256  hit.rotate(R);
257 
258  if (count_if(data.begin(), __end1, JBind2nd(match1D,hit)) >= NUMBER_OF_L1HITS) {
259  data.push_back(hit);
260  }
261  }
262  }
263 
264  __end1 = clusterize(__end1, data.end(), match1D);
265  }
266 
267 
268  if (distance(data.begin(), __end1) <= JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS) {
269  continue;
270  }
271 
272  // 1D fit
273 
274  JLine1Z tz;
275  double chi2 = numeric_limits<double>::max();
276  int NDF = 0;
277  int N = 0;
278 
279  JMatrixNZ V;
280  JVectorNZ Y;
281 
282  if (distance(data.begin(), __end1) <= FACTORY_LIMIT) {
283 
284  double ymin = numeric_limits<double>::max();
285 
286  JDataR1_t::iterator __end2 = __end1;
287 
288  for (int n = 0; n <= numberOfOutliers && distance(data.begin(), __end2) > JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS; ++n, --__end2) {
289 
290  sort(data.begin(), __end1, compare);
291 
292  do {
293 
294  try {
295 
296  JEstimator<JLine1Z> fit(data.begin(), __end2);
297 
298  V.set(fit, data.begin(), __end2, gridAngle_deg, sigma_ns);
299  Y.set(fit, data.begin(), __end2);
300 
301  V.invert();
302 
303  double y = getChi2(Y, V);
304 
305  if (y <= 0.0) {
306  WARNING(endl << "chi2(1) " << y << endl);
307  } else if (y < ymin) {
308  ymin = y;
309  tz = fit;
310  NDF = distance(data.begin(), __end2) - JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS;
311  N = getCount(data.begin(), __end2);
312  chi2 = y;
313  }
314  }
315  catch(const JException& error) {}
316 
317  } while (next_permutation(data.begin(), __end2, __end1, compare));
318 
319  ymin -= STANDARD_DEVIATIONS * STANDARD_DEVIATIONS;
320  }
321 
322  } else {
323 
324  try {
325 
326  const int number_of_outliers = distance(data.begin(), __end1) - JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS - 1;
327 
328  JEstimator<JLine1Z> fit(data.begin(), __end1);
329 
330  V.set(fit, data.begin(), __end1, gridAngle_deg, sigma_ns);
331  Y.set(fit, data.begin(), __end1);
332 
333  V.invert();
334 
335  NDF = distance(data.begin(), __end1) - JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS;
336  N = getCount(data.begin(), __end1);
337 
338  for (int n = 0; n <= number_of_outliers && NDF > 0; ++n, --NDF) {
339 
340  double ymax = 0.0;
341  int kill = -1;
342 
343  for (unsigned int i = 0; i != Y.size(); ++i) {
344 
345  double y = getChi2(Y, V, i);
346 
347  if (y <= 0.0) {
348  WARNING(endl << "chi2(2) " << y << endl);
349  } else if (y > ymax) {
350  ymax = y;
351  kill = i;
352  }
353  }
354 
355  if (ymax < STANDARD_DEVIATIONS * STANDARD_DEVIATIONS) {
356  break;
357  }
358 
359  V.update(kill, HIT_OFF);
360 
361  fit.update(data.begin(), __end1, V);
362 
363  Y.set(fit, data.begin(), __end1);
364 
365  N -= getCount(data[kill]);
366  }
367 
368  chi2 = getChi2(Y, V);
369  tz = fit;
370  }
371  catch(const JException& error) {}
372  }
373 
374  if (NDF != 0) {
375 
376  tz.rotate_back(R);
377 
378  out.push_back(getFit(JHistory(JMUONPREFIT), tz, *dir, getQuality(chi2, (useL0 ? N : NDF)), NDF));
379  }
380  }
381 
382  // apply default sorter
383 
384  if (numberOfPrefits > 0) {
385 
386  JEvt::iterator __end = out.end();
387 
388  advance(__end = out.begin(), min(numberOfPrefits, out.size()));
389 
390  partial_sort(out.begin(), __end, out.end(), qualitySorter);
391 
392  // add downward pointing solutions if available but absent so far
393 
394  if (count_if(out.begin(), __end, make_predicate(&JFit::getDZ, 0.0, JComparison::le())) == 0) {
395 
396  JEvt::iterator __begin = __end;
397  JEvt::iterator __q = partition(__begin, out.end(), make_predicate(&JFit::getDZ, 0.0, JComparison::le()));
398 
399  advance(__end = __begin, min(numberOfPrefits, (size_t) distance(__begin, __q)));
400 
401  partial_sort(__begin, __end, __q, qualitySorter);
402  }
403 
404  out.erase(__end, out.end());
405 
406  } else {
407 
408  sort(out.begin(), out.end(), qualitySorter);
409  }
410  }
411 
412  outputFile.put(out);
413  }
414  STATUS(endl);
415 
417 
418  io >> outputFile;
419 
420  outputFile.close();
421 }
JTRIGGER::JBind2nd
JBinder2nd< JHit_t > JBind2nd(const JMatch< JHit_t > &match, const JHit_t &second)
Auxiliary method to create JBinder2nd object.
Definition: JBind2nd.hh:70
KM3NETDAQ::JDAQEvent
DAQ Event.
Definition: JDAQEvent.hh:30
JMUONPREFIT
static const int JMUONPREFIT
Definition: reconstruction.hh:14
JLANG::JComparison::le
Less equals.
Definition: JComparison.hh:89
JSUPPORT::JSingleFileScanner::getCounter
counter_type getCounter() const
Get counter.
Definition: JSingleFileScanner.hh:208
JFIT::JHitL1Comparator
Auxiliary class for permutations of L1 hits.
Definition: JEvtToolkit.hh:501
JSUPPORT::JLimit
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
JTOOLS::next_permutation
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
JTRIGGER::JL2Parameters
Data structure for L2 parameters.
Definition: JTriggerParameters.hh:33
JROOT::advance
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
qualitySorter
bool qualitySorter(const JFIT::JFit &first, const JFIT::JFit &second)
Comparison of fit results.
Definition: quality_sorter.cc:10
JDETECTOR::load
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
Definition: JDetectorToolkit.hh:476
JSUPPORT::JSingleFileScanner::next
virtual const pointer_type & next()
Get next element.
Definition: JSingleFileScanner.hh:280
JTOOLS::n
const int n
Definition: JPolint.hh:628
std::vector< JHitL0 >
JTRIGGER::clusterize
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
JLANG::JTYPELIST
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
JFIT::JMatrixNZ::set
void set(const JVector3D &pos, T __begin, T __end, const double alpha, const double sigma)
Set co-variance matrix.
Definition: JMatrixNZ.hh:85
KM3NETDAQ::JDAQTimeslice
Data time slice.
Definition: JDAQTimeslice.hh:30
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
JFIT::JEvt
Data structure for set of track fit results.
Definition: JEvt.hh:293
JSUPPORT::JSingleFileScanner::hasNext
virtual bool hasNext()
Check availability of next element.
Definition: JSingleFileScanner.hh:234
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
JMATH::JMatrixNS::update
void update(const size_t k, const double value)
Update inverted matrix at given diagonal element.
Definition: JMatrixNS.hh:456
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTRIGGER::JMatch1D
1D match criterion.
Definition: JMatch1D.hh:31
WARNING
#define WARNING(A)
Definition: JMessage.hh:65
debug
int debug
debug level
Definition: JSirene.cc:59
JTRIGGER::clusterizeWeight
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
JGEOMETRY3D::JPosition3D::rotate_back
JPosition3D & rotate_back(const JRotation3D &R)
Rotate back.
Definition: JPosition3D.hh:199
JTRIGGER::JMatch3B
3D match criterion with road width.
Definition: JMatch3B.hh:34
JFIT::JEstimator
Template definition of linear fit.
Definition: JEstimator.hh:25
STATUS
#define STATUS(A)
Definition: JMessage.hh:63
JFIT::JLine1Z
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
JDETECTOR::JModuleRouter
Router for direct addressing of module data in detector data structure.
Definition: JModuleRouter.hh:34
JTRIGGER::JHitR1
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JDETECTOR::JDetector
Detector data structure.
Definition: JDetector.hh:80
JFIT::getChi2
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:80
JMATH::JMatrixNS::invert
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:80
JAANET::detector
Detector file.
Definition: JHead.hh:130
JSUPPORT::JMeta
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:71
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
JFIT::JVectorNZ
Determination of the time residual vector of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JVectorNZ.hh:28
std
Definition: jaanetDictionary.h:36
KM3NETDAQ
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
JTOOLS::PI
static const double PI
Constants.
Definition: JConstants.hh:20
JFIT::JMatrixNZ
Determination of the co-variance matrix of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JMatrixNZ.hh:28
JLANG::make_predicate
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
JFIT::getFit
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
JFIT::JVectorNZ::set
void set(const JLine1Z &track, T __begin, T __end)
Set time residual vector.
Definition: JVectorNZ.hh:75
JTRIGGER::JBuildL0< JHitL0 >
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:101
JFIT::JHistory
Container for historical events.
Definition: JHistory.hh:95
JGEOMETRY3D::JOmega3D
Direction set covering (part of) solid angle.
Definition: JOmega3D.hh:64
JTRIGGER::JSuperFrame2D
2-dimensional frame with time calibrated data from one optical module.
Definition: JSuperFrame2D.hh:41
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37
JSUPPORT::JFileRecorder
Object writing to file.
Definition: JFileRecorder.hh:41
JFIT::getQuality
double getQuality(const double chi2, const int NDF)
Get quality of fit.
Definition: JEvtToolkit.hh:195
JLANG::JException
General exception.
Definition: JException.hh:23
JGEOMETRY3D::JRotation3D
Rotation matrix.
Definition: JRotation3D.hh:111
JSUPPORT::JSingleFileScanner
Object reading from a list of files.
Definition: JSingleFileScanner.hh:75
JFIT::JEstimator< JLine1Z >
Linear fit of straight line parallel to z-axis to set of hits (objects with position and time).
Definition: JLine1ZEstimator.hh:88
JFIT::getCount
int getCount(const JHitL0 &hit)
Get hit count.
Definition: JEvtToolkit.hh:728
JTRIGGER::JBuildL2
Template L2 builder.
Definition: JBuildL2.hh:45