CMS 3D CMS Logo

Public Member Functions | Private Attributes

CandViewRefRandomSelector Class Reference

Inheritance diagram for CandViewRefRandomSelector:
edm::EDFilter edm::ProducerBase edm::ProductRegistryHelper

List of all members.

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_

Detailed Description

Definition at line 25 of file CandViewRefRandomSelector.cc.


Constructor & Destructor Documentation

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>();
}

Member Function Documentation

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 http://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 );
}

Member Data Documentation

unsigned int CandViewRefRandomSelector::choose_ [private]

Definition at line 31 of file CandViewRefRandomSelector.cc.

Referenced by CandViewRefRandomSelector(), and filter().

Definition at line 33 of file CandViewRefRandomSelector.cc.

Referenced by CandViewRefRandomSelector(), and filter().

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().

Definition at line 30 of file CandViewRefRandomSelector.cc.

Referenced by CandViewRefRandomSelector(), and filter().