Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMuonPDF.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <algorithm>
6 
9 
10 #include "JDAQ/JDAQEventIO.hh"
12 
13 #include "JDetector/JDetector.hh"
16 
17 #include "JLang/JPredicate.hh"
18 
19 #include "JTrigger/JHitL0.hh"
20 #include "JTrigger/JBuildL0.hh"
21 
22 #include "JSupport/JSupport.hh"
25 
26 #include "JPhysics/JPDF_t.hh"
27 #include "JPhysics/JNPE_t.hh"
28 
29 #include "JFit/JLine1Z.hh"
30 #include "JFit/JModel.hh"
31 
33 #include "JReconstruction/JEvt.hh"
36 
37 #include "Jeep/JPrint.hh"
38 #include "Jeep/JParser.hh"
39 #include "Jeep/JMessage.hh"
40 
41 
42 namespace {
43 
44  /**
45  * Auxiliary data structure for sorting of hits.
46  */
47  static const struct {
48  /**
49  * Compare hits by PMT identifier and time.
50  *
51  * \param first first hit
52  * \param second second hit
53  * \return true if first before second; else false
54  */
55  template<class T>
56  bool operator()(const T& first, const T& second) const
57  {
58  using namespace std;
59  using namespace JPP;
60  using namespace KM3NETDAQ;
61 
62  if (equal_to<JDAQPMTIdentifier>()(first, second))
63  return less<JHit>()(first, second);
64  else
65  return less<JDAQPMTIdentifier>()(first, second);
66  }
67  } compare;
68 }
69 
70 
71 /**
72  * \file
73  *
74  * Program to evaluate hit probabilities.
75  *
76  * \author mdejong
77  */
78 int main(int argc, char **argv)
79 {
80  using namespace std;
81  using namespace JPP;
82  using namespace KM3NETDAQ;
83 
84  typedef JParallelFileScanner< JTypeList<JDAQEvent, JEvt> > JParallelFileScanner_t;
85  typedef JParallelFileScanner_t::multi_pointer_type multi_pointer_type;
86 
87  JParallelFileScanner_t inputFile;
88  JLimit_t& numberOfEvents = inputFile.getLimit();
89  string detectorFile;
90  string pdfFile;
92  int debug;
93 
94  try {
95 
96  parameters.numberOfPrefits = 1;
97 
98  JParser<> zap("Program to perform detector calibration using reconstructed muon trajectories.");
99 
100  zap['f'] = make_field(inputFile);
101  zap['a'] = make_field(detectorFile);
102  zap['n'] = make_field(numberOfEvents) = JLimit::max();
103  zap['P'] = make_field(pdfFile);
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  //setDAQLongprint(debug >= JEEP::debug_t);
114 
115  cout.tie(&cerr);
116 
117  if (parameters.numberOfPrefits != 1) {
118  WARNING("Number of prefits " << parameters.numberOfPrefits << " != " << 1 << endl);
119  }
120 
122 
123  try {
124  load(detectorFile, detector);
125  }
126  catch(const JException& error) {
127  FATAL(error);
128  }
129 
130  const JModuleRouter router(detector);
131 
132  JSummaryFileRouter summary(inputFile, parameters.R_Hz);
133 
134  const JMuonPDF_t pdf(pdfFile, parameters.TTS_ns);
135  const JMuonNPE_t npe(pdfFile);
136 
137  const JTimeRange T_ns(parameters.TMin_ns, parameters.TMax_ns);
138 
139  typedef vector<JHitL0> JDataL0_t;
140  typedef vector<JHitW0> JDataW0_t;
141 
142  const JBuildL0<JHitL0> buildL0;
143 
144  while (inputFile.hasNext()) {
145 
146  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
147 
148  multi_pointer_type ps = inputFile.next();
149 
150  JDAQEvent* tev = ps;
151  JEvt* in = ps;
152 
153  DEBUG("event: " << *tev << endl);
154 
155  summary.update(*tev);
156 
157  in->select(parameters.numberOfPrefits, qualitySorter);
158 
159  JDataL0_t dataL0;
160 
161  buildL0(*tev, router, true, back_inserter(dataL0));
162 
163  for (JEvt::const_iterator track = in->begin(); track != in->end(); ++track) {
164 
165  DEBUG("track: " << *track << endl);
166 
167  JRotation3D R (getDirection(*track));
168  JLine1Z tz(getPosition (*track).rotate(R), track->getT());
169  JRange<double> Z_m;
170 
171  if (track->hasW(JSTART_LENGTH_METRES)) {
172  Z_m = JRange<double>(0.0, track->getW(JSTART_LENGTH_METRES)) + JRange<double>(parameters.ZMin_m, parameters.ZMax_m);
173  }
174 
175  const JModel<JLine1Z> match(tz, parameters.roadWidth_m, T_ns, Z_m);
176 
177  // hit selection based on start value
178 
179  JDataW0_t data;
180 
181  for (JDataL0_t::const_iterator i = dataL0.begin(); i != dataL0.end(); ++i) {
182 
183  double rate_Hz = summary.getRate(*i);
184 
185  if (rate_Hz <= 0.0) {
186  rate_Hz = summary.getRate();
187  }
188 
189  JHitW0 hit(*i, rate_Hz);
190 
191  hit.rotate(R);
192 
193  if (match(hit)) {
194  data.push_back(hit);
195  }
196  }
197 
198  // select first hit
199 
200  sort(data.begin(), data.end(), compare);
201 
202  JDataW0_t::iterator __end = unique(data.begin(), data.end(), equal_to<JDAQPMTIdentifier>());
203 
204 
205  double E_GeV = parameters.E_GeV;
206  /*
207  if (track->getE() > 0.1) {
208  E_GeV = track->getE();
209  }
210  */
211 
212  DEBUG("line1z: "
213  << FIXED(12,3) << tz.getX() << ' '
214  << FIXED(12,3) << tz.getY() << ' '
215  << FIXED(12,3) << tz.getZ() << ' '
216  << FIXED(12,3) << tz.getT() << ' '
217  << FIXED(12,1) << E_GeV << ' '
218  << FIXED( 8,3) << track->getQ() << endl);
219 
220  // move track to bary-center of hits
221  /*
222  int N = 0;
223  double z = 0.0;
224 
225  for (JDataW0_t::const_iterator hit = data.begin(); hit != __end; ++hit) {
226  N += 1;
227  z += hit->getZ();
228  }
229 
230  if (N != 0) {
231  tz.setZ(z/N, getSpeedOfLight());
232  }
233  */
234 
235  double Q = 0.0;
236 
237  for (JDataW0_t::const_iterator hit = data.begin(); hit != __end; ++hit) {
238 
239  const double x = hit->getX() - tz.getX();
240  const double y = hit->getY() - tz.getY();
241  const double z = hit->getZ() - tz.getZ();
242  const double R = sqrt(x*x + y*y);
243 
244  const double t1 = tz.getT() + (z + R * getTanThetaC()) * getInverseSpeedOfLight();
245 
246  JDirection3D u(hit->getDX(), hit->getDY(), hit->getDZ()); // PMT orientation
247 
248  u.rotate(JRotation3Z(-atan2(y,x))); // rotate PMT axis to x-z plane
249 
250  const double theta = u.getTheta();
251  const double phi = fabs(u.getPhi()); // rotational symmetry of Cherenkov cone
252 
253  //const double E = gWater.getE(E_GeV, z); // correct for energy loss
254  const double E = E_GeV;
255  const double dt = T_ns.constrain(hit->getT() - t1);
256 
257  JMuonPDF_t::result_type H1 = pdf.calculate(E, R, theta, phi, dt);
258  JMuonPDF_t::result_type H0(hit->getR() * 1e-9, 0.0, T_ns);
259 
260  H1 += H0; // signal + background
261 
262  const double chi2 = H1.getChi2() - H0.getChi2(); // likelihood ratio
263 
264  DEBUG("hit: "
265  << setw(10) << hit->getModuleID() << ':' << setw( 2) << setfill('0') << hit->getPMTAddress() << setfill(' ') << ' '
266  << FIXED(12,1) << E << ' '
267  << FIXED( 9,1) << R << ' '
268  << FIXED( 6,4) << theta << ' '
269  << FIXED( 6,4) << phi << ' '
270  << FIXED( 8,3) << dt << ' '
271  << FIXED(12,3) << chi2 << endl);
272 
273  Q += getQuality(chi2);
274  }
275 
276  DEBUG("quality: " << FIXED(8,3) << Q << ' ' << distance(data.begin(), __end) << endl);
277 
278 
279  double Y = 0.0;
280 
281  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
282 
283  JPosition3D pos(i->getPosition());
284 
285  pos.transform(R, tz.getPosition());
286 
287  if (pos.getX() <= parameters.roadWidth_m) {
288 
289  JModule module = *i;
290 
291  module.transform(R, tz.getPosition());
292 
293  for (JModule::iterator pmt = module.begin(); pmt != module.end(); ++pmt) {
294 
295  const double R = sqrt(pmt->getX()*pmt->getX() + pmt->getY()*pmt->getY());
296  const double theta = pmt->getTheta();
297  const double phi = fabs(pmt->getTheta());
298  const double y = npe.calculate(1.0, R, theta, phi); // MIP
299 
300  DEBUG("PMT: "
301  << setw(10) << module.getID() << ':' << setw( 2) << setfill('0') << distance(module.begin(),pmt) << setfill(' ') << ' '
302  << FIXED(9,1) << R << ' '
303  << FIXED(6,4) << theta << ' '
304  << FIXED(6,4) << phi << ' '
305  << SCIENTIFIC(7,2) << y << endl);
306 
307  Y += y;
308  }
309  }
310  }
311 
312  DEBUG("npe: " << FIXED(7,1) << Y << endl);
313  }
314  }
315  STATUS(endl);
316 }
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
#define WARNING(A)
Definition: JMessage.hh:65
double calculate(const double E, const double R, const double theta, const double phi) const
Get PDF.
Definition: JNPE_t.hh:102
int main(int argc, char *argv[])
Definition: Main.cc:15
ROOT TTree parameter settings of various packages.
Data structure for direction in three dimensions.
Definition: JDirection3D.hh:33
Data structure for a composite optical module.
Definition: JModule.hh:57
double getQuality(const double chi2, const int NDF)
Get quality of fit.
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
do rm f tmp H1
#define STATUS(A)
Definition: JMessage.hh:63
void update(const JDAQHeader &header)
Update router.
Detector data structure.
Definition: JDetector.hh:80
Router for direct addressing of module data in detector data structure.
Rotation matrix.
Definition: JRotation3D.hh:111
double getRate() const
Get default rate.
Auxiliary data structure for muon PDF.
*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:66
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:445
Data structure for detector geometry and calibration.
void transform(const JRotation3D &R, const JVector3D &pos)
Transform position.
Definition: JPosition3D.hh:331
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Basic data structure for L0 hit.
Rotation around Z-axis.
Definition: JRotation3D.hh:85
result_type calculate(const double E, const double R, const double theta, const double phi, const double t1) const
Get PDF.
Definition: JPDF_t.hh:120
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
I/O formatting auxiliaries.
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition: JAxis3D.hh:225
Detector file.
Definition: JHead.hh:196
JDirection3D getDirection(const Vec &dir)
Get direction.
JFunction1D_t::result_type result_type
Definition: JPDF_t.hh:32
Acoustic event fit.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
do set_variable OUTPUT_DIRECTORY $WORKDIR T
int getID() const
Get identifier.
Definition: JObjectID.hh:50
Parallel scanning of objects from a single file or multiple files according a format that follows fro...
JPosition3D getPosition(const Vec &pos)
Get position.
T constrain(argument_type x) const
Constrain value to range.
Definition: JRange.hh:338
File router for fast addressing of summary data.
int debug
debug level
Definition: JSirene.cc:63
Auxiliary data structure for muon PDF.
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Direct access to module in detector data structure.
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.
Utility class to parse command line options.
void transform(const JRotation3D &R, const JVector3D &pos)
Transformation of geometry (see method JGEOMETRY3D::JPosition3D::transform(const JRotation3D&amp;, const JVector3D&amp;)).
Definition: JModule.hh:382
const double getInverseSpeedOfLight()
Get inverse speed of light.
Data structure for fit of straight line paralel to z-axis.
Definition: JLine1Z.hh:27
double getTanThetaC()
Get average tangent of Cherenkov angle of water corresponding to group velocity.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
do set_variable DETECTOR_TXT $WORKDIR detector
double u[N+1]
Definition: JPolint.hh:739
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
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:483
bool qualitySorter(const JRECONSTRUCTION::JFit &first, const JRECONSTRUCTION::JFit &second)
Comparison of fit results.
then usage $script[input file[working directory[option]]] nWhere option can be E
Definition: JMuonPostfit.sh:37
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62