Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0002 #include "DataFormats/Common/interface/Handle.h"
0003 #include "DataFormats/TestObjects/interface/Thing.h"
0004 #include "DataFormats/TestObjects/interface/TrackOfDSVThings.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/ParameterSet/interface/ParameterSetDescription.h"
0015 
0016 #include <set>
0017 #include <string>
0018 
0019 namespace edmtest {
0020 
0021   class SlimmingDSVThingSelector {
0022   public:
0023     SlimmingDSVThingSelector(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     std::optional<edmtest::Thing> 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     std::set<unsigned int> keysToSave_;
0036     unsigned int const offsetToThinnedKey_;
0037     unsigned int const offsetToValue_;
0038     unsigned int const expectedDetSets_;
0039     unsigned int const expectedDetSetSize_;
0040     int const slimmedValueFactor_;
0041   };
0042 
0043   SlimmingDSVThingSelector::SlimmingDSVThingSelector(edm::ParameterSet const& pset, edm::ConsumesCollector&& cc)
0044       : trackToken_(cc.consumes<TrackOfDSVThingsCollection>(pset.getParameter<edm::InputTag>("trackTag"))),
0045         offsetToThinnedKey_(pset.getParameter<unsigned int>("offsetToThinnedKey")),
0046         offsetToValue_(pset.getParameter<unsigned int>("offsetToValue")),
0047         expectedDetSets_(pset.getParameter<unsigned int>("expectedDetSets")),
0048         expectedDetSetSize_(pset.getParameter<unsigned int>("expectedDetSetSize")),
0049         slimmedValueFactor_(pset.getParameter<int>("slimmedValueFactor")) {}
0050 
0051   void SlimmingDSVThingSelector::fillPSetDescription(edm::ParameterSetDescription& desc) {
0052     desc.add<edm::InputTag>("trackTag");
0053     desc.add<unsigned int>("offsetToThinnedKey");
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   }
0059 
0060   void SlimmingDSVThingSelector::preChoose(edm::Handle<edmNew::DetSetVector<Thing>> tc,
0061                                            edm::Event const& event,
0062                                            edm::EventSetup const& es) {
0063     for (auto const& track : event.get(trackToken_)) {
0064       keysToSave_.insert(track.ref1.key() - offsetToThinnedKey_);
0065       keysToSave_.insert(track.ref2.key() - offsetToThinnedKey_);
0066     }
0067 
0068     // Just checking to see if the collection got passed in. Not really using it for anything.
0069     if (tc->size() != expectedDetSets_) {
0070       throw cms::Exception("TestFailure") << "SlimmingDSVThingSelector::preChoose, number of DetSets = " << tc->size()
0071                                           << " expected = " << expectedDetSets_;
0072     }
0073     for (auto const& ds : *tc) {
0074       if (ds.size() != expectedDetSetSize_) {
0075         throw cms::Exception("TestFailure")
0076             << "SlimmingDSVThingSelector::preChoose, number of elements in DetSet with id " << ds.id() << " = "
0077             << ds.size() << " expected = " << expectedDetSetSize_;
0078       }
0079     }
0080   }
0081 
0082   std::optional<edmtest::Thing> SlimmingDSVThingSelector::choose(unsigned int iIndex,
0083                                                                  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") << "SlimmingDSVThingSelector::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 std::nullopt;
0095     auto copy = iItem;
0096     copy.a *= 10;
0097     return copy;
0098   }
0099 }  // namespace edmtest
0100 
0101 using SlimmingDSVThingProducer =
0102     edm::ThinningProducer<edmNew::DetSetVector<edmtest::Thing>, edmtest::SlimmingDSVThingSelector>;
0103 DEFINE_FWK_MODULE(SlimmingDSVThingProducer);