Jpp  17.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JCalibrateMuon.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 #include <memory>
8 
9 #include "TROOT.h"
10 #include "TFile.h"
11 #include "TProfile.h"
12 #include "TH2D.h"
13 
16 
17 #include "JDAQ/JDAQEventIO.hh"
18 #include "JDAQ/JDAQTimesliceIO.hh"
20 
21 #include "JDetector/JDetector.hh"
24 
25 #include "JDynamics/JDynamics.hh"
26 
27 #include "JLang/JPredicate.hh"
28 
29 #include "JTrigger/JHit.hh"
30 #include "JTrigger/JFrame.hh"
31 #include "JTrigger/JTimeslice.hh"
32 #include "JTrigger/JHitL0.hh"
33 #include "JTrigger/JBuildL0.hh"
35 
36 #include "JSupport/JSupport.hh"
39 #include "JSupport/JMeta.hh"
40 
41 #include "JReconstruction/JEvt.hh"
44 
46 #include "JROOT/JRootToolkit.hh"
47 #include "JROOT/JManager.hh"
48 
49 #include "Jeep/JParser.hh"
50 #include "Jeep/JMessage.hh"
51 
52 
53 /**
54  * \file
55  *
56  * Program to perform detector calibration using reconstructed muon trajectories.
57  *
58  * It is recommended
59  * to perform the detector calibration after JMuonGandalf.cc and
60  * to accordingly set option <tt>JMuonGandalfParameters_t::numberOfPrefits = 1</tt>,
61  * so that the best track from the previous output is used.
62  * Several histograms will be produced which can subsequently be processed with JHobbit.cc for
63  * the determination of the time calibration of optical modules and JFrodo.cc for the time-slewing correction.
64  * \author mdejong
65  */
66 int main(int argc, char **argv)
67 {
68  using namespace std;
69  using namespace JPP;
70  using namespace KM3NETDAQ;
71 
72  typedef JParallelFileScanner< JTypeList<JDAQEvent, JFIT::JEvt> > JParallelFileScanner_t;
73  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
75  JACOUSTICS::JEvt>::typelist calibration_types;
77  typedef JAbstractHistogram<double> histogram_type;
78 
79  JParallelFileScanner_t inputFile;
80  string outputFile;
81  JLimit_t& numberOfEvents = inputFile.getLimit();
82  string detectorFile;
83  JCalibration_t calibrationFile;
84  double Tmax_s;
85  string pdfFile;
87  histogram_type calibrate;
88  int debug;
89 
90  try {
91 
92  parameters.numberOfPrefits = 1;
93 
94  JParser<> zap("Program to perform detector calibration using reconstructed muon trajectories.");
95 
96  zap['f'] = make_field(inputFile);
97  zap['o'] = make_field(outputFile) = "gandalf.root";
98  zap['a'] = make_field(detectorFile);
99  zap['+'] = make_field(calibrationFile) = JPARSER::initialised();
100  zap['T'] = make_field(Tmax_s) = 100.0;
101  zap['n'] = make_field(numberOfEvents) = JLimit::max();
102  zap['P'] = make_field(pdfFile);
103  zap['c'] = make_field(calibrate, "Histogram for time calibration per optical module.");
105  zap['d'] = make_field(debug) = 1;
106 
107  zap(argc, argv);
108  }
109  catch(const exception& error) {
110  FATAL(error.what() << endl);
111  }
112 
113 
114  if (!calibrate.is_valid()) {
115  FATAL("Invalid calibration data " << calibrate << endl);
116  }
117 
118  if (parameters.numberOfPrefits != 1) {
119  WARNING("Number of prefits " << parameters.numberOfPrefits << " != " << 1 << endl);
120  }
121 
123 
124  try {
125  load(detectorFile, detector);
126  }
127  catch(const JException& error) {
128  FATAL(error);
129  }
130 
131 
132  getMechanics.load(detector.getID());
133 
134  unique_ptr<JDynamics> dynamics;
135 
136  try {
137 
138  dynamics.reset(new JDynamics(detector, Tmax_s));
139 
140  dynamics->load(calibrationFile);
141  }
142  catch(const exception& error) {
143  if (!calibrationFile.empty()) {
144  FATAL(error.what());
145  }
146  }
147 
148  const JModuleRouter router(dynamics ? dynamics->getDetector() : detector);
149 
150  JSummaryFileRouter summary(inputFile, parameters.R_Hz);
151 
152  JMuonGandalf fit(parameters, router, summary, pdfFile, debug);
153 
154  typedef vector<JHitL0> JDataL0_t;
155  typedef vector<JHitW0> JDataW0_t;
156 
157  const JBuildL0<JHitL0> buildL0;
158 
159 
160  typedef JManager<string, TProfile> JManager_t;
161 
162  TH2D ha("ha", NULL, 256, -0.5, 255.5,
163  calibrate.getNumberOfBins(), calibrate.getLowerLimit(), calibrate.getUpperLimit());
164 
165  TProfile hb("hb", NULL, 256, -0.5, 255.5);
166 
167  TProfile hr("hr", NULL, 60, 0.0, 150.0);
168 
169  TH2D h2("h2", NULL, detector.size(), -0.5, detector.size() - 0.5,
170  calibrate.getNumberOfBins(), calibrate.getLowerLimit(), calibrate.getUpperLimit());
171 
172  JManager_t g1(new TProfile("%", NULL, detector.size(), -0.5, detector.size() - 0.5, -1.0, +1.0));
173 
174 
175  while (inputFile.hasNext()) {
176 
177  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
178 
179  multi_pointer_type ps = inputFile.next();
180 
181  JDAQEvent* tev = ps;
182  JFIT::JEvt* in = ps;
183 
184  summary.update(*tev);
185 
186  if (dynamics) {
187  dynamics->update(*tev);
188  }
189 
190  in->select(parameters.numberOfPrefits, qualitySorter);
191 
192  JDataL0_t dataL0;
193 
194  buildL0(*tev, router, true, back_inserter(dataL0));
195 
196  for (JFIT::JEvt::const_iterator track = in->begin(); track != in->end(); ++track) {
197 
198  const JRotation3D R (getDirection(*track));
199  const JLine1Z tz(getPosition (*track).rotate(R), track->getT());
200  JRange<double> Z_m;
201 
202  if (track->hasW(JSTART_LENGTH_METRES)) {
203  Z_m = JZRange(0.0, track->getW(JSTART_LENGTH_METRES)) + JRange<double>(parameters.ZMin_m, parameters.ZMax_m);
204  }
205 
206  const JFIT::JModel<JLine1Z> match(tz, parameters.roadWidth_m, JRange<double>(parameters.TMin_ns, parameters.TMax_ns), Z_m);
207 
208  // hit selection based on start value
209 
210  JDataW0_t data;
211 
212  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
213 
214  double rate_Hz = summary.getRate(*i);
215 
216  if (rate_Hz <= 0.0) {
217  rate_Hz = summary.getRate();
218  }
219 
220  JHitW0 hit(*i, rate_Hz);
221 
222  hit.rotate(R);
223 
224  if (match(hit)) {
225  data.push_back(hit);
226  }
227  }
228 
229  // select first hit
230 
231  sort(data.begin(), data.end(), JHitL0::compare);
232 
233  JDataW0_t::iterator __end = unique(data.begin(), data.end(), equal_to<JDAQPMTIdentifier>());
234 
235 
236  // set fit parameters
237 
238  if (track->getE() > 0.1)
239  fit.JRegressor_t::E_GeV = track->getE();
240  else
241  fit.JRegressor_t::E_GeV = parameters.E_GeV;
242 
243  set<int> buffer;
244 
245  for (JDataW0_t::const_iterator hit = data.begin(); hit != __end; ++hit) {
246  buffer.insert(hit->getModuleID());
247  }
248 
249  const JLine3Z ta(tz);
250 
251  for (set<int>::const_iterator id = buffer.begin(); id != buffer.end(); ++id) {
252 
253  JDataW0_t::iterator q = partition(data.begin(), __end, make_predicate(&JHitW0::getModuleID, *id, JComparison::ne()));
254 
255  if (distance(data.begin(), q) - fit.parameters.size() > 0) {
256 
257  fit(ta, data.begin(), q); // re-fit
258 
259  for (JDataW0_t::const_iterator hit = q; hit != __end; ++hit) {
260 
261  const int index = router.getIndex(*id);
262  const double t1 = fit.value.getT(hit->getPosition());
263  JTrack3D gradient = fit(fit.value, *hit).gradient;
264 
265  gradient.rotate_back(R);
266 
267  ha.Fill(hit->getToT(), hit->getT1() - t1);
268  hb.Fill(hit->getToT(), gradient.getT());
269 
270  hr.Fill(fit.value.getDistance(*hit), gradient.getT());
271 
272  h2.Fill(index, hit->getT() - t1);
273 
274  g1["T"]->Fill(index, gradient.getT());
275  g1["X"]->Fill(index, gradient.getX());
276  g1["Y"]->Fill(index, gradient.getY());
277  g1["Z"]->Fill(index, gradient.getZ());
278  }
279  }
280  }
281  }
282  }
283  STATUS(endl);
284 
285  TFile out(outputFile.c_str(), "recreate");
286 
287  putObject(out, JMeta(argc, argv));
288 
289  out << ha;
290  out << hb;
291  out << hr;
292  out << h2;
293  out << g1;
294 
295  out.Write();
296  out.Close();
297 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1517
General exception.
Definition: JException.hh:23
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
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
static JDetectorMechanics getMechanics
Function object to get string mechanics.
Definition: JMechanics.hh:243
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
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:89
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
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Data structure for fit of straight line in positive z-direction.
Definition: JLine3Z.hh:36
Dynamic ROOT object management.
Simple data structure for histogram binning.
Basic data structure for time and time over threshold information of hit.
string outputFile
Data structure for detector geometry and calibration.
Wrapper class to make final fit of muon trajectory.
Definition: JMuonGandalf.hh:72
bool qualitySorter(const JFit &first, const JFit &second)
Comparison of fit results.
Basic data structure for L0 hit.
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition: JAxis3D.hh:225
Detector file.
Definition: JHead.hh:226
JDirection3D getDirection(const Vec &dir)
Get direction.
Acoustic event fit.
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
Parallel scanning of objects from a single file or multiple files according a format that follows fro...
ROOT I/O of application specific meta data.
JPosition3D getPosition(const Vec &pos)
Get position.
static struct JACOUSTICS::@4 compare
Auxiliary data structure to sort transmissions.
JAxis3D & rotate_back(const JRotation3D &R)
Rotate back axis.
Definition: JAxis3D.hh:240
Dynamic detector calibration.
File router for fast addressing of summary data.
double getY() const
Get y position.
Definition: JVector3D.hh:104
void load(const std::string &file_name)
Load mechanical model parameters from file.
Definition: JMechanics.hh:142
bool putObject(TDirectory &dir, const TObject &object)
Write object to ROOT directory.
double getT(const JVector3D &pos) const
Get arrival time of Cherenkov light at given position.
Definition: JTrack3D.hh:126
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Direct access to module in detector data structure.
Dynamic detector calibration.
Definition: JDynamics.hh:63
then JCookie sh JDataQuality D $DETECTOR_ID R
Definition: JDataQuality.sh:41
Auxiliary class for a hit with background rate value.
Definition: JHitW0.hh:21
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.
Data structure for set of track fit results.
General purpose class for object reading from a list of file names.
Utility class to parse command line options.
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
double getX() const
Get x position.
Definition: JVector3D.hh:94
Orientation of module.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
do set_variable DETECTOR_TXT $WORKDIR detector
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:186
then echo WARNING
Definition: JTuneHV.sh:91
void select(const JSelector_t &selector)
Select fits.
double getZ() const
Get z position.
Definition: JVector3D.hh:115
JTOOLS::JRange< double > JZRange
Definition: JFit/JModel.hh:21
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25