LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
SIOVChannelStatusProvider.cxx
Go to the documentation of this file.
1 
9 #ifndef SIOVCHANNELSTATUSPROVIDER_CXX
10 #define SIOVCHANNELSTATUSPROVIDER_CXX 1
11 
12 // Our header
14 
15 // LArSoft libraries
18 #include "WebError.h"
19 #include "larevt/CalibrationDBI/IOVData/IOVDataConstants.h"
21 
22 // C/C++ standard libraries
23 #include <vector>
24 #include <algorithm> // std::copy()
25 #include <iterator> // std::inserter()
26 #include <utility> // std::pair<>
27 #include <fstream>
28 
29 
30 namespace lariov {
31 
32 
33  //----------------------------------------------------------------------------
35  : DatabaseRetrievalAlg(pset.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"))
36  , fEventTimeStamp(0)
37  , fCurrentTimeStamp(0)
38  , fDefault(0)
39  {
40 
41  bool UseDB = pset.get<bool>("UseDB", false);
42  bool UseFile = pset.get<bool>("UseFile", false);
43  std::string fileName = pset.get<std::string>("FileName", "");
44 
45  //priority: (1) use db, (2) use table, (3) use defaults
46  //If none are specified, use defaults
47  if ( UseDB ) fDataSource = DataSource::Database;
48  else if (UseFile) fDataSource = DataSource::File;
49  else fDataSource = DataSource::Default;
50 
51  if (fDataSource == DataSource::Default) {
52  std::cout << "Using default channel status value: "<<kGOOD<<"\n";
53  fDefault.SetStatus(kGOOD);
54  }
55  else if (fDataSource == DataSource::File) {
56  cet::search_path sp("FW_SEARCH_PATH");
57  std::string abs_fp = sp.find_file(fileName);
58  std::cout << "Using channel statuses from local file: "<<abs_fp<<"\n";
59  std::ifstream file(abs_fp);
60  if (!file) {
61  throw cet::exception("SIOVChannelStatusProvider")
62  << "File "<<abs_fp<<" is not found.";
63  }
64 
65  std::string line;
66  ChannelStatus cs(0);
67  while (std::getline(file, line)) {
68  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, line.find(',')));
69  int status = std::stoi(line.substr(line.find(',')+1));
70 
71  cs.SetChannel(ch);
72  cs.SetStatus( ChannelStatus::GetStatusFromInt(status) );
73  fData.AddOrReplaceRow(cs);
74  }
75  } // if source from file
76  else {
77  std::cout << "Using channel statuses from conditions database\n";
78  }
79  }
80 
81  // This method saves the time stamp of the latest event.
82 
84  mf::LogInfo("SIOVChannelStatusProvider") << "SIOVChannelStatusProvider::UpdateTimeStamp called.";
85  fNewNoisy.Clear();
86  fEventTimeStamp = ts;
87  }
88 
89  // Maybe update method cached data (public non-const version).
90 
91  bool SIOVChannelStatusProvider::Update(DBTimeStamp_t ts) {
92 
93  fEventTimeStamp = ts;
94  fNewNoisy.Clear();
95  return DBUpdate(ts);
96  }
97 
98  // Maybe update method cached data (private const version using current event time).
99 
101  return DBUpdate(fEventTimeStamp);
102  }
103 
104  // Maybe update method cached data (private const version).
105  // This is the function that does the actual work of updating data from database.
106 
107  bool SIOVChannelStatusProvider::DBUpdate(DBTimeStamp_t ts) const {
108 
109  bool result = false;
110  if(fDataSource == DataSource::Database && ts != fCurrentTimeStamp) {
111 
112  mf::LogInfo("SIOVChannelStatusProvider") << "SIOVChannelStatusProvider::DBUpdate called with new timestamp.";
113 
114  fCurrentTimeStamp = ts;
115 
116  // Call non-const base class method.
117 
118  result = const_cast<SIOVChannelStatusProvider*>(this)->UpdateFolder(ts);
119  if(result) {
120  //DBFolder was updated, so now update the Snapshot
121  fData.Clear();
122  fData.SetIoV(this->Begin(), this->End());
123 
124  std::vector<DBChannelID_t> channels;
125  fFolder->GetChannelList(channels);
126  for (auto it = channels.begin(); it != channels.end(); ++it) {
127 
128  long status;
129  fFolder->GetNamedChannelData(*it, "status", status);
130 
131  ChannelStatus cs(*it);
132  cs.SetStatus( ChannelStatus::GetStatusFromInt((int)status) );
133 
134  fData.AddOrReplaceRow(cs);
135  }
136  }
137  }
138  return result;
139  }
140 
141 
142  //----------------------------------------------------------------------------
144  if (fDataSource == DataSource::Default) {
145  return fDefault;
146  }
147  DBUpdate();
148  if (fNewNoisy.HasChannel(rawToDBChannel(ch))) {
149  return fNewNoisy.GetRow(rawToDBChannel(ch));
150  }
151  else {
152  return fData.GetRow(rawToDBChannel(ch));
153  }
154  }
155 
156 
157  //----------------------------------------------------------------------------
158  SIOVChannelStatusProvider::ChannelSet_t
160 
161  ChannelSet_t retSet;
162  retSet.clear();
163  DBChannelID_t maxChannel = art::ServiceHandle<geo::Geometry>()->Nchannels() - 1;
164  if (fDataSource == DataSource::Default) {
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  //----------------------------------------------------------------------------
186  SIOVChannelStatusProvider::ChannelSet_t
188  return GetChannelsWithStatus(kGOOD);
189  }
190 
191 
192  //----------------------------------------------------------------------------
193  SIOVChannelStatusProvider::ChannelSet_t
195  ChannelSet_t dead = GetChannelsWithStatus(kDEAD);
196  ChannelSet_t ln = GetChannelsWithStatus(kLOWNOISE);
197  dead.insert(ln.begin(),ln.end());
198  return dead;
199  }
200 
201 
202  //----------------------------------------------------------------------------
203  SIOVChannelStatusProvider::ChannelSet_t
205  return GetChannelsWithStatus(kNOISY);
206  }
207 
208 
209  //----------------------------------------------------------------------------
211 
212  // for c2: ISO C++17 does not allow 'register' storage class specifier
213  //register DBChannelID_t const dbch = rawToDBChannel(ch);
214  DBChannelID_t const dbch = rawToDBChannel(ch);
215  if (!this->IsBad(dbch) && this->IsPresent(dbch)) {
216  ChannelStatus cs(dbch);
217  cs.SetStatus(kNOISY);
218  fNewNoisy.AddOrReplaceRow(cs);
219  }
220  }
221 
222 
223 
224  //----------------------------------------------------------------------------
225 
226 } // namespace lariov
227 
228 #endif
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.
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.
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:231
const IOVTimeStamp & End() const
Filters for channels, events, etc.
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:27
ChannelSet_t NoisyChannels() const override
Returns a copy of set of noisy channel IDs for the current run.
Collection of exception classes for WebDBI.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33