Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPrefit.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 #include <algorithm>
7 
8 #include "evt/Head.hh"
9 #include "evt/Evt.hh"
10 #include "JDAQ/JDAQEvent.hh"
11 #include "JDAQ/JDAQTimeslice.hh"
12 #include "JDAQ/JDAQSummaryslice.hh"
13 
14 #include "JDetector/JDetector.hh"
17 
18 #include "JTrigger/JHit.hh"
19 #include "JTrigger/JFrame.hh"
20 #include "JTrigger/JTimeslice.hh"
22 #include "JTrigger/JHitL0.hh"
23 #include "JTrigger/JHitL1.hh"
24 #include "JTrigger/JHitR1.hh"
25 #include "JTrigger/JBuildL0.hh"
26 #include "JTrigger/JBuildL1.hh"
27 #include "JTrigger/JBuildL2.hh"
28 #include "JTrigger/JAlgorithm.hh"
29 #include "JTrigger/JMatch1D.hh"
30 #include "JTrigger/JMatch3B.hh"
31 #include "JTrigger/JBind2nd.hh"
33 
37 #include "JSupport/JSupport.hh"
38 #include "JSupport/JMeta.hh"
39 
40 #include "JFit/JLine1Z.hh"
41 #include "JFit/JLine1ZEstimator.hh"
42 #include "JFit/JMatrixNZ.hh"
43 #include "JFit/JVectorNZ.hh"
44 #include "JFit/JFitToolkit.hh"
45 #include "JFit/JEvt.hh"
46 #include "JFit/JEvtToolkit.hh"
47 #include "JFit/JFitParameters.hh"
48 
49 #include "JTools/JConstants.hh"
50 #include "JTools/JPermutation.hh"
51 
52 #include "JGeometry3D/JOmega3D.hh"
54 #include "JLang/JPredicate.hh"
55 
56 #include "Jeep/JParser.hh"
57 #include "Jeep/JMessage.hh"
58 
59 
60 namespace {
61 
62  using namespace JPP;
63 
64 
65  /**
66  * Sort hits according times corrected for position along z-axis.
67  *
68  * \param first first hit
69  * \param second second hit
70  * \return true if first hit earlier than second hit; else false
71  */
72  inline bool cmz(const JHitR1& first, const JHitR1& second)
73  {
74  return (first .getT() * getSpeedOfLight() - first .getZ() <
75  second.getT() * getSpeedOfLight() - second.getZ());
76  }
77 }
78 
79 
80 /**
81  * \file
82  *
83  * Program to perform linear fits JFIT::JEstimator<JLine1Z> covering the solid angle producing JFIT::JEvt data.
84  *
85  * The option <tt>-U</tt> has the following consequences:
86  * - L0 hits are included in the fit;
87  * - L1 hits are obtained from the list of triggered hits;
88  * - quality of the fit is determined counting all L0 hits;
89  * \author mdejong
90  */
91 int main(int argc, char **argv)
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(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 }
Auxiliary methods to evaluate Poisson probabilities and chi2.
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:71
double getT() const
Get calibrated time of hit.
Definition: JHit.hh:143
Object writing to file.
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)...
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
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
Algorithms for hit clustering and sorting.
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:102
double getCount(TH1D *hptr, int muon_threshold)
const double getSpeedOfLight()
Number of bytes in a gigabyte.
Definition: JConstants.hh:89
#define STATUS(A)
Definition: JMessage.hh:61
Detector data structure.
Definition: JDetector.hh:77
Recording of objects on file according a format that follows from the file name extension.
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
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
string outputFile
Data structure for detector geometry and calibration.
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.
Basic data structure for time and time over threshold information of hit.
Scanning of objects from a single file according a format that follows from the extension of each fil...
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
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
Detector file.
Definition: JHead.hh:126
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
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
ROOT I/O of application specific meta data.
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
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.
Definition: JCounter.hh:35
#define FATAL(A)
Definition: JMessage.hh:65
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Direct access to module in detector data structure.
Reduced data structure for L1 hit.
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
Utility class to parse command line options.
ROOT TTree parameter settings.
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
2-dimensional frame with time calibrated data from one optical module.
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
Object reading from a list of files.
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
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.
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
int main(int argc, char *argv[])
Definition: Main.cpp:15