CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PATSingleVertexSelector.cc
Go to the documentation of this file.
5 
7 
8 #include <algorithm>
9 
11 
12 
14 PATSingleVertexSelector::parseMode(const std::string &mode) {
15  if (mode == "firstVertex") {
16  return First;
17  } else if (mode == "nearestToCandidate") {
18  return NearestToCand;
19  } else if (mode == "fromCandidate") {
20  return FromCand;
21  } else if (mode == "beamSpot") {
22  return FromBeamSpot;
23  } else {
24  throw cms::Exception("Configuration") << "PATSingleVertexSelector: Mode '" << mode << "' not recognized or not supported.\n";
25  }
26 }
27 
28 
30  : doFilterEvents_(false)
31 {
32  using namespace std;
33 
34  modes_.push_back( parseMode(iConfig.getParameter<std::string>("mode")) );
35  if (iConfig.exists("fallbacks")) {
36  vector<string> modes = iConfig.getParameter<vector<string> >("fallbacks");
37  for (vector<string>::const_iterator it = modes.begin(), ed = modes.end(); it != ed; ++it) {
38  modes_.push_back( parseMode(*it) );
39  }
40  }
42  verticesToken_ = consumes<vector<reco::Vertex> >(iConfig.getParameter<edm::InputTag>("vertices"));
43  if (iConfig.existsAs<string>("vertexPreselection")) {
44  string presel = iConfig.getParameter<string>("vertexPreselection");
45  if (!presel.empty()) vtxPreselection_ = auto_ptr<VtxSel>(new VtxSel(presel));
46  }
47  }
49  candidatesToken_ = edm::vector_transform(iConfig.getParameter<vector<edm::InputTag> >("candidates"), [this](edm::InputTag const & tag){return consumes<edm::View<reco::Candidate> >(tag);});
50  if (iConfig.existsAs<string>("candidatePreselection")) {
51  string presel = iConfig.getParameter<string>("candidatePreselection");
52  if (!presel.empty()) candPreselection_ = auto_ptr<CandSel>(new CandSel(presel));
53  }
54  }
55  if (hasMode_(FromBeamSpot)) {
56  beamSpotToken_ = consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"));
57  }
58 
59  if ( iConfig.exists("filter") ) doFilterEvents_ = iConfig.getParameter<bool>("filter");
60 
61  produces<vector<reco::Vertex> >();
62 }
63 
64 
66 {
67 }
68 
70  return (std::find(modes_.begin(), modes_.end(), mode) != modes_.end());
71 }
72 
73 bool
75  using namespace edm;
76  using namespace std;
77 
78  // Clear
79  selVtxs_.clear(); bestCand_ = 0;
80 
81  // Gather data from the Event
82  // -- vertex data --
84  Handle<vector<reco::Vertex> > vertices;
85  iEvent.getByToken(verticesToken_, vertices);
86  for (vector<reco::Vertex>::const_iterator itv = vertices->begin(), edv = vertices->end(); itv != edv; ++itv) {
87  if ((vtxPreselection_.get() != 0) && !((*vtxPreselection_)(*itv)) ) continue;
88  selVtxs_.push_back( &*itv );
89  }
90  }
91  // -- candidate data --
93  vector<pair<double, const reco::Candidate *> > cands;
94  for (vector<edm::EDGetTokenT<edm::View<reco::Candidate> > >::const_iterator itt = candidatesToken_.begin(), edt = candidatesToken_.end(); itt != edt; ++itt) {
95  Handle<View<reco::Candidate> > theseCands;
96  iEvent.getByToken(*itt, theseCands);
97  for (View<reco::Candidate>::const_iterator itc = theseCands->begin(), edc = theseCands->end(); itc != edc; ++itc) {
98  if ((candPreselection_.get() != 0) && !((*candPreselection_)(*itc))) continue;
99  cands.push_back( pair<double, const reco::Candidate *>(-itc->pt(), &*itc) );
100  }
101  }
102  if (!cands.empty()) bestCand_ = cands.front().second;
103  }
104 
105  bool passes = false;
106  auto_ptr<vector<reco::Vertex> > result;
107  // Run main mode + possible fallback modes
108  for (std::vector<Mode>::const_iterator itm = modes_.begin(), endm = modes_.end(); itm != endm; ++itm) {
109  result = filter_(*itm, iEvent, iSetup);
110  // Check if we got any vertices. If so, take them.
111  if (result->size()) {
112  passes = true;
113  break;
114  }
115  }
116  iEvent.put(result);
117  // Check if we want to apply the EDFilter
118  if (doFilterEvents_)
119  return passes;
120  else return true;
121 }
122 
123 std::auto_ptr<std::vector<reco::Vertex> >
125  using namespace edm;
126  using namespace std;
127  std::auto_ptr<std::vector<reco::Vertex> > result(
128  new std::vector<reco::Vertex>());
129  switch(mode) {
130  case First: {
131  if (selVtxs_.empty()) return result;
132  result->push_back(*selVtxs_.front());
133  return result;
134  }
135  case FromCand: {
136  if (bestCand_ == 0) return result;
137  reco::Vertex vtx;
138  if (typeid(*bestCand_) == typeid(reco::VertexCompositeCandidate)) {
141  } else {
142  vtx = reco::Vertex(bestCand_->vertex(), reco::Vertex::Error(), 0, 0, 0);
143  }
144  result->push_back(vtx);
145  return result;
146  }
147  case NearestToCand: {
148  if (selVtxs_.empty() || (bestCand_ == 0)) return result;
149  const reco::Vertex * which = 0;
150  float dzmin = 9999.0;
151  for (vector<const reco::Vertex *>::const_iterator itv = selVtxs_.begin(), edv = selVtxs_.end(); itv != edv; ++itv) {
152  float dz = std::abs((*itv)->z() - bestCand_->vz());
153  if (dz < dzmin) { dzmin = dz; which = *itv; }
154  }
155  if (which != 0) // actually it should not happen, but better safe than sorry
156  result->push_back(*which);
157  return result;
158  }
159  case FromBeamSpot: {
161  iEvent.getByToken(beamSpotToken_, beamSpot);
162  reco::Vertex bs(beamSpot->position(), beamSpot->covariance3D(), 0, 0, 0);
163  result->push_back(bs);
164  return result;
165  }
166  default:
167  // Return an empty vector signifying no vertices found.
168  return result;
169  }
170 }
171 
T getParameter(std::string const &) const
Produces a list containing a single vertex selected by some criteria.
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:184
std::auto_ptr< VtxSel > vtxPreselection_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::auto_ptr< std::vector< reco::Vertex > > filter_(Mode mode, const edm::Event &iEvent, const edm::EventSetup &iSetup)
static Mode parseMode(const std::string &name)
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::EDGetTokenT< std::vector< reco::Vertex > > verticesToken_
math::Error< dimension >::type Error
covariance error matrix (3x3)
Definition: Vertex.h:43
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
std::auto_ptr< CandSel > candPreselection_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
virtual size_type numberOfDaughters() const =0
number of daughters
std::vector< const reco::Vertex * > selVtxs_
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
virtual double vertexChi2() const =0
chi-squares
tuple result
Definition: query.py:137
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual const Point & vertex() const =0
vertex position
std::vector< edm::EDGetTokenT< edm::View< reco::Candidate > > > candidatesToken_
virtual bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
PATSingleVertexSelector(const edm::ParameterSet &iConfig)
StringCutObjectSelector< reco::Vertex > VtxSel
StringCutObjectSelector< reco::Candidate > CandSel
virtual double vz() const =0
z coordinate of vertex position
volatile std::atomic< bool > shutdown_flag false
virtual double vertexCovariance(int i, int j) const =0
(i, j)-th element of error matrix, i, j = 0, ... 2
const reco::Candidate * bestCand_
virtual double vertexNdof() const =0