2 #include <utl/ErrorLogger.h>
4 #include <fwk/CentralConfig.h>
7 #include <revt/REvent.h>
8 #include <revt/Header.h>
9 #include <revt/Station.h>
10 #include <revt/Channel.h>
11 #include <revt/StationRecData.h>
12 #include <revt/StationGPSData.h>
14 #include <rdet/RDetector.h>
15 #include <rdet/Channel.h>
17 #include <utl/Trace.h>
18 #include <utl/TraceAlgorithm.h>
19 #include <utl/Reader.h>
20 #include <utl/config.h>
21 #include <utl/AugerUnits.h>
22 #include <utl/SaveCurrentTDirectory.h>
27 #include <TProfile2D.h>
29 #include <boost/scoped_ptr.hpp>
47 INFO(
"RdChannelGalacticConstantsGenerator::Init()");
50 CentralConfig::GetInstance()->
GetTopBranch(
"RdChannelGalacticConstantsGenerator");
59 fOutfile.open(fOutputFileName.c_str());
66 RdChannelGalacticConstantsGenerator::Run(
evt::Event& event)
68 INFO(
"RdChannelGalacticConstantsGenerator::Run()");
70 REvent& rEvent =
event.GetREvent();
72 for (
auto& station : rEvent.CandidateStationsRange()) {
73 const int stationId = station.GetId() - 100;
74 const unsigned long gpsSec = station.GetGPSData().GetSecond();
76 if (gpsSec < fLowestGPSSec)
77 fLowestGPSSec = gpsSec;
78 if (gpsSec > fHighestGPSSec)
79 fHighestGPSSec = gpsSec;
81 for (
auto& channel : station.ChannelsRange()) {
83 if (!channel.IsActive())
86 const int channelId = channel.GetId();
87 const rdet::RDetector& rDetector = det::Detector::GetInstance().GetRDetector();
94 const int lowerBin = int(lowerDesignFreq/spectrum.
GetBinning());
95 const int upperBin = int(upperDesignFreq/spectrum.
GetBinning());
96 const int nSpecBins = upperBin - lowerBin;
98 const double lowerFreq = channel.GetFrequencyOfBin(lowerBin + 1) /
utl::MHz;
99 const double upperFreq = channel.GetFrequencyOfBin(upperBin + 1) /
utl::MHz;
101 const unsigned int fSpectrumSize = spectrum.
GetSize();
104 const double lst = GPSSecToLSTHourAuger(gpsSec);
107 const int stationNumber = 200;
108 const int lstBins = 144;
112 fLSTSpecNS =
new TProfile3D(
"LSTSpecNS",
"LSTSpecNS",
113 stationNumber, 0, stationNumber,
115 nSpecBins, lowerFreq, upperFreq);
116 fLSTSpecEW =
new TProfile3D(
"LSTSpecEW",
"LSTSpecEW",
117 stationNumber, 0, stationNumber,
119 nSpecBins, lowerFreq, upperFreq);
123 double intensity = 0;
124 int intensityEntries = 0;
126 if (fLSTSpecNS->GetNbinsZ() != nSpecBins) {
128 warn <<
"Number of spectral bins (" << nSpecBins
129 <<
") does not correspond to profile bins (" << fLSTSpecNS->GetNbinsZ()
130 <<
"). Input data probably has multiple trace lengths.";
136 const double currentFreq = channel.GetFrequencyOfBin(i) /
utl::MHz;
137 if (lowerFreq <= currentFreq && currentFreq < upperFreq) {
138 intensity +=
abs(spectrum[i])*
abs(spectrum[i]);
142 if (
sqrt(intensity) / intensityEntries > fMaxIntensity) {
144 warn <<
"Average intensity above cut, skipping this trace from station "
145 << stationId + 100 <<
" channel " << channelId;
152 if (channelId == 1) {
153 const double currentFreq = channel.GetFrequencyOfBin(i) /
utl::MHz;
154 if (lowerFreq <= currentFreq && currentFreq < upperFreq) {
159 if (channelId == 2) {
160 const double currentFreq = channel.GetFrequencyOfBin(i) /
utl::MHz;
161 if (lowerFreq <= currentFreq && currentFreq < upperFreq) {
174 RdChannelGalacticConstantsGenerator::Finish()
176 INFO(
"RdChannelGalacticConstantsGenerator::Finish()");
181 const boost::scoped_ptr<TFile>
file(
new TFile(fFileNameROOTFile.c_str(),
"RECREATE"));
182 TTree*
const tree =
new TTree(
"fTree",
"data");
183 tree->Branch(
"pLSTSpecNS",
"TProfile3D", &fLSTSpecNS);
184 tree->Branch(
"pLSTSpecEW",
"TProfile3D", &fLSTSpecEW);
185 tree->Branch(
"fLowestGPSSec", &fLowestGPSSec);
186 tree->Branch(
"fHighestGPSSec", &fHighestGPSSec);
196 vector<int> vActiveStations;
197 int nIgnoreCorrection = 0;
198 const boost::scoped_ptr<TH1D> hLSTSpecNSprojX(fLSTSpecNS->ProjectionX(
"hLSTSpecNSprojX"));
199 std::ostringstream info;
200 info <<
"Active station IDs for which calibration constants will be calculated: ";
201 for (
int jj = 1; jj <= hLSTSpecNSprojX->GetNbinsX(); ++jj) {
202 if (hLSTSpecNSprojX->GetBinContent(jj)) {
203 vActiveStations.push_back(jj - 1);
204 info << jj+99 <<
' ';
205 for (
unsigned int iignore = 0; iignore < fIgnoreStations.size(); ++iignore) {
206 if (jj-1 == fIgnoreStations[iignore]-100) {
208 info <<
"(ignored for average) ";
215 const int nActiveStations = vActiveStations.size();
218 const int totalLSTbins = fLSTSpecNS->GetNbinsY();
219 const boost::scoped_ptr<TH2D> hLSTSpecNSprojLST((TH2D*)fLSTSpecNS->Project3D(
"xy"));
220 hLSTSpecNSprojLST->SetName(
"hLSTSpecNSprojLST");
221 for (
int jj = 0; jj < nActiveStations; ++jj) {
223 for (
int ybin = 1; ybin <= hLSTSpecNSprojLST->GetNbinsY(); ++ybin) {
224 const int bin = hLSTSpecNSprojLST->GetBin(ybin, vActiveStations[jj]+1);
225 if (hLSTSpecNSprojLST->GetBinContent(bin))
228 const double lstFraction= double(lstBins) / totalLSTbins;
229 if (lstFraction < 1) {
230 std::ostringstream warn;
231 warn <<
"Data from station " << vActiveStations[jj]+100
232 <<
" does not fully cover LST range (fraction = " << lstFraction
233 <<
"). This will result in a wrong calibration!";
239 const boost::scoped_ptr<TProfile3D> pLSTSpecNSFiltered((TProfile3D*)fLSTSpecNS->Clone());
241 fLSTSpecNS =
nullptr;
242 const boost::scoped_ptr<TProfile3D> pLSTSpecEWFiltered((TProfile3D*)fLSTSpecEW->Clone());
244 fLSTSpecEW =
nullptr;
245 pLSTSpecNSFiltered->SetName(
"LSTSpecNSFiltered");
246 pLSTSpecEWFiltered->SetName(
"LSTSpecEWFiltered");
247 for (
int iz = 1; iz <= pLSTSpecNSFiltered->GetNbinsZ(); ++iz) {
248 for (
int ix = 1; ix <= pLSTSpecNSFiltered->GetNbinsX(); ++ix) {
249 for (
int iy = 1; iy <= pLSTSpecNSFiltered->GetNbinsY(); ++iy) {
250 const int currentbin = pLSTSpecNSFiltered->GetBin(ix, iy, iz);
251 if (pLSTSpecNSFiltered->GetBinContent(currentbin) < 1e-9) {
252 pLSTSpecNSFiltered->SetBinContent(currentbin, 0);
253 pLSTSpecNSFiltered->SetBinEntries(currentbin, 0);
258 for (
int iz = 1; iz <= pLSTSpecEWFiltered->GetNbinsZ(); ++iz) {
259 for (
int ix = 1; ix <= pLSTSpecEWFiltered->GetNbinsX(); ++ix) {
260 for (
int iy = 1; iy <= pLSTSpecEWFiltered->GetNbinsY(); ++iy) {
261 const int currentbin = pLSTSpecEWFiltered->GetBin(ix, iy, iz);
262 if (pLSTSpecEWFiltered->GetBinContent(currentbin) < 1e-9) {
263 pLSTSpecEWFiltered->SetBinContent(currentbin, 0);
264 pLSTSpecEWFiltered->SetBinEntries(currentbin, 0);
271 const boost::scoped_ptr<TProfile3D> pLSTSpecNSGoodStations((TProfile3D*)pLSTSpecNSFiltered->Clone());
272 const boost::scoped_ptr<TProfile3D> pLSTSpecEWGoodStations((TProfile3D*)pLSTSpecEWFiltered->Clone());
273 pLSTSpecNSGoodStations->SetName(
"LSTSpecNSGoodStations");
274 pLSTSpecEWGoodStations->SetName(
"LSTSpecEWGoodStations");
275 for (
unsigned int iignore = 0; iignore < fIgnoreStations.size(); ++iignore) {
276 for (
int iz = 1; iz <= pLSTSpecNSGoodStations->GetNbinsZ(); ++iz) {
277 for (
int iy = 1; iy <= pLSTSpecNSGoodStations->GetNbinsY(); ++iy) {
278 int zerobin = pLSTSpecNSGoodStations->GetBin(fIgnoreStations[iignore]-99, iy, iz);
279 pLSTSpecNSGoodStations->SetBinContent(zerobin, 0);
280 pLSTSpecNSGoodStations->SetBinEntries(zerobin, 0);
281 zerobin = pLSTSpecEWGoodStations->GetBin(fIgnoreStations[iignore]-99, iy, iz);
282 pLSTSpecEWGoodStations->SetBinContent(zerobin, 0);
283 pLSTSpecEWGoodStations->SetBinEntries(zerobin, 0);
289 const boost::scoped_ptr<TH2D> lstSpecNSFilteredzy((TH2D*)pLSTSpecNSGoodStations->Project3D(
"zy"));
290 lstSpecNSFilteredzy->SetName(
"LSTSpecNSFiltered_zy");
291 lstSpecNSFilteredzy->Scale(1. / (nActiveStations - nIgnoreCorrection));
292 const boost::scoped_ptr<TH2D> lstSpecEWFilteredzy((TH2D*)pLSTSpecEWGoodStations->Project3D(
"zy"));
293 lstSpecEWFilteredzy->SetName(
"LSTSpecEWFiltered_zy");
294 lstSpecEWFilteredzy->Scale(1. / (nActiveStations - nIgnoreCorrection));
296 info <<
"The following averaged calibration constants were calculated (NS, EW):";
299 for (
int jj = 0; jj < nActiveStations; ++jj) {
302 pLSTSpecNSFiltered->GetXaxis()->SetRange(vActiveStations[jj]+1, vActiveStations[jj]+1);
303 pLSTSpecEWFiltered->GetXaxis()->SetRange(vActiveStations[jj]+1, vActiveStations[jj]+1);
305 str1 <<
"zy_stat" << vActiveStations[jj] <<
" ns", vActiveStations[jj];
307 const boost::scoped_ptr<TProfile2D> pLSTSpecNSFilteredDivided((TProfile2D*)pLSTSpecNSFiltered->Project3D(str1.str().c_str()));
309 str1 <<
"zy_stat" << vActiveStations[jj] <<
" ew";
310 const boost::scoped_ptr<TProfile2D> pLSTSpecEWFilteredDivided((TProfile2D*)pLSTSpecEWFiltered->Project3D(str1.str().c_str()));
311 pLSTSpecNSFilteredDivided->Divide(lstSpecNSFilteredzy.get());
312 pLSTSpecEWFilteredDivided->Divide(lstSpecEWFilteredzy.get());
315 const boost::scoped_ptr<TH1D> hCalibrationConstantsNS(pLSTSpecNSFilteredDivided->ProjectionY());
316 const boost::scoped_ptr<TH1D> hCalibrationConstantsEW(pLSTSpecEWFilteredDivided->ProjectionY());
317 hCalibrationConstantsNS->Scale(1. / totalLSTbins);
318 hCalibrationConstantsEW->Scale(1. / totalLSTbins);
321 for (
int ibin = 1; ibin <= hCalibrationConstantsNS->GetNbinsX(); ++ibin) {
322 fOutfile << vActiveStations[jj] <<
" 1 " << hCalibrationConstantsNS->GetBinCenter(ibin)
323 <<
" " << hCalibrationConstantsNS->GetBinContent(ibin) << endl;
325 for (
int ibin = 1; ibin <= hCalibrationConstantsEW->GetNbinsX(); ++ibin) {
326 fOutfile << vActiveStations[jj] <<
" 2 " << hCalibrationConstantsEW->GetBinCenter(ibin)
327 <<
" " << hCalibrationConstantsEW->GetBinContent(ibin) << endl;
331 info <<
"\n station ID " << vActiveStations[jj]+100 <<
": ";
332 double averageConstantsNS = 0;
333 int constentries = 0;
334 for (
int ibin = 0; ibin < hCalibrationConstantsNS->GetNbinsX(); ++ibin) {
335 if (hCalibrationConstantsNS->GetBinContent(ibin)) {
336 averageConstantsNS += hCalibrationConstantsNS->GetBinContent(ibin);
340 averageConstantsNS /= constentries;
341 double averageConstantsEW = 0;
343 for (
int ibin = 0; ibin < hCalibrationConstantsEW->GetNbinsX(); ++ibin) {
344 if (hCalibrationConstantsEW->GetBinContent(ibin)) {
345 averageConstantsEW += hCalibrationConstantsEW->GetBinContent(ibin);
349 averageConstantsEW /= constentries;
350 info <<
'(' << averageConstantsNS <<
", " << averageConstantsEW <<
")";
362 #define J2000 2451545.0
363 #define Dtohr 0.066666666666666
368 RdChannelGalacticConstantsGenerator::GPSSecToLSTHourAuger(
const int gps)
371 const time_t utc = gps + 315964819 + 16;
372 const tm*
const t = gmtime(&utc);
373 int year = t->tm_year + 1900;
374 int month = t->tm_mon + 1;
375 const int day = t->tm_mday;
376 const int hour = t->tm_hour;
377 const int min = t->tm_min;
378 const int sec = t->tm_sec;
386 const int a = int(floor(year / 100.));
387 const int b = 2 - a + a/4;
388 double jd = floor(365.25*(year + 4716)) + floor(30.6001*(month + 1)) + day + b - 1524.5;
389 const double fd = double(60*60*hour + 60*min + sec) / 86400;
392 static double sidereal_coeff[4] = { 280.46061837, 360.98564736629, 0.000387933, 38710000 };
393 const double dt = jd -
J2000;
394 const double dt0 = dt / 36525.;
396 sidereal_coeff[0] + dt * sidereal_coeff[1] +
397 dt0 * dt0 * (sidereal_coeff[2] - dt0 / sidereal_coeff[3]);
400 const double lst = fmod(theta + (360 - 69.6), 360.) *
Dtohr;
Branch GetTopBranch() const
double GetDesignUpperFreq() const
Get design value of the freq-band.
Interface class to access to the Radio part of an event.
double GetDesignLowerFreq() const
Get design value of the freq-band.
double GetBinning() const
size of one slot
#define INFO(message)
Macro for logging informational messages.
void Init()
Initialise the registry.
Detector description interface for Channel-related data.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Detector description interface for RDetector-related data.
Class representing a document branch.
std::vector< std::complex< double > >::size_type SizeType
double abs(const SVector< n, T > &v)
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
ResultFlag
Flag returned by module methods to the RunController.
const Station & GetStation(const int stationId) const
Get station by Station Id.