LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
SIOVChannelStatusProvider.cxx
Go to the documentation of this file.
1 
9 // Our header
11 
12 // LArSoft libraries
14 #include "fhiclcpp/ParameterSet.h"
19 
20 // C/C++ standard libraries
21 #include <fstream>
22 
23 namespace lariov {
24 
25  //----------------------------------------------------------------------------
27  : DatabaseRetrievalAlg(pset.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"))
28  , fEventTimeStamp(0)
29  , fCurrentTimeStamp(0)
30  , fDefault(0)
31  {
32 
33  bool UseDB = pset.get<bool>("UseDB", false);
34  bool UseFile = pset.get<bool>("UseFile", false);
35  std::string fileName = pset.get<std::string>("FileName", "");
36 
37  //priority: (1) use db, (2) use table, (3) use defaults
38  //If none are specified, use defaults
39  if (UseDB)
41  else if (UseFile)
43  else
45 
47  std::cout << "Using default channel status value: " << kGOOD << "\n";
48  fDefault.SetStatus(kGOOD);
49  }
50  else if (fDataSource == DataSource::File) {
51  cet::search_path sp("FW_SEARCH_PATH");
52  std::string abs_fp = sp.find_file(fileName);
53  std::cout << "Using channel statuses from local file: " << abs_fp << "\n";
54  std::ifstream file(abs_fp);
55  if (!file) {
56  throw cet::exception("SIOVChannelStatusProvider") << "File " << abs_fp << " is not found.";
57  }
58 
59  std::string line;
60  ChannelStatus cs(0);
61  while (std::getline(file, line)) {
62  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, line.find(',')));
63  int status = std::stoi(line.substr(line.find(',') + 1));
64 
65  cs.SetChannel(ch);
67  fData.AddOrReplaceRow(cs);
68  }
69  } // if source from file
70  else {
71  std::cout << "Using channel statuses from conditions database\n";
72  }
73  }
74 
75  // This method saves the time stamp of the latest event.
76 
78  {
79  mf::LogInfo("SIOVChannelStatusProvider")
80  << "SIOVChannelStatusProvider::UpdateTimeStamp called.";
81  fNewNoisy.Clear();
82  fEventTimeStamp = ts;
83  }
84 
85  // Maybe update method cached data (public non-const version).
86 
87  bool SIOVChannelStatusProvider::Update(DBTimeStamp_t ts)
88  {
89 
90  fEventTimeStamp = ts;
91  fNewNoisy.Clear();
92  return DBUpdate(ts);
93  }
94 
95  // Maybe update method cached data (private const version using current event time).
96 
98  {
99  return DBUpdate(fEventTimeStamp);
100  }
101 
102  // Maybe update method cached data (private const version).
103  // This is the function that does the actual work of updating data from database.
104 
105  bool SIOVChannelStatusProvider::DBUpdate(DBTimeStamp_t ts) const
106  {
107 
108  // A static mutex that is shared across all invocations of the function.
109  static std::mutex mutex;
110  std::lock_guard<std::mutex> lock(mutex);
111 
112  bool result = false;
114 
115  mf::LogInfo("SIOVChannelStatusProvider")
116  << "SIOVChannelStatusProvider::DBUpdate called with new timestamp.";
117 
118  fCurrentTimeStamp = ts;
119 
120  // Call non-const base class method.
121 
122  result = const_cast<SIOVChannelStatusProvider*>(this)->UpdateFolder(ts);
123  if (result) {
124  //DBFolder was updated, so now update the Snapshot
125  fData.Clear();
126  fData.SetIoV(this->Begin(), this->End());
127 
128  std::vector<DBChannelID_t> channels;
129  fFolder->GetChannelList(channels);
130  for (auto it = channels.begin(); it != channels.end(); ++it) {
131 
132  long status;
133  fFolder->GetNamedChannelData(*it, "status", status);
134 
135  ChannelStatus cs(*it);
137 
138  fData.AddOrReplaceRow(cs);
139  }
140  }
141  }
142  return result;
143  }
144 
145  //----------------------------------------------------------------------------
147  {
148  if (fDataSource == DataSource::Default) { return fDefault; }
149  DBUpdate();
150  if (fNewNoisy.HasChannel(rawToDBChannel(ch))) { return fNewNoisy.GetRow(rawToDBChannel(ch)); }
151  else {
152  return fData.GetRow(rawToDBChannel(ch));
153  }
154  }
155 
156  //----------------------------------------------------------------------------
157  SIOVChannelStatusProvider::ChannelSet_t SIOVChannelStatusProvider::GetChannelsWithStatus(
158  chStatus status) const
159  {
160 
161  ChannelSet_t retSet;
162  retSet.clear();
163  DBChannelID_t maxChannel = art::ServiceHandle<geo::Geometry const>()->Nchannels() - 1;
165  if (fDefault.Status() == status) {
166  std::vector<DBChannelID_t> chs;
167  for (DBChannelID_t ch = 0; ch != maxChannel; ++ch) {
168  chs.push_back(ch);
169  }
170  retSet.insert(chs.begin(), chs.end());
171  }
172  }
173  else {
174  std::vector<DBChannelID_t> chs;
175  for (DBChannelID_t ch = 0; ch != maxChannel; ++ch) {
176  if (this->GetChannelStatus(ch).Status() == status) chs.push_back(ch);
177  }
178 
179  retSet.insert(chs.begin(), chs.end());
180  }
181  return retSet;
182  }
183 
184  //----------------------------------------------------------------------------
185  SIOVChannelStatusProvider::ChannelSet_t SIOVChannelStatusProvider::GoodChannels() const
186  {
188  }
189 
190  //----------------------------------------------------------------------------
191  SIOVChannelStatusProvider::ChannelSet_t SIOVChannelStatusProvider::BadChannels() const
192  {
193  ChannelSet_t dead = GetChannelsWithStatus(kDEAD);
194  ChannelSet_t ln = GetChannelsWithStatus(kLOWNOISE);
195  dead.insert(ln.begin(), ln.end());
196  return dead;
197  }
198 
199  //----------------------------------------------------------------------------
200  SIOVChannelStatusProvider::ChannelSet_t SIOVChannelStatusProvider::NoisyChannels() const
201  {
203  }
204 
205  //----------------------------------------------------------------------------
207  {
208 
209  // for c2: ISO C++17 does not allow 'register' storage class specifier
210  //register DBChannelID_t const dbch = rawToDBChannel(ch);
211  DBChannelID_t const dbch = rawToDBChannel(ch);
212  if (!this->IsBad(dbch) && this->IsPresent(dbch)) {
213  ChannelStatus cs(dbch);
214  cs.SetStatus(kNOISY);
215  fNewNoisy.AddOrReplaceRow(cs);
216  }
217  }
218 
219  //----------------------------------------------------------------------------
220 
221 } // namespace lariov
std::unique_ptr< DBFolder > fFolder
bool IsPresent(raw::ChannelID_t channel) const override
Returns whether the specified channel is bad in the current run.
static DBChannelID_t rawToDBChannel(raw::ChannelID_t channel)
Converts LArSoft channel ID in the one proper for the DB.
chStatus Status() const
Definition: ChannelStatus.h:47
ChannelSet_t GoodChannels() const override
Returns a copy of set of bad channel IDs for the current run.
ChannelSet_t BadChannels() const override
Returns a copy of set of bad channel IDs for the current run.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
Class providing information about the quality of channels.
static chStatus GetStatusFromInt(int status)
Definition: ChannelStatus.h:51
bool UpdateFolder(DBTimeStamp_t ts)
Return true if fFolder is successfully updated.
bool IsBad(raw::ChannelID_t channel) const override
Returns whether the specified channel is bad in the current run.
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:314
void SetChannel(unsigned int ch)
Definition: ChData.h:33
const IOVTimeStamp & End() const
Filters for channels, events, etc.
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
ChannelSet_t GetChannelsWithStatus(chStatus status) const
TFile * file
const ChannelStatus & GetChannelStatus(raw::ChannelID_t channel) const
Returns Channel Status.
Channel quality provider with information from configuration file.
const IOVTimeStamp & Begin() const
Get Timestamp information.
bool Update(DBTimeStamp_t)
Allows a service to add to the list of noisy channels.
bool DBUpdate() const
Do actual database updates.
SIOVChannelStatusProvider(fhicl::ParameterSet const &pset)
Constructor.
void AddNoisyChannel(raw::ChannelID_t ch)
Allows a service to add to the list of noisy channels.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
ChannelSet_t NoisyChannels() const override
Returns a copy of set of noisy channel IDs for the current run.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void SetStatus(chStatus status)
Definition: ChannelStatus.h:49