RdChannelASCIINoiseImporterRD.cc
Go to the documentation of this file.
2 
3 #include <evt/Event.h>
4 #include <evt/ShowerSimData.h>
5 
6 #include <revt/REvent.h>
7 #include <revt/Header.h>
8 #include <revt/Station.h>
9 #include <revt/Channel.h>
10 
11 #include <det/Detector.h>
12 #include <rdet/RDetector.h>
13 #include <rdet/Station.h>
14 #include <rdet/Channel.h>
15 
16 #include <utl/Trace.h>
17 #include <utl/TraceAlgorithm.h>
18 #include <utl/ErrorLogger.h>
19 #include <utl/Reader.h>
20 #include <utl/config.h>
21 #include <utl/AugerUnits.h>
22 #include <utl/RandomEngine.h>
23 #include <utl/UTCDateTime.h>
24 #include <utl/TimeStamp.h>
25 
26 #include <fwk/RandomEngineRegistry.h>
27 #include <fwk/CentralConfig.h>
28 
29 #include <boost/algorithm/string.hpp>
30 
31 #include <vector>
32 #include <string>
33 #include <iostream>
34 #include <fstream>
35 
36 #include <CLHEP/Random/RandFlat.h>
37 
38 
40 
43  {
44  utl::Branch topBranch = fwk::CentralConfig::GetInstance()->GetTopBranch("RdChannelASCIINoiseImporterRD");
45  topBranch.GetChild("InfoLevel").GetData(fInfoLevel);
46 
47  topBranch.GetChild("NoiseFilePathCh0").GetData(fNoiseFilePathCh0);
48  topBranch.GetChild("NoiseFilePathCh1").GetData(fNoiseFilePathCh1);
49  topBranch.GetChild("AddNoiseFromSameTimeToBothChannels").GetData(fAddNoiseFromSameTimeToBothChannels);
50  topBranch.GetChild("CheckTimeWindow").GetData(fCheckTimeWindow);
51  topBranch.GetChild("TimeWindow").GetData(fMaxTimeDifference);
52  topBranch.GetChild("AllowTraceShifting").GetData(fTraceShifting);
53  topBranch.GetChild("SkipTrailingColumnsInNoiseFile").GetData(fSkipTrailingColumnsInNoiseFile);
54  topBranch.GetChild("GainFactor").GetData(fGainFactor);
55 
56  topBranch.GetChild("ReplaceDataWithNoise").GetData(fReplaceDataWithNoise);
57 
59  &fwk::RandomEngineRegistry::GetInstance().Get(fwk::RandomEngineRegistry::eDetector).GetEngine();
60 
63 
65  if (fNoiseTraces[0].size() != fNoiseTraces[1].size() || fNoiseTraces[0].size() == 0) {
66  ERROR("Imported noise for the two channels are not same size or size is 0!");
67  return eFailure;
68  }
69  }
70 
71  std::ostringstream infostr;
72  infostr << "\n\tRead " << fNoiseTraces[0].size() << " traces for channel 0 and "
73  << fNoiseTraces[1].size() << " traces for channel 1"
74  << "\n\tAddNoiseFromSameTimeToBothChannels : " << fAddNoiseFromSameTimeToBothChannels
75  << "\n\tAllow trace shifting : " << fTraceShifting
76  << "\n\tCheck time window (not implmented) : " << fCheckTimeWindow;
77 
78  infostr << std::endl;
79  INFO(infostr);
80  return eSuccess;
81  }
82 
83 
85  RdChannelASCIINoiseImporterRD::Run(evt::Event& event)
86  {
87  // Check if there are events at all
88  if (!event.HasREvent()) {
89  WARNING("RdChannelASCIINoiseImporterRD::No radio event found!");
90  return eContinueLoop;
91  }
92 
93  det::Detector& det = det::Detector::GetInstance();
94  revt::REvent& rEvent = event.GetREvent();
95  const rdet::RDetector& rDetector = det.GetRDetector();
96 
97  std::ostringstream infostr;
98  std::ostringstream warnstr;
99  std::ostringstream debugstr;
100 
101  // Adding noise to the data, depending on if the station exists in both: noise-file and data.
102  for (auto& station : rEvent.StationsRange()) {
103  const int stationID = station.GetId();
104 
105  // choose same trace (index) for both channel
106  unsigned int traceIndex = CLHEP::RandFlat::shootInt(fRandomEngine, 0, fNoiseTraces[0].size());
107  unsigned int sampleIndexNoiseStart = 0;
108 
109  // Traces have a lenght of 2048 (samples) / 250 MHz = 8192 ns
110  // Shift traces by multiple of 100ns, i.e., 25 sample
111  // This does not prevent the trace end to be within the signal window
113  sampleIndexNoiseStart = CLHEP::RandFlat::shootInt(fRandomEngine, 0, 25) *
114  fNoiseTraces[0][traceIndex].size() / 25;
115  }
116 
117  const rdet::Station& rDetStation = rDetector.GetStation(stationID);
118  for (auto& channel : station.ChannelsRange()) {
119  revt::ChannelADCTimeSeries& timeSeries = channel.GetChannelADCTimeSeries();
120  const unsigned int channelID = channel.GetId();
121 
122  // Checking if the channel of the processed station contribute to the event.
123  // If not, continue with the next channel.
124  if (!station.HasChannel(channelID) || !channel.IsActive())
125  continue;
126 
128  // choose trace and shift for each channel independently
129  traceIndex = CLHEP::RandFlat::shootInt(fRandomEngine, 0, fNoiseTraces[channelID].size());
130  if (fTraceShifting) {
131  sampleIndexNoiseStart = CLHEP::RandFlat::shootInt(fRandomEngine, 0, 25) *
132  fNoiseTraces[channelID][traceIndex].size() / 25;
133  }
134  }
135 
136  // Get number of adc counts from RDetector
137  const short bitDepth = rDetStation.GetChannel(channel.GetId()).GetBitDepth();
138  // const short numberOfADCCounts = pow(2., bitDepth) - 1;
139  double minVoltage = rDetStation.GetChannel(channel.GetId()).GetMinVoltage();
140  double maxVoltage = rDetStation.GetChannel(channel.GetId()).GetMaxVoltage();
141  double voltagePerCount = (maxVoltage - minVoltage) / pow(2, bitDepth);
142 
143  // Finally adding the noise event to the processed pedestal free event.
144  const revt::ChannelADCTimeSeries timeSeriesOrig = timeSeries;
145 
146  const unsigned int traceLength = fNoiseTraces[channelID][traceIndex].size();
147  for (unsigned int i = 0; i < timeSeries.GetSize(); ++i) {
148  // get (shifted) adc count
149  unsigned int sampleIndexNoise = (sampleIndexNoiseStart + i) % traceLength;
150 
151  const int noiseADCValue = fNoiseTraces.at(channelID).at(traceIndex).at(sampleIndexNoise);
152 
153  // // account for gain correction (if wanted)
154  // const double mean = utl::TraceAlgorithm::Mean(timeSeriesOrig, 0, timeSeriesOrig.GetSize() - 1);
155  // const int int_mean = int(std::abs(mean));
156  // const int noiseADCValue = int(tmpADCValue * pow(10.0, fGainFactor / 20) + int_mean * (1 - pow(10.0, fGainFactor/ 20)));
157 
158  int val = timeSeries[i] + noiseADCValue;
159 
161  val = noiseADCValue;
162 
163  // ADC count can not be out of range. This channel has to be flagged later.
164  if (val > maxVoltage/voltagePerCount -1) {
165  val = maxVoltage/voltagePerCount -1;
166  infostr.str("");
167  infostr << "ADC overflow (ADC = " << val << ", bin: " << i << ").\tStation "
168  << channel.GetStationId() << ")\tnoise = " << noiseADCValue << "\tsignal = " << timeSeries[i];
169  INFODebug(infostr);
170  }
171  if (val < minVoltage/voltagePerCount) {
172  val = minVoltage/voltagePerCount;
173  infostr.str("");
174  infostr << "ADC underflow (ADC = " << val << ", bin: " << i << ").\tStation "
175  << channel.GetStationId() << ")\tnoise = " << noiseADCValue << "\tsignal = " << timeSeries[i];
176  INFODebug(infostr);
177  }
178 
179  timeSeries[i] = val;
180  }
181  }
182  }
183 
184  return eSuccess;
185  }
186 
188  RdChannelASCIINoiseImporterRD::Finish()
189  {
190  return eSuccess;
191  }
192 
193  void
194  RdChannelASCIINoiseImporterRD::ReadNoiseFile(const std::string fileName, const unsigned int channelId) {
195  std::string line;
196  std::ifstream noiseFile(fileName);
197  if (!noiseFile)
198  ERROR("Could not imported noise from asci file.");
199 
200  std::vector<std::vector<int>> noiseTracesChannel;
201  if (noiseFile.is_open())
202  {
203  while (getline(noiseFile, line))
204  {
205  if (line[0] == '#') // skip any comments
206  continue;
207 
208  std::vector<std::string> lineSplited;
209  boost::split(lineSplited, line, [](char c) { return c == ','; });
210  std::vector<int> noiseTrace;
211  // assumes that first 2 entries of each row is something else than trace
212  for (unsigned int column = fSkipTrailingColumnsInNoiseFile; column < lineSplited.size(); column++)
213  noiseTrace.push_back(std::stoi(lineSplited[column]));
214 
215  noiseTracesChannel.push_back(noiseTrace);
216  }
217  noiseFile.close();
218  }
219  fNoiseTraces.at(channelId) = noiseTracesChannel;
220  }
221 
222 }
std::vector< std::vector< std::vector< int > > > fNoiseTraces
Report success to RunController.
Definition: VModule.h:62
Detector description interface for Station-related data.
Interface class to access to the Radio part of an event.
Definition: REvent.h:42
Skip remaining modules in the current loop and continue with next iteration of the loop...
Definition: VModule.h:68
void ReadNoiseFile(const std::string fileName, const unsigned int channelId)
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
double pow(const double x, const unsigned int i)
bool HasREvent() const
int fInfoLevel
Definition: VModule.h:123
Detector description interface for RDetector-related data.
Definition: RDetector.h:46
Class representing a document branch.
Definition: Branch.h:107
Top of the hierarchy of the detector description interface.
Definition: Detector.h:81
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
#define INFODebug(y)
Definition: VModule.h:163
const Channel & GetChannel(const int id) const
Get specified Channel by id.
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
Report failure to RunController, causing RunController to terminate execution.
Definition: VModule.h:64
const rdet::RDetector & GetRDetector() const
Definition: Detector.cc:143
#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
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)

, generated on Tue Sep 26 2023.