DUNEAnaUtilsBase.h
Go to the documentation of this file.
1 /**
2  *
3  * @file dunereco/AnaUtils/DUNEAnaUtilsBase.h
4  *
5  * @brief Base class containing functionality to extract products from the event
6 */
7 
8 #ifndef DUNE_ANA_UTILS_BASE_H
9 #define DUNE_ANA_UTILS_BASE_H
10 
13 #include "canvas/Persistency/Common/FindManyP.h"
14 
15 #include <string>
16 #include <vector>
17 
18 namespace dune_ana
19 {
20 /**
21  *
22  * @brief DUNEAnaUtilsBase class containing some template functions
23  *
24 */
26 {
27 protected:
28  template <typename T> static std::vector<art::Ptr<T>> GetProductVector(const art::Event &evt, const std::string &label);
29  template <typename T, typename U> static std::vector<art::Ptr<T>> GetAssocProductVector(const art::Ptr<U> &part, const art::Event &evt, const std::string &label, const std::string &assocLabel);
30  template <typename T, typename U> static art::Ptr<T> GetAssocProduct(const art::Ptr<U> &part, const art::Event &evt, const std::string &label, const std::string &assocLabel);
31 };
32 
33 // Implementation of the template function to get the products from the event
34 template <typename T> std::vector<art::Ptr<T>> DUNEAnaUtilsBase::GetProductVector(const art::Event &evt, const std::string &label)
35 {
36  auto theseProds = evt.getHandle<std::vector<T>>(label);
37  bool success = theseProds.isValid();
38 
39  if (!success)
40  {
41  mf::LogError("DUNEAna") << " Failed to find product with label " << label << " ... returning empty vector" << std::endl;
42  return std::vector<art::Ptr<T>>();
43  }
44 
45  // We need to convert these to art pointers
46  std::vector<art::Ptr<T>> productVector;
47  art::fill_ptr_vector(productVector,theseProds);
48 
49  return productVector;
50 }
51 
52 // Implementation of the template function to get the associated products from the event
53 template <typename T, typename U> std::vector<art::Ptr<T>> DUNEAnaUtilsBase::GetAssocProductVector(const art::Ptr<U> &pProd, const art::Event &evt, const std::string &label, const std::string &assocLabel)
54 {
55  auto products = evt.getHandle<std::vector<U>>(label);
56  bool success = products.isValid();
57 
58  if (!success)
59  {
60  mf::LogError("DUNEAna") << " Failed to find product with label " << label << " ... returning empty vector" << std::endl;
61  return std::vector<art::Ptr<T>>();
62  }
63 
64  const art::FindManyP<T> findParticleAssocs(products,evt,assocLabel);
65 
66  return findParticleAssocs.at(pProd.key());
67 }
68 
69 // Implementation of the template function to get the associated product from the event
70 template <typename T, typename U> art::Ptr<T> DUNEAnaUtilsBase::GetAssocProduct(const art::Ptr<U> &pProd, const art::Event &evt, const std::string &label, const std::string &assocLabel)
71 {
72  std::vector<art::Ptr<T>> associatedProducts = DUNEAnaUtilsBase::GetAssocProductVector<T>(pProd,evt,label,assocLabel);
73  if (associatedProducts.empty())
74  {
75  throw cet::exception("DUNEAna") << "DUNEAnaUtilsBase::GetAssocProduct --- No associated object found";
76  }
77  return associatedProducts.at(0);
78 }
79 
80 
81 } // namespace dune_ana
82 
83 #endif // DUNE_ANA_PFPARTICLE_UTILS_H
84 
DUNEAnaUtilsBase class containing some template functions.
static std::vector< art::Ptr< T > > GetProductVector(const art::Event &evt, const std::string &label)
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
static art::Ptr< T > GetAssocProduct(const art::Ptr< U > &part, const art::Event &evt, const std::string &label, const std::string &assocLabel)
key_type key() const noexcept
Definition: Ptr.h:216
static std::vector< art::Ptr< T > > GetAssocProductVector(const art::Ptr< U > &part, const art::Event &evt, const std::string &label, const std::string &assocLabel)
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
Definition: fwd.h:31
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)