RdChannelNoiseImporter_AERA.cc
Go to the documentation of this file.
2 
3 #include <revt/REvent.h>
4 #include <revt/Header.h>
5 #include <revt/Station.h>
6 #include <revt/Channel.h>
7 
8 #include <det/Detector.h>
9 #include <rdet/RDetector.h>
10 #include <rdet/Station.h>
11 
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>
19 
20 #include <fwk/RandomEngineRegistry.h>
21 #include <fwk/CentralConfig.h>
22 
23 #include <vector>
24 #include <string>
25 #include <iostream>
26 #include <fstream>
27 
28 #include <TKey.h>
29 #include <TFile.h>
30 
31 #include <CLHEP/Random/RandFlat.h>
32 
34 
35 
38  {
39  INFODebug("");
40 
41  const utl::Branch topBranch = fwk::CentralConfig::GetInstance()->GetTopBranch("RdChannelNoiseImporter_AERA");
42  topBranch.GetChild("InfoLevel").GetData(fInfoLevel);
43  topBranch.GetChild("NoiseFileSelection").GetData(fNoiseFileSelection);
44  topBranch.GetChild("NoiseFilePath").GetData(fNoiseFilePath);
45  topBranch.GetChild("FilenamePrefix").GetData(fFilenamePrefix);
46  topBranch.GetChild("FilenameSuffix").GetData(fFilenameSuffix);
47  topBranch.GetChild("ExpectSubDirectories").GetData(fExpectSubDirectories);
48  topBranch.GetChild("MaxTimeDifference").GetData(fMaxTimeDifference);
49  topBranch.GetChild("NoiseEvtNr").GetData(fNoiseEvtNr);
50  topBranch.GetChild("RejectStationsWithoutNoiseInformation").GetData(fRejectStationsWithoutNoiseInformation);
51  topBranch.GetChild("ReplaceDataWithNoise").GetData(fReplaceDataWithNoise);
52  topBranch.GetChild("RandomNoiseTimeIntervalStart").GetData(fRandomNoiseTimeIntervalStart);
53  topBranch.GetChild("RandomNoiseTimeIntervalStop").GetData(fRandomNoiseTimeIntervalStop);
54 
55  //Add a / at the end of the path if missing and if the file is searched automatically in a directory
56  if (fNoiseFileSelection == "Automatically") {
57  if (fNoiseFilePath.compare(fNoiseFilePath.size() - 1, fNoiseFilePath.size(), "/") != 0)
58  fNoiseFilePath.append("/");
59  }
60 
61  topBranch.GetChild("EvtSelInNoiseFile").GetData(fEvtSelInNoiseFile);
62  topBranch.GetChild("TimeIntervalInFile").GetData(fTimeIntervalInFile);
63  fRandomEngine = &fwk::RandomEngineRegistry::GetInstance().Get(fwk::RandomEngineRegistry::eDetector).GetEngine();
64 
65  std::ostringstream info;
66  info << "\n\t Reject stations without noise information (or try to find matching station with noise information): "
68  INFO(info);
69  return (RdChannelNoiseImporter_AERA::checkSetup());
70  }
71 
72 
74  RdChannelNoiseImporter_AERA::Run(evt::Event& event)
75  {
76  // Check if there are events at all
77  if (!event.HasREvent()) {
78  WARNING("RdChannelNoiseImporter_AERA::No radio event found!");
79  return eContinueLoop;
80  }
81 
82  utl::UTCDateTime eventTime(event.GetHeader().GetTime());
83  const utl::TimeStamp eventTimeStamp = event.GetHeader().GetTime();
84  det::Detector& det = det::Detector::GetInstance();
85  revt::REvent& rEvent = event.GetREvent();
86 
87  std::ostringstream sstr;
88  sstr.str("");
89 
90  utl::TimeStamp noiseTimeStamp;
91 
92  // find noise file based on module config
93  std::string NoiseImportfilename = GetNoiseFileName(eventTime, noiseTimeStamp);
94 
95  // Check if at selection method was successful
96  if (NoiseImportfilename == "None") {
97  return eContinueLoop;
98  }
99 
100  // Reading in a noise-event from selected file
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; //maps rootFileStationNumber on LS_ID
109 
110  // from here on different selection methods are applied, which are ordered hierarchically
111  // in case several methods are selected, the last one used will determine the event number
112  const int EvtNum = GetNoiseEventNumber(event, RadioFileIORead, evtSecBranch, IOLibTree, NumberEvents, eventTime);
113 
114  // Check if at selection method was successfully applied
115  if (EvtNum == -1) {
116  ERROR("No noise event selected. Can not continue with import procedure. Skipping event!");
117  return eContinueLoop;
118  }
119 
120  // Fetching the whole selected noise-event from the root-file
121  IOLibTree->GetEntry(EvtNum);
122  AERAevent* const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
123  // update to detector time to noise event time
124  if (fNoiseFileSelection == "RandomlyInTimeInterval") {
125  noiseTimeStamp.SetGPSTime(aeraevent->getAeraEventSeconds(), 0);
126  det.Update(noiseTimeStamp);
127  }
128 
129  // Printing some general information
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';
133  INFOIntermediate(sstr);
134 
135  // Checking the time difference between processed data-event and the selected noise-event,
136  // if noise-event not set fixed or random by xml-config.
137  if (fEvtSelInNoiseFile == "ByEstimationFromEvtNr" || fEvtSelInNoiseFile == "ByTimestamp" ||
138  fEvtSelInNoiseFile == "RandomlyInTimeWindow") {
139  const unsigned int noiseEvtNanoSec = aeraevent->getAeraEventNanoSeconds();
140  const unsigned int noiseEvtSec = aeraevent->getAeraEventSeconds();
141  const utl::TimeStamp noiseEvtTimestamp(noiseEvtSec, noiseEvtNanoSec);
142  if (std::abs(noiseEvtTimestamp - eventTimeStamp) > fMaxTimeDifference) {
143  std::ostringstream warning;
144  warning <<
145  "The difference between the time of the estimated noise-event "
146  "time and the currently processed event exceeds " << fMaxTimeDifference / utl::minute <<
147  " minutes. Skipping event.";
148  WARNING(warning);
149  return eContinueLoop;
150  }
151  }
152 
153  // Mapping the indices of the noise event stations within the root container
154  // to their station number for later use.
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));
158  }
159 
160  // Getting rDetector after time might have been updated
161  const rdet::RDetector& rDetector = det.GetRDetector();
162 
163  // Adding noise to the data, depending on if the station exists in both: noise-file and data.
164  for (auto& station : rEvent.StationsRange()) {
165  const int stationID = station.GetId();
166  std::map<int, int>::iterator idIt;
167  int rootID = 0;
168  //set to true if the current station has no noise trace and the noise trace from a different station is used
169  bool usingReplacementStation;
170  // this array stores the channel association of the replacement station. This is necessary as numbering of the
171  // channels might not be the same. However, in most cases channel 1 of the current station corresponds also to
172  // channel 1 of the replacement station
173  short replacementChannelId[4] = { -1, -1, -1, -1 };
174 
175  // Check if the event-station exists in the noise-file and reject it if fRejectStationsWithoutNoiseInformation == true.
176  if ((idIt = IDtoNumber.find(stationID - 100)) == IDtoNumber.end()) { // if not in data:
178  sstr.str("");
179  sstr << "Did not find Station " << stationID << " in event number " << EvtNum << " of noise-file " <<
180  NoiseImportfilename << "---> Excluding station!";
181  INFOFinal(sstr);
182  station.SetExcludedReason(revt::eNoNoiseDataAvailable);
183  continue;
184  } else {
185  sstr.str("");
186  sstr << "No noise information available for station " << stationID
187  << ". Searching for other station to use its noise";
188  INFOIntermediate(sstr);
189 
190  // if that bool is turned true noise is added otherwise the station is excluded.
191  // in the following the loop is continued if it turns out that the tested candidate is not suitable
192  // (and the bool is then not switched)
193  bool suitableStationFound = false;
194  // create randomized list of all LS ids of the root file
195  std::vector<int>::size_type n = aeraevent->getAeraEventLsCount();
196  std::vector<int> rootIds;
197  rootIds.reserve(n);
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);
202  }
203  }
204 
205  for (auto tmpRootID : rootIds) {
206  aerarootiostation::Station* const rootIoReplacementStation =
207  RadioFileIORead->getAeraRadioFileEvent()->getAeraEventStation(tmpRootID);
208  //FIXME: Station Ids in RadioFile and RDetector differ by 100. Do not compare station Ids but rather station names (AERA_XXX)
209  const int tmpReplacementStationID = rootIoReplacementStation->getAeraStationLsID() + 100;
210  sstr.str("");
211  sstr << "checking station " << tmpReplacementStationID
212  << " if it is a suitable replacement (this corresponds to station number "
213  << tmpRootID << " in rootio file)";
214  INFODebug(sstr);
215  const std::vector<int> stationIdVector = rDetector.GetFullStationList();
216  // first check if station present in the data is also available in the detector description
217  if (std::find(stationIdVector.begin(), stationIdVector.end(),
218  tmpReplacementStationID) == stationIdVector.end()) {
219  sstr.str("");
220  sstr << "Station " << tmpReplacementStationID
221  << " is present in data but not in the detector description";
222  INFOIntermediate(sstr);
223  continue;
224  }
225 
226  // checking BP database
227  unsigned long long int reason = rDetector.GetBadStationReason(tmpReplacementStationID);
228  if (reason) {
229  sstr.str("");
230  sstr << "Station " << tmpReplacementStationID << " is excluded for bad period reason: " << reason;
231  INFOFinal(sstr);
232  continue;
233  }
234 
235  const rdet::Station& replacementStation = rDetector.GetStation(tmpReplacementStationID);
236  bool stationIsSuitable = true;
237 
238  // loop through the channels of the station and check if the station in the noise file has the same properties
239  for (auto chIt = rDetector.GetStation(station.GetId()).ChannelsBegin();
240  chIt != rDetector.GetStation(station.GetId()).ChannelsEnd(); ++chIt) {
241  sstr.str("");
242  sstr << "checking channel " << chIt->GetId();
243  INFODebug(sstr);
244  if (!station.HasChannel(chIt->GetId())) { // if channel is not present in data, skip to next channel
245  continue;
246  }
247 
248  const revt::Channel& channel = station.GetChannel(chIt->GetId());
249  if (!channel.IsActive()) {
250  sstr.str("");
251  sstr << "channel " << chIt->GetId() << " is not active, continue with next channel";
252  INFODebug(sstr);
253  continue;
254  }
255 
256  const rdet::Channel& detChannel = *chIt;
257  bool similarChannelFound = false; //used to break the loop when we found a channel
258 
259  for (auto replacementChIt = replacementStation.ChannelsBegin();
260  (replacementChIt != replacementStation.ChannelsEnd()) && !similarChannelFound; ++replacementChIt) {
261  const rdet::Channel& replacementChannel = *replacementChIt;
262  similarChannelFound = CheckMatchingChannel(detChannel, replacementChannel);
263 
264  if (similarChannelFound) {
265  replacementChannelId[chIt->GetId() - 1] = replacementChannel.GetId();
266  sstr.str("");
267  sstr << "found replacement for channel " << chIt->GetId() << ": it is channel "
268  << replacementChannel.GetId() << " of station " << replacementStation.GetId() << " ("
269  << tmpReplacementStationID << ")" << "\n " << detChannel.GetAntennaTypeName() << " "
270  << replacementChannel.GetAntennaTypeName();
271  INFOIntermediate(sstr);
272  }
273  }
274 
275  if (!similarChannelFound) {
276  stationIsSuitable = false;
277  }
278 
279  }
280 
281  if (stationIsSuitable) {
282  suitableStationFound = true;
283  sstr.str("");
284  sstr << "Suitable noise station was found for station " << stationID << ". Will use station "
285  << tmpReplacementStationID << " in the noise file";
286  INFOIntermediate(sstr);
287  rootID = tmpRootID;
288  break;
289  } else {
290  sstr.str("");
291  sstr << "Station " << tmpReplacementStationID << " is not a suitable replacement for station "
292  << stationID;
293  INFODebug(sstr);
294  }
295  }
296 
297  if (!suitableStationFound) {
298  sstr.str("");
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.";
302  INFOFinal(sstr);
303  station.SetExcludedReason(revt::eNoNoiseDataAvailable);
305  continue;
306  } else {
307  usingReplacementStation = true;
308  }
309 
310  } // end looking for replacement station
311 
312  } else {
313  sstr.str("");
314  sstr << "Found station " << station.GetId() << " in noise data.";
315  // checking BP database
316  unsigned long long int reason = rDetector.GetBadStationReason(station.GetId());
317  if (reason) {
318  station.SetExcludedReason(revt::eNoNoiseDataAvailable);
319  sstr << " But was excluded because for bad period reason: " << reason;
320  INFOFinal(sstr);
322  continue;
323  }
324 
325  INFOIntermediate(sstr);
326  rootID = idIt->second;
327  usingReplacementStation = false;
328  }
329 
330  // Storing which channels of the station contribute to the noise-event.
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];
337  INFODebug(sstr);
338 
339  for (auto& channel : station.ChannelsRange()) {
340  // Checking if the channel of the processed station contribute to the event.
341  // If not, continue with the next channel.
342  if (!station.HasChannel(channel.GetId()) || !channel.IsActive())
343  continue;
344 
345  revt::ChannelADCTimeSeries& timeSeries = channel.GetChannelADCTimeSeries();
346 
347  // Get number of adc counts from RDetector
348  const short bitDepth = det.GetRDetector().GetStation(station.GetId()).GetChannel(channel.GetId()).GetBitDepth();
349  const short numberOfADCCounts = pow(2., bitDepth) - 1;
350  // Checking if the noise of this channel can be added to the event. If not, reject channel.
351  unsigned int noiseSeriesLength = 0;
352  int channelIdInNoiseFile = channel.GetId();
353 
354  if (usingReplacementStation) {
355  channelIdInNoiseFile = replacementChannelId[channel.GetId() - 1];
356  sstr.str("");
357  sstr << "original channel id " << channel.GetId() << " cooresponds to channel id " << channelIdInNoiseFile
358  << " in noise file";
359  INFODebug(sstr);
360  }
361 
362  if (!noiseChannelExists[channelIdInNoiseFile - 1]) {
363  channel.SetNotActive();
364  sstr.str("");
365  sstr << "The channel " << channel.GetId() << " of station " << stationID
366  << " exists for data but not for noise. ---> Setting channel not active!";
367  INFOIntermediate(sstr);
368  continue;
369  }
370 
371  noiseSeriesLength = RadioFileIORead->getAeraRadioFileEvent()->getAeraEventStation(rootID)->getAeraStationADC(
372  channelIdInNoiseFile - 1)->GetADCValuesSize();
373  if (noiseSeriesLength <= timeSeries.GetSize()) {
374  channel.SetNotActive();
375  sstr.str("");
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!";
379  WARNING(sstr);
380  continue;
381  }
382 
383  // Finally adding the noise event to the processed pedestal free event.
384  const revt::ChannelADCTimeSeries timeSeriesOrig = timeSeries;
385  revt::ChannelADCTimeSeries pedestalFree = timeSeries;
386  //Removing pedestal of event time-series, in order to add the pedestal-free event to a noise-trace.
387  RemovePedestal(pedestalFree);
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);
392  // Decide if noise should be added to simulated data or replace it to create pure noise events
393  int val = fReplaceDataWithNoise ? noiseADCValue : pedestalFree[i] + noiseADCValue;
394  // Need to be done correctly, and info should be taken from detector geometry
395  // WHY, it seems to be done properly??
396  if (val > numberOfADCCounts) {
397  sstr.str("");
398  sstr << "ADC overflow (ADC = " << val << ", \tbin " << i << " , \tstation " << channel.GetStationId()
399  << ")\tnoise = " << noiseADCValue << "\tsignal = " << timeSeries[i] << "\tsignal without pedestal = "
400  << pedestalFree[i];
401  WARNING(sstr);
402  val = numberOfADCCounts;
403  }
404 
405  if (val < 0) {
406  sstr.str("");
407  sstr << "ADC underflow (ADC = " << val << ", \tbin " << i << " , \tstation " << channel.GetStationId()
408  << ")\tnoise = " << noiseADCValue << "\tsignal = " << timeSeries[i] << "\tsignal without pedestal = "
409  << pedestalFree[i];
410  WARNING(sstr);
411  val = 0;
412  }
413 
414  timeSeries[i] = val;
415  sstr.str("");
416  sstr << "Timeseries orig: " << timeSeriesOrig[i] << " Timeseries after NoiseImporter " << timeSeries[i];
417  INFODebug(sstr);
418  }
419  } // for Channel
420 
421  int notActiveChannels = 0;
422  for (auto& channel : station.ChannelsRange()) {
423  if (!station.HasChannel(channel.GetId()) || !channel.IsActive()) {
424  ++notActiveChannels;
425  }
426  }
427 
428  if (notActiveChannels > 2) {
429  sstr.str("");
430  sstr << "Station " << stationID << " has more than two not active channels " << "---> Exclude station!";
431  WARNING(sstr);
432  station.SetExcludedReason(revt::eHardwareMismatch);
433  }
434  } // for Station
435 
436  // Some clean up:
437  delete Readfile;
438  delete RadioFileIORead;
439 
440  // in case we changed the detector time when adding the noise
441  // (happens when the option <RandomlyInTimeInterval> is chosen)
442  // we now change it back to its original value
443  if (fNoiseFileSelection == "RandomlyInTimeInterval") {
444  det.Update(eventTimeStamp);
445  }
446 
447  return eSuccess;
448  }
449 
450 
452  RdChannelNoiseImporter_AERA::Finish()
453  {
454  std::ostringstream out;
455  out << "\n\tNumber of stations rejected due to missing noise: " << fNumberOfRejectedStationsWithoutNoise;
456  INFO(out);
457  return eSuccess;
458  }
459 
460 
461  void
462  RdChannelNoiseImporter_AERA::RemovePedestal(revt::ChannelADCTimeSeries& timeSeries)
463  {
464  const double mean = utl::TraceAlgorithm::Mean(timeSeries, 0, timeSeries.GetSize() - 1);
465  const int intMean = int(std::abs(mean));
466  for (auto it = timeSeries.Begin(); it != timeSeries.End(); ++it)
467  *it -= intMean;
468  }
469 
470 
471  void
472  RdChannelNoiseImporter_AERA::RemovePedestal(std::vector<int>& timeSeries)
473  {
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);
477 
478  for (auto& e : timeSeries) {
479  e -= intMean;
480  }
481  }
482 
483 
485  RdChannelNoiseImporter_AERA::checkSetup()
486  {
487  // Check if a proper path to the noise file or directory is given.
488  if (fNoiseFilePath == "/path/to/noise/file/") {
489  std::ostringstream error;
490  error << "The <NoiseFilePath> has not been set in the initfile";
491  ERROR(error);
492  return eFailure;
493  }
494 
495  return eSuccess;
496  }
497 
498 
499  bool
500  RdChannelNoiseImporter_AERA::GetNoiseFileNameAtTime(const utl::UTCDateTime& dateTime,
501  const utl::UTCDateTime& startingDay,
502  std::string& NoiseImportfilename)
503  {
504  std::ostringstream filename;
505  std::ostringstream subdirectory;
506  if (fExpectSubDirectories) {
507  subdirectory << startingDay.GetYear() << std::setfill('0') << "/" << std::setw(2) << startingDay.GetMonth() << "/";
508  if ((fTimeIntervalInFile == "Hour")) {
509  subdirectory << std::setfill('0') << std::setw(2) << startingDay.GetDay() << "/";
510  }
511  }
512 
513  filename << fFilenamePrefix;
514  filename << startingDay.GetYear() << "_" << std::setfill('0') << std::setw(2) << startingDay.GetMonth() << "_"
515  << std::setfill('0') << std::setw(2) << startingDay.GetDay();
516  if ((fTimeIntervalInFile == "Hour")) {
517  filename << "_" << std::setfill('0') << std::setw(2) << dateTime.GetHour();
518  }
519 
520  filename << fFilenameSuffix << ".root";
521  NoiseImportfilename = fNoiseFilePath + subdirectory.str() + filename.str();
522  // Checking if the noise-file exists.
523  std::ifstream checkfile(NoiseImportfilename.c_str());
524  if (!checkfile) {
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!";
530  WARNING(warning);
531  return false;
532  } else {
533  std::ostringstream info;
534  info << "Reading from noise-file " << NoiseImportfilename << "." << std::endl;
535  INFOFinal(info.str());
536  }
537 
538  return true;
539  }
540 
541 
542  int
543  RdChannelNoiseImporter_AERA::GetNoiseEventNumber(evt::Event& event, RadioFileIO* RadioFileIORead,
544  TBranch* const evtSecBranch, TTree* const IOLibTree,
545  unsigned int NumberEvents, utl::UTCDateTime eventTime)
546  {
547  int EvtNum = -1;
548  std::ostringstream sstr;
549 
550  // Select noise-event by event number specified by xml-config
551  if (fEvtSelInNoiseFile == "ByNrInFile") {
552  EvtNum = fNoiseEvtNr;
553  sstr.str("");
554  sstr << "Noise-event number chosen in init-file is " << EvtNum << ".";
555  INFOIntermediate(sstr);
556  }
557 
558  // Select a random noise-event
559  if (fEvtSelInNoiseFile == "Randomly") {
560  EvtNum = CLHEP::RandFlat::shootInt(fRandomEngine, NumberEvents - 1);
561  // Fetching the whole selected noise-event from the root-file
562  IOLibTree->GetEntry(EvtNum);
563  sstr.str("");
564  sstr << "Randomly chosen noise-event number is " << EvtNum << " out of " << NumberEvents << " events.";
565  INFOIntermediate(sstr);
566  }
567 
568  // Estimate the noise-event according to the data-event
569  if (fEvtSelInNoiseFile == "ByEstimationFromEvtNr") {
570  if (fTimeIntervalInFile == "Day") {
571  int hourOffset = 0;
572  if (eventTime.GetHour() < 12) {
573  hourOffset = 12;
574  } else {
575  hourOffset = -12;
576  }
577  EvtNum = int(((eventTime.GetHour() + hourOffset) * 60. + eventTime.GetMinute()) * (NumberEvents / 24. / 60.));
578  }
579 
580  if (fTimeIntervalInFile == "Hour") {
581  EvtNum = int(eventTime.GetMinute() * (NumberEvents / 60.));
582  }
583 
584  sstr.str("");
585  sstr << "Estimated noise-event number by the processed data-event time is " << EvtNum << ".";
586  INFOIntermediate(sstr);
587  }
588 
589  // Select the noise-event succeeding the processed data-event in time
590  if (fEvtSelInNoiseFile == "ByTimestamp") {
591  for (unsigned int EvtCounter = 0; EvtCounter < NumberEvents; ++EvtCounter) {
592  evtSecBranch->GetEntry(EvtCounter);
593  // Be CAREFUL: Only the event-second of aeraevent is filled and updated in this scope.
594  // The other members of aeraevent (like ID,...) contain unknown/initial values. Do not call them!
595  AERAevent* const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
596  const unsigned int noiseEvtSec = aeraevent->getAeraEventSeconds();
597  const utl::TimeStamp eventTimestamp = event.GetHeader().GetTime();
598  const double diff = utl::TimeStamp(noiseEvtSec) - eventTimestamp;
599  if (diff > 0) {
600  EvtNum = EvtCounter;
601  break;
602  }
603 
604  if (EvtCounter == NumberEvents - 1) {
605  EvtNum = EvtCounter;
606  }
607  }
608 
609  sstr.str("");
610  sstr << "Noise-event number " << EvtNum << " succeeds the processed data-event in time.";
611  INFOIntermediate(sstr);
612  }
613 
614  // Search in a given interval around event time, noise-event is randomly selected from candidates
615  if (fEvtSelInNoiseFile == "RandomlyInTimeWindow") {
616  // get list of event numbers that are within the allowed time interval
617  std::vector<int> closeEventIds;
618  for (unsigned int i = 0; i < NumberEvents; ++i) {
619  evtSecBranch->GetEntry(i);
620  // Be CAREFUL: Only the event-second of aeraevent is filled and updated in this scope.
621  // The other members of aeraevent (like ID,...) contain unknown/initial values. Do not call them!
622  AERAevent* const aeraevent = RadioFileIORead->getAeraRadioFileEvent();
623  const unsigned int noiseGPSSeconds = aeraevent->getAeraEventSeconds();
624  const utl::TimeStamp eventTimestamp = event.GetHeader().GetTime();
625  const double diff = std::abs(eventTimestamp - utl::TimeStamp(noiseGPSSeconds));
626  if (diff < fMaxTimeDifference) {
627  closeEventIds.push_back(i);
628  }
629  }
630 
631  // check for existing noise trace
632  if (closeEventIds.size() == 0) {
633  sstr << "No noise trace within " << fMaxTimeDifference / utl::minute
634  << " minutes has been found. Event will be skipped";
635  INFOIntermediate(sstr);
636  return -1;
637  }
638 
639  // Fetch a random noise-event from the the closeEventIds-vector
640  int randVecPos = CLHEP::RandFlat::shootInt(fRandomEngine, closeEventIds.size() - 1);
641  EvtNum = closeEventIds[randVecPos];
642  }
643 
644  return EvtNum;
645  }
646 
647 
648  std::string
649  RdChannelNoiseImporter_AERA::GetNoiseFileName(const utl::UTCDateTime& eventTime, utl::TimeStamp& noiseTimeStamp)
650  {
651  std::ostringstream sstr;
652 
653  utl::UTCDateTime noiseFileStartingDay;
654  utl::UTCDateTime noiseUTCDateTime;
655  std::string NoiseImportfilename;
656 
657  if (fNoiseFileSelection == "RandomlyInTimeInterval") {
658  const long noiseTimeIntervalStartGPSSecond = fRandomNoiseTimeIntervalStart.GetGPSSecond();
659  const long noiseTimeIntervalStopGPSSecond = fRandomNoiseTimeIntervalStop.GetGPSSecond();
660  int tries = 0;
661  do {
662  if (tries > 10) {
663  sstr << "Tried more than 10 times to import noise from a randomly chosen file. Skip Event...";
664  INFO(sstr);
665  return "None";
666  }
667 
668  long noiseTimeGPSSecond = CLHEP::RandFlat::shootInt(fRandomEngine, noiseTimeIntervalStartGPSSecond,
669  noiseTimeIntervalStopGPSSecond);
670  const unsigned long secondsPerDay = 24 * 60 * 60;
671  noiseTimeStamp = utl::TimeStamp(noiseTimeGPSSecond);
672  noiseUTCDateTime = utl::UTCDateTime(noiseTimeStamp);
673  const utl::TimeStamp noiseTimeStampMinus24h = utl::TimeStamp(noiseTimeGPSSecond - secondsPerDay);
674  const utl::UTCDateTime noiseUTCDateTimeMinus24h = utl::UTCDateTime(noiseTimeStampMinus24h);
675  noiseFileStartingDay = (noiseUTCDateTime.GetHour() < 12) ? noiseUTCDateTimeMinus24h : noiseUTCDateTime;
676  tries++;
677  } while (!GetNoiseFileNameAtTime(noiseUTCDateTime, noiseFileStartingDay, NoiseImportfilename));
678 
679  sstr << "Importing noise from file " << NoiseImportfilename;
680  INFOIntermediate(sstr);
681  }
682 
683  //If only a directory is given in init-file: Search for and check a suiting noise-file.
684  if (fNoiseFileSelection == "Automatically") {
685  const unsigned long secondsPerDay = 24 * 60 * 60;
686  const utl::TimeStamp eventTimeStampMinus24h(eventTime.GetTimeStamp().GetGPSSecond() - secondsPerDay,
687  eventTime.GetTimeStamp().GetGPSNanoSecond());
688  const utl::UTCDateTime eventTimeMinus24h(eventTimeStampMinus24h);
689 
690  // Taking into account, that daily recorded background-files
691  // contain data from 12 noon of the starting day until 12 noon
692  // of the following day.
693  bool eventBefore12amUTC = false;
694  if (eventTime.GetHour() < 12) {
695  eventBefore12amUTC = true;
696  }
697 
698  // Using the file one day before if the processed event has taken place before 12 noon UTC.
699  // But only if the files contain noise-data of a whole day.
700  if (eventBefore12amUTC && (fTimeIntervalInFile == "Day")) {
701  noiseFileStartingDay = eventTimeMinus24h;
702  } else {
703  noiseFileStartingDay = eventTime;
704  }
705 
706  // Composing the path to the noise-file.
707  if (!GetNoiseFileNameAtTime(eventTime, noiseFileStartingDay, NoiseImportfilename)) {
708  return "None";
709  }
710  }
711 
712  // If the noise file is given explicitly in the init-file:
713  if (fNoiseFileSelection == "Manually") {
714  NoiseImportfilename = fNoiseFilePath;
715  }
716 
717  return NoiseImportfilename;
718  }
719 
720 
721  bool
722  RdChannelNoiseImporter_AERA::CheckMatchingChannel(const rdet::Channel& detChannel, const rdet::Channel& replacementChannel)
723  {
724  // check if orientation of the channel is the same
725  if (std::abs(detChannel.GetOrientationZenith() - replacementChannel.GetOrientationZenith()) > 3 * utl::degree ||
726  std::abs(detChannel.GetOrientationAzimuth() - replacementChannel.GetOrientationAzimuth()) > 3 * utl::degree)
727  return false;
728 
729  // check is channel and antenna type are the same
730  if ((detChannel.GetChannelType() != replacementChannel.GetChannelType()) ||
731  (detChannel.GetAntennaTypeName() != replacementChannel.GetAntennaTypeName()))
732  return false;
733 
734  // check if sampling frequency is the same
735  if (std::abs(detChannel.GetSamplingFrequency() - replacementChannel.GetSamplingFrequency()) > 0.01)
736  return false;
737 
738  // check if ADC is the same
739  if (detChannel.GetBitDepth() != replacementChannel.GetBitDepth())
740  return false;
741 
742  // replacement found
743  return true;
744  }
745 
746 }
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.
Definition: Detector.cc:179
bool IsActive() const
int GetId() const
Return Id of the Channel.
Report success to RunController.
Definition: VModule.h:62
Detector description interface for Station-related data.
ChannelIterator ChannelsEnd() const
End of the collection of pointers to Channels.
evt::Header & GetHeader()
int GetHour() const
Definition: UTCDateTime.h:54
int GetId() const
return ID of the Channel
Interface class to access to the Radio part of an event.
Definition: REvent.h:42
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...
Definition: VModule.h:68
const std::vector< int > & GetFullStationList() const
Get list of ID&#39;s for all stations available in the database or configuration file.
Definition: RDetector.cc:84
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
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
Definition: XbAlgo.cc:32
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.
Definition: Branch.cc:211
unsigned long long int GetBadStationReason(const int stationId) const
Definition: RDetector.cc:217
double pow(const double x, const unsigned int i)
bool HasREvent() const
int GetYear() const
Definition: UTCDate.h:44
int fInfoLevel
Definition: VModule.h:123
int GetMinute() const
Definition: UTCDateTime.h:56
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
const utl::TimeStamp & GetTime() const
Definition: Event/Header.h:33
Detector description interface for RDetector-related data.
Definition: RDetector.h:46
Iterator Begin()
Definition: Trace.h:75
const std::string & GetChannelType() const
Get description of Channel Type.
#define INFOIntermediate(y)
Definition: VModule.h:162
Class representing a document branch.
Definition: Branch.h:107
int GetNoiseEventNumber(evt::Event &event, RadioFileIO *RadioFileIORead, TBranch *const evtSecBranch, TTree *const IOLibTree, unsigned int NumberEvents, utl::UTCDateTime eventTime)
void SetGPSTime(const unsigned long sec, const double nsec=0)
Set GPS second and (optionally) nanosecond.
Definition: TimeStamp.h:120
double abs(const SVector< n, T > &v)
Top of the hierarchy of the detector description interface.
Definition: Detector.h:81
constexpr double degree
int GetId() const
Station ID.
SizeType GetSize() const
Definition: Trace.h:156
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
constexpr double minute
Definition: AugerUnits.h:149
#define INFODebug(y)
Definition: VModule.h:163
int GetMonth() const
Definition: UTCDate.h:46
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
int GetDay() const
Definition: UTCDate.h:48
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
double GetGPSNanoSecond() const
GPS nanosecond.
Definition: TimeStamp.h:127
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.
Class that holds the data associated to an individual radio channel.
Report failure to RunController, causing RunController to terminate execution.
Definition: VModule.h:64
const rdet::RDetector & GetRDetector() const
Definition: Detector.cc:143
void RemovePedestal(revt::ChannelADCTimeSeries &timeSeries)
char * filename
Definition: dump1090.h:266
double GetSamplingFrequency() const
Get sampling Frequency of ADC (unit?)
#define INFOFinal(y)
Definition: VModule.h:161
std::string GetNoiseFileName(const utl::UTCDateTime &eventTime, utl::TimeStamp &noiseTimeStamp)
ChannelIterator ChannelsBegin() const
Beginning of the collection of pointers to Channels.
Iterator End()
Definition: Trace.h:76
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
const Station & GetStation(const int stationId) const
Get station by Station Id.
Definition: RDetector.cc:141
TimeStamp GetTimeStamp() const
Definition: UTCDateTime.cc:115
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)

, generated on Tue Sep 26 2023.