Public Member Functions | |
CandViewRefRandomSelector (const edm::ParameterSet &pset) | |
bool | filter (edm::Event &, const edm::EventSetup &) |
Private Attributes | |
unsigned int | choose_ |
bool | filter_ |
TRandom3 | randy_ |
unsigned int | seed_ |
edm::InputTag | src_ |
Definition at line 25 of file CandViewRefRandomSelector.cc.
CandViewRefRandomSelector::CandViewRefRandomSelector | ( | const edm::ParameterSet & | pset | ) | [explicit] |
Definition at line 37 of file CandViewRefRandomSelector.cc.
References choose_, edm::ParameterSet::exists(), filter_, edm::ParameterSet::getParameter(), randy_, seed_, and src_.
{ src_ = pset.getParameter<edm::InputTag>("src"); choose_ = pset.getParameter<unsigned int>("choose"); filter_ = pset.getParameter<bool>("filter"); seed_ = pset.exists("seed") ? pset.getParameter<unsigned int>("seed") : 123; randy_ = TRandom3(seed_); produces<reco::CandidateBaseRefVector>(); }
bool CandViewRefRandomSelector::filter | ( | edm::Event & | evt, |
const edm::EventSetup & | es | ||
) | [virtual] |
Implements edm::EDFilter.
Definition at line 47 of file CandViewRefRandomSelector.cc.
References choose_, filter_, edm::Event::getByLabel(), i, convertSQLitetoXML_cfg::output, edm::RefToBaseVector< T >::push_back(), edm::Event::put(), randy_, edm::RefToBaseVector< T >::size(), and src_.
{ edm::Handle<edm::View<reco::Candidate> > cands; evt.getByLabel(src_, cands); std::auto_ptr<reco::CandidateBaseRefVector> output( new reco::CandidateBaseRefVector(cands)); // If we don't have enough elements to select, just copy what we have const reco::CandidateBaseRefVector &inputRefs = cands->refVector(); if (inputRefs.size() <= choose_) { *output = inputRefs; } else { for (size_t i = 0; i < inputRefs.size() && output->size() < choose_; ++i) { // based on https://stackoverflow.com/questions/48087/ // select-a-random-n-elements-from-listt-in-c/48089#48089 double selectionProb = (choose_ - output->size())*1.0/(inputRefs.size() - i); // throw a number to see if we select this element if (randy_.Rndm() < selectionProb) output->push_back(inputRefs[i]); } } size_t outputSize = output->size(); evt.put(output); return ( !filter_ || outputSize ); }
unsigned int CandViewRefRandomSelector::choose_ [private] |
Definition at line 31 of file CandViewRefRandomSelector.cc.
Referenced by CandViewRefRandomSelector(), and filter().
bool CandViewRefRandomSelector::filter_ [private] |
Definition at line 33 of file CandViewRefRandomSelector.cc.
Referenced by CandViewRefRandomSelector(), and filter().
TRandom3 CandViewRefRandomSelector::randy_ [private] |
Definition at line 34 of file CandViewRefRandomSelector.cc.
Referenced by CandViewRefRandomSelector(), and filter().
unsigned int CandViewRefRandomSelector::seed_ [private] |
Definition at line 32 of file CandViewRefRandomSelector.cc.
Referenced by CandViewRefRandomSelector().
edm::InputTag CandViewRefRandomSelector::src_ [private] |
Definition at line 30 of file CandViewRefRandomSelector.cc.
Referenced by CandViewRefRandomSelector(), and filter().