All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChargedSpacePointCreator.cpp
Go to the documentation of this file.
1 /**
2  * @file lardata/ArtDataHelper/ChargedSpacePointCreator.cpp
3  * @brief Helpers to create space points with associated charge.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date December 21, 2017
6  * @see lardata/ArtDataHelper/ChargedSpacePointCreator.h
7  *
8  */
9 
10 // class header
12 
13 // framework libraries
14 #include "art/Framework/Core/ProducesCollector.h"
15 #include "art/Framework/Principal/Event.h"
16 #include "cetlib_except/exception.h"
17 
18 // C/C++ standard libraries
19 #include <utility> // std::move()
20 #include <iterator> // std::back_inserter()
21 #include <cassert>
22 
23 
24 //------------------------------------------------------------------------------
26  (art::Event& event, 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 
34 //------------------------------------------------------------------------------
37 (art::Event& event,
38  std::string const& instanceName /* = {} */)
39 {
40  ChargedSpacePointCollectionCreator creator(event, instanceName);
41  creator.fSpacePointPtrMaker = std::make_unique<art::PtrMaker<recob::SpacePoint>>
42  (event, instanceName);
43  creator.fChargePtrMaker = std::make_unique<art::PtrMaker<recob::PointCharge>>
44  (event, instanceName);
45  return creator;
46 } // ChargedSpacePointCollectionCreator(ProducesCollector)
47 
48 
49 //------------------------------------------------------------------------------
51  (recob::SpacePoint const& spacePoint, recob::PointCharge const& charge)
52 {
53  // if these assertion fail, add() is being called after put()
54  assert(fSpacePoints);
55  assert(fCharges);
56 
57  fSpacePoints->push_back(spacePoint);
58  fCharges->push_back(charge);
59 
60  assert(fSpacePoints->size() == fCharges->size());
61 
62 } // recob::ChargedSpacePointCollectionCreator::add(copy)
63 
64 
65 //------------------------------------------------------------------------------
67  (recob::SpacePoint&& spacePoint, recob::PointCharge&& charge)
68 {
69  // if these assertion fail, add() is being called after put()
70  assert(fSpacePoints);
71  assert(fCharges);
72 
73  fSpacePoints->push_back(std::move(spacePoint));
74  fCharges->push_back(std::move(charge));
75 
76  assert(fSpacePoints->size() == fCharges->size());
77 
78 } // recob::ChargedSpacePointCollectionCreator::add()
79 
80 
81 //------------------------------------------------------------------------------
83  std::vector<recob::SpacePoint>&& spacePoints,
84  std::vector<recob::PointCharge>&& charges
85  )
86 {
87  // if these assertion fail, addAll() is being called after put()
88  assert(fSpacePoints);
89  assert(fCharges);
90 
91  if (spacePoints.size() != charges.size()) {
92  throw cet::exception("ChargedSpacePointCollectionCreator")
93  << "Input collections of inconsistent size:"
94  << " " << spacePoints.size() << " (space points)"
95  << "and " << charges.size() << " (charges)"
96  << "\n";
97  }
98  if (empty()) {
99  *fSpacePoints = std::move(spacePoints);
100  *fCharges = std::move(charges);
101  }
102  else {
103  fSpacePoints->reserve(fSpacePoints->size() + spacePoints.size());
104  for (auto&& obj: spacePoints) fSpacePoints->push_back(std::move(obj));
105  spacePoints.clear();
106 
107  fCharges->reserve(fCharges->size() + charges.size());
108  for (auto&& obj: charges) fCharges->push_back(std::move(obj));
109  charges.clear();
110  }
111 
112  assert(fSpacePoints->size() == fCharges->size());
113 
114 } // recob::ChargedSpacePointCollectionCreator::addAll()
115 
116 
117 //------------------------------------------------------------------------------
119  std::vector<recob::SpacePoint> const& spacePoints,
120  std::vector<recob::PointCharge> const& charges
121  )
122 {
123  // if these assertion fail, addAll() is being called after put()
124  assert(fSpacePoints);
125  assert(fCharges);
126 
127 
128  if (spacePoints.size() != charges.size()) {
129  throw cet::exception("ChargedSpacePointCollectionCreator")
130  << "Input collections of inconsistent size:"
131  << " " << spacePoints.size() << " (space points)"
132  << "and " << charges.size() << " (charges)"
133  << "\n";
134  }
135  fSpacePoints->reserve(fSpacePoints->size() + spacePoints.size());
136  std::copy
137  (spacePoints.begin(), spacePoints.end(), std::back_inserter(*fSpacePoints));
138 
139  fCharges->reserve(fCharges->size() + charges.size());
140  std::copy(charges.begin(), charges.end(), std::back_inserter(*fCharges));
141 
142  assert(fSpacePoints->size() == fCharges->size());
143 
144 } // recob::ChargedSpacePointCollectionCreator::addAll()
145 
146 
147 //------------------------------------------------------------------------------
148  /// Puts all data products into the event, leaving the creator `empty()`.
150 
151  fEvent.put(std::move(fSpacePoints), fInstanceName);
152  fEvent.put(std::move(fCharges), fInstanceName);
153 
154  assert(spent());
155  assert(empty());
156 } // recob::ChargedSpacePointCollectionCreator::put()
157 
158 
159 //------------------------------------------------------------------------------
161 
162  if (fSpacePoints) fSpacePoints->clear();
163  if (fCharges) fCharges->clear();
164 
165  assert(empty());
166 
167 } // recob::ChargedSpacePointCollectionCreator::clear()
168 
169 
170 //------------------------------------------------------------------------------
171 art::Ptr<recob::SpacePoint>
173  (std::size_t i) const
174 {
175  return fSpacePointPtrMaker
176  ? (*fSpacePointPtrMaker)(i): art::Ptr<recob::SpacePoint>{};
177 } // recob::ChargedSpacePointCollectionCreator::spacePointPtr()
178 
179 
180 //------------------------------------------------------------------------------
181 art::Ptr<recob::PointCharge>
183  (std::size_t i) const
184 {
185  return fChargePtrMaker? (*fChargePtrMaker)(i): art::Ptr<recob::PointCharge>{};
186 } // recob::ChargedSpacePointCollectionCreator::chargePtr()
187 
188 
189 //------------------------------------------------------------------------------
191  (art::ProducesCollector& producesCollector, std::string const& instanceName)
192 {
193 
194  producesCollector.produces<std::vector<recob::SpacePoint>>(instanceName);
195  producesCollector.produces<std::vector<recob::PointCharge>>(instanceName);
196 
197 } // recob::ChargedSpacePointCollectionCreator::produces()
198 
199 
200 //------------------------------------------------------------------------------
art::Ptr< recob::SpacePoint > spacePointPtr(std::size_t i) const
Returns an art pointer to the specified space point (no check done!).
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::unique_ptr< art::PtrMaker< recob::SpacePoint > > fSpacePointPtrMaker
Space point pointer maker.
ChargedSpacePointCollectionCreator(art::Event &event, std::string const &instanceName={})
Constructor binding this object to a specific art event.
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.
T copy(T const &v)
Helpers to create space points with associated charge.
void put()
Puts all data products into the event, leaving the creator empty().
void clear()
Removes all data from the collection, making it empty().
bool empty(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:555
void add(recob::SpacePoint const &spacePoint, recob::PointCharge const &charge)
Inserts the specified space point and associated data into the collection.
std::unique_ptr< art::PtrMaker< recob::PointCharge > > fChargePtrMaker
Charge pointer maker.
Charge reconstructed in the active volume.
Definition: PointCharge.h:31