Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMuonPrefit.hh
Go to the documentation of this file.
1 #ifndef JMUONPREFIT_INCLUDE
2 #define JMUONPREFIT_INCLUDE
3 
4 #include <string>
5 #include <iostream>
6 #include <iomanip>
7 #include <vector>
8 #include <algorithm>
9 
10 #include "JDAQ/JDAQTimeslice.hh"
11 #include "JDAQ/JDAQSummaryslice.hh"
12 
13 #include "JTrigger/JHitL0.hh"
14 #include "JTrigger/JHitL1.hh"
15 #include "JTrigger/JHitR1.hh"
16 #include "JTrigger/JBuildL0.hh"
17 #include "JTrigger/JBuildL2.hh"
18 #include "JTrigger/JAlgorithm.hh"
19 #include "JTrigger/JMatch1D.hh"
20 #include "JTrigger/JMatch3B.hh"
21 #include "JTrigger/JBind2nd.hh"
22 
23 #include "JFit/JLine1Z.hh"
24 #include "JFit/JLine1ZEstimator.hh"
25 #include "JFit/JMatrixNZ.hh"
26 #include "JFit/JVectorNZ.hh"
27 #include "JFit/JFitToolkit.hh"
28 #include "JFit/JEvt.hh"
29 #include "JFit/JEvtToolkit.hh"
30 #include "JFit/JFitParameters.hh"
31 #include "JFit/JFitStatus.hh"
33 
34 #include "JTools/JConstants.hh"
35 #include "JTools/JPermutation.hh"
36 
37 #include "JGeometry3D/JOmega3D.hh"
39 
40 #include "JLang/JPredicate.hh"
41 #include "JLang/JSharedPointer.hh"
42 
43 /**
44  * \author mdejong, gmaggi
45  */
46 
47 namespace JFIT
48 {
49  /**
50  * class to handle Prefit angular reconstruction.
51  * this should be combined with JMuonSimplex and JMuonGandalf to achieve an optimal angular resolution.
52  */
54  {
55  private:
59 
60 
61  public:
62 
63  /**
64  * Default constructor
65  */
67  {}
68 
69  /**
70  * Parameterized constructor
71  *
72  * \param router JSharedPointer of JModuleRouter, this is built via detector file.
73  * \param parameters struct that holds default-optimized parameters for the reconstruction, for ARCA and ORCA configuration.
74  */
76  const JFIT::JMuonPrefitParameters_t &parameters):
77  router_(router),
78  parameters_(parameters),
79  omega_(parameters.gridAngle_deg* JTOOLS::PI/180.0)
80  {}
81 
82 
83  /**
84  * Parameterized constructor
85  *
86  * \param router JSharedPointer of JModuleRouter, this is built via detector file.
87  * \param parameters struct that holds default-optimized parameters for the reconstruction, for ARCA and ORCA configuration.
88  * \param omega which is constructed by gridAngle_deg (part of JMuonPrefitParameters_t)
89  */
91  const JFIT::JMuonPrefitParameters_t &parameters,
92  const JGEOMETRY3D::JOmega3D &omega):
93  router_(router),
94  parameters_(parameters),
95  omega_(omega)
96  {}
97 
99  {}
100 
101  /**
102  * Declaration Member function that actually performs the reconstruction
103  *
104  * \param timeSliceBuildL0 which is contructed via a JDAQEvent
105  * \param timeSliceBuildL2 which is contructed via a JDAQEvent
106  * \param out non const JEvt.
107  */
108  void
109  getJEvt(const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL0,
110  const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL2,
111  JFIT::JEvt &out) const;
112 
113  /**
114  * Sort hits according times corrected for position along z-axis.
115  * used as a predicate in std::partial_sort
116  *
117  * \param first first hit
118  * \param second second hit
119  * \return true if first hit earlier than second hit; else false
120  */
121  static bool
122  cmz(const JTRIGGER::JHitR1& first,
123  const JTRIGGER::JHitR1& second)
124  {
125  return (first .getT() * JTOOLS::getSpeedOfLight() - first .getZ() <
126  second.getT() * JTOOLS::getSpeedOfLight() - second.getZ());
127  }
128 
129  };
130 }
131 
132 
133 /**
134  * Member function definition
135  */
136 
137 void
139  const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL2,
140  JFIT::JEvt &out) const
141 {
142  const double sigma_ns = parameters_.sigma_ns;
143  const double gridAngle_deg = parameters_.gridAngle_deg;
144  const bool useL0 = parameters_.useL0;
145  const int numberOfOutliers = parameters_.numberOfOutliers;
146  const std::size_t numberOfPrefits = parameters_.numberOfPrefits;
147  const double Tmax_ns = parameters_.Tmax_ns;
148  const double ctMin = parameters_.ctMin;
149  const double roadWidth_m = parameters_.roadWidth_m;
150 
151  const int FACTORY_LIMIT = 8; // [unit]
152  const double STANDARD_DEVIATIONS = 3.0; // [unit]
153  const double HIT_OFF = 1.0e3 * sigma_ns * sigma_ns; // [ns^2]
154  const int NUMBER_OF_L1HITS = (useL0 ? 1 : 4);
155  const int MAXIMUM_NUMBER_OF_HITS = 50; //numeric_limits<int>::max();
156 
157  typedef std::vector<JTRIGGER::JHitL0> JDataL0_t;
158  typedef std::vector<JTRIGGER::JHitL1> JDataL1_t;
159  typedef std::vector<JTRIGGER::JHitR1> JDataR1_t;
160 
163  const JTRIGGER::JBuildL2<JTRIGGER::JHitL1> buildL2(2, Tmax_ns, ctMin);
164  const JTRIGGER::JMatch1D<JTRIGGER::JHitR1> match1D(roadWidth_m, Tmax_ns);
165  const JTRIGGER::JMatch3B<JTRIGGER::JHitL1> match3B(roadWidth_m, Tmax_ns);
166  const JFIT::JHitL1Comparator compare;
167 
168  // L1 and L0 data
169  JDataL0_t dataL0;
170  JDataL1_t dataL1;
171 
172  buildL0( timeSliceBuildL0, *router_, back_inserter(dataL0) );
173  buildL2( timeSliceBuildL2, *router_, back_inserter(dataL1) );
174 
175  // 3D cluster of unique optical modules
176 
177  JDataL1_t::iterator __end = std::unique(dataL1.begin(), dataL1.end(),
178  std::equal_to<KM3NETDAQ::JDAQModuleIdentifier>()
179  );
180 
181  __end = JTRIGGER::clusterizeWeight(dataL1.begin(), __end, match3B);
182 
183  if (std::distance(dataL1.begin(), __end) >= NUMBER_OF_L1HITS) {
184 
185  JDataR1_t data;
186 
187  for (JGEOMETRY3D::JOmega3D_t::const_iterator dir = omega_.begin(); dir != omega_.end(); ++dir) {
188 
189  const JGEOMETRY3D::JRotation3D R(*dir);
190 
191  data.clear();
192 
193  std::copy(dataL1.begin(), __end, back_inserter(data));
194 
195  for (JDataR1_t::iterator i = data.begin(); i != data.end(); ++i) {
196  i->rotate(R);
197  }
198 
199 
200  JDataR1_t::iterator __end1 = data.end();
201 
202 
203  // reduce data
204 
205  if ( std::distance(data.begin(), __end1) > MAXIMUM_NUMBER_OF_HITS ) {
206 
207  std::advance(__end1 = data.begin(), MAXIMUM_NUMBER_OF_HITS);
208 
209  std::partial_sort(data.begin(), __end1, data.end(), cmz);
210  }
211 
212 
213  // 1D cluster
214 
215  __end1 = JTRIGGER::clusterizeWeight(data.begin(), __end1, match1D);
216 
217  if ( std::distance(data.begin(), __end1) < NUMBER_OF_L1HITS ) {
218  continue;
219  }
220 
221  if (useL0) {
222 
223  data.erase(__end1, data.end());
224 
225  data.reserve(data.size() + dataL0.size());
226 
227  __end1 = data.end();
228 
229  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
230 
231  if ( std::find_if( data.begin(), __end1,
232  std::bind2nd(std::equal_to<KM3NETDAQ::JDAQModuleIdentifier>(), i->getModuleID())
233  ) == __end1 ){
234  JHitR1 hit(*i);
235 
236  hit.rotate(R);
237 
238  if ( std::count_if(data.begin(), __end1, JBind2nd(match1D,hit)) >= NUMBER_OF_L1HITS ) {
239  data.push_back(hit);
240  }
241  }
242  }
243 
244  __end1 = JTRIGGER::clusterize(__end1, data.end(), match1D);
245  }
246 
247 
248  if ( std::distance(data.begin(), __end1) <= JFIT::JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS ) {
249  continue;
250  }
251 
252  // 1D fit
253 
254  JFIT::JLine1Z tz;
255  double chi2 = std::numeric_limits<double>::max();
256  int NDF = 0;
257  int N = 0;
258 
259  JFIT::JMatrixNZ V;
260  JFIT::JVectorNZ Y;
261 
262  if ( std::distance(data.begin(), __end1) <= FACTORY_LIMIT ) {
263 
264  double ymin = std::numeric_limits<double>::max();
265 
266  JDataR1_t::iterator __end2 = __end1;
267 
268  for (int n = 0;
269  n <= numberOfOutliers && std::distance(data.begin(), __end2) > JFIT::JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS;
270  ++n, --__end2) {
271 
272  std::sort(data.begin(), __end1, compare);
273 
274  do {
275 
276  try {
277 
278  JFIT::JEstimator<JFIT::JLine1Z> fit(data.begin(), __end2);
279 
280  V.set(fit, data.begin(), __end2, gridAngle_deg, sigma_ns);
281  Y.set(fit, data.begin(), __end2);
282 
283  V.invert();
284 
285  double y = getChi2(Y, V);
286 
287  if (y <= 0.0) {
288  //std::cout<<"THIS IS A WARNING chi2(1) " << y <<std::endl;
289  //WARNING(endl << "chi2(1) " << y << endl); this gives: text+0x4320): undefined reference to `debug'
290  }else if (y < ymin) {
291  ymin = y;
292  tz = fit;
293  NDF = std::distance(data.begin(), __end2) - JFIT::JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS;
294  N = JFIT::getCount(data.begin(), __end2);
295  chi2 = y;
296  }
297  }
298  catch(const JLANG::JException& error) {}
299 
300  } while ( JTOOLS::next_permutation(data.begin(), __end2, __end1, compare) );
301 
302  ymin -= STANDARD_DEVIATIONS * STANDARD_DEVIATIONS;
303  }
304 
305  }else {
306 
307  try {
308 
309  const int number_of_outliers = std::distance(data.begin(), __end1) - JFIT::JEstimator<JLine1Z>::NUMBER_OF_PARAMETERS - 1;
310 
311  JFIT::JEstimator<JLine1Z> fit(data.begin(), __end1);
312 
313  V.set(fit, data.begin(), __end1, gridAngle_deg, sigma_ns);
314  Y.set(fit, data.begin(), __end1);
315 
316  V.invert();
317 
318  NDF = std::distance(data.begin(), __end1) - JFIT::JEstimator<JFIT::JLine1Z>::NUMBER_OF_PARAMETERS;
319  N = JFIT::getCount(data.begin(), __end1);
320 
321  for (int n = 0; n <= number_of_outliers && NDF > 0; ++n, --NDF) {
322 
323  double ymax = 0.0;
324  int kill = -1;
325 
326  for (unsigned int i = 0; i != Y.size(); ++i) {
327 
328  double y = JFIT::getChi2(Y, V, i);
329 
330  if (y <= 0.0) {
331  //std::cout<<"THIS IS A WARNING chi2(1) " << y <<std::endl;
332  //WARNING(endl << "chi2(2) " << y << endl);
333  } else if (y > ymax) {
334  ymax = y;
335  kill = i;
336  }
337  }
338 
339  if (ymax < STANDARD_DEVIATIONS * STANDARD_DEVIATIONS) {
340  break;
341  }
342 
343  V.update(kill, HIT_OFF);
344 
345  fit.update(data.begin(), __end1, V);
346 
347  Y.set(fit, data.begin(), __end1);
348 
349  N -= JFIT::getCount(data[kill]);
350  }
351 
352  chi2 = JFIT::getChi2(Y, V);
353  tz = fit;
354  }
355  catch(const JLANG::JException& error) {}
356  }
357 
358  if (NDF != 0) {
359 
360  tz.rotate_back(R);
361 
362  const double energy(0);
363  out.push_back( JFIT::getFit(JHistory(JMUONPREFIT), tz, *dir, getQuality(chi2, (useL0 ? N : NDF)), NDF,
364  energy,JFIT::JFitStatus_t::OKAY) );
365  }
366 
367  }
368 
369  // apply default sorter
370 
371  if (numberOfPrefits > 0) {
372 
373  JEvt::iterator __end = out.end();
374 
375  std::advance(__end = out.begin(), std::min(numberOfPrefits, out.size()));
376 
377  std::partial_sort(out.begin(), __end, out.end(), qualitySorter);
378 
379  // add downward pointing solutions if available but absent so far
380 
381  if ( std::count_if(out.begin(), __end,
383  ) == 0) {
384 
385  JEvt::iterator __begin = __end;
386  JEvt::iterator __q = std::partition(__begin, out.end(),
388  );
389 
390  std::advance(__end = __begin, std::min(numberOfPrefits, (size_t) std::distance(__begin, __q)));
391 
392  std::partial_sort(__begin, __end, __q, qualitySorter);
393  }
394 
395  out.erase(__end, out.end());
396 
397  } else {
398 
399  std::sort(out.begin(), out.end(), qualitySorter);
400  }
401 
402  }
403 }
404 
405 #endif
Auxiliary methods to evaluate Poisson probabilities and chi2.
JMuonPrefit()
Default constructor.
Definition: JMuonPrefit.hh:66
double getT() const
Get calibrated time of hit.
Definition: JHit.hh:143
General exception.
Definition: JException.hh:40
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)...
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
Match operator for Cherenkov light from muon with given direction.
JBinder2nd< JHit_t > JBind2nd(const JMatch< JHit_t > &match, const JHit_t &second)
Auxiliary method to create JBinder2nd object.
Definition: JBind2nd.hh:70
JMuonPrefit(const JLANG::JSharedPointer< const JDETECTOR::JModuleRouter > &router, const JFIT::JMuonPrefitParameters_t &parameters, const JGEOMETRY3D::JOmega3D &omega)
Parameterized constructor.
Definition: JMuonPrefit.hh:90
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
Definition for fit results, A good fit should hold: OKAY.
Algorithms for hit clustering and sorting.
const double getSpeedOfLight()
Number of bytes in a gigabyte.
Definition: JConstants.hh:89
int getCount(const JHitL0 &hit)
Get hit count.
Definition: JEvtToolkit.hh:728
Rotation matrix.
Definition: JRotation3D.hh:108
static const double PI
Constants.
Definition: JConstants.hh:20
Container for historical events.
Definition: JHistory.hh:95
Direction set covering (part of) solid angle.
Definition: JOmega3D.hh:62
Linear fit of JFIT::JLine1Z.
Template definition of linear fit.
Definition: JEstimator.hh:25
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
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
Basic data structure for L0 hit.
Auxiliary class for permutations of L1 hits.
Definition: JEvtToolkit.hh:501
Definition of fit parameters from various applications.
JMuonPrefit(const JLANG::JSharedPointer< const JDETECTOR::JModuleRouter > &router, const JFIT::JMuonPrefitParameters_t &parameters)
Parameterized constructor.
Definition: JMuonPrefit.hh:75
Constants.
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
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
Data time slice.
void set(const JLine1Z &track, T __begin, T __end)
Set time residual vector.
Definition: JVectorNZ.hh:74
Data struct that holds the parameters for JMuonPrefit angular reconstruction This is part of the cons...
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
double getDZ() const
Get Z-slope.
Definition: JEvt.hh:149
Reduced data structure for L1 hit.
JGEOMETRY3D::JOmega3D omega_
Definition: JMuonPrefit.hh:58
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
JFIT::JMuonPrefitParameters_t parameters_
Definition: JMuonPrefit.hh:57
static bool cmz(const JTRIGGER::JHitR1 &first, const JTRIGGER::JHitR1 &second)
Sort hits according times corrected for position along z-axis.
Definition: JMuonPrefit.hh:122
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
JLANG::JSharedPointer< const JDETECTOR::JModuleRouter > router_
Definition: JMuonPrefit.hh:56
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:40
2-dimensional frame with time calibrated data from one optical module.
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
bool qualitySorter(const JFit &first, const JFit &second)
Comparison of fit results.
Definition: JEvtToolkit.hh:223
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
void getJEvt(const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL0, const KM3NETDAQ::JDAQTimeslice &timeSliceBuildL2, JFIT::JEvt &out) const
Declaration Member function that actually performs the reconstruction.
Definition: JMuonPrefit.hh:138
Template L0 hit builder.
Definition: JBuildL0.hh:35
class to handle Prefit angular reconstruction.
Definition: JMuonPrefit.hh:53
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:80
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
double getZ() const
Get z position.
Definition: JVector3D.hh:113
void invert()
Invert matrix.
Definition: JMatrixNS.hh:61
Basic data structure for L1 hit.
3D match criterion with road width.
Definition: JMatch3B.hh:34