AssociationUtil.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////
2 // \version $ $
3 //
4 // \ricken Utility object to perform functions of association
5 // immitated Brian R's AssociationUtil.h in offline
6 //
7 //
8 ////////////////////////////////////////////////////////////////////////////
9 
10 #ifndef ASSOCIATIONUTIL_H
11 #define ASSOCIATIONUTIL_H
12 
13 //ART Include
24 
25 namespace util {
26 
27  /// \name Create and query associations
28  /// see https://cdcvs.fnal.gov/redmine/projects/art/wiki/Inter-Product_References
29  /// for information about using art::Assns
30  //@{
31 
32  /// \brief Create a 1 to 1 association between a new product and one already in the event
33  ///
34  /// \param a The collection about to be added to the event
35  /// \param b The product already in the event
36  /// \param indx Which element of \a a to associate. By default the last one.
37  /// \param instance instance label for product, defaulted to be an empty string
38  template<class T, class U> static bool CreateAssn(art::EDProducer const& prod,
39  art::Event &evt,
40  std::vector<T> &a,
41  art::Ptr<U> b,
42  art::Assns<T,U> &assn,
43  size_t indx=UINT_MAX,
44  std::string const& instance=std::string());
45 
46  template<class T, class U> static bool CreateAssn(art::EDFilter const& filt,
47  art::Event &evt,
48  std::vector<T> &a,
49  art::Ptr<U> b,
50  art::Assns<T,U> &assn,
51  size_t indx=UINT_MAX,
52  std::string const& instance=std::string());
53 
54  /// \brief Create a 1 to 1 association between each of a series of new
55  /// products and one already in the event
56  ///
57  /// \param a The collection about to be added to the event
58  /// \param b The product already in the event
59  /// \param begin_indx Which element of \a a to associate first.
60  /// \param end_indx One more than the index of the last element to associate
61  /// \param instance instance label for product, defaulted to be an empty string
62  template<class T, class U> static bool CreateAssn(art::EDProducer const& prod,
63  art::Event &evt,
64  std::vector<T> &a,
65  art::Ptr<U> b,
66  art::Assns<T,U> &assn,
67  size_t begin_indx,
68  size_t end_indx,
69  std::string const& instance=std::string());
70 
71  /// \brief Create a 1 to 1 association between two products already in the event
72  ///
73  /// \param a A product already in the event
74  /// \param b Another product already in the event
75  template<class T, class U> static bool CreateAssn(art::EDProducer const& prod,
76  art::Event &evt,
77  art::Ptr<T> &a,
78  art::Ptr<U> b,
79  art::Assns<T,U> &assn);
80 
81  /// \brief Create a 1 to many association between a new product and a PtrVector already in the event
82  ///
83  /// \param a The collection about to be added to the event
84  /// \param b The products already in the event
85  /// \param indx Which element of \a a to associate. By default the last one.
86  /// \param instance instance label for product, defaulted to be an empty string
87  template<class T, class U> static bool CreateAssn(art::EDProducer const& prod,
88  art::Event &evt,
89  std::vector<T> &a,
91  art::Assns<T,U> &assn,
92  size_t indx=UINT_MAX,
93  std::string const& instance=std::string());
94 
95  /// \brief Create a 1 to many association between products already in the event
96  ///
97  /// \param a A product already in the event
98  /// \param b A vector of products already in the event (the many)
99  template<class T, class U> static bool CreateAssn(art::EDProducer const& prod,
100  art::Event &evt,
101  art::Ptr<T> &a,
102  std::vector< art::Ptr<U> > b,
103  art::Assns<T,U> &assn);
104 
105  /// \brief Create a 1 to many association between a new product and a vector of Ptrs already in the event
106  ///
107  /// \param a The collection about to be added to the event
108  /// \param b The products already in the event
109  /// \param indx Which element of \a a to associate. By default the last one.
110  /// \param instance instance label for product, defaulted to be an empty string
111  template<class T, class U> static bool CreateAssn(art::EDProducer const& prod,
112  art::Event &evt,
113  std::vector<T> &a,
114  std::vector< art::Ptr<U> > b,
115  art::Assns<T,U> &assn,
116  size_t indx=UINT_MAX,
117  std::string const& instance=std::string());
118 
119  /// \brief Create a 1 to many association between new products
120  ///
121  /// \param a A collection about to be added to the event
122  /// \param b Another collection about to be added to the event
123  /// \param startU The first element of \a b to associate
124  /// \param endU The last element of \a b to associate +1 (like STL begin() and end())
125  /// \param indx Which element of \a a to associate. By default the last one.
126  /// \param instancea instance label for product a, defaulted to be an empty string
127  /// \param instanceb instance label for product b, defaulted to be an empty string
128  template<class T, class U> static bool CreateAssn(art::EDProducer const& prod,
129  art::Event &evt,
130  std::vector<T> &a,
131  std::vector<U> &b,
132  art::Assns<T,U> &assn,
133  size_t startU,
134  size_t endU,
135  size_t indx=UINT_MAX,
136  std::string const& instancea=std::string(),
137  std::string const& instanceb=std::string());
138  //The same function for filter
139  template<class T, class U> static bool CreateAssn(art::EDFilter const& filt,
140  art::Event &evt,
141  std::vector<T> &a,
142  std::vector<U> &b,
143  art::Assns<T,U> &assn,
144  size_t startU,
145  size_t endU,
146  size_t indx=UINT_MAX,
147  std::string const& instancea=std::string(),
148  std::string const& instanceb=std::string());
149 
150 
151  /// \brief Return all objects of type U that are not associated to objects of
152  /// type T.
153  ///
154  /// Label is the module label that would have produced
155  /// the associations and likely the objects of type T
156  /// this method assumes there is a one to many relationship between T and U
157  /// for example if you want to get all rb::CellHits
158  /// that are not associated to rb::Clusters
159  /// std::vector<const rb::CellHit*> hits = FindUNotAssociatedToU<rb::Cluster>(art::Handle<rb::CellHit>, ...);
160  template<class T, class U> static std::vector<const U*> FindUNotAssociatedToT(art::Handle<U> b,
161  art::Event const& evt,
162  std::string const& label);
163 
164  /// \brief Return all objects of type U that are not associated to objects of
165  /// type T.
166  ///
167  /// Label is the module label that would have produced the associations and
168  /// likely the objects of type T this method assumes there is a one to many
169  /// relationship between T and U for example if you want to get all rb::CellHits
170  /// that are not associated to rb::Clusters std::vector<art::Ptr<rb::CellHit> > hits =
171  /// FindUNotAssociatedToTP<rb::Cluster>(art::Handle<rb::CellHit>, ...);
172  template<class T, class U> static std::vector< art::Ptr<U> > FindUNotAssociatedToTP(art::Handle<U> b,
173  art::Event const& evt,
174  std::string const& label);
175 
176  //@}
177 
178 }// end namespace
179 
180 //----------------------------------------------------------------------
181 template<class T, class U> inline bool util::CreateAssn(art::EDProducer const& prod,
182  art::Event &evt,
183  std::vector<T> &a,
184  art::Ptr<U> b,
185  art::Assns<T,U> &assn,
186  size_t indx,
187  std::string const& instance)
188 {
189  bool ret = true;
190 
191  if(indx == UINT_MAX) indx = a.size()-1;
192 
193  try{
194  art::ProductID aid = prod.getProductID< std::vector<T> >(instance);
195  art::Ptr<T> aptr(aid, indx, evt.productGetter(aid));
196  assn.addSingle(aptr, b);
197  }
198  catch(cet::exception &e){
199  mf::LogWarning("AssociationUtil") << "unable to create requested "
200  << "art:Assns, exception thrown: "
201  << e;
202  ret = false;
203  }
204 
205  return ret;
206 }
207 
208 //----------------------------------------------------------------------
209 template<class T, class U> inline bool util::CreateAssn(art::EDFilter const& filt,
210  art::Event &evt,
211  std::vector<T> &a,
212  art::Ptr<U> b,
213  art::Assns<T,U> &assn,
214  size_t indx,
215  std::string const& instance)
216 {
217  bool ret = true;
218 
219  if(indx == UINT_MAX) indx = a.size()-1;
220 
221  try{
222  art::ProductID aid = filt.getProductID< std::vector<T> >(instance);
223  art::Ptr<T> aptr(aid, indx, evt.productGetter(aid));
224  assn.addSingle(aptr, b);
225  }
226  catch(cet::exception &e){
227  mf::LogWarning("AssociationUtil") << "unable to create requested "
228  << "art:Assns, exception thrown: "
229  << e;
230  ret = false;
231  }
232 
233  return ret;
234 }
235 
236 //----------------------------------------------------------------------
237 template<class T, class U> inline bool util::CreateAssn(art::EDProducer const& prod,
238  art::Event &evt,
239  std::vector<T> &a,
240  art::Ptr<U> b,
241  art::Assns<T,U> &assn,
242  size_t begin_indx,
243  size_t end_indx,
244  std::string const& instance)
245 {
246  if(end_indx == UINT_MAX) end_indx = a.size();
247 
248  try{
249  art::ProductID aid = prod.getProductID< std::vector<T> >(instance);
250  auto getter = evt.productGetter(aid);
251  for(size_t i = begin_indx; i < end_indx; ++i){
252  art::Ptr<T> aptr(aid, i, getter);
253  assn.addSingle(aptr, b);
254  }
255  }
256  catch(cet::exception &e){
257  mf::LogWarning("AssociationUtil") << "unable to create requested "
258  << "art:Assns, exception thrown: "
259  << e;
260  return false;
261  }
262 
263  return true;
264 }
265 
266 
267 //----------------------------------------------------------------------
268 template<class T, class U> inline bool util::CreateAssn(art::EDProducer const& prod,
269  art::Event &evt,
270  art::Ptr<T> &a,
271  art::Ptr<U> b,
272  art::Assns<T,U> &assn)
273 {
274  bool ret = true;
275 
276  try{
277  assn.addSingle(a, b);
278  }
279  catch(cet::exception &e){
280  mf::LogWarning("AssociationUtil") << "unable to create requested "
281  << "art:Assns, exception thrown: "
282  << e;
283  ret = false;
284  }
285 
286  return ret;
287 }
288 
289 //----------------------------------------------------------------------
290 template<class T, class U> inline bool util::CreateAssn(art::EDProducer const& prod,
291  art::Event &evt,
292  std::vector<T> &a,
294  art::Assns<T,U> &assn,
295  size_t indx,
296  std::string const& instance)
297 {
298 
299  bool ret = true;
300 
301  if(indx == UINT_MAX) indx = a.size()-1;
302 
303  try{
304  art::ProductID aid = prod.getProductID< std::vector<T> >(instance);
305  art::Ptr<T> aptr(aid, indx, evt.productGetter(aid));
306  for(size_t i = 0; i < b.size(); ++i) assn.addSingle(aptr, b[i]);
307  }
308  catch(cet::exception &e){
309  mf::LogWarning("AssociationUtil") << "unable to create requested "
310  << "art:Assns, exception thrown: "
311  << e;
312  ret = false;
313  }
314 
315  return ret;
316 }
317 
318 //----------------------------------------------------------------------
319 template<class T, class U> inline bool util::CreateAssn(art::EDProducer const& prod,
320  art::Event &evt,
321  std::vector<T> &a,
322  std::vector<art::Ptr<U> > b,
323  art::Assns<T,U> &assn,
324  size_t indx,
325  std::string const& instance)
326 {
327  bool ret = true;
328 
329  if(indx == UINT_MAX) indx = a.size()-1;
330 
331  try{
332  art::ProductID aid = prod.getProductID< std::vector<T> >(instance);
333  art::Ptr<T> aptr(aid, indx, evt.productGetter(aid));
334 
335  for(size_t i = 0; i < b.size(); ++i) assn.addSingle(aptr, b[i]);
336  }
337  catch(cet::exception &e){
338  mf::LogWarning("AssociationUtil") << "unable to create requested "
339  << "art:Assns, exception thrown: "
340  << e;
341  ret = false;
342  }
343 
344  return ret;
345 }
346 
347 //----------------------------------------------------------------------
348 template<class T, class U> inline bool util::CreateAssn(art::EDProducer const& prod,
349  art::Event &evt,
350  art::Ptr<T> &a,
351  std::vector<art::Ptr<U> > b,
352  art::Assns<T,U> &assn)
353 {
354  bool ret = true;
355 
356  try{
357  for(size_t i = 0; i < b.size(); ++i) assn.addSingle(a, b[i]);
358  }
359  catch(cet::exception &e){
360  mf::LogWarning("AssociationUtil") << "unable to create requested "
361  << "art:Assns, exception thrown: "
362  << e;
363  ret = false;
364  }
365 
366  return ret;
367 }
368 
369 //----------------------------------------------------------------------
370 template<class T, class U> inline bool util::CreateAssn(art::EDProducer const& prod,
371  art::Event &evt,
372  std::vector<T> &a,
373  std::vector<U> &b,
374  art::Assns<T,U> &assn,
375  size_t startU,
376  size_t endU,
377  size_t indx,
378  std::string const& instancea,
379  std::string const& instanceb)
380 {
381 
382  bool ret = true;
383 
384  if(indx == UINT_MAX) indx = a.size()-1;
385 
386  try{
387  art::ProductID aid = prod.getProductID< std::vector<T> >(instancea);
388  art::Ptr<T> aptr(aid, indx, evt.productGetter(aid));
389  for(size_t i = startU; i < endU; ++i){
390  art::ProductID bid = prod.getProductID< std::vector<U> >(instanceb);
391  art::Ptr<U> bptr(bid, i, evt.productGetter(bid));
392  assn.addSingle(aptr, bptr);
393  }
394  }
395  catch(cet::exception &e){
396  mf::LogWarning("AssociationUtil") << "unable to create requested "
397  << "art:Assns, exception thrown: "
398  << e;
399  ret = false;
400  }
401 
402  return ret;
403 }
404 
405 
406 //For Filter usage:
407 template<class T, class U> inline bool util::CreateAssn(art::EDFilter const& filt,
408  art::Event &evt,
409  std::vector<T> &a,
410  std::vector<U> &b,
411  art::Assns<T,U> &assn,
412  size_t startU,
413  size_t endU,
414  size_t indx,
415  std::string const& instancea,
416  std::string const& instanceb)
417 {
418 
419  bool ret = true;
420 
421  if(indx == UINT_MAX) indx = a.size()-1;
422 
423  try{
424  art::ProductID aid = filt.getProductID< std::vector<T> >(instancea);
425  art::Ptr<T> aptr(aid, indx, evt.productGetter(aid));
426  for(size_t i = startU; i < endU; ++i){
427  art::ProductID bid = filt.getProductID< std::vector<U> >(instanceb);
428  art::Ptr<U> bptr(bid, i, evt.productGetter(bid));
429  assn.addSingle(aptr, bptr);
430  }
431  }
432  catch(cet::exception &e){
433  mf::LogWarning("AssociationUtil") << "unable to create requested "
434  << "art:Assns, exception thrown: "
435  << e;
436  ret = false;
437  }
438 
439  return ret;
440 }
441 
442 
443 
444 
445 //----------------------------------------------------------------------
446 template<class T, class U> inline std::vector<const U*> util::FindUNotAssociatedToT(art::Handle<U> b,
447  art::Event const& evt,
448  std::string const& label)
449 {
450 
451  // Do a FindOne for type T for each object of type U
452  // If the FindOne returns an invalid maybe ref, add the pointer
453  // of object type U to the return vector
454 
455  std::vector<const U*> notAssociated;
456 
457  art::FindOne<T> fa(b, evt, label);
458 
459  for(size_t u = 0; u < b->size(); ++u){
460  cet::maybe_ref<T const> t(fa.at(u));
461  if( !t.isValid() ){
462  art::Ptr<U> ptr(b, u);
463  notAssociated.push_back(ptr.get());
464  }
465  }
466 
467  return notAssociated;
468 }
469 
470 //----------------------------------------------------------------------
471 template<class T, class U> inline std::vector< art::Ptr<U> > util::FindUNotAssociatedToTP(art::Handle<U> b,
472  art::Event const& evt,
473  std::string const& label)
474 {
475 
476  // Do a FindOneP for type T for each object of type U
477  // If the FindOne returns an invalid maybe ref, add the pointer
478  // of object type U to the return vector
479 
480  std::vector< art::Ptr<U> > notAssociated;
481 
482  art::FindOneP<T> fa(b, evt, label);
483 
484  for(size_t u = 0; u < b->size(); ++u){
485  cet::maybe_ref<T const> t(fa.at(u));
486  if( !t.isValid() ){
487  art::Ptr<U> ptr(b, u);
488  notAssociated.push_back(ptr);
489  }
490  }
491 
492  return notAssociated;
493 }
494 
495 #endif //ASSOCIATIONUTIL_H
EDProductGetter const * productGetter(ProductID const) const
static std::vector< const U * > FindUNotAssociatedToT(art::Handle< U > b, art::Event const &evt, std::string const &label)
Return all objects of type U that are not associated to objects of type T.
static bool CreateAssn(art::EDProducer const &prod, art::Event &evt, std::vector< T > &a, art::Ptr< U > b, art::Assns< T, U > &assn, size_t indx=UINT_MAX, std::string const &instance=std::string())
Create a 1 to 1 association between a new product and one already in the event.
Filter events based on their run/event numbers.
ProductID getProductID(std::string const &instanceName={}) const
Definition: EDFilter.h:131
static std::vector< art::Ptr< U > > FindUNotAssociatedToTP(art::Handle< U > b, art::Event const &evt, std::string const &label)
Return all objects of type U that are not associated to objects of type T.
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
ProductID getProductID(std::string const &instanceName={}) const
Definition: EDProducer.h:123
const char * label
const double a
int evt
T prod(const std::vector< T > &v)
Definition: prod.hpp:17
size_type size() const
Definition: PtrVector.h:308
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T const * get() const
Definition: Ptr.h:321
void addSingle(Ptr< left_t > const &left, Ptr< right_t > const &right, data_t const &data)
Definition: Assns.h:489
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
const hit & b
Definition: hits.cxx:21
Float_t e
Definition: plot.C:35
TString filt[ntarg]
Definition: Style.C:28
Definition: fwd.h:28
TFile fa("Li7.root")