3 #include <sevt/SEvent.h>
4 #include <fwk/CentralConfig.h>
5 #include <sdet/SDetector.h>
6 #include <utl/ErrorLogger.h>
7 #include <utl/zfstream.h>
9 #include <utl/Branch.h>
10 #include <utl/LineStringTo.h>
11 #include <utl/UTCDateTime.h>
14 #include <boost/range/istream_range.hpp>
19 namespace SdGainRatioCorrectorKG {
54 explicit operator bool()
const
58 GetObservedGainRatio()
65 const double minMe = 0.8;
69 const unsigned int minN = 5;
70 const unsigned int maxN = 35;
73 if (
Is(
fN).InRange(minN, maxN) &&
75 Is(
fMean).InRange(minMe, maxMe) &&
77 Is(
fStd).InRange(0, maxStd)) {
87 if (
Is(
fNFit).InRange(minNFit, maxNFit) &&
97 const double minMe = 0.5;
98 const double maxMe = 1.5;
99 const unsigned int minN = 5;
100 const unsigned int maxN = 50;
101 const double maxSlope = 0.1/373;
102 const double maxStd = 0.03;
103 if (
Is(
fN).InRange(minN, maxN) &&
104 Is(
fNFit).InRange(2*minN, maxN) &&
112 if (sigma <= maxStd &&
Is(mean).InRange(minMe, maxMe))
126 >> std::ws >> c.
fYear
128 >> std::ws >> c.
fMean
135 >> std::ws >> c.
fNFit;
144 const auto i = line.find_first_not_of(
" \t");
145 return i == std::string::npos || line[i] ==
'#';
149 template<std::
size_t n>
154 for (std::size_t i = 0; i < n; ++i)
173 const auto filename = topB.GetChild(
"gainRatioCorrectionsFile").Get<std::string>();
175 if (!ifs.is_open()) {
176 std::ostringstream err;
177 err <<
"Cannot open file '" <<
filename;
184 const auto r = gc.GetObservedGainRatio();
189 fFactors[gc.fYear][gc.fStationId].fPMT[gc.fPmtId - 1] = 1 / r;
194 ERROR(
"The database of gain-ration correction factors is empty!");
200 std::map<int, utl::SVector<Factors::fgNPmts, int>> stationPmtCounts;
202 const auto& y = yf.first;
204 for (
const auto& sf : yf.second) {
205 const auto&
s = sf.first;
206 auto& count = stationPmtCounts[
s];
207 const auto& pmt = sf.second.fPMT;
214 std::ostringstream info;
215 info <<
"For years [" << yearRange.
GetMin() <<
", " << yearRange.GetMax() <<
"] there are "
216 << stationPmtCounts.size() <<
" stations with gain-ratio correction factors.";
221 for (
const auto& sc : stationPmtCounts) {
224 if (info.str().empty())
225 info <<
"The following stations are missing yearly corrections for certain PMTs "
226 "(counts of years given in brackets): " << sc.first <<
" (" << sc.second <<
')';
228 info <<
", " << sc.first <<
" (" << sc.second <<
')';
232 info <<
"; a total of " << incomplete <<
" stations.";
245 auto& sEvent =
event.GetSEvent();
247 const auto& sDet = det::Detector::GetInstance().GetSDetector();
249 for (
auto& station : sEvent.StationsRange()) {
251 const bool isNotSim = !station.HasSimData();
254 if (!pmt.HasCalibData() || !pmt.GetCalibData().IsTubeOk())
256 double gainRatio = pmt.GetCalibData().GetGainRatio();
259 if (isLargeWCD && station.GetCalibData().GetVersion() == 12)
262 const auto stationId = station.GetId();
263 const auto pmtId = pmt.GetId();
264 gainRatio = sDet.GetStation(stationId).GetPMT(pmtId).GetGainRatio();
265 std::ostringstream warn;
266 warn <<
"Gain ratio zero encountered for station " << stationId <<
" PMT"
267 << pmtId <<
", replacing it with the nominal value " << gainRatio <<
"!";
271 if (!pmt.HasRecData())
273 pmt.GetRecData().SetGainRatio(gainRatio);
281 const auto& eventTime =
event.GetHeader().GetTime();
283 const auto yearIt =
fFactors.find(year);
288 const auto& yearStations = yearIt->second;
290 for (
auto& station : sEvent.StationsRange()) {
292 if (station.HasSimData() || station.IsUUB())
296 const auto stationIt = yearStations.find(station.GetId());
297 if (stationIt == yearStations.end())
299 const auto& pmtFactors = stationIt->second.fPMT;
301 for (
int i = 0; i < 3; ++i) {
302 const auto& factor = pmtFactors[i];
305 const int pmtId = i + 1;
306 if (!station.HasPMT(pmtId))
308 auto& pmt = station.GetPMT(pmtId);
309 if (!pmt.HasCalibData() || !pmt.GetCalibData().IsTubeOk())
311 auto& pmtRec = pmt.GetRecData();
312 pmtRec.SetGainRatio(factor * pmtRec.GetGainRatio());
fwk::VModule::ResultFlag Init() override
Initialize: invoked at beginning of run (NOT beginning of event)
bool fUseGainRatioCorrections
friend std::istream & operator>>(std::istream &is, GainRatioCorrectionLine &c)
const unsigned int maxNFit
IsProxy< T > Is(const T &x)
Report success to RunController.
const double maxSlopeError
bool is(const double a, const double b)
#define INFO(message)
Macro for logging informational messages.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
double GetStandardDeviation() const
bool HasAllCounts(const utl::SVector< n, int > &c)
Static (small and dense) vector class.
double abs(const SVector< n, T > &v)
YearStationFactors fFactors
const double maxOffsetError
static const short fgNPmts
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
double fCalibrationVersion12GainFactor
const unsigned int minNFit
ResultFlag
Flag returned by module methods to the RunController.
double GetAverage() const
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
Report failure to RunController, causing RunController to terminate execution.
Accumulates and calculates standard deviation.
#define ERROR(message)
Macro for logging error messages.
fwk::VModule::ResultFlag Run(evt::Event &event) override
Run: invoked once per event.
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)
bool IsCommentOrEmpty(const std::string &line)