Jpp
JHistHDF.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <fstream>
4 #include <iomanip>
5 
9 
10 #include "JAAnet/JHead.hh"
11 #include "JAAnet/JAAnetToolkit.hh"
12 
15 #include "JSupport/JSupport.hh"
16 
17 #include "JPhysics/JPDFToolkit.hh"
19 #include "JPhysics/JDispersion.hh"
20 #include "JPhysics/Antares.hh"
21 #include "JPhysics/KM3NeT.hh"
22 #include "JIO/JFileStreamIO.hh"
23 
26 #include "JGeometry3D/JAxis3D.hh"
28 
29 #include "JTools/JConstants.hh"
30 #include "JTools/JHistogram1D_t.hh"
33 #include "JTools/JFunction1D_t.hh"
35 
36 #include "JDetector/JDetector.hh"
38 #include "JDetector/JPMTRouter.hh"
39 
40 #include "Jeep/JParser.hh"
41 #include "Jeep/JMessage.hh"
42 
43 
44 namespace {
45 
46  using namespace JPP;
47 
48  /**
49  * Hit classifier class
50  */
51  template<class JMultiHistogram_t>
52  class JHitClassifier
53  {
54  public:
55  /**
56  * Constructor
57  * \param head MC file header
58  * \param detector detector
59  */
60  JHitClassifier(const JHead& head, JDetector& detector) :
61  KM3Sim(is_km3sim(head))
62  {
63  if (KM3Sim) {
64  detector -= JCenter3D(detector.begin(), detector.end()); // For KM3Sim, you get it from the detector file
65  }
66  else {
67  detector -= get<JPosition3D>(head); // For KM3, the MC origin is stored in the header
68  }
69  }
70 
71 
72  /**
73  * Function to classify hits
74  * \param track muon track
75  * \param hit hit
76  * \return 1 for muon; 2 for shower; -1 for anything else
77  */
78  int operator()(const Trk& track, const Hit& hit) const
79  {
80  if (KM3Sim)
81  return classify_km3sim(track, hit);
82  else
83  return classify_km3(track, hit);
84  }
85 
86 
87  /**
88  * Auxillary function to calculate the shower energy.
89  * For KM3Sim, (ORCA neutrinos < 100GeV) this will be the hadronic shower energy.
90  * For KM3 and JSirene, (ARCA or atmospheric muons) this will be the bremsstrahlung showers along the track.
91  *
92  * \param event Monte Carlo event
93  * \param track muon track
94  * \param axis PMT axis
95  *
96  * \return energy [GeV]
97  */
98  inline double getEnergy(const Evt& event, const Trk& track, const JAxis3D& axis) const
99  {
100  if (KM3Sim)
101  return get_hadronic_cascade(event).E;
102  else
103  return gWater(track.E, axis.getZ());
104  }
105 
106 
107  private:
108  const bool KM3Sim; //!< true if KM3Sim was used (ORCA neutrinos < 100 GeV); else false
109 
110 
111  /**
112  * Function for classifying hits generated with KM3
113  *
114  * \param track muon track
115  * \param hit hit
116  * \return 1 for muon; 2 for shower; -1 for anything else
117  */
118  int classify_km3(const Trk& track, const Hit& hit) const
119  {
120  if (track.id == hit.origin) {
121  switch(hit.type) {
123  return 1;
125  return 1;
127  return 2;
129  return 2;
130  default:
131  return -1;
132  }
133  }
134  return -1;
135  }
136 
137 
138  /**
139  * Function for classifying hits generated with KM3Sim
140  *
141  * \param track muon track
142  * \param hit hit
143  *
144  * \return 1 for muon; 2 for shower
145  */
146  int classify_km3sim(const Trk& track, const Hit& hit) const
147  {
148  if (from_muon(hit))
149  return 1;
150  else
151  return 2;
152  }
153  };
154 }
155 
156 
157 /**
158  * \file
159  *
160  * Program to histogram event-by-event data of muon light for making PDFs.
161  * \author mdejong
162  */
163 int main(int argc, char **argv)
164 {
165  using namespace std;
166  using namespace JPP;
167 
168  JMultipleFileScanner<Evt> inputFile;
169  JLimit_t& numberOfEvents = inputFile.getLimit();
170  string outputFile;
171  string detectorFile;
172  int debug;
173 
174  try {
175 
176  JParser<> zap("Program to histogram event-by-event data of muon light for making PDFs.");
177 
178  zap['f'] = make_field(inputFile);
179  zap['o'] = make_field(outputFile);
180  zap['n'] = make_field(numberOfEvents) = JLimit::max();
181  zap['a'] = make_field(detectorFile);
182  zap['d'] = make_field(debug) = 1;
183 
184  zap(argc, argv);
185  }
186  catch(const exception &error) {
187  FATAL(error.what() << endl);
188  }
189 
190 
191  using namespace KM3NET;
192 
193 
195 
196  try {
197  load(detectorFile, detector);
198  }
199  catch(const JException& error) {
200  FATAL(error);
201  }
202 
203  JHead head(JMultipleFileScanner<Head>(inputFile).getHeader()); // Monte Carlo header
204 
205  const JPMTRouter pmtRouter(detector);
206 
207  const double P_atm = NAMESPACE::getAmbientPressure();
208  const double wmax = getMaximalWavelength();
209 
210  const JDispersion dispersion(P_atm);
211 
212  typedef JHistogram1D_t::abscissa_type abscissa_type;
213 
217  JHistogramGridMap_t>::maplist> JMultiHistogram_t;
218 
219  typedef JPDFTransformer<3, abscissa_type> JFunction3DTransformer_t;
220 
221  JMultiHistogram_t h0; // occurrence rate of PMT (used for normalisation)
222  JMultiHistogram_t h1; // light from muon
223  JMultiHistogram_t h2; // light from showers
224 
225  const JHitClassifier<JMultiHistogram_t> classifier(head, detector);
226 
227  const double cmin = dispersion.getKmin (wmax);
228 
229  h1.transformer.reset(new JFunction3DTransformer_t(35.0, 2, cmin, 0.0, NAMESPACE::getAngularAcceptance, 0.10));
230  h2.transformer.reset(new JFunction3DTransformer_t(35.0, 2, cmin, 0.0, NAMESPACE::getAngularAcceptance, 0.10));
231 
232 
233  // configure bins
234 
235  const double R[] = { 0.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 70.0, 80.0, 90.0, 100.0, 170.0, 250.0 };
236 
237  for (int i = 0; i != sizeof(R)/sizeof(R[0]); ++i) {
238 
239  const double R_m = R[i];
240 
241  const double grid = 10.0 + 0.0 * R_m/100.0; // [deg]
242  const double alpha = 2.0 * sqrt(1.0 - cos(grid * PI / 180.0)); // azimuth angle unit step size
243 
244  const int number_of_theta_points = max(2, (int) (180.0/(1.4 * grid)));
245  const double theta_step = PI / (number_of_theta_points + 1);
246 
247  for (double theta = -0.5*theta_step; theta < PI + theta_step; theta += theta_step) {
248 
249  const int number_of_phi_points = max(2, (int) (PI * sin(theta) / alpha));
250  const double phi_step = PI / (number_of_phi_points + 1);
251 
252  for (double phi = -0.5*phi_step; phi < PI + phi_step; phi += phi_step) {
253 
254  for (JMultiHistogram_t* buffer[] = { &h0, &h1, &h2, NULL }, **histogram = buffer; *histogram != NULL; ++histogram) {
255  (**histogram)[R_m][theta][phi];
256  }
257  }
258  }
259  }
260 
261  for (JMultiHistogram_t::super_iterator
262  i1 = h1.super_begin(),
263  i2 = h2.super_begin(); i1 != h1.super_end(); ++i1, ++i2) {
264 
265  for (double buffer[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 7.5, 10.0, 15.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 85.0, 100.0, 125.0, 150.0, 200.0, 500.0, -1.0 }, *x = buffer; *x >= 0.0; ++x) {
266  i1.getValue()[*x];
267  i2.getValue()[*x];
268  }
269  }
270 
271 
272  while (inputFile.hasNext()) {
273 
274  NOTICE("event: " << setw(10) << inputFile.getCounter() << '\r'); STATUS(endl);
275 
276  const Evt* event = inputFile.next();
277 
278  for (vector<Trk>::const_iterator track = event->mc_trks.begin(); track != event->mc_trks.end(); ++track) {
279 
280  if (is_muon(*track)) {
281 
282  // track parameters
283 
284  double E = track->E;
285  JPosition3D pos = getPosition (*track);
286  JDirection3D dir = getDirection(*track);
287  double t0 = track->t;
288 
289  const JRotation3D R(dir);
290 
291  pos.rotate(R);
292 
293  JRange<double> Z(0.0, gWater(E)); // range of muon
294 
295  for (vector<Hit>::const_iterator hit = event->mc_hits.begin(); hit != event->mc_hits.end(); ++hit) {
296 
297  try {
298 
299  JAxis3D axis = pmtRouter.getPMT(hit->pmt_id);
300 
301  axis.transform(R, pos);
302 
303  const double E1 = classifier.getEnergy(*event, *track, axis);
304  const double t1 = t0 + (axis.getZ() + axis.getX()*getTanThetaC()) / getSpeedOfLight();
305 
306  const double R_m = axis.getX();
307  const double theta = axis.getTheta();
308  const double phi = fabs(axis.getPhi());
309  const double dt = getTime(*hit) - t1;
310  const double npe = getNPE(*hit);
311 
312  switch (classifier(*track, *hit)) {
313 
314  case 1:
315  h1.fill(R_m, theta, phi, dt, npe); // light from muon
316  break;
317 
318  case 2:
319  h2.fill(R_m, theta, phi, dt, npe/max(1.0, E1)); // light from shower
320  break;
321  }
322  }
323  catch(const exception& error) {
324  FATAL(error.what() << endl);
325  }
326  }
327 
328 
329  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
330 
331  JPosition3D P = module->getPosition();
332 
333  P.rotate(R);
334 
335  P.sub(pos);
336 
337  if (sqrt(P.getX()*P.getX() + P.getY()*P.getY()) <= h0.getXmax()) {
338 
339  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
340 
341  JAxis3D axis = *pmt;
342 
343  axis.transform(R, pos);
344 
345  if (Z(axis.getZ() - axis.getX()/getTanThetaC())) {
346  h0.fill(axis.getX(), axis.getTheta(), fabs(axis.getPhi()), 0.0, 1.0);
347  }
348  }
349  }
350  }
351  }
352  }
353  }
354  NOTICE(endl);
355 
356 
357  // output
358 
359  JFileStreamWriter out(outputFile.c_str());
360 
361  for (const JMultiHistogram_t* buffer[] = { &h0, &h1, &h2, NULL }, **i = buffer; *i != NULL; ++i) {
362  out.store(**i);
363  }
364 
365  out.close();
366 }
Hit
Definition: Hit.hh:7
JIO::JFileStreamWriter
Binary buffered file output.
Definition: JFileStreamIO.hh:72
Head.hh
Hit::origin
int origin
track id of the track that created this hit
Definition: Hit.hh:29
Evt::mc_trks
std::vector< Trk > mc_trks
MC: list of MC truth tracks
Definition: Evt.hh:45
JSUPPORT::JLimit
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
JGEOMETRY3D::JCenter3D
Center.
Definition: JGeometry3DToolkit.hh:29
Antares.hh
JTOOLS::getSpeedOfLight
const double getSpeedOfLight()
Number of bytes in a gigabyte.
Definition: JConstants.hh:89
JMessage.hh
JHead.hh
JHistogramMap_t.hh
JPHYSICS::gWater
static const JGeaneWater gWater
Function object for energy loss of muon in sea water.
Definition: JGeane.hh:328
JFileStreamIO.hh
JPosition3D.hh
JDirection3D.hh
JGEOMETRY3D::JVector3D::getZ
double getZ() const
Get z position.
Definition: JVector3D.hh:114
JGEOMETRY3D::JAxis3D
Axis object.
Definition: JAxis3D.hh:38
JDETECTOR::load
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
Definition: JDetectorToolkit.hh:476
Hit::pmt_id
int pmt_id
global PMT identifier as found in evt files
Definition: Hit.hh:18
JGEOMETRY3D::JAxis3D::transform
void transform(const JAxis3D &axis)
Transform axis to reference frame of given axis.
Definition: JAxis3D.hh:370
Trk::E
double E
Energy (either MC truth or reconstructed)
Definition: Trk.hh:18
JPDFToolkit.hh
std::vector
Definition: JSTDTypes.hh:12
Trk
The Trk class represents a Monte Carlo (MC) particle as well as a reconstructed track/shower.
Definition: Trk.hh:12
JTOOLS::JRange< double >
JGEOMETRY3D::JDirection3D
Data structure for direction in three dimensions.
Definition: JDirection3D.hh:32
Evt
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:19
JAANET::getNPE
double getNPE(const Hit &hit)
Get true charge of hit.
Definition: JAAnetToolkit.hh:104
Evt.hh
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
Trk::t
double t
track time (when the particle is at pos )
Definition: Trk.hh:17
JTOOLS::JMAPLIST
Auxiliary class for recursive map list generation.
Definition: JMapList.hh:108
JIO::JWriter::store
JWriter & store(const JSerialisable &object)
Write object.
Definition: JSerialisable.hh:170
NOTICE
#define NOTICE(A)
Definition: JMessage.hh:64
JDETECTOR::JPMTRouter
Router for direct addressing of PMT data in detector data structure.
Definition: JPMTRouter.hh:33
JAANET::from_muon
bool from_muon(const Hit &hit)
Test whether given hit was produced by a muon.
Definition: JAAnetToolkit.hh:541
JPHYSICS::JPDFTransformer
Template definition of transformer of the Probability Density Functions of the time response of a PMT...
Definition: JPDFTransformer.hh:33
JPDFTransformer.hh
JAANET::is_muon
bool is_muon(const Trk &track)
Test whether given track is a (anti-)muon.
Definition: JAAnetToolkit.hh:367
JGEOMETRY3D::JPosition3D::rotate
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
JAANET::is_km3sim
bool is_km3sim(const JHead &header)
Check for detector simulation.
Definition: JHeadToolkit.hh:260
JAANET::JHead
Monte Carlo run header.
Definition: JHead.hh:839
JAANET::HIT_TYPE_MUON_SCATTERED
Scattered light from muon.
Definition: JAAnetToolkit.hh:69
main
int main(int argc, char **argv)
Definition: JHistHDF.cc:163
JAxis3D.hh
JSupport.hh
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTransformableMultiHistogram.hh
JFunction1D_t.hh
Hit.hh
JSUPPORT::getHeader
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
Definition: JMonteCarloFileSupportkit.hh:458
JSUPPORT::JMultipleFileScanner::getCounter
counter_type getCounter() const
Get counter.
Definition: JMultipleFileScanner.hh:323
JAANET::HIT_TYPE_MUON_DIRECT
Direct light from muon.
Definition: JAAnetToolkit.hh:68
JGEOMETRY3D::JVersor3D::getTheta
double getTheta() const
Get theta angle.
Definition: JVersor3D.hh:125
JTOOLS::JHistogram1D::abscissa_type
collection_type::abscissa_type abscissa_type
Definition: JHistogram1D.hh:121
debug
int debug
debug level
Definition: JSirene.cc:59
JSUPPORT::JMultipleFileScanner::next
virtual const pointer_type & next()
Get next element.
Definition: JMultipleFileScanner.hh:398
ANTARES::getAmbientPressure
double getAmbientPressure()
Get ambient pressure.
Definition: Antares.hh:30
JConstants.hh
JPHYSICS::JDispersion
Implementation of dispersion for water in deep sea.
Definition: JDispersion.hh:26
JGEOMETRY3D::JPosition3D
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
JFunctionalMap_t.hh
JSUPPORT::JMultipleFileScanner< Head >
Template specialisation of JMultipleFileScanner for Monte Carlo header.
Definition: JMonteCarloFileSupportkit.hh:324
JAAnetToolkit.hh
JAANET::getTime
double getTime(const Hit &hit)
Get true time of hit.
Definition: JAAnetToolkit.hh:87
JTOOLS::JHistogramMap_t
Type definition of a JHistogramMap based on JMap implementation.
Definition: JHistogramMap_t.hh:25
JPHYSICS::JDispersionInterface::getKmin
double getKmin(const double lambda) const
Get smallest index of refraction for Bremsstrahlung light (i.e.
Definition: JDispersionInterface.hh:91
JMultipleFileScanner.hh
STATUS
#define STATUS(A)
Definition: JMessage.hh:63
JSUPPORT::JMultipleFileScanner::hasNext
virtual bool hasNext()
Check availability of next element.
Definition: JMultipleFileScanner.hh:350
JParser.hh
JDetectorToolkit.hh
JTOOLS::JHistogram1D_t
JHistogram1D< JElement2D< double, double >, JCollection > JHistogram1D_t
Type definition of a 1 dimensional histogram based on a JCollection.
Definition: JHistogram1D_t.hh:22
JDispersion.hh
JAANET::get_hadronic_cascade
Trk get_hadronic_cascade(const Evt &evt)
Auxiliary function to get average direction and total energy of a hadronic cascade.
Definition: JAAnetToolkit.hh:627
JAANET::getDirection
JDirection3D getDirection(const Vec &v)
Get direction.
Definition: JAAnetToolkit.hh:221
JTOOLS::getTanThetaC
double getTanThetaC()
Get average tangent of Cherenkov angle of water.
Definition: JConstants.hh:133
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JSUPPORT::JMultipleFileScanner
General purpose class for object reading from a list of file names.
Definition: JMultipleFileScanner.hh:167
JDETECTOR::JDetector
Detector data structure.
Definition: JDetector.hh:80
JGEOMETRY3D::JPosition3D::getPosition
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:129
JAANET::getPosition
JPosition3D getPosition(const Vec &v)
Get position.
Definition: JAAnetToolkit.hh:197
Trk::id
int id
track identifier
Definition: Trk.hh:14
JAANET::detector
Detector file.
Definition: JHead.hh:130
JPMTRouter.hh
std
Definition: jaanetDictionary.h:36
JGEOMETRY3D::JVector3D::getY
double getY() const
Get y position.
Definition: JVector3D.hh:103
JTOOLS::PI
static const double PI
Constants.
Definition: JConstants.hh:20
JTOOLS::JTransformableMultiHistogram
Transformable multidimensional histogram.
Definition: JTransformableMultiHistogram.hh:37
JAANET::HIT_TYPE_BREMSSTRAHLUNG_DIRECT
Direct light from Bremsstrahlung.
Definition: JAAnetToolkit.hh:72
JMonteCarloFileSupportkit.hh
JTOOLS::JHistogramGridMap_t
Type definition of a JHistogramMap based on JGridMap implementation.
Definition: JHistogramMap_t.hh:34
JDetector.hh
JHistogram1D_t.hh
JGEOMETRY3D::JVector3D::getX
double getX() const
Get x position.
Definition: JVector3D.hh:93
getAngularAcceptance
double getAngularAcceptance(const double x)
Angular acceptence of PMT.
Definition: JDrawLED.cc:84
JAANET::HIT_TYPE_BREMSSTRAHLUNG_SCATTERED
Scattered light from Bremsstrahlung.
Definition: JAAnetToolkit.hh:73
Evt::mc_hits
std::vector< Hit > mc_hits
MC: list of MC truth hits.
Definition: Evt.hh:44
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37
JGEOMETRY3D::JVector3D::sub
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:157
KM3NeT.hh
JPHYSICS::getMaximalWavelength
double getMaximalWavelength()
Get maximal wavelength for PDF evaluations.
Definition: JPDFToolkit.hh:49
JLANG::JException
General exception.
Definition: JException.hh:23
Hit::type
int type
particle type or parametrisation used for hit (mc only)
Definition: Hit.hh:28
JDETECTOR::JPMTRouter::getPMT
const JPMT & getPMT(const JPMTAddress &address) const
Get PMT.
Definition: JPMTRouter.hh:90
JGeometry3DToolkit.hh
JGEOMETRY3D::JRotation3D
Rotation matrix.
Definition: JRotation3D.hh:111
KM3NET
Name space for KM3NeT.
Definition: Jpp.hh:16
JGEOMETRY3D::JVersor3D::getPhi
double getPhi() const
Get phi angle.
Definition: JVersor3D.hh:141