RdChannelGalacticCalibrator.cc
Go to the documentation of this file.
2 
3 #include <fwk/CentralConfig.h>
4 
5 #include <evt/Event.h>
6 #include <revt/REvent.h>
7 #include <revt/Station.h>
8 #include <revt/Channel.h>
9 #include <revt/StationGPSData.h>
10 
11 #include <det/Detector.h>
12 #include <rdet/RDetector.h>
13 #include <rdet/Channel.h>
14 
15 #include <utl/ErrorLogger.h>
16 #include <utl/Trace.h>
17 #include <utl/Reader.h>
18 #include <utl/config.h>
19 #include <utl/AugerUnits.h>
20 
21 #include <fstream>
22 
23 
24 using namespace std;
25 using namespace utl;
26 using namespace fwk;
27 using namespace revt;
28 
29 
31 
34  {
35  Branch topBranch = CentralConfig::GetInstance()->GetTopBranch("RdChannelGalacticCalibrator");
36  topBranch.GetChild("InfoLevel").GetData(fInfoLevel);
37  topBranch.GetChild("NameConstantsFile").GetData(fNameConstantsFile);
38  topBranch.GetChild("RejectIfCalibrationFailed").GetData(fRejectIfCalibrationFailed);
39 
40  GetCalibrationConstants();
41  return eSuccess;
42  }
43 
44 
46  RdChannelGalacticCalibrator::Run(evt::Event& event)
47  {
48  REvent& rEvent = event.GetREvent();
49  ostringstream info;
50 
51  for (auto& station : rEvent.CandidateStationsRange()) {
52 
53  const int stationId = station.GetId() - 100;
54  const unsigned long gpsSec = station.GetGPSData().GetSecond();
55  int month = 0;
56  int year = 0;
57  GetDataEvent(gpsSec, year, month);
58 
59  info.str("");
60  info << "Run galactic calibration for station " << stationId
61  << " in " << month << "/" << year << ".";
62  INFODebug(info);
63 
64  bool stationRejected = false;
65 
66  for (auto& channel : station.ChannelsRange()) {
67  if (!channel.IsActive() || stationRejected)
68  continue;
69 
70  const int channelId = channel.GetId();
71  const auto& rDetector = det::Detector::GetInstance().GetRDetector();
72  const auto& detChannel = rDetector.GetStation(station.GetId()).GetChannel(channel.GetId());
73 
74  auto& spectrum = channel.GetChannelFrequencySpectrum();
75  const double lowerDesignFreq = detChannel.GetDesignLowerFreq();
76  const double upperDesignFreq = detChannel.GetDesignUpperFreq();
77  const int lowerBin = int(lowerDesignFreq / spectrum.GetBinning());
78  const int upperBin = int(upperDesignFreq / spectrum.GetBinning());
79  const double lowerFreq = channel.GetFrequencyOfBin(lowerBin + 1) / MHz;
80  const double upperFreq = channel.GetFrequencyOfBin(upperBin + 1) / MHz;
81 
82  bool frequencyFilled[100] = { false };
83 
84  const int indexMonth = GetMonthIndex(month, year);
85 
86  // save corrections factors in channel parameter storage
87  if (!channel.HasRecData()) {
88  channel.MakeRecData();
89  }
90 
91  for (ChannelFrequencySpectrum::SizeType f = 0; f < spectrum.GetSize(); ++f) {
92  const double currentFreq = channel.GetFrequencyOfBin(f) / MHz;
93  if (currentFreq > upperFreq || currentFreq < lowerFreq) {
94  continue;
95  }
96 
97  calibrationMap channelMap;
98  if (channelId == 1) {
99  channelMap = fCalibrationConstantChannel1[stationId];
100  }
101  if (channelId == 2) {
102  channelMap = fCalibrationConstantChannel2[stationId];
103  }
104 
105  const int intFreq = int(currentFreq);
106  const auto it = channelMap.find(make_pair(indexMonth, intFreq));
107  if (it == channelMap.end()) {
108  info.str("");
109  info << "no calibration constant in map for station " << stationId
110  << ", channel " << channel.GetId() << " at a frequency of "
111  << intFreq << "MHz.";
112 
113  if (fRejectIfCalibrationFailed) {
114  info << " Rejecting station " << stationId << ".";
115  INFOFinal(info);
116 
117  // does not fit perfectly but best we have
118  station.SetRejectedReason(eLNANotCalibrated);
119 
120  // break the current loop over frequencies and do not iterate other channels of same station
121  stationRejected = true;
122  break;
123  }
124 
125  INFOIntermediate(info);
126 
127  // do no correct current frequency but continue correcting other frequencies
128  continue;
129  }
130 
131  const double C0 = it->second;
132  spectrum[f] /= C0;
133 
134  if (!frequencyFilled[intFreq]) {
135  const auto par = GetParameterName(intFreq);
136  channel.GetRecData().SetParameter(par, C0);
137  frequencyFilled[intFreq] = true;
138  }
139  }
140  }
141  }
142 
143  return eSuccess;
144  }
145 
146 
148  RdChannelGalacticCalibrator::Finish()
149  {
150  return eSuccess;
151  }
152 
153 
154  // function to calculate LST
155  void
156  RdChannelGalacticCalibrator::GetDataEvent(const int gps,int& year, int& month)
157  {
158  const time_t utc = gps + 315964819 + 16; // GPS UTC offset, Leap Seconds have to be updated
159  const tm* const t = gmtime(&utc);
160  year = t->tm_year + 1900;
161  month = t->tm_mon + 1;
162  }
163 
164 
165  void
166  RdChannelGalacticCalibrator::GetCalibrationConstants()
167  {
168  ifstream obj;
169  obj.open(fNameConstantsFile.c_str());
170 
171  if (!obj.is_open()) {
172  const string error = "File with calibration constants not found. The trace cannot be corrected.";
173  ERROR(error);
174  throw utl::AugerException(error);
175  }
176 
177  ostringstream info;
178  info << "file found : " << fNameConstantsFile;
179  INFO(info);
180 
181  while (!obj.eof()) {
182  int channel, year, month, antenna, frequency;
183  double calConstant;
184  obj >> channel >> year >> month >> antenna >> frequency >> calConstant;
185 
186  const int indexMonth = GetMonthIndex(month, year);
187  if (channel == 1) {
188  fCalibrationConstantChannel1[antenna].insert({{indexMonth, frequency}, calConstant});
189  }
190  if (channel == 2){
191  fCalibrationConstantChannel2[antenna].insert({{indexMonth, frequency}, calConstant});
192  }
193  }
194  }
195 
196 
197  unsigned int
198  RdChannelGalacticCalibrator::GetMonthIndex(int month, int year)
199  {
200  return 12 * (year - fInitYear) + month;
201  }
202 
203 
204  revt::ChannelRRecDataQuantities
205  RdChannelGalacticCalibrator::GetParameterName(unsigned int intFreq)
206  {
207  if (intFreq == 30)
208  return eChannelGalacticCalibrationFactor30MHz;
209  if (intFreq == 31)
210  return eChannelGalacticCalibrationFactor31MHz;
211  if (intFreq == 32)
212  return eChannelGalacticCalibrationFactor32MHz;
213  if (intFreq == 33)
214  return eChannelGalacticCalibrationFactor33MHz;
215  if (intFreq == 34)
216  return eChannelGalacticCalibrationFactor34MHz;
217  if (intFreq == 35)
218  return eChannelGalacticCalibrationFactor35MHz;
219  if (intFreq == 36)
220  return eChannelGalacticCalibrationFactor36MHz;
221  if (intFreq == 37)
222  return eChannelGalacticCalibrationFactor37MHz;
223  if (intFreq == 38)
224  return eChannelGalacticCalibrationFactor38MHz;
225  if (intFreq == 39)
226  return eChannelGalacticCalibrationFactor39MHz;
227  if (intFreq == 40)
228  return eChannelGalacticCalibrationFactor40MHz;
229  if (intFreq == 41)
230  return eChannelGalacticCalibrationFactor41MHz;
231  if (intFreq == 42)
232  return eChannelGalacticCalibrationFactor42MHz;
233  if (intFreq == 43)
234  return eChannelGalacticCalibrationFactor43MHz;
235  if (intFreq == 44)
236  return eChannelGalacticCalibrationFactor44MHz;
237  if (intFreq == 45)
238  return eChannelGalacticCalibrationFactor45MHz;
239  if (intFreq == 46)
240  return eChannelGalacticCalibrationFactor46MHz;
241  if (intFreq == 47)
242  return eChannelGalacticCalibrationFactor47MHz;
243  if (intFreq == 48)
244  return eChannelGalacticCalibrationFactor48MHz;
245  if (intFreq == 49)
246  return eChannelGalacticCalibrationFactor49MHz;
247  if (intFreq == 50)
248  return eChannelGalacticCalibrationFactor50MHz;
249  if (intFreq == 51)
250  return eChannelGalacticCalibrationFactor51MHz;
251  if (intFreq == 52)
252  return eChannelGalacticCalibrationFactor52MHz;
253  if (intFreq == 53)
254  return eChannelGalacticCalibrationFactor53MHz;
255  if (intFreq == 54)
256  return eChannelGalacticCalibrationFactor54MHz;
257  if (intFreq == 55)
258  return eChannelGalacticCalibrationFactor55MHz;
259  if (intFreq == 56)
260  return eChannelGalacticCalibrationFactor56MHz;
261  if (intFreq == 57)
262  return eChannelGalacticCalibrationFactor57MHz;
263  if (intFreq == 58)
264  return eChannelGalacticCalibrationFactor58MHz;
265  if (intFreq == 59)
266  return eChannelGalacticCalibrationFactor59MHz;
267  if (intFreq == 60)
268  return eChannelGalacticCalibrationFactor60MHz;
269  if (intFreq == 61)
270  return eChannelGalacticCalibrationFactor61MHz;
271  if (intFreq == 62)
272  return eChannelGalacticCalibrationFactor62MHz;
273  if (intFreq == 63)
274  return eChannelGalacticCalibrationFactor63MHz;
275  if (intFreq == 64)
276  return eChannelGalacticCalibrationFactor64MHz;
277  if (intFreq == 65)
278  return eChannelGalacticCalibrationFactor65MHz;
279  if (intFreq == 66)
280  return eChannelGalacticCalibrationFactor66MHz;
281  if (intFreq == 67)
282  return eChannelGalacticCalibrationFactor67MHz;
283  if (intFreq == 68)
284  return eChannelGalacticCalibrationFactor68MHz;
285  if (intFreq == 69)
286  return eChannelGalacticCalibrationFactor69MHz;
287  if (intFreq == 70)
288  return eChannelGalacticCalibrationFactor70MHz;
289  if (intFreq == 71)
290  return eChannelGalacticCalibrationFactor71MHz;
291  if (intFreq == 72)
292  return eChannelGalacticCalibrationFactor72MHz;
293  if (intFreq == 73)
294  return eChannelGalacticCalibrationFactor73MHz;
295  if (intFreq == 74)
296  return eChannelGalacticCalibrationFactor74MHz;
297  if (intFreq == 75)
298  return eChannelGalacticCalibrationFactor75MHz;
299  if (intFreq == 76)
300  return eChannelGalacticCalibrationFactor76MHz;
301  if (intFreq == 77)
302  return eChannelGalacticCalibrationFactor77MHz;
303  if (intFreq == 78)
304  return eChannelGalacticCalibrationFactor78MHz;
305  if (intFreq == 79)
306  return eChannelGalacticCalibrationFactor79MHz;
307  if (intFreq == 80)
308  return eChannelGalacticCalibrationFactor80MHz;
309 
310  throw logic_error("no corresponding parameter found");
311  }
312 
313 }
Branch GetTopBranch() const
Definition: Branch.cc:63
Base class for all exceptions used in the auger offline code.
constexpr double MHz
Definition: AugerUnits.h:159
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
#define INFOIntermediate(y)
Definition: VModule.h:162
Class representing a document branch.
Definition: Branch.h:107
std::vector< std::complex< double > >::size_type SizeType
Definition: Trace.h:58
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
#define INFODebug(y)
Definition: VModule.h:163
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
#define INFOFinal(y)
Definition: VModule.h:161
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
const double year
Definition: GalacticUnits.h:22

, generated on Tue Sep 26 2023.