LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ChargedSpacePointCreator.cpp
Go to the documentation of this file.
1 
10 // class header
12 
13 // framework libraries
16 #include "cetlib_except/exception.h"
17 
18 // C/C++ standard libraries
19 #include <cassert>
20 #include <iterator> // std::back_inserter()
21 #include <utility> // std::move()
22 
23 //------------------------------------------------------------------------------
26  std::string const& instanceName /* = {} */)
27  : fEvent(event)
28  , fInstanceName(instanceName)
29  , fSpacePoints(std::make_unique<std::vector<recob::SpacePoint>>())
30  , fCharges(std::make_unique<std::vector<recob::PointCharge>>())
31 {}
32 
33 //------------------------------------------------------------------------------
36  std::string const& instanceName /* = {} */)
37 {
38  ChargedSpacePointCollectionCreator creator(event, instanceName);
39  creator.fSpacePointPtrMaker =
40  std::make_unique<art::PtrMaker<recob::SpacePoint>>(event, instanceName);
41  creator.fChargePtrMaker =
42  std::make_unique<art::PtrMaker<recob::PointCharge>>(event, instanceName);
43  return creator;
44 } // ChargedSpacePointCollectionCreator(ProducesCollector)
45 
46 //------------------------------------------------------------------------------
49 {
50  // if these assertion fail, add() is being called after put()
51  assert(fSpacePoints);
52  assert(fCharges);
53 
54  fSpacePoints->push_back(spacePoint);
55  fCharges->push_back(charge);
56 
57  assert(fSpacePoints->size() == fCharges->size());
58 
59 } // recob::ChargedSpacePointCollectionCreator::add(copy)
60 
61 //------------------------------------------------------------------------------
64 {
65  // if these assertion fail, add() is being called after put()
66  assert(fSpacePoints);
67  assert(fCharges);
68 
69  fSpacePoints->push_back(std::move(spacePoint));
70  fCharges->push_back(std::move(charge));
71 
72  assert(fSpacePoints->size() == fCharges->size());
73 
74 } // recob::ChargedSpacePointCollectionCreator::add()
75 
76 //------------------------------------------------------------------------------
77 void recob::ChargedSpacePointCollectionCreator::addAll(std::vector<recob::SpacePoint>&& spacePoints,
78  std::vector<recob::PointCharge>&& charges)
79 {
80  // if these assertion fail, addAll() is being called after put()
81  assert(fSpacePoints);
82  assert(fCharges);
83 
84  if (spacePoints.size() != charges.size()) {
85  throw cet::exception("ChargedSpacePointCollectionCreator")
86  << "Input collections of inconsistent size:"
87  << " " << spacePoints.size() << " (space points)"
88  << "and " << charges.size() << " (charges)"
89  << "\n";
90  }
91  if (empty()) {
92  *fSpacePoints = std::move(spacePoints);
93  *fCharges = std::move(charges);
94  }
95  else {
96  fSpacePoints->reserve(fSpacePoints->size() + spacePoints.size());
97  for (auto&& obj : spacePoints)
98  fSpacePoints->push_back(std::move(obj));
99  spacePoints.clear();
100 
101  fCharges->reserve(fCharges->size() + charges.size());
102  for (auto&& obj : charges)
103  fCharges->push_back(std::move(obj));
104  charges.clear();
105  }
106 
107  assert(fSpacePoints->size() == fCharges->size());
108 
109 } // recob::ChargedSpacePointCollectionCreator::addAll()
110 
111 //------------------------------------------------------------------------------
113  std::vector<recob::SpacePoint> const& spacePoints,
114  std::vector<recob::PointCharge> const& charges)
115 {
116  // if these assertion fail, addAll() is being called after put()
117  assert(fSpacePoints);
118  assert(fCharges);
119 
120  if (spacePoints.size() != charges.size()) {
121  throw cet::exception("ChargedSpacePointCollectionCreator")
122  << "Input collections of inconsistent size:"
123  << " " << spacePoints.size() << " (space points)"
124  << "and " << charges.size() << " (charges)"
125  << "\n";
126  }
127  fSpacePoints->reserve(fSpacePoints->size() + spacePoints.size());
128  std::copy(spacePoints.begin(), spacePoints.end(), std::back_inserter(*fSpacePoints));
129 
130  fCharges->reserve(fCharges->size() + charges.size());
131  std::copy(charges.begin(), charges.end(), std::back_inserter(*fCharges));
132 
133  assert(fSpacePoints->size() == fCharges->size());
134 
135 } // recob::ChargedSpacePointCollectionCreator::addAll()
136 
137 //------------------------------------------------------------------------------
140 {
141 
142  fEvent.put(std::move(fSpacePoints), fInstanceName);
143  fEvent.put(std::move(fCharges), fInstanceName);
144 
145  assert(spent());
146  assert(empty());
147 } // recob::ChargedSpacePointCollectionCreator::put()
148 
149 //------------------------------------------------------------------------------
151 {
152 
153  if (fSpacePoints) fSpacePoints->clear();
154  if (fCharges) fCharges->clear();
155 
156  assert(empty());
157 
158 } // recob::ChargedSpacePointCollectionCreator::clear()
159 
160 //------------------------------------------------------------------------------
162  std::size_t i) const
163 {
164  return fSpacePointPtrMaker ? (*fSpacePointPtrMaker)(i) : art::Ptr<recob::SpacePoint>{};
165 } // recob::ChargedSpacePointCollectionCreator::spacePointPtr()
166 
167 //------------------------------------------------------------------------------
169  std::size_t i) const
170 {
171  return fChargePtrMaker ? (*fChargePtrMaker)(i) : art::Ptr<recob::PointCharge>{};
172 } // recob::ChargedSpacePointCollectionCreator::chargePtr()
173 
174 //------------------------------------------------------------------------------
176  std::string const& instanceName)
177 {
178 
179  producesCollector.produces<std::vector<recob::SpacePoint>>(instanceName);
180  producesCollector.produces<std::vector<recob::PointCharge>>(instanceName);
181 
182 } // recob::ChargedSpacePointCollectionCreator::produces()
183 
184 //------------------------------------------------------------------------------
art::Ptr< recob::SpacePoint > spacePointPtr(std::size_t i) const
Returns an art pointer to the specified space point (no check done!).
Reconstruction base classes.
art::Event & fEvent
The event this object is bound to.
recob::PointCharge const & charge(std::size_t i) const
Returns the last inserted charge; undefined behaviour if empty().
std::unique_ptr< std::vector< recob::SpacePoint > > fSpacePoints
Space point data.
static void produces(art::ProducesCollector &producesCollector, std::string const &instanceName={})
Declares the data products being produced.
std::string fInstanceName
Instance name of all the data products.
std::unique_ptr< art::PtrMaker< recob::SpacePoint > > fSpacePointPtrMaker
Space point pointer maker.
STL namespace.
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
ChargedSpacePointCollectionCreator(art::Event &event, std::string const &instanceName={})
Constructor binding this object to a specific art event.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
bool spent() const
Returns whether put() has already been called.
bool empty() const
Returns whether there are currently no space points in the collection.
static ChargedSpacePointCollectionCreator forPtrs(art::Event &event, std::string const &instanceName={})
Static function binding a new object to a specific art event.
Creates a collection of space points with associated charge.
art::Ptr< recob::PointCharge > chargePtr(std::size_t i) const
Returns an art pointer to the specified charge (no check done!).
std::unique_ptr< std::vector< recob::PointCharge > > fCharges
Charge data.
void addAll(std::vector< recob::SpacePoint > &&spacePoints, std::vector< recob::PointCharge > &&charges)
Inserts all the space points and associated data from the vectors into the collection.
Helpers to create space points with associated charge.
recob::SpacePoint const & spacePoint(std::size_t i) const
Returns the specified space point; undefined behaviour if not there.
void put()
Puts all data products into the event, leaving the creator empty().
void clear()
Removes all data from the collection, making it empty().
void add(recob::SpacePoint const &spacePoint, recob::PointCharge const &charge)
Inserts the specified space point and associated data into the collection.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Event finding and building.
std::unique_ptr< art::PtrMaker< recob::PointCharge > > fChargePtrMaker
Charge pointer maker.
Charge reconstructed in the active volume.
Definition: PointCharge.h:30