LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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 112 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 175 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 178 of file FindAllP.h.

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

Definition at line 115 of file FindAllP.h.

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

Definition at line 114 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 121 of file FindAllP.h.

References Read().

121 : 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:238
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 124 of file FindAllP.h.

References Read().

124 : FindAllP() { 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:238

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 267 of file FindAllP.h.

References art::ProductRetriever::getByLabel(), and art::errors::ProductNotFound.

268  {
269 
270  // read the association between source and destination class types
271  art::Handle<Assns_t> handle;
272  if (!event.getByLabel(assnTag, handle)) {
274  << "no association found with input tag '" << assnTag << "'";
275  }
276 
277  return Merge(handle);
278  } // 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:281
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
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 226 of file FindAllP.h.

227  {
228  return cache.AssnCache.count(id) > 0;
229  }
Cache_t AssnCache
association cache, keyed by product ID and index
Definition: FindAllP.h:78
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:180
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 232 of file FindAllP.h.

References art::Ptr< T >::id().

233  {
234  return hasProduct(ptr.id());
235  }
bool hasProduct(art::ProductID const &id) const
Returns whether there are associations from objects in product id.
Definition: FindAllP.h:226
ProductID id() const noexcept
Definition: Ptr.h:160
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 281 of file FindAllP.h.

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

282  {
283  // product ID of the last source object; initialized invalid
284  art::ProductID LastProductID = art::Ptr<Source_t>().id();
285  typename Cache_t::InProductCache_t const* AssnsList = nullptr;
286 
287  unsigned int count = 0;
288 
289  MF_LOG_DEBUG("FindAllP") << "Merge(): importing " << handle->size() << " associations from "
290  << handle.provenance();
291 
292  for (auto const& assn : *handle) {
293  // assn is a std::pair<art::Ptr<Source_t>, art::Ptr<Dest_t>>
294  art::Ptr<Source_t> const& src = assn.first;
295 
296  if (src.isNull()) {
297  MF_LOG_ERROR("FindAllP")
298  << "Empty pointer found in association " << handle.provenance();
299  continue; // this should not happen
300  }
301 
302  art::Ptr<Dest_t> const& dest = assn.second;
303 
304  // if we have changed product (that should be fairly rare),
305  // update the running pointers
306  if (src.id() != LastProductID) {
307  LastProductID = src.id();
308  AssnsList = &(cache.AssnCache[LastProductID]);
309 
310  // if the list is empty, it means we have just created it!
311  if (AssnsList->empty()) {
312  // allocate enough space to accomodate all the associations,
313  // (provided that source IDs are sequencial);
314  // in fact typically all the associations in the same handle
315  // have the same product ID
316  ResizeToPower2(*AssnsList, handle->size());
317  }
318  } // if different product ID
319 
320  // make sure there is enough room in the vector
321  typename art::Ptr<Source_t>::key_type key = src.key();
322  if (key >= AssnsList->size()) ResizeToPower2(*AssnsList, key + 1);
323 
324  // store the association to dest
325  art::Ptr<Dest_t>& dest_cell = (*AssnsList)[key];
326  if (dest_cell.isNonnull() && (dest_cell != dest)) {
328  << "Object Ptr" << src << " is associated with at least two objects: " << dest
329  << " and " << dest_cell;
330  }
331  dest_cell = dest;
332  ++count;
333  } // for all associations in a list
334 
335  MF_LOG_DEBUG("FindAllP") << "Merged " << count << " associations from "
336  << handle.provenance();
337  return count;
338  } // FindAllP::Merge()
#define MF_LOG_ERROR(category)
size_type size() const
Definition: Assns.h:500
std::size_t key_type
Definition: Ptr.h:78
Cache_t AssnCache
association cache, keyed by product ID and index
Definition: FindAllP.h:78
Provenance const * provenance() const
Definition: Handle.h:217
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:180
key_type key() const noexcept
Definition: Ptr.h:166
bool isNull() const noexcept
Definition: Ptr.h:211
std::vector< DestPtr_t > InProductCache_t
type for a cache of dest products for a given source product ID
Definition: FindAllP.h:70
ProductID id() const noexcept
Definition: Ptr.h:160
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:361
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool isNonnull() const noexcept
Definition: Ptr.h:204
#define MF_LOG_DEBUG(id)
Definition: fwd.h:26
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 213 of file FindAllP.h.

215  {
216  // we expect a missing match to be exceptional
217  try {
218  return cache.AssnCache.at(src.id()).at(src.key());
219  }
220  catch (std::out_of_range) {
221  return {};
222  }
223  } // FindAllP<>::operator[]
Cache_t AssnCache
association cache, keyed by product ID and index
Definition: FindAllP.h:78
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:180
key_type key() const noexcept
Definition: Ptr.h:166
ProductID id() const noexcept
Definition: Ptr.h:160
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 238 of file FindAllP.h.

References MF_LOG_DEBUG.

239  {
240 
241  // read all the associations between source and destination class types
242  //std::vector<art::Handle<Assns_t>> assns_list;
243  //event.getManyByType(assns_list);
244  auto assns_list = event.getMany<Assns_t>();
245 
246  MF_LOG_DEBUG("FindAllP") << "Read(): read " << assns_list.size() << " association sets";
247 
248  unsigned int count = 0;
249  // parse all the associations, and translate them into a local cache
250  for (art::Handle<Assns_t> handle : assns_list)
251  count += Merge(handle);
252 
253  MF_LOG_DEBUG("FindAllP") << "Read " << count << " associations for " << cache.NProductIDs()
254  << " product IDs";
255 
256  return count;
257  } // FindAllP::Read(Event)
unsigned int Merge(art::Handle< Assns_t > &handle)
Adds all associations in the specified handle; returns their number.
Definition: FindAllP.h:281
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:180
art::Assns< Source_t, Dest_t > Assns_t
Definition: FindAllP.h:175
#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 260 of file FindAllP.h.

261  {
262  cache.clear();
263  return Add(event, assnTag);
264  } // FindAllP::Read(Event, InputTag)
void clear()
Empties the cache.
Definition: FindAllP.h:94
Cache_t cache
set of associations, keyed by product ID and key
Definition: FindAllP.h:180
unsigned int Add(art::Event &event, art::InputTag const &assnTag)
Reads the specified association from the event.
Definition: FindAllP.h:267

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 180 of file FindAllP.h.


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