CMS 3D CMS Logo

ThinnedRefFromTestAnalyzer.cc
Go to the documentation of this file.
2 
12 #include "DataFormats/TestObjects/interface/Thing.h"
13 #include "DataFormats/TestObjects/interface/ThingCollection.h"
14 #include "DataFormats/TestObjects/interface/TrackOfThings.h"
16 
17 namespace {
18  template <typename F>
19  void requireExceptionCategory(edm::errors::ErrorCodes code, F&& function) {
20  bool threwException = false;
21  try {
22  function();
23  } catch (edm::Exception& ex) {
24  if (ex.categoryCode() != code) {
25  throw cms::Exception("TestFailure")
26  << "Got edm::Exception with category code " << ex.categoryCode() << " expected " << code << " message:\n"
27  << ex.explainSelf();
28  }
29  threwException = true;
30  }
31  if (not threwException) {
32  throw cms::Exception("TestFailure") << "Expected edm::Exception, but was not thrown";
33  }
34  }
35 } // namespace
36 
37 namespace edmtest {
39  public:
41 
42  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
43 
44  void analyze(edm::StreamID streamID, edm::Event const& e, edm::EventSetup const& c) const override;
45 
46  private:
51  };
52 
54  : parentToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("parentTag"))},
55  thinnedToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("thinnedTag"))},
56  unrelatedToken_{consumes<ThingCollection>(pset.getParameter<edm::InputTag>("unrelatedTag"))},
57  trackToken_{consumes<TrackOfThingsCollection>(pset.getParameter<edm::InputTag>("trackTag"))} {}
58 
61  desc.add<edm::InputTag>("parentTag");
62  desc.add<edm::InputTag>("thinnedTag");
63  desc.add<edm::InputTag>("unrelatedTag");
64  desc.add<edm::InputTag>("trackTag");
65  descriptions.addDefault(desc);
66  }
67 
69  edm::Event const& event,
70  edm::EventSetup const& c) const {
71  auto parentHandle = event.getHandle(parentToken_);
72  auto thinnedHandle = event.getHandle(thinnedToken_);
73  auto unrelatedHandle = event.getHandle(unrelatedToken_);
74  auto const& trackCollection = event.get(trackToken_);
75 
76  edm::RefProd parentRefProd{parentHandle};
77  edm::RefProd thinnedRefProd{thinnedHandle};
78  edm::RefProd unrelatedRefProd{unrelatedHandle};
79 
80  requireExceptionCategory(edm::errors::InvalidReference, [&]() {
81  auto invalidParentRef = edm::thinnedRefFrom(edm::Ref(unrelatedHandle, 0), thinnedRefProd, event.productGetter());
82  });
83  if (auto invalidParentRef =
84  edm::tryThinnedRefFrom(edm::Ref(unrelatedHandle, 0), thinnedRefProd, event.productGetter());
85  invalidParentRef.isNonnull()) {
86  throw cms::Exception("TestFailure") << "Expected to get Null Ref when passing in a Ref to unrelated parent "
87  "collection, got a non-null Ref instead";
88  }
89 
90  for (auto const& track : trackCollection) {
91  auto parentRef1 = edm::thinnedRefFrom(track.ref1, parentRefProd, event.productGetter());
92  if (parentRef1.id() != track.ref1.id()) {
93  throw cms::Exception("TestFailure")
94  << "Ref1-to-parent ProductID " << parentRef1.id() << " expected " << track.ref1.id();
95  }
96  if (parentRef1.key() != track.ref1.key()) {
97  throw cms::Exception("TestFailure")
98  << "Ref1-to-parent key " << parentRef1.key() << " expected " << track.ref1.key();
99  }
100 
101  auto thinnedRef1 = edm::thinnedRefFrom(track.ref1, thinnedRefProd, event.productGetter());
102  if (thinnedRef1.id() != thinnedRefProd.id()) {
103  throw cms::Exception("TestFailure")
104  << "Ref1-to-thinned ProductID " << thinnedRef1.id() << " expected " << thinnedRefProd.id();
105  }
106 
107  requireExceptionCategory(edm::errors::InvalidReference, [&]() {
108  auto invalidThinnedRef = edm::thinnedRefFrom(track.ref1, unrelatedRefProd, event.productGetter());
109  });
110  if (auto invalidThinnedRef = edm::tryThinnedRefFrom(track.ref1, unrelatedRefProd, event.productGetter());
111  invalidThinnedRef.isNonnull()) {
112  throw cms::Exception("TestFailure") << "Expected to get Null Ref when passing in a RefProd to unrelated "
113  "thinned collection, got a non-null Ref instead";
114  }
115  }
116  }
117 } // namespace edmtest
118 
virtual std::string explainSelf() const
Definition: Exception.cc:108
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Code categoryCode() const
Definition: EDMException.h:99
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ref.h:247
ThinnedRefFromTestAnalyzer(edm::ParameterSet const &pset)
const edm::EDGetTokenT< ThingCollection > thinnedToken_
const edm::EDGetTokenT< ThingCollection > unrelatedToken_
void addDefault(ParameterSetDescription const &psetDescription)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
trackCollection
Definition: JetHT_cfg.py:51
Ref< C, T, F > thinnedRefFrom(Ref< C, T, F > const &parent, RefProd< C > const &thinned, edm::EDProductGetter const &prodGetter)
Return a Ref to thinned collection corresponding to an element of the Ref to parent collection...
Definition: RefItemGet.h:129
Ref< C, T, F > tryThinnedRefFrom(Ref< C, T, F > const &parent, RefProd< C > const &thinned, edm::EDProductGetter const &prodGetter)
Return a Ref to thinned collection corresponding to an element of the Ref to parent collection...
Definition: RefItemGet.h:157
const edm::EDGetTokenT< ThingCollection > parentToken_
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
const edm::EDGetTokenT< TrackOfThingsCollection > trackToken_
void analyze(edm::StreamID streamID, edm::Event const &e, edm::EventSetup const &c) const override
Definition: event.py:1