All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
makeOneTo01dataFrom.h
Go to the documentation of this file.
1 /**
2  * @file lardata/RecoBaseProxy/ProxyBase/makeOneTo01dataFrom.h
3  * @brief Helper functions to create data structures associated to a proxy.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date July 27, 2017
6  * @see lardata/RecoBaseProxy/ProxyBase/withZeroOrOne.h
7  *
8  * This library is header-only.
9  */
10 
11 #ifndef LARDATA_RECOBASEPROXY_PROXYBASE_MAKEONETO01DATAFROM_H
12 #define LARDATA_RECOBASEPROXY_PROXYBASE_MAKEONETO01DATAFROM_H
13 
14 // LArSoft libraries
16 #include "larcorealg/CoreUtils/ContainerMeta.h" // util::collection_value_t
17 
18 // framework libraries
19 #include "canvas/Utilities/InputTag.h"
20 
21 // C/C++ standard libraries
22 #include <utility> // std::forward()
23 #include <cstdlib> // std::size_t
24 
25 
26 namespace proxy {
27 
28  // --- BEGIN LArSoftProxiesAssociatedData ------------------------------------
29  /// @addtogroup LArSoftProxiesAssociatedData
30  /// @{
31 
32  //----------------------------------------------------------------------------
33  //--- one-to-(zero/one) associations
34  //----------------------------------------------------------------------------
35  /**
36  * @brief Processes and returns an one-to-(zero/one) associated data object.
37  * @tparam Tag the tag labelling this associated data
38  * (if omitted: second type of the association: `right_t`)
39  * @tparam Assns type of association to be processed
40  * @param assns association object to be processed
41  * @param minSize minimum number of entries in the produced association data
42  * @return a new `OneTo01Data` filled with associations from `tag`
43  *
44  * The content of the association object must fulfill the requirements of
45  * @ref LArSoftProxyDefinitionOneToZeroOrOneSeqAssn "one-to-(zero or one) sequential association".
46  * The `Assns` type is expected to be a `art::Assns` instance. At least,
47  * the `Assns` type is required to have `left_t` and `right_t` definitions
48  * representing respectively the main data type and the associated one, and
49  * to respond to `begin()` and `end()` functions. The iterated object must
50  * also respond to `std::get<0>()` with a `art::Ptr<left_t>` and to
51  * `std::get<1>()` with a `art::Ptr<right_t>`.
52  *
53  * Elements in the main collection not associated with any object will present
54  * an invalid _art_ pointer (`isNull()` true). If there is information for
55  * less than `minSize` main objects, more records will be added to mark the
56  * missing objects as not associated to anything.
57  *
58  * Example:
59  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
60  * art::Assns<recob::Track, recob::Vertex> trackVertexAssns;
61  * // ...
62  * auto assData = makeOneTo01dataFrom(assns);
63  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64  * will have `assData` tagged as `recob::Vertex`.
65  */
66  template <typename Tag, typename Assns>
67  auto makeOneTo01dataFrom(Assns const& assns, std::size_t minSize = 0)
68  { return proxy::makeOneTo01data<Tag>(assns, minSize); }
69 
70  template <typename Assns>
71  auto makeOneTo01dataFrom(Assns const& assns, std::size_t minSize = 0)
72  { return proxy::makeOneTo01data(assns, minSize); }
73 
74  /**
75  * @brief Creates and returns an one-to-(zero/one) associated data object.
76  * @tparam Main type of main object to be associated
77  * @tparam Aux type of data to be associated to the main objects
78  * @tparam Metadata type of metadata in the association
79  * @tparam Tag the tag labelling this associated data (if omitted: `Aux`)
80  * @tparam Event type of event to read associations from
81  * @param event event to read associations from
82  * @param tag input tag of the association object
83  * @param minSize minimum number of entries in the produced association data
84  * @return a new `OneTo01Data` filled with associations from `tag`
85  * @see `makeOneTo01dataFrom(Assns, std::size_t)`
86  *
87  * The association being retrieved must fulfill the requirements of
88  * @ref LArSoftProxyDefinitionOneToZeroOrOneSeqAssn "one-to-(zero or one) sequential association".
89  *
90  * Two template types must be explicitly specified, e.g.
91  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
92  * auto assData = makeOneTo01dataFrom<recob::Track, recob::Vertex>(event, tag);
93  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94  */
95  template <
96  typename Main, typename Aux, typename Metadata, typename Tag, typename Event
97  >
99  (Event const& event, art::InputTag const& tag, std::size_t minSize = 0);
100 
101  template <typename Main, typename Aux, typename Metadata, typename Event>
103  (Event const& event, art::InputTag const& tag, std::size_t minSize = 0)
104  {
105  return makeOneTo01dataFrom<Main, Aux, Metadata, Aux, Event>
106  (event, tag, minSize);
107  }
108 
109  /**
110  * @brief Creates and returns an one-to-(zero/one) associated data object.
111  * @tparam Aux type of data to be associated to the main objects
112  * @tparam Tag the tag labelling this associated data (if omitted: `Aux`)
113  * @tparam Handle type of handle to the main collection object
114  * @tparam Event type of event to read associations from
115  * @param handle handle to the main collection object
116  * @param event event to read associations from
117  * @param tag input tag of the association object
118  * @return a new `OneTo01Data` wrapping the information in `assns`
119  * @see `makeAssociatedDataFrom(Event const&, art::InputTag, std::size_t)`
120  *
121  * This function operates like
122  * `makeOneTo01dataFrom(Event const&, art::InputTag, std::size_t)`, but it
123  * extracts the information about the type of main object and the minimum
124  * number of them from a handle.
125  * The handle object is expected to behave as a smart pointer to a
126  * collection of elements of the associated type.
127  *
128  * One template type must be explicitly specified, e.g.
129  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
130  * auto assData = makeOneTo01dataFrom<recob::Vertex>(handle, event, tag);
131  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132  */
133  template <
134  typename Aux, typename Metadata, typename Tag,
135  typename Handle, typename Event
136  >
138  (Handle&& handle, Event const& event, art::InputTag const& tag);
139 
140  template <typename Aux, typename Metadata, typename Handle, typename Event>
142  (Handle&& handle, Event const& event, art::InputTag const& tag)
143  {
144  return makeOneTo01dataFrom<Aux, Metadata, Aux, Handle, Event>
145  (std::forward<Handle>(handle), event, tag);
146  }
147 
148 
149  /**
150  * @brief Creates and returns an one-to-(zero/one) associated data object.
151  * @tparam Tag the tag labelling this associated data (if omitted: `Aux`)
152  * @tparam MainColl type of the main collection object
153  * @tparam Assns type of the association object
154  * @param mainColl the main collection object
155  * @param assns association data object
156  * @return a new `OneTo01Data` wrapping the information in `assns`
157  * @see `makeOneTo01dataFrom(Assns const&, std::size_t)`
158  *
159  * This function operates like
160  * `makeOneTo01dataFrom(Assns const&, std::size_t)`, where the size is
161  * extracted from the main data collection.
162  */
163  template <typename Tag, typename MainColl, typename Assns>
164  auto makeOneTo01dataFrom(MainColl const& mainColl, Assns const& assns)
165  { return proxy::makeOneTo01data<Tag>(assns, mainColl.size()); }
166 
167  template <typename MainColl, typename Assns>
168  auto makeOneTo01dataFrom(MainColl const& mainColl, Assns const& assns)
169  { return proxy::makeOneTo01data<typename Assns::right_t>(mainColl, assns); }
170 
171 
172  /// @}
173  // --- END LArSoftProxiesAssociatedData --------------------------------------
174 
175 
176 } // namespace proxy
177 
178 
179 //------------------------------------------------------------------------------
180 //--- template implementation
181 //------------------------------------------------------------------------------
182 namespace proxy {
183 
184  //----------------------------------------------------------------------------
185  //--- makeOneTo01dataFrom() implementations
186  //----------------------------------------------------------------------------
187  template <
188  typename Main, typename Aux, typename Metadata,
189  typename Tag,
190  typename Event
191  >
193  Event const& event, art::InputTag const& tag, std::size_t minSize /* = 0 */
194  )
195  {
196  using Main_t = Main;
197  using Aux_t = Aux;
198  using Metadata_t = Metadata;
199  using AssociatedData_t
201  using Assns_t = typename AssociatedData_t::assns_t;
202 
203  return makeOneTo01dataFrom<Tag>
204  (*(event.template getValidHandle<Assns_t>(tag)), minSize);
205 
206  } // makeOneTo01dataFrom(tag)
207 
208 
209  //----------------------------------------------------------------------------
210  template <
211  typename Aux, typename Metadata,
212  typename Tag,
213  typename Handle, typename Event
214  >
216  (Handle&& handle, Event const& event, art::InputTag const& tag)
217  {
218  // Handle::value_type is the main data product type (a collection)
220  using Aux_t = Aux;
221  using Metadata_t = Metadata;
222  return makeOneTo01dataFrom<Main_t, Aux_t, Metadata_t, Tag>
223  (event, tag, handle->size());
224  } // makeOneTo01dataFrom(handle)
225 
226 
227  //----------------------------------------------------------------------------
228 
229 } // namespace proxy
230 
231 
232 #endif // LARDATA_RECOBASEPROXY_PROXYBASE_MAKEONETO01DATAFROM_H
auto makeOneTo01dataFrom(Assns const &assns, std::size_t minSize=0)
Processes and returns an one-to-(zero/one) associated data object.
auto makeOneTo01data(Assns const &assns, std::size_t minSize=0)
Processes and returns an one-to-(zero/one) associated data object.
Definition: OneTo01Data.h:270
Auxiliary data from one-to-(zero-or-one) sequential association.
Object for one-to-zero/or/one associated data interface.
Definition: OneTo01Data.h:71
typename collection_value_type< Coll >::type collection_value_t
Type contained in the collection Coll.
Definition: ContainerMeta.h:65
C++ metaprogramming utilities for dealing with containers.