17 #include <fwk/CentralConfig.h>
19 #include <utl/config.h>
20 #include <utl/ErrorLogger.h>
21 #include <utl/Reader.h>
22 #include <utl/HannWindow.h>
23 #include <utl/StringCompare.h>
24 #include <utl/AugerException.h>
26 #include <evt/Event.h>
27 #include <evt/ShowerRRecDataQuantities.h>
28 #include <revt/StationRecData.h>
29 #include <revt/REvent.h>
30 #include <revt/Header.h>
31 #include <revt/Channel.h>
32 #include <revt/Station.h>
35 #define OUT(x) if ((x) <= fInfoLevel) cerr << " "
45 SampleData::HelpOutput() {
46 ostringstream helpoutput;
47 helpoutput <<
"Useful values are:\n"
48 <<
" fBinning / ns = " << fBinning /
nanosecond <<
"\n"
49 <<
"All following units are samples: \n"
50 <<
" fNumCoefficients = " << fNumCoefficients <<
"\n"
51 <<
" fDelayLine = " << fDelayLine <<
"\n"
52 <<
" fTrainRegionStart = " << fTrainRegionStart <<
"\n"
53 <<
" fTrainRegionStop = " << fTrainRegionStop <<
"\n"
54 <<
" fMinimumTrainRegionLength = " << fMinimumTrainRegionLength <<
"\n"
55 <<
" fTraceLength = " << fTraceLength <<
"\n"
56 <<
"Occupied samples are the samples that can't be used because they are "
57 "in/close to the SignalSearchWindow or the NoiseWindow \n"
58 <<
" fOccupiedRegion1Start = " << fOccupiedRegion1Start <<
"\n"
59 <<
" fOccupiedRegion1Stop = " << fOccupiedRegion1Stop <<
"\n"
60 <<
" fOccupiedRegion2Start = " << fOccupiedRegion2Start <<
"\n"
61 <<
" fOccupiedRegion2Stop = " << fOccupiedRegion2Stop <<
"\n"
62 <<
" fTrainRegionLength = " << fTrainRegionLength <<
"\n";
63 return helpoutput.str();
68 RdChannelLinearPredictorRFISuppressor::TranslateTimeToSample(
double time,
double binning,
char const *name) {
69 const double floatSamples = time / binning;
70 const int signedintSamples = round(time/binning);
71 const unsigned int intSamples = signedintSamples > 0 ?
abs(signedintSamples) : 0;
72 if ( fabs(floatSamples - intSamples) > 1E-10) {
73 ostringstream warning;
74 warning <<
"The configuration variable " << name <<
" with value "
75 << time /
nanosecond <<
"ns doesn't seem to be a multiple of the time-binning ("
76 << binning /
nanosecond <<
"ns) of this trace. Rounding to the nearest value of "
77 << intSamples <<
"samples. This is not necessarily a problem.";
85 RdChannelLinearPredictorRFISuppressor::TestOverlap(
int a1,
int a2,
int b1,
int b2) {
87 if (a1 > b1 && a1 < b2)
89 if (a2 > b1 && a2 < b2)
91 if (b1 > a1 && b1 < a2)
93 if (b2 > a1 && b2 < a2)
102 INFO(
"RdChannelLinearPredictorRFISuppressor::Init()");
106 CentralConfig::GetInstance()->
GetTopBranch(
"RdChannelLinearPredictorRFISuppressor");
109 topBranch.
GetChild(
"TrainRegionStartOverride").
GetData(fTrainRegionStartOverride);
110 topBranch.
GetChild(
"TrainRegionStopOverride").
GetData(fTrainRegionStopOverride);
111 topBranch.
GetChild(
"MinimumTrainRegionLength").
GetData(fMinimumTrainRegionLength);
118 if ( !( fDelayLine + fSegmentLength < (fTrainRegionStopOverride - fTrainRegionStartOverride)*2 ) ) {
120 "samples than those that are spanned by the coefficients and the delay line.");
132 samples.
fDelayLine = TranslateTimeToSample(fDelayLine, samples.
fBinning,
"DelayLine");
141 double NoiseWindowStart = recStation.
GetParameter(eNoiseWindowStart) - fWindowMargins;
142 double NoiseWindowStop = recStation.
GetParameter(eNoiseWindowStop) + fWindowMargins;
143 double SignalSearchWindowStart = recStation.
GetParameter(eSignalSearchWindowStart) - fWindowMargins;
144 double SignalSearchWindowStop = recStation.
GetParameter(eSignalSearchWindowStop) + fWindowMargins;
145 if ( NoiseWindowStart < SignalSearchWindowStart ) {
159 unsigned int Range1Start = timeseries.
GetStart();
164 unsigned int Range3Stop = timeseries.
GetStop() + 1;
165 int Range1Length = int(Range1Stop) - int(Range1Start);
166 int Range2Length = int(Range2Stop) - int(Range2Start);
167 int Range3Length = int(Range3Stop) - int(Range3Start);
168 if ((Range1Length > Range2Length) && (Range1Length > Range3Length)) {
171 }
else if ((Range2Length > Range1Length) && (Range2Length > Range3Length)) {
191 error <<
"There is overlap of the TrainRegion with the SignalSearchWindow or the SignalNoiseWindow "
192 "Maybe you have set the train-region to a wrong manual value?). Here are some values to help you" << endl
200 error <<
"The number of samples in the train region (" << samples.
fTrainRegionLength <<
201 ") is lower than the minimum number of train-region samples (" <<
211 ") is lower than the lowest valid sample of the trace (" <<
212 timeseries.
GetStart() <<
"). Here are some values to help you:" << endl
220 error <<
"Highest sample of the noise region (" << samples.
fTrainRegionStop - 1 <<
221 ") is higher than the highest valid sample of the trace (" <<
222 timeseries.
GetStop() <<
"). Here are some values to help you:" << endl
229 ERROR(
"Something bad is going on. The traces that I am asked to work with have "
230 "different binning. I can't work with that.");
235 WARNING(
"Something bad might be going on. The traces of one station not have the "
236 "same length. I am adujsting the total length to the shortest trace.");
246 error <<
"This is not good: based on this sampling "
247 "the train-region does not contain more samples than those that are spanned "
248 "by the coefficients and the delay line. It would be impossible to calculate "
249 "the covariances and the coefficients. Here are some values to help you:" << endl
259 RdChannelLinearPredictorRFISuppressor::Run(
evt::Event& event)
261 INFO(
"RdChannelLinearPredictorRFISuppressor::Run()");
265 WARNING(
"No radio event found!");
266 return eContinueLoop;
269 REvent& rEvent =
event.GetREvent();
273 for (
auto& station : rEvent.StationsRange()) {
275 unsigned int countActiveChannels = 0;
280 for (
auto& channel : station.ChannelsRange()) {
282 if (!channel.IsActive())
286 if (countActiveChannels == 0) {
290 FillSampleDataWithRelevantValuesForFirstChannel(samples, timeseries, station);
297 VModule::ResultFlag test_samples_result = TestSampleDataForConsistency(samples, timeseries);
298 if (test_samples_result !=
eSuccess) {
299 return test_samples_result;
301 ++countActiveChannels;
303 if ( countActiveChannels == 0 ) {
304 WARNING(
"No active channels were found. Skipping.");
305 return eContinueLoop;
307 if ( countActiveChannels != fNumChannels && fNumChannels != 0 ) {
309 error <<
"The value of fNumChannels (" << fNumChannels <<
") is not the same as the number of active channels ("
310 << countActiveChannels <<
")" << endl;
317 <<
" and station with Id=" << station.GetId() <<
"---" << endl;
318 OUT(2) <<
"Setting up the linear predictor with:" << endl
320 <<
" - number of coefficients: " << samples.
fNumCoefficients <<
" samples" << endl
321 <<
" - delay line : " << samples.
fDelayLine <<
" samples" << endl
322 <<
" - total trace length : " << samples.
fTotalTraceLength <<
" samples" << endl;
327 unsigned int channelNum = 0;
328 vector< vector<double> > noiseRegion(countActiveChannels, vector<double>(samples.
fTrainRegionLength));
329 vector< vector<double> > predictRegion(countActiveChannels, vector<double>(samples.
fTraceLength));
331 for (
auto& channel : station.ChannelsRange()) {
333 if (!channel.IsActive())
340 for (
unsigned int index = 0; index < samples.
fTraceLength; ++index) {
341 predictRegion[channelNum][index] = timeseries[index];
350 OUT(2) <<
"Training the linear predictor..." << endl;
351 lp.
train(noiseRegion, fFudgeFactor);
352 vector< vector<double> > prediction(lp.
predict(predictRegion));
354 OUT(2) <<
"Subtracting the predicted signal from the traces..." << endl;
359 for (
auto& channel : station.ChannelsRange()) {
361 if (!channel.IsActive())
365 for (
unsigned int index = 0; index < samples.
fTraceLength; ++index) {
367 timeseries[index] = 0;
369 timeseries[index] -= prediction[channelNum][index];
379 RdChannelLinearPredictorRFISuppressor::Finish()
384 INFO(
"RdChannelLinearPredictorRFISuppressor::Finish()");
Branch GetTopBranch() const
unsigned int fOccupiedRegion2Start
Class to access station level reconstructed data.
std::vector< std::vector< double > > predict(std::vector< std::vector< double > > const &channels) const
unsigned int fTrainRegionStart
StationRecData & GetRecData()
Get station level reconstructed data.
SizeType GetStop() const
Get valid data stop bin.
unsigned int fTraceLength
unsigned int fTotalTraceLength
Interface class to access to the Radio part of an event.
Base class for exceptions arising because configuration data are not valid.
double GetBinning() const
size of one slot
#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.
#define INFOIntermediate(y)
Class representing a document branch.
unsigned int fTrainRegionStop
class to hold data at the radio Station level.
constexpr double nanosecond
double abs(const SVector< n, T > &v)
Header & GetHeader()
access to REvent Header
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
Linear Predictor. Trains on noise with RFI, predicts RFI given other noisy signal.
unsigned int fMinimumTrainRegionLength
double GetParameter(const Parameter i) const
void train(std::vector< std::vector< double > > const &channels, double fudgeFactor)
ResultFlag
Flag returned by module methods to the RunController.
SizeType GetStart() const
Get valid data start bin.
unsigned int fOccupiedRegion1Stop
unsigned int fNumCoefficients
unsigned int fOccupiedRegion2Stop
#define ERROR(message)
Macro for logging error messages.
unsigned int fOccupiedRegion1Start