CMS 3D CMS Logo

ViewAnalyzer.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cassert>
3 #include <deque>
4 #include <list>
5 #include <set>
6 #include <typeinfo>
7 #include <utility>
8 #include <vector>
9 #include <string>
10 
21 #include "DataFormats/TestObjects/interface/ToyProducts.h"
24 
25 using namespace edm;
26 using namespace std::rel_ops;
27 
28 namespace edmtest {
30  public:
31  explicit ViewAnalyzer(edm::ParameterSet const& /* no parameters*/);
32  void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const& /* unused */) const override;
33 
34  template <typename P, typename V>
35  void testProduct(edm::Event const& e, std::string const& moduleLabel) const;
36 
37  void testDSVProduct(edm::Event const& e, std::string const& moduleLabel) const;
38 
39  void testAVProduct(edm::Event const& e, std::string const& moduleLabel) const;
40 
41  void testProductWithBaseClass(edm::Event const& e, std::string const& moduleLabel) const;
42 
43  void testRefVector(edm::Event const& e, std::string const& moduleLabel) const;
44 
45  void testRefToBaseVector(edm::Event const& e, std::string const& moduleLabel) const;
46 
47  void testPtrVector(edm::Event const& e, std::string const& moduleLabel) const;
48 
49  void testStdVectorPtr(edm::Event const& e, std::string const& moduleLabel) const;
50 
51  void testStdVectorUniquePtr(edm::Event const& e, std::string const& moduleLabel) const;
52  };
53 
54  ViewAnalyzer::ViewAnalyzer(ParameterSet const&) {
55  consumes<edm::View<int>>(edm::InputTag{"intvec", "", "TEST"});
56  consumes<edm::View<int>>(edm::InputTag{"intvec", ""});
57  consumes<std::vector<int>>(edm::InputTag{"intvec"});
58  consumes<edm::View<int>>(edm::InputTag{"intvec"});
59  consumes<std::list<int>>(edm::InputTag{"intlist"});
60  consumes<edm::View<int>>(edm::InputTag{"intlist"});
61  consumes<std::deque<int>>(edm::InputTag{"intdeque"});
62  consumes<edm::View<int>>(edm::InputTag{"intdeque"});
63  consumes<std::set<int>>(edm::InputTag{"intset"});
64  consumes<edm::View<int>>(edm::InputTag{"intset"});
65  consumes<SCSimpleProduct>(edm::InputTag{"simple"});
66  consumes<edm::View<SCSimpleProduct::value_type>>(edm::InputTag{"simple"});
67  consumes<OVSimpleProduct>(edm::InputTag{"ovsimple"});
68  consumes<edm::View<OVSimpleProduct::value_type>>(edm::InputTag{"ovsimple"});
69  consumes<AVSimpleProduct>(edm::InputTag{"avsimple"});
70  consumes<edm::View<AVSimpleProduct::value_type>>(edm::InputTag{"avsimple"});
71  consumes<edmtest::DSVSimpleProduct>(edm::InputTag{"dsvsimple"});
72  consumes<edm::View<edmtest::DSVSimpleProduct::value_type>>(edm::InputTag{"dsvsimple"});
73 
74  consumes<OVSimpleDerivedProduct>(edm::InputTag{"ovsimple", "derived"});
75  consumes<edm::View<Simple>>(edm::InputTag{"ovsimple", "derived"});
76 
77  consumes<RefVector<std::vector<int>>>(edm::InputTag{"intvecrefvec"});
78  consumes<edm::View<int>>(edm::InputTag{"intvecrefvec"});
79 
80  consumes<RefToBaseVector<int>>(edm::InputTag{"intvecreftbvec"});
81  consumes<edm::View<int>>(edm::InputTag{"intvecreftbvec"});
82 
83  consumes<PtrVector<int>>(edm::InputTag{"intvecptrvec"});
84  consumes<edm::View<int>>(edm::InputTag{"intvecptrvec"});
85 
86  consumes<std::vector<edm::Ptr<int>>>(edm::InputTag{"intvecstdvecptr"});
87  consumes<edm::View<int>>(edm::InputTag{"intvecstdvecptr"});
88 
89  consumes<std::vector<std::unique_ptr<int>>>(edm::InputTag{"intvecstdvecuniqptr"});
90  consumes<edm::View<int>>(edm::InputTag{"intvecstdvecuniqptr"});
91  consumes<std::vector<std::unique_ptr<IntProduct>>>(edm::InputTag{"intvecstdvecuniqptr"});
92  consumes<edm::View<IntProduct>>(edm::InputTag{"intvecstdvecuniqptr"});
93 
94  mayConsume<edm::View<int>>(edm::InputTag{"intvecptrvecdoesNotExist"});
95  }
96 
97  template <typename P, typename V = typename P::value_type>
98  struct tester {
99  static void call(ViewAnalyzer const* va, Event const& e, char const* moduleLabel) {
100  va->template testProduct<P, V>(e, moduleLabel);
101  }
102  };
103 
104  void ViewAnalyzer::analyze(StreamID, Event const& e, EventSetup const& /* unused */) const {
105  tester<std::vector<int>>::call(this, e, "intvec");
106  tester<std::list<int>>::call(this, e, "intlist");
107  tester<std::deque<int>>::call(this, e, "intdeque");
108  tester<std::set<int>>::call(this, e, "intset");
109 
110  tester<SCSimpleProduct>::call(this, e, "simple");
111  tester<OVSimpleProduct>::call(this, e, "ovsimple");
112  tester<AVSimpleProduct>::call(this, e, "avsimple");
113 
114  testDSVProduct(e, "dsvsimple");
115  testAVProduct(e, "avsimple");
116  testProductWithBaseClass(e, "ovsimple");
117  testRefVector(e, "intvecrefvec");
118  testRefToBaseVector(e, "intvecreftbvec");
119  testPtrVector(e, "intvecptrvec");
120  testStdVectorPtr(e, "intvecstdvecptr");
121  testStdVectorUniquePtr(e, "intvecstdvecuniqptr");
122 
123  //See if InputTag works
124  {
125  edm::InputTag tag("intvec", "");
127  e.getByLabel(tag, hInt);
128  assert(hInt.isValid());
129  }
130  {
131  edm::InputTag tag("intvec", "", "TEST");
133  e.getByLabel(tag, hInt);
134  assert(hInt.isValid());
135  }
136  }
137 
138  template <typename P, typename V>
139  void ViewAnalyzer::testProduct(Event const& e, std::string const& moduleLabel) const {
140  typedef P sequence_t;
141  typedef V value_t;
142  typedef View<value_t> view_t;
143 
144  Handle<sequence_t> hproduct;
145  e.getByLabel(moduleLabel, hproduct);
146  assert(hproduct.isValid());
147 
148  Handle<view_t> hview;
149  e.getByLabel(moduleLabel, hview);
150  assert(hview.isValid());
151 
152  assert(hproduct.id() == hview.id());
153  assert(*hproduct.provenance() == *hview.provenance());
154 
155  assert(hproduct->size() == hview->size());
156 
157  typename sequence_t::const_iterator i_product = hproduct->begin();
158  typename sequence_t::const_iterator e_product = hproduct->end();
159  typename view_t::const_iterator i_view = hview->begin();
160  typename view_t::const_iterator e_view = hview->end();
161  size_t slot = 0;
162  while (i_product != e_product && i_view != e_view) {
163  value_t const& product_item = *i_product;
164  value_t const& view_item = *i_view;
165  assert(product_item == view_item);
166 
167  edm::Ref<sequence_t> ref3(hproduct, slot);
168  assert(*ref3 == product_item);
169 
170  edm::RefProd<sequence_t> refProd4(hproduct);
171  edm::Ref<sequence_t> ref4(refProd4, slot);
172  assert(*ref4 == product_item);
173 
174  ++i_product;
175  ++i_view;
176  ++slot;
177  }
178 
179  // Make sure the references are right.
180  size_t numElements = hview->size();
181  for (size_t i = 0; i < numElements; ++i) {
182  RefToBase<value_t> ref = hview->refAt(i);
183  assert(ref.isNonnull());
184  }
185  }
186 
187  void ViewAnalyzer::testDSVProduct(Event const& e, std::string const& moduleLabel) const {
188  typedef edmtest::DSVSimpleProduct sequence_t;
189  typedef sequence_t::value_type value_t;
190  typedef View<value_t> view_t;
191 
192  Handle<sequence_t> hprod;
193  e.getByLabel(moduleLabel, hprod);
194  assert(hprod.isValid());
195 
196  Handle<view_t> hview;
197  e.getByLabel(moduleLabel, hview);
198  assert(hview.isValid());
199 
200  assert(hprod.id() == hview.id());
201  assert(*hprod.provenance() == *hview.provenance());
202 
203  assert(hprod->size() == hview->size());
204 
205  sequence_t::const_iterator i_prod = hprod->begin();
206  sequence_t::const_iterator e_prod = hprod->end();
207  view_t::const_iterator i_view = hview->begin();
208  view_t::const_iterator e_view = hview->end();
209 
210  while (i_prod != e_prod && i_view != e_view) {
211  value_t const& prod = *i_prod;
212  value_t const& view = *i_view;
213  assert(prod.detId() == view.detId());
214  assert(prod.data == view.data);
215 
216  ++i_prod;
217  ++i_view;
218  }
219  }
220 
221  void ViewAnalyzer::testAVProduct(Event const& e, std::string const& moduleLabel) const {
222  typedef edmtest::AVSimpleProduct sequence_t;
223  typedef sequence_t::value_type value_t;
224  typedef View<value_t> view_t;
225 
226  Handle<sequence_t> hprod;
227  e.getByLabel(moduleLabel, hprod);
228  assert(hprod.isValid());
229 
230  Handle<view_t> hview;
231  e.getByLabel(moduleLabel, hview);
232  assert(hview.isValid());
233 
234  assert(hprod.id() == hview.id());
235  assert(*hprod.provenance() == *hview.provenance());
236 
237  assert(hprod->size() == hview->size());
238 
239  sequence_t::const_iterator i_prod = hprod->begin();
240  sequence_t::const_iterator e_prod = hprod->end();
241  view_t::const_iterator i_view = hview->begin();
242  view_t::const_iterator e_view = hview->end();
243 
244  while (i_prod != e_prod && i_view != e_view) {
245  value_t const& prod = *i_prod;
246  value_t const& view = *i_view;
247  assert(prod == view);
248  assert((*hprod)[prod.first] == prod.second);
249  edm::Ptr<sequence_t::key_type> ptr(prod.first.id(), prod.first.key(), &e.productGetter());
250  assert((*hprod)[ptr] == prod.second);
252  assert((*hprod)[refToBase] == prod.second);
253  ++i_prod;
254  ++i_view;
255  }
256  }
257 
258  // The point of this one is to test that a one can get
259  // a View of "Simple" objects even when the sequence
260  // has elements of a different type. The different type
261  // inherits from "Simple" and is named "SimpleDerived"
262  void ViewAnalyzer::testProductWithBaseClass(Event const& e, std::string const& moduleLabel) const {
263  typedef OVSimpleDerivedProduct sequence_t;
264  typedef Simple value_t;
265  typedef View<value_t> view_t;
266 
267  Handle<sequence_t> hprod;
268  e.getByLabel(moduleLabel, "derived", hprod);
269  assert(hprod.isValid());
270 
271  Handle<view_t> hview;
272  e.getByLabel(moduleLabel, "derived", hview);
273  assert(hview.isValid());
274 
275  assert(hprod.id() == hview.id());
276  assert(*hprod.provenance() == *hview.provenance());
277 
278  assert(hprod->size() == hview->size());
279 
280  unsigned slot = 0;
281 
282  sequence_t::const_iterator i_prod = hprod->begin();
283  sequence_t::const_iterator e_prod = hprod->end();
284  view_t::const_iterator i_view = hview->begin();
285  view_t::const_iterator e_view = hview->end();
286 
287  while (i_prod != e_prod && i_view != e_view) {
288  SimpleDerived const& prod = *i_prod;
289  Simple const& view = *i_view;
290  assert(prod == view);
291 
292  // Tack on a test of RefToBase::castTo here
293 
294  edm::RefToBaseProd<Simple> refToBaseProd(hview);
295  edm::RefToBase<Simple> refToBase(refToBaseProd, slot);
296 
298  SimpleDerived const& valueFromPtr = *ptr;
299  assert(valueFromPtr == view);
300 
302  SimpleDerived const& valueFromRef = *ref;
303  assert(valueFromRef == view);
304 
305  ++i_prod;
306  ++i_view;
307  ++slot;
308  }
309  }
310 
311  void ViewAnalyzer::testRefVector(Event const& e, std::string const& moduleLabel) const {
312  typedef RefVector<std::vector<int>> sequence_t;
313  typedef int value_t;
314  typedef View<value_t> view_t;
315 
316  Handle<sequence_t> hproduct;
317  e.getByLabel(moduleLabel, hproduct);
318  assert(hproduct.isValid());
319 
320  Handle<view_t> hview;
321  e.getByLabel(moduleLabel, hview);
322  assert(hview.isValid());
323 
324  assert(hproduct.id() == hview.id());
325  assert(*hproduct.provenance() == *hview.provenance());
326 
327  assert(hproduct->size() == hview->size());
328 
329  sequence_t::const_iterator i_product = hproduct->begin();
330  sequence_t::const_iterator e_product = hproduct->end();
331  view_t::const_iterator i_view = hview->begin();
332  view_t::const_iterator e_view = hview->end();
333  size_t slot = 0;
334  while (i_product != e_product && i_view != e_view) {
335  value_t const& product_item = **i_product;
336  value_t const& view_item = *i_view;
337  assert(product_item == view_item);
338 
339  // Tack on a test of RefToBase::castTo here
340  edm::RefToBaseProd<int> refToBaseProd(hview);
341  edm::RefToBase<int> refToBase(refToBaseProd, slot);
342 
343  edm::Ptr<int> ref = refToBase.castTo<edm::Ptr<int>>();
344  int item_other = *ref;
345  assert(item_other == product_item);
346 
348  int item_other2 = *ref2;
349  assert(item_other2 == product_item);
350 
351  edm::Ref<sequence_t> ref3(hproduct, slot);
352  assert(*ref3 == product_item);
353 
354  ++i_product;
355  ++i_view;
356  ++slot;
357  }
358  }
359 
360  void ViewAnalyzer::testRefToBaseVector(Event const& e, std::string const& moduleLabel) const {
361  typedef RefToBaseVector<int> sequence_t;
362  typedef int value_t;
363  typedef View<value_t> view_t;
364 
365  Handle<sequence_t> hproduct;
366  e.getByLabel(moduleLabel, hproduct);
367  assert(hproduct.isValid());
368 
369  Handle<view_t> hview;
370  e.getByLabel(moduleLabel, hview);
371  assert(hview.isValid());
372 
373  assert(hproduct.id() == hview.id());
374  assert(*hproduct.provenance() == *hview.provenance());
375 
376  assert(hproduct->size() == hview->size());
377 
378  sequence_t::const_iterator i_product = hproduct->begin();
379  sequence_t::const_iterator e_product = hproduct->end();
380  view_t::const_iterator i_view = hview->begin();
381  view_t::const_iterator e_view = hview->end();
382  size_t slot = 0;
383  while (i_product != e_product && i_view != e_view) {
384  value_t const& product_item = **i_product;
385  value_t const& view_item = *i_view;
386  assert(product_item == view_item);
387  ++i_product;
388  ++i_view;
389  ++slot;
390  }
391  }
392 
393  void ViewAnalyzer::testPtrVector(Event const& e, std::string const& moduleLabel) const {
394  typedef PtrVector<int> sequence_t;
395  typedef int value_t;
396  typedef View<value_t> view_t;
397 
398  Handle<sequence_t> hproduct;
399  e.getByLabel(moduleLabel, hproduct);
400  assert(hproduct.isValid());
401 
402  Handle<view_t> hview;
403 
404  InputTag tag(moduleLabel + "doesNotExist");
405  e.getByLabel(tag, hview);
406  assert(!hview.isValid());
407 
408  e.getByLabel(moduleLabel + "doesNotExist", hview);
409  assert(!hview.isValid());
410 
411  InputTag tag2(moduleLabel);
412  e.getByLabel(tag2, hview);
413  assert(hview.isValid());
414 
415  assert(hproduct.id() == hview.id());
416  assert(*hproduct.provenance() == *hview.provenance());
417 
418  assert(hproduct->size() == hview->size());
419 
420  sequence_t::const_iterator i_product = hproduct->begin();
421  sequence_t::const_iterator e_product = hproduct->end();
422  view_t::const_iterator i_view = hview->begin();
423  view_t::const_iterator e_view = hview->end();
424  size_t slot = 0;
425  while (i_product != e_product && i_view != e_view) {
426  value_t const& product_item = **i_product;
427  value_t const& view_item = *i_view;
428  assert(product_item == view_item);
429  ++i_product;
430  ++i_view;
431  ++slot;
432  }
433  }
434 } // namespace edmtest
435 
436 namespace {
437  template <typename PtrT>
438  struct ValueType {
439  using type = typename PtrT::element_type;
440  };
441 
442  template <typename T>
443  struct ValueType<edm::Ptr<T>> {
444  using type = T;
445  };
446 
447  template <typename Ptr>
448  void testStdVectorPtrT(Event const& e, std::string const& moduleLabel) {
449  using sequence_t = std::vector<Ptr>;
450  using value_t = typename ValueType<Ptr>::type;
451  using view_t = View<value_t>;
452 
453  Handle<sequence_t> hproduct;
454  e.getByLabel(moduleLabel, hproduct);
455  assert(hproduct.isValid());
456 
457  Handle<view_t> hview;
458 
460  e.getByLabel(tag, hview);
461  assert(hview.isValid());
462 
463  assert(hproduct.id() == hview.id());
464  assert(*hproduct.provenance() == *hview.provenance());
465 
466  assert(hproduct->size() == hview->size());
467 
468  typename sequence_t::const_iterator i_product = hproduct->begin();
469  typename sequence_t::const_iterator e_product = hproduct->end();
470  typename view_t::const_iterator i_view = hview->begin();
471  typename view_t::const_iterator e_view = hview->end();
472  unsigned int slot = 0;
473  while (i_product != e_product && i_view != e_view) {
474  value_t const& product_item = **i_product;
475  value_t const& view_item = *i_view;
476  assert(product_item == view_item);
477 
478  edm::Ref<sequence_t> ref3(hproduct, slot);
479  assert(**ref3 == product_item);
480 
481  ++i_product;
482  ++i_view;
483  ++slot;
484  }
485  }
486 } // namespace
487 
488 namespace edmtest {
489  void ViewAnalyzer::testStdVectorPtr(Event const& e, std::string const& moduleLabel) const {
490  testStdVectorPtrT<edm::Ptr<int>>(e, moduleLabel);
491  }
492 
493  void ViewAnalyzer::testStdVectorUniquePtr(Event const& e, std::string const& moduleLabel) const {
494  testStdVectorPtrT<std::unique_ptr<int>>(e, moduleLabel);
495  testStdVectorPtrT<std::unique_ptr<IntProduct>>(e, moduleLabel);
496  }
497 
498 } // namespace edmtest
499 
ProductID id() const
Definition: HandleBase.cc:29
REF castTo() const
Definition: RefToBase.h:259
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:303
assert(be >=bs)
Provenance const * provenance() const
Definition: HandleBase.h:74
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ValueType
Type of the value held by a Value object.
Definition: value.h:23
static void call(ViewAnalyzer const *va, Event const &e, char const *moduleLabel)
Definition: ViewAnalyzer.cc:99
ProductIndex id() const
Definition: ProductID.h:35
uint16_t *__restrict__ uint16_t const *__restrict__ uint32_t const *__restrict__ uint32_t *__restrict__ uint32_t const *__restrict__ int32_t *__restrict__ uint32_t numElements
std::pair< OmniClusterRef, TrackingParticleRef > P
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
long double T