10 #ifndef ICARUSCORE_UTILITIES_ARTHANDLETRACKERMANAGER_H
11 #define ICARUSCORE_UTILITIES_ARTHANDLETRACKERMANAGER_H
15 #include "art/Framework/Principal/Handle.h"
16 #include "art/Framework/Principal/Provenance.h"
17 #include "canvas/Utilities/InputTag.h"
18 #include "messagefacility/MessageLogger/MessageLogger.h"
158 template <
typename Event>
220 template <
typename T,
typename... Args>
234 template <
typename T,
typename... Args>
245 template <
typename Handle>
313 (
bool removeCache =
true, art::Event
const* event =
nullptr);
361 template <typename Handle>
366 template <typename Handle>
418 template <typename Event>
442 LocalArtHandleTrackerManager(Event
const& event,
bool removeCache =
true);
445 ~LocalArtHandleTrackerManager();
457 bool willRemoveCachedProducts()
const;
478 template <
typename T,
typename... Args>
492 template <
typename T,
typename... Args>
503 template <
typename Handle>
516 void setRemoveCachedProducts(
bool removeCache);
533 unsigned int removeCachedProducts();
536 void forgetAllHandles();
592 template <typename Event>
596 virtual ~ArtHandleTrackerInterface() =
default;
610 std::type_info
const*
productType()
const {
return doProductType(); }
613 art::InputTag
inputTag()
const {
return doInputTag(); }
616 bool hasSameDataProduct
619 return inputTag() == other.
inputTag()
626 virtual bool doRemoveCachedProduct() = 0;
629 virtual std::any doHandlePtr()
const = 0;
632 virtual std::string doProductClass()
const = 0;
635 virtual std::type_info
const* doProductType()
const = 0;
638 virtual art::InputTag doInputTag()
const = 0;
648 namespace util::details {
653 template <
typename Handle>
655 template <
typename Handle>
657 template <
typename Handle>
658 art::InputTag
inputTagOf(Handle
const& handle);
665 template <
typename Handle,
typename>
668 static art::Provenance
const*
provenance(Handle
const& handle)
669 {
return handle.provenance(); }
675 auto const* prov = provenance(handle);
676 return prov? prov->producedClassName():
"";
681 {
return &
typeid(
typename Handle::element_type); }
685 {
return productType(); }
689 static art::InputTag
inputTag(Handle
const& handle)
691 auto const* prov = provenance(handle);
692 return prov? prov->inputTag(): art::InputTag{};
699 template <
typename Handle>
703 template <
typename Handle>
707 template <
typename Handle>
723 template <
typename Event,
typename Handle>
737 mf::LogDebug{
"ArtHandleTracker" } <<
"Removing cache for handle<"
738 << fHandle.provenance()->producedClassName()
739 <<
">(" << fHandle.provenance()->inputTag().encode() <<
").";
740 return fEvent->removeCachedProduct(fHandle);
745 {
return { &handle() }; }
767 :
fEvent(&event), fHandle(
std::move(handle)) {}
770 Handle
const&
handle()
const {
return fHandle; }
773 art::Provenance
const*
provenance()
const {
return fHandle.provenance(); }
781 template <
typename Event>
784 : fConfig{ std::move(config) }
789 template <
typename Event>
792 :
fEvent{ &
event }, fConfig{ std::move(config) }
797 template <
typename Event>
803 throw art::Exception{ art::errors::LogicError }
804 <<
"ArtHandleTrackerManager attempted to change event to "
806 <<
" handles are still tracked.\n";
815 template <
typename Event>
816 template <
typename T,
typename... Args>
822 (
fEvent->template getHandle<T>(std::forward<Args>(
args)...));
827 template <
typename Event>
828 template <
typename T,
typename... Args>
834 (
fEvent->template getValidHandle<T>(std::forward<Args>(
args)...));
839 template <
typename Event>
840 template <
typename Handle>
842 (Handle&& handle) -> decltype(
auto)
846 using Handle_t = std::decay_t<Handle>;
854 auto const& registeredHandle = *ptr;
855 mf::LogDebug
msg { fConfig.logCategory };
859 <<
") was already registered";
860 if (
typeid(registeredHandle) !=
typeid(Handle_t)) {
861 msg <<
" as a different handle type (" <<
typeid(registeredHandle).
name()
862 <<
", now " <<
typeid(Handle_t).
name() <<
")";
867 mf::LogDebug{ fConfig.logCategory }
870 <<
") (handle type: " <<
typeid(Handle_t).
name() <<
")."
873 fTrackers.push_back(std::make_unique<Tracker_t>(*
fEvent, handle));
876 return std::forward<Handle>(handle);
882 template <
typename Event>
884 {
return fTrackers.size(); }
888 template <
typename Event>
894 template <
typename Event>
898 unsigned int const nRemoved = std::count_if(
899 fTrackers.crbegin(), fTrackers.crend(),
900 [](
auto const& tracker){
return tracker->removeCachedProduct(); }
910 template <
typename Event>
912 { fTrackers.clear(); }
918 template <
typename Event>
920 (
bool removeCache , art::Event
const* event )
922 if (event && (
fEvent != event))
return 0;
924 unsigned int count = 0;
935 template <
typename Event>
936 template <
typename Handle>
938 (Handle
const& like)
const
946 std::any anyHandlePtr = tracker->handlePtr();
947 Handle
const** handlePtr = std::any_cast<Handle
const*>(&anyHandlePtr);
948 if (!handlePtr)
continue;
950 if ((*handlePtr)->provenance()->inputTag() != like.provenance()->inputTag())
962 template <
typename Event>
963 template <
typename Handle>
969 if (!tracker)
return false;
970 if (ProvenanceGetter<Handle>::productType() != tracker->productType())
972 if (ProvenanceGetter<Handle>::inputTag() != tracker->inputTag())
980 template <
typename Event>
985 throw art::Exception(art::errors::LogicError)
986 <<
"util::ArtHandleTrackerManager attempted an operation without a valid"
987 " event.\nThe operation was: " << where <<
"\n";
995 template <
typename Event>
997 (Event
const& event,
bool removeCache )
998 : fManager{
event }, fRemoveCache{ removeCache }
1003 template <
typename Event>
1009 template <
typename Event>
1015 template <
typename Event>
1017 {
return fRemoveCache; }
1021 template <
typename Event>
1022 template <
typename T,
typename... Args>
1025 {
return fManager.template getHandle<T>(std::forward<Args>(
args)...); }
1029 template <
typename Event>
1030 template <
typename T,
typename... Args>
1033 {
return fManager.template getValidHandle<T>(std::forward<Args>(
args)...); }
1037 template <
typename Event>
1038 template <
typename Handle>
1040 (Handle&& handle) -> decltype(
auto)
1042 return fManager.template registerHandle<Handle>(std::forward<Handle>(handle));
1047 template <
typename Event>
1050 { fRemoveCache = removeCache; }
1054 template <
typename Event>
1060 template <
typename Event>
1066 template <
typename Event>
1080 template <
typename Event>
1088 #endif // ICARUSCORE_UTILITIES_ARTHANDLETRACKERMANAGER_H
virtual std::type_info const * doProductType() const override
Returns the C++ type of the handled data product.
unsigned int nTrackedHandles() const
Returns the number of handles currently tracked.
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Event Event_t
Type of data viewer object to operate with.
std::any handlePtr() const
Returns a container for a pointer to the managed handle. Caveat emptor.
bool fRemoveCache
Whether to remove cache on destructor.
unsigned int removeCachedProducts()
Clears the cached data products for all tracked handles.
~LocalArtHandleTrackerManager()
Destructor; will implement the RAII pattern (i.e. call doneWithEvent()).
ArtHandleTracker(Event const &event, Handle handle)
Constructor: records all the needed information.
art::ValidHandle< T > getValidHandle(Args &&...args)
Retrieves a valid handle from the event, and registers it.
Interface to facilitate the use of util::ArtHandleTracker specializations.
std::vector< TrackerPtr > fTrackers
List of managed handle trackers.
void forgetAllHandles()
Stops tracking any handle, without removing their cache first.
static art::InputTag inputTag(Handle const &handle)
virtual art::InputTag doInputTag() const override
static art::Provenance const * provenance(Handle const &handle)
Config_t const fConfig
Configuration.
void useEvent(Event_t const &event)
Changes the event being served.
bool hasEvent() const
Returns whether the object is associated to an event.
ArtHandleTrackerManager< Event > fManager
The actual manager doing the work.
static std::type_info const * productType()
Returns the C++ type of the handled data product.
std::unique_ptr< util::ArtHandleTrackerInterface< Event_t >> TrackerPtr
Type of pointer to any handle tracker.
virtual std::string doProductClass() const override
Returns the name of the class pointed by the handle.
art::Provenance const * provenance() const
Returns the provenance information of the handle.
art::ValidHandle< T > getValidHandle(Args &&...args)
Retrieves a valid handle from the event, and registers it.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
unsigned int doneWithEvent()
Completes the work on the associated event.
Manages handle trackers for an easy call of removeCachedProduct().
virtual std::any doHandlePtr() const override
Returns a pointer to the managed handle, wrapped in std::any.
decltype(auto) registerHandle(Handle &&handle)
Registers an existing handle.
LocalArtHandleTrackerManager(Event const &event, bool removeCache=true)
Constructor: operates on the specified event.
std::type_info const * productType() const
Returns the name of the class of handled data product.
static bool handleSameProduct(TrackerPtr tracker, Handle const &handle)
Returns whether tracker tracks the same product that handle handles.
static std::type_info const * productType(Handle const &)
Returns the C++ type of the handled data product.
fhicl::ParameterSet Config_t
std::string logCategory
Name of the output category for messages.
art::InputTag inputTag() const
Returns the tag of handled data product.
static std::string productClass(Handle const &handle)
Returns the name of the class pointed by the handle.
unsigned int doneWithEvent(bool removeCache=true, art::Event const *event=nullptr)
Completes the work on the associated event.
void canOperate(const char *where) const
Handle const * findHandle(Handle const &like) const
Returns the pointer to an handle equivalent to like.
art::Handle< T > getHandle(Args &&...args)
Retrieves an handle from the event, and registers it.
art::InputTag inputTagOf(Event const &event, art::ProductID const &productID)
Reads and returns the input tag of the producer of productID.
std::string productClassOf(Handle const &handle)
use candy
bool removeCachedProduct()
unsigned int nTrackedHandles() const
Returns the number of handles currently tracked.
art::InputTag inputTagOf(Handle const &handle)
void forgetAllHandles()
Stops tracking any handle, without removing their cache first.
Event_t const * fEvent
Event being manager. Must be present!
unsigned int removeCachedProducts()
Clears the cached data products for all tracked handles.
bool willRemoveCachedProducts() const
std::type_info const * productTypeOf(Handle const &handle)
ArtHandleTrackerManager(Config_t config={})
Constructs a handle manager.
decltype(auto) registerHandle(Handle &&handle)
Registers an existing handle.
std::size_t count(Cont const &cont)
art::Handle< T > getHandle(Args &&...args)
Retrieves an handle from the event, and registers it.
virtual bool doRemoveCachedProduct() override
Actually removes the cache.
std::string productClass() const
Returns the name of the class of handled data product.
void setRemoveCachedProducts(bool removeCache)
Sets whether to remove tracked data caches on destruction or not.
Variant of util::ArtHandleTrackerManager in local scope.
BEGIN_PROLOG don t mess with this pandoraTrackGausCryoW true
Handle const & handle() const
Returns the tracked handle.
Tracks art handle objects.