Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JGandalf.cc File Reference

Program to perform JFIT::JRegressor<JLine3Z,JGandalf> fit with I/O of JFIT::JEvt data. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <algorithm>
#include "TROOT.h"
#include "TFile.h"
#include "TH2D.h"
#include "evt/Head.hh"
#include "evt/Evt.hh"
#include "JDAQ/JDAQEvent.hh"
#include "JDAQ/JDAQTimeslice.hh"
#include "JDAQ/JDAQSummaryslice.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JLang/JPredicate.hh"
#include "JTrigger/JHit.hh"
#include "JTrigger/JFrame.hh"
#include "JTrigger/JTimeslice.hh"
#include "JTrigger/JHitL0.hh"
#include "JTrigger/JBuildL0.hh"
#include "JTrigger/JTriggerParameters.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JParallelFileScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JMeta.hh"
#include "JFit/JHitW0.hh"
#include "JFit/JPMTW0.hh"
#include "JFit/JLine3Z.hh"
#include "JFit/JGandalf.hh"
#include "JFit/JLine3ZRegressor.hh"
#include "JFit/JFitToolkit.hh"
#include "JFit/JEvt.hh"
#include "JFit/JEvtToolkit.hh"
#include "JFit/JRegressor.hh"
#include "JFit/JFitParameters.hh"
#include "JFit/JModel.hh"
#include "JTools/JConstants.hh"
#include "JTools/JFunction1D_t.hh"
#include "JTools/JFunctionalMap_t.hh"
#include "JPhysics/JPDFTable.hh"
#include "JPhysics/JPDFToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program to perform JFIT::JRegressor<JLine3Z,JGandalf> fit with I/O of JFIT::JEvt data.

Author
mdejong

Definition in file JGandalf.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 88 of file JGandalf.cc.

89 {
90  using namespace std;
91  using namespace JPP;
92  using namespace KM3NETDAQ;
93 
94  typedef JParallelFileScanner< JTypeList<JDAQEvent, JEvt> > JParallelFileScanner_t;
95  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
97 
98  JParallelFileScanner_t inputFile;
100  JLimit_t& numberOfEvents = inputFile.getLimit();
101  string detectorFile;
102  string pdfFile;
103  double roadWidth_m;
104  double R_Hz;
105  size_t numberOfPrefits;
106  double TTS_ns;
107  double E_GeV;
108  bool reprocess;
109  bool calibrate;
110  int debug;
111 
112  try {
113 
114  JParser<> zap("Program to perform PDF fit of muon trajectory to data.");
115 
116  zap['f'] = make_field(inputFile);
117  zap['o'] = make_field(outputFile) = "gandalf.root";
118  zap['a'] = make_field(detectorFile);
119  zap['n'] = make_field(numberOfEvents) = JLimit::max();
120  zap['P'] = make_field(pdfFile);
121  zap['R'] = make_field(roadWidth_m) = numeric_limits<double>::max();
122  zap['B'] = make_field(R_Hz) = 6.0e3;
123  zap['N'] = make_field(numberOfPrefits) = 0;
124  zap['T'] = make_field(TTS_ns);
125  zap['E'] = make_field(E_GeV) = 1.0e3;
126  zap['r'] = make_field(reprocess);
127  zap['c'] = make_field(calibrate, "Enable time calibration per optical module.");
128  zap['d'] = make_field(debug) = 1;
129 
130  zap(argc, argv);
131  }
132  catch(const exception& error) {
133  FATAL(error.what() << endl);
134  }
135 
136 
137 
138  cout.tie(&cerr);
139 
140 
142 
143  try {
144  load(detectorFile, detector);
145  }
146  catch(const JException& error) {
147  FATAL(error);
148  }
149 
150  const JModuleRouter moduleRouter(detector);
151 
152 
153  typedef vector<JHitL0> JDataL0_t;
154  typedef vector<JHitW0> JDataW0_t;
155  const JBuildL0<JHitL0> buildL0;
156 
157 
158  typedef JRegressor<JLine3Z, JGandalf> JRegressor_t;
159 
161  JRegressor_t::T_ns.setRange(-50.0, +450.0); // ns
162  JRegressor_t::Vmax_npe = 10.0; // npe
163  JRegressor_t::MAXIMUM_ITERATIONS = 1000;
164 
165  JRegressor_t fit(pdfFile, TTS_ns);
166 
167  fit.parameters.resize(5);
168 
169  fit.parameters[0] = JLine3Z::pX();
170  fit.parameters[1] = JLine3Z::pY();
171  fit.parameters[2] = JLine3Z::pT();
172  fit.parameters[3] = JLine3Z::pDX();
173  fit.parameters[4] = JLine3Z::pDY();
174 
175  if (fit.getRmax() < roadWidth_m) {
176  roadWidth_m = fit.getRmax();
177  }
178 
179  TH2D h2("h2", NULL, detector.size(), -0.5, detector.size() - 0.5, 1001, -250.0, +250.0);
180 
181  h2.Sumw2();
182 
183  outputFile.open();
184 
185  outputFile.put(JMeta(argc, argv));
186 
187  while (inputFile.hasNext()) {
188 
189  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
190 
191  multi_pointer_type ps = inputFile.next();
192 
193  JDAQEvent* tev = ps;
194  JEvt* evt = ps;
195  JEvt out;
196 
197  JEvt::iterator __end = evt->end();
198 
199  if (reprocess) {
200  __end = partition(evt->begin(), __end, JHistory::is_not_event(JMUONGANDALF));
201  }
202 
203  if (evt->begin() != __end) {
204 
205  copy(evt->begin(), __end, back_inserter(out));
206 
207  if (numberOfPrefits > 0) {
208  advance(__end = evt->begin(), min(numberOfPrefits, out.size()));
209  }
210 
211  partial_sort(evt->begin(), __end, evt->end(), qualitySorter);
212 
213  __end = partition(evt->begin(), __end, JHistory::is_event(evt->begin()->getHistory()));
214 
215 
216  JDataL0_t dataL0;
217 
218  buildL0(*tev, moduleRouter, true, back_inserter(dataL0));
219 
220 
221  if (dataL0.size() >= fit.parameters.size()) {
222 
223  for (JEvt::const_iterator track = evt->begin(); track != __end; ++track) {
224 
225  const JRotation3D R (getDirection(*track));
226  const JLine1Z tz(getPosition (*track).rotate(R), track->getT());
227  const JModel<JLine1Z> match(tz, roadWidth_m, JRegressor_t::T_ns);
228 
229  // hit selection based on start value
230 
231  JDataW0_t data;
232 
233  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
234 
235  JHitW0 hit(*i, R_Hz);
236 
237  hit.rotate(R);
238 
239  if (match(hit)) {
240  data.push_back(hit);
241  }
242  }
243 
244  // select first hit
245 
246  sort(data.begin(), data.end(), compare);
247 
248  JDataW0_t::iterator __end = unique(data.begin(), data.end(), equal_to<JDAQPMTIdentifier>());
249 
250 
251  const int NDF = distance(data.begin(), __end) - fit.parameters.size();
252 
253  if (NDF >= 0) {
254 
255  // set fit parameters
256 
257  if (track->getE() > 0.1)
258  fit.E_GeV = track->getE();
259  else
260  fit.E_GeV = E_GeV;
261 
262  const double chi2 = fit(JLine3Z(tz), data.begin(), __end);
263 
264  JTrack3D tb(fit.value);
265 
266  tb.rotate_back(R);
267 
268  out.push_back(getFit(JHistory(track->getHistory()).add(JMUONGANDALF), tb, getQuality(chi2), NDF));
269 
270  // set additional values
271 
272  out.rbegin()->setW(JGANDALF_BETA0_RAD, sqrt(fit.error.getDX() * fit.error.getDX() +
273  fit.error.getDY() * fit.error.getDY()));
274  out.rbegin()->setW(JGANDALF_BETA1_RAD, sqrt(fit.error.getDX() * fit.error.getDY()));
275  out.rbegin()->setW(JGANDALF_CHI2, chi2);
276  out.rbegin()->setW(JGANDALF_NUMBER_OF_HITS, distance(data.begin(), __end));
277  out.rbegin()->setW(JGANDALF_LAMBDA, fit.lambda);
278  out.rbegin()->setW(JGANDALF_NUMBER_OF_ITERATIONS, fit.numberOfIterations);
279 
280 
281  if (calibrate) {
282 
283  set<int> buffer;
284 
285  for (JDataW0_t::const_iterator hit = data.begin(); hit != __end; ++hit) {
286  buffer.insert(hit->getModuleID());
287  }
288 
289  const JLine3Z ta = fit.value;
290 
291  for (set<int>::const_iterator id = buffer.begin(); id != buffer.end(); ++id) {
292 
293  JDataW0_t::iterator q = partition(data.begin(), __end, make_predicate(&JHitW0::getModuleID, *id, JComparison::ne()));
294 
295  if (distance(data.begin(), q) - fit.parameters.size() > 0) {
296 
297  fit(ta, data.begin(), q);
298 
299  sort(q, __end, compare); // select first hit in module
300 
301  h2.Fill(moduleRouter.getIndex(*id), q->getT() - fit.value.getT(q->getPosition()));
302  }
303  }
304  }
305  }
306  }
307  }
308 
309  // apply default sorter
310 
311  sort(out.begin(), out.end(), qualitySorter);
312  }
313 
314  outputFile.put(out);
315  }
316  STATUS(endl);
317 
318  if (calibrate) {
319  outputFile.put(h2);
320  }
321 
323 
324  io >> outputFile;
325 
326  outputFile.close();
327 }
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
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
number of iterations from JGandalf.cc
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:102
#define STATUS(A)
Definition: JMessage.hh:61
Detector data structure.
Definition: JDetector.hh:77
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
Auxiliary class to test history.
Definition: JHistory.hh:149
Data structure for fit of straight line in positive z-direction.
Definition: JLine3Z.hh:35
Container for historical events.
Definition: JHistory.hh:95
chi2 from JGandalf.cc
string outputFile
angular resolution [rad] from JGandalf.cc
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
Type list.
Definition: JTypeList.hh:22
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
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
Auxiliary class to test history.
Definition: JHistory.hh:101
Auxiliary class for a hit with background rate value.
Definition: JHitW0.hh:21
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
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
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.
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
JDirection3D getDirection(const Vec &v)
Get direction.
Object reading from a list of files.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:72
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
JPosition3D getPosition(const Vec &v)
Get position.