Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:37

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