ThinnedRefFromTestAnalyzer

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#include "FWCore/Framework/interface/global/EDAnalyzer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "DataFormats/TestObjects/interface/Thing.h"
#include "DataFormats/TestObjects/interface/ThingCollection.h"
#include "DataFormats/TestObjects/interface/TrackOfThings.h"
#include "DataFormats/Provenance/interface/ProductID.h"

namespace {
  template <typename F>
  void requireExceptionCategory(edm::errors::ErrorCodes code, F&& function) {
    bool threwException = false;
    try {
      function();
    } catch (edm::Exception& ex) {
      if (ex.categoryCode() != code) {
        throw cms::Exception("TestFailure")
            << "Got edm::Exception with category code " << ex.categoryCode() << " expected " << code << " message:\n"
            << ex.explainSelf();
      }
      threwException = true;
    }
    if (not threwException) {
      throw cms::Exception("TestFailure") << "Expected edm::Exception, but was not thrown";
    }
  }
}  // namespace

namespace edmtest {
  class ThinnedRefFromTestAnalyzer : public edm::global::EDAnalyzer<> {
  public:
    explicit ThinnedRefFromTestAnalyzer(edm::ParameterSet const& pset);

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

    void analyze(edm::StreamID streamID, edm::Event const& e, edm::EventSetup const& c) const override;

  private:
    const edm::EDGetTokenT<ThingCollection> parentToken_;
    const edm::EDGetTokenT<ThingCollection> thinnedToken_;
    const edm::EDGetTokenT<ThingCollection> unrelatedToken_;
    const edm::EDGetTokenT<TrackOfThingsCollection> trackToken_;
  };

  ThinnedRefFromTestAnalyzer::ThinnedRefFromTestAnalyzer(edm::ParameterSet const& pset)
      : parentToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("parentTag"))},
        thinnedToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("thinnedTag"))},
        unrelatedToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("unrelatedTag"))},
        trackToken_{consumes<TrackOfThingsCollection>(pset.getParameter<edm::InputTag>("trackTag"))} {}

  void ThinnedRefFromTestAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
    edm::ParameterSetDescription desc;
    desc.add<edm::InputTag>("parentTag");
    desc.add<edm::InputTag>("thinnedTag");
    desc.add<edm::InputTag>("unrelatedTag");
    desc.add<edm::InputTag>("trackTag");
    descriptions.addDefault(desc);
  }

  void ThinnedRefFromTestAnalyzer::analyze(edm::StreamID streamID,
                                           edm::Event const& event,
                                           edm::EventSetup const& c) const {
    auto parentHandle = event.getHandle(parentToken_);
    auto thinnedHandle = event.getHandle(thinnedToken_);
    auto unrelatedHandle = event.getHandle(unrelatedToken_);
    auto const& trackCollection = event.get(trackToken_);

    edm::RefProd parentRefProd{parentHandle};
    edm::RefProd thinnedRefProd{thinnedHandle};
    edm::RefProd unrelatedRefProd{unrelatedHandle};

    requireExceptionCategory(edm::errors::InvalidReference, [&]() {
      auto invalidParentRef = edm::thinnedRefFrom(edm::Ref(unrelatedHandle, 0), thinnedRefProd, event.productGetter());
    });
    if (auto invalidParentRef =
            edm::tryThinnedRefFrom(edm::Ref(unrelatedHandle, 0), thinnedRefProd, event.productGetter());
        invalidParentRef.isNonnull()) {
      throw cms::Exception("TestFailure") << "Expected to get Null Ref when passing in a Ref to unrelated parent "
                                             "collection, got a non-null Ref instead";
    }

    for (auto const& track : trackCollection) {
      auto parentRef1 = edm::thinnedRefFrom(track.ref1, parentRefProd, event.productGetter());
      if (parentRef1.id() != track.ref1.id()) {
        throw cms::Exception("TestFailure")
            << "Ref1-to-parent ProductID " << parentRef1.id() << " expected " << track.ref1.id();
      }
      if (parentRef1.key() != track.ref1.key()) {
        throw cms::Exception("TestFailure")
            << "Ref1-to-parent key " << parentRef1.key() << " expected " << track.ref1.key();
      }

      auto thinnedRef1 = edm::thinnedRefFrom(track.ref1, thinnedRefProd, event.productGetter());
      if (thinnedRef1.id() != thinnedRefProd.id()) {
        throw cms::Exception("TestFailure")
            << "Ref1-to-thinned ProductID " << thinnedRef1.id() << " expected " << thinnedRefProd.id();
      }

      requireExceptionCategory(edm::errors::InvalidReference, [&]() {
        auto invalidThinnedRef = edm::thinnedRefFrom(track.ref1, unrelatedRefProd, event.productGetter());
      });
      if (auto invalidThinnedRef = edm::tryThinnedRefFrom(track.ref1, unrelatedRefProd, event.productGetter());
          invalidThinnedRef.isNonnull()) {
        throw cms::Exception("TestFailure") << "Expected to get Null Ref when passing in a RefProd to unrelated "
                                               "thinned collection, got a non-null Ref instead";
      }
    }
  }
}  // namespace edmtest

using edmtest::ThinnedRefFromTestAnalyzer;
DEFINE_FWK_MODULE(ThinnedRefFromTestAnalyzer);