LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
lar::util::details::FindAllP< Source, Dest > Class Template Reference

Query object reading all the associations between two classes. More...

#include "FindAllP.h"

Public Types

using Source_t = Source
 
using Dest_t = Dest
 

Public Member Functions

 FindAllP ()=default
 Default constructor: empty query, read information with Read() More...
 
 FindAllP (art::Event &event)
 Constructor: reads all associations from the specified event. More...
 
 FindAllP (art::Event &event, art::InputTag assnTag)
 Constructor: reads one association from the specified event. More...
 
art::Ptr< Dest_t > const & operator[] (art::Ptr< Dest_t > const &src) const
 Returns the object associated to the specified one. More...
 
bool hasProduct (art::ProductID const &id) const
 Returns whether there are associations from objects in product id. More...
 
bool hasProduct (art::Ptr< Source_t > const &ptr) const
 Returns if there are associations from objects with product as ptr. More...
 
unsigned int Read (art::Event &event)
 Reads all the associations from the event. More...
 
unsigned int Read (art::Event &event, art::InputTag const &assnTag)
 Reads the specified association from the event. More...
 
unsigned int Add (art::Event &event, art::InputTag const &assnTag)
 Reads the specified association from the event. More...
 

Protected Types

using Assns_t = art::Assns< Source_t, Dest_t >
 
using Cache_t = UniqueAssociationCache< Source_t, Dest_t >
 Type of the cache. More...
 

Protected Member Functions

unsigned int Merge (art::Handle< Assns_t > &handle)
 Adds all associations in the specified handle; returns their number. More...
 

Protected Attributes

Cache_t cache
 set of associations, keyed by product ID and key More...
 

Detailed Description

template<typename Source, typename Dest>
class lar::util::details::FindAllP< Source, Dest >

Query object reading all the associations between two classes.


Template Parameters
Sourcetype of the object we use as query key (indexing object)
Desttype of the object we query for

When assigned an event, this object reads all the associations from Source type classes to Dest type classes in the event, and stores their information in a map to track a Dest object from its Source one. In fact, it assumes that only one Dest object is associated, event-wise, to each single Source object.

Definition at line 117 of file FindAllP.h.

Member Typedef Documentation

template<typename Source , typename Dest >
using lar::util::details::FindAllP< Source, Dest >::Assns_t = art::Assns<Source_t, Dest_t>
protected

Definition at line 185 of file FindAllP.h.

template<typename Source , typename Dest >
using lar::util::details::FindAllP< Source, Dest >::Cache_t = UniqueAssociationCache<Source_t, Dest_t>
protected

Type of the cache.

Definition at line 188 of file FindAllP.h.

template<typename Source , typename Dest >
using lar::util::details::FindAllP< Source, Dest >::Dest_t = Dest

Definition at line 120 of file FindAllP.h.

template<typename Source , typename Dest >
using lar::util::details::FindAllP< Source, Dest >::Source_t = Source

Definition at line 119 of file FindAllP.h.

Constructor & Destructor Documentation

template<typename Source , typename Dest >
lar::util::details::FindAllP< Source, Dest >::FindAllP ( )
default

Default constructor: empty query, read information with Read()

template<typename Source , typename Dest >
lar::util::details::FindAllP< Source, Dest >::FindAllP ( art::Event event)
inline

Constructor: reads all associations from the specified event.

Definition at line 126 of file FindAllP.h.

126 : FindAllP() { Read(event); }
FindAllP()=default
Default constructor: empty query, read information with Read()
unsigned int Read(art::Event &event)
Reads all the associations from the event.
Definition: FindAllP.h:256
template<typename Source , typename Dest >
lar::util::details::FindAllP< Source, Dest >::FindAllP ( art::Event event,
art::InputTag  assnTag 
)
inline

Constructor: reads one association from the specified event.

Definition at line 129 of file FindAllP.h.

129  : FindAllP()
130  { Read(event, assnTag); }
FindAllP()=default
Default constructor: empty query, read information with Read()
unsigned int Read(art::Event &event)
Reads all the associations from the event.
Definition: FindAllP.h:256

Member Function Documentation

template<typename Source , typename Dest >
unsigned int lar::util::details::FindAllP< Source, Dest >::Add ( art::Event event,
art::InputTag const &  assnTag 
)

Reads the specified association from the event.

Parameters
eventthe event to read the associations from
assnTagthe input tag for the association
Exceptions
art::Exceptionif multiple dest objects are found for one source object

The existing associations already in cache are not removed.

The input tag for the association is usually simply a string with the name of the module that produced the association, and often the same module has also produced the source objects as well.

Definition at line 291 of file FindAllP.h.

References art::DataViewImpl::getByLabel(), lar::util::details::FindAllP< Source, Dest >::Merge(), and art::errors::ProductNotFound.

Referenced by lar::util::details::FindAllP< Source, Dest >::Read().

292  {
293 
294  // read the association between source and destination class types
295  art::Handle<Assns_t> handle;
296  if (!event.getByLabel(assnTag, handle)) {
298  << "no association found with input tag '" << assnTag << "'";
299  }
300 
301  return Merge(handle);
302  } // FindAllP::Add(Event, InputTag)
unsigned int Merge(art::Handle< Assns_t > &handle)
Adds all associations in the specified handle; returns their number.
Definition: FindAllP.h:307
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
template<typename Source , typename Dest >
bool lar::util::details::FindAllP< Source, Dest >::hasProduct ( art::ProductID const &  id) const
inline

Returns whether there are associations from objects in product id.

Definition at line 244 of file FindAllP.h.

Referenced by lar::util::details::FindAllP< Source, Dest >::operator[]().

245  { return cache.AssnCache.count(id) > 0; }
Cache_t AssnCache
association cache, keyed by product ID and index
Definition: FindAllP.h:83
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:190
template<typename Source , typename Dest >
bool lar::util::details::FindAllP< Source, Dest >::hasProduct ( art::Ptr< Source_t > const &  ptr) const
inline

Returns if there are associations from objects with product as ptr.

Definition at line 250 of file FindAllP.h.

References art::Ptr< T >::id(), and lar::util::details::FindAllP< Source, Dest >::Read().

251  { return hasProduct(ptr.id()); }
bool hasProduct(art::ProductID const &id) const
Returns whether there are associations from objects in product id.
Definition: FindAllP.h:244
ProductID id() const
Definition: Ptr.h:349
template<typename Source , typename Dest >
unsigned int lar::util::details::FindAllP< Source, Dest >::Merge ( art::Handle< Assns_t > &  handle)
protected

Adds all associations in the specified handle; returns their number.

Definition at line 307 of file FindAllP.h.

References art::Ptr< T >::id(), art::errors::InvalidNumber, art::Ptr< T >::isNonnull(), art::Ptr< T >::isNull(), art::Ptr< T >::key(), LOG_DEBUG, LOG_ERROR, art::Handle< T >::provenance(), and lar::util::details::ResizeToPower2().

Referenced by lar::util::details::FindAllP< Source, Dest >::Add().

308  {
309  // product ID of the last source object; initialized invalid
310  art::ProductID LastProductID = art::Ptr<Source_t>().id();
311  typename Cache_t::InProductCache_t const* AssnsList = nullptr;
312 
313  unsigned int count = 0;
314 
315  LOG_DEBUG("FindAllP") << "Merge(): importing " << handle->size()
316  << " associations from " << handle.provenance();
317 
318  for (auto const& assn: *handle) {
319  // assn is a std::pair<art::Ptr<Source_t>, art::Ptr<Dest_t>>
320  art::Ptr<Source_t> const& src = assn.first;
321 
322  if (src.isNull()) {
323  LOG_ERROR("FindAllP") << "Empty pointer found in association "
324  << handle.provenance();
325  continue; // this should not happen
326  }
327 
328  art::Ptr<Dest_t> const& dest = assn.second;
329 
330  // if we have changed product (that should be fairly rare),
331  // update the running pointers
332  if (src.id() != LastProductID) {
333  LastProductID = src.id();
334  AssnsList = &(cache.AssnCache[LastProductID]);
335 
336  // if the list is empty, it means we have just created it!
337  if (AssnsList->empty()) {
338  // allocate enough space to accomodate all the associations,
339  // (provided that source IDs are sequencial);
340  // in fact typically all the associations in the same handle
341  // have the same product ID
342  ResizeToPower2(*AssnsList, handle->size());
343  }
344  } // if different product ID
345 
346  // make sure there is enough room in the vector
347  typename art::Ptr<Source_t>::key_type key = src.key();
348  if (key >= AssnsList->size()) ResizeToPower2(*AssnsList, key + 1);
349 
350  // store the association to dest
351  art::Ptr<Dest_t>& dest_cell = (*AssnsList)[key];
352  if (dest_cell.isNonnull() && (dest_cell != dest)) {
354  << "Object Ptr" << src
355  << " is associated with at least two objects: "
356  << dest << " and " << dest_cell;
357  }
358  dest_cell = dest;
359  ++count;
360  } // for all associations in a list
361 
362  LOG_DEBUG("FindAllP")
363  << "Merged " << count << " associations from " << handle.provenance();
364  return count;
365  } // FindAllP::Merge()
key_type key() const
Definition: Ptr.h:356
size_type size() const
Definition: Assns.h:440
bool isNonnull() const
Definition: Ptr.h:335
Cache_t AssnCache
association cache, keyed by product ID and index
Definition: FindAllP.h:83
#define LOG_ERROR(category)
Provenance const * provenance() const
Definition: Handle.h:204
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:190
std::vector< DestPtr_t > InProductCache_t
type for a cache of dest products for a given source product ID
Definition: FindAllP.h:74
ProductID id() const
Definition: Ptr.h:349
void ResizeToPower2(std::vector< T > &v, size_t min_size)
Resizes a vector to a size power of 2, with a minimum size.
Definition: FindAllP.h:393
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
#define LOG_DEBUG(id)
std::size_t key_type
Definition: Ptr.h:102
bool isNull() const
Definition: Ptr.h:328
Definition: fwd.h:25
template<typename Source , typename Dest >
auto lar::util::details::FindAllP< Source, Dest >::operator[] ( art::Ptr< Dest_t > const &  src) const

Returns the object associated to the specified one.

Parameters
srca art pointer to the source object
Returns
a pointer to the associated object, or a null pointer if none

Definition at line 230 of file FindAllP.h.

References lar::util::details::FindAllP< Source, Dest >::hasProduct().

231  {
232  // we expect a missing match to be exceptional
233  try {
234  return cache.AssnCache.at(src.id()).at(src.key());
235  }
236  catch (std::out_of_range) {
237  return {};
238  }
239  } // FindAllP<>::operator[]
key_type key() const
Definition: Ptr.h:356
Cache_t AssnCache
association cache, keyed by product ID and index
Definition: FindAllP.h:83
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:190
ProductID id() const
Definition: Ptr.h:349
template<typename Source , typename Dest >
unsigned int lar::util::details::FindAllP< Source, Dest >::Read ( art::Event event)

Reads all the associations from the event.

Exceptions
art::Exceptionif multiple dest objects are found for one source object

Definition at line 256 of file FindAllP.h.

References LOG_DEBUG.

Referenced by lar::util::details::FindAllP< Source, Dest >::hasProduct().

257  {
258 
259  // read all the associations between source and destination class types
260  std::vector<art::Handle<Assns_t>> assns_list;
261  event.getManyByType(assns_list);
262 
263  LOG_DEBUG("FindAllP") << "Read(): read " << assns_list.size()
264  << " association sets";
265 
266  unsigned int count = 0;
267  // parse all the associations, and translate them into a local cache
268  for (art::Handle<Assns_t> handle: assns_list)
269  count += Merge(handle);
270 
271  LOG_DEBUG("FindAllP") << "Read " << count << " associations for "
272  << cache.NProductIDs() << " product IDs";
273 
274  return count;
275  } // FindAllP::Read(Event)
unsigned int Merge(art::Handle< Assns_t > &handle)
Adds all associations in the specified handle; returns their number.
Definition: FindAllP.h:307
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:190
#define LOG_DEBUG(id)
template<typename Source , typename Dest >
unsigned int lar::util::details::FindAllP< Source, Dest >::Read ( art::Event event,
art::InputTag const &  assnTag 
)

Reads the specified association from the event.

Parameters
eventthe event to read the associations from
assnTagthe input tag for the association
Exceptions
art::Exceptionif multiple dest objects are found for one source object

The input tag for the association is usually simply a string with the name of the module that produced the association, and often the same module has also produced the source objects as well.

Definition at line 281 of file FindAllP.h.

References lar::util::details::FindAllP< Source, Dest >::Add().

282  {
283  cache.clear();
284  return Add(event, assnTag);
285  } // FindAllP::Read(Event, InputTag)
void clear()
Empties the cache.
Definition: FindAllP.h:98
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:190
unsigned int Add(art::Event &event, art::InputTag const &assnTag)
Reads the specified association from the event.
Definition: FindAllP.h:291

Member Data Documentation

template<typename Source , typename Dest >
Cache_t lar::util::details::FindAllP< Source, Dest >::cache
protected

set of associations, keyed by product ID and key

Definition at line 190 of file FindAllP.h.


The documentation for this class was generated from the following file: