lsst.meas.base  14.0-9-g876143c+3
FlagHandler.h
Go to the documentation of this file.
1 /*// -*- lsst-c++ -*-
2  * LSST Data Management System
3  * Copyright 2008-2014 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #ifndef LSST_MEAS_BASE_FlagHandler_h_INCLUDED
24 #define LSST_MEAS_BASE_FlagHandler_h_INCLUDED
25 
26 #include <vector>
27 
28 #include "lsst/afw/table/Schema.h"
31 
32 namespace lsst { namespace meas { namespace base {
39 
40  static constexpr std::size_t number_undefined = SIZE_MAX;
41 
42  FlagDefinition() : name(), doc(), number() {}
43 
44  FlagDefinition(std::string const& name, std::string const& doc, std::size_t number=number_undefined)
45  : name(name), doc(doc), number(number) {}
46 
47  // equality of this type is based solely on the name attribute
48  bool operator==(FlagDefinition const & other) const {
49  return (other.name == name);
50  }
51  bool operator!=(FlagDefinition const & other) const {
52  return (other.name != name);
53  }
54 
58 };
59 
64 public:
69  };
70 
75  for (FlagDefinition const * iter = list.begin(); iter < list.end(); iter++) {
76  add(iter->name, iter->doc);
77  }
78  }
79 
80  static FlagDefinitionList const & getEmptyList() {
81  static FlagDefinitionList list;
82  return list;
83  }
88  return _vector[index];
89  }
94  for (std::size_t i = 0; i < size(); i++) {
95  if (_vector[i].name == name) return _vector[i];
96  }
97  throw FatalAlgorithmError("No Flag Definition for " + name);
98  }
103  return getDefinition(index);
104  }
108  bool hasDefinition(std::string const & name) const {
109  for (std::size_t i = 0; i < size(); i++) {
110  if (_vector[i].name == name) return true;
111  }
112  return false;
113  }
118  FlagDefinition addFailureFlag(std::string const & doc="General Failure Flag");
119 
125  FlagDefinition flagDef = FlagDefinition(name, doc, _vector.size());
126  _vector.push_back(flagDef);
127  return _vector.back();
128  }
133  std::size_t size() const { return _vector.size(); }
134 
135 private:
136  mutable std::vector<FlagDefinition> _vector;
137 };
138 
156 class FlagHandler {
157 public:
184  FlagHandler() : failureFlagNumber(FlagDefinition::number_undefined) {}
185 
189  static std::string const & getFailureFlagName() {
190  static std::string name = "flag";
191  return name;
192  }
209  static FlagHandler addFields(
211  std::string const & prefix,
212  FlagDefinitionList const & flagDefs,
214  );
228  FlagHandler(
229  afw::table::SubSchema const & s,
230  FlagDefinitionList const & flagDefs,
232  );
236  unsigned int getFlagNumber(std::string const & flagName) const {
237  for (unsigned int i=0; i < _vector.size(); i++) {
238  if (_vector[i].first == flagName && _vector[i].second.isValid()) {
239  return i;
240  }
241  }
242  throw FatalAlgorithmError("No FlagHandler entry for " + flagName);
243  }
248  if (i < _vector.size() && _vector[i].second.isValid()) {
249  return _vector[i].first;
250  }
251  throw FatalAlgorithmError("No legal FlagHandler entry number " + std::to_string(i));
252  }
256  bool getValue(afw::table::BaseRecord const & record, std::size_t i) const {
257  if (i < _vector.size() && _vector[i].second.isValid()) {
258  return record.get(_vector[i].second);
259  }
260  throw FatalAlgorithmError("No legal FlagHandler entry number " + std::to_string(i));
261  }
265  bool getValue(afw::table::BaseRecord const & record, std::string const & flagName) const {
266  for (std::size_t i = 0; i < _vector.size(); i++) {
267  if (_vector[i].first == flagName && _vector[i].second.isValid()) {
268  return record.get(_vector[i].second);
269  }
270  }
271  throw FatalAlgorithmError("No FlagHandler entry for " + flagName);
272  }
276  void setValue(afw::table::BaseRecord & record, std::size_t i, bool value) const {
277  if (i < _vector.size() && _vector[i].second.isValid()) {
278  record.set(_vector[i].second, value);
279  return;
280  }
281  throw FatalAlgorithmError("No legal FlagHandler entry number " + std::to_string(i));
282  }
286  void setValue(afw::table::BaseRecord & record, std::string const & flagName, bool value) const {
287  for (std::size_t i = 0; i < _vector.size(); i++) {
288  if (_vector[i].first == flagName && _vector[i].second.isValid()) {
289  record.set(_vector[i].second, value);
290  return;
291  }
292  }
293  throw FatalAlgorithmError("No FlagHandler entry for " + flagName);
294  }
301  return failureFlagNumber;
302  }
311  void handleFailure(afw::table::BaseRecord & record, MeasurementError const * error=nullptr) const;
312 
314 private:
315 
317  Vector _vector;
318 };
319 
320 }}} // lsst::meas::base
321 
322 #endif // !LSST_MEAS_BASE_FlagHandler_h_INCLUDED
int iter
std::size_t size() const
return the current size (number of defined elements) of the collection
Definition: FlagHandler.h:133
FlagDefinition(std::string const &name, std::string const &doc, std::size_t number=number_undefined)
Definition: FlagHandler.h:44
bool operator!=(FlagDefinition const &other) const
Definition: FlagHandler.h:51
FlagDefinitionList()
initialize a FlagDefinition list with no entries.
Definition: FlagHandler.h:68
afw::table::Schema schema
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
Definition: FlagHandler.h:38
T to_string(T... args)
std::string getFlagName(std::size_t i) const
Return the value of the flag name corresponding to the given flag index.
Definition: FlagHandler.h:247
unsigned int getFlagNumber(std::string const &flagName) const
Return the index of a flag with the given flag name.
Definition: FlagHandler.h:236
void setValue(afw::table::BaseRecord &record, std::size_t i, bool value) const
Set the flag field corresponding to the given flag index.
Definition: FlagHandler.h:276
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
Field< T >::Value get(Key< T > const &key) const
FlagDefinition operator[](std::size_t index) const
get a reference to the FlagDefinition with specified array index
Definition: FlagHandler.h:102
Exception to be thrown when a measurement algorithm experiences a fatal error.
Definition: exceptions.h:83
std::size_t getFailureFlagNumber() const
Get the index of the General Failure flag, if one is defined.
Definition: FlagHandler.h:300
static constexpr std::size_t number_undefined
Definition: FlagHandler.h:40
STL class.
Utility class for handling flag fields that indicate the failure modes of an algorithm.
Definition: FlagHandler.h:156
first
FlagHandler()
Each error should have a corresponding static FlagDefinition object.
Definition: FlagHandler.h:184
FlagDefinition getDefinition(std::string const &name) const
get a reference to the FlagDefinition with specified name.
Definition: FlagHandler.h:93
FlagDefinition getDefinition(std::size_t index) const
get a reference to the FlagDefinition with specified index.
Definition: FlagHandler.h:87
bool operator==(FlagDefinition const &other) const
Definition: FlagHandler.h:48
static FlagDefinitionList const & getEmptyList()
Definition: FlagHandler.h:80
bool hasDefinition(std::string const &name) const
See if there is a FlagDefinition with specified name.
Definition: FlagHandler.h:108
FlagDefinitionList(std::initializer_list< FlagDefinition > const &list)
initialize a FlagDefinition list from initializer_list.
Definition: FlagHandler.h:74
FlagDefinition add(std::string const &name, std::string const &doc)
Add a new FlagDefinition to this list.
Definition: FlagHandler.h:124
void setValue(afw::table::BaseRecord &record, std::string const &flagName, bool value) const
Set the flag field corresponding to the given flag name.
Definition: FlagHandler.h:286
second
T size(T... args)
STL class.
static std::string const & getFailureFlagName()
Define the universal name of the general failure flag.
Definition: FlagHandler.h:189
bool getValue(afw::table::BaseRecord const &record, std::string const &flagName) const
Return the value of the flag field with the given flag name.
Definition: FlagHandler.h:265
void set(Key< T > const &key, U const &value)
bool getValue(afw::table::BaseRecord const &record, std::size_t i) const
Return the value of the flag field corresponding to the given flag index.
Definition: FlagHandler.h:256
vector-type utility class to build a collection of FlagDefinitions
Definition: FlagHandler.h:63