RdChannelNoiseASCIIImporter.cc
Go to the documentation of this file.
2 #include <utl/ErrorLogger.h>
3 
4 #include <evt/Event.h>
5 #include <revt/REvent.h>
6 #include <revt/Header.h>
7 #include <revt/Station.h>
8 #include <revt/Channel.h>
9 
10 #include <det/Detector.h>
11 #include <rdet/RDetector.h>
12 #include <rdet/Station.h>
13 #include <rdet/Channel.h>
14 
15 #include <utl/Trace.h>
16 #include <utl/TraceAlgorithm.h>
17 #include <utl/ErrorLogger.h>
18 #include <utl/Reader.h>
19 #include <utl/config.h>
20 #include <utl/AugerUnits.h>
21 
22 #include <vector>
23 #include <map>
24 #include <algorithm>
25 #include <fwk/CentralConfig.h>
26 
27 #include <time.h>
28 #include <sstream>
29 
30 #include <boost/tokenizer.hpp>
31 #include <boost/algorithm/string/replace.hpp>
32 using namespace std;
33 
34 using namespace std;
35 using namespace utl;
36 using namespace fwk;
37 using namespace revt;
38 
39 typedef boost::tokenizer<boost::char_separator<char> > mytok;
40 
42 
44  fNumberOfFiles(0),
45  fInfoLevel(0),
46  fPathToAsciitrace(""),
47  fUseAlwaysTheSameTrace(false),
48  fUseRandomSeed(false),
49  fStopAtLastNoiseTrace(true)
50 {
51 }
52 
53 RdChannelNoiseASCIIImporter::~RdChannelNoiseASCIIImporter()
54 {
55 }
56 
58 {
59 
60  // Initialize your module here. This method
61  // is called once at the beginning of the run.
62  // The eSuccess flag indicates the method ended
63  // successfully. For other possible return types,
64  // see the VModule documentation.
65 
66  INFO("RdChannelNoiseASCIIImporter::Init()");
67 
68  Branch topBranch = CentralConfig::GetInstance()->GetTopBranch("RdChannelNoiseASCIIImporter");
69  topBranch.GetChild("NoiseFilePath").GetData(fPathToAsciitrace);
70  topBranch.GetChild("InfoLevel").GetData(fInfoLevel);
71  topBranch.GetChild("NumberOfTraces").GetData(fNumberOfFiles);
72  topBranch.GetChild("UseAlwaysTheSameTrace").GetData(fUseAlwaysTheSameTrace);
73  topBranch.GetChild("UseRandomSeed").GetData(fUseRandomSeed);
74  topBranch.GetChild("StopAtLastNoiseTrace").GetData(fStopAtLastNoiseTrace);
75  //Add a / at the end of the path if missing
76  if (fPathToAsciitrace.compare(fPathToAsciitrace.size() - 1, fPathToAsciitrace.size(), "/") != 0)
77  fPathToAsciitrace.append("/");
78  if (fUseRandomSeed) {
79  std::srand(time(NULL));
80  }
81  //Randomize the available traces
82  for (int i = 1; i <= fNumberOfFiles; i++) {
83  v_id.push_back(i);
84  }
85  std::random_shuffle(v_id.begin(), v_id.end());
86  viter = v_id.begin();
87  return eSuccess;
88 }
89 
90 VModule::ResultFlag RdChannelNoiseASCIIImporter::Run(evt::Event& event)
91 {
92  INFO("RdChannelNoiseASCIIImporter::Run()");
93  if (viter == v_id.end()) {
94  if (!fStopAtLastNoiseTrace) {
95  WARNING("end of noise list reached, traces will be reshuffled and used again");
96  std::random_shuffle(v_id.begin(), v_id.end());
97  viter = v_id.begin();
98  } else {
99  cerr << "End of the Noise List" << endl;
100  return eContinueLoop;
101  }
102  }
103  vector<int> v_stationid;
104  ostringstream fname;
105  fname.str("");
106  fname << fPathToAsciitrace << "Traces_" << AddZero(*viter) << *viter << ".dat";
107  INFO(fname.str());
108  ifstream inf(fname.str().c_str());
109  while (!inf.good()) {
110  INFO("file does not exist, moving to next file");
111  ++viter;
112  if (viter == v_id.end()) {
113  if (!fStopAtLastNoiseTrace) {
114  WARNING("end of noise list reached, traces will be reshuffled and used again");
115  std::random_shuffle(v_id.begin(), v_id.end());
116  viter = v_id.begin();
117  } else {
118  cerr << "End of the Noise List" << endl;
119  return eContinueLoop;
120  }
121  }
122  fname.str("");
123  fname << fPathToAsciitrace << "Traces_" << AddZero(*viter) << *viter << ".dat";
124  INFO(fname.str());
125  inf.open(fname.str().c_str());
126  }
127  if (!fUseAlwaysTheSameTrace) {
128  ++viter; // We go here to the next value of viter
129  }
130  string zeline;
131  boost::char_separator<char> sep(" ");
132  map<string, vector<int> > m_traces;
133  m_traces.clear();
134  int currentStationId = 0;
135  while (getline(inf, zeline)) {
136  vector<int> v_values;
137  vector<string> v_tokens;
138  // Just in case of clear the vector
139  v_values.clear();
140  v_tokens.clear();
141  mytok tok(zeline, sep); //Split the line into tok;
142  for (mytok::const_iterator titer = tok.begin(); titer != tok.end(); ++titer) { //Put all tokens in a vector;
143  v_tokens.push_back(*titer);
144  }
145  for (unsigned int i = 3; i < v_tokens.size(); i++) {
146  int val = atoi(v_tokens[i].c_str());
147  v_values.push_back(val);
148  }
149  string name = GetStringFromStatChan(atoi(v_tokens[0].c_str()), atoi(v_tokens[1].c_str()));
150  if(atoi(v_tokens[0].c_str()) != currentStationId) {
151  v_stationid.push_back(atoi(v_tokens[0].c_str()));
152  currentStationId = atoi(v_tokens[0].c_str());
153  }
154  RemovePedestal(v_values);
155  m_traces[name] = v_values;
156  }
157  inf.close();
158  random_shuffle(v_stationid.begin(), v_stationid.end());
159  vector<int>::iterator randomstationiterator = v_stationid.begin();
160 
161  // Check if there are events at all
162  if (!event.HasREvent()) {
163  WARNING("RdChannelNoiseASCIIImporter::No radio event found!");
164  return eContinueLoop;
165  } else {
166  det::Detector& det = det::Detector::GetInstance();
167  ostringstream out;
168  REvent& rEvent = event.GetREvent();
169  // loop through stations and for every station through every channel
170  bool randomNoiseStationUsed = false;
171  // reshuffle random station iterator before each event to prevent selecting the same noise trace twice in one event
172  for (REvent::StationIterator sIt = rEvent.StationsBegin(); sIt != rEvent.StationsEnd(); ++sIt) {
173  Station& station = *sIt;
174  if(randomNoiseStationUsed) {
175  std::advance(randomstationiterator, 1); // increase random station iterator by 1 to get a different station next time
176  randomNoiseStationUsed = false;
177  }
178  for (Station::ChannelIterator cIt = station.ChannelsBegin(); cIt != station.ChannelsEnd();
179  ++cIt) {
180  Channel& channel = *cIt;
181  revt::ChannelTimeSeries& timeSeries = channel.GetChannelTimeSeries();
182 
183  string name = GetStringFromStatChan(station.GetId(), channel.GetId());
184  map<string, vector<int> >::const_iterator miter = m_traces.find(name);
185  if (miter == m_traces.end()) {
186  if (randomstationiterator == v_stationid.end()) {
187  std::random_shuffle(v_stationid.begin(), v_stationid.end());
188  randomstationiterator = v_stationid.begin();
189 //#warning TH: this randomization is dangerous because the sample rate can be different for different stations and this is not checked for!
190  }
191  randomNoiseStationUsed = true;
192  out.str("");
193  out << " Unable to Find Station_Channel " << name;
194  name = GetStringFromStatChan(*randomstationiterator, channel.GetId());
195  out << " -> Will use a station " << name << " instead";
196  if (fInfoLevel > eFinal) {
197  INFO(out.str());
198  }
199  miter = m_traces.find(name);
200  if (miter == m_traces.end()) {
201  if (fInfoLevel > eFinal) {
202  out.str("");
203  out << " Damned we are unlucky station " << name << " is not found I give up. Setting channel as inactive";
204  WARNING(out.str());
205  }
206  channel.SetNotActive();
207  continue;
208  }
209  }
210 
211  // get number of adc counts from RDetector
212  short bitDepth = det.GetRDetector().GetStation(station.GetId()).GetChannel(channel.GetId())
213  .GetBitDepth();
214  short numberOfADCCounts = pow(2., bitDepth) - 1;
215 
216  if (miter->second.size() != timeSeries.GetSize()) {
217  out.str("");
218  out << " Station_Channel " << name << "\n";
219  out << "Channel size " << timeSeries.GetSize() << " \n";
220  out << "Noise size " << miter->second.size() << "\n";
221  out << " Channel " << name
222  << " Is set as inactive, please check if this is coherent with what you are doing";
223  WARNING(out.str().c_str());
224  channel.SetNotActive();
225  continue;
226  }
227  for (unsigned int i = 0; i < timeSeries.GetSize(); ++i) {
228  int val = timeSeries[i] + miter->second[i];
229 
230  //Need to be done correctly, and info should be taken from detector geometry
231  if (val > numberOfADCCounts) {
232  out.str("");
233  out << "ADC overflow (ADC = " << val << ", bin " << i << " , station "
234  << cIt->GetStationId() << ")\tnoise = " << miter->second[i] << "\tsignal = "
235  << timeSeries[i];
236  WARNING(out.str());
237  val = numberOfADCCounts;
238  }
239  if (val < 0) {
240  out.str("");
241  out << "ADC underflow (ADC = " << val << ", bin " << i << " , station "
242  << cIt->GetStationId() << ")\tnoise = " << miter->second[i] << "\tsignal = "
243  << timeSeries[i];
244  WARNING(out.str());
245  val = 0;
246  }
247  timeSeries[i] = val;
248  }
249  } // for Channel
250  } //for Station
251  }
252  return eSuccess;
253 }
254 
255 VModule::ResultFlag RdChannelNoiseASCIIImporter::Finish()
256 {
257  // Put any termination or cleanup code here.
258  // This method is called once at the end of the run.
259 
260  INFO("RdChannelNoiseASCIIImporter::Finish()");
261 
262  return eSuccess;
263 }
264 
265 string RdChannelNoiseASCIIImporter::GetStringFromStatChan(int statid, int chanid)
266 {
267  ostringstream name;
268  name << statid << "_" << chanid;
269  return name.str();
270 }
271 
272 void RdChannelNoiseASCIIImporter::RemovePedestal(revt::ChannelTimeSeries& timeSeries)
273 {
274  double mean = utl::TraceAlgorithm::Mean(timeSeries, 0, timeSeries.GetSize() - 1);
275  int int_mean = int(fabs(mean));
276  for (revt::ChannelTimeSeries::Iterator it = timeSeries.Begin(); it != timeSeries.End(); ++it) {
277  *it = *it - int_mean;
278  }
279 }
280 
281 void RdChannelNoiseASCIIImporter::RemovePedestal(std::vector<int>& timeSeries)
282 {
283  double mean = 0.;
284  unsigned int N = 0;
285  for (std::vector<int>::iterator it = timeSeries.begin(); it != timeSeries.end(); ++it) {
286  mean += *it;
287  N += 1;
288  }
289  mean = mean / N;
290  int int_mean = int(fabs(mean));
291  for (std::vector<int>::iterator it = timeSeries.begin(); it != timeSeries.end(); ++it) {
292  *it = *it - int_mean;
293  }
294 }
295 }
Branch GetTopBranch() const
Definition: Branch.cc:63
int GetId() const
Return Id of the Channel.
Report success to RunController.
Definition: VModule.h:62
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
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
StationIterator StationsEnd()
Definition: REvent.h:130
StationIterator StationsBegin()
Definition: REvent.h:128
void Init()
Initialise the registry.
vector< t2list > out
output of the algorithm: a list of clusters
Definition: XbAlgo.cc:32
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
ChannelIterator ChannelsBegin()
begin Channel iterator for read/write
double pow(const double x, const unsigned int i)
boost::filter_iterator< StationFilter, AllStationIterator > StationIterator
Iterator over all (non-exculded) stations.
Definition: REvent.h:125
bool HasREvent() const
ChannelTimeSeries & GetChannelTimeSeries()
retrieve Channel Time Series (write access, only use this if you intend to change the data) ...
ChannelIterator ChannelsEnd()
end Channel iterator for read/write
int fNumberOfFiles
Writes out a spectrum in an ASCII file.
Iterator Begin()
Definition: Trace.h:75
Class representing a document branch.
Definition: Branch.h:107
class to hold data at the radio Station level.
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
int GetId() const
Get the station Id.
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
boost::tokenizer< boost::char_separator< char > > mytok
Class that holds the data associated to an individual radio channel.
const rdet::RDetector & GetRDetector() const
Definition: Detector.cc:143
void SetNotActive()
Iterator End()
Definition: Trace.h:76
const Station & GetStation(const int stationId) const
Get station by Station Id.
Definition: RDetector.cc:141
std::vector< double >::iterator Iterator
Definition: Trace.h:59

, generated on Tue Sep 26 2023.