lsst.afw  17.0.1-20-g89cff3a03+1
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 
33  for (auto const & detector : detectorList) {
34  _nameDict[detector->getName()] = detector;
35  _idDict[detector->getId()] = detector;
36  for (auto const & corner : detector->getCorners(FOCAL_PLANE)) {
37  _fpBBox.include(corner);
38  }
39  }
40 
41  if (_idDict.size() < detectorList.size()) {
42  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Detector IDs are not unique");
43  }
44  if (_nameDict.size() < detectorList.size()) {
45  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Detector names are not unique");
46  }
47 }
48 
49 DetectorCollection::~DetectorCollection() noexcept = default;
50 
52  auto det = get(name);
53  if (det == nullptr) {
54  throw LSST_EXCEPT(pex::exceptions::NotFoundError, "Detector name not found");
55  }
56  return det;
57 }
58 
60  auto det = get(id);
61  if (det == nullptr) {
62  throw LSST_EXCEPT(pex::exceptions::NotFoundError, "Detector id not found");
63  }
64  return det;
65 }
66 
68  std::shared_ptr<Detector> def) const {
69  auto i = _nameDict.find(name);
70  if (i == _nameDict.end()) {
71  return def;
72  }
73  return i->second;
74 }
75 
77  auto i = _idDict.find(id);
78  if (i == _idDict.end()) {
79  return def;
80  }
81  return i->second;
82 }
83 
84 namespace {
85 
86 class PersistenceHelper {
87 public:
88 
89  static PersistenceHelper const & get() {
90  static PersistenceHelper const instance;
91  return instance;
92  }
93 
94  table::Schema schema;
95  table::Key<int> detector;
96 
97  DetectorCollection::List makeDetectorList(
98  table::io::InputArchive const & archive,
99  table::io::CatalogVector const & catalogs
100  ) const {
101  LSST_ARCHIVE_ASSERT(catalogs.size() >= 1u);
102  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == schema);
104  result.reserve(catalogs.front().size());
105  for (auto const & record : catalogs.front()) {
106  int archiveId = record.get(detector);
107  result.push_back(archive.get<Detector>(archiveId));
108  }
109  return result;
110  }
111 
112 private:
113 
114  PersistenceHelper() :
115  schema(),
116  detector(schema.addField<int>("detector", "archive ID of Detector in a DetectorCollection"))
117  {
118  schema.getCitizen().markPersistent();
119  }
120 
121  PersistenceHelper(PersistenceHelper const &) = delete;
122  PersistenceHelper(PersistenceHelper &&) = delete;
123 
124  PersistenceHelper & operator=(PersistenceHelper const &) = delete;
125  PersistenceHelper & operator=(PersistenceHelper &&) = delete;
126 
127 };
128 
129 } // anonymous
130 
131 
133 public:
134 
135  Factory() : table::io::PersistableFactory("DetectorCollection") {}
136 
138  CatalogVector const& catalogs) const override {
139  // can't use make_shared because ctor is protected
140  return std::shared_ptr<DetectorCollection>(new DetectorCollection(archive, catalogs));
141  }
142 
143  static Factory const registration;
144 };
145 
147 
148 
150  table::io::InputArchive const & archive,
151  table::io::CatalogVector const & catalogs
152 ) : DetectorCollection(PersistenceHelper::get().makeDetectorList(archive, catalogs))
153 {}
154 
156  return "DetectorCollection";
157 }
158 
160  return "lsst.afw.cameraGeom";
161 }
162 
164  auto const & keys = PersistenceHelper::get();
165  auto cat = handle.makeCatalog(keys.schema);
166  for (auto const & pair : getIdMap()) {
167  auto record = cat.addNew();
168  record->set(keys.detector, handle.put(pair.second));
169  }
170  handle.saveCatalog(cat);
171 }
172 
173 } // namespace cameraGeom
174 
175 namespace table {
176 namespace io {
177 
178 template class PersistableFacade<cameraGeom::DetectorCollection>;
179 
180 } // namespace io
181 } // namespace table
182 
183 } // namespace afw
184 } // namespace lsst
def keys(self)
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...
An object passed to Persistable::write to allow it to persist itself.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
void include(Point2D const &point) noexcept
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:228
table::Key< int > detector
std::vector< std::shared_ptr< Detector > > List
T end(T... args)
STL class.
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
A base class for image defects.
std::shared_ptr< Detector > get(std::string const &name, std::shared_ptr< Detector > def=nullptr) const
Support the "in" operator.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
std::shared_ptr< Detector > operator[](std::string const &name) const
Implement the [name] operator.
table::Key< int > id
Definition: Detector.cc:166
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
An immutable collection of Detectors that can be accessed by name or ID.
T find(T... args)
T size(T... args)
table::Schema schema
#define LSST_EXCEPT(type,...)
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
STL class.
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.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
DetectorCollection & operator=(DetectorCollection const &)=delete
IdMap const & getIdMap() const noexcept
Get an unordered map over detector IDs.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
CameraSys const FOCAL_PLANE
Focal plane coordinates: Rectilinear x, y (and z when talking about the location of a detector) on th...
Definition: CameraSys.cc:30
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:472