Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
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 292 of file FindAllP.h.

293  {
294 
295  // read the association between source and destination class types
296  art::Handle<Assns_t> handle;
297  if (!event.getByLabel(assnTag, handle)) {
299  << "no association found with input tag '" << assnTag << "'";
300  }
301 
302  return Merge(handle);
303  } // 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:308
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
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.

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.

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 noexcept
Definition: Ptr.h:190
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 308 of file FindAllP.h.

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

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[]
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
key_type key() const noexcept
Definition: Ptr.h:216
ProductID id() const noexcept
Definition: Ptr.h:190
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.

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  auto assns_list = event.getMany<Assns_t>();
263 
264  MF_LOG_DEBUG("FindAllP") << "Read(): read " << assns_list.size()
265  << " association sets";
266 
267  unsigned int count = 0;
268  // parse all the associations, and translate them into a local cache
269  for (art::Handle<Assns_t> handle: assns_list)
270  count += Merge(handle);
271 
272  MF_LOG_DEBUG("FindAllP") << "Read " << count << " associations for "
273  << cache.NProductIDs() << " product IDs";
274 
275  return count;
276  } // FindAllP::Read(Event)
unsigned int Merge(art::Handle< Assns_t > &handle)
Adds all associations in the specified handle; returns their number.
Definition: FindAllP.h:308
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:190
art::Assns< Source_t, Dest_t > Assns_t
Definition: FindAllP.h:185
#define MF_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 282 of file FindAllP.h.

283  {
284  cache.clear();
285  return Add(event, assnTag);
286  } // 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:292

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: