2 #include <fwk/CentralConfig.h>
3 #include <utl/ErrorLogger.h>
6 #include <revt/REvent.h>
7 #include <revt/Header.h>
8 #include <revt/Station.h>
9 #include <revt/Channel.h>
10 #include <revt/StationRecData.h>
12 #include <utl/Trace.h>
13 #include <utl/TraceAlgorithm.h>
14 #include <utl/Reader.h>
15 #include <utl/config.h>
16 #include <utl/AugerUnits.h>
17 #include <utl/MathConstants.h>
18 #include <utl/TimeStamp.h>
19 #include <utl/TimeInterval.h>
20 #include <utl/FFTDataContainerAlgorithm.h>
21 #include <utl/AugerException.h>
23 #include <det/Detector.h>
24 #include <rdet/RDetector.h>
35 #include <mysql/mysql.h>
46 fBeaconFrequencies(vector<double>()),
47 fOutputRefPhaseFile(
""),
49 fReferenceStation(145),
51 fStationWithZeroPhase(101),
52 fConvergenceLimit(0.001),
54 fMaxNumberEvents(true),
55 fPhaseDifferences(std::map<double ,std::map<int ,vector<double> > >() )
60 CalcBeaconRefPhase::~CalcBeaconRefPhase()
74 INFO(
"CalcBeaconRefPhase::Init()");
78 CentralConfig::GetInstance()->
GetTopBranch(
"CalcBeaconRefPhase");
80 if (fInfoLevel < eNone || fInfoLevel >
eDebug) {
81 ERROR(
"<infoLevel> out of bounds");
100 stringstream message;
101 message.str(
"CalcBeaconRefPhase::Run()");
107 fBeaconFrequencies = det::Detector::GetInstance().GetRDetector().GetBeaconFrequencies();
112 std::map< double, std::map<int, double> > phases = map< double, map<int, double> >();
117 WARNING(
"PhaseFinder::No radio event found!");
121 REvent& rEvent =
event.GetREvent();
129 map<double, double> refStationPhases = map<double, double> ();
135 if ( ((refSpectrum.
GetSize()-1) % 1024) != 0 ) {
137 message <<
"Trace of reference station " <<
fReferenceStation <<
" must consist of 2048 samples or an integer multiple of it!";
138 ERROR(message.str());
142 for (
unsigned int beaconFreqN=0; beaconFreqN <
fBeaconFrequencies.size(); ++beaconFreqN) {
144 if( (
fBeaconFrequencies[beaconFreqN] < min(refChannel.GetFrequencyOfBin(0),refChannel.GetFrequencyOfBin(refSpectrum.
GetSize()-1)))
146 WARNING(
"Beacon frequency must be inside of the spectrum!");
153 / (refChannel.GetFrequencyOfBin(refSpectrum.
GetSize()-1) - refChannel.GetFrequencyOfBin(0));
176 message <<
"Calculating the phases of the beacon frequencies for channel "
187 if ( ((refSpectrum.
GetSize()-1) % 1024) != 0 ) {
189 message <<
"Trace of station " << station.
GetId() <<
" does not consist of 2048 samples or an integer multiple of it!"
190 <<
"It has " << (spectrum.
GetSize()-1)*2 <<
" samples.";
202 for (
unsigned int beaconFreqN=0; beaconFreqN <
fBeaconFrequencies.size(); ++beaconFreqN) {
205 message <<
"Beacon frequency: "
211 message <<
"Spectrum reaches from "
215 <<
" MHz, binning = "
224 WARNING(
"Beacon frequency must be inside of the spectrum!");
234 double phase = arg(spectrum[round(bin)]);
238 message <<
"Looking at frequency: "
241 <<
"Phase: " << phase;
261 CalcBeaconRefPhase::Finish()
263 stringstream message;
267 INFO(
"CalcBeaconRefPhase::Finish()");
269 INFO(
"Calculating reference phases...");
273 message <<
"\nPlease check manually, if the beacon frequency IDs are correct (i.e. the same as in the database):\n";
274 for (
unsigned int beaconFreqN = 0; beaconFreqN <
fBeaconFrequencies.size(); ++beaconFreqN)
282 for (
unsigned int beaconFreqN=0; beaconFreqN <
fBeaconFrequencies.size(); ++beaconFreqN) {
292 ERROR(message.str());
298 for (map<
int, pair<double, double> >::iterator it = referencePhases.begin(); it != referencePhases.end() ; ++it) {
300 it->second.first -= zeroPhaseStationPhase;
302 while ( it->second.first >
kPi ) {
303 it->second.first -=
kTwoPi;
305 while ( it->second.first < -
kPi ) {
306 it->second.first +=
kTwoPi;
326 message <<
"Station ID \t #events";
328 message <<
" \t" << *it/
megahertz <<
" MHz";
337 for (
unsigned int beaconFreqN=0; beaconFreqN <
fBeaconFrequencies.size(); ++beaconFreqN) {
362 cout << station.
GetId() <<
": \t" << stationTimeStamp << endl;
363 if (stationTimeStamp != firstTimeStamp) {
369 FFTDataContainerAlgorithm::ShiftTimeSeries(channel.
GetFFTDataContainer(), stationTimeStamp-firstTimeStamp);
377 map<int, pair<double, double> >
378 CalcBeaconRefPhase::calculateReferencePhases(std::map<
int,std::vector<double> > phaseDifferences)
380 map<int, pair<double, double> > referencePhases;
383 for(map<
int, vector<double> >::iterator it=phaseDifferences.begin(); it!=phaseDifferences.end(); ++it) {
385 double totalShift = 0.;
392 double phasesum = 0.;
397 for(vector<double>::iterator vpos=it->second.begin(); vpos!=it->second.end(); ++vpos) {
398 *vpos = *vpos - mean;
401 }
else if ( *vpos < -
kPi ) {
405 rmssum += (*vpos * *vpos);
407 mean = phasesum/double(it->second.size());
408 rms =
sqrt(rmssum/
double(it->second.size()));
415 while ( totalShift >
kPi ) {
418 while ( totalShift < -
kPi ) {
423 referencePhases[it->first] = pair<double,double>(totalShift, rms);
426 return referencePhases;
431 CalcBeaconRefPhase::writeFileForDatabase(
void)
433 string startDate =
"2012-08-15";
436 stringstream outFilenameStringStream;
438 string outFilename(outFilenameStringStream.str());
440 stringstream message;
442 message <<
"\nWriting script for database to file: " << outFilename;
446 outFile.open(outFilename.c_str());
450 for (
unsigned int nBeaconFreq = 0; nBeaconFreq <
fBeaconFrequencies.size(); ++nBeaconFreq) {
451 outFile <<
"update BeaconRefPhase set Stop='" << startDate <<
"' where BeaconFreq_id=" << nBeaconFreq+1
452 <<
" and RStationId=" << *it <<
" and Stop>'" << startDate <<
"';" << endl;
453 outFile <<
"INSERT INTO BeaconRefPhase (BeaconFreq_id, RStationId, RefPhase, Start) VALUES ('"
454 << nBeaconFreq+1 <<
"', '" << *it <<
"', '" <<
fReferencePhases[nBeaconFreq][*it].first
455 <<
"', '" << startDate <<
"' );" << endl;
465 CalcBeaconRefPhase::writeReferencePhasesFile(
void)
468 stringstream outFilenameStringStream;
470 string outFilename(outFilenameStringStream.str());
472 stringstream message;
474 message <<
"\nWriting reference phases to file: " << outFilename;
478 outFile.open(outFilename.c_str());
481 outFile <<
"Station";
482 for (
unsigned int beaconFreqN = 0; beaconFreqN <
fBeaconFrequencies.size(); ++beaconFreqN)
489 for (
unsigned int nBeaconFreq = 0; nBeaconFreq <
fBeaconFrequencies.size(); ++nBeaconFreq) {
ChannelFFTDataContainer & GetFFTDataContainer()
retrieve Channel FFTDataContainer (write access)
Branch GetTopBranch() const
std::vector< std::map< int, std::pair< double, double > > > fReferencePhases
Internal storage of reference phases: VectorCounter:,nBeaconFreq, Key: StationID, Value: RefPhase...
boost::filter_iterator< CandidateStationFilter, AllStationIterator > CandidateStationIterator
Iterator over all CandidateStations (i.e., HasSignal, HasNoSignal)
int GetId() const
Return Id of the Channel.
Report success to RunController.
std::map< int, int > fEventsPerStation
Number of events read in for each station.
StationRecData & GetRecData()
Get station level reconstructed data.
CandidateStationIterator CandidateStationsEnd()
Interface class to access to the Radio part of an event.
Skip remaining modules in the current loop and continue with next iteration of the loop...
int fReferenceStation
ID of reference station.
std::map< int, std::pair< double, double > > calculateReferencePhases(std::map< int, std::vector< double > >)
Method for calculation of reference phase difference and the corresponding RMS for each station...
double GetBinning() const
size of one slot
double fConvergenceLimit
Convergence limit for iterative calculation of reference phases.
#define INFO(message)
Macro for logging informational messages.
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
ChannelIterator ChannelsBegin()
begin Channel iterator for read/write
std::string fDBServer
Name of the mysql db server.
Station & GetStation(const int stationId)
retrieve station by id throw utl::NonExistentComponentException if n.a.
int GetStationId() const
Return Id of the station to which this Channel belongs.
void writeReferencePhasesFile(void)
Write reference phases into ASCII file.
ChannelIterator ChannelsEnd()
end Channel iterator for read/write
void matchStationTimeStamps(revt::REvent &) const
Class representing a document branch.
Break current loop. It works for nested loops too!
class to hold data at the radio Station level.
double abs(const SVector< n, T > &v)
unsigned int fMaxNumberEvents
maximum number of events, after which module sequence is stopped, and reference phases are calculated...
std::set< unsigned int > fExistingStationIDs
Set of all station IDs present in the reference phases.
std::map< double,std::map< int,std::vector< double > > > fPhaseDifferences
phase differences: BeaconFreq, Station, vector of individual phase differences (one per event) ...
CandidateStationIterator CandidateStationsBegin()
constexpr double megahertz
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
int GetId() const
Get the station Id.
std::string fOutputRefPhaseFile
Name of file for ASCII output of phases (not needed for normal analysis)
int fReferenceChannel
Reference channel (ususally 1 = NS, high gain)
std::vector< double > fBeaconFrequencies
Vector of frequencies emitted by the beacon.
double GetParameter(const Parameter i) const
Channel & GetChannel(const int pmtId)
Retrieve a Channel by Id.
ResultFlag
Flag returned by module methods to the RunController.
int fInfoLevel
xml settings: info level (verbosity)
ChannelFrequencySpectrum & GetChannelFrequencySpectrum()
retrieve Channel Frequency Spectrum (write access, only use this if you intend to change the data) ...
unsigned int fEventCounter
Counts "successfull" events usable for reference phases.
double GetFrequencyOfBin(const ChannelFrequencySpectrum::SizeType bin) const
Get the frequency corresponding to a bin of the frequency spectrum.
void SetParameter(Parameter i, double value, bool lock=true)
Class that holds the data associated to an individual radio channel.
Report failure to RunController, causing RunController to terminate execution.
unsigned int fStationWithZeroPhase
Station which is set to a reference phase of 0 (exploits freedom of global constant) ...
#define ERROR(message)
Macro for logging error messages.
void writeFileForDatabase(void)
Write script to transfer reference phases into database.