View.h
Go to the documentation of this file.
1 #ifndef art_Framework_Principal_View_h
2 #define art_Framework_Principal_View_h
3 // vim: set sw=2 expandtab :
4 
5 // ====================================================================
6 // The class template View<T> provides a means to obtain pointers (of
7 // type T const*) into an arbitrary collection in an Event.
8 //
9 // A View<T> is *valid* if the container data product pointer is set.
10 // A valid View may still hold an empty vector of pointes; this means
11 // that either the referenced container was empty, or that the View's
12 // vector was emptied after the View was created.
13 //
14 // While View<T> is not a persistent class, one can fill a
15 // PtrVector<T> from a View<T>, as long as no new pointers have been
16 // added to the View<T>.
17 // ====================================================================
18 
22 
23 #include <string>
24 #include <utility>
25 
26 namespace art {
27 
28  class DataViewImpl;
29  class InputTag;
30 
31  template <typename T>
32  class View {
33 
34  // Give access to private ctor.
35  template <typename Element>
36  friend bool DataViewImpl::getView(std::string const&,
37  std::string const&,
38  std::string const&,
39  View<Element>&) const;
40 
41  public:
42  using collection_type = std::vector<T const*>;
43  using value_type = typename collection_type::value_type;
46  using size_type = typename collection_type::size_type;
47 
48  View() = default;
49  View(std::vector<T const*>, ProductID const, EDProduct const*);
50 
51  auto
52  isValid() const noexcept
53  {
54  return prod_ != nullptr;
55  }
56 
57  auto
58  id() const
59  {
60  return id_;
61  }
62 
63  // Fill a PtrVector<T> with Ptrs to each element of the container data
64  // product that can be reached from the internal vector<T const*>.
65  void fill(PtrVector<T>& pv) const;
66 
67  auto&
68  vals() noexcept
69  {
70  return vals_;
71  }
72 
73  auto const&
74  vals() const noexcept
75  {
76  return vals_;
77  }
78 
79  operator auto&() noexcept { return vals_; }
80  operator auto const&() const noexcept { return vals_; }
81 
82  auto
83  begin() noexcept
84  {
85  return vals_.begin();
86  }
87 
88  auto
89  end() noexcept
90  {
91  return vals_.end();
92  }
93 
94  auto
95  begin() const noexcept
96  {
97  return vals_.begin();
98  }
99 
100  auto
101  end() const noexcept
102  {
103  return vals_.end();
104  }
105 
106  auto
107  cbegin() const noexcept
108  {
109  return vals_.cbegin();
110  }
111 
112  auto
113  cend() const noexcept
114  {
115  return vals_.cend();
116  }
117 
118  auto
119  size() const noexcept
120  {
121  return vals_.size();
122  }
123 
124  private:
125  // Vector of pointers to elements in the container product (this is the view
126  // itself).
127  std::vector<T const*> vals_{};
128 
129  // The container id.
131 
132  // The container itself, we do not own.
133  EDProduct const* prod_{nullptr};
134  };
135 
136  template <typename T>
137  View<T>::View(std::vector<T const*> v, ProductID const id, EDProduct const* p)
138  : vals_{move(v)}, id_{id}, prod_{p}
139  {}
140 
141  // Fill a PtrVector<T> with Ptrs to each element of the container
142  // data product that can be reached from the internal vector<T const*>.
143  template <typename T>
144  void
146  {
147  if (not isValid()) {
148  return;
149  }
150  typename std::vector<T const*>::size_type i{};
151  for (auto a : prod_->getView()) {
152  // We do this as a sloppy guard against the container data
153  // product having been replaced between the View<T> ctor and the
154  // fill call. The user is supposed to do event.getView<T>()
155  // immediately followed by View<T>::fill(), but well. This can
156  // be O(n^2) so maybe it is not worth it in the case of large
157  // containers.
158  if (cet::search_all(vals_, a)) {
159  pv.emplace_back(id_, reinterpret_cast<T const*>(a), i);
160  ++i;
161  }
162  }
163  }
164 
165 } // namespace art
166 
167 #endif /* art_Framework_Principal_View_h */
168 
169 // Local Variables:
170 // mode: c++
171 // End:
intermediate_table::iterator iterator
auto const & vals() const noexcept
Definition: View.h:74
EDProduct const * prod_
Definition: View.h:133
AdcChannelData::View View
std::string string
Definition: nybbler.cc:12
auto end() noexcept
Definition: View.h:89
auto end() const noexcept
Definition: View.h:101
auto id() const
Definition: View.h:58
typename collection_type::const_iterator const_iterator
Definition: View.h:44
void emplace_back(Args &&...args)
Definition: PtrVector.h:448
intermediate_table::const_iterator const_iterator
void fill(PtrVector< T > &pv) const
Definition: View.h:145
auto begin() const noexcept
Definition: View.h:95
auto & vals() noexcept
Definition: View.h:68
View()=default
ProductID id_
Definition: View.h:130
bool search_all(FwdCont const &, Datum const &)
auto begin() noexcept
Definition: View.h:83
const double a
def move(depos, offset)
Definition: depos.py:107
std::vector< T const * > collection_type
Definition: View.h:42
std::vector< T const * > vals_
Definition: View.h:127
p
Definition: test.py:223
typename collection_type::iterator iterator
Definition: View.h:45
auto size() const noexcept
Definition: View.h:119
auto isValid() const noexcept
Definition: View.h:52
auto cbegin() const noexcept
Definition: View.h:107
auto cend() const noexcept
Definition: View.h:113
typename collection_type::value_type value_type
Definition: View.h:43
std::size_t getView(std::string const &moduleLabel, std::string const &productInstanceName, std::string const &processName, std::vector< ELEMENT const * > &result) const
Definition: DataViewImpl.h:500
virtual std::vector< void const * > getView() const
Definition: EDProduct.h:39
typename collection_type::size_type size_type
Definition: View.h:46