RdStationRejector.cc
Go to the documentation of this file.
1 #include "RdStationRejector.h"
2 
3 #include <fwk/CentralConfig.h>
4 
5 #include <det/Detector.h>
6 #include <rdet/RDetector.h>
7 #include <rdet/Channel.h>
8 
9 #include <utl/Branch.h>
10 #include <utl/ErrorLogger.h>
11 #include <utl/String.h>
12 
13 #include <evt/Event.h>
14 #include <revt/REvent.h>
15 
16 #include <vector>
17 
18 
19 using namespace std;
20 using namespace fwk;
21 using namespace det;
22 using namespace utl;
23 using namespace evt;
24 using namespace revt;
25 
26 
27 namespace RdStationRejector {
28 
29  void
30  RdStationRejector::ExcludeOrRejectStation(Station& station, unsigned long long int reason=1)
31  {
32  if (fExcludeStations) {
33  if (reason > 1)
35  else
37  } else {
38  station.SetRejectedReason(reason); // use bad period DB reason
39  }
40  }
41 
42 
45  {
46  const Branch topB = CentralConfig::GetInstance()->GetTopBranch("RdStationRejector");
47  topB.GetChild("InfoLevel").GetData(fInfoLevel);
48  topB.GetChild("useStationsInStationList").GetData(fUseStationsInStationList);
49  topB.GetChild("stationList").GetData(fStationList);
50  topB.GetChild("excludeStations").GetData(fExcludeStations);
51  topB.GetChild("excludeNlStations").GetData(fExcludeNlStations);
52  topB.GetChild("useBadPeriodDB").GetData(fUseBadPeriodDB);
53 
54  fBadPeriodReasons =
55  static_cast<unsigned long long int>((((1ULL << (eNumRejectionStatusBits - 1)) - 1) << 1) + 1);
56 
57  const auto rejectAllExceptOf =
58  topB.GetChild("rejectAllBadPeriodReasonsExceptOf").Get<vector<string>>();
59  for (const auto& elem : rejectAllExceptOf) {
60  fBadPeriodReasons = fBadPeriodReasons ^ gMapRejectionStatus[elem];
61  }
62 
63  ostringstream msg;
64 
65  if (fUseStationsInStationList) {
66  if (fStationList.empty()) {
67  ERROR("Only select stations for list but list is empty!");
68  return eFailure;
69  }
70  msg << "Use only the following stations:";
71  } else {
72  msg << "Exclude or reject the following stations:";
73  }
74  for (auto & id: fStationList)
75  msg << " " << id;
76  msg << ".";
77 
78  msg << "\nExclude stations (instead of rejecting them) : " << fExcludeStations
79  << "\nExclude all stations with NL electronis: " << fExcludeNlStations
80  << "\nUse BadPeriod DB: " << fUseBadPeriodDB
81  << "\nRegister additional BadStations:";
82 
83  fRejectMap.clear();
84  for (Branch b = topB.GetFirstChild(); b; b = b.GetNextSibling()) {
85  if (b.GetName() != "BadStation")
86  continue;
87 
88  const size_t stationId = VManager::FindComponent<size_t>("id", b.GetAttributes());
89  const string reason = VManager::FindComponent<string>("reason", b.GetAttributes());
90  vector<utl::TimeStamp> range;
91  b.GetData(range);
92  fRejectMap.insert(make_pair(range[0], make_pair(range[1], stationId)));
93  msg << "\n registered: id = " << stationId << " [" << range[0] << ", " << range[1] << ")";
94  }
95 
96  INFOFinal(msg);
97 
98  return fRejectMap.empty() ? eFailure : eSuccess;
99  }
100 
101 
103  RdStationRejector::Run(Event& event)
104  {
105  if (!event.HasREvent())
106  return eSuccess;
107 
108  REvent& rEvent = event.GetREvent();
109  Detector& detector = Detector::GetInstance();
110 
111  ostringstream info;
112 
113  if (detector.GetTime() != event.GetHeader().GetTime())
114  detector.Update(event.GetHeader().GetTime());
115 
116  const rdet::RDetector& rDetector = detector.GetRDetector();
117 
118  if (fExcludeNlStations) {
119  for (auto& station : rEvent.StationsRange()) {
120  // Get bit depth to distinguish between german (12) and dutch stations (14)
121  const auto& detChannel = rDetector.GetStation(station.GetId()).GetChannel(1);
122  const int bitDepth = detChannel.GetBitDepth();
123 
124  if (bitDepth != 12 && bitDepth != 14) {
125  info.str("");
126  info << "Station " << station.GetId() << " has invalid bit depth of " << bitDepth
127  << ". Can not decide if DE or NL electronics. Station is not rejected.";
128  WARNING(info);
129  continue;
130  }
131 
132  if (bitDepth == 14) {
133  info.str("");
134  info << "Station " << station.GetId() << " has NL electronics. Station is excluded.";
135  INFOIntermediate(info);
136 
137  station.SetExcludedReason(eManuallyExcluded);
138  continue;
139  }
140  }
141  }
142 
143  // reject station that are in bad period database
144  if (fUseBadPeriodDB) {
145  for (auto& station : rEvent.StationsRange()) {
146  unsigned long long int reason = rDetector.GetBadStationReason(station.GetId());
147  if (reason & fBadPeriodReasons) {
148  info.str("");
149  info << "Excluded station " << station.GetId()
150  << " as bad period because of reason " << (reason & fBadPeriodReasons);
151  INFOFinal(info);
152  ExcludeOrRejectStation(station, reason);
153  }
154  }
155  }
156 
157  // manually reject stations - bad station
158  const TimeStamp& time = detector.GetTime();
159  TimeRangeMap::IdCollection badIds = fRejectMap.GetBadStations(time);
160  vector<int> deselectedIds;
161 
162  for (auto& stationId : badIds) {
163  if (rEvent.HasStation(stationId)) {
164  deselectedIds.push_back(stationId);
165  ExcludeOrRejectStation(rEvent.GetStation(stationId));
166  }
167  }
168 
169  // manually reject stations
170  if (fStationList.size() > 0) {
171  if (fUseStationsInStationList) {
172  for (auto& station : rEvent.StationsRange()) {
173  const int stationId = station.GetId();
174 
175  // remove station if not found in list
176  auto it = find(fStationList.begin(), fStationList.end(), stationId);
177  if (it == fStationList.end()) {
178  deselectedIds.push_back(stationId);
179  ExcludeOrRejectStation(station);
180  }
181  }
182  } else {
183  for (auto& stationId : fStationList) {
184  if (rEvent.HasStation(stationId)) {
185  deselectedIds.push_back(stationId);
186  ExcludeOrRejectStation(rEvent.GetStation(stationId));
187  }
188  }
189  }
190  }
191 
192  if (!deselectedIds.empty()) {
193  ostringstream info;
194  info << "station" << String::Plural(deselectedIds) << ' '
195  << String::OfSortedIds(deselectedIds) << " excluded or rejected.";
196  INFO(info);
197  }
198 
199  return eSuccess;
200  }
201 
202 
204  RdStationRejector::Finish()
205  {
206  return eSuccess;
207  }
208 
209 }
Branch GetTopBranch() const
Definition: Branch.cc:63
void Update(const utl::TimeStamp &time, const bool invData=true, const bool invComp=true, const bool forceRadio=false)
Update detector: deletes currently constructed stations and sets new time.
Definition: Detector.cc:179
evt::Header & GetHeader()
Interface class to access to the Radio part of an event.
Definition: REvent.h:42
utl::TimeStamp GetTime() const
Get time pertaining to the detector description.
Definition: Detector.h:134
#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
unsigned long long int GetBadStationReason(const int stationId) const
Definition: RDetector.cc:217
bool HasREvent() const
Station & GetStation(const int stationId)
retrieve station by id throw utl::NonExistentComponentException if n.a.
Definition: REvent.h:190
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
const utl::TimeStamp & GetTime() const
Definition: Event/Header.h:33
Detector description interface for RDetector-related data.
Definition: RDetector.h:46
T Get() const
Definition: Branch.h:271
const char * Plural(const T n)
Definition: String.h:104
#define INFOIntermediate(y)
Definition: VModule.h:162
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
int GetId() const
Station ID.
#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
void SetRejectedReason(const unsigned long long int reason)
static std::map< std::string, RejectionStatus > gMapRejectionStatus
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
void SetExcludedReason(const ExcludedReason reason)
const rdet::RDetector & GetRDetector() const
Definition: Detector.cc:143
Branch GetFirstChild() const
Get first child of this Branch.
Definition: Branch.cc:98
bool HasStation(const int stationId) const
Check whether station exists.
Definition: REvent.cc:132
string OfSortedIds(vector< int > ids)
Definition: String.cc:65
#define INFOFinal(y)
Definition: VModule.h:161
#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

, generated on Tue Sep 26 2023.