RdChannelNoiseImporter_RD.cc
Go to the documentation of this file.
2 
3 #include "TTree.h"
4 #include "TFile.h"
5 #include "TKey.h"
6 
7 #include <evt/Event.h>
8 #include <evt/ShowerSimData.h>
9 
10 #include <revt/REvent.h>
11 #include <revt/Header.h>
12 #include <revt/Station.h>
13 #include <revt/Channel.h>
14 
15 #include <det/Detector.h>
16 #include <rdet/RDetector.h>
17 #include <rdet/Station.h>
18 #include <rdet/Channel.h>
19 
20 #include <utl/Trace.h>
21 #include <utl/TraceAlgorithm.h>
22 #include <utl/ErrorLogger.h>
23 #include <utl/Reader.h>
24 #include <utl/config.h>
25 #include <utl/AugerUnits.h>
26 #include <utl/RandomEngine.h>
27 
28 #include <fwk/RandomEngineRegistry.h>
29 #include <fwk/CentralConfig.h>
30 
31 #include <CLHEP/Random/RandFlat.h>
32 
33 
34 using namespace fwk;
35 using namespace revt;
36 
37 
39 
42  {
43  utl::Branch topBranch = CentralConfig::GetInstance()->GetTopBranch("RdChannelNoiseImporter_RD");
44 
45  topBranch.GetChild("InfoLevel").GetData(fInfoLevel);
46  topBranch.GetChild("NoiseFilePath").GetData(fNoiseFilePath);
47  topBranch.GetChild("AddNoiseFromSameTimeToBothChannels").GetData(fAddNoiseFromSameTimeToBothChannels);
48  topBranch.GetChild("AllowTraceShifting").GetData(fTraceShifting);
49  topBranch.GetChild("ReplaceDataWithNoise").GetData(fReplaceDataWithNoise);
50 
51  fRandomEngine =
52  &RandomEngineRegistry::GetInstance().Get(RandomEngineRegistry::eDetector).GetEngine();
53 
54  return eSuccess;
55  }
56 
57 
59  RdChannelNoiseImporter_RD::Run(evt::Event& event)
60  {
61  // Check if there are events at all
62  if (!event.HasREvent()) {
63  WARNING("No radio event found!");
64  return eContinueLoop;
65  }
66 
67  ReadFromRootFile();
68  if (fAddNoiseFromSameTimeToBothChannels) {
69  if (fNoiseTraces.first.size() != fNoiseTraces.second.size() || fNoiseTraces.first.size() == 0) {
70  ERROR("Imported noise for the two channels are not same size or size is 0!");
71  return eFailure;
72  }
73  }
74 
75  std::ostringstream infostr;
76  infostr << "\n\tRead " << fNoiseTraces.first.size() << " traces for channel 0 and "
77  << fNoiseTraces.second.size() << " traces for channel 1"
78  << "\n\tAddNoiseFromSameTimeToBothChannels : " << fAddNoiseFromSameTimeToBothChannels
79  << "\n\tAllow trace shifting : " << fTraceShifting;
80 
81  infostr << std::endl;
82  INFO(infostr);
83 
84  const det::Detector& det = det::Detector::GetInstance();
85  REvent& rEvent = event.GetREvent();
86  const rdet::RDetector& rDetector = det.GetRDetector();
87 
88  // Adding noise to the data, depending on if the station exists in both: noise-file and data.
89  for (auto& station : rEvent.StationsRange()) {
90  const int stationID = station.GetId();
91 
92  // choose same trace (index) for both channel
93  unsigned int traceIndex = CLHEP::RandFlat::shootInt(fRandomEngine, 0, fNoiseTraces.first.size());
94  unsigned int sampleIndexNoiseStart = 0;
95 
96  // Traces have a length of 2048 (samples) / 250 MHz = 8192 ns
97  // Shift traces by multiple of 100ns, i.e., 25 sample
98  // This does not prevent the trace end to be within the signal window
99  if (fTraceShifting && fAddNoiseFromSameTimeToBothChannels) {
100  sampleIndexNoiseStart = CLHEP::RandFlat::shootInt(fRandomEngine, 0, 25) *
101  fNoiseTraces.first[traceIndex].GetSize() / 25;
102  }
103 
104  const rdet::Station& rDetStation = rDetector.GetStation(stationID);
105  for (auto& channel : station.ChannelsRange()) {
106  auto& timeSeries = channel.GetChannelADCTimeSeries();
107  const unsigned int channelID = channel.GetId();
108 
109  // Checking if the channel of the processed station contribute to the event.
110  // If not, continue with the next channel.
111  if (!station.HasChannel(channelID) || !channel.IsActive())
112  continue;
113 
114  std::vector<ChannelADCTimeSeries> tempVec;
115  if (channelID == 0) {
116  tempVec = fNoiseTraces.first;
117  } else {
118  tempVec = fNoiseTraces.second;
119  }
120 
121  if (!fAddNoiseFromSameTimeToBothChannels) {
122  // choose trace and shift for each channel independently
123  traceIndex = CLHEP::RandFlat::shootInt(fRandomEngine, 0, tempVec.size());
124  if (fTraceShifting) {
125  sampleIndexNoiseStart = CLHEP::RandFlat::shootInt(fRandomEngine, 0, 25) *
126  tempVec[traceIndex].GetSize() / 25;
127  }
128  }
129 
130  // Get number of adc counts from RDetector
131  const short bitDepth = rDetStation.GetChannel(channel.GetId()).GetBitDepth();
132  const short numberOfADCCounts = pow(2., bitDepth) / 2;
133 
134  // Finally adding the noise event to the processed pedestal free event.
135  const ChannelADCTimeSeries timeSeriesOrig = timeSeries;
136 
137  const unsigned int traceLength = tempVec[traceIndex].GetSize();
138  for (unsigned int i = 0; i < timeSeries.GetSize(); ++i) {
139  // get (shifted) adc count
140  unsigned int sampleIndexNoise = (sampleIndexNoiseStart + i) % traceLength;
141 
142  const int noiseADCValue = tempVec.at(traceIndex).At(sampleIndexNoise);
143 
144  int val = timeSeries[i] + noiseADCValue;
145 
146  if (fReplaceDataWithNoise)
147  val = noiseADCValue;
148 
149  // ADC count can not be out of range. This channel has to be flagged later.
150  if (val > numberOfADCCounts - 1) {
151  val = numberOfADCCounts - 1;
152  infostr.str("");
153  infostr << "ADC overflow (ADC = " << val << ", bin: " << i << ").\tStation "
154  << channel.GetStationId() << ")\tnoise = " << noiseADCValue << "\tsignal = " << timeSeries[i];
155  INFODebug(infostr);
156  }
157 
158  if (val < -1 * numberOfADCCounts) {
159  val = -1 * numberOfADCCounts;
160  infostr.str("");
161  infostr << "ADC overflow (ADC = " << val << ", bin: " << i << ").\tStation "
162  << channel.GetStationId() << ")\tnoise = " << noiseADCValue << "\tsignal = " << timeSeries[i];
163  INFODebug(infostr);
164  }
165 
166  timeSeries[i] = val;
167  }
168  }
169  }
170 
171  return eSuccess;
172  }
173 
174 
176  RdChannelNoiseImporter_RD::Finish()
177  {
178  return eSuccess;
179  }
180 
181 
183  RdChannelNoiseImporter_RD::ReadFromRootFile()
184  {
185  std::ifstream noiseFile(fNoiseFilePath);
186  if (!noiseFile) {
187  ERROR("Given noise file not existent.");
188  return eFailure;
189  }
190 
191  TFile* fileIn = TFile::Open(fNoiseFilePath.c_str());
192  TList* keys = fileIn->GetListOfKeys();
193  int numTrees = 0;
194  TKey* key;
195  TIter next(keys);
196  while ((key = (TKey*)next())) {
197  TObject* obj = key->ReadObj();
198  if (obj->InheritsFrom("TTree")) {
199  numTrees++;
200  }
201  }
202 
203  for(int counter = 0; counter < numTrees -1; ++counter) {
204  std::string treename = "Noisetree_" + std::to_string(counter);
205  TTree* noiseTree = (TTree*)fileIn->Get(treename.c_str());
206 
208 
209  TBranch* traceCh0Branch = noiseTree->GetBranch("traceCh0");
210  traceCh0Branch->SetAddress(&noiseEvent.traceCh0);
211  TBranch* traceCh1Branch = noiseTree->GetBranch("traceCh1");
212  traceCh1Branch->SetAddress(&noiseEvent.traceCh1);
213  /*
214  // not available with daves noise files but needed in the future
215  TBranch* stationIdBranch = noiseTree->GetBranch("stationId");
216  stationIdBranch->SetAddress(&noiseEvent.stationId);
217  TBranch* secBranch = noiseTree->GetBranch("sec");
218  secBranch->SetAddress(&noiseEvent.sec);
219  TBranch* nanosecBranch = noiseTree->GetBranch("nanosec");
220  nanosecBranch->SetAddress(&noiseEvent.nanosec);
221  TBranch* evtnoBranch = noiseTree->GetBranch("evtno");
222  evtnoBranch->SetAddress(&noiseEvent.evtno);
223  noiseTree->GetEntry(0);
224  */
225 
226  ChannelADCTimeSeries noiseTraceCh0;
227  ChannelADCTimeSeries noiseTraceCh1;
228 
229  for (const auto& bin : noiseEvent.traceCh0) {
230  noiseTraceCh0.PushBack(bin);
231  }
232  for (const auto& bin : noiseEvent.traceCh1) {
233  noiseTraceCh1.PushBack(bin);
234  }
235 
236  fNoiseTraces.first.push_back(noiseTraceCh0);
237  fNoiseTraces.second.push_back(noiseTraceCh1);
238  }
239 
240  return eSuccess;
241  }
242 
243 }
244 
Detector description interface for Station-related data.
Interface class to access to the Radio part of an event.
Definition: REvent.h:42
#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
Detector description interface for RDetector-related data.
Definition: RDetector.h:46
Iterator next(Iterator it)
Class representing a document branch.
Definition: Branch.h:107
Top of the hierarchy of the detector description interface.
Definition: Detector.h:81
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
#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.
const rdet::RDetector & GetRDetector() const
Definition: Detector.cc:143
void PushBack(const T &value)
Insert a single value at the end.
Definition: Trace.h:119
#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.