RdChannelBeaconSimulator.cc
Go to the documentation of this file.
2 
3 #include <fwk/CentralConfig.h>
4 #include <fwk/RunController.h>
5 #include <fwk/LocalCoordinateSystem.h>
6 #include <fwk/CoordinateSystemRegistry.h>
7 #include <fwk/VModule.h>
8 
9 #include <utl/ErrorLogger.h>
10 #include <utl/Reader.h>
11 #include <utl/config.h>
12 #include <utl/TraceAlgorithm.h>
13 #include <utl/PhysicalConstants.h>
14 #include <utl/AxialVector.h>
15 #include <utl/Math.h>
16 #include <utl/FFTDataContainerAlgorithm.h>
17 
18 #include <evt/Event.h>
19 #include <revt/REvent.h>
20 #include <revt/Station.h>
21 #include <revt/Channel.h>
22 #include <revt/Header.h>
23 #include <revt/StationRecData.h>
24 
25 #include <det/Detector.h>
26 #include <rdet/RDetector.h>
27 
28 using namespace fwk;
29 using namespace utl;
30 using namespace std;
31 using namespace revt;
32 
34 
35  template<typename G>
36  inline
37  string
38  ToString(const G& g, const CoordinateSystemPtr cs)
39  {
40  ostringstream os;
41  os << '('
42  << g.GetX(cs)/meter << ", "
43  << g.GetY(cs)/meter << ", "
44  << g.GetZ(cs)/meter << ") [m]";
45  return os.str();
46  }
47 
49  fBeaconFrequencies(vector<double>())
50  {
51  }
52 
53  RdChannelBeaconSimulator::~RdChannelBeaconSimulator()
54  {
55  }
56 
59  {
60  // Initialize your module here. This method
61  // is called once at the beginning of the run.
62  // The eSuccess flag indicates the method ended
63  // successfully. For other possible return types,
64  // see the VModule documentation.
65 
66  INFO("RdChannelBeaconSimulator::Init()");
67 
68  // Read in the configurations of the xml file
69  Branch topBranch =
70  CentralConfig::GetInstance()->GetTopBranch("RdChannelBeaconSimulator");
71  topBranch.GetChild("BeaconFrequencies").GetData(fBeaconFrequencies);
72  topBranch.GetChild("BeaconPosition").GetData(fBeaconPosition);
73  topBranch.GetChild("Amplitude").GetData(fAmplitude);
74  topBranch.GetChild("TimeJitter").GetData(fTimeJitter);
75  topBranch.GetChild("TimeJitterMean").GetData(fTimeJitterMean);
76  topBranch.GetChild("OutputShiftFile").GetData(fOutputShiftFile);
77 
78  return eSuccess;
79  }
80 
82  RdChannelBeaconSimulator::Run(evt::Event& event)
83  {
84  stringstream fMessage;
85  // Check if there are events at all
86  if(!event.HasREvent()) {
87  WARNING("No radio event found!");
88  return eContinueLoop;
89  }
90 
91  fMessage.str("");
92  fMessage << "Start Beaconsimulator!!! ";
93  INFO(fMessage.str());
94 
95  REvent& rEvent = event.GetREvent();
96  // get detector description (for antenna positions)
97  const det::Detector& detector = det::Detector::GetInstance();
98  const rdet::RDetector& rDetector = detector.GetRDetector();
99  // get reference coordinate system of detector (usually PampaAmarilla)
100  const CoordinateSystemPtr referenceCS = detector.GetReferenceCoordinateSystem();
101 
102  // initialize random number generator
103  srand( time(NULL));
104 
105  float shift = 0.0;
106  ofstream outputfile;
107 
108  if(fOutputShiftFile != "") {
109  outputfile.open(fOutputShiftFile.c_str(), ofstream::app);
110  }
111 
112  // the absolute points in time and space
113  const Point siteOrigin(0,0,0, referenceCS);
114  Vector vStationPosition(0,0,0, referenceCS);
115  Vector vBeaconPosition(fBeaconPosition[0],fBeaconPosition[1],fBeaconPosition[2], referenceCS);
116  Vector vPositionDifference(0,0,0, referenceCS);
117 
118  sleep(1);
119  // loop through stations and for every station through every channel
120  for (REvent::StationIterator sIt = rEvent.StationsBegin();
121  sIt != rEvent.StationsEnd(); ++sIt) {
122  Station& station = *sIt;
123 
124  //First try to get the position
125  const rdet::Station& dStation = rDetector.GetStation(*sIt);
126  vStationPosition = dStation.GetPosition() - siteOrigin;
127 
128  //Calculate the vector and the distance from Beacon to the station
129  vPositionDifference = vBeaconPosition - vStationPosition;
130  double distance = sqrt(vPositionDifference * vPositionDifference);
131 
132  fMessage.str("");
133  fMessage << "Position of Station " << dStation.GetId() << ": " << ToString(vStationPosition, referenceCS)<< endl
134  << "Difference to Beacon: " << ToString(vPositionDifference, referenceCS)
135  << " => Distance to Beacon: " << distance << " [m]";
136  INFO(fMessage.str());
137 
138  if (fTimeJitter == 1) {
139  // randomly shift the timeseries to emulate the GPS jitter
140 // float shift = ((rand() / (float)RAND_MAX) * 6. ) - 3.;
141  // try gaussian distibution
142  shift = (1/sqrt(2)) * fTimeJitterMean * sqrt( -2. * log((float)rand() / ((float)RAND_MAX + 1.))) * sin( 2. * kPi * ((float)rand() / ((float)RAND_MAX + 1.)) );
143  }
144  // loop through channels
145  for (Station::ChannelIterator cIt = station.ChannelsBegin();
146  cIt != station.ChannelsEnd(); ++cIt) {
147  Channel& channel = *cIt;
148  if (!channel.IsActive())
149  continue;
150 
151  // Read the frequency spectrum of this channel
153  channel.GetChannelFrequencySpectrum();
154 
155  // loop through beacon frequencies and find corresponding frequency in the spectrum
156  for (ChannelTimeSeries::SizeType i=0; i < fBeaconFrequencies.size(); ++i) {
157 
158  //Calculate the distance in wavelength and the corresponding phase at the station
159  double n_wav = ((fBeaconFrequencies[i]/hertz)*distance)/300000000. ;
160  int result = static_cast<int>(n_wav/1.);
161  double mod = n_wav - static_cast<double>(result);
162  double phase = mod * kTwoPi;
163 // double amplitude = 10000. * pow(distance/1000, -4);
164  double amplitude = fAmplitude * pow(distance/1000, -2);
165 
166  fMessage.str("");
167  fMessage << "Beacon frequency: " << fBeaconFrequencies[i]/megahertz << " MHz"
168  << " Phase: " << phase << " Amplitude: " << amplitude;
169  INFO(fMessage.str());
170 
171  // check if frequency is inside the range (take care that the order of the spectrum is unknown)
172  if( (fBeaconFrequencies[i] < min(channel.GetFrequencyOfBin(0),channel.GetFrequencyOfBin(spectrum.GetSize()-1)))
173  || (fBeaconFrequencies[i] > max(channel.GetFrequencyOfBin(0),channel.GetFrequencyOfBin(spectrum.GetSize()-1))) ) {
174  WARNING("Beacon frequency must be inside of the spectrum!");
175  return eContinueLoop;
176  }
177 
178  // find nearest frequency bin:
179  // bin = (f_beacon-f_0) * #lastBin / (f_1 - f_0)
180  double bin = (fBeaconFrequencies[i]-channel.GetFrequencyOfBin(0)) * (spectrum.GetSize()-1)
181  / (channel.GetFrequencyOfBin(spectrum.GetSize()-1) - channel.GetFrequencyOfBin(0));
182 
183  //Sum up calculated amplitude and original amplitude and set bin
184  spectrum[bin] += amplitude * exp(complex<double>(0,1)*phase);
185 
186  // get the phase and amplitude of the bin
187  phase = 0;
188  amplitude = 0;
189 
190  phase = arg(spectrum[round(bin)]);
191  amplitude = abs(spectrum[round(bin)]);
192  }
193 
194  if(fOutputShiftFile != "") {
195  if ( channel.GetId() == 1 )
196  {
197  outputfile << shift << endl;
198  }
199  }
200  //Shift the trace of the channel
201  FFTDataContainerAlgorithm::ShiftTimeSeries(channel.GetFFTDataContainer(), shift);
202  }
203  }
204  if(fOutputShiftFile != "")
205  outputfile.close();
206  return eSuccess;
207  }
208 
210  RdChannelBeaconSimulator::Finish()
211  {
212  // Put any termination or cleanup code here.
213  // This method is called once at the end of the run.
214 
215  INFO("RdChannelBeaconSimulator::Finish()");
216 
217  return eSuccess;
218  }
219 
220 }
ChannelFFTDataContainer & GetFFTDataContainer()
retrieve Channel FFTDataContainer (write access)
Branch GetTopBranch() const
Definition: Branch.cc:63
bool IsActive() const
Point object.
Definition: Point.h:32
int GetId() const
Return Id of the Channel.
Report success to RunController.
Definition: VModule.h:62
Detector description interface for Station-related data.
Interface class to access to the Radio part of an event.
Definition: REvent.h:42
Skip remaining modules in the current loop and continue with next iteration of the loop...
Definition: VModule.h:68
const double meter
Definition: GalacticUnits.h:29
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
string ToString(const G &g, const CoordinateSystemPtr cs)
string ToString(const G &g, const CoordinateSystemPtr cs)
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
ChannelIterator ChannelsBegin()
begin Channel iterator for read/write
double pow(const double x, const unsigned int i)
boost::filter_iterator< StationFilter, AllStationIterator > StationIterator
Iterator over all (non-exculded) stations.
Definition: REvent.h:125
bool HasREvent() const
ChannelIterator ChannelsEnd()
end Channel iterator for read/write
Detector description interface for RDetector-related data.
Definition: RDetector.h:46
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
#define max(a, b)
Class representing a document branch.
Definition: Branch.h:107
class to hold data at the radio Station level.
std::vector< double >::size_type SizeType
Definition: Trace.h:58
constexpr double kPi
Definition: MathConstants.h:24
double abs(const SVector< n, T > &v)
const Data result[]
Top of the hierarchy of the detector description interface.
Definition: Detector.h:81
constexpr double kTwoPi
Definition: MathConstants.h:27
constexpr double megahertz
Definition: AugerUnits.h:155
constexpr double g
Definition: AugerUnits.h:200
int GetId() const
Station ID.
SizeType GetSize() const
Definition: Trace.h:156
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
std::string fOutputShiftFile
Name of file for ASCII output of shifts (not needed for normal analysis)
constexpr double hertz
Definition: AugerUnits.h:153
std::vector< double > fBeaconFrequencies
Vector of frequencies to simulate.
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
utl::CoordinateSystemPtr GetReferenceCoordinateSystem() const
Get the reference coordinate system used for analysis (usually PampaAmarilla for Auger) ...
Definition: Detector.h:141
ChannelFrequencySpectrum & GetChannelFrequencySpectrum()
retrieve Channel Frequency Spectrum (write access, only use this if you intend to change the data) ...
double GetFrequencyOfBin(const ChannelFrequencySpectrum::SizeType bin) const
Get the frequency corresponding to a bin of the frequency spectrum.
double fTimeJitterMean
Mean of gaussian distibution for the time jitter.
Vector object.
Definition: Vector.h:30
Class that holds the data associated to an individual radio channel.
const rdet::RDetector & GetRDetector() const
Definition: Detector.cc:143
double mod(const double d, const double periode)
utl::Point GetPosition() const
Tank position in Site Cartesian Coordinates.
const Station & GetStation(const int stationId) const
Get station by Station Id.
Definition: RDetector.cc:141
std::vector< double > fBeaconPosition
Vector for the position of the beacon.

, generated on Tue Sep 26 2023.