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

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

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <algorithm>
#include <limits>
#include "TMath.h"
#include "km3net-dataformat/offline/Head.hh"
#include "km3net-dataformat/offline/Evt.hh"
#include "km3net-dataformat/definitions/fitparameters.hh"
#include "JDAQ/JDAQEventIO.hh"
#include "JDAQ/JDAQTimesliceIO.hh"
#include "JDAQ/JDAQSummarysliceIO.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorSubset.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JTrigger/JHit.hh"
#include "JTrigger/JFrame.hh"
#include "JTrigger/JTimeslice.hh"
#include "JTrigger/JHitL0.hh"
#include "JTrigger/JHitR1.hh"
#include "JTrigger/JBuildL0.hh"
#include "JTrigger/JTriggerParameters.hh"
#include "JSupport/JSingleFileScanner.hh"
#include "JSupport/JParallelFileScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JSupport.hh"
#include "JSupport/JMeta.hh"
#include "JSupport/JSummaryFileRouter.hh"
#include "JFit/JLine1Z.hh"
#include "JFit/JEnergy.hh"
#include "JFit/JEnergyRegressor.hh"
#include "JFit/JFitToolkit.hh"
#include "JFit/JNPEHit.hh"
#include "JFit/JMEstimator.hh"
#include "JFit/JTimeRange.hh"
#include "JFit/JModel.hh"
#include "JReconstruction/JEvt.hh"
#include "JReconstruction/JEvtToolkit.hh"
#include "JReconstruction/JMuonEnergyParameters_t.hh"
#include "JReconstruction/JEnergyCorrection.hh"
#include "JPhysics/JConstants.hh"
#include "JTools/JRange.hh"
#include "JTools/JFunction1D_t.hh"
#include "JTools/JFunctionalMap_t.hh"
#include "JPhysics/JPDFTable.hh"
#include "JPhysics/JPDFToolkit.hh"
#include "JPhysics/JNPETable.hh"
#include "JLang/JComparator.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<JEnergy, JGandalf> fit using I/O of JFIT::JEvt data.

Author
mdejong

Definition in file JMuonEnergy.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 119 of file JMuonEnergy.cc.

120 {
121  using namespace std;
122  using namespace JPP;
123  using namespace KM3NETDAQ;
124 
125  typedef JParallelFileScanner< JTypeList<JDAQEvent, JEvt> > JParallelFileScanner_t;
126  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
128 
129  JParallelFileScanner_t inputFile;
131  JLimit_t& numberOfEvents = inputFile.getLimit();
132  string detectorFile;
133  string pdfFile;
134  JEnergyCorrection correct;
136  int debug;
137 
138  try {
139 
140  JParser<> zap("Program to perform fit of muon energy to data.");
141 
142  zap['f'] = make_field(inputFile);
143  zap['o'] = make_field(outputFile) = "energy.root";
144  zap['a'] = make_field(detectorFile);
145  zap['n'] = make_field(numberOfEvents) = JLimit::max();
146  zap['P'] = make_field(pdfFile);
147  zap['E'] = make_field(correct) = JEnergyCorrection(); // off
149  zap['d'] = make_field(debug) = 1;
150 
151  zap(argc, argv);
152  }
153  catch(const exception& error) {
154  FATAL(error.what() << endl);
155  }
156 
157 
158  cout.tie(&cerr);
159 
160 
162 
163  try {
164  load(detectorFile, detector);
165  }
166  catch(const JException& error) {
167  FATAL(error);
168  }
169 
170  const JModuleRouter router(detector);
171 
172  JSummaryFileRouter summary(inputFile, parameters.R_Hz);
173 
174  typedef vector<JHitL0> JDataL0_t;
175  const JBuildL0<JHitL0> buildL0;
176 
177 
178  typedef JRegressor<JEnergy> JRegressor_t;
179 
181  JRegressor_t::T_ns.setRange(parameters.TMin_ns, parameters.TMax_ns);
182 
183  JRegressor_t fit(pdfFile);
184 
185  fit.estimator.reset(getMEstimator(parameters.mestimator));
186 
187  if (!fit.estimator.is_valid()) {
188  FATAL("Invalid M-Estimator." << endl);
189  }
190 
191  if (fit.getRmax() < parameters.roadWidth_m) {
192  parameters.roadWidth_m = fit.getRmax();
193  }
194 
195 
196  outputFile.open();
197 
198  outputFile.put(JMeta(argc, argv));
199 
200  while (inputFile.hasNext()) {
201 
202  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
203 
204  multi_pointer_type ps = inputFile.next();
205 
206  const JDAQEvent* tev = ps;
207  const JEvt* in = ps;
208 
209  summary.update(*tev);
210 
211  // select start values
212 
213  JEvt cp = *in;
214  JEvt out;
215 
216  if (parameters.reprocess) {
218  }
219 
220  cp.select(parameters.numberOfPrefits, qualitySorter);
221 
222  if (!cp.empty()) {
223  cp.select(JHistory::is_event(cp.begin()->getHistory()));
224  }
225 
226 
227  JDataL0_t dataL0;
228 
229  buildL0(*tev, router, true, back_inserter(dataL0));
230 
231 
232  for (JEvt::const_iterator track = cp.begin(); track != cp.end(); ++track) {
233 
234  const JRotation3D R (getDirection(*track));
235  const JLine1Z tz(getPosition (*track).rotate(R), track->getT());
236  const JModel<JLine1Z> match(tz, parameters.roadWidth_m, JRegressor_t::T_ns);
237 
238 
240 
241  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
242 
243  JHitR1 hit(*i);
244 
245  hit.rotate(R);
246 
247  if (match(hit)) {
248  top.insert(i->getPMTIdentifier());
249  }
250  }
251 
252 
253  double EMin_log = parameters.EMin_log;
254  double EMax_log = parameters.EMax_log;
255  JRange<double> Z_m;
256 
257  if (track->hasW(JSTART_LENGTH_METRES) && track->getW(JSTART_LENGTH_METRES) > 0.0) {
258 
259  Z_m.setLowerLimit(parameters.ZMin_m);
260 
261  EMin_log = log10(track->getW(JSTART_LENGTH_METRES) * gWater.getA());
262  }
263 
264 
265  vector<JNPEHit> data;
266 
267  const JDetectorSubset_t subdetector(detector, getAxis(*track), parameters.roadWidth_m, Z_m);
268 
269  for (JDetector::const_iterator module = subdetector.begin(); module != subdetector.end(); ++module) {
270 
271  if (summary.hasSummaryFrame(module->getID())) {
272 
273  const JDAQSummaryFrame& frame = summary.getSummaryFrame(module->getID());
274 
275  for (size_t i = 0; i != module->size(); ++i) {
276 
277  if (getDAQStatus(frame, *module, i) &&
278  getPMTStatus(frame, *module, i) &&
279  !module->getPMT(i).has(PMT_DISABLE)) {
280 
281  const double rate_Hz = frame.getRate(i);
282  const size_t count = top.count(JDAQPMTIdentifier(module->getID(), i));
283 
284  data.push_back(JNPEHit(fit.getNPE(module->getPMT(i), rate_Hz), count));
285  }
286  }
287  }
288  }
289 
290  const int NDF = distance(data.begin(), data.end()) - 1;
291 
292  if (NDF >= 0) {
293 
294 
295  // 5-point search between given limits
296 
297  const int N = 5;
298 
299  JResult result[N];
300 
301  for (int i = 0; i != N; ++i) {
302  result[i].x = EMin_log + i * (EMax_log - EMin_log) / (N-1);
303  }
304 
305  do {
306 
307  int j = 0;
308 
309  for (int i = 0; i != N; ++i) {
310 
311  if (!result[i]) {
312  result[i].chi2 = fit(result[i].x, data.begin(), data.end());
313  }
314 
315  if (result[i].chi2 < result[j].chi2) {
316  j = i;
317  }
318  }
319 
320  for (int i = 0; i != N; ++i) {
321  DEBUG(' ' << FIXED(5,2) << result[i].x << ' ' << FIXED(9,3) << result[i].chi2);
322  }
323  DEBUG(endl);
324 
325 
326  // squeeze range
327 
328  switch (j) {
329 
330  case 0:
331  case 1:
332  result[4] = result[2];
333  result[2] = result[1];
334  break;
335 
336  case 2:
337  result[0] = result[1];
338  result[4] = result[3];
339  break;
340 
341  case 3:
342  case 4:
343  result[0] = result[2];
344  result[2] = result[3];
345  break;
346  }
347 
348  result[1] = JResult(0.5 * (result[0].x + result[2].x));
349  result[3] = JResult(0.5 * (result[2].x + result[4].x));
350 
351  } while (result[4].x - result[0].x > parameters.resolution);
352 
353 
354  if (result[1].chi2 != result[3].chi2) {
355 
356  result[2].x += 0.25 * (result[3].x - result[1].x) * (result[1].chi2 - result[3].chi2) / (result[1].chi2 + result[3].chi2 - 2*result[2].chi2);
357  result[2].chi2 = fit(result[2].x, data.begin(), data.end());
358  }
359 
360  const double chi2 = result[2].chi2;
361  const double E = result[2].x.getE();
362 
363  // calculate additional variables
364 
365  const double mu_range = gWater(E); // range of a muon with the reconstructed energy
366 
367  double noise_likelihood = 0.0; // log-likelihood of every hit being from K40
368  int number_of_hits = 0; // number of hits selected for JEnergy
369 
370  for (vector<JNPEHit>::const_iterator i = data.begin(); i != data.end(); ++i) {
371  noise_likelihood += log10(i->getP()); // probability of getting the observed multiplicity with K40
372  number_of_hits += i->getN(); // hit multiplicity
373  }
374 
375 
376  out.push_back(JFit(*track).add(JMUONENERGY));
377 
378  // set corrected energy
379 
380  out.rbegin()->setE(correct(E));
381 
382  // set additional values
383 
384  out.rbegin()->setW(track->getW());
385  out.rbegin()->setW(JENERGY_ENERGY, E);
386  out.rbegin()->setW(JENERGY_CHI2, chi2);
387  out.rbegin()->setW(JENERGY_MUON_RANGE_METRES, mu_range);
388  out.rbegin()->setW(JENERGY_NOISE_LIKELIHOOD, noise_likelihood);
389  out.rbegin()->setW(JENERGY_NDF, NDF);
390  out.rbegin()->setW(JENERGY_NUMBER_OF_HITS, number_of_hits);
391  }
392  }
393 
394  // apply default sorter
395 
396  sort(out.begin(), out.end(), qualitySorter);
397 
398  copy(in->begin(), in->end(), back_inserter(out));
399 
400  outputFile.put(out);
401  }
402  STATUS(endl);
403 
405 
406  io >> outputFile;
407 
408  outputFile.close();
409 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
double getRate(const int tdc, const double factor=1.0) const
Get count rate.
static const int JENERGY_ENERGY
uncorrected energy [GeV] from JEnergy.cc
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Regressor function object for JEnergy fit.
Template specialisation of L0 builder for JHitL0 data type.
Definition: JBuildL0.hh:102
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:80
Router for direct addressing of module data in detector data structure.
Rotation matrix.
Definition: JRotation3D.hh:111
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
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: JFit/JModel.hh:34
Auxiliary class to test history.
Definition: JHistory.hh:152
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
static const JGeaneWater gWater
Function object for energy loss of muon in sea water.
Definition: JGeane.hh:323
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:445
JFit & add(const int type)
Add event to history.
string outputFile
static const int JENERGY_CHI2
chi2 from JEnergy.cc
static const int JENERGY_NOISE_LIKELIHOOD
log likelihood of every hit being K40 from JEnergy.cc
static const int JENERGY_NDF
number of degrees of freedom from JEnergy.cc
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:196
JDirection3D getDirection(const Vec &dir)
Get direction.
Auxiliary class for correction of energy determined by JEnergy.cc.
Acoustic event fit.
static const int JENERGY_NUMBER_OF_HITS
number of hits from JEnergy.cc
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
Data storage class for rate measurements of all PMTs in one module.
Auxiliary class to test history.
Definition: JHistory.hh:104
JAxis3D getAxis(const Trk &track)
Get axis.
return result
Definition: JPolint.hh:727
static const int PMT_DISABLE
KM3NeT Data Definitions v2.0.0 https://git.km3net.de/common/km3net-dataformat.
Definition: pmt_status.hh:12
JPosition3D getPosition(const Vec &pos)
Get position.
File router for fast addressing of summary data.
Auxiliary class for simultaneously handling light yields and response of PMT.
Definition: JNPEHit.hh:19
int debug
debug level
Definition: JSirene.cc:63
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
Detector subset without binary search functionality.
Data structure for fit parameters.
#define FATAL(A)
Definition: JMessage.hh:67
bool getPMTStatus(const JStatus &status)
Test status of PMT.
static const int JSTART_LENGTH_METRES
distance between first and last hits in metres from JStart.cc
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
std::vector< int > count
Definition: JAlgorithm.hh:184
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
then cp
Reduced data structure for L1 hit.
Definition: JHitR1.hh:31
Object reading from a list of files.
int j
Definition: JPolint.hh:666
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
JMEstimator * getMEstimator(const int type)
Get M-Estimator.
Definition: JMEstimator.hh:166
do set_variable DETECTOR_TXT $WORKDIR detector
bool getDAQStatus(const JDAQFrameStatus &frame, const JStatus &status)
Test status of DAQ.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:186
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
static const int JENERGY_MUON_RANGE_METRES
range of a muon with the reconstructed energy [m] from JEnergy.cc
void setLowerLimit(argument_type x)
Set lower limit.
Definition: JRange.hh:224
bool qualitySorter(const JRECONSTRUCTION::JFit &first, const JRECONSTRUCTION::JFit &second)
Comparison of fit results.
static const int JMUONENERGY
then usage $script[input file[working directory[option]]] nWhere option can be E
Definition: JMuonPostfit.sh:37
virtual double getA() const override
Get energy loss constant.
Definition: JGeane.hh:229
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62