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  while (i_product != e_product && i_view != e_view) {
383  value_t const& product_item = **i_product;
384  value_t const& view_item = *i_view;
385  assert(product_item == view_item);
386  ++i_product;
387  ++i_view;
388  }
389  }
390 
391  void ViewAnalyzer::testPtrVector(Event const& e, std::string const& moduleLabel) const {
392  typedef PtrVector<int> sequence_t;
393  typedef int value_t;
394  typedef View<value_t> view_t;
395 
396  Handle<sequence_t> hproduct;
397  e.getByLabel(moduleLabel, hproduct);
398  assert(hproduct.isValid());
399 
400  Handle<view_t> hview;
401 
402  InputTag tag(moduleLabel + "doesNotExist");
403  e.getByLabel(tag, hview);
404  assert(!hview.isValid());
405 
406  e.getByLabel(moduleLabel + "doesNotExist", hview);
407  assert(!hview.isValid());
408 
409  InputTag tag2(moduleLabel);
410  e.getByLabel(tag2, hview);
411  assert(hview.isValid());
412 
413  assert(hproduct.id() == hview.id());
414  assert(*hproduct.provenance() == *hview.provenance());
415 
416  assert(hproduct->size() == hview->size());
417 
418  sequence_t::const_iterator i_product = hproduct->begin();
419  sequence_t::const_iterator e_product = hproduct->end();
420  view_t::const_iterator i_view = hview->begin();
421  view_t::const_iterator e_view = hview->end();
422  while (i_product != e_product && i_view != e_view) {
423  value_t const& product_item = **i_product;
424  value_t const& view_item = *i_view;
425  assert(product_item == view_item);
426  ++i_product;
427  ++i_view;
428  }
429  }
430 } // namespace edmtest
431 
432 namespace {
433  template <typename PtrT>
434  struct ValueType {
435  using type = typename PtrT::element_type;
436  };
437 
438  template <typename T>
439  struct ValueType<edm::Ptr<T>> {
440  using type = T;
441  };
442 
443  template <typename Ptr>
444  void testStdVectorPtrT(Event const& e, std::string const& moduleLabel) {
445  using sequence_t = std::vector<Ptr>;
446  using value_t = typename ValueType<Ptr>::type;
447  using view_t = View<value_t>;
448 
449  Handle<sequence_t> hproduct;
450  e.getByLabel(moduleLabel, hproduct);
451  assert(hproduct.isValid());
452 
453  Handle<view_t> hview;
454 
456  e.getByLabel(tag, hview);
457  assert(hview.isValid());
458 
459  assert(hproduct.id() == hview.id());
460  assert(*hproduct.provenance() == *hview.provenance());
461 
462  assert(hproduct->size() == hview->size());
463 
464  typename sequence_t::const_iterator i_product = hproduct->begin();
465  typename sequence_t::const_iterator e_product = hproduct->end();
466  typename view_t::const_iterator i_view = hview->begin();
467  typename view_t::const_iterator e_view = hview->end();
468  unsigned int slot = 0;
469  while (i_product != e_product && i_view != e_view) {
470  value_t const& product_item = **i_product;
471  value_t const& view_item = *i_view;
472  assert(product_item == view_item);
473 
474  edm::Ref<sequence_t> ref3(hproduct, slot);
475  assert(**ref3 == product_item);
476 
477  ++i_product;
478  ++i_view;
479  ++slot;
480  }
481  }
482 } // namespace
483 
484 namespace edmtest {
485  void ViewAnalyzer::testStdVectorPtr(Event const& e, std::string const& moduleLabel) const {
486  testStdVectorPtrT<edm::Ptr<int>>(e, moduleLabel);
487  }
488 
489  void ViewAnalyzer::testStdVectorUniquePtr(Event const& e, std::string const& moduleLabel) const {
490  testStdVectorPtrT<std::unique_ptr<int>>(e, moduleLabel);
491  testStdVectorPtrT<std::unique_ptr<IntProduct>>(e, moduleLabel);
492  }
493 
494 } // namespace edmtest
495 
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
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.
ValueType
Type of the value held by a Value object.
Definition: value.h:25
long double T