Jpp
 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 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TProfile.h"
11 #include "TH2D.h"
12 
15 
16 #include "JDAQ/JDAQEventIO.hh"
17 #include "JDAQ/JDAQTimesliceIO.hh"
19 
20 #include "JDetector/JDetector.hh"
23 
24 #include "JLang/JPredicate.hh"
25 
26 #include "JTrigger/JHit.hh"
27 #include "JTrigger/JFrame.hh"
28 #include "JTrigger/JTimeslice.hh"
29 #include "JTrigger/JHitL0.hh"
30 #include "JTrigger/JBuildL0.hh"
32 
33 #include "JSupport/JSupport.hh"
36 #include "JSupport/JMeta.hh"
37 
38 #include "JReconstruction/JEvt.hh"
41 
43 #include "JROOT/JRootToolkit.hh"
44 #include "JGizmo/JManager.hh"
45 
46 #include "Jeep/JParser.hh"
47 #include "Jeep/JMessage.hh"
48 
49 
50 /**
51  * \file
52  *
53  * Program to perform detector calibration using reconstructed muon trajectories.
54  *
55  * It is recommended
56  * to perform the detector calibration after JMuonGandalf.cc and
57  * to accordingly set option <tt>JMuonGandalfParameters_t::numberOfPrefits = 1</tt>,
58  * so that the best track from the previous output is used.
59  * Several histograms will be produced which can subsequently be processed with JHobbit.cc for
60  * the determination of the time calibration of optical modules and JFrodo.cc for the time-slewing correction.
61  * \author mdejong
62  */
63 int main(int argc, char **argv)
64 {
65  using namespace std;
66  using namespace JPP;
67  using namespace KM3NETDAQ;
68 
69  typedef JParallelFileScanner< JTypeList<JDAQEvent, JEvt> > JParallelFileScanner_t;
70  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
71  typedef JAbstractHistogram<double> histogram_type;
72 
73  JParallelFileScanner_t inputFile;
74  string outputFile;
75  JLimit_t& numberOfEvents = inputFile.getLimit();
76  string detectorFile;
77  string pdfFile;
79  histogram_type calibrate;
80  int debug;
81 
82  try {
83 
84  parameters.numberOfPrefits = 1;
85 
86  JParser<> zap("Program to perform detector calibration using reconstructed muon trajectories.");
87 
88  zap['f'] = make_field(inputFile);
89  zap['o'] = make_field(outputFile) = "gandalf.root";
90  zap['a'] = make_field(detectorFile);
91  zap['n'] = make_field(numberOfEvents) = JLimit::max();
92  zap['P'] = make_field(pdfFile);
93  zap['c'] = make_field(calibrate, "Histogram for time calibration per optical module.");
95  zap['d'] = make_field(debug) = 1;
96 
97  zap(argc, argv);
98  }
99  catch(const exception& error) {
100  FATAL(error.what() << endl);
101  }
102 
103  cout.tie(&cerr);
104 
105  if (!calibrate.is_valid()) {
106  FATAL("Invalid calibration data " << calibrate << endl);
107  }
108 
109  if (parameters.numberOfPrefits != 1) {
110  WARNING("Number of prefits " << parameters.numberOfPrefits << " != " << 1 << endl);
111  }
112 
114 
115  try {
116  load(detectorFile, detector);
117  }
118  catch(const JException& error) {
119  FATAL(error);
120  }
121 
122  const JModuleRouter router(detector);
123 
124  JSummaryFileRouter summary(inputFile, parameters.R_Hz);
125 
126  JMuonGandalf fit(parameters, router, summary, pdfFile, debug);
127 
128  typedef vector<JHitL0> JDataL0_t;
129  typedef vector<JHitW0> JDataW0_t;
130 
131  const JBuildL0<JHitL0> buildL0;
132 
133 
134  typedef JManager<string, TProfile> JManager_t;
135 
136  TH2D ha("ha", NULL, 256, -0.5, 255.5,
137  calibrate.getNumberOfBins(), calibrate.getLowerLimit(), calibrate.getUpperLimit());
138 
139  TProfile hb("hb", NULL, 256, -0.5, 255.5);
140 
141  TH2D h2("h2", NULL, detector.size(), -0.5, detector.size() - 0.5,
142  calibrate.getNumberOfBins(), calibrate.getLowerLimit(), calibrate.getUpperLimit());
143 
144  JManager_t g1(new TProfile("%", NULL, detector.size(), -0.5, detector.size() - 0.5, -1.0, +1.0));
145 
146 
147  while (inputFile.hasNext()) {
148 
149  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
150 
151  multi_pointer_type ps = inputFile.next();
152 
153  JDAQEvent* tev = ps;
154  JEvt* in = ps;
155 
156  summary.update(*tev);
157 
158  in->select(parameters.numberOfPrefits, qualitySorter);
159 
160  JDataL0_t dataL0;
161 
162  buildL0(*tev, router, true, back_inserter(dataL0));
163 
164  for (JEvt::const_iterator track = in->begin(); track != in->end(); ++track) {
165 
166  const JRotation3D R (getDirection(*track));
167  const JLine1Z tz(getPosition (*track).rotate(R), track->getT());
168  JRange<double> Z_m;
169 
170  if (track->hasW(JSTART_LENGTH_METRES)) {
171  Z_m = JZRange(0.0, track->getW(JSTART_LENGTH_METRES)) + JRange<double>(parameters.ZMin_m, parameters.ZMax_m);
172  }
173 
174  const JModel<JLine1Z> match(tz, parameters.roadWidth_m, JRange<double>(parameters.TMin_ns, parameters.TMax_ns), Z_m);
175 
176  // hit selection based on start value
177 
178  JDataW0_t data;
179 
180  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
181 
182  double rate_Hz = summary.getRate(*i);
183 
184  if (rate_Hz <= 0.0) {
185  rate_Hz = summary.getRate();
186  }
187 
188  JHitW0 hit(*i, rate_Hz);
189 
190  hit.rotate(R);
191 
192  if (match(hit)) {
193  data.push_back(hit);
194  }
195  }
196 
197  // select first hit
198 
199  sort(data.begin(), data.end(), JMuonGandalf::compare);
200 
201  JDataW0_t::iterator __end = unique(data.begin(), data.end(), equal_to<JDAQPMTIdentifier>());
202 
203 
204  // set fit parameters
205 
206  if (track->getE() > 0.1)
207  fit.JRegressor_t::E_GeV = track->getE();
208  else
209  fit.JRegressor_t::E_GeV = parameters.E_GeV;
210 
211  set<int> buffer;
212 
213  for (JDataW0_t::const_iterator hit = data.begin(); hit != __end; ++hit) {
214  buffer.insert(hit->getModuleID());
215  }
216 
217  const JLine3Z ta(tz);
218 
219  for (set<int>::const_iterator id = buffer.begin(); id != buffer.end(); ++id) {
220 
221  JDataW0_t::iterator q = partition(data.begin(), __end, make_predicate(&JHitW0::getModuleID, *id, JComparison::ne()));
222 
223  if (distance(data.begin(), q) - fit.parameters.size() > 0) {
224 
225  fit(ta, data.begin(), q); // re-fit
226 
227  sort(q, __end, JMuonGandalf::compare); // select first hit in module
228 
229  const int index = router.getIndex(*id);
230  const double t1 = fit.value.getT(q->getPosition());
231  JTrack3D gradient = fit(fit.value, *q).gradient;
232 
233  gradient.rotate_back(R);
234 
235  ha.Fill(q->getToT(), q->getT1() - t1);
236  hb.Fill(q->getToT(), gradient.getT());
237 
238  h2.Fill(index, q->getT() - t1);
239 
240  g1["T"]->Fill(index, gradient.getT());
241  g1["X"]->Fill(index, gradient.getX());
242  g1["Y"]->Fill(index, gradient.getY());
243  g1["Z"]->Fill(index, gradient.getZ());
244  }
245  }
246  }
247  }
248  STATUS(endl);
249 
250  TFile out(outputFile.c_str(), "recreate");
251 
252  putObject(&out, JMeta(argc, argv));
253 
254  out << ha;
255  out << hb;
256  out << h2;
257  out << g1;
258 
259  out.Write();
260  out.Close();
261 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:71
Utility class to parse command line options.
Definition: JParser.hh:1493
General exception.
Definition: JException.hh:23
#define WARNING(A)
Definition: JMessage.hh:65
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
ROOT TTree parameter settings.
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
void update(const JDAQHeader &header)
Update router.
Detector data structure.
Definition: JDetector.hh:80
const int getIndex(const JObjectID &id) const
Get index of module.
Router for direct addressing of module data in detector data structure.
Rotation matrix.
Definition: JRotation3D.hh:111
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
double getRate() const
Get default rate.
*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:63
Data structure for fit of straight line in positive z-direction.
Definition: JLine3Z.hh:35
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
Basic data structure for L0 hit.
Auxiliary class to manage set of compatible ROOT objects (e.g.
Definition: JManager.hh:42
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition: JAxis3D.hh:236
Detector file.
Definition: JHead.hh:130
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
Parallel scanning of objects from a single file or multiple files according a format that follows fro...
Auxiliary class for a hit with background rate value.
Definition: JHitW0.hh:21
ROOT I/O of application specific meta data.
JAxis3D & rotate_back(const JRotation3D &R)
Rotate back axis.
Definition: JAxis3D.hh:251
File router for fast addressing of summary data.
double getY() const
Get y position.
Definition: JVector3D.hh:103
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:61
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
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.
static const int JSTART_LENGTH_METRES
distance between first and last hits in metres from JStart.cc
Data structure for set of track fit results.
Definition: JEvt.hh:294
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:93
JDirection3D getDirection(const Vec &v)
Get direction.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
void select(const JSelector_t &selector)
Select fits.
Definition: JEvt.hh:314
double getZ() const
Get z position.
Definition: JVector3D.hh:114
JTOOLS::JRange< double > JZRange
Definition: JFit/JModel.hh:21
bool qualitySorter(const JRECONSTRUCTION::JFit &first, const JRECONSTRUCTION::JFit &second)
Comparison of fit results.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
JPosition3D getPosition(const Vec &v)
Get position.
bool putObject(TDirectory *dir, const T &object)
Write object to ROOT directory.
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
int main(int argc, char *argv[])
Definition: Main.cpp:15