Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:37

0001 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0002 
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/EDGetToken.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0012 #include "DataFormats/TestObjects/interface/Thing.h"
0013 #include "DataFormats/TestObjects/interface/ThingCollection.h"
0014 #include "DataFormats/TestObjects/interface/TrackOfThings.h"
0015 #include "DataFormats/Provenance/interface/ProductID.h"
0016 
0017 namespace {
0018   template <typename F>
0019   void requireExceptionCategory(edm::errors::ErrorCodes code, F&& function) {
0020     bool threwException = false;
0021     try {
0022       function();
0023     } catch (edm::Exception& ex) {
0024       if (ex.categoryCode() != code) {
0025         throw cms::Exception("TestFailure")
0026             << "Got edm::Exception with category code " << ex.categoryCode() << " expected " << code << " message:\n"
0027             << ex.explainSelf();
0028       }
0029       threwException = true;
0030     }
0031     if (not threwException) {
0032       throw cms::Exception("TestFailure") << "Expected edm::Exception, but was not thrown";
0033     }
0034   }
0035 }  // namespace
0036 
0037 namespace edmtest {
0038   class ThinnedRefFromTestAnalyzer : public edm::global::EDAnalyzer<> {
0039   public:
0040     explicit ThinnedRefFromTestAnalyzer(edm::ParameterSet const& pset);
0041 
0042     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0043 
0044     void analyze(edm::StreamID streamID, edm::Event const& e, edm::EventSetup const& c) const override;
0045 
0046   private:
0047     const edm::EDGetTokenT<ThingCollection> parentToken_;
0048     const edm::EDGetTokenT<ThingCollection> thinnedToken_;
0049     const edm::EDGetTokenT<ThingCollection> unrelatedToken_;
0050     const edm::EDGetTokenT<TrackOfThingsCollection> trackToken_;
0051   };
0052 
0053   ThinnedRefFromTestAnalyzer::ThinnedRefFromTestAnalyzer(edm::ParameterSet const& pset)
0054       : parentToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("parentTag"))},
0055         thinnedToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("thinnedTag"))},
0056         unrelatedToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("unrelatedTag"))},
0057         trackToken_{consumes<TrackOfThingsCollection>(pset.getParameter<edm::InputTag>("trackTag"))} {}
0058 
0059   void ThinnedRefFromTestAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0060     edm::ParameterSetDescription desc;
0061     desc.add<edm::InputTag>("parentTag");
0062     desc.add<edm::InputTag>("thinnedTag");
0063     desc.add<edm::InputTag>("unrelatedTag");
0064     desc.add<edm::InputTag>("trackTag");
0065     descriptions.addDefault(desc);
0066   }
0067 
0068   void ThinnedRefFromTestAnalyzer::analyze(edm::StreamID streamID,
0069                                            edm::Event const& event,
0070                                            edm::EventSetup const& c) const {
0071     auto parentHandle = event.getHandle(parentToken_);
0072     auto thinnedHandle = event.getHandle(thinnedToken_);
0073     auto unrelatedHandle = event.getHandle(unrelatedToken_);
0074     auto const& trackCollection = event.get(trackToken_);
0075 
0076     edm::RefProd parentRefProd{parentHandle};
0077     edm::RefProd thinnedRefProd{thinnedHandle};
0078     edm::RefProd unrelatedRefProd{unrelatedHandle};
0079 
0080     requireExceptionCategory(edm::errors::InvalidReference, [&]() {
0081       auto invalidParentRef = edm::thinnedRefFrom(edm::Ref(unrelatedHandle, 0), thinnedRefProd, event.productGetter());
0082     });
0083     if (auto invalidParentRef =
0084             edm::tryThinnedRefFrom(edm::Ref(unrelatedHandle, 0), thinnedRefProd, event.productGetter());
0085         invalidParentRef.isNonnull()) {
0086       throw cms::Exception("TestFailure") << "Expected to get Null Ref when passing in a Ref to unrelated parent "
0087                                              "collection, got a non-null Ref instead";
0088     }
0089 
0090     for (auto const& track : trackCollection) {
0091       auto parentRef1 = edm::thinnedRefFrom(track.ref1, parentRefProd, event.productGetter());
0092       if (parentRef1.id() != track.ref1.id()) {
0093         throw cms::Exception("TestFailure")
0094             << "Ref1-to-parent ProductID " << parentRef1.id() << " expected " << track.ref1.id();
0095       }
0096       if (parentRef1.key() != track.ref1.key()) {
0097         throw cms::Exception("TestFailure")
0098             << "Ref1-to-parent key " << parentRef1.key() << " expected " << track.ref1.key();
0099       }
0100 
0101       auto thinnedRef1 = edm::thinnedRefFrom(track.ref1, thinnedRefProd, event.productGetter());
0102       if (thinnedRef1.id() != thinnedRefProd.id()) {
0103         throw cms::Exception("TestFailure")
0104             << "Ref1-to-thinned ProductID " << thinnedRef1.id() << " expected " << thinnedRefProd.id();
0105       }
0106 
0107       requireExceptionCategory(edm::errors::InvalidReference, [&]() {
0108         auto invalidThinnedRef = edm::thinnedRefFrom(track.ref1, unrelatedRefProd, event.productGetter());
0109       });
0110       if (auto invalidThinnedRef = edm::tryThinnedRefFrom(track.ref1, unrelatedRefProd, event.productGetter());
0111           invalidThinnedRef.isNonnull()) {
0112         throw cms::Exception("TestFailure") << "Expected to get Null Ref when passing in a RefProd to unrelated "
0113                                                "thinned collection, got a non-null Ref instead";
0114       }
0115     }
0116   }
0117 }  // namespace edmtest
0118 
0119 using edmtest::ThinnedRefFromTestAnalyzer;
0120 DEFINE_FWK_MODULE(ThinnedRefFromTestAnalyzer);