CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UniqueObjectSelector.cc
Go to the documentation of this file.
2 
3 template <typename T>
5  : cut_(0),
6  rank_(0)
7 {
8  if ( cfg.existsAs<edm::InputTag>("src") ) src_.push_back(cfg.getParameter<edm::InputTag>("src"));
9  else src_ = cfg.getParameter<vInputTag>("src");
10 
11  if ( cfg.exists("cut" ) ) cut_ = new StringCutObjectSelector<T>(cfg.getParameter<std::string>("cut"));
13 
14  filter_ = cfg.getParameter<bool>("filter");
15 
16  produces<ObjectCollection>();
17 }
18 
19 template <typename T>
21 {
22  delete cut_;
23  delete rank_;
24 }
25 
26 namespace
27 {
28  template <typename T>
29  struct objectWithRank
30  {
31  const T* object_;
32  double rank_;
33  };
34 
35  template <typename T>
36  struct higherRankT
37  {
38  bool operator() (const objectWithRank<T>& t1, const objectWithRank<T>& t2)
39  {
40  return (t1.rank_ > t2.rank_);
41  }
42  };
43 }
44 
45 template <typename T>
47 {
48  std::vector<objectWithRank<T> > selectedObjects;
49 
50  // check which objects pass cuts
51  for ( vInputTag::const_iterator src_i = src_.begin();
52  src_i != src_.end(); ++src_i ) {
54  evt.getByLabel(*src_i, objects);
55  for ( typename ObjectCollection::const_iterator object = objects->begin();
56  object != objects->end(); ++object ) {
57  if ( !cut_ || (*cut_)(*object) ) { // either no cut defined or object passes cut
58  objectWithRank<T> selectedObject;
59  selectedObject.object_ = &(*object);
60  selectedObject.rank_ = (*rank_)(*object);
61  selectedObjects.push_back(selectedObject);
62  }
63  }
64  }
65 
66  // sort collection of selected objects by rank
67  higherRankT<T> higherRank;
68  std::sort(selectedObjects.begin(), selectedObjects.end(), higherRank);
69 
70  // store in output collection the object which passes selection and is of highest rank
71  std::auto_ptr<ObjectCollection> objects_output(new ObjectCollection());
72  if ( selectedObjects.size() > 0 ) objects_output->push_back(*selectedObjects.front().object_);
73 
74  evt.put(objects_output);
75 
76  if ( filter_ ) return (objects_output->size() > 0);
77  else return true;
78 }
79 
82 
85 
87 
88 DEFINE_FWK_MODULE(UniquePATMuonSelector);
89 DEFINE_FWK_MODULE(UniqueCompositeCandidateSelector);
90 
T getParameter(std::string const &) const
UniqueObjectSelector< pat::Muon > UniquePATMuonSelector
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:184
bool filter(edm::Event &, const edm::EventSetup &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< edm::InputTag > vInputTag
bool exists(std::string const &parameterName) const
checks if a parameter exists
UniqueObjectSelector(const edm::ParameterSet &)
StringObjectFunction< T > * rank_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
UniqueObjectSelector< reco::CompositeCandidate > UniqueCompositeCandidateSelector
list object
Definition: dbtoconf.py:77
StringCutObjectSelector< T > * cut_
std::vector< T > ObjectCollection
long double T