CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Attributes
CandViewRefRandomSelector Class Reference
Inheritance diagram for CandViewRefRandomSelector:
edm::EDFilter edm::ProducerBase edm::EDConsumerBase edm::ProductRegistryHelper

Public Member Functions

 CandViewRefRandomSelector (const edm::ParameterSet &pset)
 
bool filter (edm::Event &, const edm::EventSetup &) override
 
- Public Member Functions inherited from edm::EDFilter
 EDFilter ()
 
ModuleDescription const & moduleDescription () const
 
virtual ~EDFilter ()
 
- Public Member Functions inherited from edm::ProducerBase
 ProducerBase ()
 
void registerProducts (ProducerBase *, ProductRegistry *, ModuleDescription const &)
 
std::function< void(BranchDescription
const &)> 
registrationCallback () const
 used by the fwk to register list of products More...
 
virtual ~ProducerBase ()
 
- Public Member Functions inherited from edm::EDConsumerBase
 EDConsumerBase ()
 
ProductHolderIndexAndSkipBit indexFrom (EDGetToken, BranchType, TypeID const &) const
 
void itemsMayGet (BranchType, std::vector< ProductHolderIndexAndSkipBit > &) const
 
void itemsToGet (BranchType, std::vector< ProductHolderIndexAndSkipBit > &) const
 
std::vector
< ProductHolderIndexAndSkipBit >
const & 
itemsToGetFromEvent () const
 
void labelsForToken (EDGetToken iToken, Labels &oLabels) const
 
bool registeredToConsume (ProductHolderIndex, bool, BranchType) const
 
bool registeredToConsumeMany (TypeID const &, BranchType) const
 
void updateLookup (BranchType iBranchType, ProductHolderIndexHelper const &)
 
virtual ~EDConsumerBase ()
 

Private Attributes

unsigned int choose_
 
bool filter_
 
TRandom3 randy_
 
unsigned int seed_
 
edm::InputTag src_
 

Additional Inherited Members

- Public Types inherited from edm::EDFilter
typedef EDFilter ModuleType
 
- Public Types inherited from edm::ProducerBase
typedef
ProductRegistryHelper::TypeLabelList 
TypeLabelList
 
- Static Public Member Functions inherited from edm::EDFilter
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
static void prevalidate (ConfigurationDescriptions &)
 
- Protected Member Functions inherited from edm::ProducerBase
void callWhenNewProductsRegistered (std::function< void(BranchDescription const &)> const &func)
 
- Protected Member Functions inherited from edm::EDConsumerBase
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > consumes (edm::InputTag const &tag)
 
EDGetToken consumes (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken consumes (TypeToGet const &id, edm::InputTag const &tag)
 
ConsumesCollector consumesCollector ()
 Use a ConsumesCollector to gather consumes information from helper functions. More...
 
template<typename ProductType , BranchType B = InEvent>
void consumesMany ()
 
void consumesMany (const TypeToGet &id)
 
template<BranchType B>
void consumesMany (const TypeToGet &id)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > mayConsume (edm::InputTag const &tag)
 
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 

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_.

38  {
39  src_ = pset.getParameter<edm::InputTag>("src");
40  choose_ = pset.getParameter<unsigned int>("choose");
41  filter_ = pset.getParameter<bool>("filter");
42  seed_ = pset.exists("seed") ? pset.getParameter<unsigned int>("seed") : 123;
43  randy_ = TRandom3(seed_);
44  produces<reco::CandidateBaseRefVector>();
45 }
T getParameter(std::string const &) const
bool exists(std::string const &parameterName) const
checks if a parameter exists

Member Function Documentation

bool CandViewRefRandomSelector::filter ( edm::Event evt,
const edm::EventSetup es 
)
overridevirtual

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_.

48  {
50  evt.getByLabel(src_, cands);
51  std::auto_ptr<reco::CandidateBaseRefVector> output(
52  new reco::CandidateBaseRefVector(cands));
53  // If we don't have enough elements to select, just copy what we have
54  const reco::CandidateBaseRefVector &inputRefs = cands->refVector();
55  if (inputRefs.size() <= choose_) {
56  *output = inputRefs;
57  } else {
58  for (size_t i = 0; i < inputRefs.size() && output->size() < choose_; ++i) {
59  // based on http://stackoverflow.com/questions/48087/
60  // select-a-random-n-elements-from-listt-in-c/48089#48089
61  double selectionProb =
62  (choose_ - output->size())*1.0/(inputRefs.size() - i);
63  // throw a number to see if we select this element
64  if (randy_.Rndm() < selectionProb)
65  output->push_back(inputRefs[i]);
66  }
67  }
68  size_t outputSize = output->size();
69  evt.put(output);
70  return ( !filter_ || outputSize );
71 }
int i
Definition: DBlmapReader.cc:9
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
size_type size() const
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390

Member Data Documentation

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