Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGandalfx.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"
21 #include "JTrigger/JHitL0.hh"
22 #include "JTrigger/JBuildL0.hh"
24 
28 #include "JSupport/JSupport.hh"
29 #include "JSupport/JMeta.hh"
30 
31 #include "JFit/JHitW0.hh"
32 #include "JFit/JPMTW0.hh"
33 #include "JFit/JLine3Z.hh"
34 #include "JFit/JGandalf.hh"
35 #include "JFit/JLine3ZRegressor.hh"
36 #include "JFit/JFitToolkit.hh"
37 #include "JFit/JEvt.hh"
38 #include "JFit/JEvtToolkit.hh"
39 #include "JFit/JRegressor.hh"
40 #include "JFit/JFitParameters.hh"
41 #include "JFit/JModel.hh"
42 
43 #include "JTools/JConstants.hh"
44 #include "JTools/JFunction1D_t.hh"
46 
47 #include "JPhysics/JPDFTable.hh"
48 #include "JPhysics/JPDFToolkit.hh"
49 
50 #include "Jeep/JParser.hh"
51 #include "Jeep/JMessage.hh"
52 
53 
54 namespace {
55 
56  using namespace JPP;
57 
58  /**
59  * Compare hits by PMT identifier and time.
60  *
61  * \param first first hit
62  * \param second second hit
63  * \return true if first before second; else false
64  */
65  inline bool compare(const JHitL0& first, const JHitL0& second)
66  {
67  if (std::equal_to<KM3NETDAQ::JDAQPMTIdentifier>()(first, second))
68  return std::less<JTRIGGER::JHit>()(first, second);
69  else
70  return std::less<KM3NETDAQ::JDAQPMTIdentifier>()(first, second);
71  }
72 }
73 
74 
75 /**
76  * \file
77  *
78  * Program to perform JFIT::JRegressor<JLine3Z,JGandalf> fit with I/O of JFIT::JEvt data.
79  * \author mdejong
80  */
81 int main(int argc, char **argv)
82 {
83  using namespace std;
84  using namespace JPP;
85  using namespace KM3NETDAQ;
86 
87  typedef JParallelFileScanner< JTypeList<JDAQEvent, JEvt> > JParallelFileScanner_t;
88  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
90 
91  JParallelFileScanner_t inputFile;
93  JLimit_t& numberOfEvents = inputFile.getLimit();
94  string detectorFile;
95  string pdfFile;
96  double roadWidth_m;
97  double R_Hz;
98  size_t numberOfPrefits;
99  double TTS_ns;
100  double E_GeV;
101  int debug;
102 
103  try {
104 
105  JParser<> zap("Program to perform PDF fit of muon trajectory to data.");
106 
107  zap['f'] = make_field(inputFile);
108  zap['o'] = make_field(outputFile) = "gandalf.root";
109  zap['a'] = make_field(detectorFile);
110  zap['n'] = make_field(numberOfEvents) = JLimit::max();
111  zap['P'] = make_field(pdfFile);
112  zap['R'] = make_field(roadWidth_m) = numeric_limits<double>::max();
113  zap['B'] = make_field(R_Hz) = 6.0e3;
114  zap['N'] = make_field(numberOfPrefits) = 1;
115  zap['T'] = make_field(TTS_ns);
116  zap['E'] = make_field(E_GeV) = 1.0e3;
117  zap['d'] = make_field(debug) = 1;
118 
119  zap(argc, argv);
120  }
121  catch(const exception& error) {
122  FATAL(error.what() << endl);
123  }
124 
125 
126 
127  cout.tie(&cerr);
128 
129 
131 
132  try {
133  load(detectorFile, detector);
134  }
135  catch(const JException& error) {
136  FATAL(error);
137  }
138 
139  const JModuleRouter moduleRouter(detector);
140 
141 
142  typedef vector<JHitL0> JDataL0_t;
143  typedef vector<JHitW0> JDataW0_t;
144  const JBuildL0<JHitL0> buildL0;
145 
146 
147  typedef JRegressor<JLine3Z, JGandalf> JRegressor_t;
148 
150  JRegressor_t::T_ns.setRange(-50.0, +450.0); // ns
151  JRegressor_t::Vmax_npe = 10.0; // npe
152  JRegressor_t::MAXIMUM_ITERATIONS = 10000;
153 
154  const double Rmax_m = 100.0; //!< hit selection [m]
155  //const double Tmax_ns = 10.0; //!< hit selection [ns]
156 
157  JRegressor_t fit(pdfFile, TTS_ns);
158 
159  fit.estimator.reset(new JMEstimatorLorentzian());
160 
161  fit.parameters.resize(5);
162 
163  fit.parameters[0] = JLine3Z::pX();
164  fit.parameters[1] = JLine3Z::pY();
165  fit.parameters[2] = JLine3Z::pT();
166  fit.parameters[3] = JLine3Z::pDX();
167  fit.parameters[4] = JLine3Z::pDY();
168 
169  if (fit.getRmax() < roadWidth_m) {
170 
171  roadWidth_m = fit.getRmax();
172 
173  WARNING("Set road width to [m] " << roadWidth_m << endl);
174  }
175 
176 
177  outputFile.open();
178 
179  outputFile.put(JMeta(argc, argv));
180 
181  while (inputFile.hasNext()) {
182 
183  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
184 
185  multi_pointer_type ps = inputFile.next();
186 
187  JDAQEvent* tev = ps;
188  JEvt* evt = ps;
189  JEvt out;
190 
191  if (!evt->empty()) {
192 
193  JEvt::iterator __end = evt->end();
194 
195  if (numberOfPrefits > 0) {
196  advance(__end = evt->begin(), min(numberOfPrefits, evt->size()));
197  }
198 
199  partial_sort(evt->begin(), __end, evt->end(), qualitySorter);
200 
201 
202  JDataL0_t dataL0;
203 
204  buildL0(*tev, moduleRouter, true, back_inserter(dataL0));
205 
206 
207  if (dataL0.size() >= fit.parameters.size()) {
208 
209  for (JEvt::const_iterator track = evt->begin(); track != __end; ++track) {
210 
211  const JRotation3D R (getDirection(*track));
212  const JLine1Z tz(getPosition (*track).rotate(R), track->getT());
213  const JModel<JLine1Z> match(tz, roadWidth_m, JRegressor_t::T_ns);
214 
215  // hit selection based on start value
216 
217  JDataW0_t data;
218 
220 
221  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
222 
223  JHitW0 hit(*i, R_Hz);
224 
225  hit.rotate(R);
226 
227  if (match(hit)) {
228 
229  data.push_back(hit);
230 
231  if (tz.getDistance(hit) <= Rmax_m) {
232  top.insert(hit.getPMTIdentifier());
233  }
234  }
235  }
236 
237  // select first hit
238 
239  sort(data.begin(), data.end(), compare);
240 
241  JDataW0_t::iterator __end = unique(data.begin(), data.end(), equal_to<JDAQPMTIdentifier>());
242 
243 
244  vector<JPMTW0> buffer;
245 
246  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
247 
248  JPosition3D pos(module->getPosition());
249 
250  pos.rotate(R);
251 
252  if (tz.getDistance(pos) <= Rmax_m) {
253 
254  for (unsigned int i = 0; i != module->size(); ++i) {
255 
256  const JDAQPMTIdentifier id(module->getID(), i);
257 
258  JPMT pmt(module->getPMT(i));
259 
260  pmt.rotate(R);
261 
262  buffer.push_back(JPMTW0(pmt, R_Hz, top.count(id)));
263  }
264  }
265  }
266 
267 
268 
269  const int NDF = distance(data.begin(), __end) - fit.parameters.size();
270 
271  if (NDF >= 0) {
272 
273  // set fit parameters
274 
275  if (track->getE() > 0.1)
276  fit.E_GeV = track->getE();
277  else
278  fit.E_GeV = E_GeV;
279 
280  const double chi2 = fit(JLine3Z(tz), data.begin(), __end);
281 
282  fit(fit.value, data.begin(), __end, buffer.begin(), buffer.end());
283 
284  JTrack3D tb(fit.value);
285 
286  tb.rotate_back(R);
287 
288  out.push_back(getFit(JMUONGANDALF, tb, getQuality(chi2), NDF));
289 
290  // set additional weights
291 
292  out.rbegin()->setW(JGANDALF_BETA0_RAD, sqrt(fit.error.getDX() * fit.error.getDX() +
293  fit.error.getDY() * fit.error.getDY()));
294  out.rbegin()->setW(JGANDALF_BETA1_RAD, sqrt(fit.error.getDX() * fit.error.getDY()));
295  out.rbegin()->setW(JGANDALF_CHI2, chi2);
296  out.rbegin()->setW(JGANDALF_NUMBER_OF_HITS, distance(data.begin(), __end));
297  out.rbegin()->setW(JGANDALF_LAMBDA, fit.lambda);
298  out.rbegin()->setW(JGANDALF_NUMBER_OF_ITERATIONS, fit.numberOfIterations);
299  }
300  }
301 
302  // apply default sorter
303 
304  sort(out.begin(), out.end(), qualitySorter);
305  }
306  }
307 
308  outputFile.put(out);
309  }
310  STATUS(endl);
311 
313 
314  io >> outputFile;
315 
316  outputFile.close();
317 }
Auxiliary methods to evaluate Poisson probabilities and chi2.
Data regression method for JFIT::JLine3Z.
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:71
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1410
General exception.
Definition: JException.hh:40
General purpose data regression method.
#define WARNING(A)
Definition: JMessage.hh:63
Auxiliary methods for PDF calculations.
number of iterations from JGandalf.cc
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:102
const JDAQPMTIdentifier & getPMTIdentifier() const
Get PMT identifier.
Auxiliary class for handling PMT geometry, rate and response.
Definition: JPMTW0.hh:22
#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
General purpose class for parallel reading of objects from a single file or multiple files...
Template specialisation of class JModel to match hit with muon trajectory along z-axis.
Definition: JModel.hh:31
Data structure for fit of straight line in positive z-direction.
Definition: JLine3Z.hh:35
chi2 from JGandalf.cc
string outputFile
Data structure for detector geometry and calibration.
angular resolution [rad] from JGandalf.cc
Various implementations of functional maps.
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.
Definition of fit parameters from various applications.
Basic data structure for time and time over threshold information of hit.
Type list.
Definition: JTypeList.hh:22
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
Constants.
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition: JAxis3D.hh:235
Detector file.
Definition: JHead.hh:126
control parameter from JGandalf.cc
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
Parallel scanning of objects from a single file or multiple files according a format that follows fro...
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:52
Auxiliary class for a hit with background rate value.
Definition: JHitW0.hh:21
ROOT I/O of application specific meta data.
angular resolution [rad] from JGandalf.cc
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.
number of hits from JGandalf.cc
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
Regressor function object for JLine3Z fit using JGandalf minimiser.
General purpose class for object reading from a list of file names.
Utility class to parse command line options.
Data structure for L0 hit.
Definition: JHitL0.hh:27
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.
JDirection3D getDirection(const Vec &v)
Get direction.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:72
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
Lorentzian M-estimator.
Definition: JMEstimator.hh:79
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
JPosition3D getPosition(const Vec &v)
Get position.
int main(int argc, char *argv[])
Definition: Main.cpp:15