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