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

Program to histogram event-by-event and track-by-track for making PDFs of lights of individual particles. More...

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdexcept>
#include "evt/Head.hh"
#include "evt/Evt.hh"
#include "evt/Hit.hh"
#include "evt/Trk.hh"
#include "JAAnet/JHead.hh"
#include "JAAnet/JHeadToolkit.hh"
#include "JAAnet/JAAnetToolkit.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JMonteCarloFileSupportkit.hh"
#include "JSupport/JSupport.hh"
#include "JPhysics/JPDFToolkit.hh"
#include "JPhysics/JDispersion.hh"
#include "JPhysics/Antares.hh"
#include "JPhysics/KM3NeT.hh"
#include "JIO/JFileStreamIO.hh"
#include "JGeometry3D/JGeometry3DToolkit.hh"
#include "JGeometry3D/JPosition3D.hh"
#include "JGeometry3D/JDirection3D.hh"
#include "JGeometry3D/JAxis3D.hh"
#include "JTools/JConstants.hh"
#include "JTools/JHistogram1D_t.hh"
#include "JTools/JHistogramMap_t.hh"
#include "JTools/JTransformableMultiHistogram.hh"
#include "JTools/JFunction1D_t.hh"
#include "JTools/JFunctionalMap_t.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JPMTRouter.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "JPhysics/JPDFTransformer.hh"
#include "JPhysics/JPDF.hh"
#include "JAAnet/JParticleTypes.hh"

Go to the source code of this file.

Functions

vector< Int_t > getHitRemapping (const vector< Trk > *tracklist)
 
int main (int argc, char **argv)
 

Detailed Description

Program to histogram event-by-event and track-by-track for making PDFs of lights of individual particles.

Author
jseneca

Definition in file JHistHDE.cc.

Function Documentation

vector<Int_t> getHitRemapping ( const vector< Trk > *  tracklist)
inline

Definition at line 59 of file JHistHDE.cc.

59  {
60  vector<Int_t> hit_remapping;
61  for (vector<Trk>::const_iterator i = tracklist->begin(); i != tracklist->end(); ++i) {
62  //Skip pathological neutrino that is not the primary incoming neutrino
63  if(i->type == TRACK_TYPE_NUMU && i != tracklist->begin()){continue;}
64  hit_remapping.push_back(i->id);
65  }
66  return hit_remapping;
67 }
int main ( int  argc,
char **  argv 
)

Definition at line 69 of file JHistHDE.cc.

70 {
71  using namespace std;
72  using namespace JPP;
73 
74  JMultipleFileScanner<Evt> inputFile;
75  //JLimit_t& numberOfEvents = inputFile.getLimit();
76  string outputFile = "J%s.HDE"; //'s' for single (particle), or simulation
77  string detectorFile;
78  vector<int> particles;
79  bool fix_bug;
80  int debug;
81 
82  try {
83 
84  JParser<> zap("Program to histogram event-by-event data of shower light for making PDFs.");
85 
86  zap['f'] = make_field(inputFile);
87  zap['o'] = make_field(outputFile);
88  //zap['n'] = make_field(numberOfEvents) = JLimit::max();
89  zap['a'] = make_field(detectorFile);
90  zap['p'] = make_field(particles);
91  zap['b'] = make_field(fix_bug);
92  zap['d'] = make_field(debug) = 1;
93 
94  if (zap.read(argc, argv) != 0)
95  return 1;
96  }
97  catch(const exception &error) {
98  FATAL(error.what() << endl);
99  }
100 
101  JDetector detector;
102 
103  if(outputFile.find('%') != string::npos){
104  sort(particles.begin(), particles.end());
105  string prefix_t = "";
106  for(vector<int>::iterator ptype = particles.begin(); ptype != particles.end(); ptype++){
107  prefix_t += to_string((long long int)*ptype) + "_";
108  }
109  prefix_t.erase(prefix_t.size() - 1);
110  string::size_type pos_1 = outputFile.find('%');
111  outputFile.replace(pos_1, 1, prefix_t);
112  }
113 
114  try {
115  load(detectorFile, detector);
116  }
117  catch(const JException& error) {
118  FATAL(error);
119  }
120 
121  const JPosition3D center = get<JPosition3D>(getHeader(inputFile));
122 
123  NOTICE("Apply detector offset " << center << endl);
124 
125  detector -= center;
126 
127  const JPMTRouter pmtRouter(detector);
128 
129  const double P_atm = NAMESPACE::getAmbientPressure();
130  const double wmin = getMinimalWavelength();
131  const double wmax = getMaximalWavelength();
132 
133  const JDispersion dispersion(P_atm);
134 
135  const double ng[] = { dispersion.getIndexOfRefractionGroup(wmax),
136  dispersion.getIndexOfRefractionGroup(wmin) };
137 
138  typedef JHistogram1D_t::abscissa_type abscissa_type;
139 
140  typedef JTransformableMultiHistogram<JHistogram1D_t,
141  JMAPLIST<JHistogramMap_t,
142  JHistogramMap_t,
143  JHistogramMap_t,
144  JHistogramGridMap_t,
145  JHistogramGridMap_t>::maplist> JMultiHistogram_t;
146 
147  typedef JPDFTransformer<5, abscissa_type> JFunction5DTransformer_t;
148 
149  JMultiHistogram_t h0; // occurrence rate of PMT (used for normalisation)
150  JMultiHistogram_t h1; // light from track cascade
151 
152  h1.transformer.reset(new JFunction5DTransformer_t(21.5, 2, ng[0], 0.0, JGeant(JGeanx(1.00, -2.2)), 1e-2, NAMESPACE::getAngularAcceptance, 0.05));
153 
154  set<double> C; // cosine emission angle
155 
156  JQuadrature qeant(-1.0, 1.0, 30, JGeanx(1.00, -2.2));
157 
158  for (JQuadrature::const_iterator i = qeant.begin(); i != qeant.end(); ++i) {
159  C.insert(i->getX());
160  }
161 
162  C.insert(-1.01);
163  C.insert(-1.00);
164  C.insert( 1.00);
165  C.insert( 1.01);
166 
167  const double E_b[] = {0.01, 0.2, 0.4, 0.6, 0.8, 1.0, 1.5, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 25.0, 30.0, 40.0, 55.0, 70.0, 85.0, 100.0};
168 
169  const double R[] = {0.0, 1.0, 2.5, 5.0, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0, 25.0, 30.0, 35.0, 40.0, 50.0, 65.0, 80.0};
170  const double Dmax_m = R[sizeof(R)/sizeof(R[0]) - 1]; //Last element of R
171 
172  for (int iE = 0; iE != sizeof(E_b)/sizeof(E_b[0]); ++iE) {
173 
174  const double E_m = E_b[iE];
175 
176  for (int i = 0; i != sizeof(R)/sizeof(R[0]); ++i) {
177 
178  const double R_m = R[i];
179 
180  for (set<double>::const_iterator c = C.begin(); c != C.end(); ++c) {
181 
182  const double cd = *c;
183 
184  const double grid = 10.0 + 0.0 * R_m/100.0; // [deg]
185  const double alpha = 2.0 * sqrt(1.0 - cos(grid * PI / 180.0)); // azimuth angle unit step size
186 
187  const int number_of_theta_points = (max(2, (int) (180.0/(1.4 * grid))));
188  const double theta_step = PI / (number_of_theta_points + 1);
189 
190  for (double theta = -0.5*theta_step; theta < PI + theta_step; theta += theta_step) {
191  const int number_of_phi_points = (max(2, (int) (PI * sin(theta) / alpha)));
192  const double phi_step = PI / (number_of_phi_points + 1);
193 
194  for (double phi = -0.5*phi_step; phi < PI + phi_step; phi += phi_step) {
195 
196  for (JMultiHistogram_t* buffer[] = { &h0, &h1, NULL }, **histogram = buffer; *histogram != NULL; ++histogram) {
197  (**histogram)[E_m][R_m][cd][theta][phi];
198  }
199  }
200  }
201  }
202  }
203  }
204 
205  double buffer[] = {-15.0,-10.0,-7.5,-5,-4,-3,-2,-1.5,-1.0,-0.5,-0.1, 0.0,0.1, 0.5, 1.0, 1.5, 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 };
206  for (JMultiHistogram_t::super_iterator
207  i1 = h1.super_begin(); i1 != h1.super_end(); ++i1) {
208  for (int j = 0; j != sizeof(buffer)/sizeof(buffer[0]); ++j) {
209  i1.getValue()[buffer[j]];
210  }
211  }//Buffer defines the binning of the inner histograms (dt)
212 
213 
214  long long int h0_fillcount = 0;
215  long long int h1_fillcount = 0;
216 
217  while (inputFile.hasNext()) {
218 
219  //NOTICE("event: " << setw(10) << inputFile.getCounter() << '\r'); STATUS(endl);
220  //I suspect the printing above slows down the program considerably
221 
222  const Evt* event = inputFile.next();
223  const vector<Trk>* mc_tracks = &(event->mc_trks);
224 
225  vector<int> hit_remap;
226 
227  //Fixes 2016 atmospheric neutrino simulation hit-making neutrino bug
228  //Remove after fix.
229  if(fix_bug){
230  hit_remap = getHitRemapping(mc_tracks);
231  //*************************************************
232  } else {
233  for (vector<Trk>::const_iterator i = mc_tracks->begin(); i != mc_tracks->end(); ++i) {
234  hit_remap.push_back(i->id);
235  }
236  }
237 
238  double t0 = (*mc_tracks)[1].t;
239 
240  for (vector<Hit>::const_iterator hit = event->mc_hits.begin(); hit != event->mc_hits.end(); ++hit) {
241 
242  try {
243 
244  if(hit->origin >= (int)(*mc_tracks).size()){
245  std::out_of_range err("Hit origin not in tracklist. Avoided segfault");
246  throw err;
247  }
248 
249  Int_t corr_origin = hit_remap[hit->origin];
250  Trk curr_track = (*mc_tracks)[corr_origin]; //Get track from corrected origin
251  JDirection3D dir = getDirection(curr_track); const JRotation3D R(dir);
252  JPosition3D pos = getPosition(curr_track); pos.rotate(R);
253  JAxis3D axis = pmtRouter.getPMT(hit->pmt_id);
254  axis.transform(R, pos);
255 
256  const double E = curr_track.E;
257  const double t1 = t0 + axis.getLength() * getInverseSpeedOfLight() * getIndexOfRefraction();
258  const double D_m = axis.getLength();
259  const double cd = axis.getZ()/D_m;
260  const double theta = axis.getTheta();
261  const double phi = fabs(axis.getPhi());
262  const double dt = getTime(*hit) - t1;
263  const double npe = getNPE(*hit);
264 
265  if(D_m < Dmax_m){
266  h1.fill(E, D_m, cd, theta, phi, dt, npe);
267  h1_fillcount += 1;
268  }
269  }
270  catch(const exception& error) {
271  std::cout << "Fatal error for event: " << inputFile.getCounter() << std::endl;
272  FATAL(error.what() << endl);
273  }
274  }
275 
276  // ignore primary neutrino -> ^
277  for (vector<Trk>::const_iterator tr = event->mc_trks.begin() + 1; tr != event->mc_trks.end(); ++tr) {
278 
279  if(find(particles.begin(), particles.end(), tr->type) == particles.end()){
280  continue;
281  }
282 
283  //cout << "Particle accepted: " << tr->type << endl;
284 
285 
286  JDirection3D dir = getDirection(*tr); const JRotation3D R(dir);
287  JPosition3D pos = getPosition(*tr); pos.rotate(R);
288 
289  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
290 
291  JPosition3D P = module->getPosition();
292 
293  P.rotate(R);
294 
295  P.sub(pos);
296 
297  if (P.getLength() < Dmax_m) {
298 
299  for (JModule::const_iterator pmt = module->begin(); pmt != module->end(); ++pmt) {
300 
301  JAxis3D axis = *pmt;
302  axis.transform(R, pos);
303 
304  h0.fill(tr->E, axis.getLength(), axis.getZ()/axis.getLength(), axis.getTheta(), fabs(axis.getPhi()), 0.0, 1.0);
305  h0_fillcount += 1;
306  }
307  }
308  }
309  }
310 
311  if(h1_fillcount > h0_fillcount){
312  std::cout << "WARNING: recorded hits in normalization histogram that were not recorded in normalization histogram. This should not happen." << std::endl;
313  std::cout << "h1_fillcount: " << h1_fillcount << ", h0_fillcount: " << h0_fillcount << std::endl;
314  }
315  };
316 
317  double integral = 0;
318  for (JMultiHistogram_t::super_iterator i = h0.super_begin(); i != h0.super_end(); ++i) {
319  integral+=i.getValue().getIntegral();
320  }
321  DEBUG("Integral:\t" << integral << endl);
322 
323  // output
324 
325  JFileStreamWriter out(outputFile.c_str());
326 
327  NOTICE("Storing " << outputFile << ", " << flush);
328 
329  for (const JMultiHistogram_t* buffer[] = { &h0, &h1, NULL }, **i = buffer; *i != NULL; ++i) {
330  out.store(**i);
331  }
332 
333  out.close();
334  NOTICE("JHistHDE done. " << endl);
335  return 1;
336 }
Router for direct addressing of PMT data in detector data structure.
Definition: JPMTRouter.hh:33
Utility class to parse command line options.
Definition: JParser.hh:1410
General exception.
Definition: JException.hh:40
Data structure for direction in three dimensions.
Definition: JDirection3D.hh:32
double getIndexOfRefraction()
Get average index of refraction of water.
Definition: JConstants.hh:111
static const double PI
Constants.
Definition: JConstants.hh:20
Structure to store the ToT mean and standard deviation of the hits produced by a nanobeacon in a sour...
double getMinimalWavelength()
Get minimal wavelength for PDF evaluations.
Definition: JPDFToolkit.hh:38
double getTime(const Hit &hit)
Get true time of hit.
double getPhi() const
Get phi angle.
Definition: JVersor3D.hh:139
string outputFile
void transform(const JAxis3D &axis)
Transform axis to reference frame of given axis.
Definition: JAxis3D.hh:354
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
Axis object.
Definition: JAxis3D.hh:37
const double getInverseSpeedOfLight()
Get inverse speed of light.
Definition: JConstants.hh:100
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:156
double getMaximalWavelength()
Get maximal wavelength for PDF evaluations.
Definition: JPDFToolkit.hh:49
Detector file.
Definition: JHead.hh:126
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
double getTheta() const
Get theta angle.
Definition: JVersor3D.hh:123
double getAmbientPressure()
Get ambient pressure.
Definition: Antares.hh:30
#define NOTICE(A)
Definition: JMessage.hh:62
JHistogram1D< JElement2D< double, double >, JCollection > JHistogram1D_t
Type definition of a 1 dimensional histogram based on a JCollection.
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:59
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:129
double getLength() const
Get length.
Definition: JVector3D.hh:242
#define FATAL(A)
Definition: JMessage.hh:65
vector< Int_t > getHitRemapping(const vector< Trk > *tracklist)
Definition: JHistHDE.cc:59
std::string to_string(const T &value)
Convert value to string.
collection_type::abscissa_type abscissa_type
JDirection3D getDirection(const Vec &v)
Get direction.
double getNPE(const Hit &hit)
Get true charge of hit.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
static const double C
Speed of light in vacuum [m/ns].
Definition: JConstants.hh:22
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:185
double getZ() const
Get z position.
Definition: JVector3D.hh:113
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
JPosition3D getPosition(const Vec &v)
Get position.
double getAngularAcceptance(const double x)
Angular acceptence of PMT.
Definition: JDrawLED.cc:84