LSSTApplications  20.0.0
LSSTDataManagementBasePackage
DetectorCollection.cc
Go to the documentation of this file.
1 /*
2  * Developed for the LSST Data Management System.
3  * This product includes software developed by the LSST Project
4  * (https://www.lsst.org).
5  * See the COPYRIGHT file at the top-level directory of this distribution
6  * for details of code ownership.
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 GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
27 
28 namespace lsst {
29 namespace afw {
30 namespace cameraGeom {
31 
32 template <typename T>
34 
35 template <typename T>
36 std::shared_ptr<T> DetectorCollectionBase<T>::operator[](std::string const & name) const {
37  auto det = get(name);
38  if (det == nullptr) {
40  (boost::format("Detector with name %s not found") % name).str());
41  }
42  return det;
43 }
44 
45 template <typename T>
47  auto det = get(id);
48  if (det == nullptr) {
50  (boost::format("Detector with ID %s not found") % id).str());
51  }
52  return det;
53 }
54 
55 template <typename T>
57  auto i = _nameDict.find(name);
58  if (i == _nameDict.end()) {
59  return def;
60  }
61  return i->second;
62 }
63 
64 template <typename T>
66  auto i = _idDict.find(id);
67  if (i == _idDict.end()) {
68  return def;
69  }
70  return i->second;
71 }
72 
73 template <typename T>
75  for (auto const & detector : detectorList) {
76  _nameDict[detector->getName()] = detector;
77  _idDict[detector->getId()] = detector;
78  }
79 
80  if (_idDict.size() < detectorList.size()) {
81  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Detector IDs are not unique");
82  }
83  if (_nameDict.size() < detectorList.size()) {
84  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Detector names are not unique");
85  }
86 }
87 
88 template <typename T>
90  auto idIter = _idDict.find(detector->getId());
91  auto nameIter = _nameDict.find(detector->getName());
92  if (idIter == _idDict.end()) {
93  if (nameIter == _nameDict.end()) {
94  try {
95  _idDict.emplace(detector->getId(), detector);
96  _nameDict.emplace(detector->getName(), detector);
97  } catch (...) {
98  _idDict.clear();
99  _nameDict.clear();
100  throw;
101  }
102  } else {
103  throw LSST_EXCEPT(
105  (boost::format("Detector name %s is not unique.") % detector->getName()).str()
106  );
107  }
108  } else {
109  if (nameIter == _nameDict.end()) {
110  throw LSST_EXCEPT(
112  (boost::format("Detector ID %s is not unique.") % detector->getId()).str()
113  );
114  } else {
115  if (nameIter->second != detector) {
116  assert(idIter->second != detector);
117  throw LSST_EXCEPT(
119  (boost::format("Detector name %s and ID %s are not unique.") % detector->getName()
120  % detector->getId()).str()
121  );
122  }
123  // detector is already present; do nothing
124  }
125  }
126 }
127 
128 template <typename T>
130  auto nameIter = _nameDict.find(name);
131  if (nameIter == _nameDict.end()) {
132  throw LSST_EXCEPT(
134  (boost::format("Detector with name %s not found.") % name).str()
135  );
136  }
137  auto idIter = _idDict.find(nameIter->second->getId());
138  assert(idIter != _idDict.end());
139  _nameDict.erase(nameIter);
140  _idDict.erase(idIter);
141 }
142 
143 template <typename T>
145  auto idIter = _idDict.find(id);
146  if (idIter == _idDict.end()) {
147  throw LSST_EXCEPT(
149  (boost::format("Detector with ID %s not found.") % id).str()
150  );
151  }
152  auto nameIter = _nameDict.find(idIter->second->getName());
153  assert(nameIter != _nameDict.end());
154  _nameDict.erase(nameIter);
155  _idDict.erase(idIter);
156 }
157 
160 
161 namespace {
162 
163 class PersistenceHelper {
164 public:
165 
166  static PersistenceHelper const & get() {
167  static PersistenceHelper const instance;
168  return instance;
169  }
170 
171  table::Schema schema;
172  table::Key<int> detector;
173 
174  DetectorCollection::List makeDetectorList(
175  table::io::InputArchive const & archive,
176  table::io::CatalogVector const & catalogs
177  ) const {
178  LSST_ARCHIVE_ASSERT(catalogs.size() >= 1u);
179  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == schema);
181  result.reserve(catalogs.front().size());
182  for (auto const & record : catalogs.front()) {
183  int archiveId = record.get(detector);
184  result.push_back(archive.get<Detector>(archiveId));
185  }
186  return result;
187  }
188 
189 private:
190 
191  PersistenceHelper() :
192  schema(),
193  detector(schema.addField<int>("detector", "archive ID of Detector in a DetectorCollection"))
194  {}
195 
196  PersistenceHelper(PersistenceHelper const &) = delete;
197  PersistenceHelper(PersistenceHelper &&) = delete;
198 
199  PersistenceHelper & operator=(PersistenceHelper const &) = delete;
200  PersistenceHelper & operator=(PersistenceHelper &&) = delete;
201 
202 };
203 
204 } // anonymous
205 
206 
207 DetectorCollection::DetectorCollection(List const & detectorList) :
208  DetectorCollectionBase<Detector const>(detectorList)
209 {
210  for (auto const & detector : detectorList) {
211  for (auto const & corner : detector->getCorners(FOCAL_PLANE)) {
212  _fpBBox.include(corner);
213  }
214  }
215 }
216 
218 public:
219 
220  Factory() : table::io::PersistableFactory("DetectorCollection") {}
221 
223  CatalogVector const& catalogs) const override {
224  // can't use make_shared because ctor is protected
225  return std::shared_ptr<DetectorCollection>(new DetectorCollection(archive, catalogs));
226  }
227 
228  static Factory const registration;
229 };
230 
232 
233 
235  table::io::InputArchive const & archive,
236  table::io::CatalogVector const & catalogs
237 ) : DetectorCollection(PersistenceHelper::get().makeDetectorList(archive, catalogs))
238 {}
239 
240 DetectorCollection::~DetectorCollection() noexcept = default;
241 
242 std::string DetectorCollection::getPersistenceName() const {
243  return "DetectorCollection";
244 }
245 
247  return "lsst.afw.cameraGeom";
248 }
249 
251  auto const & keys = PersistenceHelper::get();
252  auto cat = handle.makeCatalog(keys.schema);
253  for (auto const & pair : getIdMap()) {
254  auto record = cat.addNew();
255  record->set(keys.detector, handle.put(pair.second));
256  }
257  handle.saveCatalog(cat);
258 }
259 
260 } // namespace cameraGeom
261 
262 namespace table {
263 namespace io {
264 
265 template class PersistableFacade<cameraGeom::DetectorCollection>;
266 
267 } // namespace io
268 } // namespace table
269 
270 } // namespace afw
271 } // namespace lsst
lsst::afw::cameraGeom::DetectorCollection::Factory::read
std::shared_ptr< Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const override
Construct a new object from the given InputArchive and vector of catalogs.
Definition: DetectorCollection.cc:222
lsst::afw::cameraGeom::DetectorCollection::getPythonModule
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: DetectorCollection.cc:246
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::cameraGeom::DetectorCollection::write
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: DetectorCollection.cc:250
lsst::afw::cameraGeom::DetectorCollection::Factory
Definition: DetectorCollection.cc:217
lsst::afw::cameraGeom::DetectorCollection::DetectorCollection
DetectorCollection(List const &list)
Definition: DetectorCollection.cc:207
lsst::afw::table::io::OutputArchiveHandle
An object passed to Persistable::write to allow it to persist itself.
Definition: OutputArchive.h:118
std::vector
STL class.
Persistable.cc
lsst::pex::exceptions::NotFoundError
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
lsst::afw::table::io::OutputArchiveHandle::saveCatalog
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Definition: OutputArchive.cc:211
pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::table::io::InputArchive
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
astshim.keyMap.keyMapContinued.keys
def keys(self)
Definition: keyMapContinued.py:6
CatalogVector.h
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::afw::cameraGeom::DetectorCollectionBase::remove
void remove(std::string const &name)
Definition: DetectorCollection.cc:129
DetectorCollection.h
lsst::afw::cameraGeom::DetectorCollectionBase< Detector const >::List
std::vector< std::shared_ptr< Detector const > > List
Definition: DetectorCollection.h:54
lsst::afw::table::io::OutputArchiveHandle::makeCatalog
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
Definition: OutputArchive.cc:207
LSST_ARCHIVE_ASSERT
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
lsst::afw::cameraGeom::DetectorCollectionBase::operator[]
std::shared_ptr< T > operator[](std::string const &name) const
Implement the [name] operator.
Definition: DetectorCollection.cc:36
lsst::afw::cameraGeom::DetectorCollectionBase::get
std::shared_ptr< T > get(std::string const &name, std::shared_ptr< T > def=nullptr) const
Retrieve a detector by name, or fall back to a default.
Definition: DetectorCollection.cc:56
lsst::afw::table::io::CatalogVector
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
lsst::afw::cameraGeom::DetectorCollectionBase::~DetectorCollectionBase
virtual ~DetectorCollectionBase() noexcept=0
lsst::afw::table::io::PersistableFactory
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:228
lsst::afw::cameraGeom::DetectorCollection::Factory::registration
static Factory const registration
Definition: DetectorCollection.cc:228
result
py::object result
Definition: _schema.cc:429
lsst::afw::cameraGeom::DetectorCollectionBase< Detector const >::getIdMap
IdMap const & getIdMap() const noexcept
Get a map keyed and ordered by ID.
Definition: DetectorCollection.h:62
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::cameraGeom::DetectorCollection
An immutable collection of Detectors that can be accessed by name or ID.
Definition: DetectorCollection.h:151
lsst::afw::detection
Definition: Footprint.h:50
lsst::afw::cameraGeom::DetectorCollectionBase::DetectorCollectionBase
DetectorCollectionBase() noexcept=default
lsst::afw::cameraGeom::FOCAL_PLANE
CameraSys const FOCAL_PLANE
Focal plane coordinates: Position on a 2-d planar approximation to the focal plane (x,...
Definition: CameraSys.cc:30
lsst::afw::image.slicing.Factory
Factory
Definition: slicing.py:252
schema
table::Schema schema
Definition: DetectorCollection.cc:171
std
STL namespace.
lsst::afw::cameraGeom::DetectorCollectionBase::add
void add(std::shared_ptr< T > detector)
Add a detector to the collection.
Definition: DetectorCollection.cc:89
detector
table::Key< int > detector
Definition: DetectorCollection.cc:172
lsst::afw::cameraGeom::DetectorCollection::Factory::Factory
Factory()
Definition: DetectorCollection.cc:220
lsst::geom::Box2D::include
void include(Point2D const &point) noexcept
Expand this to ensure that this->contains(point).
Definition: Box.cc:380
lsst::afw::cameraGeom::DetectorCollection::~DetectorCollection
virtual ~DetectorCollection() noexcept
InputArchive.h
lsst::afw::table::io::OutputArchiveHandle::put
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
Definition: OutputArchive.cc:216
lsst::afw::table::CatalogT::addNew
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:485
OutputArchive.h
lsst::afw::cameraGeom::Detector
A representation of a detector in a mosaic camera.
Definition: Detector.h:185
lsst::pex::exceptions::RuntimeError
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
lsst::afw::cameraGeom::DetectorCollectionBase
An abstract base class for collections of Detectors and specific subclasses thereof.
Definition: DetectorCollection.h:49