LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PtrMaker.h
Go to the documentation of this file.
1 #ifndef art_Persistency_Common_PtrMaker_h
2 #define art_Persistency_Common_PtrMaker_h
3 // vim: set sw=2 expandtab :
4 //
5 // Class: PtrMaker
6 // File: art/art/Persistency/Common/PtrMaker.h
7 //
8 // Description: A common pattern is to create a collection A, and a
9 // collection B, create art::Ptrs to the objects in each of the
10 // collection and then create associations between the objects in the
11 // two collections. The purpose of art::PtrMaker is to simplify the
12 // process of creating art::Assns by providing a utility to create
13 // art::Ptrs. It is a two step process to create an art::Ptr with this
14 // approach.
15 //
16 // Step I has two cases; the product and ptrs are constructed in the
17 // same module, or in different modules. For each case, there is a way
18 // to construct a PtrMaker object.
19 //
20 // case 1: Construct a PtrMaker object that creates Ptrs into a
21 // collection of type C, the most common one is std::vector and hence
22 // is the default, created by the module of type Module, where the
23 // collection has instance name "instance", which is optional. For
24 // example, to create a PtrMaker for an std::vector<A> in an event
25 // evt, and current module, we will use the PtrMaker as follows:
26 //
27 // PtrMaker<A> make_Aptr{evt}; // or
28 // auto make_Aptr = PtrMaker<A>::create<std::vector<A>>(evt);
29 //
30 // If a container other std::vector<A> is desired, the static function
31 // 'create' must be used instead of one of the constructors.
32 //
33 // case 2: In this case, the collection of type C is created in
34 // another module. We need the product ID to create an object of
35 // PtrMaker. The way to get a product ID is to first get an
36 // art::Handle and then use "id()". Assuming, h is the art::Handle to
37 // the data product, and evt is art::Event, then we will use it as
38 // follows:
39 //
40 // art::Handle<std::vector<A>> h;
41 // PtrMaker<A> make_Aptr{evt, h.id()};
42 //
43 // Step II: Use an index to create an art::Ptr to an object in the
44 // slot indicated by "index"
45 //
46 // auto const a = make_Aptr(index);
47 //
48 
51 
52 #include <cstddef>
53 #include <string>
54 #include <vector>
55 
56 namespace art {
57 
58  // To create art::Ptrs into a particular collection in an event,
59  // subrun, run, or results.
60  template <typename T>
61  class PtrMaker {
62  public:
63  // Creates a PtrMaker that creates Ptrs into a collection of type
64  // 'Container'.
65  template <typename Container, typename DataLevel>
66  static PtrMaker<T> create(DataLevel const& E,
67  std::string const& instance = {});
68 
69  // Creates a PtrMaker that creates Ptrs into a collection of type
70  // std::vector<T>.
71  template <typename DataLevel>
72  PtrMaker(DataLevel const& evt, std::string const& instance = {});
73 
74  // Use this constructor when making Ptrs to products created in
75  // other modules.
76  template <typename DataLevel>
77  PtrMaker(DataLevel const& evt, ProductID prodId);
78 
79  // Creates a Ptr to an object in the slot indicated by "index"
80  Ptr<T> operator()(std::size_t index) const;
81 
82  private:
85  };
86 
87  template <typename T>
88  template <typename Container, typename DataLevel>
90  PtrMaker<T>::create(DataLevel const& evt, std::string const& instance)
91  {
92  auto const pid = evt.template getProductID<Container>(instance);
93  return PtrMaker<T>{evt, pid};
94  }
95 
96  template <typename T>
97  template <typename DataLevel>
98  PtrMaker<T>::PtrMaker(DataLevel const& evt, std::string const& instance)
99  : PtrMaker{evt, evt.template getProductID<std::vector<T>>(instance)}
100  {}
101 
102  template <typename T>
103  template <typename DataLevel>
104  PtrMaker<T>::PtrMaker(DataLevel const& evt, ProductID const pid)
105  : prodId_{pid}, prodGetter_{evt.productGetter(pid)}
106  {}
107 
108  template <typename T>
109  Ptr<T>
110  PtrMaker<T>::operator()(size_t const index) const
111  {
112  return Ptr<T>{prodId_, index, prodGetter_};
113  }
114 
115 } // namespace art
116 
117 #endif /* art_Persistency_Common_PtrMaker_h */
118 
119 // Local Variables:
120 // mode: c++
121 // End:
static PtrMaker< T > create(DataLevel const &E, std::string const &instance={})
Definition: PtrMaker.h:90
const std::string instance
Ptr< T > operator()(std::size_t index) const
Definition: PtrMaker.h:110
PtrMaker(DataLevel const &evt, std::string const &instance={})
Definition: PtrMaker.h:98
Float_t E
Definition: plot.C:20
ProductID const prodId_
Definition: PtrMaker.h:83
Definition: MVAAlg.h:12
TCEvent evt
Definition: DataStructs.cxx:8
Definition: fwd.h:26
EDProductGetter const * prodGetter_
Definition: PtrMaker.h:84