3 #include <revt/REvent.h>
4 #include <revt/Header.h>
5 #include <revt/Station.h>
6 #include <revt/Channel.h>
8 #include <det/Detector.h>
9 #include <rdet/RDetector.h>
10 #include <rdet/Station.h>
12 #include <utl/Trace.h>
13 #include <utl/TraceAlgorithm.h>
14 #include <utl/ErrorLogger.h>
15 #include <utl/Reader.h>
16 #include <utl/config.h>
17 #include <utl/RandomEngine.h>
18 #include <utl/TimeStamp.h>
20 #include <fwk/RandomEngineRegistry.h>
21 #include <fwk/CentralConfig.h>
31 #include <CLHEP/Random/RandFlat.h>
65 std::ostringstream info;
66 info <<
"\n\t Reject stations without noise information (or try to find matching station with noise information): "
69 return (RdChannelNoiseImporter_AERA::checkSetup());
78 WARNING(
"RdChannelNoiseImporter_AERA::No radio event found!");
87 std::ostringstream sstr;
93 std::string NoiseImportfilename =
GetNoiseFileName(eventTime, noiseTimeStamp);
96 if (NoiseImportfilename ==
"None") {
101 RadioFileIO* RadioFileIORead =
new RadioFileIO();
102 TFile*
const Readfile =
new TFile(NoiseImportfilename.c_str(),
"READ");
103 TTree*
const IOLibTree = (TTree*)Readfile->Get(
"AERAIOTree");
104 TBranch*
const IObranch = IOLibTree->GetBranch(
"AERAIO");
105 TBranch*
const evtSecBranch = IOLibTree->GetBranch(
"vAERAevents.EVseconds");
106 IObranch->SetAddress(&RadioFileIORead);
107 unsigned int NumberEvents = IOLibTree->GetEntries();
108 std::map<int, int> IDtoNumber;
112 const int EvtNum =
GetNoiseEventNumber(event, RadioFileIORead, evtSecBranch, IOLibTree, NumberEvents, eventTime);
116 ERROR(
"No noise event selected. Can not continue with import procedure. Skipping event!");
121 IOLibTree->GetEntry(EvtNum);
122 AERAevent*
const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
125 noiseTimeStamp.
SetGPSTime(aeraevent->getAeraEventSeconds(), 0);
126 det.
Update(noiseTimeStamp);
130 sstr <<
"General info concerning the noise-event selection:\n\tGPSSecond of the chosen noise-event: "
131 << aeraevent->getAeraEventSeconds() <<
"\n\tGPSSecond of the processed event: "
132 << eventTime.GetTimeStamp().GetGPSSecond() <<
'\n';
139 const unsigned int noiseEvtNanoSec = aeraevent->getAeraEventNanoSeconds();
140 const unsigned int noiseEvtSec = aeraevent->getAeraEventSeconds();
141 const utl::TimeStamp noiseEvtTimestamp(noiseEvtSec, noiseEvtNanoSec);
143 std::ostringstream warning;
145 "The difference between the time of the estimated noise-event "
147 " minutes. Skipping event.";
155 for (
int i = 0; i < aeraevent->getAeraEventLsCount(); ++i) {
156 aerarootiostation::Station*
const station = RadioFileIORead->getAeraRadioFileEvent()->getAeraEventStation(i);
157 IDtoNumber.insert(std::pair<int, int>(station->getAeraStationLsID(), i));
164 for (
auto& station : rEvent.StationsRange()) {
165 const int stationID = station.GetId();
166 std::map<int, int>::iterator idIt;
169 bool usingReplacementStation;
173 short replacementChannelId[4] = { -1, -1, -1, -1 };
176 if ((idIt = IDtoNumber.find(stationID - 100)) == IDtoNumber.end()) {
179 sstr <<
"Did not find Station " << stationID <<
" in event number " << EvtNum <<
" of noise-file " <<
180 NoiseImportfilename <<
"---> Excluding station!";
186 sstr <<
"No noise information available for station " << stationID
187 <<
". Searching for other station to use its noise";
193 bool suitableStationFound =
false;
195 std::vector<int>::size_type n = aeraevent->getAeraEventLsCount();
196 std::vector<int> rootIds;
198 while (rootIds.size() < n) {
199 int randInt = CLHEP::RandFlat::shootInt(
fRandomEngine, 0, n);
200 if (std::find(rootIds.begin(), rootIds.end(), randInt) == rootIds.end()) {
201 rootIds.push_back(randInt);
205 for (
auto tmpRootID : rootIds) {
206 aerarootiostation::Station*
const rootIoReplacementStation =
207 RadioFileIORead->getAeraRadioFileEvent()->getAeraEventStation(tmpRootID);
209 const int tmpReplacementStationID = rootIoReplacementStation->getAeraStationLsID() + 100;
211 sstr <<
"checking station " << tmpReplacementStationID
212 <<
" if it is a suitable replacement (this corresponds to station number "
213 << tmpRootID <<
" in rootio file)";
217 if (std::find(stationIdVector.begin(), stationIdVector.end(),
218 tmpReplacementStationID) == stationIdVector.end()) {
220 sstr <<
"Station " << tmpReplacementStationID
221 <<
" is present in data but not in the detector description";
230 sstr <<
"Station " << tmpReplacementStationID <<
" is excluded for bad period reason: " << reason;
236 bool stationIsSuitable =
true;
239 for (
auto chIt = rDetector.
GetStation(station.GetId()).ChannelsBegin();
240 chIt != rDetector.
GetStation(station.GetId()).ChannelsEnd(); ++chIt) {
242 sstr <<
"checking channel " << chIt->
GetId();
244 if (!station.HasChannel(chIt->GetId())) {
248 const revt::Channel& channel = station.GetChannel(chIt->GetId());
251 sstr <<
"channel " << chIt->
GetId() <<
" is not active, continue with next channel";
257 bool similarChannelFound =
false;
259 for (
auto replacementChIt = replacementStation.
ChannelsBegin();
260 (replacementChIt != replacementStation.
ChannelsEnd()) && !similarChannelFound; ++replacementChIt) {
264 if (similarChannelFound) {
265 replacementChannelId[chIt->GetId() - 1] = replacementChannel.
GetId();
267 sstr <<
"found replacement for channel " << chIt->GetId() <<
": it is channel "
268 << replacementChannel.
GetId() <<
" of station " << replacementStation.
GetId() <<
" ("
275 if (!similarChannelFound) {
276 stationIsSuitable =
false;
281 if (stationIsSuitable) {
282 suitableStationFound =
true;
284 sstr <<
"Suitable noise station was found for station " << stationID <<
". Will use station "
285 << tmpReplacementStationID <<
" in the noise file";
291 sstr <<
"Station " << tmpReplacementStationID <<
" is not a suitable replacement for station "
297 if (!suitableStationFound) {
299 sstr <<
"Exclude station " << stationID <<
"!" <<
"\n\t It does not exist in event number " << EvtNum
300 <<
" of noise-file " << NoiseImportfilename
301 <<
". Tried to use noise information from another station but no suitable station was found.";
307 usingReplacementStation =
true;
314 sstr <<
"Found station " << station.GetId() <<
" in noise data.";
319 sstr <<
" But was excluded because for bad period reason: " << reason;
326 rootID = idIt->second;
327 usingReplacementStation =
false;
331 const int noiseChMask =
332 RadioFileIORead->getAeraRadioFileEvent()->getAeraEventStation(rootID)->getAeraStationChannelMask();
333 const bool noiseChannelExists[4] = {bool(noiseChMask & 1), bool(noiseChMask & 2),
334 bool(noiseChMask & 4), bool(noiseChMask & 8)};
335 sstr <<
"ChannelMask from noise-station" << noiseChMask <<
":" << noiseChannelExists[0]
336 << noiseChannelExists[1] << noiseChannelExists[2] << noiseChannelExists[3];
339 for (
auto& channel : station.ChannelsRange()) {
342 if (!station.HasChannel(channel.GetId()) || !channel.IsActive())
348 const short bitDepth = det.
GetRDetector().
GetStation(station.GetId()).GetChannel(channel.GetId()).GetBitDepth();
349 const short numberOfADCCounts =
pow(2., bitDepth) - 1;
351 unsigned int noiseSeriesLength = 0;
352 int channelIdInNoiseFile = channel.GetId();
354 if (usingReplacementStation) {
355 channelIdInNoiseFile = replacementChannelId[channel.GetId() - 1];
357 sstr <<
"original channel id " << channel.GetId() <<
" cooresponds to channel id " << channelIdInNoiseFile
362 if (!noiseChannelExists[channelIdInNoiseFile - 1]) {
363 channel.SetNotActive();
365 sstr <<
"The channel " << channel.GetId() <<
" of station " << stationID
366 <<
" exists for data but not for noise. ---> Setting channel not active!";
371 noiseSeriesLength = RadioFileIORead->getAeraRadioFileEvent()->getAeraEventStation(rootID)->getAeraStationADC(
372 channelIdInNoiseFile - 1)->GetADCValuesSize();
373 if (noiseSeriesLength <= timeSeries.
GetSize()) {
374 channel.SetNotActive();
376 sstr <<
"The length " << noiseSeriesLength <<
" of the noise timeseries in channel " << channel.GetId()
377 <<
" is shorter then the length " << timeSeries.
GetSize() <<
" of data timeseries in station "
378 << stationID <<
"---> Setting channel not active!";
388 for (
unsigned int i = 0; i < timeSeries.
GetSize(); ++i) {
389 const int noiseADCValue =
390 RadioFileIORead->getAeraRadioFileEvent()->getAeraEventStation(rootID)->getAeraStationADC(
391 channelIdInNoiseFile - 1)->GetADCValues(i);
396 if (val > numberOfADCCounts) {
398 sstr <<
"ADC overflow (ADC = " << val <<
", \tbin " << i <<
" , \tstation " << channel.GetStationId()
399 <<
")\tnoise = " << noiseADCValue <<
"\tsignal = " << timeSeries[i] <<
"\tsignal without pedestal = "
402 val = numberOfADCCounts;
407 sstr <<
"ADC underflow (ADC = " << val <<
", \tbin " << i <<
" , \tstation " << channel.GetStationId()
408 <<
")\tnoise = " << noiseADCValue <<
"\tsignal = " << timeSeries[i] <<
"\tsignal without pedestal = "
416 sstr <<
"Timeseries orig: " << timeSeriesOrig[i] <<
" Timeseries after NoiseImporter " << timeSeries[i];
421 int notActiveChannels = 0;
422 for (
auto& channel : station.ChannelsRange()) {
423 if (!station.HasChannel(channel.GetId()) || !channel.IsActive()) {
428 if (notActiveChannels > 2) {
430 sstr <<
"Station " << stationID <<
" has more than two not active channels " <<
"---> Exclude station!";
438 delete RadioFileIORead;
444 det.
Update(eventTimeStamp);
452 RdChannelNoiseImporter_AERA::Finish()
454 std::ostringstream
out;
465 const int intMean = int(
std::abs(mean));
466 for (
auto it = timeSeries.
Begin(); it != timeSeries.
End(); ++it)
472 RdChannelNoiseImporter_AERA::RemovePedestal(std::vector<int>& timeSeries)
474 const double sum = std::accumulate(timeSeries.begin(), timeSeries.end(), 0);
475 const int length = timeSeries.size();
476 const int intMean = int(
std::abs(sum)/length);
478 for (
auto& e : timeSeries) {
485 RdChannelNoiseImporter_AERA::checkSetup()
489 std::ostringstream error;
490 error <<
"The <NoiseFilePath> has not been set in the initfile";
502 std::string& NoiseImportfilename)
505 std::ostringstream subdirectory;
507 subdirectory << startingDay.
GetYear() << std::setfill(
'0') <<
"/" << std::setw(2) << startingDay.
GetMonth() <<
"/";
509 subdirectory << std::setfill(
'0') << std::setw(2) << startingDay.
GetDay() <<
"/";
514 filename << startingDay.
GetYear() <<
"_" << std::setfill(
'0') << std::setw(2) << startingDay.
GetMonth() <<
"_"
515 << std::setfill(
'0') << std::setw(2) << startingDay.
GetDay();
517 filename <<
"_" << std::setfill(
'0') << std::setw(2) << dateTime.
GetHour();
521 NoiseImportfilename =
fNoiseFilePath + subdirectory.str() + filename.str();
523 std::ifstream checkfile(NoiseImportfilename.c_str());
525 std::ostringstream warning;
526 warning <<
"Cannot find the corresponding noise-file to the event from " << dateTime.
GetYear()
527 << std::setfill(
'0') << std::setw(2) << dateTime.
GetMonth() << std::setfill(
'0') << std::setw(2)
528 << dateTime.
GetDay() <<
" at " << NoiseImportfilename <<
"." <<
"/n"
529 <<
"Check if the desired file exists at the path specified by the variable " "\"NoiseFilePath\" in the initfile!";
533 std::ostringstream info;
534 info <<
"Reading from noise-file " << NoiseImportfilename <<
"." << std::endl;
543 RdChannelNoiseImporter_AERA::GetNoiseEventNumber(
evt::Event& event, RadioFileIO* RadioFileIORead,
544 TBranch*
const evtSecBranch, TTree*
const IOLibTree,
548 std::ostringstream sstr;
554 sstr <<
"Noise-event number chosen in init-file is " << EvtNum <<
".";
560 EvtNum = CLHEP::RandFlat::shootInt(
fRandomEngine, NumberEvents - 1);
562 IOLibTree->GetEntry(EvtNum);
564 sstr <<
"Randomly chosen noise-event number is " << EvtNum <<
" out of " << NumberEvents <<
" events.";
572 if (eventTime.
GetHour() < 12) {
577 EvtNum = int(((eventTime.
GetHour() + hourOffset) * 60. + eventTime.
GetMinute()) * (NumberEvents / 24. / 60.));
581 EvtNum = int(eventTime.
GetMinute() * (NumberEvents / 60.));
585 sstr <<
"Estimated noise-event number by the processed data-event time is " << EvtNum <<
".";
591 for (
unsigned int EvtCounter = 0; EvtCounter < NumberEvents; ++EvtCounter) {
592 evtSecBranch->GetEntry(EvtCounter);
595 AERAevent*
const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
596 const unsigned int noiseEvtSec = aeraevent->getAeraEventSeconds();
597 const utl::TimeStamp eventTimestamp =
event.GetHeader().GetTime();
604 if (EvtCounter == NumberEvents - 1) {
610 sstr <<
"Noise-event number " << EvtNum <<
" succeeds the processed data-event in time.";
617 std::vector<int> closeEventIds;
618 for (
unsigned int i = 0; i < NumberEvents; ++i) {
619 evtSecBranch->GetEntry(i);
622 AERAevent*
const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
623 const unsigned int noiseGPSSeconds = aeraevent->getAeraEventSeconds();
624 const utl::TimeStamp eventTimestamp =
event.GetHeader().GetTime();
627 closeEventIds.push_back(i);
632 if (closeEventIds.size() == 0) {
634 <<
" minutes has been found. Event will be skipped";
640 int randVecPos = CLHEP::RandFlat::shootInt(
fRandomEngine, closeEventIds.size() - 1);
641 EvtNum = closeEventIds[randVecPos];
651 std::ostringstream sstr;
655 std::string NoiseImportfilename;
663 sstr <<
"Tried more than 10 times to import noise from a randomly chosen file. Skip Event...";
668 long noiseTimeGPSSecond = CLHEP::RandFlat::shootInt(
fRandomEngine, noiseTimeIntervalStartGPSSecond,
669 noiseTimeIntervalStopGPSSecond);
670 const unsigned long secondsPerDay = 24 * 60 * 60;
675 noiseFileStartingDay = (noiseUTCDateTime.
GetHour() < 12) ? noiseUTCDateTimeMinus24h : noiseUTCDateTime;
679 sstr <<
"Importing noise from file " << NoiseImportfilename;
685 const unsigned long secondsPerDay = 24 * 60 * 60;
693 bool eventBefore12amUTC =
false;
694 if (eventTime.
GetHour() < 12) {
695 eventBefore12amUTC =
true;
701 noiseFileStartingDay = eventTimeMinus24h;
703 noiseFileStartingDay = eventTime;
717 return NoiseImportfilename;
int GetBitDepth() const
Get number of bits of ADC.
void Update(const utl::TimeStamp &time, const bool invData=true, const bool invComp=true, const bool forceRadio=false)
Update detector: deletes currently constructed stations and sets new time.
std::string fEvtSelInNoiseFile
int GetId() const
Return Id of the Channel.
Report success to RunController.
bool fExpectSubDirectories
Detector description interface for Station-related data.
ChannelIterator ChannelsEnd() const
End of the collection of pointers to Channels.
evt::Header & GetHeader()
int GetId() const
return ID of the Channel
Interface class to access to the Radio part of an event.
static double Mean(const Trace< T > &trace, const unsigned int bin1, const unsigned int bin2)
Evaluate the mean of trace between bin1 and bin2.
Skip remaining modules in the current loop and continue with next iteration of the loop...
std::string fFilenameSuffix
utl::TimeStamp fRandomNoiseTimeIntervalStart
const std::vector< int > & GetFullStationList() const
Get list of ID's for all stations available in the database or configuration file.
#define INFO(message)
Macro for logging informational messages.
bool CheckMatchingChannel(const rdet::Channel &detChannel, const rdet::Channel &replacementChannel)
bool GetNoiseFileNameAtTime(const utl::UTCDateTime &dateTime, const utl::UTCDateTime &startingDay, std::string &NoiseImportfilename)
void Init()
Initialise the registry.
vector< t2list > out
output of the algorithm: a list of clusters
Detector description interface for Channel-related data.
double GetOrientationZenith() const
Get zenith-direction of Antenna for this Channel.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
unsigned long long int GetBadStationReason(const int stationId) const
double pow(const double x, const unsigned int i)
A TimeStamp holds GPS second and nanosecond for some event.
Detector description interface for RDetector-related data.
const std::string & GetChannelType() const
Get description of Channel Type.
#define INFOIntermediate(y)
Class representing a document branch.
int GetNoiseEventNumber(evt::Event &event, RadioFileIO *RadioFileIORead, TBranch *const evtSecBranch, TTree *const IOLibTree, unsigned int NumberEvents, utl::UTCDateTime eventTime)
std::string fFilenamePrefix
void SetGPSTime(const unsigned long sec, const double nsec=0)
Set GPS second and (optionally) nanosecond.
double abs(const SVector< n, T > &v)
std::string fNoiseFilePath
Top of the hierarchy of the detector description interface.
int GetId() const
Station ID.
unsigned int fNumberOfRejectedStationsWithoutNoise
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
ResultFlag
Flag returned by module methods to the RunController.
unsigned long GetGPSSecond() const
GPS second.
double GetGPSNanoSecond() const
GPS nanosecond.
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
bool fReplaceDataWithNoise
const std::string & GetAntennaTypeName() const
Get description of Antenna-Type.
Class that holds the data associated to an individual radio channel.
double fMaxTimeDifference
Report failure to RunController, causing RunController to terminate execution.
std::string fNoiseFileSelection
const rdet::RDetector & GetRDetector() const
bool fRejectStationsWithoutNoiseInformation
void RemovePedestal(revt::ChannelADCTimeSeries &timeSeries)
double GetSamplingFrequency() const
Get sampling Frequency of ADC (unit?)
std::string GetNoiseFileName(const utl::UTCDateTime &eventTime, utl::TimeStamp &noiseTimeStamp)
ChannelIterator ChannelsBegin() const
Beginning of the collection of pointers to Channels.
#define ERROR(message)
Macro for logging error messages.
const Station & GetStation(const int stationId) const
Get station by Station Id.
std::string fTimeIntervalInFile
TimeStamp GetTimeStamp() const
double GetOrientationAzimuth() const
Get azimuth-direction of Antenna for this Channel.
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)
utl::RandomEngine::RandomEngineType * fRandomEngine
utl::TimeStamp fRandomNoiseTimeIntervalStop