ZHAireSFile.cc
Go to the documentation of this file.
1 #include <evt/Event.h>
2 #include <evt/ShowerSimData.h>
3 #include <evt/SimRadioPulse.h>
4 #include <evt/RadioSimulation.h>
5 #include <evt/DefaultShowerGeometryProducer.h>
6 
7 #include <fwk/CentralConfig.h>
8 #include <fwk/LocalCoordinateSystem.h>
9 #include <fwk/CoordinateSystemRegistry.h>
10 
11 #include <utl/AugerCoordinateSystem.h>
12 
13 #include <io/ZHAireSFile.h>
14 #include <io/ZHAireSIOException.h>
15 
16 #include <utl/AugerUnits.h>
17 #include <utl/ErrorLogger.h>
18 #include <utl/PhysicalConstants.h>
19 #include <utl/TimeStamp.h>
20 #include <utl/config.h>
21 #include <utl/Particle.h>
22 
23 #include <utl/CoordinateSystem.h>
24 #include <utl/Point.h>
25 #include <utl/ReferenceEllipsoid.h>
26 #include <utl/UTMPoint.h>
27 #include <utl/System.h>
28 
29 #include <det/Detector.h>
30 
31 #include <cmath>
32 #include <dirent.h>
33 #include <iostream>
34 #include <iomanip>
35 #include <sstream>
36 #include <string>
37 #include <string.h>
38 #include <fstream>
39 #include <boost/filesystem/path.hpp>
40 
41 #ifdef HAVE_AIRES
42 # include <io/AiresUtilities.h>
43 # include <io/AiresShowerFile.h>
44 #endif
45 
46 using namespace std;
47 using namespace utl;
48 using namespace evt;
49 using namespace fwk;
50 
51 namespace fs = boost::filesystem;
52 
53 
54 namespace io {
55 
56  ZHAireSFile::~ZHAireSFile()
57  {
58  delete fZHAireSin;
59  delete fsry;
60  delete fdef;
61  #ifdef HAVE_AIRES
62  delete fAiresShowerFile;
63  #endif
64  }
65 
66 
67  ZHAireSFile::ZHAireSFile(const std::string& fileName, const Mode mode, utl::Branch* const b) :
68  VEventFile(fileName, mode, b)
69  {
70  // TEST log
71  logfile.open("ZHAireSFile.log");
72  cout << "Opening Log File..." << endl;
73 
74  Open(fileName, mode, b);
75  }
76 
77 
78  void
79  ZHAireSFile::Open(const std::string& fileName, const Mode mode, utl::Branch* const /*b*/)
80  {
81  // make a copy of the file name, we might have to update it
82  std::string ourFileName = fileName;
83 
84  // determine the host name from REASFile.cc
85  char tmpHostNameString[256] = { '\0' };
86  gethostname(tmpHostNameString, 256);
87  fHostName = tmpHostNameString;
88 
89  if (fileName.size() < 8 ||
90  (fileName.substr(fileName.size() - 8, 8) != ".zhaires" &&
91  fileName.substr(fileName.size() - 7, 7) != ".tar.gz")) {
92  ostringstream msg;
93  msg << "Cannot open file " << fileName << " - file name does not end on .zhaires";
94  ERROR(msg);
95  throw io::ZHAireSIOException(msg.str());
96  }
97 
98  if (mode != eRead) {
99  ostringstream msg;
100  msg << "Cannot open file " << fileName << " - ZHAireS files are read-only";
101  ERROR(msg);
102  throw io::ZHAireSIOException(msg.str());
103  }
104 
105  stringstream directory;
106  // check if we try to read in a .tar.gz file; if so, uncompress it and update the file name accordingly adopted from REASFile.cc
107  if (fileName.substr(fileName.size() - 7, 7) == ".tar.gz") {
108 
109  directory << "./TempExtractionDir/" << fHostName << "_" << fProcessId;
110 
111  stringstream command;
112  command << "rm -rf " << directory.str();
113  System(command);
114 
115  ostringstream msg;
116  msg << "Extracting " << fileName << " to directory " << directory.str() << " ...";
117  INFO(msg);
118  command.str("");
119  command << "mkdir 2>/dev/null ./TempExtractionDir";
120  System(command);
121  command.str("");
122  command << "mkdir 2>/dev/null " << directory.str();
123  System(command);
124  command.str("");
125 
126  // set flag according to file extension
127  string flag = (fileName.substr(fileName.size() - 7, 7) == ".tar.gz") ? "xfz": "xfj";
128 
129  command << "tar " << flag << " " << fileName << " -C " << directory.str();
130  System(command);
131 
132  // extract entry name and update filename
133  string entryname = fileName;
134  size_t pos = 0;
135  while ((pos = entryname.find("/")) != std::string::npos) {
136  entryname.erase(0, pos + 1);
137  }
138  ourFileName = directory.str() + "/" + entryname.substr(0,entryname.size()-7) + ".zhaires";
139 
140  }
141 
142  fZHAireSin = new ifstream(ourFileName.c_str());
143  if (!*fZHAireSin) {
144  ostringstream msg;
145  msg << "Cannot open file " << ourFileName << " - ERROR";
146  ERROR(msg);
147  throw io::ZHAireSIOException(msg.str());
148  }
149 
150  ostringstream info;
151  info << "ZHAireS simulation " << ourFileName << '\n';
152 
153  //TEST LOG
154  logfile << "ZHAireS simulation " << ourFileName << '\n';
155 
156  const string basename = ourFileName.substr(0, ourFileName.size() - 8); // strip of extension ".zhaires"
157  const size_t pathlength = ourFileName.find_last_of("/"); // find last slash
158  string pathname;
159  string simname;
160 
161  if (pathlength >= ourFileName.size()) {
162  pathname = "./";
163  simname = basename;
164  } else {
165  pathname = ourFileName.substr(0, pathlength) + "/";
166  simname = basename.substr(pathname.size(),basename.size()-pathlength);
167  }
168 
169  const string sryFileName = basename + ".sry";
170  const string alternative_sryFileName = directory.str() + "/" + basename.substr(2, basename.size()+1) + ".sry"; // *tar.gz
171  fsry = new ifstream(sryFileName.c_str());
172  if (!*fsry) {
173  fsry = new ifstream(alternative_sryFileName.c_str());
174  ostringstream msg;
175  msg << "Cannot open Aires summary file " << sryFileName
176  << "\tLooking for .sry file in " << alternative_sryFileName;
177  INFO(msg);
178  if (!*fsry) {
179  msg.str("");
180  msg << "No .sry file found in " << alternative_sryFileName;
181  ERROR(msg);
182  throw io::ZHAireSIOException(msg.str());
183  }
184  }
185 
186  const string defFileName = basename + ".def";
187  const string alternative_defFileName = directory.str() + "/" + basename.substr(2, basename.size()+1) + ".def"; // *tar.gz
188  fdef = new ifstream(defFileName.c_str());
189  if (!*fdef) {
190  fdef = new ifstream(alternative_defFileName.c_str());
191  ostringstream msg;
192  msg << "Cannot open event auger definitions file (.def) " << defFileName
193  << "\tLooking for .def file in " << alternative_defFileName;
194  ERROR(msg);
195  if (!*fdef) {
196  msg.str("");
197  msg << "No .def file found in " << alternative_defFileName;
198  ERROR(msg);
199  throw io::ZHAireSIOException(msg.str());
200  }
201  }
202 
203 //#warning Should refactor ZHAireSFile reader to use boost filesystem library.
204 
205  INFO(info);
206  }
207 
208 
209  void
211  {
212  fZHAireSin->close();
213  fsry->close();
214  fdef->close();
215 #ifdef HAVE_AIRES
216  if (fAiresShowerFile) {
217  fAiresShowerFile->Close();
218  delete fAiresShowerFile;
219  fAiresShowerFile = nullptr;
220  }
221 #endif
222 
223  // delete temporary extraction directory; Taken from ZhairesFile.cc
224  stringstream command;
225  command << "rm -rf ./TempExtractionDir/" << fHostName << "_" << fProcessId;
226  System(command);
227  }
228 
229 
230  Status
232  {
233  // test if there are any more events in this file to read
234  if (fCurrentPosition >= (unsigned int)GetNEvents())
235  return eEOF;
236 
237  INFO("Reading ZHAireSFile");
238 
239  if (event.HasSimShower()) {
240  ERROR("Event not cleared - has SimShower. Cannot read ZHAireSFile.");
241  return eFail;
242  }
243 
244  double xcorepampa = 0; // core coordinates in Pampa AmarillaCS
245  double ycorepampa = 0; // will be read from .def file
246  double zcorepampa = 0;
247 
248  // core altitude: will be read from aires .sry file (Ground Altitude)
249  double corealtitude = 0;
250  double primaryenergy = 0;
251  double showerzenith = 0;
252  double Xmax = 0;
253  // Shower azimuth is different in Aires and Auger systems
254  double showerazimuthaires = 0;
255  double showerazimuthauger = 0;
256 
257  string airesfilepath;
258  string airesparamfile;
259  int eventnumber = -1;
260  int runnumber = -1;
261  int gpssecs = 0; // integers for gpssec and gpsns
262  int gpsnanosecs = 0;
263 
264  char lineBuf[1024];
265  char cdummy[80];
266 
267  // ==========Read Info from aires .def file
268  // Disregard comments
269  while (fdef->peek() == '#')
270  fdef->getline(lineBuf, 1024);
271  // Read
272  *fdef >> cdummy
273  >> xcorepampa
274  >> cdummy
275  >> ycorepampa
276  >> cdummy
277  >> zcorepampa
278  >> cdummy
279  >> gpssecs
280  >> cdummy
281  >> gpsnanosecs
282  >> cdummy
283  >> eventnumber
284  >> runnumber;
285  // set units to m (redundant)
286  xcorepampa *= m;
287  ycorepampa *= m;
288  zcorepampa *= m;
289 
290  // print what was read
291  cout << "===========================================\n"
292  "Data read from .def file:\n"
293  "xcorepampa(m): " << xcorepampa << " "
294  "ycorepampa(m): " << ycorepampa << " "
295  "zcorepampa(m): " << zcorepampa << " "
296  "GPSs: " << gpssecs << " GPSns: " << gpsnanosecs << " "
297  "evt#: " << eventnumber << " "
298  "runnumber: " << runnumber << endl;
299  // =========finished reading Aires .def file
300 
301  // TEST LOG
302  logfile << "===========================================\n"
303  << "Data read from .def file:\n"
304  "xcorepampa(m): " << xcorepampa << " "
305  "ycorepampa(m): " << ycorepampa << " "
306  "zcorepampa(m): " << zcorepampa << " "
307  "GPSs: " << gpssecs << " GPSns: " << gpsnanosecs << " "
308  "evt#: " << eventnumber << " "
309  "runnumber: " << runnumber << endl;
311 
312  const long sec = gpssecs;
313  const long nsec = gpsnanosecs;
314  utl::TimeStamp timestamp(sec, nsec);
315  // TEST LOG
316  logfile << "long sec:" << sec << " long nsec:" << nsec << "\n"
317  " Reading TimeStamp....\n"
318  "sec:" << timestamp.GetGPSSecond() << "\n"
319  "nanosec:" << timestamp.GetGPSNanoSecond() << "\n"
320  "nanosec-gpsnanosecs: " << timestamp.GetGPSNanoSecond() - nsec << endl;
322 
323  // =========Read Aires Info from .sry
324  // NOTE: If the magnetic field is Off in ZHAires fBDeclination will be set to 0
325  int Nlines = 0, NlinesE = 0, Nlinesalt = 0, NlinesBdeclination = 0, NlinesXmax = 0;
326  double value;
327  char teststringangle[] = "angle:", teststringenergy[] = "energy:";
328  char teststringalt[] = "altitude:", ReadString[80], testBdeclination[] = "D:";
329  char teststringXmax[] = "(g/cm2):";
330 
331  while (fsry->good() && (Nlines != 2 || NlinesE != 1 || Nlinesalt != 2 || NlinesXmax != 2)) {
332 
333  *fsry >> ReadString;
334 
335  if (strcmp(teststringangle, ReadString) == 0) {
336  ++Nlines;
337  *fsry >> value;
338  if (Nlines == 1) {
339  showerzenith = value*deg;
340  // TEST LOG
341  logfile << "showerzenith=" << showerzenith << " rad=" << showerzenith/deg << " deg" << endl;
342  } else if (Nlines == 2) {
343  showerazimuthaires = value*deg;
344  // TEST LOG
345  logfile << "showerazimuthaires=" << showerazimuthaires << " rad=" << showerazimuthaires/deg << " deg" << endl;
346  }
347  }
348  if (strcmp(teststringenergy, ReadString) == 0) {
349  ++NlinesE;
350  if (NlinesE == 1) {
351  *fsry >> value;
352  *fsry >> ReadString; // read the units
353 
354  if (strcmp(ReadString, "eV") == 0)
355  primaryenergy = value*eV;
356  else if (strcmp(ReadString, "keV") == 0)
357  primaryenergy = value*keV;
358  else if (strcmp(ReadString, "MeV") == 0)
359  primaryenergy = value*MeV;
360  else if (strcmp(ReadString, "GeV") == 0)
361  primaryenergy = value*GeV;
362  else if (strcmp(ReadString, "TeV") == 0)
363  primaryenergy = value*TeV;
364  else if (strcmp(ReadString, "PeV") == 0)
365  primaryenergy = value*PeV;
366  else if (strcmp(ReadString, "EeV") == 0)
367  primaryenergy = value*EeV;
368  else {
369  ostringstream msg;
370  msg << "Unrecognized unit:" << ReadString << " in sry file - ERROR";
371  ERROR(msg);
372  throw io::ZHAireSIOException(msg.str());
373  }
374  // TEST LOG
375  logfile << "primaryenergy=" << primaryenergy/eV << " eV" << endl;
376  }
377  }
378 
379  if (strcmp(teststringalt, ReadString) == 0) {
380  ++Nlinesalt;
381  if (Nlinesalt == 2) {
382  *fsry >> value;
383  *fsry >> ReadString; // read the units
384 
385  if (strcmp(ReadString, "km") == 0)
386  corealtitude = value*km;
387  else if (strcmp(ReadString, "m") == 0)
388  corealtitude = value*m;
389  else if (strcmp(ReadString, "cm") == 0)
390  corealtitude = value*cm;
391  else {
392  ostringstream msg;
393  msg << "Unrecognized unit:" << ReadString << " in sry file - ERROR";
394  ERROR(msg);
395  throw io::ZHAireSIOException(msg.str());
396  }
397  // TEST LOG
398  logfile << "corealtitude=" << corealtitude/m << " m" << endl;
399  }
400  }
401  if (strcmp(testBdeclination, ReadString) == 0) {
402  ++NlinesBdeclination;
403 
404  if (NlinesBdeclination == 1) {
405  *fsry >> value;
406  fBDeclination = value*deg;
407  // TEST LOG
408  logfile << "Found B declination of " << fBDeclination/deg << " deg=" << fBDeclination << "rad value from .sry: " << value << endl;
409  }
410  }
411  if (strcmp(teststringXmax, ReadString) == 0) {
412  ++NlinesXmax;
413  if (NlinesXmax == 2) {
414  *fsry >> value;
415  Xmax = value*g/cm2;
416  }
417  }
418  }
419  cout << "===========================================\n"
420  "Data read from .sry file:\n"
421  "Zenith: " << showerzenith/deg << " deg, "
422  "Aires Azimuth: " << showerazimuthaires/deg << " deg, "
423  "E0: " << primaryenergy/eV << " eV, "
424  "Ground Altitude: " << corealtitude/m << " m, "
425  "B declination: " << fBDeclination/degree << " deg" << ", "
426  "Xmax: " << Xmax/g*cm2 << " g/cm2" << endl;
427  // =========finished reading Aires .sry file
428 
429  // TEST LOG
430  logfile << "===========================================\n"
431  "Data read from .sry file:\n"
432  "Zenith: " << showerzenith/deg << " deg, "
433  "Aires Azimuth: " << showerazimuthaires/deg << " deg, "
434  "E0: " << primaryenergy/eV << " eV, "
435  "Ground Altitude: " << corealtitude/m << " m, "
436  "B declination: " << fBDeclination/degree << " deg, "
437  "Xmax: " << Xmax/g*cm2 << " g/cm2" << endl;
439 
440  // After reading the .sry file we can set the sim shower etc
441 
442 #warning RU: this is inconsistent with how this is handled in corsika
443  // Magnetic declination and rotation angle for Aires to CoreCS transformation
444  const double MagDeclination = fBDeclination;
445  const double RotationAngle = 90*deg - MagDeclination;
446  event.MakeSimShower(DefaultShowerGeometryProducer(RotationAngle));
447 
448  logfile << "MagDeclination=" << MagDeclination << " rad=" << MagDeclination/deg << " deg\n"
449  "RotationAngle=" << RotationAngle << " rad=" << RotationAngle/deg << " deg" << endl;
450 
451  ShowerSimData& shower = event.GetSimShower();
452 
453  if (shower.HasRadioSimulation()) {
454  WARNING ("Event not cleared - has RadioSimulation");
455  // what is the consequence of this? could lead to multiple antenna pulses for same positions!?
456  } else
457  shower.MakeRadioSimulation();
458 
459  RadioSimulation& radiosim = shower.GetRadioSimulation();
460 
461  // Set shower general parameters
462 //#warning At the moment, ZHAireSFile reader fixes proton as primary.
464  shower.SetEnergy(primaryenergy);
465  //showerzenith *= deg;
466  shower.SetGroundParticleCoordinateSystemZenith(showerzenith);
467 
468  // Azimuth definition is different between Aires and Corsika (and Auger)
469  // transform Aires Azimuth to Auger Azimuth
470  showerazimuthauger = ZHAireSAzimuthToAuger(showerazimuthaires);
471  // Set shower azimuth
472  shower.SetGroundParticleCoordinateSystemAzimuth(showerazimuthaires); // Sets shower azimuth angle
473  cout << "Azimuth angle: Aires: " << showerazimuthaires/deg << " deg "
474  "Auger: " << showerazimuthauger/deg << " deg" << endl;
475  // TEST LOG
476  logfile << "((((((((((((((((((\n"
477  "Azimuth angle: Aires: " << showerazimuthaires/deg << " deg "
478  "Auger: " << showerazimuthauger/deg << " deg\n"
479  "Reading ShowerSimData...\n"
480  "ShowerNumber:" << shower.GetShowerNumber() << "\n"
481  "RunId:" << shower.GetShowerRunId() << "\n"
482  "Primary:" << shower.GetPrimaryParticle() << "\n"
483  "Energy:" << shower.GetEnergy() << "\n"
484  "Azimuth:" << shower.GetGroundParticleCoordinateSystemAzimuth()/deg << " deg\n"
485  "Zenith:" << shower.GetGroundParticleCoordinateSystemZenith()/deg << " deg\n"
486  "B incl:" << shower.GetMagneticFieldInclination()/deg << " deg\n"
487  "B decl:" << shower.GetMagneticFieldDeclination()/deg << " deg\n"
488  "B:" << shower.GetMagneticFieldStrength()/gauss << " gauss\n"
489  "((((((((((((((((((" << endl;
491 
492 //#warning At the moment, ZHAireSFile reader fixes shower number and id to 1
493  shower.SetShowerNumber(1);
494  shower.SetShowerRunId("1");
495  shower.MakeTimeStamp(TimeStamp(sec, nsec));
496  // set event number and time stamp of the radio simulation
497  radiosim.SetEventNumber(eventnumber);
498  radiosim.SetRunNumber(runnumber);
499  //radiosim.SetEventTime(TimeStamp(gpssecs,gpsnanosecs));
500  radiosim.SetEventTime(TimeStamp(sec, nsec));
501  // TEST LOG
502  logfile << "radiosim time: " << radiosim.GetEventTime().GetGPSSecond() << "," << radiosim.GetEventTime().GetGPSNanoSecond() << endl;
503 
504  // set the event (not revent) id
505  ostringstream tmpout;
506  tmpout << "ZHAireS_r" << runnumber << "_e" << eventnumber;
507  event.GetHeader().SetId(tmpout.str());
508 
509  // get reference coordinate system of detector (usually PampaAmarilla)
510  const utl::CoordinateSystemPtr& referenceCS = det::Detector::GetInstance().GetReferenceCoordinateSystem();
511  const Point core(xcorepampa, ycorepampa, zcorepampa, referenceCS);
512  const UTMPoint utmcore(core, utl::ReferenceEllipsoid::GetWGS84());
513  // Core altitude (used as ground altitude in AIRES)
514  //float coreutmaltitude = utmcore.GetHeight();
515  cout << "===========================================\n"
516  "CORE altitude: utmcore.GetHeight()=" << utmcore.GetHeight() << " "
517  "corealtitude (from aires .sry file)=" << corealtitude/m << endl;
518 
519  // TEST LOG
520  logfile << "===========================================\n"
521  "CORE altitude: utmcore.GetHeight()=" << utmcore.GetHeight() << " "
522  "corealtitude (from aires .sry file)=" << corealtitude/m << endl;
524 
525  // Set shower core coordinates (we assume it should be in Pampa AmarillaCS)
526  radiosim.SetCoreCoordinates(xcorepampa, ycorepampa, zcorepampa);
527  cout << "===========================================\n"
528  "Core coords (Pampa) sent to radiosim.SetCoreCoordinates: "
529  << xcorepampa << ", " << ycorepampa << ", " << zcorepampa << endl;
530 
531  // TEST LOG
532  logfile << "===========================================\n"
533  "Core coords (Pampa) sent to radiosim.SetCoreCoordinates (m): "
534  << xcorepampa/m << ", " << ycorepampa/m << ", " << zcorepampa/m << endl;
536 
537  /* ZHAireS File column setup
538  # 1 - Shower Number
539  # 2 - Antenna Number
540  # 3 - Antenna X (m) --> North
541  # 4 - Antenna Y (m) --> West
542  # 5 - Antenna Z (m)
543  # 6 - Time (ns)
544  # 7 - |A| (V/M)
545  # 8 - Ax (V/M)
546  # 9 - Ay (V/M)
547  # 10 - Az (V/M)
548  # 11 -|E| (V/M)
549  # 12 - Ex (V/M)
550  # 13 - Ey (V/M)
551  # 14 - Ez (V/M)
552  */
553 
554  // Shower number and antenna number (aires)
555  int showern, antn, lastant;
556  // Antenna coordinates in Aires and CoreCS system
557  double antxaires, antyaires, antzINJ;
558  double antxcorecs, antycorecs, antzcorecs;
559  // Antenna altitude
560  double antalt;
561  // Electric field components in Aires and CoreCS systems
562  double exvmzhaires, eyvmzhaires, ezvmzhaires, exvmcorecs, eyvmcorecs, ezvmcorecs;
563  // Time in ns
564  double time, dummy;
565 
566  //get rid of commented lines at the beginning
567  while (fZHAireSin->peek() == '#')
568  fZHAireSin->getline(lineBuf, 1024);
569 
570  if (!fZHAireSin->good()) {
571  ostringstream msg;
572  msg << "Cannot read from ZHAireS File";
573  ERROR(msg);
574  throw io::ZHAireSIOException(msg.str());
575  }
576 
577  // Antenna position (antxaires,antyaires,antzaires) is in AiresCS
578  // Note that ZHAireS input accepts antz in m above ground, but in the output the z coordinate is in m below
579  // the injection point (antzINJ).
580  // This is the internal ZHAires System. For now we assume an injection altitude of 100km!
581  // PLEASE CHECK THAT YOUR SIMULATION USES THIS INJECTION ALTITUDE!!!!
582 
583  // Read from .zhaires file
584  *fZHAireSin >> showern
585  >> antn
586  >> antxaires
587  >> antyaires
588  >> antzINJ
589  >> time
590  >> dummy
591  >> dummy
592  >> dummy
593  >> dummy
594  >> dummy
595  >> exvmzhaires
596  >> eyvmzhaires
597  >> ezvmzhaires;
598 
599  // set units
600  antxaires *= m;
601  antyaires *= m;
602  antzINJ *= m;
603  time *= ns;
604  exvmzhaires *= (V/m);
605  eyvmzhaires *= (V/m);
606  ezvmzhaires *= (V/m);
607 
608  double firsttime = time, secondtime;
609  bool readsecondtime = false;
610  double binning = 0;
611  while (fZHAireSin->good()) {
612 
613  SimRadioPulse currpulse;
614  TraceV3D& currtimeseries = currpulse.GetEfieldTimeSeries();
615  lastant = antn;
616 
617  // X and Y coordinate transformation from ZHAireS System to CoreCS (offline):
618  // Station coordinates (CoreCS transformed to Aires system):
619  // The transformation is just a rotation around the z-axis of
620  // an angle of 90deg-magdeclination=85.767deg.
621  // (The old transformation just disregarded the magnetic declination)
622  // From CoreCS to AiresCS the rotation angle is negative
623  // From AiresCS to CoreCS the rotation angle is positive
624  // ZHAireS output has a different Z coordinate (different from AIRES)
625  // Z transformation is done separately!!!
626  double vantaires[3] = { antxaires, antyaires, antzINJ };
627  double vantcorecs[3];
628  Rotatez(RotationAngle, vantaires, vantcorecs);
629  antxcorecs = vantcorecs[0];
630  antycorecs = vantcorecs[1];
631 
632  // Antenna Z coordinate needs transformations from zhaires system to CoreCS:
633  // step 1-transform antenna z coordinate (antzINJ) read from zhaires output to altitude above sea level (m)
634 #warning readZHAireS assumes injection altitude of 100km (should be read from.sry later).
635  antalt = (100000 - antzINJ)*m;
636  // step 2-transform antenna altitude to CoreCS
637  antzcorecs = antalt - corealtitude;
638 
639  // Print antenna position in both systems
640  cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"
641  "Antenna " << antn << " "
642  "position in zhairesCS (m): x=" << antxaires/m << " y=" << antyaires/m << " z=" << antzINJ/m << " "
643  "(antalt=" << antalt/m << ")\n"
644  "Antenna " << antn << " "
645  "position in CoreCS (m): x=" << antxcorecs/m << " y=" << antycorecs/m << " z=" << antzcorecs/m << endl;
646 
647  // TEST LOG
648  // Print antenna position in both systems
649  logfile << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"
650  "Antenna " << antn << " "
651  "position in zhairesCS (m): x=" << antxaires/m << " y=" << antyaires/m << " z=" << antzINJ/m << " "
652  "(antalt=" << antalt/m << ")\n"
653  "Antenna " << antn << " "
654  "position in CoreCS (m): x=" << antxcorecs/m << " y=" << antycorecs/m << " z=" << antzcorecs/m << endl;
656 
657  // The SimRadioPulse documentation doesn't specify in which system northing, easting and height are given!!!!
658  // Poor documentation leads to poor code and waste of (LOTS OF) time!
659 
660  // Assume misnoma in variable names and set antenna position in CoreCS coordinates
661  // Setting Antenna position
662  currpulse.SetRelativeCoordinates(antxcorecs, antycorecs, antzcorecs);
663 
664 #warning RU: this must be checked:
665  bool first = true;
666  double efield[3];
667  while (lastant == antn && fZHAireSin->good()) {
668 
669  if (first) { // first time entry for this antenna
670  // Set start time
671  currpulse.SetStartTime(time);
672  cout << "StartTime:" << time/nanosecond << " ns" << endl;
673 
674  // TEST LOG
675  logfile << "StartTime:" << time/nanosecond << " ns" << endl;
677 
678  // Field components need transformation from zhairesCS to CoreCS
679  double veaires[3] = { exvmzhaires, eyvmzhaires, ezvmzhaires};
680  double vecorecs[3];
681  Rotatez(RotationAngle, veaires, vecorecs);
682  exvmcorecs = vecorecs[0];
683  eyvmcorecs = vecorecs[1];
684  // ZHAireS z coordinate points down
685  ezvmcorecs = -vecorecs[2];
686 
687  // Set field for current time
688  efield[0] = exvmcorecs;
689  efield[1] = eyvmcorecs;
690  efield[2] = ezvmcorecs;
691  currtimeseries.PushBack(Vector3D(efield));
692  first = false; // Set first time entry as done
693 
694  // TEST LOG
695  logfile << "First time bin ZHAireSCS E (V/m): ("
696  << exvmzhaires/(V/m) << "," << eyvmzhaires/(V/m) << "," << ezvmzhaires/(V/m) << ")\n"
697  "First time bin CoreCS E (V/m): ("
698  << exvmcorecs/(V/m) << "," << eyvmcorecs/(V/m) << "," << ezvmcorecs/(V/m) << ")" << endl;
700 
701  } else {
702  // Field components need transformation from zhairesCS to CoreCS
703  double veaires[3] = { exvmzhaires, eyvmzhaires, ezvmzhaires};
704  double vecorecs[3];
705  Rotatez(RotationAngle, veaires, vecorecs);
706  exvmcorecs = vecorecs[0]*volt/meter;
707  eyvmcorecs = vecorecs[1]*volt/meter;
708  // ZHAireS z coordinate points down
709  ezvmcorecs = -vecorecs[2]*volt/meter;
710 
711  // Set field for current time
712  efield[0] = exvmcorecs;
713  efield[1] = eyvmcorecs;
714  efield[2] = ezvmcorecs;
715  currtimeseries.PushBack(Vector3D(efield));
716  }
717 
718  *fZHAireSin >> showern
719  >> antn
720  >> antxaires
721  >> antyaires
722  >> antzINJ
723  >> time
724  >> dummy
725  >> dummy
726  >> dummy
727  >> dummy
728  >> dummy
729  >> exvmzhaires
730  >> eyvmzhaires
731  >> ezvmzhaires;
732 
733  // set units
734  antxaires *= m;
735  antyaires *= m;
736  antzINJ *= m;
737  time *= ns;
738  exvmzhaires *= (V/m);
739  eyvmzhaires *= (V/m);
740  ezvmzhaires *= (V/m);
741 
742  if (!readsecondtime) { // use 1st and 2nd time entries to calculate bin width
743  secondtime = time;
744  binning = secondtime - firsttime;
745  readsecondtime = true;
746  }
747  if (!first && (lastant != antn || !fZHAireSin->good())) { // Changed antenna
748  if (!fZHAireSin->good()) {
749  // Field components need transformation from zhairesCS to CoreCS
750  double veaires[3] = { exvmzhaires, eyvmzhaires, ezvmzhaires };
751  double vecorecs[3];
752  Rotatez(RotationAngle, veaires, vecorecs);
753  //exvmcorecs = vecorecs[0]*volt/meter;
754  //eyvmcorecs = vecorecs[1]*volt/meter;
755  // ZHAireS z coordinate points down
756  //ezvmcorecs = -vecorecs[2]*volt/meter;
757  exvmcorecs = vecorecs[0];
758  eyvmcorecs = vecorecs[1];
759  // ZHAireS z coordinate points down
760  ezvmcorecs = -vecorecs[2];
761 
762  // Set field for current time
763  efield[0] = exvmcorecs;
764  efield[1] = eyvmcorecs;
765  efield[2] = ezvmcorecs;
766  currtimeseries.PushBack(Vector3D(efield));
767  }
768  // Set bin width
769  currpulse.SetBinning(binning);
770  //cout << "BINNING:" << currpulse.GetBinning() << endl;
771 
772  // TEST LOG
773  logfile << "BINNING:" << currpulse.GetBinning()/ns << " ns=" << binning/ns << " ns" << endl;
775 
776  // Finished with this antenna
777  radiosim.AddSimRadioPulse(currpulse);
779  }
780  }
781  }
782 
783  // TEST LOG
784  logfile << "((((((((((((((((((\n"
785  "Azimuth angle: Aires: " << showerazimuthaires/deg << " deg "
786  "Auger: " << showerazimuthauger/deg << " deg\n"
787  "Reading ShowerSimData...\n"
788  "ShowerNumber:" << shower.GetShowerNumber() << "\n"
789  "RunId:" << shower.GetShowerRunId() << "\n"
790  "Primary:" << shower.GetPrimaryParticle() << "\n"
791  "Energy:" << shower.GetEnergy() << "\n"
792  "Azimuth:" << shower.GetGroundParticleCoordinateSystemAzimuth()/deg << " deg\n"
793  "Zenith:" << shower.GetGroundParticleCoordinateSystemZenith()/deg << " deg\n"
794  "B incl:" << shower.GetMagneticFieldInclination()/deg << " deg\n"
795  "B decl:" << shower.GetMagneticFieldDeclination()/deg << " deg\n"
796  "B:" << shower.GetMagneticFieldStrength()/gauss << " gauss\n"
797  "((((((((((((((((((\n"
798  "\n"
799  "Reading RadioSimulation....\n"
800  "NumPulses:" << radiosim.GetNumPulses() << "\n"
801  "HasCorePosition:" << radiosim.HasCorePosition() << "\n"
802  "Corex:" << radiosim.GetCorePosition().GetX(referenceCS) << "\n"
803  "Corey:" << radiosim.GetCorePosition().GetY(referenceCS) << "\n"
804  "Corez:" << radiosim.GetCorePosition().GetZ(referenceCS) << "\n"
805  "RunNumber:" << radiosim.GetRunNumber() << "\n"
806  "EventNumber:" << radiosim.GetEventNumber() << "\n"
807  "EventTime:" << radiosim.GetEventTime() << "\n"
808  "((((((((((((((((((\n"
809  "\n"
810  "Reading RadioSimulation pulses...." << endl;
812  for (int i = 0; i < radiosim.GetNumPulses(); ++i) {
813  const SimRadioPulse& pulse = radiosim.GetSimPulseByIndex(i);
814  pulse.SetLocalCoordinateSystem(referenceCS);
815  logfile << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
816  "pulse #" << i << "\n"
817  "Binning:" << pulse.GetBinning() << "\n"
818  "StartTime:" << pulse.GetStartTime() << "\n"
819  "X CoreCS:" << pulse.GetLocation().GetX(coreCS) << "\n"
820  "Y CoreCS:" << pulse.GetLocation().GetY(coreCS) << "\n"
821  "Z CoreCS:" << pulse.GetLocation().GetZ(coreCS) << "\n"
822  "X referenceCS:" << pulse.GetLocation().GetX(referenceCS) << "\n"
823  "Y referenceCS:" << pulse.GetLocation().GetY(referenceCS) << "\n"
824  "Z referenceCS:" << pulse.GetLocation().GetZ(referenceCS) << "\n"
825  "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
826  }
828 
829  return eSuccess;
830  }
831 
832 
833  void
834  ZHAireSFile::Write(const evt::Event& /*event*/)
835  {
836  const string msg = "Cannot write to ZHAireS File - read-only file";
837  ERROR(msg);
838  throw io::ZHAireSIOException(msg);
839  }
840 
841 
842  Status
843  ZHAireSFile::FindEvent(const unsigned int eventId)
844  {
845  // ZHAireS shower don't have event ids
846  return GotoPosition(eventId);
847  }
848 
849 
850  Status
851  ZHAireSFile::GotoPosition(const unsigned int position)
852  {
853  if (position > (unsigned int)GetNEvents())
854  return eFail;
855  else {
856  //fCurrentPosition = position;
857  return eSuccess;
858  }
859  }
860 
861 }
const double eV
Definition: GalacticUnits.h:35
int GetPrimaryParticle() const
Get the type of the shower primary particle.
Definition: ShowerSimData.h:84
double GetBinning() const
Get the sampling time scale.
Definition: SimRadioPulse.h:34
const double degree
Point object.
Definition: Point.h:32
const double PeV
void SetStartTime(const double starttime)
Definition: SimRadioPulse.h:41
const SimRadioPulse & GetSimPulseByIndex(const int index)
void System(const char *const command, const bool throwOnError, const bool notify)
Definition: System.cc:10
int GetEventNumber() const
Get the event number of the RadioSimulation.
void MakeRadioSimulation()
Make the RadioSimulation.
Class to hold and convert a point in geodetic coordinates.
Definition: UTMPoint.h:40
Data structure for simulated Radio pulses.
Definition: SimRadioPulse.h:29
bool HasSimShower() const
Data structure for a radio simulation (including several SimRadioPulses)
void SetBinning(const double samplingtime)
Definition: SimRadioPulse.h:36
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)...
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
bool HasRadioSimulation() const
Check initialization of the RadioSimulation.
utl::SVector< 3, double > Vector3D
Definition: Trace-fwd.h:32
double ZHAireSAzimuthToAuger(const double airesAzimuth)
Returns the azimuth rotated from AIRES&#39;s system to Auger standard.
Definition: ZHAireSFile.h:41
utl::Point GetLocation() const
Definition: SimRadioPulse.h:51
std::ifstream * fsry
Definition: ZHAireSFile.h:67
void SetRunNumber(const int runnum)
Set the run number of the RadioSimulation.
const double EeV
Definition: GalacticUnits.h:34
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.
#define V
constexpr double deg
Definition: AugerUnits.h:140
void SetLocalCoordinateSystem(utl::CoordinateSystemPtr localCS) const
Definition: SimRadioPulse.h:70
constexpr double MeV
Definition: AugerUnits.h:184
Interface class to access Shower Simulated parameters.
Definition: ShowerSimData.h:49
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
int GetShowerNumber() const
Get the number of the shower in the file.
Definition: ShowerSimData.h:72
Class representing a document branch.
Definition: Branch.h:107
const double gauss
Definition: GalacticUnits.h:41
Status
Return code for seek operation.
Definition: IoCodes.h:24
double GetMagneticFieldStrength() const
Get the absolute strength of the Earth&#39;s magnetic field used in CORSIKA/REAS simulation.
std::ofstream logfile
Definition: ZHAireSFile.h:76
static void Rotatez(const double theta, const double vi[], double vf[])
Definition: ZHAireSFile.h:50
utl::Point GetCorePosition() const
Get the core position of the RadioSimulation as a utl::Point.
double GetHeight() const
Get the height.
Definition: UTMPoint.h:212
double GetX(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:206
void SetEventTime(const utl::TimeStamp &t)
Set the event time of the RadioSimulation.
bool HasCorePosition() const
const double ns
RadioSimulation & GetRadioSimulation()
Get the radio simulation data.
constexpr double nanosecond
Definition: AugerUnits.h:143
utl::CoordinateSystemPtr GetLocalCoordinateSystem() const
get local coordinate system anchored at the core position
void Close() override
Definition: ZHAireSFile.cc:210
void SetCoreCoordinates(const double x, const double y, const double z)
Set the core position coordinates of the RadioSimulation.
static const ReferenceEllipsoid & GetWGS84()
Get the auger standard ellipsoid: wgs84.
void SetEnergy(const double theEnergy)
Set the energy of the shower primary particle.
Definition: ShowerSimData.h:91
const double km
constexpr double g
Definition: AugerUnits.h:200
double GetEnergy() const
Get the energy of the shower primary particle.
Definition: ShowerSimData.h:89
double fBDeclination
Definition: ZHAireSFile.h:64
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
void AddSimRadioPulse(const SimRadioPulse &rp)
Add a radio pulse to the RadioSimulation.
unsigned long fProcessId
Definition: ZHAireSFile.h:74
std::ifstream * fdef
Definition: ZHAireSFile.h:68
int GetNEvents() override
Definition: ZHAireSFile.h:37
const utl::TimeStamp & GetEventTime() const
Get the event time of the RadioSimulation.
double GetY(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:209
constexpr double TeV
Definition: AugerUnits.h:188
void SetPrimaryParticle(const int type)
Set the type of the shower primary particle.
constexpr double GeV
Definition: AugerUnits.h:187
double GetMagneticFieldDeclination() const
Get the declination of the Earth&#39;s magnetic field used in CORSIKA/REAS simulation.
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
void SetGroundParticleCoordinateSystemZenith(const double zenith)
Set the zenith angle of the shower. Room angle between z-axis and direction from where the shower is ...
double GetGPSNanoSecond() const
GPS nanosecond.
Definition: TimeStamp.h:127
std::string fHostName
Definition: ZHAireSFile.h:73
double GetMagneticFieldInclination() const
Get the inclination of the Earth&#39;s magnetic field used in CORSIKA/REAS simulation.
std::string GetShowerRunId() const
Get the run id for the shower.
Definition: ShowerSimData.h:78
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
void SetRelativeCoordinates(double northing, double easting, double height)
Definition: SimRadioPulse.h:56
void SetShowerNumber(const int sid)
Definition: ShowerSimData.h:75
constexpr double cm
Definition: AugerUnits.h:117
void MakeTimeStamp(const utl::TimeStamp &ts)
Make the TimeStamp of the shower.
int GetRunNumber() const
Get the run number of the RadioSimulation.
Base for exceptions in the ZHAireS reader.
Status GotoPosition(const unsigned int position) override
goto by position in the file
Definition: ZHAireSFile.cc:851
Status FindEvent(const unsigned int eventId) override
seek Event id set cursor there
Definition: ZHAireSFile.cc:843
long GetNumPulses() const
Get the number of radio pulses contained in the RadioSimulation.
constexpr double keV
Definition: AugerUnits.h:186
Status Read(evt::Event &event) override
read current event advance cursor by 1
Definition: ZHAireSFile.cc:231
void Write(const evt::Event &event) override
Definition: ZHAireSFile.cc:834
double GetGroundParticleCoordinateSystemZenith() const
Get the zenith angle of the shower. Room angle between z-axis and direction from where the shower is ...
double GetZ(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:212
void SetShowerRunId(const std::string srid)
Definition: ShowerSimData.h:81
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
constexpr double m
Definition: AugerUnits.h:121
double GetGroundParticleCoordinateSystemAzimuth() const
Get the azimuth angle of the shower. Angle in x-y plane wrt. to the x axis (0 is from east)...
const utl::TraceV3D & GetEfieldTimeSeries() const
Get the Trace of the simulated electric field.
Definition: SimRadioPulse.h:44
void Open(const std::string &fileName, const Mode mode=eRead, utl::Branch *const b=nullptr) override
Definition: ZHAireSFile.cc:79
unsigned int fCurrentPosition
Definition: ZHAireSFile.h:63
const double volt
Definition: GalacticUnits.h:38
double GetStartTime() const
Get the timestamp of the first sample.
Definition: SimRadioPulse.h:39
constexpr double cm2
Definition: AugerUnits.h:118
std::ifstream * fZHAireSin
Definition: ZHAireSFile.h:66

, generated on Tue Sep 26 2023.