makeAssociatedDataFrom.h
Go to the documentation of this file.
1 /**
2  * @file lardata/RecoBaseProxy/ProxyBase/makeAssociatedDataFrom.h
3  * @brief Helper functions to create an `AssociatedData` object.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date July 27, 2017
6  * @see lardata/RecoBaseProxy/ProxyBase/withAssociated.h
7  *
8  * This library is header-only.
9  */
10 
11 #ifndef LARDATA_RECOBASEPROXY_PROXYBASE_MAKEASSOCIATEDDATAFROM_H
12 #define LARDATA_RECOBASEPROXY_PROXYBASE_MAKEASSOCIATEDDATAFROM_H
13 
14 // LArSoft libraries
16 #include "larcorealg/CoreUtils/ContainerMeta.h" // util::collection_value_t, ...
17 
18 // framework libraries
21 
22 // C/C++ standard
23 #include <utility> // std::forward()
24 #include <cstdlib> // std::size_t
25 
26 
27 namespace proxy {
28 
29 
30  // --- BEGIN Associated data infrastructure ----------------------------------
31  /// @addtogroup LArSoftProxiesAssociatedData
32  /// @{
33 
34  template <typename Tag, typename Assns>
35  auto makeAssociatedDataFrom(Assns const& assns, std::size_t minSize = 0)
36  { return proxy::makeAssociatedData<Tag>(assns, minSize); }
37 
38  template <typename Assns>
39  auto makeAssociatedDataFrom(Assns const& assns, std::size_t minSize = 0)
40  { return makeAssociatedDataFrom<typename Assns::right_t>(assns, minSize); }
41 
42  /**
43  * @brief Creates and returns an associated data object.
44  * @tparam Main type of main object to be associated
45  * @tparam Aux type of data to be associated to the main objects
46  * @tparam Metadata type of metadata in the association (if omitted: `void`)
47  * @tparam Tag the tag labelling this associated data (if omitted: `Aux`)
48  * @tparam Event type of event to read associations from
49  * @param event event to read associations from
50  * @param tag input tag of the association object
51  * @param minSize minimum number of entries in the produced association data
52  * @return a new `AssociatedData` filled with associations from `tag`
53  *
54  * The association being retrieved must fulfill the requirements of
55  * @ref LArSoftProxyDefinitionOneToManySeqAssn "one-to-many sequential association".
56  *
57  * Elements in the main collection not associated with any object will be
58  * recorded as such. If there is information for less than `minSize` main
59  * objects, more records will be added to mark the missing objects as not
60  * associated to anything.
61  *
62  * Two template types must be explicitly specified, e.g.
63  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
64  * auto assData = makeAssociatedDataFrom<recob::Track, recob::Hit>(event, tag);
65  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66  */
67  template <
68  typename Main, typename Aux, typename Metadata, typename Tag,
69  typename Event
70  >
72  (Event const& event, art::InputTag const& tag, std::size_t minSize = 0);
73 
74  template <typename Main, typename Aux, typename Metadata, typename Event>
76  (Event const& event, art::InputTag const& tag, std::size_t minSize = 0)
77  {
78  return makeAssociatedDataFrom<Main, Aux, Metadata, Aux, Event>
79  (event, tag, minSize);
80  }
81 
82  template <typename Main, typename Aux, typename Event>
84  (Event const& event, art::InputTag const& tag, std::size_t minSize = 0)
85  {
86  return makeAssociatedDataFrom<Main, Aux, void, Aux, Event>
87  (event, tag, minSize);
88  }
89 
90  /**
91  * @brief Creates and returns an associated data object.
92  * @tparam Aux type of data to be associated to the main objects
93  * @tparam Metadata type of metadata in the association (if omitted: `void`)
94  * @tparam Tag the tag labelling this associated data (if omitted: `Aux`)
95  * @tparam Handle type of handle to the main collection object
96  * @tparam Event type of event to read associations from
97  * @param handle handle to the main collection object
98  * @param event event to read associations from
99  * @param tag input tag of the association object
100  * @return a new `AssociatedData` filled with associations from `tag`
101  * @see `makeAssociatedDataFrom(Event const&, art::InputTag, std::size_t)`
102  *
103  * This function operates like
104  * `makeAssociatedDataFrom(Event const&, art::InputTag, std::size_t)`, but it
105  * extracts the information about the type of main object and the minimum
106  * number of them from a handle.
107  * The handle object is expected to behave as a smart pointer to a
108  * collection of elements of the associated type.
109  *
110  * One template type must be explicitly specified, e.g.
111  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
112  * auto assData = makeAssociatedDataFrom<recob::Hit>(handle, event, tag);
113  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114  */
115  template <
116  typename Aux, typename Metadata, typename Tag,
117  typename Handle, typename Event
118  >
120  (Handle&& handle, Event const& event, art::InputTag const& tag);
121 
122  template <typename Aux, typename Metadata, typename Handle, typename Event>
124  (Handle&& handle, Event const& event, art::InputTag const& tag)
125  {
126  return makeAssociatedDataFrom<Aux, Metadata, Aux, Handle, Event>
127  (std::forward<Handle>(handle), event, tag);
128  }
129 
130  template <typename Aux, typename Handle, typename Event>
132  (Handle&& handle, Event const& event, art::InputTag const& tag)
133  {
134  return makeAssociatedDataFrom<Aux, void, Aux, Handle, Event>
135  (std::forward<Handle>(handle), event, tag);
136  }
137 
138 
139  /**
140  * @brief Creates and returns an associated data object.
141  * @tparam Tag the tag labelling this associated data (if omitted: `Aux`)
142  * @tparam MainColl type of the main collection object
143  * @tparam Assns type of the association object
144  * @param mainColl the main collection object
145  * @param assns association data object
146  * @return a new `AssociatedData` wrapping the information in `assns`
147  * @see `makeAssociatedDataFrom(Assns const&, std::size_t)`
148  *
149  * This function operates like
150  * `makeAssociatedDataFrom(Assns const&, std::size_t)`, where the size is
151  * extracted from the main data collection.
152  */
153  template <typename Tag, typename MainColl, typename Assns>
154  auto makeAssociatedDataFrom(MainColl const& mainColl, Assns const& assns)
155  { return proxy::makeAssociatedData<Tag>(assns, mainColl.size()); }
156 
157  template <typename MainColl, typename Assns>
158  auto makeAssociatedDataFrom(MainColl const& mainColl, Assns const& assns)
159  { return makeAssociatedDataFrom<typename Assns::right_t>(mainColl, assns); }
160 
161 
162  //----------------------------------------------------------------------------
163 
164  /// @}
165  // --- END Associated data infrastructure ------------------------------------
166 
167 
168 } // namespace proxy
169 
170 
171 //------------------------------------------------------------------------------
172 //--- template implementation
173 //------------------------------------------------------------------------------
174 namespace proxy {
175 
176 
177  //----------------------------------------------------------------------------
178  //--- makeAssociatedDataFrom() implementations
179  //----------------------------------------------------------------------------
180  template <
181  typename Main, typename Aux, typename Metadata, typename Tag,
182  typename Event
183  >
185  Event const& event, art::InputTag const& tag, std::size_t minSize /* = 0 */
186  )
187  {
188  using Main_t = Main;
189  using Aux_t = Aux;
190  using Metadata_t = Metadata;
191  using AssociatedData_t
193  using Assns_t = typename AssociatedData_t::assns_t;
194 
195  return
196  makeAssociatedDataFrom<Tag>(*(event.template getValidHandle<Assns_t>(tag)));
197 
198  } // makeAssociatedDataFrom(tag)
199 
200 
201  //----------------------------------------------------------------------------
202  template <
203  typename Aux, typename Metadata, typename Tag,
204  typename Handle, typename Event
205  >
207  (Handle&& handle, Event const& event, art::InputTag const& tag)
208  {
209  // Handle::value_type is the main data product type (a collection)
211  using Aux_t = Aux;
212  using Metadata_t = Metadata;
213  return makeAssociatedDataFrom<Main_t, Aux_t, Metadata_t, Tag>
214  (event, tag, handle->size());
215  } // makeAssociatedDataFrom(handle)
216 
217 
218 } // namespace proxy
219 
220 
221 #endif // LARDATA_RECOBASEPROXY_PROXYBASE_MAKEASSOCIATEDDATAFROM_H
auto makeAssociatedDataFrom(Assns const &assns, std::size_t minSize=0)
Auxiliary data from one-to-many sequential association.
Definition: tag.cpp:4
Object to draft associated data interface.
Definition: types.h:32
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.
Event finding and building.