4 #include <evt/RadioSimulation.h>
5 #include <evt/ShowerSimData.h>
7 #include <revt/REvent.h>
8 #include <revt/Header.h>
9 #include <revt/Station.h>
10 #include <revt/Channel.h>
12 #include <det/Detector.h>
13 #include <rdet/RDetector.h>
14 #include <rdet/Station.h>
15 #include <rdet/Channel.h>
17 #include <utl/Trace.h>
18 #include <utl/TraceAlgorithm.h>
19 #include <utl/ErrorLogger.h>
20 #include <utl/Reader.h>
21 #include <utl/config.h>
22 #include <utl/RandomEngine.h>
23 #include <utl/UTCDateTime.h>
24 #include <utl/TimeStamp.h>
26 #include <fwk/RandomEngineRegistry.h>
27 #include <fwk/CentralConfig.h>
39 #include <RadioFileIO.h>
41 #include <CLHEP/Random/RandFlat.h>
63 topBranch.
GetChild(
"ExpectSubDirectories").
GetData(fExpectSubDirectories);
65 topBranch.
GetChild(
"RandomNoiseTimeIntervalStart").
GetData(fRandomNoiseTimeIntervalStart);
66 topBranch.
GetChild(
"RandomNoiseTimeIntervalStop").
GetData(fRandomNoiseTimeIntervalStop);
72 topBranch.
GetChild(
"ReplaceDataWithNoise").
GetData(fReplaceDataWithNoise);
80 if (fStationIDList.empty()) {
81 ERROR(
"Station list is empty, no noise can be added!");
83 msg <<
"\n\tNoise from the following stations is added: ";
84 for (
auto&
id : fStationIDList)
85 msg <<
id + 100 <<
" ";
93 msg <<
"Additional Bad Period entries:";
95 if (
b.GetName() !=
"BadStation")
97 const size_t stationId = det::VManager::FindComponent<size_t>(
"id",
b.GetAttributes());
98 const string reason = det::VManager::FindComponent<string>(
"reason",
b.GetAttributes());
100 vector<utl::TimeStamp> range;
102 fRejectMap.insert(make_pair(range[0], make_pair(range[1], stationId)));
103 msg <<
"\n\tregistered: id = " << stationId <<
" [" << range[0] <<
", " << range[1] <<
"]";
117 WARNING(
"RdVirtualStationNoiseImporter::No radio event found!");
118 return eContinueLoop;
124 Detector& det = det::Detector::GetInstance();
125 REvent& rEvent =
event.GetREvent();
128 ostringstream infostr;
129 ostringstream warnstr;
130 ostringstream debugstr;
132 string NoiseImportfilename;
136 long noiseTimeGPSSecond;
139 if (fNoiseFileSelection ==
"Manually")
140 NoiseImportfilename = fNoiseFilePath;
144 RadioFileIO* RadioFileIORead =
nullptr;
145 TFile* Readfile =
nullptr;
146 TTree* IOLibTree =
nullptr;
147 TBranch* IObranch =
nullptr;
149 vector<int> closeEventIds;
150 vector<pair<int,vector<int>>> EventListWithStations;
155 if (tries > 1 && fNoiseFileSelection ==
"Manually") {
156 ERROR(
"Manually file sleection do not try a second time... aborting!");
161 if (fNoiseFileSelection ==
"Randomly") {
162 const long noiseTimeIntervalStartGPSSecond = fRandomNoiseTimeIntervalStart.GetGPSSecond();
163 const long noiseTimeIntervalStopGPSSecond = fRandomNoiseTimeIntervalStop.GetGPSSecond();
165 CLHEP::RandFlat::shootInt(fRandomEngine, noiseTimeIntervalStartGPSSecond, noiseTimeIntervalStopGPSSecond);
167 const unsigned long secondsPerDay = 24 * 60 * 60;
171 det.
Update(noiseTimeStamp);
174 bool dont_skip =
false;
175 for (
auto it = fStationIDList.begin() ; it != fStationIDList.end(); ++it) {
176 if (!rDetector.
GetBadStationReason(*it) && !(std::find(badIds.begin(), badIds.end(), *it) != badIds.end()))
180 INFOIntermediate(
"Random time stamp for noise import is rejected, because of Bad Period. "
181 "Look for next file... ");
188 noiseFileStartingDay = (noiseUTCDateTime.
GetHour() < 12) ? noiseUTCDateTimeMinus24h : noiseUTCDateTime;
190 if (!GetNoiseFileNameAtTime(noiseUTCDateTime, noiseFileStartingDay, NoiseImportfilename)) {
192 ERROR(
"Failed to fetch noise file too many times. Continue loop...");
193 return eContinueLoop;
196 infostr <<
"Attempt " << tries <<
": Failed to import noise from file " << NoiseImportfilename;
197 infostr <<
". please check if the path and suffix/prefix are set properly.\nTrying next random time";
201 infostr <<
"Importing noise from file " << NoiseImportfilename;
205 RadioFileIORead =
new RadioFileIO();
206 Readfile =
new TFile(NoiseImportfilename.c_str(),
"READ");
207 IOLibTree = (TTree*)Readfile->Get(
"AERAIOTree");
208 IObranch = IOLibTree->GetBranch(
"AERAIO");
210 IObranch->SetAddress(&RadioFileIORead);
212 unsigned int NumberEvents = IOLibTree->GetEntries();
216 vector<int> notBadStations;
217 unsigned int stationsInTotal = 0;
218 vector<int> infoList;
219 EventListWithStations.clear();
220 debugstr <<
"Number of events: " << NumberEvents << endl
221 <<
"TimeStamp:" << noiseTimeStamp << endl;
231 requiered_number = (
unsigned int)requiered_number / 6 + 1;
237 unsigned int n_tried_events_in_file = 0;
246 if (fCheckTimeWindow) {
250 used_index = CLHEP::RandFlat::shootInt(fRandomEngine, 0, NumberEvents - 1);
251 n_tried_events_in_file++;
252 if (n_tried_events_in_file > 5 && stationsInTotal == 0)
257 IOLibTree->GetEntry(used_index);
258 AERAevent*
const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
259 if (!aeraevent->getAeraEventTriggerIsPeriodic())
262 if (fCheckTimeWindow) {
264 if (noiseTimeStamp < probeTimeStamp
265 ||
std::abs(probeTimeStamp - noiseTimeStamp) > fMaxTimeDifference){
266 debugstr <<
"Event " << used_index <<
": (" << probeTimeStamp <<
") is NOT in the specified range. continue. ";
271 debugstr <<
"event " << used_index <<
" is in the specified range. "
272 <<
"Now check how many stations are found (also check BP database). ";
276 if (EventHasStations(fStationIDList, aeraevent, rDetector).size() == 0) {
277 INFODebug(
"No matched stations in event.. try next");
282 notBadStations.clear();
285 for (
auto it = fStationIDList.begin() ; it != fStationIDList.end(); ++it) {
288 debugstr <<
"\n\tStation rejceted: " << *it <<
", reason: " << reason;
291 if(!(std::find(badIds.begin(), badIds.end(), *it) != badIds.end())) {
292 notBadStations.push_back(*it);
293 debugstr <<
"\n\tStation accepted: " << *it;
299 if (notBadStations.empty()) {
300 INFOFinal(
"all stations in event are bad.. try next");
304 vector<int> matchedIDs = EventHasStations(notBadStations, aeraevent, rDetector);
305 if (matchedIDs.size() == 0) {
306 INFODebug(
"No matched good stations in event.. try next");
310 closeEventIds.push_back(used_index);
311 EventListWithStations.emplace_back(used_index, matchedIDs);
312 stationsInTotal += matchedIDs.size();
314 }
while (stationsInTotal < requiered_number and (
unsigned int)idx < NumberEvents);
317 if (closeEventIds.size() == 0 || stationsInTotal < requiered_number) {
318 warnstr <<
"To few stations found within " << fMaxTimeDifference /
utl::minute
319 <<
" minutes has been found. (Checked time: " << fCheckTimeWindow <<
")" << endl
320 <<
"Or no stations were found (in total: "
321 << stationsInTotal <<
"). File will be skipped";
323 delete RadioFileIORead;
324 if (fNoiseFileSelection ==
"Manually") {
325 ERROR(
"The manually selected noise file doesn't contain the defined stations...aborting!");
333 infostr <<
"Found " << stationsInTotal <<
" stations in " << closeEventIds.size()
334 <<
" events. (If Shifting is used, each trace is used 6 times)";
342 unsigned int iterationCounter = 0;
343 unsigned int internalStationCounter = 0;
347 const int stationID = station.
GetId();
348 unsigned int rootID = 0;
349 unsigned int eventsRead = fTraceShifting ? (int)iterationCounter / 6 : iterationCounter;
350 unsigned int nTimesUsed = fTraceShifting ? iterationCounter % 6: iterationCounter;
353 IOLibTree->GetEntry(EventListWithStations[eventsRead].first);
354 AERAevent*
const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
357 debugstr <<
"\n\tIteration " << iterationCounter <<
", Event: " << eventsRead
358 <<
" (is event no. " << closeEventIds[eventsRead] <<
")"
359 <<
" Traces were used " << nTimesUsed <<
" times."
360 <<
"\n\tImporting Noise for station " << stationID
361 <<
", fetching aera event " << EventListWithStations[eventsRead].first;
372 vector<int> stationsInEvent = EventListWithStations[eventsRead].second;
377 short replacementChannelId[4] = { -1, -1, -1, -1 };
379 const int tmpRootID = stationsInEvent[internalStationCounter];
380 debugstr <<
"\n\tevent station counter " << internalStationCounter <<
" (index) of " << stationsInEvent.size() <<
" (size)"
381 <<
"\n\tEvent: " << eventsRead <<
" of " << EventListWithStations.size();
385 aerarootiostation::Station*
const rootIoReplacementStation = aeraevent->getAeraEventStation(tmpRootID);
386 const int tmpReplacementStationID = rootIoReplacementStation->getAeraStationLsID() + 100;
388 debugstr <<
"\n\tChecking station " << tmpReplacementStationID
389 <<
" if it is a suitable replacement (this corresponds to station number "
390 << tmpRootID <<
" in rootio file)";
394 if (stationsInEvent.size() > internalStationCounter + 1) {
395 internalStationCounter += 1;
398 internalStationCounter = 0;
399 iterationCounter += 1;
402 if (eventsRead > EventListWithStations.size()-2) {
404 iterationCounter = 0;
409 if (std::find(stationIdVector.begin(), stationIdVector.end(), tmpReplacementStationID) == stationIdVector.end()) {
411 infostr <<
"Station " << tmpReplacementStationID
412 <<
" is present in data but not in the detector description";
418 bool stationIsSuitable =
true;
424 debugstr <<
"checking channel " << chIt->
GetId();
432 debugstr <<
"channel " << chIt->
GetId() <<
" is not active, continue with next channel";
438 bool similarChannelFound =
false;
440 (replacementChIt != replacementStation.
ChannelsEnd()) && !similarChannelFound;
453 similarChannelFound =
true;
454 replacementChannelId[chIt->GetId() - 1] = replacementChannel.
GetId();
456 infostr <<
"found replacement for channel " << chIt->GetId()
457 <<
": it is channel " << replacementChannel.
GetId() <<
" of station "
458 << replacementStation.
GetId() <<
" (" << tmpReplacementStationID <<
")";
466 if (!similarChannelFound) {
467 stationIsSuitable =
false;
471 if (stationIsSuitable) {
473 infostr <<
"Suitable noise station was found for station " << stationID
474 <<
". Will use station " << tmpReplacementStationID <<
" in the noise file";
479 debugstr <<
"Station " << tmpReplacementStationID
480 <<
" is not a suitable replacement for station " << stationID
481 <<
" I think this shouldn't have happened... ";
488 const int noiseChMask = aeraevent->getAeraEventStation(rootID)->getAeraStationChannelMask();
489 const bool noiseChannelExists[4] = {
490 bool(noiseChMask & 1),
491 bool(noiseChMask & 2),
492 bool(noiseChMask & 4),
493 bool(noiseChMask & 8)
505 std::ostringstream
debug;
506 debug <<
"ChannelMask from noise-station" << noiseChMask <<
":" << noiseChannelExists[0]
507 << noiseChannelExists[1] << noiseChannelExists[2] << noiseChannelExists[3];
522 const short bitDepth =
524 const short numberOfADCCounts =
pow(2., bitDepth) - 1;
527 unsigned int noiseSeriesLength = 0;
528 int channelIdInNoiseFile = cIt->GetId();
529 channelIdInNoiseFile = replacementChannelId[cIt->GetId() - 1];
531 debug <<
"original channel id " << cIt->GetId() <<
" cooresponds to channel id "
532 << channelIdInNoiseFile <<
" in noise file";
535 if (!noiseChannelExists[channelIdInNoiseFile - 1]) {
538 infostr <<
"The channel " << cIt->GetId() <<
" of station " << stationID
539 <<
" exists for data but not for noise. ---> Setting channel not active!";
544 noiseSeriesLength = aeraevent->getAeraEventStation(rootID)->getAeraStationADC(channelIdInNoiseFile - 1)->GetADCValuesSize();
545 if (noiseSeriesLength <= timeSeries.
GetSize()) {
548 warnstr <<
"The length " << noiseSeriesLength <<
" of the noise timeseries in channel "
549 << cIt->GetId() <<
" is shorter then the length " << timeSeries.
GetSize()
550 <<
" of data timeseries in station " << stationID <<
"---> Setting channel not active!";
558 for (
unsigned int i = 0; i < timeSeries.
GetSize(); ++i) {
560 RemovePedestal(pedestalFree);
563 unsigned int j = fTraceShifting ? (i + nTimesUsed * noiseSeriesLength / 6) % noiseSeriesLength : i;
564 const int tmpADCValue =
565 aeraevent->getAeraEventStation(rootID)->getAeraStationADC(channelIdInNoiseFile - 1)->GetADCValues(j);
569 const int int_mean = int(
std::abs(mean));
571 const int noiseADCValue = int(tmpADCValue *
pow(10.0, fGainFactor / 20) + int_mean * (1 -
pow(10.0, fGainFactor/ 20)));
574 int val = fReplaceDataWithNoise ? noiseADCValue : pedestalFree[i] + noiseADCValue;
578 if (val > numberOfADCCounts) {
580 warnstr <<
"ADC overflow (ADC = " << val <<
", \tbin " << i <<
" , (shifted-> bin is actually: " << j <<
").\tstation "
581 << cIt->GetStationId() <<
")\tnoise = " << noiseADCValue <<
"\tsignal = "
582 << timeSeries[i] <<
"\tsignal without pedestal = " << pedestalFree[i];
584 val = numberOfADCCounts;
589 warnstr <<
"ADC underflow (ADC = " << val <<
", \tbin " << i <<
" , (shifted-> bin is actually: " << j <<
".\tstation "
590 << cIt->GetStationId() <<
")\tnoise = " << noiseADCValue <<
"\tsignal = "
591 << timeSeries[i] <<
"\tsignal without pedestal = " << pedestalFree[i];
603 int NotActiveChannels = 0;
611 if (NotActiveChannels > 2) {
613 warnstr <<
"Station " << stationID <<
" has more than two not active channels "
614 <<
"---> Rejecting whole station!";
622 delete RadioFileIORead;
626 det.
Update(origTimeStamp);
634 RdVirtualStationNoiseImporter::Finish()
636 std::ostringstream
out;
637 out <<
"\n\tNumber of stations rejected due to missing noise: " << fNumberOfRejectedStationsWithoutNoise;
649 const int int_mean = int(
std::abs(mean));
656 RdVirtualStationNoiseImporter::RemovePedestal(std::vector<int>& timeSeries)
660 for (std::vector<int>::iterator it = timeSeries.begin(); it != timeSeries.end(); ++it) {
665 const int int_mean = int(
std::abs(mean));
666 for (std::vector<int>::iterator it = timeSeries.begin(); it != timeSeries.end(); ++it)
674 std::string& NoiseImportfilename)
677 std::ostringstream subdirectory;
679 if (fExpectSubDirectories) {
681 subdirectory << startingDay.
GetYear() << std::setfill(
'0') <<
"/" << std::setw(2)
683 if ((fTimeIntervalInFile ==
"Hour")) {
684 subdirectory << std::setfill(
'0') << std::setw(2) << startingDay.
GetDay() <<
"/";
688 filename << fFilenamePrefix;
689 filename << startingDay.
GetYear() <<
"_" << std::setfill(
'0') << std::setw(2)
690 << startingDay.
GetMonth() <<
"_" << std::setfill(
'0') << std::setw(2)
692 if ((fTimeIntervalInFile ==
"Hour")) {
693 filename <<
"_" << std::setfill(
'0') << std::setw(2) << dateTime.
GetHour();
695 if (fFilenameSuffix !=
"0")
696 filename << fFilenameSuffix;
698 NoiseImportfilename = fNoiseFilePath + subdirectory.str() + filename.str();
701 std::ifstream checkfile(NoiseImportfilename.c_str());
703 std::ostringstream warning;
704 warning <<
"Cannot find the corresponding noise-file to the event from " << dateTime.
GetYear()
705 << std::setfill(
'0') << std::setw(2) << dateTime.
GetMonth() << std::setfill(
'0')
706 << std::setw(2) << dateTime.
GetDay() <<
" at " << NoiseImportfilename <<
"."
708 <<
"Check if the desired file exists at the path specified by the variable "
709 "\"NoiseFilePath\" in the initfile!";
713 std::ostringstream info;
714 info <<
"Reading from noise-file " << NoiseImportfilename <<
"." << std::endl;
721 RdVirtualStationNoiseImporter::EventHasStations(vector<int> stationID, AERAevent*
const aeraevent,
const rdet::RDetector& rDetector)
723 ostringstream debugstr;
724 std::vector<int> matchedID;
727 for (
int i = 0; i < aeraevent->getAeraEventLsCount(); ++i) {
728 const int tmpRootID = i;
729 aerarootiostation::Station*
const rootIoReplacementStation = aeraevent->getAeraEventStation(tmpRootID);
733 const int tmpReplacementStationID = rootIoReplacementStation->getAeraStationLsID() + 100;
735 for (std::vector<int>::iterator matchedIdIt = stationID.begin(); matchedIdIt != stationID.end(); ++matchedIdIt) {
737 if (*matchedIdIt == tmpReplacementStationID - 100) {
740 if (rootIoReplacementStation->getNumberOfAvailableAERAStationADCs() == 0
741 || rootIoReplacementStation->getAeraStationStatus() == 0) {
743 debugstr <<
"number of ADS is " << rootIoReplacementStation->getNumberOfAvailableAERAStationADCs()
744 <<
"\nStation Status is " << rootIoReplacementStation->getAeraStationStatus() <<
" --> skip " << endl;
765 if (std::find(stationIdVector.begin(), stationIdVector.end(), tmpReplacementStationID) == stationIdVector.end()) {
767 debugstr <<
"Station " << tmpReplacementStationID <<
" is present in data but not in the detector description";
772 debugstr <<
"Matched station "<< tmpReplacementStationID <<
" (root id: " << tmpRootID <<
")";
784 matchedID.push_back(tmpRootID);
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.
int GetId() const
Return Id of the Channel.
Detector description interface for Station-related data.
boost::indirect_iterator< InternalChannelIterator, Channel & > ChannelIterator
Iterator over station for read/write.
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.
Data structure for a radio simulation (including several SimRadioPulses)
std::vector< size_t > IdCollection
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.
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.
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.
ChannelIterator ChannelsBegin()
begin Channel iterator for read/write
unsigned long long int GetBadStationReason(const int stationId) const
double pow(const double x, const unsigned int i)
ChannelIterator ChannelsEnd()
end Channel iterator for read/write
A TimeStamp holds GPS second and nanosecond for some event.
Detector description interface for RDetector-related data.
Interface class to access Shower Simulated parameters.
bool HasChannel(const int pmtId) const
Check if a particular Channel object exists.
const std::string & GetChannelType() const
Get description of Channel Type.
#define INFOIntermediate(y)
Class representing a document branch.
class to hold data at the radio Station level.
RadioSimulation & GetRadioSimulation()
Get the radio simulation data.
double abs(const SVector< n, T > &v)
Top of the hierarchy of the detector description interface.
int GetId() const
Station ID.
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
void SetRejectedReason(const unsigned long long int reason)
int GetId() const
Get the station Id.
boost::indirect_iterator< InternalChannelIterator, const Channel & > ChannelIterator
ChannelIterator returns a pointer to a Channel.
Channel & GetChannel(const int pmtId)
Retrieve a Channel by Id.
ResultFlag
Flag returned by module methods to the RunController.
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
const std::string & GetAntennaTypeName() const
Get description of Antenna-Type.
ChannelADCTimeSeries & GetChannelADCTimeSeries()
Get Channel ADC trace (write access, only use this if you intend to change the data) ...
Class that holds the data associated to an individual radio channel.
const rdet::RDetector & GetRDetector() const
Branch GetFirstChild() const
Get first child of this Branch.
long GetNumPulses() const
Get the number of radio pulses contained in the RadioSimulation.
double GetSamplingFrequency() const
Get sampling Frequency of ADC (unit?)
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::vector< int >::iterator Iterator
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)