Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DataFormats/Common/interface/Handle.h"
0002 #include "DataFormats/TestObjects/interface/Thing.h"
0003 #include "DataFormats/TestObjects/interface/ThingCollection.h"
0004 #include "DataFormats/TestObjects/interface/TrackOfThings.h"
0005 #include "FWCore/Framework/interface/ConsumesCollector.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/stream/ThinningProducer.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/Utilities/interface/EDGetToken.h"
0012 #include "FWCore/Utilities/interface/Exception.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 #include "FWCore/Framework/interface/ESHandle.h"
0015 #include "FWCore/Integration/test/WhatsIt.h"
0016 #include "FWCore/Integration/test/GadgetRcd.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0018 
0019 #include <set>
0020 #include <string>
0021 
0022 namespace edmtest {
0023 
0024   class SlimmingThingSelector {
0025   public:
0026     SlimmingThingSelector(edm::ParameterSet const& pset, edm::ConsumesCollector&& cc);
0027 
0028     static void fillPSetDescription(edm::ParameterSetDescription& desc);
0029 
0030     void preChoose(edm::Handle<edmtest::ThingCollection> tc, edm::Event const& event, edm::EventSetup const& es);
0031 
0032     std::optional<edmtest::Thing> choose(unsigned int iIndex, edmtest::Thing const& iItem) const;
0033 
0034     void reset() { keysToSave_.clear(); }
0035 
0036   private:
0037     edm::EDGetTokenT<TrackOfThingsCollection> const trackToken_;
0038     edm::ESGetToken<edmtest::WhatsIt, GadgetRcd> const setupToken_;
0039     std::set<unsigned int> keysToSave_;
0040     unsigned int const offsetToThinnedKey_;
0041     unsigned int const offsetToValue_;
0042     unsigned int const expectedCollectionSize_;
0043     int const slimmedValueFactor_;
0044   };
0045 
0046   SlimmingThingSelector::SlimmingThingSelector(edm::ParameterSet const& pset, edm::ConsumesCollector&& cc)
0047       : trackToken_(cc.consumes<TrackOfThingsCollection>(pset.getParameter<edm::InputTag>("trackTag"))),
0048         setupToken_(cc.esConsumes<edmtest::WhatsIt, GadgetRcd>()),
0049         offsetToThinnedKey_(pset.getParameter<unsigned int>("offsetToThinnedKey")),
0050         offsetToValue_(pset.getParameter<unsigned int>("offsetToValue")),
0051         expectedCollectionSize_(pset.getParameter<unsigned int>("expectedCollectionSize")),
0052         slimmedValueFactor_(pset.getParameter<int>("slimmedValueFactor")) {}
0053 
0054   void SlimmingThingSelector::fillPSetDescription(edm::ParameterSetDescription& desc) {
0055     desc.add<edm::InputTag>("trackTag");
0056     desc.add<unsigned int>("offsetToThinnedKey");
0057     desc.add<unsigned int>("offsetToValue", 0);
0058     desc.add<unsigned int>("expectedCollectionSize");
0059     desc.add<int>("slimmedValueFactor", 1);
0060   }
0061 
0062   void SlimmingThingSelector::preChoose(edm::Handle<edmtest::ThingCollection> tc,
0063                                         edm::Event const& event,
0064                                         edm::EventSetup const& es) {
0065     for (auto const& track : event.get(trackToken_)) {
0066       keysToSave_.insert(track.ref1.key() - offsetToThinnedKey_);
0067       keysToSave_.insert(track.ref2.key() - offsetToThinnedKey_);
0068       keysToSave_.insert(track.ptr1.key() - offsetToThinnedKey_);
0069       keysToSave_.insert(track.ptr2.key() - offsetToThinnedKey_);
0070     }
0071 
0072     // Just checking to see if the collection got passed in. Not really using it for anything.
0073     if (tc->size() != expectedCollectionSize_) {
0074       throw cms::Exception("TestFailure") << "SlimmingThingSelector::preChoose, collection size = " << tc->size()
0075                                           << " expected size = " << expectedCollectionSize_;
0076     }
0077 
0078     // Just checking to see the EventSetup works from here. Not really using it for anything.
0079     edm::ESHandle<edmtest::WhatsIt> pSetup = es.getHandle(setupToken_);
0080     pSetup.isValid();
0081   }
0082 
0083   std::optional<edmtest::Thing> SlimmingThingSelector::choose(unsigned int iIndex, edmtest::Thing const& iItem) const {
0084     // Just checking to see the element in the container got passed in OK. Not really using it.
0085     // Just using %10 because it coincidentally works with the arbitrary numbers I picked, no meaning really.
0086     auto const expected = slimmedValueFactor_ * (iIndex + offsetToValue_);
0087     if (static_cast<unsigned>(iItem.a % 10) != static_cast<unsigned>(expected % 10)) {
0088       throw cms::Exception("TestFailure") << "SlimmingThingSelector::choose, item content = " << iItem.a
0089                                           << " index = " << iIndex << " expected " << expected;
0090     }
0091 
0092     // Save the Things referenced by the Tracks
0093     if (keysToSave_.find(iIndex) == keysToSave_.end())
0094       return {};
0095     auto copy = iItem;
0096     copy.a *= 10;
0097     return copy;
0098   }
0099 }  // namespace edmtest
0100 
0101 typedef edm::ThinningProducer<edmtest::ThingCollection, edmtest::SlimmingThingSelector> SlimmingThingProducer;
0102 DEFINE_FWK_MODULE(SlimmingThingProducer);