File indexing completed on 2024-04-06 12:12:37
0001 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0002 #include "DataFormats/Common/interface/Handle.h"
0003 #include "DataFormats/Common/interface/ThinnedRefSet.h"
0004 #include "DataFormats/TestObjects/interface/Thing.h"
0005 #include "DataFormats/TestObjects/interface/TrackOfDSVThings.h"
0006 #include "FWCore/Framework/interface/ConsumesCollector.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/EventSetup.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 #include "FWCore/Framework/interface/stream/ThinningProducer.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/Utilities/interface/EDGetToken.h"
0013 #include "FWCore/Utilities/interface/Exception.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0016
0017 #include <string>
0018
0019 namespace edmtest {
0020
0021 class ThinningDSVThingSelector {
0022 public:
0023 ThinningDSVThingSelector(edm::ParameterSet const& pset, edm::ConsumesCollector&& cc);
0024
0025 static void fillPSetDescription(edm::ParameterSetDescription& desc);
0026
0027 void preChoose(edm::Handle<edmNew::DetSetVector<Thing>> tc, edm::Event const& event, edm::EventSetup const& es);
0028
0029 bool choose(unsigned int iIndex, edmtest::Thing const& iItem) const;
0030
0031 void reset() { keysToSave_.clear(); }
0032
0033 private:
0034 edm::EDGetTokenT<TrackOfDSVThingsCollection> const trackToken_;
0035 edm::ThinnedRefSet<edmNew::DetSetVector<Thing>> keysToSave_;
0036 unsigned int const offsetToValue_;
0037 unsigned int const expectedDetSets_;
0038 unsigned int const expectedDetSetSize_;
0039 int const slimmedValueFactor_;
0040 };
0041
0042 ThinningDSVThingSelector::ThinningDSVThingSelector(edm::ParameterSet const& pset, edm::ConsumesCollector&& cc)
0043 : trackToken_(cc.consumes<TrackOfDSVThingsCollection>(pset.getParameter<edm::InputTag>("trackTag"))),
0044 keysToSave_(pset.getParameter<bool>("thinnedRefSetIgnoreInvalidParentRef")
0045 ? edm::ThinnedRefSetMode::ignoreInvalidParentRef
0046 : edm::ThinnedRefSetMode::throwOnInvalidParentRef),
0047 offsetToValue_(pset.getParameter<unsigned int>("offsetToValue")),
0048 expectedDetSets_(pset.getParameter<unsigned int>("expectedDetSets")),
0049 expectedDetSetSize_(pset.getParameter<unsigned int>("expectedDetSetSize")),
0050 slimmedValueFactor_(pset.getParameter<int>("slimmedValueFactor")) {}
0051
0052 void ThinningDSVThingSelector::fillPSetDescription(edm::ParameterSetDescription& desc) {
0053 desc.add<edm::InputTag>("trackTag");
0054 desc.add<unsigned int>("offsetToValue", 0);
0055 desc.add<unsigned int>("expectedDetSets");
0056 desc.add<unsigned int>("expectedDetSetSize");
0057 desc.add<int>("slimmedValueFactor", 1);
0058 desc.add<bool>("thinnedRefSetIgnoreInvalidParentRef", false);
0059 }
0060
0061 void ThinningDSVThingSelector::preChoose(edm::Handle<edmNew::DetSetVector<Thing>> tc,
0062 edm::Event const& event,
0063 edm::EventSetup const& es) {
0064 auto filler = keysToSave_.fill(edm::RefProd(tc), event.productGetter());
0065 for (auto const& track : event.get(trackToken_)) {
0066 filler.insert(track.ref1);
0067 filler.insert(track.ref2);
0068 }
0069
0070
0071 if (tc->size() != expectedDetSets_) {
0072 throw cms::Exception("TestFailure") << "ThinningDSVThingSelector::preChoose, number of DetSets = " << tc->size()
0073 << " expected = " << expectedDetSets_;
0074 }
0075 for (auto const& ds : *tc) {
0076 if (ds.size() != expectedDetSetSize_) {
0077 throw cms::Exception("TestFailure")
0078 << "ThinningDSVThingSelector::preChoose, number of elements in DetSet with id " << ds.id() << " = "
0079 << ds.size() << " expected = " << expectedDetSetSize_;
0080 }
0081 }
0082 }
0083
0084 bool ThinningDSVThingSelector::choose(unsigned int iIndex, edmtest::Thing const& iItem) const {
0085
0086
0087 auto const expected = slimmedValueFactor_ * (iIndex + offsetToValue_);
0088 if (static_cast<unsigned>(iItem.a % 10) != static_cast<unsigned>(expected % 10)) {
0089 throw cms::Exception("TestFailure") << "ThinningDSVThingSelector::choose, item content = " << iItem.a
0090 << " index = " << iIndex << " expected " << expected;
0091 }
0092
0093
0094 return keysToSave_.contains(iIndex);
0095 }
0096 }
0097
0098 using ThinningDSVThingProducer =
0099 edm::ThinningProducer<edmNew::DetSetVector<edmtest::Thing>, edmtest::ThinningDSVThingSelector>;
0100 DEFINE_FWK_MODULE(ThinningDSVThingProducer);