Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:38

0001 #include <algorithm>
0002 #include <cassert>
0003 #include <deque>
0004 #include <list>
0005 #include <set>
0006 #include <typeinfo>
0007 #include <utility>
0008 #include <vector>
0009 #include <string>
0010 
0011 #include "FWCore/Framework/interface/Frameworkfwd.h"
0012 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0013 #include "DataFormats/Common/interface/Handle.h"
0014 #include "DataFormats/Common/interface/Ptr.h"
0015 #include "DataFormats/Common/interface/PtrVector.h"
0016 #include "DataFormats/Common/interface/Ref.h"
0017 #include "DataFormats/Common/interface/RefVector.h"
0018 #include "DataFormats/Common/interface/RefToBase.h"
0019 #include "DataFormats/Common/interface/RefToBaseVector.h"
0020 #include "DataFormats/Common/interface/View.h"
0021 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0022 #include "FWCore/Framework/interface/Event.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 
0025 using namespace edm;
0026 using namespace std::rel_ops;
0027 
0028 namespace edmtest {
0029   class ViewAnalyzer : public edm::global::EDAnalyzer<> {
0030   public:
0031     explicit ViewAnalyzer(edm::ParameterSet const& /* no parameters*/);
0032     void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const& /* unused */) const override;
0033 
0034     template <typename P, typename V>
0035     void testProduct(edm::Event const& e, std::string const& moduleLabel) const;
0036 
0037     void testDSVProduct(edm::Event const& e, std::string const& moduleLabel) const;
0038 
0039     void testAVProduct(edm::Event const& e, std::string const& moduleLabel) const;
0040 
0041     void testProductWithBaseClass(edm::Event const& e, std::string const& moduleLabel) const;
0042 
0043     void testRefVector(edm::Event const& e, std::string const& moduleLabel) const;
0044 
0045     void testRefToBaseVector(edm::Event const& e, std::string const& moduleLabel) const;
0046 
0047     void testPtrVector(edm::Event const& e, std::string const& moduleLabel) const;
0048 
0049     void testStdVectorPtr(edm::Event const& e, std::string const& moduleLabel) const;
0050 
0051     void testStdVectorUniquePtr(edm::Event const& e, std::string const& moduleLabel) const;
0052   };
0053 
0054   ViewAnalyzer::ViewAnalyzer(ParameterSet const&) {
0055     consumes<edm::View<int>>(edm::InputTag{"intvec", "", "TEST"});
0056     consumes<edm::View<int>>(edm::InputTag{"intvec", ""});
0057     consumes<std::vector<int>>(edm::InputTag{"intvec"});
0058     consumes<edm::View<int>>(edm::InputTag{"intvec"});
0059     consumes<std::list<int>>(edm::InputTag{"intlist"});
0060     consumes<edm::View<int>>(edm::InputTag{"intlist"});
0061     consumes<std::deque<int>>(edm::InputTag{"intdeque"});
0062     consumes<edm::View<int>>(edm::InputTag{"intdeque"});
0063     consumes<std::set<int>>(edm::InputTag{"intset"});
0064     consumes<edm::View<int>>(edm::InputTag{"intset"});
0065     consumes<SCSimpleProduct>(edm::InputTag{"simple"});
0066     consumes<edm::View<SCSimpleProduct::value_type>>(edm::InputTag{"simple"});
0067     consumes<OVSimpleProduct>(edm::InputTag{"ovsimple"});
0068     consumes<edm::View<OVSimpleProduct::value_type>>(edm::InputTag{"ovsimple"});
0069     consumes<AVSimpleProduct>(edm::InputTag{"avsimple"});
0070     consumes<edm::View<AVSimpleProduct::value_type>>(edm::InputTag{"avsimple"});
0071     consumes<edmtest::DSVSimpleProduct>(edm::InputTag{"dsvsimple"});
0072     consumes<edm::View<edmtest::DSVSimpleProduct::value_type>>(edm::InputTag{"dsvsimple"});
0073 
0074     consumes<OVSimpleDerivedProduct>(edm::InputTag{"ovsimple", "derived"});
0075     consumes<edm::View<Simple>>(edm::InputTag{"ovsimple", "derived"});
0076 
0077     consumes<RefVector<std::vector<int>>>(edm::InputTag{"intvecrefvec"});
0078     consumes<edm::View<int>>(edm::InputTag{"intvecrefvec"});
0079 
0080     consumes<RefToBaseVector<int>>(edm::InputTag{"intvecreftbvec"});
0081     consumes<edm::View<int>>(edm::InputTag{"intvecreftbvec"});
0082 
0083     consumes<PtrVector<int>>(edm::InputTag{"intvecptrvec"});
0084     consumes<edm::View<int>>(edm::InputTag{"intvecptrvec"});
0085 
0086     consumes<std::vector<edm::Ptr<int>>>(edm::InputTag{"intvecstdvecptr"});
0087     consumes<edm::View<int>>(edm::InputTag{"intvecstdvecptr"});
0088 
0089     consumes<std::vector<std::unique_ptr<int>>>(edm::InputTag{"intvecstdvecuniqptr"});
0090     consumes<edm::View<int>>(edm::InputTag{"intvecstdvecuniqptr"});
0091     consumes<std::vector<std::unique_ptr<IntProduct>>>(edm::InputTag{"intvecstdvecuniqptr"});
0092     consumes<edm::View<IntProduct>>(edm::InputTag{"intvecstdvecuniqptr"});
0093 
0094     mayConsume<edm::View<int>>(edm::InputTag{"intvecptrvecdoesNotExist"});
0095   }
0096 
0097   template <typename P, typename V = typename P::value_type>
0098   struct tester {
0099     static void call(ViewAnalyzer const* va, Event const& e, char const* moduleLabel) {
0100       va->template testProduct<P, V>(e, moduleLabel);
0101     }
0102   };
0103 
0104   void ViewAnalyzer::analyze(StreamID, Event const& e, EventSetup const& /* unused */) const {
0105     tester<std::vector<int>>::call(this, e, "intvec");
0106     tester<std::list<int>>::call(this, e, "intlist");
0107     tester<std::deque<int>>::call(this, e, "intdeque");
0108     tester<std::set<int>>::call(this, e, "intset");
0109 
0110     tester<SCSimpleProduct>::call(this, e, "simple");
0111     tester<OVSimpleProduct>::call(this, e, "ovsimple");
0112     tester<AVSimpleProduct>::call(this, e, "avsimple");
0113 
0114     testDSVProduct(e, "dsvsimple");
0115     testAVProduct(e, "avsimple");
0116     testProductWithBaseClass(e, "ovsimple");
0117     testRefVector(e, "intvecrefvec");
0118     testRefToBaseVector(e, "intvecreftbvec");
0119     testPtrVector(e, "intvecptrvec");
0120     testStdVectorPtr(e, "intvecstdvecptr");
0121     testStdVectorUniquePtr(e, "intvecstdvecuniqptr");
0122 
0123     //See if InputTag works
0124     {
0125       edm::InputTag tag("intvec", "");
0126       edm::Handle<edm::View<int>> hInt;
0127       e.getByLabel(tag, hInt);
0128       assert(hInt.isValid());
0129     }
0130     {
0131       edm::InputTag tag("intvec", "", "TEST");
0132       edm::Handle<edm::View<int>> hInt;
0133       e.getByLabel(tag, hInt);
0134       assert(hInt.isValid());
0135     }
0136   }
0137 
0138   template <typename P, typename V>
0139   void ViewAnalyzer::testProduct(Event const& e, std::string const& moduleLabel) const {
0140     typedef P sequence_t;
0141     typedef V value_t;
0142     typedef View<value_t> view_t;
0143 
0144     Handle<sequence_t> hproduct;
0145     e.getByLabel(moduleLabel, hproduct);
0146     assert(hproduct.isValid());
0147 
0148     Handle<view_t> hview;
0149     e.getByLabel(moduleLabel, hview);
0150     assert(hview.isValid());
0151 
0152     assert(hproduct.id() == hview.id());
0153     assert(*hproduct.provenance() == *hview.provenance());
0154 
0155     assert(hproduct->size() == hview->size());
0156 
0157     typename sequence_t::const_iterator i_product = hproduct->begin();
0158     typename sequence_t::const_iterator e_product = hproduct->end();
0159     typename view_t::const_iterator i_view = hview->begin();
0160     typename view_t::const_iterator e_view = hview->end();
0161     size_t slot = 0;
0162     while (i_product != e_product && i_view != e_view) {
0163       value_t const& product_item = *i_product;
0164       value_t const& view_item = *i_view;
0165       assert(product_item == view_item);
0166 
0167       edm::Ref<sequence_t> ref3(hproduct, slot);
0168       assert(*ref3 == product_item);
0169 
0170       edm::RefProd<sequence_t> refProd4(hproduct);
0171       edm::Ref<sequence_t> ref4(refProd4, slot);
0172       assert(*ref4 == product_item);
0173 
0174       ++i_product;
0175       ++i_view;
0176       ++slot;
0177     }
0178 
0179     // Make sure the references are right.
0180     size_t numElements = hview->size();
0181     for (size_t i = 0; i < numElements; ++i) {
0182       RefToBase<value_t> ref = hview->refAt(i);
0183       assert(ref.isNonnull());
0184     }
0185   }
0186 
0187   void ViewAnalyzer::testDSVProduct(Event const& e, std::string const& moduleLabel) const {
0188     typedef edmtest::DSVSimpleProduct sequence_t;
0189     typedef sequence_t::value_type value_t;
0190     typedef View<value_t> view_t;
0191 
0192     Handle<sequence_t> hprod;
0193     e.getByLabel(moduleLabel, hprod);
0194     assert(hprod.isValid());
0195 
0196     Handle<view_t> hview;
0197     e.getByLabel(moduleLabel, hview);
0198     assert(hview.isValid());
0199 
0200     assert(hprod.id() == hview.id());
0201     assert(*hprod.provenance() == *hview.provenance());
0202 
0203     assert(hprod->size() == hview->size());
0204 
0205     sequence_t::const_iterator i_prod = hprod->begin();
0206     sequence_t::const_iterator e_prod = hprod->end();
0207     view_t::const_iterator i_view = hview->begin();
0208     view_t::const_iterator e_view = hview->end();
0209 
0210     while (i_prod != e_prod && i_view != e_view) {
0211       value_t const& prod = *i_prod;
0212       value_t const& view = *i_view;
0213       assert(prod.detId() == view.detId());
0214       assert(prod.data == view.data);
0215 
0216       ++i_prod;
0217       ++i_view;
0218     }
0219   }
0220 
0221   void ViewAnalyzer::testAVProduct(Event const& e, std::string const& moduleLabel) const {
0222     typedef edmtest::AVSimpleProduct sequence_t;
0223     typedef sequence_t::value_type value_t;
0224     typedef View<value_t> view_t;
0225 
0226     Handle<sequence_t> hprod;
0227     e.getByLabel(moduleLabel, hprod);
0228     assert(hprod.isValid());
0229 
0230     Handle<view_t> hview;
0231     e.getByLabel(moduleLabel, hview);
0232     assert(hview.isValid());
0233 
0234     assert(hprod.id() == hview.id());
0235     assert(*hprod.provenance() == *hview.provenance());
0236 
0237     assert(hprod->size() == hview->size());
0238 
0239     sequence_t::const_iterator i_prod = hprod->begin();
0240     sequence_t::const_iterator e_prod = hprod->end();
0241     view_t::const_iterator i_view = hview->begin();
0242     view_t::const_iterator e_view = hview->end();
0243 
0244     while (i_prod != e_prod && i_view != e_view) {
0245       value_t const& prod = *i_prod;
0246       value_t const& view = *i_view;
0247       assert(prod == view);
0248       assert((*hprod)[prod.first] == prod.second);
0249       edm::Ptr<sequence_t::key_type> ptr(prod.first.id(), prod.first.key(), &e.productGetter());
0250       assert((*hprod)[ptr] == prod.second);
0251       edm::RefToBase<sequence_t::key_type> refToBase(prod.first);
0252       assert((*hprod)[refToBase] == prod.second);
0253       ++i_prod;
0254       ++i_view;
0255     }
0256   }
0257 
0258   // The point of this one is to test that a one can get
0259   // a View of "Simple" objects even when the sequence
0260   // has elements of a different type. The different type
0261   // inherits from "Simple" and is named "SimpleDerived"
0262   void ViewAnalyzer::testProductWithBaseClass(Event const& e, std::string const& moduleLabel) const {
0263     typedef OVSimpleDerivedProduct sequence_t;
0264     typedef Simple value_t;
0265     typedef View<value_t> view_t;
0266 
0267     Handle<sequence_t> hprod;
0268     e.getByLabel(moduleLabel, "derived", hprod);
0269     assert(hprod.isValid());
0270 
0271     Handle<view_t> hview;
0272     e.getByLabel(moduleLabel, "derived", hview);
0273     assert(hview.isValid());
0274 
0275     assert(hprod.id() == hview.id());
0276     assert(*hprod.provenance() == *hview.provenance());
0277 
0278     assert(hprod->size() == hview->size());
0279 
0280     unsigned slot = 0;
0281 
0282     sequence_t::const_iterator i_prod = hprod->begin();
0283     sequence_t::const_iterator e_prod = hprod->end();
0284     view_t::const_iterator i_view = hview->begin();
0285     view_t::const_iterator e_view = hview->end();
0286 
0287     while (i_prod != e_prod && i_view != e_view) {
0288       SimpleDerived const& prod = *i_prod;
0289       Simple const& view = *i_view;
0290       assert(prod == view);
0291 
0292       // Tack on a test of RefToBase::castTo here
0293 
0294       edm::RefToBaseProd<Simple> refToBaseProd(hview);
0295       edm::RefToBase<Simple> refToBase(refToBaseProd, slot);
0296 
0297       edm::Ptr<SimpleDerived> ptr = refToBase.castTo<edm::Ptr<SimpleDerived>>();
0298       SimpleDerived const& valueFromPtr = *ptr;
0299       assert(valueFromPtr == view);
0300 
0301       edm::Ref<edm::OwnVector<SimpleDerived>> ref = refToBase.castTo<edm::Ref<edm::OwnVector<SimpleDerived>>>();
0302       SimpleDerived const& valueFromRef = *ref;
0303       assert(valueFromRef == view);
0304 
0305       ++i_prod;
0306       ++i_view;
0307       ++slot;
0308     }
0309   }
0310 
0311   void ViewAnalyzer::testRefVector(Event const& e, std::string const& moduleLabel) const {
0312     typedef RefVector<std::vector<int>> sequence_t;
0313     typedef int value_t;
0314     typedef View<value_t> view_t;
0315 
0316     Handle<sequence_t> hproduct;
0317     e.getByLabel(moduleLabel, hproduct);
0318     assert(hproduct.isValid());
0319 
0320     Handle<view_t> hview;
0321     e.getByLabel(moduleLabel, hview);
0322     assert(hview.isValid());
0323 
0324     assert(hproduct.id() == hview.id());
0325     assert(*hproduct.provenance() == *hview.provenance());
0326 
0327     assert(hproduct->size() == hview->size());
0328 
0329     sequence_t::const_iterator i_product = hproduct->begin();
0330     sequence_t::const_iterator e_product = hproduct->end();
0331     view_t::const_iterator i_view = hview->begin();
0332     view_t::const_iterator e_view = hview->end();
0333     size_t slot = 0;
0334     while (i_product != e_product && i_view != e_view) {
0335       value_t const& product_item = **i_product;
0336       value_t const& view_item = *i_view;
0337       assert(product_item == view_item);
0338 
0339       // Tack on a test of RefToBase::castTo here
0340       edm::RefToBaseProd<int> refToBaseProd(hview);
0341       edm::RefToBase<int> refToBase(refToBaseProd, slot);
0342 
0343       edm::Ptr<int> ref = refToBase.castTo<edm::Ptr<int>>();
0344       int item_other = *ref;
0345       assert(item_other == product_item);
0346 
0347       edm::Ref<std::vector<int>> ref2 = refToBase.castTo<edm::Ref<std::vector<int>>>();
0348       int item_other2 = *ref2;
0349       assert(item_other2 == product_item);
0350 
0351       edm::Ref<sequence_t> ref3(hproduct, slot);
0352       assert(*ref3 == product_item);
0353 
0354       ++i_product;
0355       ++i_view;
0356       ++slot;
0357     }
0358   }
0359 
0360   void ViewAnalyzer::testRefToBaseVector(Event const& e, std::string const& moduleLabel) const {
0361     typedef RefToBaseVector<int> sequence_t;
0362     typedef int value_t;
0363     typedef View<value_t> view_t;
0364 
0365     Handle<sequence_t> hproduct;
0366     e.getByLabel(moduleLabel, hproduct);
0367     assert(hproduct.isValid());
0368 
0369     Handle<view_t> hview;
0370     e.getByLabel(moduleLabel, hview);
0371     assert(hview.isValid());
0372 
0373     assert(hproduct.id() == hview.id());
0374     assert(*hproduct.provenance() == *hview.provenance());
0375 
0376     assert(hproduct->size() == hview->size());
0377 
0378     sequence_t::const_iterator i_product = hproduct->begin();
0379     sequence_t::const_iterator e_product = hproduct->end();
0380     view_t::const_iterator i_view = hview->begin();
0381     view_t::const_iterator e_view = hview->end();
0382     while (i_product != e_product && i_view != e_view) {
0383       value_t const& product_item = **i_product;
0384       value_t const& view_item = *i_view;
0385       assert(product_item == view_item);
0386       ++i_product;
0387       ++i_view;
0388     }
0389   }
0390 
0391   void ViewAnalyzer::testPtrVector(Event const& e, std::string const& moduleLabel) const {
0392     typedef PtrVector<int> sequence_t;
0393     typedef int value_t;
0394     typedef View<value_t> view_t;
0395 
0396     Handle<sequence_t> hproduct;
0397     e.getByLabel(moduleLabel, hproduct);
0398     assert(hproduct.isValid());
0399 
0400     Handle<view_t> hview;
0401 
0402     InputTag tag(moduleLabel + "doesNotExist");
0403     e.getByLabel(tag, hview);
0404     assert(!hview.isValid());
0405 
0406     e.getByLabel(moduleLabel + "doesNotExist", hview);
0407     assert(!hview.isValid());
0408 
0409     InputTag tag2(moduleLabel);
0410     e.getByLabel(tag2, hview);
0411     assert(hview.isValid());
0412 
0413     assert(hproduct.id() == hview.id());
0414     assert(*hproduct.provenance() == *hview.provenance());
0415 
0416     assert(hproduct->size() == hview->size());
0417 
0418     sequence_t::const_iterator i_product = hproduct->begin();
0419     sequence_t::const_iterator e_product = hproduct->end();
0420     view_t::const_iterator i_view = hview->begin();
0421     view_t::const_iterator e_view = hview->end();
0422     while (i_product != e_product && i_view != e_view) {
0423       value_t const& product_item = **i_product;
0424       value_t const& view_item = *i_view;
0425       assert(product_item == view_item);
0426       ++i_product;
0427       ++i_view;
0428     }
0429   }
0430 }  // namespace edmtest
0431 
0432 namespace {
0433   template <typename PtrT>
0434   struct ValueType {
0435     using type = typename PtrT::element_type;
0436   };
0437 
0438   template <typename T>
0439   struct ValueType<edm::Ptr<T>> {
0440     using type = T;
0441   };
0442 
0443   template <typename Ptr>
0444   void testStdVectorPtrT(Event const& e, std::string const& moduleLabel) {
0445     using sequence_t = std::vector<Ptr>;
0446     using value_t = typename ValueType<Ptr>::type;
0447     using view_t = View<value_t>;
0448 
0449     Handle<sequence_t> hproduct;
0450     e.getByLabel(moduleLabel, hproduct);
0451     assert(hproduct.isValid());
0452 
0453     Handle<view_t> hview;
0454 
0455     InputTag tag(moduleLabel);
0456     e.getByLabel(tag, hview);
0457     assert(hview.isValid());
0458 
0459     assert(hproduct.id() == hview.id());
0460     assert(*hproduct.provenance() == *hview.provenance());
0461 
0462     assert(hproduct->size() == hview->size());
0463 
0464     typename sequence_t::const_iterator i_product = hproduct->begin();
0465     typename sequence_t::const_iterator e_product = hproduct->end();
0466     typename view_t::const_iterator i_view = hview->begin();
0467     typename view_t::const_iterator e_view = hview->end();
0468     unsigned int slot = 0;
0469     while (i_product != e_product && i_view != e_view) {
0470       value_t const& product_item = **i_product;
0471       value_t const& view_item = *i_view;
0472       assert(product_item == view_item);
0473 
0474       edm::Ref<sequence_t> ref3(hproduct, slot);
0475       assert(**ref3 == product_item);
0476 
0477       ++i_product;
0478       ++i_view;
0479       ++slot;
0480     }
0481   }
0482 }  // namespace
0483 
0484 namespace edmtest {
0485   void ViewAnalyzer::testStdVectorPtr(Event const& e, std::string const& moduleLabel) const {
0486     testStdVectorPtrT<edm::Ptr<int>>(e, moduleLabel);
0487   }
0488 
0489   void ViewAnalyzer::testStdVectorUniquePtr(Event const& e, std::string const& moduleLabel) const {
0490     testStdVectorPtrT<std::unique_ptr<int>>(e, moduleLabel);
0491     testStdVectorPtrT<std::unique_ptr<IntProduct>>(e, moduleLabel);
0492   }
0493 
0494 }  // namespace edmtest
0495 
0496 using edmtest::ViewAnalyzer;
0497 DEFINE_FWK_MODULE(ViewAnalyzer);