LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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 
22 #include "lsst/afw/table/io/Persistable.cc"
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
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.
table::Schema schema
void include(Point2D const &point) noexcept
Expand this to ensure that this->contains(point).
Definition: Box.cc:343
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:228
py::object result
Definition: schema.cc:418
std::vector< std::shared_ptr< Detector > > List
T end(T... args)
table::Key< int > id
Definition: Detector.cc:166
STL class.
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
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 > detector
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)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
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.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
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: Position on a 2-d planar approximation to the focal plane (x...
Definition: CameraSys.cc:30
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
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