72 #ifndef ASSOCIATIONUTIL_H
73 #define ASSOCIATIONUTIL_H
86 #include "art/Framework/Principal/Handle.h"
87 #include "art/Persistency/Common/PtrMaker.h"
88 #include "canvas/Persistency/Common/Assns.h"
89 #include "canvas/Persistency/Common/FindMany.h"
90 #include "canvas/Persistency/Common/FindManyP.h"
91 #include "canvas/Persistency/Common/FindOne.h"
92 #include "canvas/Persistency/Common/FindOneP.h"
93 #include "canvas/Persistency/Common/Ptr.h"
94 #include "canvas/Persistency/Common/PtrVector.h"
95 #include "messagefacility/MessageLogger/MessageLogger.h"
157 template<
class T,
class U>
159 std::vector<T>
const&
a,
161 art::Assns<U,T> &assn,
162 std::string a_instance,
163 size_t index=UINT_MAX
213 template<
class T,
class U>
215 std::vector<T>
const&
a,
217 art::Assns<U,T> &assn,
218 size_t index=UINT_MAX)
219 {
return CreateAssn(evt, a, b, assn, std::string(), index); }
236 template<
class T,
class U>
239 art::Ptr<T>
const&
a,
240 art::Ptr<U>
const& b,
241 art::Assns<U,T> & assn
262 template<
class T,
class U>
265 std::vector<T>
const&
a,
266 art::PtrVector<U>
const& b,
267 art::Assns<T,U> & assn,
268 size_t index = UINT_MAX
290 template<
class T,
class U>
293 art::Ptr<T>
const&
a,
295 art::Assns<T,U> & assn
316 template<
class T,
class U>
319 std::vector<T>
const&
a,
321 art::Assns<T,U> & assn,
322 size_t index = UINT_MAX
352 template<
class T,
class U>
355 std::vector<T>
const&
a,
356 std::vector<U>
const& b,
357 art::Assns<T,U> & assn,
360 size_t index = UINT_MAX
389 template<
class T,
class U>
392 std::vector<T>
const&
a,
393 std::vector<U>
const& b,
394 art::Assns<T,U> & assn,
395 std::vector<size_t>
const&
indices,
396 size_t index = UINT_MAX
437 template <
typename T,
typename U,
typename Iter>
440 art::Assns<T,U> & assn,
442 Iter from_second_index,
502 template <
typename T,
typename U,
typename D>
505 art::Assns<T,U,D> & assn,
508 typename art::Assns<T,U,D>::data_t&& data
511 template <
typename T,
typename U,
typename D>
514 art::Assns<T,U,D> & assn,
517 typename art::Assns<T,U,D>::data_t
const& data
529 template<
class T,
class U>
530 std::vector<const U*>
532 art::Event
const&
evt,
533 std::string
const& label);
542 template<
class T,
class U>
543 std::vector< art::Ptr<U> >
545 art::Event
const&
evt,
546 std::string
const& label);
562 template<
class T,
class U>
565 art::Handle< std::vector<T> > index_p);
566 template<
class T,
class U>
567 std::vector<const U*>
569 art::Handle< std::vector<T> > index_p);
571 template<
class T,
class U>
572 std::vector< std::vector<size_t> >
574 art::Handle< std::vector<T> > index_p);
575 template<
class T,
class U>
576 std::vector< std::vector<const U*> >
578 art::Handle< std::vector<T> > index_p);
585 template<
class T,
class U>
588 std::vector<T>
const&
a,
589 art::Ptr<U>
const& b,
590 art::Assns<U,T> & assn,
591 std::string a_instance,
595 if (index == UINT_MAX) index = a.size()-1;
598 assn.addSingle(b, art::PtrMaker<T>{
evt, a_instance}(index));
601 catch(cet::exception &
e){
602 mf::LogWarning(
"AssociationUtil")
603 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
612 template<
class T,
class U>
615 art::Ptr<T>
const&
a,
616 art::Ptr<U>
const& b,
617 art::Assns<U,T> & assn
621 assn.addSingle(b, a);
623 catch(cet::exception &
e){
624 mf::LogWarning(
"AssociationUtil")
625 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
635 template<
class T,
class U>
638 std::vector<T>
const&
a,
639 art::PtrVector<U>
const& b,
640 art::Assns<T,U> & assn,
643 if(index == UINT_MAX) index = a.size() - 1;
646 auto const aptr = art::PtrMaker<T>{evt}(index);
647 for(art::Ptr<U>
const& b_item: b) assn.addSingle(aptr, b_item);
649 catch(cet::exception &
e){
650 mf::LogWarning(
"AssociationUtil")
651 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
660 template<
class T,
class U>
663 art::Ptr<T>
const&
a,
665 art::Assns<T,U> & assn
669 for (art::Ptr<U>
const& b_item: b) assn.addSingle(a, b_item);
671 catch(cet::exception
const&
e){
672 mf::LogWarning(
"AssociationUtil")
673 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
682 template<
class T,
class U>
685 std::vector<T>
const&
a,
687 art::Assns<T,U> & assn,
691 if (index == UINT_MAX) index = a.size() - 1;
694 auto const aptr = art::PtrMaker<T>{evt}(index);
695 for (art::Ptr<U>
const& b_item: b) assn.addSingle(aptr, b_item);
697 catch(cet::exception &
e){
698 mf::LogWarning(
"AssociationUtil")
699 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
708 template<
class T,
class U>
711 std::vector<T>
const&
a,
712 std::vector<U>
const& ,
713 art::Assns<T,U> & assn,
719 if(index == UINT_MAX) index = a.size() - 1;
722 auto const aptr = art::PtrMaker<T>{evt}(index);
723 art::PtrMaker<U>
const make_bptr{evt};
724 for(
size_t i = startU; i < endU; ++i){
725 assn.addSingle(aptr, make_bptr(i));
728 catch(cet::exception &
e){
729 mf::LogWarning(
"AssociationUtil")
730 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
739 template<
class T,
class U>
742 std::vector<T>
const&
a,
743 std::vector<U>
const& ,
744 art::Assns<T,U> & assn,
745 std::vector<size_t>
const&
indices,
749 if(index == UINT_MAX) index = a.size() - 1;
752 auto const aptr = art::PtrMaker<T>{evt}(index);
753 art::PtrMaker<U>
const make_bptr{evt};
754 for(
size_t index: indices){
755 assn.addSingle(aptr, make_bptr(index));
758 catch(cet::exception &
e){
759 mf::LogWarning(
"AssociationUtil")
760 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
769 template <
typename T,
typename U,
typename Iter>
772 art::Assns<T,U> & assn,
774 Iter from_second_index,
782 auto const first_ptr = art::PtrMaker<T>{evt}(first_index);
786 art::PtrMaker<U>
const make_second_ptr{evt};
787 for (; from_second_index != to_second_index; ++from_second_index) {
788 assn.addSingle(first_ptr, make_second_ptr(*from_second_index));
791 catch(cet::exception &
e){
792 mf::LogWarning(
"AssociationUtil")
793 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
803 template <
typename T,
typename U,
typename D>
806 art::Assns<T,U,D> & assn,
809 typename art::Assns<T,U,D>::data_t&& data
815 auto const first_ptr = art::PtrMaker<T>{evt}(first_index);
819 auto const second_ptr = art::PtrMaker<U>{evt}(second_index);
821 assn.addSingle(first_ptr, second_ptr, std::move(data));
823 catch(cet::exception &
e){
824 mf::LogWarning(
"AssociationUtil")
825 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
832 template <
typename T,
typename U,
typename D>
835 art::Assns<T,U,D> & assn,
838 typename art::Assns<T,U,D>::data_t
const& data
844 auto const first_ptr = art::PtrMaker<T>{evt}(first_index);
848 auto const second_ptr = art::PtrMaker<U>{evt}(second_index);
850 assn.addSingle(first_ptr, second_ptr, data);
852 catch(cet::exception &
e){
853 mf::LogWarning(
"AssociationUtil")
854 <<
"unable to create requested art:Assns, exception thrown: " <<
e;
862 template<
class T,
class U>
863 inline std::vector<const U*>
865 art::Event
const&
evt,
866 std::string
const& label)
872 std::vector<const U*> notAssociated;
874 art::FindOne<T>
const fa(b, evt, label);
876 for(
size_t u = 0; u < b->size(); ++u){
877 cet::maybe_ref<T const> t(fa.at(u));
879 art::Ptr<U> ptr(b, u);
880 notAssociated.push_back(ptr.get());
884 return notAssociated;
888 template<
class T,
class U>
889 inline std::vector< art::Ptr<U> >
891 art::Event
const&
evt,
892 std::string
const& label)
898 std::vector< art::Ptr<U> > notAssociated;
900 art::FindOneP<T>
const fa(b, evt, label);
902 for(
size_t u = 0; u < b->size(); ++u){
903 cet::maybe_ref<T const> t(fa.at(u));
905 notAssociated.emplace_back(b, u);
909 return notAssociated;
914 template<
class T,
class U>
915 inline std::vector<size_t>
917 art::Handle< std::vector<T> > index_p)
919 std::vector<size_t> associated_index(index_p->size());
920 for(
auto const& pair : *
h)
921 associated_index.at(pair.first.key()) = pair.second.key();
922 return associated_index;
925 template<
class T,
class U>
926 inline std::vector<const U*>
928 art::Handle< std::vector<T> > index_p)
930 std::vector<const U*> associated_pointer(index_p->size());
931 for(
auto const& pair : *
h)
932 associated_pointer.at(pair.first.key()) = &(*(pair.second));
933 return associated_pointer;
936 template<
class T,
class U>
937 inline std::vector< std::vector<size_t> >
939 art::Handle< std::vector<T> > index_p)
941 std::vector< std::vector<size_t> > associated_indices(index_p->size());
942 for(
auto const& pair : *
h)
943 associated_indices.at(pair.first.key()).push_back(pair.second.key());
944 return associated_indices;
947 template<
class T,
class U>
948 inline std::vector< std::vector<const U*> >
950 art::Handle< std::vector<T> > index_p)
952 std::vector< std::vector<const U*> > associated_pointers(index_p->size());
953 for(
auto const& pair : *
h)
954 associated_pointers.at(pair.first.key()).push_back( &(*(pair.second)) );
955 return associated_pointers;
968 #include "art/Framework/Core/EDProducer.h"
971 template <
typename Producer,
typename... Args>
972 std::enable_if_t<std::is_base_of_v<art::EDProducer, Producer>,
bool>
977 template <
typename Producer,
typename... Args>
978 std::enable_if_t<std::is_base_of_v<art::EDProducer, Producer>,
bool>
984 #endif //ASSOCIATIONUTIL_H
bool CreateAssnD(art::Event &evt, art::Assns< T, U, D > &assn, size_t first_index, size_t second_index, typename art::Assns< T, U, D >::data_t &&data)
Creates a single one-to-one association with associated data.
std::vector< const U * > GetAssociatedVectorOneP(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
std::vector< size_t > GetAssociatedVectorOneI(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
std::vector< art::Ptr< U > > FindUNotAssociatedToTP(art::Handle< U > b, art::Event const &evt, std::string const &label)
std::vector< std::vector< size_t > > GetAssociatedVectorManyI(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)
std::vector< const U * > FindUNotAssociatedToT(art::Handle< U > b, art::Event const &evt, std::string const &label)
std::vector< std::vector< const U * > > GetAssociatedVectorManyP(art::Handle< art::Assns< T, U > > h, art::Handle< std::vector< T > > index_p)