4 #include <fwk/CentralConfig.h>
7 #include <utl/ErrorLogger.h>
8 #include <utl/TimeStamp.h>
9 #include <utl/TraceAlgorithm.h>
10 #include <utl/UTCDateTime.h>
11 #include <utl/Minou.h>
13 #include <evt/Event.h>
16 #include <revt/REvent.h>
17 #include <revt/Header.h>
18 #include <revt/Station.h>
21 #include <rdet/RDetector.h>
22 #include <rdet/Channel.h>
33 #include <sys/types.h>
36 #include <boost/algorithm/string.hpp>
47 REvent* RdGalacticDatasetMaker::fgCurrentREvent;
61 ffilteredDebugData(false),
62 fchannels(vector<int>()),
66 ftraceCutBinLength(0),
67 fsamplingFrequency4Header(0),
74 INFO(
"RdGalacticDatasetMaker::Init()");
80 CentralConfig::GetInstance()->
GetTopBranch(
"RdGalacticDatasetMaker");
87 string fdayStackStr =
"";
90 string filteredDebugDataStr =
"";
99 string fimpedanceFileStr =
"";
103 vector<int> StationIDlist_dummy;
106 StationIDlist_dummy.
push_back(stoi(stationB.GetAttributes().find(
"id")->second));
109 fStationIds = vector<int>(StationIDlist_dummy.begin(), StationIDlist_dummy.end());
111 for (
unsigned int i = 0; i <
fStationIds.size(); ++i) {
120 WARNING(
"No impedance file selected. Impedance will be set to 1 Ohms for all frequencies.");
123 info <<
"Settings: Frequency bin width: " <<
ffreqBinWidth <<
"MHz" <<
"; Time bin width: "
126 <<
" MaxFrequency: " <<
fMaxFrequency <<
" MHz" <<
"; Start bin: " <<
fstartBin <<
"; Total bins to take from time trace: "
134 for (
float currentTimeBin = 0; currentTimeBin <= 24; currentTimeBin +=
ftimeBinWidth){
143 vector<vector<vector<vector<float>>>> AllChannelsAndStationsDatasets_dummy(
fchannels.size(), vector<vector<vector<float>>>
147 vector<vector<float> > stationTraceCounter_dummy(
fStationIds.size(), vector<float>(2,0));
150 vector<vector<vector<vector<float>>>> (0, vector<vector<vector<float>>>
151 (0, vector<vector<float>>(0, vector<float>(0)) )).swap(AllChannelsAndStationsDatasets_dummy);
169 if (mkdir(
"datasets", 0777) == -1){
170 WARNING(
"Directory for datasets cannot be created, maybe it exists already?");
173 info <<
"Directory for datasets has been created";
180 if (mkdir(
"debug", 0777) == -1){
181 WARNING(
"Directory for debug data cannot be created, maybe it exists already?");
184 info <<
"Directory for debug has been created";
190 for (
unsigned int i=0; i<
fStationIds.size(); ++i) {
192 for (
unsigned int j=0; j<
fchannels.size(); ++j) {
195 ofstream tracePropertiesDebugFile;
196 tracePropertiesDebugFile.open
199 tracePropertiesDebugFile <<
"event" <<
" " <<
"station" <<
" " <<
"channel" <<
" gpsTime"
200 <<
" UTChour" <<
" sidTime" <<
" STD" <<
" absMaxPeakADC" <<endl;
201 tracePropertiesDebugFile.close();
206 ofstream ADCtraceFile;
210 ADCtraceFile <<
"event" <<
" " <<
"station" <<
" " <<
"channel" <<
" gpsTime" <<
" UTChour" <<
" sidTime" ;
212 ADCtraceFile <<
" " << k;
214 ADCtraceFile << endl;
215 ADCtraceFile.close();
218 ofstream spectrumTraceFile;
219 spectrumTraceFile.open
222 spectrumTraceFile <<
"event" <<
" " <<
"station" <<
" " <<
"channel" <<
" gpsTime" <<
" UTChour" <<
" sidTime";
223 for (
int k=
fstartBin; k<(ftraceCutBinLength/2+1); ++k) {
226 spectrumTraceFile << endl;
227 spectrumTraceFile.close();
241 ERROR(
"Read event without radio part");
247 REvent& rEvent =
event.GetREvent();
261 bool filterPass =
true;
264 vector<vector<float>> powerDepositedInFreqBins(
fchannels.size(), vector<float>(
freqBins.size()-1, 0.0));
265 vector<vector<float>> cachedSpectra(
fchannels.size(), vector<float>());
266 vector<vector<float>> cachedADCtraces(
fchannels.size(), vector<float>());
267 vector<vector<float>> cachedSTD(
fchannels.size(), vector<float>());
268 vector<vector<float>> cachedTraceADCmax(
fchannels.size(), vector<float>());
271 const rdet::Station& detStation = det::Detector::GetInstance().GetRDetector().GetStation(station);
300 info <<
"BEFORE cutting: trace size: " << trace.
GetSize() <<
"; ADC trace size: " << ADCtrace.
GetSize()
301 <<
"; Spectrum should be half+1 of that" <<
"; The length of the time trace is: "<< T <<
"ns" <<endl;
314 for (
unsigned int i =
fstartBin; i < endBin; ++i) {
316 tempADCTimeSeries.
PushBack(ADCtrace[i]);
318 trace = tempTimeSeries;
319 ADCtrace = tempADCTimeSeries;
321 trace = cIt->GetChannelTimeSeries();
331 info <<
"AFTER cutting: trace size: " << trace.
GetSize() <<
"; ADC trace size: " << ADCtrace.
GetSize()
332 <<
"; Spectrum size: " << spectrum.
GetSize() <<
"; The length of the time trace is: "<< T <<
"ns" <<endl;
336 info <<
"Trace size: " << trace.
GetSize() <<
"; Trace bin length (ns): " << trace.
GetBinning()
337 <<
"Spectrum bin size (GHz): " << spectrum.
GetBinning() <<
"; Trace time length (nano sec): " << T << endl;
339 <<
"; HHMMSS: " <<
fHHMMSS <<
"; Converted to UTC hour: " <<
fCurUTChour <<
"; Spectrum.GetSize(): "
340 << spectrum.
GetSize() <<
"; Sampling rate from XML: " << samplingfreq <<
"MHz" << endl;
347 double ADCtraceMax = TraceAlgorithm::Max(ADCtrace, 0, trace.
GetSize()-1);
348 double ADCtraceMin = TraceAlgorithm::Min(ADCtrace, 0, trace.
GetSize()-1);
350 if (ADCtraceMax >=
abs(ADCtraceMin)){
351 ADCtraceAbsMax = ADCtraceMax;
353 if (ADCtraceMax <
abs(ADCtraceMin)){
354 ADCtraceAbsMax =
abs(ADCtraceMin);
358 if (filterPass ==
true){
366 info <<
"Threshold settings and quantity values in the Channel " << to_string(
fCurChannelId)
368 <<
"Properties of current trace::: abs Max: " << ADCtraceAbsMax <<
"; STD: " << ADCtraceSTD
369 <<
"; Min: " << ADCtraceMin <<
"; Max" << ADCtraceMax <<
"; Filterpass flag: " << filterPass << endl;
375 for (
unsigned j=0; j <
freqBins.size()-1; ++j){
376 for (
unsigned int i = 0; i < spectrum.
GetSize(); ++i){
380 WARNING(
"No impedance from file! Setting Z = 1 for all frequencies.");
385 info <<
"Interpolated impedance for frequency " << to_string(channel.
GetFrequencyOfBin(i)*1000)
386 <<
"in channel " << to_string(
fCurChannelId) <<
" is " << to_string(Z) <<
" This was done in bin interval: "
392 powerDepositedInFreqBins[channelIndex][j] += 2*(1/Z)*(1/T)*
abs(spectrum[i]*spectrum[i])*spectrum.
GetBinning();
400 cachedSTD[channelIndex].push_back(ADCtraceSTD);
401 cachedTraceADCmax[channelIndex].push_back(ADCtraceAbsMax);
404 for (
unsigned int j = spectrum.
GetStart(); j < spectrum.
GetSize(); ++j) {
405 cachedSpectra[channelIndex].push_back(
abs(spectrum[j]));
407 for (
unsigned int j = ADCtrace.
GetStart(); j < ADCtrace.
GetSize(); ++j) {
408 cachedADCtraces[channelIndex].push_back(ADCtrace[j]);
414 bool filterPass4debugData =
true;
416 if (filterPass ==
true){
417 filterPass4debugData =
true;
419 filterPass4debugData =
false;
424 if (filterPass4debugData ==
true) {
425 for (
unsigned int channelIndex=0; channelIndex <
fchannels.size(); ++channelIndex) {
428 ofstream tracePropertiesDebugFile;
429 tracePropertiesDebugFile.open
434 <<
" " << cachedSTD[channelIndex][0] <<
" " << cachedTraceADCmax[channelIndex][0] << endl;
435 tracePropertiesDebugFile.close();
440 ofstream spectrumTraceFile;
441 spectrumTraceFile.open
446 for (
unsigned int j = 0; j < cachedSpectra[channelIndex].size(); ++j) {
447 spectrumTraceFile <<
" " << cachedSpectra[channelIndex][j];
449 spectrumTraceFile <<
"\n";
450 spectrumTraceFile.close();
454 ofstream ADCtraceFile;
460 for (
unsigned int j = 0; j < cachedADCtraces[channelIndex].size(); j++) {
461 ADCtraceFile <<
" " << cachedADCtraces[channelIndex][j] ;
463 ADCtraceFile <<
"\n";
464 ADCtraceFile.close();
473 if (filterPass ==
true){
486 for (
unsigned int row = 0; row <
timeBins.size()-1; ++row) {
508 if (filterPass ==
true){
517 info <<
"Output of the cached dataset vector after reading the event: " << endl;
518 info <<
"*********************" << endl;
520 info <<
" Channel number:" <<
fchannels[chan] <<
" " << endl;
522 info <<
" Station number:" <<
fStationIds[st] <<
" " << endl;
531 info <<
"*********************" << endl;
540 RdGalacticDatasetMaker::Finish()
545 info <<
"Output of the whole final dataset vector: \n";
546 info <<
"*********************" << endl;
549 info <<
" Channel number:" <<
fchannels[chan] <<
" " << endl;
551 info <<
" Station number:" <<
fStationIds[st] <<
" " << endl;
560 info <<
"*********************" << endl;
566 vector<vector<vector<float>>>
567 AllChannelsAndStationsDatasets_average(
fchannels.size(), vector<vector<float>>(
timeBins.size()-1, vector<float>(
freqBins.size()+1)));
570 for (
unsigned int chan = 0; chan < AllChannelsAndStationsDatasets_average.size(); ++chan) {
571 for (
unsigned int row = 0; row < AllChannelsAndStationsDatasets_average[chan].size(); ++row) {
572 AllChannelsAndStationsDatasets_average[chan][row][0] =
timeBins[row];
577 vector<float> tempVector;
587 if (tempVector.size() != 0){
588 float tempAverage = accumulate(tempVector.begin(), tempVector.end(), 0.0) / tempVector.size();
589 AllChannelsAndStationsDatasets_average[chan][row][col] = tempAverage;
592 AllChannelsAndStationsDatasets_average[chan][row][col] = 0;
597 info <<
"Weight of the rows in each station and channel: \n";
598 info <<
"weight test: station:" <<
fStationIds[st] <<
"channel: "<<
fchannels[chan] <<
" row:"<<row<<
" "
613 INFO(
"Saving average of station datasets for each channel.");
615 string timeHeaderString;
617 timeHeaderString =
"UTChour";
619 timeHeaderString =
"LSTtime";
621 for (
unsigned int chan = 0; chan < AllChannelsAndStationsDatasets_average.size(); ++chan) {
622 ofstream AllChannelsAndStationsDatasets_average_FILE;
623 AllChannelsAndStationsDatasets_average_FILE.open(
"./datasets/dataSetFile_Ch"+to_string(
fchannels[chan])+
"_average.txt",
ios::out | ios::app);
624 AllChannelsAndStationsDatasets_average_FILE << std::setprecision(6) << fixed;
625 AllChannelsAndStationsDatasets_average_FILE << timeHeaderString << setw(16) <<
"weight";
626 for (
unsigned int i=0; i<
freqBins.size()-1; ++i){
627 AllChannelsAndStationsDatasets_average_FILE << setw(16) <<
freqBins[i];
629 AllChannelsAndStationsDatasets_average_FILE << endl;
631 cout <<
"\tStoring dataset. " << endl;
632 AllChannelsAndStationsDatasets_average_FILE << std::setprecision(9) << scientific;
633 for (
unsigned int row = 0; row < AllChannelsAndStationsDatasets_average[chan].size(); ++row) {
634 for (
unsigned int col = 0; col < AllChannelsAndStationsDatasets_average[chan][row].size(); ++col) {
635 AllChannelsAndStationsDatasets_average_FILE << AllChannelsAndStationsDatasets_average[chan][row][col] <<
"\t" ;
637 AllChannelsAndStationsDatasets_average_FILE << endl;
639 AllChannelsAndStationsDatasets_average_FILE.close();
644 info <<
"Output of the final averaged datasets for each channel: \n";
645 info <<
"*********************" << endl;
646 info <<
"**********************" << endl;
648 for (
unsigned int chan = 0; chan < AllChannelsAndStationsDatasets_average.size(); ++chan) {
649 info <<
"***channel: " <<
fchannels[chan] <<
" ***" << endl;
650 for (
unsigned int row=0; row < AllChannelsAndStationsDatasets_average[chan].size(); ++row) {
651 for (
unsigned int col=0; col < AllChannelsAndStationsDatasets_average[chan][row].size(); ++col) {
652 info << AllChannelsAndStationsDatasets_average[chan][row][col] <<
" ";
657 info <<
"\n" <<
"**********************" <<
"\n";
658 info <<
"\n" <<
"**********************" <<
"\n";
663 INFO(
"Saving station and channel datasets (separately).");
667 ofstream dataSetFileOutput;
668 dataSetFileOutput.open(
"./datasets/dataSetFile_Station"+to_string(
fStationIds[st])+
"_Ch"+to_string(
fchannels[chan])+
".txt",
ios::out | ios::app);
669 dataSetFileOutput << std::setprecision(6) << fixed;
670 dataSetFileOutput << timeHeaderString << setw(16) <<
"weight";
671 for (
unsigned int u=0; u<
freqBins.size()-1; ++u) {
672 dataSetFileOutput << setw(16) <<
freqBins[u];
674 dataSetFileOutput << endl;
675 dataSetFileOutput << std::setprecision(9) << scientific;
680 dataSetFileOutput << endl;
687 INFO(
"How many traces in each station passed the filtering conditions from the total number of traces that were in the station:");
697 RdGalacticDatasetMaker::onlineMean(
float oldMean,
float nextValue,
int n)
699 return oldMean + (nextValue-oldMean)/n;
703 RdGalacticDatasetMaker::getChannelIndex(vector<int> vector,
int element)
705 auto it = find(vector.begin(), vector.end(), element);
708 if (it != vector.end()) {
709 index = distance(vector.begin(), it);
711 ERROR(
"Channel number not found! \n");
720 RdGalacticDatasetMaker::GPSSecToLSTHourAuger(
const int gps)
722 const double J2000 = 2451545.0;
723 const float Dtohr = 0.066666666666666;
724 const time_t utc = gps + 315964800 - 18;
726 const tm*
const t = gmtime(&utc);
727 int year = t->tm_year + 1900;
728 int month = t->tm_mon + 1;
729 const int day = t->tm_mday;
730 const int hour = t->tm_hour;
731 const int min = t->tm_min;
732 const int sec = t->tm_sec;
740 const int a = int(floor(year / 100.));
741 const int b = 2 - a + a/4;
742 double jd = floor(365.25*(year + 4716)) + floor(30.6001*(month + 1)) + day + b - 1524.5;
744 const double fd = double(60*60*hour + 60*min + sec) / 86400;
747 static double sidereal_coeff[4] = { 280.46061837, 360.98564736629, 0.000387933, 38710000 };
748 const double dt = jd -
J2000;
749 const double dt0 = dt / 36525.;
751 sidereal_coeff[0] + dt * sidereal_coeff[1] +
752 dt0 * dt0 * (sidereal_coeff[2] - dt0 / sidereal_coeff[3]);
756 const double lst = fmod(theta + (360 - 69.25), 360.) *
Dtohr;
761 RdGalacticDatasetMaker::HHMMSS2hour(
float ffHHMMSS)
763 float fhour = floor(ffHHMMSS/10000);
764 float fminute = floor((ffHHMMSS - fhour*10000)/100);
765 float fsecond = ffHHMMSS - fhour*10000 - fminute*100;
766 return fhour + fminute/60 + fsecond/3600;
777 RdGalacticDatasetMaker::GetImpedanceAt(
Branch topBranch,
int channelID,
float frequencyMHz)
783 vector<float> frequency;
784 vector<float> impedance;
786 if (stoi(mainBranch.
GetAttributes().begin()->second) == channelID){
787 impBranch = mainBranch.
GetChild(
"impedance");
790 for (
unsigned int i = 0; i < frequency.size(); ++i)
791 returnData.
PushBack(frequency[i], impedance[i]);
792 return returnData.
Y(frequencyMHz/1000);
795 ERROR(
"Impedance for the channel not found!");
Branch GetTopBranch() const
static double GetImpedanceAt(utl::Branch topBranch, int channelID, float frequencyMHz)
double StandardDeviation(const std::vector< double > &v, const double mean)
std::vector< float > timeBins
int GetId() const
Return Id of the Channel.
Report success to RunController.
Detector description interface for Station-related data.
Interface class to access to the Radio part of an event.
Class to hold collection (x,y) points and provide interpolation between them.
double GetBinning() const
size of one slot
#define INFO(message)
Macro for logging informational messages.
StationIterator StationsEnd()
StationIterator StationsBegin()
void Init()
Initialise the registry.
vector< t2list > out
output of the algorithm: a list of clusters
Detector description interface for Channel-related data.
void PushBack(const double x, const double y)
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
static float HHMMSS2hour(float ffHHMMSS)
unsigned int TimeStamp2HHMMSS(const utl::TimeStamp ×t)
Convert a TimeStamp into an integer representing the time as HHMMSS.
ChannelIterator ChannelsBegin()
begin Channel iterator for read/write
boost::filter_iterator< StationFilter, AllStationIterator > StationIterator
Iterator over all (non-exculded) stations.
std::vector< float > freqBins
ChannelIterator ChannelsEnd()
end Channel iterator for read/write
static unsigned int TimeStamp2HHMMSS(const utl::TimeStamp ×t)
A TimeStamp holds GPS second and nanosecond for some event.
AttributeMap GetAttributes() const
Get a map<string, string> containing all the attributes of this Branch.
Branch GetNextSibling() const
Get next sibling of this branch.
std::vector< int > fStationIds
#define INFOIntermediate(y)
Class representing a document branch.
class to hold data at the radio Station level.
double fsamplingFrequency4Header
double abs(const SVector< n, T > &v)
Header & GetHeader()
access to REvent Header
std::vector< int > fchannels
std::vector< std::vector< std::vector< std::vector< float > > > > AllChannelsAndStationsDatasets
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
const Channel & GetChannel(const int id) const
Get specified Channel by id.
void SetBinning(const double binning)
ResultFlag
Flag returned by module methods to the RunController.
unsigned long GetGPSSecond() const
GPS second.
static double GPSSecToLSTHourAuger(const int gpsSec)
static revt::REvent * fgCurrentREvent
SizeType GetStart() const
Get valid data start bin.
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.
static int getChannelIndex(std::vector< int > vector, int element)
Class that holds the data associated to an individual radio channel.
utl::Branch impedanceTopBranch
Report failure to RunController, causing RunController to terminate execution.
std::vector< std::vector< float > > stationTraceCounter
Branch GetFirstChild() const
Get first child of this Branch.
static float onlineMean(float oldMean, float nextValue, int n)
double GetSamplingFrequency() const
Get sampling Frequency of ADC (unit?)
double Y(const double x) const
Get or interpolate the Y value that corresponds to parameter x.
void PushBack(const T &value)
Insert a single value at the end.
#define ERROR(message)
Macro for logging error messages.