SELFASFile.cc
Go to the documentation of this file.
1 
8 #include <io/SELFASFile.h>
9 #include <io/SELFASIOException.h>
10 
11 #include <evt/Event.h>
12 #include <evt/ShowerSimData.h>
13 #include <evt/SimRadioPulse.h>
14 #include <evt/RadioSimulation.h>
15 #include <evt/DefaultShowerGeometryProducer.h>
16 
17 #include <utl/VParticleProperties.h>
18 #include <utl/ErrorLogger.h>
19 #include <utl/AugerUnits.h>
20 #include <utl/PhysicalConstants.h>
21 #include <utl/Vector.h>
22 #include <utl/UTMPoint.h>
23 #include <utl/TimeStamp.h>
24 #include <utl/Particle.h>
25 #include <det/Detector.h>
26 
27 #include <cmath>
28 #include <iostream>
29 #include <iomanip>
30 #include <sstream>
31 #include <string>
32 
33 #include <boost/tokenizer.hpp>
34 
35 using namespace evt;
36 using namespace utl;
37 using std::vector;
38 using std::string;
39 using std::ostringstream;
40 using std::cout;
41 using std::endl;
42 
43 
44 namespace io {
45 
46  SELFASFile::SELFASFile(const string& fileName, const Mode mode, utl::Branch* const /*b*/) :
47  VEventFile(fileName, mode)
48  {
49  Open(fileName, mode);
50  }
51 
52 
53  void
54  SELFASFile::Open(const string& fileName, const Mode mode, utl::Branch* const /*b*/)
55  {
56  finput_file.open(fileName.c_str());
57  if (!finput_file.is_open()) {
58  ostringstream msg;
59  msg << "Cannot open file " << FileName << " - ERROR";
60  ERROR(msg);
61  }
62 
63  if (mode != eRead) {
64  ostringstream msg;
65  msg << "Cannot open file " << fileName << " - SELFAS files are read-only";
66  ERROR(msg);
67  throw io::SELFASIOException(msg.str());
68  }
69  }
70 
71 
72  // String splitter
73  vector<string>
74  SELFASFile::StringSplitter(const string& line, const string& sep)
75  {
76  typedef boost::tokenizer<boost::char_separator<char>> my_tok;
77  boost::char_separator<char> separ(sep.c_str());
78  my_tok tok(line, separ);
79  vector<string> zetokens;
80  for (boost::tokenizer<boost::char_separator<char> >::const_iterator titer = tok.begin(); titer != tok.end(); ++titer)
81  zetokens.push_back(*titer);
82  return zetokens;
83  }
84 
85 
86  //conversion from string to double
87  template<class T>
88  bool
89  SELFASFile::from_string(T& t, const std::string& s, std::ios_base&(*f)(std::ios_base&))
90  {
91  std::istringstream iss(s);
92  return !(iss >> f >> t).fail();
93  }
94 
95 
96  Status
98  {
99  INFO("READ SELFASFile");
100 
101  if (!events_remaining--)
102  return eEOF;
103 
104  if (!finput_file) {
105  const string msg = "Cannot read from a closed file.";
106  ERROR(msg);
107  throw io::SELFASIOException(msg);
108  }
109 
110  fantennas_number = 0;
111  fprimary_energy = 0;
112  vector<double> xinflexion;
113 
114  while (fantennas_number == 0) {
115 
116  // if (fantennas_number != 0) break;
117  string theLine;
118  getline(finput_file, theLine);
119  string LineSep = " ";
120  vector<string> SplitedLine = StringSplitter(theLine, LineSep.c_str());
121 
122  if (SplitedLine[1] == "shower") {
123  fparticle_id = SplitedLine[3];
124  } else if (SplitedLine[1] == "shower_id") {
125  fparticle_id = SplitedLine[2];
126  } else if (SplitedLine[1] == "B0") { // magnetic field
127  from_string<double>(fB0, SplitedLine[2], std::dec);
128  } else if(SplitedLine[1] == "Bfield") {
129  int strsize = SplitedLine[3].size();
130  std::string SplitedLine2 = SplitedLine[3].substr(1,strsize-2);
131  string LineSep = ",";
132  vector<string> SplitedLine3 = StringSplitter(SplitedLine2, LineSep.c_str());
133  from_string<double>(fBx, SplitedLine3[0], std::dec);
134  from_string<double>(fBy, SplitedLine3[1], std::dec);
135  from_string<double>(fBz, SplitedLine3[2], std::dec);
136  } else if (SplitedLine[1] == "Bfield_Vec::") {
137  int strsize = SplitedLine[2].size();
138  std::string SplitedLine2 = SplitedLine[2].substr(1,strsize-2);
139  string LineSep = ",";
140  vector<string> SplitedLine3 = StringSplitter(SplitedLine2, LineSep.c_str());
141  from_string<double>(fBx, SplitedLine3[0], std::dec);
142  from_string<double>(fBy, SplitedLine3[1], std::dec);
143  from_string<double>(fBz, SplitedLine3[2], std::dec);
144  } else if (SplitedLine[1] == "site" && SplitedLine[3] == "(m)") {
145  from_string<double>(fSiteGround_m, SplitedLine[4], std::dec);
146  fSiteGround_m = fSiteGround_m * meter;
147  } else if (SplitedLine[1] == "site_ground_(m)") {
148  from_string<double>(fSiteGround_m, SplitedLine[2], std::dec);
149  fSiteGround_m = fSiteGround_m * meter;
150  } else if(SplitedLine[1] == "site" && SplitedLine[3] == "(gcm2)") {
151  from_string<double>(fSiteGround_gcm2, SplitedLine[4], std::dec);
152  fSiteGround_m = fSiteGround_m * g * centimeter * centimeter;
153  } else if (SplitedLine[1] == "site_ground_(gcm2)") {
154  from_string<double>(fSiteGround_gcm2, SplitedLine[2], std::dec);
155  fSiteGround_m = fSiteGround_m * g * centimeter * centimeter;
156  } else if (SplitedLine[1] == "theta") {
157  from_string<double>(fzenith_angle, SplitedLine[2], std::dec);
158  fzenith_angle = fzenith_angle * deg;
159  } else if (SplitedLine[1] == "phi") {
160  from_string<double>(fazimuth_angle, SplitedLine[2], std::dec);
161  fazimuth_angle = fazimuth_angle * deg;
162  } else if (SplitedLine[1] == "xc") {
163  from_string<double>(fx_core_position, SplitedLine[2], std::dec);
164  fx_core_position = fx_core_position * meter;
165  } else if (SplitedLine[1] == "yc") {
166  from_string<double>(fy_core_position, SplitedLine[2], std::dec);
167  fy_core_position = fy_core_position * meter;
168  } else if (SplitedLine[1] == "zc") {
169  from_string<double>(fz_core_position, SplitedLine[2], std::dec);
170  fz_core_position = fz_core_position * meter;
171  } else if (SplitedLine[1] == "primary") {
172  if (SplitedLine[2] == "1")
173  fprimary_particle = "eProton";
174  } else if (SplitedLine[1] == "Ep") {
175  from_string<double>(fprimary_energy, SplitedLine[3], std::dec);
176  fprimary_energy = fprimary_energy * exa * electronvolt;
177  } else if (SplitedLine[1] == "Ep_(EeV)") {
178  from_string<double>(fprimary_energy, SplitedLine[2], std::dec);
179  fprimary_energy = fprimary_energy * exa * electronvolt;
180  cout << "primary_energy " << fprimary_energy << endl;
181  } else if (SplitedLine[1] == "x1") {
182  from_string<double>(ffirst_interaction_lenght, SplitedLine[2], std::dec);
183  ffirst_interaction_lenght = ffirst_interaction_lenght * meter;
184  } else if (SplitedLine[1] == "xmax") {
185  from_string<double>(fxmax, SplitedLine[2], std::dec);
186  } else if (SplitedLine[1] == "xinflexion") {
187  from_string<double>(fxinflexion, SplitedLine[2], std::dec);
188  xinflexion.push_back(fxinflexion);
189  } else if (SplitedLine[1] == "timestep") {
190  from_string<double>(ftime_step,SplitedLine[2],std::dec);
191  ftime_step = ftime_step * ns;
192  } else if (SplitedLine[1] == "npart") {
193  from_string<double>(fsimulated_particles,SplitedLine[2],std::dec);
194  } else if (SplitedLine[1] == "has_charge_excess") {
195  from_string<double>(charge_excess,SplitedLine[2], std::dec);
196  if (charge_excess == 1) {
197  fhas_charge_excess = true;
198  cout << "charge excess on" << endl;
199  } else
200  cout << "charge excess off " << endl ;
201  } else if (SplitedLine[1] == "has_multiple_scattering") {
202  from_string<double>(multiple_scattering, SplitedLine[2], std::dec);
203  if (multiple_scattering == 1) {
205  cout << "multiple scattering on" << endl;
206  } else
207  cout << "multiple scattering off " << endl ;
208  } else if (SplitedLine[1] == "has_dE_dx") {
209  from_string<double>(de_dx, SplitedLine[2], std::dec);
210  if (de_dx == 1) {
211  fhas_dE_dx = true;
212  cout << "de_dx on" << endl;
213  } else
214  cout << "de_dx off " << endl ;
215  } else if (SplitedLine[1] == "has_Cerenkov") {
216  from_string<double>(Cerenkov, SplitedLine[2], std::dec);
217  if (Cerenkov == 1) {
218  fhas_Cerenkov = true;
219  cout << "Cerenkov on" << endl;
220  } else
221  cout <<"Cerenkov off" << endl ;
222  } else if (SplitedLine[1] == "Nantennas") {
223  from_string<int>(fantennas_number, SplitedLine[2], std::dec);
224  cout << "number of antennas = " << fantennas_number << endl;
225  } else {
226  cout<< "unknown parameter" <<endl;
227  }
228  }
229 
230  //antennas positions
231  double antenna_position[fantennas_number][3];
232  unsigned int gpssecs = 1010534400; //will be added to the output
233  unsigned int gpsnanosecs = 200;//will be added to the output
234  int eventnumber ;
235  from_string<int>(eventnumber, fparticle_id, std::dec);
236  int runnumber = 1;
237 
238  if (event.HasSimShower()) {
239  ERROR("Event not cleared - has SimShower. Cannot read SELFASFile.");
240  return eFail;
241  }
242 
243  //Read the simulated radio data
244  #warning RU: check geometry
245  event.MakeSimShower(DefaultShowerGeometryProducer());
246  ShowerSimData& shower = event.GetSimShower();
247  shower.MakeRadioSimulation();
248  RadioSimulation& radiosim = shower.GetRadioSimulation();
249  radiosim.SetEventNumber(eventnumber);
250  radiosim.SetRunNumber(runnumber);
251  TimeStamp ts(gpssecs,gpsnanosecs);
252  radiosim.SetEventTime(ts);
253 
254  // get reference coordinate system of detector (usually PampaAmarilla)
255  const utl::CoordinateSystemPtr referenceCS = det::Detector::GetInstance().GetReferenceCoordinateSystem();
256  // This is really an ugly patch to be able to read in SELFAS file, this piece of code should be done by the sim preparator
257  Point pampaOrig(fx_core_position, fy_core_position, 0, referenceCS);
258  UTMPoint utmpampaOrig(pampaOrig, ReferenceEllipsoid::GetWGS84());
259  float pampaheight = utmpampaOrig.GetHeight();
260  //cout << "pampaheight = " << pampaheight/m;
261  radiosim.SetCoreCoordinates(fx_core_position, fy_core_position, fz_core_position - pampaheight); //Here the relative coordinate of the core should be read-in and set
262  //------------------------------------------------------------------------------------------------------------------------
263  cout << "warning - it may be necessary to increase the </MaximumAllowedDistance>\n"
264  "---------------------------------------------------------------------" << endl;
266  if (fprimary_energy == 0)
267  fprimary_energy = 0.1 * exa*electronvolt;//will be added to the output
268  shower.SetEnergy(fprimary_energy);
271  event.GetHeader().SetId(fparticle_id);
272 
273  //SimRadioPulse pulses[fantennas_number]; // Not allowed in standard C++ for non-POD
274  vector<SimRadioPulse> pulses(fantennas_number); // Simply pre-allocate
275 
276  for (int i = 0; i < fantennas_number; ++i) {
277  string theLine(" ");
278  string LineSep = " ";
279  getline(finput_file, theLine);
280 
281  vector<string> SplitedLine = StringSplitter(theLine, LineSep.c_str());
282 
283  int strsize = SplitedLine[6].size();
284  std::string SplitedLine2 = SplitedLine[6].substr(1, strsize - 2);
285 
286  vector<string> SplitedLine3 = StringSplitter(SplitedLine2, string(",").c_str());
287  from_string<double>(antenna_position[i][0], SplitedLine3[0], std::dec);
288  from_string<double>(antenna_position[i][1], SplitedLine3[1], std::dec);
289  from_string<double>(antenna_position[i][2], SplitedLine3[2], std::dec);
290 
291  int strsize2 = SplitedLine[8].size();
292  std::string SplitedLine4 = SplitedLine[8].substr(1, strsize2 - 2);
293  vector<string> SplitedLine5 = StringSplitter(SplitedLine4, ",");
294 
295  from_string<double>(fx_core_relative_position, SplitedLine5[0], std::dec);
296  from_string<double>(fy_core_relative_position, SplitedLine5[1], std::dec);
297  from_string<double>(fz_core_relative_position, SplitedLine5[2], std::dec);
298 
299  fx_core_relative_position = fx_core_relative_position * meter;
300  fy_core_relative_position = fy_core_relative_position * meter;
301  fz_core_relative_position = fz_core_relative_position * meter;
302  pulses[i].SetRelativeCoordinates(fx_core_relative_position, fy_core_relative_position, fz_core_relative_position);
303  }
304 
305  vector<int> CountStartTime(fantennas_number, 0);
306  vector<int> CountEmptyBins(fantennas_number, 0);
307 
308  double e_field[3] = { 0, 0, 0 };
309  for (int i = 0; i < fantennas_number; ++i) {
310  //CountStartTime[i] = 0;
311  //CountEmptyBins[i] = 0;
312  pulses[i].SetBinning(ftime_step);
313  }
314  while (!finput_file.eof()) {
315  double binTime = 0;
316  string theLine(" ");
317  string LineSep = " ";
318  getline(finput_file, theLine);
319 
320  if (finput_file.eof())
321  break;
322  vector<string> SplitedLine = StringSplitter(theLine, LineSep.c_str());
323  from_string<double>(binTime, SplitedLine[0], std::dec);
324  binTime = binTime * ns;
325 
326  for (int i = 0; i < fantennas_number; ++i) {
327  from_string<double>(fxefield, SplitedLine[1+3*i], std::dec);
328  from_string<double>(fyefield, SplitedLine[2+3*i], std::dec);
329  from_string<double>(fzefield, SplitedLine[3+3*i], std::dec);
330 
331  e_field[0] = fxefield * volt / meter;
332  e_field[1] = fyefield * volt / meter;
333  e_field[2] = fzefield * volt / meter;
334 
335  //trace stored only for non-empty bins
336  if (e_field[0] && e_field[1] && e_field[2])
337  ++CountEmptyBins[i];
338  if (CountEmptyBins[i] >= 1) {
339  TraceV3D& pulsestimeseries = pulses[i].GetEfieldTimeSeries();
340  pulsestimeseries.PushBack(Vector3D(e_field));
341  }
342  // start time stored for the 1st non-empty bin
343  if (CountEmptyBins[i] >= 1 && CountStartTime[i] == 0) {
344  pulses[i].SetStartTime(binTime);
345  ++CountStartTime[i];
346  }
347 
349  }
350  }
351  for (int i = 0; i < fantennas_number; ++i) {
352  //cout << "start time " << i << " = " << pulses[i].GetStartTime() << endl;
353  radiosim.AddSimRadioPulse(pulses[i]);
354  }
355  return eSuccess;
356  }
357 
358 
359  void
361  {
362  file_open = false;
363  finput_file.close();
364  }
365 
366 
367  void
369  {
370  const string msg = "Cannot write to SELFASFile - read-only file";
371  ERROR(msg);
372  throw io::SELFASIOException(msg);
373  }
374 
375 
376  Status
377  SELFASFile::FindEvent(const unsigned int eventId)
378  {
379  return GotoPosition(eventId);
380  }
381 
382 
383  Status
384  SELFASFile::GotoPosition(const unsigned int position)
385  {
386  return (position > (unsigned int)GetNEvents()) ? eFail : eSuccess;
387  }
388 
389 }
int GetNEvents() override
Definition: SELFASFile.h:48
std::ifstream finput_file
Definition: SELFASFile.h:65
constexpr double centimeter
Definition: AugerUnits.h:89
double ffirst_interaction_lenght
Definition: SELFASFile.h:81
double fxinflexion
Definition: SELFASFile.h:105
double fzenith_angle
Definition: SELFASFile.h:76
double fSiteGround_gcm2
Definition: SELFASFile.h:103
Point object.
Definition: Point.h:32
double fxefield
Definition: SELFASFile.h:95
double ftime_step
Definition: SELFASFile.h:82
double fz_core_relative_position
Definition: SELFASFile.h:94
void Close() override
Definition: SELFASFile.cc:360
void MakeRadioSimulation()
Make the RadioSimulation.
Class to hold and convert a point in geodetic coordinates.
Definition: UTMPoint.h:40
double fazimuth_angle
Definition: SELFASFile.h:77
bool HasSimShower() const
Data structure for a radio simulation (including several SimRadioPulses)
Mode
Available open modes.
Definition: IoCodes.h:16
const double meter
Definition: GalacticUnits.h:29
void SetGroundParticleCoordinateSystemAzimuth(const double azimuth)
Set the azimuth angle of the shower. Angle in x-y plane wrt. to the x axis (0 is from east)...
double de_dx
Definition: SELFASFile.h:87
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
utl::SVector< 3, double > Vector3D
Definition: Trace-fwd.h:32
Status FindEvent(const unsigned int eventId) override
seek Event id set cursor there
Definition: SELFASFile.cc:377
void SetRunNumber(const int runnum)
Set the run number of the RadioSimulation.
double fyefield
Definition: SELFASFile.h:96
std::string fprimary_particle
Definition: SELFASFile.h:72
void fail(const std::string &name)
Definition: testlib.cc:83
double charge_excess
Definition: SELFASFile.h:86
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
void SetEventNumber(const int eventnum)
Set the event number of the RadioSimulation.
constexpr double deg
Definition: AugerUnits.h:140
std::vector< std::string > StringSplitter(const std::string &line, const std::string &sep)
Definition: SELFASFile.cc:74
Interface class to access Shower Simulated parameters.
Definition: ShowerSimData.h:49
void Open(const std::string &fileName, const Mode mode, utl::Branch *const b=nullptr) override
Definition: SELFASFile.cc:54
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
Class representing a document branch.
Definition: Branch.h:107
Status
Return code for seek operation.
Definition: IoCodes.h:24
constexpr double s
Definition: AugerUnits.h:163
constexpr double exa
Definition: AugerUnits.h:76
double fzefield
Definition: SELFASFile.h:97
double GetHeight() const
Get the height.
Definition: UTMPoint.h:212
void SetEventTime(const utl::TimeStamp &t)
Set the event time of the RadioSimulation.
const double ns
int fantennas_number
Definition: SELFASFile.h:91
RadioSimulation & GetRadioSimulation()
Get the radio simulation data.
unsigned int events_remaining
Definition: SELFASFile.h:67
double fsimulated_particles
Definition: SELFASFile.h:83
void SetCoreCoordinates(const double x, const double y, const double z)
Set the core position coordinates of the RadioSimulation.
Base for exceptions in the SELFAS reader.
double Cerenkov
Definition: SELFASFile.h:89
bool from_string(T &t, const std::string &s, std::ios_base &(*f)(std::ios_base &))
Definition: SELFASFile.cc:89
void SetEnergy(const double theEnergy)
Set the energy of the shower primary particle.
Definition: ShowerSimData.h:91
constexpr double g
Definition: AugerUnits.h:200
double fSiteGround_m
Definition: SELFASFile.h:102
std::string fparticle_id
Definition: SELFASFile.h:74
void AddSimRadioPulse(const SimRadioPulse &rp)
Add a radio pulse to the RadioSimulation.
bool fhas_multiple_scattering
Definition: SELFASFile.h:85
Status Read(evt::Event &theEvent) override
read current event advance cursor by 1
Definition: SELFASFile.cc:97
double fprimary_energy
Definition: SELFASFile.h:73
Status GotoPosition(const unsigned int pos) override
goto by position in the file
Definition: SELFASFile.cc:384
std::string FileName
Definition: SELFASFile.h:59
void SetPrimaryParticle(const int type)
Set the type of the shower primary particle.
double fx_core_position
Definition: SELFASFile.h:78
void SetGroundParticleCoordinateSystemZenith(const double zenith)
Set the zenith angle of the shower. Room angle between z-axis and direction from where the shower is ...
Template class for a FADC data or calibrated data container. Use the typedefs (TraceD, TraceI, etc.) defined in Trace-fwd.h.
Definition: Trace-fwd.h:19
double multiple_scattering
Definition: SELFASFile.h:88
bool fhas_charge_excess
Definition: SELFASFile.h:84
double fy_core_relative_position
Definition: SELFASFile.h:93
bool fhas_Cerenkov
Definition: SELFASFile.h:106
unsigned int fCurrentPosition
Definition: SELFASFile.h:58
double fy_core_position
Definition: SELFASFile.h:79
void PushBack(const T &value)
Insert a single value at the end.
Definition: Trace.h:119
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
double fx_core_relative_position
Definition: SELFASFile.h:92
double fz_core_position
Definition: SELFASFile.h:80
const double volt
Definition: GalacticUnits.h:38
constexpr double electronvolt
Definition: AugerUnits.h:172
void Write(const evt::Event &theEvent) override
Definition: SELFASFile.cc:368

, generated on Tue Sep 26 2023.