Jpp
JGandalf.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <set>
6 #include <algorithm>
7 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TProfile.h"
11 #include "TH2D.h"
12 
13 #include "evt/Head.hh"
14 #include "evt/Evt.hh"
15 #include "JDAQ/JDAQEvent.hh"
16 #include "JDAQ/JDAQTimeslice.hh"
17 #include "JDAQ/JDAQSummaryslice.hh"
18 
19 #include "JDetector/JDetector.hh"
22 
23 #include "JLang/JPredicate.hh"
24 
25 #include "JTrigger/JHit.hh"
26 #include "JTrigger/JFrame.hh"
27 #include "JTrigger/JTimeslice.hh"
28 #include "JTrigger/JHitL0.hh"
29 #include "JTrigger/JBuildL0.hh"
31 
35 #include "JSupport/JSupport.hh"
36 #include "JSupport/JMeta.hh"
38 
39 #include "JFit/JHitW0.hh"
40 #include "JFit/JPMTW0.hh"
41 #include "JFit/JLine3Z.hh"
42 #include "JFit/JGandalf.hh"
43 #include "JFit/JLine3ZRegressor.hh"
44 #include "JFit/JFitToolkit.hh"
45 #include "JFit/JEvt.hh"
46 #include "JFit/JEvtToolkit.hh"
47 #include "JFit/JRegressor.hh"
48 #include "JFit/JFitParameters.hh"
49 #include "JFit/JModel.hh"
50 
51 #include "JTools/JConstants.hh"
52 #include "JTools/JFunction1D_t.hh"
55 
56 #include "JROOT/JRootToolkit.hh"
57 #include "JGizmo/JManager.hh"
58 
59 #include "JPhysics/JPDFTable.hh"
60 #include "JPhysics/JPDFToolkit.hh"
61 
62 #include "Jeep/JParser.hh"
63 #include "Jeep/JMessage.hh"
64 
65 
66 namespace {
67 
68  using namespace JPP;
69 
70  /**
71  * Compare hits by PMT identifier and time.
72  *
73  * \param first first hit
74  * \param second second hit
75  * \return true if first before second; else false
76  */
77  inline bool compare(const JHitL0& first, const JHitL0& second)
78  {
79  if (std::equal_to<KM3NETDAQ::JDAQPMTIdentifier>()(first, second))
80  return std::less<JTRIGGER::JHit>()(first, second);
81  else
82  return std::less<KM3NETDAQ::JDAQPMTIdentifier>()(first, second);
83  }
84 }
85 
86 /**
87  * \file
88  *
89  * Program to perform JFIT::JRegressor<JLine3Z,JGandalf> fit with I/O of JFIT::JEvt data.
90  *
91  * This program can also be used for detector calibrations.
92  * To this end, the output from a previous run of this program can be used in conjunction with option <tt>-c</tt>.
93  * It is recommended to accordingly set option <tt>-N 1</tt> so that only the best track from the previous output is used.
94  * Several histograms will be produced which can subsequently be processed with JHobbit.cc for
95  * the determination of the time calibration of optical modules and JFrodo.cc for the time-slewing correction.
96  * \author mdejong
97  */
98 int main(int argc, char **argv)
99 {
100  using namespace std;
101  using namespace JPP;
102  using namespace KM3NETDAQ;
103 
104  typedef JParallelFileScanner< JTypeList<JDAQEvent, JEvt> > JParallelFileScanner_t;
105  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
107  typedef JAbstractHistogram<double> histogram_type;
108 
109  JParallelFileScanner_t inputFile;
111  JLimit_t& numberOfEvents = inputFile.getLimit();
112  string detectorFile;
113  string pdfFile;
114  double roadWidth_m;
115  double R_Hz;
116  size_t numberOfPrefits;
117  double TTS_ns;
118  JTimeRange T_compress;
119  double E_GeV;
120  JZRange z;
121  bool reprocess;
122  histogram_type calibrate;
123  int debug;
124 
125  try {
126 
127  JParser<> zap("Program to perform PDF fit of muon trajectory to data.");
128 
129  zap['f'] = make_field(inputFile);
130  zap['o'] = make_field(outputFile) = "gandalf.root";
131  zap['a'] = make_field(detectorFile);
132  zap['n'] = make_field(numberOfEvents) = JLimit::max();
133  zap['P'] = make_field(pdfFile);
134  zap['R'] = make_field(roadWidth_m) = numeric_limits<double>::max();
135  zap['B'] = make_field(R_Hz, "Default background rate if no summary data are available.") = 6.0e3;
136  zap['N'] = make_field(numberOfPrefits) = 0;
137  zap['T'] = make_field(TTS_ns, "Time smearing of PDF.");
138  zap['C'] = make_field(T_compress) = JTimeRange();
139  zap['E'] = make_field(E_GeV, "Default energy if no energy estimate is available.") = 1.0e3;
140  zap['z'] = make_field(z, "Extension of longitudinal range of muon track.") = JZRange(0.0, 0.0);
141  zap['r'] = make_field(reprocess);
142  zap['c'] = make_field(calibrate, "Histogram for time calibration per optical module.") = histogram_type();
143  zap['d'] = make_field(debug) = 1;
144 
145  zap(argc, argv);
146  }
147  catch(const exception& error) {
148  FATAL(error.what() << endl);
149  }
150 
151  cout.tie(&cerr);
152 
154 
155  try {
156  load(detectorFile, detector);
157  }
158  catch(const JException& error) {
159  FATAL(error);
160  }
161 
162  const JModuleRouter moduleRouter (detector);
163  JSummaryFileRouter summaryRouter(inputFile, R_Hz);
164 
165 
166  typedef vector<JHitL0> JDataL0_t;
167  typedef vector<JHitW0> JDataW0_t;
168  const JBuildL0<JHitL0> buildL0;
169 
170 
171  typedef JRegressor<JLine3Z, JGandalf> JRegressor_t;
172 
174  JRegressor_t::T_ns.setRange(-50.0, +450.0); // ns
175  JRegressor_t::Vmax_npe = 10.0; // npe
176  JRegressor_t::MAXIMUM_ITERATIONS = 1000;
177 
178  JRegressor_t fit(pdfFile, TTS_ns);
179 
180  if (T_compress != JTimeRange()) {
181  fit.compress(T_compress);
182  }
183 
184  fit.parameters.resize(5);
185 
186  fit.parameters[0] = JLine3Z::pX();
187  fit.parameters[1] = JLine3Z::pY();
188  fit.parameters[2] = JLine3Z::pT();
189  fit.parameters[3] = JLine3Z::pDX();
190  fit.parameters[4] = JLine3Z::pDY();
191 
192  if (fit.getRmax() < roadWidth_m) {
193  roadWidth_m = fit.getRmax();
194  }
195 
196  TH2D* ha = NULL;
197  TProfile* hb = NULL;
198  TH2D* h2 = NULL;
199 
200  typedef JManager<string, TProfile> JManager_t;
201 
202  JManager_t gd;
203 
204  if (calibrate.is_valid()) {
205 
206  if (numberOfPrefits != 1) {
207  WARNING("Running in calibration mode but number of prefits " << numberOfPrefits << " != " << 1 << endl);
208  }
209 
210  ha = new TH2D ("ha", NULL, 256, -0.5, 255.5,
211  calibrate.getNumberOfBins(), calibrate.getLowerLimit(), calibrate.getUpperLimit());
212  hb = new TProfile("hb", NULL, 256, -0.5, 255.5);
213 
214  h2 = new TH2D ("h2", NULL, detector.size(), -0.5, detector.size() - 0.5,
215  calibrate.getNumberOfBins(), calibrate.getLowerLimit(), calibrate.getUpperLimit());
216 
217  gd = JManager_t(new TProfile("%", NULL, detector.size(), -0.5, detector.size() - 0.5));
218  }
219 
220  outputFile.open();
221 
222  outputFile.put(JMeta(argc, argv));
223 
224  while (inputFile.hasNext()) {
225 
226  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
227 
228  multi_pointer_type ps = inputFile.next();
229 
230  JDAQEvent* tev = ps;
231  JEvt* evt = ps;
232  JEvt out;
233 
234  summaryRouter.update(*tev);
235 
236  JEvt::iterator __end = evt->end();
237 
238  if (reprocess) {
239  __end = partition(evt->begin(), __end, JHistory::is_not_event(JMUONGANDALF));
240  }
241 
242  if (evt->begin() != __end) {
243 
244  copy(evt->begin(), __end, back_inserter(out));
245 
246  if (numberOfPrefits > 0) {
247  advance(__end = evt->begin(), min(numberOfPrefits, out.size()));
248  }
249 
250  partial_sort(evt->begin(), __end, evt->end(), qualitySorter);
251 
252  __end = partition(evt->begin(), __end, JHistory::is_event(evt->begin()->getHistory()));
253 
254 
255  JDataL0_t dataL0;
256 
257  buildL0(*tev, moduleRouter, true, back_inserter(dataL0));
258 
259 
260  if (dataL0.size() >= fit.parameters.size()) {
261 
262  for (JEvt::const_iterator track = evt->begin(); track != __end; ++track) {
263 
264  const JRotation3D R (getDirection(*track));
265  const JLine1Z tz(getPosition (*track).rotate(R), track->getT());
266  JZRange Z; // default infinite track length for hit selection
267 
268  if (track->hasW(JSTART_LENGTH_METRES)) { // use output of JStart if available
269  Z = JZRange(0.0, track->getW(JSTART_LENGTH_METRES)) + z;
270  }
271 
272  const JModel<JLine1Z> match(tz, roadWidth_m, JRegressor_t::T_ns, Z);
273 
274  // hit selection based on start value
275 
276  JDataW0_t data;
277 
278  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
279 
280  double rate_Hz = summaryRouter.getRate(*i);
281 
282  if (rate_Hz <= 0.0) {
283  rate_Hz = summaryRouter.getRate();
284  }
285 
286  JHitW0 hit(*i, rate_Hz);
287 
288  hit.rotate(R);
289 
290  if (match(hit)) {
291  data.push_back(hit);
292  }
293  }
294 
295  // select first hit
296 
297  sort(data.begin(), data.end(), compare);
298 
299  JDataW0_t::iterator __end = unique(data.begin(), data.end(), equal_to<JDAQPMTIdentifier>());
300 
301 
302  const int NDF = distance(data.begin(), __end) - fit.parameters.size();
303 
304  if (NDF >= 0) {
305 
306  // set fit parameters
307 
308  if (track->getE() > 0.1)
309  fit.E_GeV = track->getE();
310  else
311  fit.E_GeV = E_GeV;
312 
313  const double chi2 = fit(JLine3Z(tz), data.begin(), __end);
314 
315  JTrack3D tb(fit.value);
316 
317  tb.rotate_back(R);
318 
319  out.push_back(getFit(JHistory(track->getHistory()).add(JMUONGANDALF), tb, getQuality(chi2), NDF));
320 
321  // set additional values
322 
323  out.rbegin()->setW(track->getW());
324  out.rbegin()->setW(JGANDALF_BETA0_RAD, sqrt(fit.error.getDX() * fit.error.getDX() +
325  fit.error.getDY() * fit.error.getDY()));
326  out.rbegin()->setW(JGANDALF_BETA1_RAD, sqrt(fit.error.getDX() * fit.error.getDY()));
327  out.rbegin()->setW(JGANDALF_CHI2, chi2);
328  out.rbegin()->setW(JGANDALF_NUMBER_OF_HITS, distance(data.begin(), __end));
329  out.rbegin()->setW(JGANDALF_LAMBDA, fit.lambda);
330  out.rbegin()->setW(JGANDALF_NUMBER_OF_ITERATIONS, fit.numberOfIterations);
331 
332  if (calibrate.is_valid()) {
333 
334  set<int> buffer;
335 
336  for (JDataW0_t::const_iterator hit = data.begin(); hit != __end; ++hit) {
337  buffer.insert(hit->getModuleID());
338  }
339 
340  const JLine3Z ta = fit.value;
341 
342  for (set<int>::const_iterator id = buffer.begin(); id != buffer.end(); ++id) {
343 
344  JDataW0_t::iterator q = partition(data.begin(), __end, make_predicate(&JHitW0::getModuleID, *id, JComparison::ne()));
345 
346  if (distance(data.begin(), q) - fit.parameters.size() > 0) {
347 
348  fit(ta, data.begin(), q); // re-fit
349 
350  sort(q, __end, compare); // select first hit in module
351 
352  const int index = moduleRouter.getIndex(*id);
353  const double t1 = fit.value.getT(q->getPosition());
354  JTrack3D gradient = fit(fit.value, *q).gradient;
355 
356  gradient.rotate_back(R);
357 
358  ha->Fill(q->getToT(), q->getT1() - t1);
359  hb->Fill(q->getToT(), gradient.getT());
360 
361  h2->Fill(index, q->getT() - t1);
362 
363  gd["T"]->Fill(index, gradient.getT());
364  gd["X"]->Fill(index, gradient.getX());
365  gd["Y"]->Fill(index, gradient.getY());
366  gd["Z"]->Fill(index, gradient.getZ());
367  }
368  }
369  }
370  }
371  }
372  }
373 
374  // apply default sorter
375 
376  sort(out.begin(), out.end(), qualitySorter);
377  }
378 
379  outputFile.put(out);
380  }
381  STATUS(endl);
382 
383  if (calibrate.is_valid()) {
384 
385  outputFile.put(*ha);
386  outputFile.put(*hb);
387  outputFile.put(*h2);
388 
389  for (JManager_t::const_iterator i = gd.begin(); i != gd.end(); ++i) {
390  outputFile.put(*(i->second));
391  }
392  }
393 
395 
396  io >> outputFile;
397 
398  outputFile.close();
399 }
JMeta.hh
JPredicate.hh
JAbstractHistogram.hh
JManager< string, TProfile >
KM3NETDAQ::JDAQEvent
DAQ Event.
Definition: JDAQEvent.hh:34
JLine3Z.hh
JTriggerParameters.hh
JTOOLS::pX
pX
Definition: JPolint.hh:625
JFIT::JHitW0
Auxiliary class for a hit with background rate value.
Definition: JHitW0.hh:21
JSUPPORT::JSummaryFileRouter::update
void update(const JDAQHeader &header)
Update router.
Definition: JSummaryFileRouter.hh:72
JFIT::JGANDALF_NUMBER_OF_ITERATIONS
number of iterations from JGandalf.cc
Definition: JFitParameters.hh:24
JFileRecorder.hh
JSUPPORT::JLimit
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
JFIT::JLine3Z
Data structure for fit of straight line in positive z-direction.
Definition: JLine3Z.hh:35
JGandalf.hh
JMessage.hh
JFIT::JMUONGANDALF
JGandalf.cc.
Definition: JFitApplications.hh:25
JDETECTOR::JModuleRouter::getIndex
const int getIndex(const JObjectID &id) const
Get index of module.
Definition: JModuleRouter.hh:113
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
JSummaryFileRouter.hh
JFitToolkit.hh
JLine3ZRegressor.hh
qualitySorter
bool qualitySorter(const JFIT::JFit &first, const JFIT::JFit &second)
Comparison of fit results.
Definition: quality_sorter.cc:10
JTimeslice.hh
JRegressor.hh
JGEOMETRY3D::JVector3D::getZ
double getZ() const
Get z position.
Definition: JVector3D.hh:114
JFIT::JGANDALF_CHI2
chi2 from JGandalf.cc
Definition: JFitParameters.hh:19
JDETECTOR::load
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
Definition: JDetectorToolkit.hh:456
JFIT::JGANDALF_BETA0_RAD
angular resolution [rad] from JGandalf.cc
Definition: JFitParameters.hh:17
JPDFToolkit.hh
std::vector< JHitL0 >
JTOOLS::JRange< double >
JEvtToolkit.hh
JGEOMETRY3D::JAxis3D::rotate_back
JAxis3D & rotate_back(const JRotation3D &R)
Rotate back axis.
Definition: JAxis3D.hh:251
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::JZRange
JTOOLS::JRange< double > JZRange
Definition: JModel.hh:21
JDAQTimeslice.hh
JFIT::JEvt
Data structure for set of track fit results.
Definition: JEvt.hh:292
JGEOMETRY3D::JTrack3D::getT
double getT(const JVector3D &pos) const
Get arrival time of Cherenkov light at given position.
Definition: JTrack3D.hh:126
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
JManager.hh
std::set< int >
JDAQSummaryslice.hh
JGEOMETRY3D::JPosition3D::rotate
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
JTOOLS::JTimeRange
JRange< double > JTimeRange
Type definition for time range.
Definition: JTools/JTimeRange.hh:19
JPMTW0.hh
JSupport.hh
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JEvt.hh
JFunction1D_t.hh
JBuildL0.hh
JSUPPORT::JSummaryFileRouter::getRate
double getRate() const
Get default rate.
Definition: JSummaryFileRouter.hh:61
JHitW0.hh
WARNING
#define WARNING(A)
Definition: JMessage.hh:65
JFIT::JRegressor< JLine3Z, JGandalf >
Regressor function object for JLine3Z fit using JGandalf minimiser.
Definition: JLine3ZRegressor.hh:112
debug
int debug
debug level
Definition: JSirene.cc:59
JConstants.hh
JFunctionalMap_t.hh
JFIT::JModel< JLine1Z >
Template specialisation of class JModel to match hit with muon trajectory along z-axis.
Definition: JModel.hh:34
JSUPPORT::JLimit::getLimit
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
JHit.hh
JModuleRouter.hh
JRootToolkit.hh
JFIT::JHistory::is_event
Auxiliary class to test history.
Definition: JHistory.hh:101
JGEOMETRY3D::JAxis3D::rotate
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition: JAxis3D.hh:236
JFIT::JGANDALF_LAMBDA
control parameter from JGandalf.cc
Definition: JFitParameters.hh:23
JParallelFileScanner.hh
JMultipleFileScanner.hh
JLANG::JTypeList
Type list.
Definition: JTypeList.hh:22
STATUS
#define STATUS(A)
Definition: JMessage.hh:63
JFIT::JGANDALF_NUMBER_OF_HITS
number of hits from JGandalf.cc
Definition: JFitParameters.hh:20
JFIT::JGANDALF_BETA1_RAD
angular resolution [rad] from JGandalf.cc
Definition: JFitParameters.hh:18
JParser.hh
JDetectorToolkit.hh
JFIT::JLine1Z
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
JFIT::JHistory::is_not_event
Auxiliary class to test history.
Definition: JHistory.hh:149
JDETECTOR::JModuleRouter
Router for direct addressing of module data in detector data structure.
Definition: JModuleRouter.hh:34
JModel.hh
JAANET::getDirection
JDirection3D getDirection(const Vec &v)
Get direction.
Definition: JAAnetToolkit.hh:221
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JFitParameters.hh
JSUPPORT::JParallelFileScanner
General purpose class for parallel reading of objects from a single file or multiple files.
Definition: JParallelFileScanner.hh:31
JDETECTOR::JDetector
Detector data structure.
Definition: JDetector.hh:80
JAANET::getPosition
JPosition3D getPosition(const Vec &v)
Get position.
Definition: JAAnetToolkit.hh:197
JTRIGGER::JHitL0
Data structure for L0 hit.
Definition: JHitL0.hh:27
JAANET::detector
Detector file.
Definition: JHead.hh:130
JFIT::JSTART_LENGTH_METRES
distance between first and last hits in metres from JStart.cc
Definition: JFitParameters.hh:27
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
std
Definition: jaanetDictionary.h:36
JGEOMETRY3D::JVector3D::getY
double getY() const
Get y position.
Definition: JVector3D.hh:103
KM3NETDAQ
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
JGEOMETRY3D::JTrack3D
3D track.
Definition: JTrack3D.hh:30
JTOOLS::JAbstractHistogram
Simple data structure for histogram binning.
Definition: JAbstractHistogram.hh:24
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
JSUPPORT::JSummaryFileRouter
File router for fast addressing of summary data.
Definition: JSummaryFileRouter.hh:37
JDAQEvent.hh
main
int main(int argc, char **argv)
Definition: JGandalf.cc:98
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
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
JDetector.hh
JGEOMETRY3D::JVector3D::getX
double getX() const
Get x position.
Definition: JVector3D.hh:93
JHitL0.hh
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:40
JPDFTable.hh
JGEOMETRY3D::JRotation3D
Rotation matrix.
Definition: JRotation3D.hh:111
JSUPPORT::JSingleFileScanner
Object reading from a list of files.
Definition: JSingleFileScanner.hh:75
JFrame.hh
JLANG::JComparison::ne
Not equal.
Definition: JComparison.hh:41