Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMuonPrefit.hh
Go to the documentation of this file.
1 #ifndef __JRECONSTRUCTION__JMUONPREFIT__
2 #define __JRECONSTRUCTION__JMUONPREFIT__
3 
4 #include <iostream>
5 #include <iomanip>
6 #include <vector>
7 #include <algorithm>
8 
12 
13 #include "JTrigger/JHitR1.hh"
14 #include "JTrigger/JBuildL0.hh"
15 #include "JTrigger/JBuildL2.hh"
16 #include "JTrigger/JAlgorithm.hh"
17 #include "JTrigger/JMatch1D.hh"
18 #include "JTrigger/JMatch3B.hh"
19 
20 #include "JFit/JLine1Z.hh"
21 #include "JFit/JLine1ZEstimator.hh"
22 #include "JFit/JMatrixNZ.hh"
23 #include "JFit/JVectorNZ.hh"
24 #include "JFit/JFitToolkit.hh"
25 
26 #include "JReconstruction/JEvt.hh"
29 
30 #include "JTools/JConstants.hh"
31 #include "JTools/JPermutation.hh"
32 
35 
36 #include "JGeometry3D/JOmega3D.hh"
38 
39 #include "Jeep/JMessage.hh"
40 
41 
42 /**
43  * \author mdejong, gmaggi
44  */
45 
46 namespace JRECONSTRUCTION {}
47 namespace JPP { using namespace JRECONSTRUCTION; }
48 
49 namespace JRECONSTRUCTION {
50 
52  using JFIT::JLine1Z;
53  using JFIT::JEstimator;
54  using JFIT::JMatrixNZ;
55  using JFIT::JVectorNZ;
56  using JTRIGGER::JMatch3B;
57  using JTRIGGER::JMatch1D;
59 
60 
61  /**
62  * Wrapper class to make pre-fit of muon trajectory.
63  *
64  * The JMuonPrefit fit is used to generate start values for subsequent fits (usually JMuonSimplex and JMuonGandalf).\n
65  * To this end, a scan of directions is made and the time and transverse positions of the track are fitted for each direction (JFIT::JEstimator<JLine1Z>).\n
66  * The directions are spaced by the parameters JMuonPrefitParameters_t::gridAngle_deg.\n
67  * This angle corresponds to the envisaged angular accuracy of the result.\n
68  * The probability that one of the results is less than this angle away from the correct value,
69  * multiple start values should be considered (JMuonPrefitParameters_t::numberOfPrefits).\n
70  * Note that the CPU time scales with the inverse of the square of this angle.\n
71  * The chi-squared is based on the time residuals.\n
72  */
73  struct JMuonPrefit :
75  public JEstimator<JLine1Z>
76  {
80 
81  using JEstimator_t::operator();
82 
83  /**
84  * Constructor
85  *
86  * \param parameters parameters
87  * \param router module router
88  * \param debug debug
89  */
91  const JModuleRouter& router,
92  const int debug = 0) :
93  JMuonPrefitParameters_t(parameters),
94  router(router),
95  omega (parameters.gridAngle_deg * JTOOLS::PI/180.0),
96  debug (debug)
97  {
98  configure();
99  }
100 
101 
102  /**
103  * Constructor
104  *
105  * \param router module router
106  * \param parameters parameters
107  * \param omega directions
108  * \param debug debug
109  */
111  const JModuleRouter& router,
112  const JOmega3D& omega,
113  const int debug = 0) :
114  JMuonPrefitParameters_t(parameters),
115  router(router),
116  omega (omega),
117  debug (debug)
118  {
119  configure();
120  }
121 
122 
123  /**
124  * Fit function.
125  *
126  * \param event event
127  * \return fit results
128  */
130  {
131  using namespace std;
132  using namespace JPP;
133 
134  const JBuildL0<hit_type> buildL0;
136 
137  buffer_type dataL0;
138  buffer_type dataL1;
139 
140  buildL2(event, router, !useL0, back_inserter(dataL1));
141 
142  // 3D cluster of unique optical modules
143 
145 
146  sort(dataL1.begin(), dataL1.end(), compare);
147 
148  buffer_type::iterator __end = dataL1.end();
149 
150  __end = unique(dataL1.begin(), __end, equal_to<JDAQModuleIdentifier>());
151 
152  __end = clusterizeWeight(dataL1.begin(), __end, match3B);
153 
154  dataL1.erase(__end, dataL1.end());
155 
156 
157  if (useL0) {
158 
159  buildL0(event, router, true, back_inserter(dataL0));
160 
161  __end = dataL0.end();
162 
163  for (buffer_type::iterator i = dataL0.begin(); i != __end; ) {
164 
165  if (match3B.count(*i, dataL1.begin(), dataL1.end()) != 0)
166  ++i;
167  else
168  swap(*i, *--__end);
169  }
170 
171  dataL0.erase(__end, dataL0.end());
172  }
173 
174  return (*this)(dataL0, dataL1);
175  }
176 
177 
178  /**
179  * Fit function.
180  *
181  * \param dataL0 L0 hit data
182  * \param dataL1 L1 hit data
183  * \return fit results
184  */
185  JEvt operator()(const buffer_type& dataL0,
186  const buffer_type& dataL1)
187  {
188  using namespace std;
189  using namespace JPP;
190 
191  const double STANDARD_DEVIATIONS = 3.0; // [unit]
192  const double HIT_OFF = 1.0e3 * sigma_ns * sigma_ns; // [ns^2]
193 
195 
196  data.reserve(dataL0.size() +
197  dataL1.size());
198 
199  JEvt out;
200 
201  for (JOmega3D_t::const_iterator dir = omega.begin(); dir != omega.end(); ++dir) {
202 
203  const JRotation3D R(*dir);
204 
205 
206  buffer_type::iterator __end = copy(dataL1.begin(), dataL1.end(), data.begin());
207 
208  for (buffer_type::iterator i = data.begin(); i != __end; ++i) {
209  i->rotate(R);
210  }
211 
212 
213  // reduce data
214 
215  if (distance(data.begin(), __end) > NMaxHits) {
216 
217  advance(__end = data.begin(), NMaxHits);
218 
219  partial_sort(data.begin(), __end, data.end(), cmz);
220  }
221 
222 
223  // 1D cluster
224 
225  __end = clusterizeWeight(data.begin(), __end, match1D);
226 
227  if (useL0) {
228 
229  buffer_type::iterator p = __end; // begin L0 data
230  buffer_type::iterator q = copy(dataL0.begin(), dataL0.end(), p); // end L0 data
231 
232  for (buffer_type::iterator i = p; i != q; ++i) {
233 
234  if (find_if(data.begin(), __end, bind2nd(equal_to<JDAQModuleIdentifier>(), i->getModuleID())) == __end) {
235 
236  i->rotate(R);
237 
238  if (match1D.count(*i, data.begin(), __end) != 0) {
239  *p = *i;
240  ++p;
241  }
242  }
243  }
244 
245  __end = clusterize(__end, p, match1D);
246  }
247 
248 
249  if (distance(data.begin(), __end) <= NUMBER_OF_PARAMETERS) {
250  continue;
251  }
252 
253 
254  // 1D fit
255 
256  JLine1Z tz;
257  double chi2 = numeric_limits<double>::max();
258  int NDF = distance(data.begin(), __end) - NUMBER_OF_PARAMETERS;
259  int N = getCount(data.begin(), __end);
260 
261 
262  if (distance(data.begin(), __end) <= factoryLimit) {
263 
264  int number_of_outliers = numberOfOutliers;
265 
266  if (number_of_outliers > NDF - 1) {
267  number_of_outliers = NDF - 1;
268  }
269 
270  double ymin = numeric_limits<double>::max();
271 
272  buffer_type::iterator __end1 = __end;
273 
274  for (int n = 0; n <= number_of_outliers; ++n, --__end1) {
275 
276  sort(data.begin(), __end, compare);
277 
278  do {
279  /*
280  if (getNumberOfStrings(router, data.begin(), __end1) < 2) {
281  continue;
282  }
283  */
284  try {
285 
286  (*this)(data.begin(), __end1);
287 
288  V.set(*this, data.begin(), __end1, gridAngle_deg, sigma_ns);
289  Y.set(*this, data.begin(), __end1);
290 
291  V.invert();
292 
293  double y = getChi2(Y, V);
294 
295  if (y <= -(STANDARD_DEVIATIONS * STANDARD_DEVIATIONS)) {
296 
297  WARNING(endl << "chi2(1) " << y << endl);
298 
299  } else {
300 
301  if (y < 0.0) {
302  y = 0.0;
303  }
304 
305  if (y < ymin) {
306  ymin = y;
307  tz = *this;
308  chi2 = ymin;
309  NDF = distance(data.begin(), __end1) - NUMBER_OF_PARAMETERS;
310  N = getCount(data.begin(), __end1);
311  }
312  }
313  }
314  catch(const JException& error) {}
315 
316  } while (next_permutation(data.begin(), __end1, __end, compare));
317 
318  ymin -= STANDARD_DEVIATIONS * STANDARD_DEVIATIONS;
319  }
320 
321  } else {
322 
323  const int number_of_outliers = NDF - 1;
324 
325  try {
326 
327  (*this)(data.begin(), __end);
328 
329  V.set(*this, data.begin(), __end, gridAngle_deg, sigma_ns);
330  Y.set(*this, data.begin(), __end);
331 
332  V.invert();
333 
334  for (int n = 0; n <= number_of_outliers; ++n) {
335 
336  double ymax = 0.0;
337  int k = -1;
338 
339  for (size_t i = 0; i != Y.size(); ++i) {
340 
341  double y = getChi2(Y, V, i);
342 
343  if (y > ymax) {
344  ymax = y;
345  k = i;
346  }
347  }
348 
349  if (ymax < STANDARD_DEVIATIONS * STANDARD_DEVIATIONS) {
350  break;
351  }
352 
353  V.update(k, HIT_OFF);
354 
355  this->update(data.begin(), __end, V);
356 
357  Y.set(*this, data.begin(), __end);
358 
359  NDF -= 1;
360  N -= getCount(data[k]);
361  }
362 
363  chi2 = getChi2(Y, V);
364  tz = *this;
365  }
366  catch(const JException& error) {}
367  }
368 
369  if (chi2 != numeric_limits<double>::max()) {
370 
371  tz.rotate_back(R);
372 
373  out.push_back(getFit(JHistory(JMUONPREFIT), tz, *dir, getQuality(chi2, N, NDF), NDF));
374  }
375  }
376 
377  return out;
378  }
379 
380 
381  /**
382  * Auxiliary data structure for sorting of hits.
383  */
384  static const struct {
385  /**
386  * Sort hits according times corrected for position along z-axis.
387  *
388  * \param first first hit
389  * \param second second hit
390  * \return true if first hit earlier than second hit; else false
391  */
392  template<class T>
393  inline bool operator()(const T& first, const T& second) const
394  {
395  using namespace JPP;
396 
397  return (first .getT() * getSpeedOfLight() - first .getZ() <
398  second.getT() * getSpeedOfLight() - second.getZ());
399  }
400  } cmz;
401 
402 
406  int debug;
407 
408  private:
409  /**
410  * Configure internal buffer(s).
411  */
412  void configure()
413  {
414  using namespace JPP;
415 
416  data.reserve(getNumberOfPMTs (router.getReference()) +
417  getNumberOfModules(router.getReference()));
418  }
419 
423  };
424 }
425 
426 #endif
*fatal Wrong number of arguments esac JCookie sh JRuns D $DETECTOR d sort n k
Definition: JRunrange.sh:16
Auxiliary methods to evaluate Poisson probabilities and chi2.
JMuonPrefit(const JMuonPrefitParameters_t &parameters, const JModuleRouter &router, const int debug=0)
Constructor.
Definition: JMuonPrefit.hh:90
General exception.
Definition: JException.hh:23
#define WARNING(A)
Definition: JMessage.hh:65
1D match criterion.
Definition: JMatch1D.hh:31
Wrapper class to make pre-fit of muon trajectory.
Definition: JMuonPrefit.hh:73
Linear fit of straight line parallel to z-axis to set of hits (objects with position and time)...
Match operator for Cherenkov light from muon with given direction.
void set(const JVector3D &pos, T __begin, T __end, const double alpha, const double sigma)
Set co-variance matrix.
Definition: JMatrixNZ.hh:85
JPosition3D & rotate_back(const JRotation3D &R)
Rotate back.
Definition: JPosition3D.hh:199
static struct JTRIGGER::@69 clusterize
Anonymous structure for clustering of hits.
JEvt operator()(const KM3NETDAQ::JDAQEvent &event)
Fit function.
Definition: JMuonPrefit.hh:129
Algorithms for hit clustering and sorting.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
const double getSpeedOfLight()
Number of bytes in a gigabyte.
Definition: JConstants.hh:89
Router for direct addressing of module data in detector data structure.
Rotation matrix.
Definition: JRotation3D.hh:111
static const double PI
Constants.
Definition: JConstants.hh:20
Data structure for fit parameters.
int getNumberOfPMTs(const JModule &module)
Get number of PMTs.
*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
Direction set covering (part of) solid angle.
Definition: JOmega3D.hh:64
Linear fit of JFIT::JLine1Z.
Template definition of linear fit.
Definition: JEstimator.hh:25
static const JModuleCounter getNumberOfModules
Function object to count unique modules.
void update(const size_t k, const double value)
Update inverted matrix at given diagonal element.
Definition: JMatrixNS.hh:456
double getQuality(const double chi2, const int N, const int NDF)
Get quality of fit.
Definition: JEvtToolkit.hh:206
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
static struct JTRIGGER::@71 clusterizeWeight
Anonymous struct for weighed clustering of hits.
Constants.
static const int JMUONPREFIT
Template L2 builder.
Definition: JBuildL2.hh:45
Determination of the time residual vector of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JVectorNZ.hh:28
Match operator for Cherenkov light from muon in any direction.
Determination of the co-variance matrix of hits for a track along z-axis (JFIT::JLine1Z).
Definition: JMatrixNZ.hh:28
JTRIGGER::JHitR1 hit_type
Definition: JMuonPrefit.hh:78
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JMuonPrefit(const JMuonPrefitParameters_t &parameters, const JModuleRouter &router, const JOmega3D &omega, const int debug=0)
Constructor.
Definition: JMuonPrefit.hh:110
void set(const JLine1Z &track, T __begin, T __end)
Set time residual vector.
Definition: JVectorNZ.hh:75
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
General purpose messaging.
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
JEstimator< JLine1Z > JEstimator_t
Definition: JMuonPrefit.hh:77
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:80
Direct access to module in detector data structure.
Data structure for L2 parameters.
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:126
Reduced data structure for L1 hit.
Auxiliary class for permutations of L1 hits.
Definition: JEvtToolkit.hh:554
JFIT::JHistory JHistory
Definition: JHistory.hh:283
const JClass_t & getReference() const
Get reference to object.
Definition: JReference.hh:38
static struct JRECONSTRUCTION::JMuonPrefit::@52 cmz
Auxiliary data structure for sorting of hits.
Data structure for set of track fit results.
Definition: JEvt.hh:294
alias put_queue eval echo n
Definition: qlib.csh:19
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
int getCount(const T &hit)
Get hit count.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:153
const JModuleRouter & router
Definition: JMuonPrefit.hh:403
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
void configure()
Configure internal buffer(s).
Definition: JMuonPrefit.hh:412
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
JEvt operator()(const buffer_type &dataL0, const buffer_type &dataL1)
Fit function.
Definition: JMuonPrefit.hh:185
Template L0 hit builder.
Definition: JBuildL0.hh:35
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:79
std::vector< hit_type > buffer_type
Definition: JMuonPrefit.hh:79
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
3D match criterion with road width.
Definition: JMatch3B.hh:34