CMS 3D CMS Logo

PATSingleVertexSelector.cc
Go to the documentation of this file.
1 
23 
24 #include <algorithm>
25 
26 namespace pat {
27 
29  public:
30  explicit PATSingleVertexSelector(const edm::ParameterSet &iConfig);
31  ~PATSingleVertexSelector() override;
32 
33  bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
34 
35  private:
39 
40  Mode parseMode(const std::string &name) const;
41 
42  std::unique_ptr<std::vector<reco::Vertex>> filter_(Mode mode,
43  const edm::Event &iEvent,
44  const edm::EventSetup &iSetup);
45  bool hasMode_(Mode mode) const;
46  // configurables
47  std::vector<Mode> modes_; // mode + optional fallbacks
49  std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>> candidatesToken_;
53  // transient data. meaningful while 'filter()' is on the stack
54  std::vector<reco::VertexRef> selVtxs_;
56 
57  // flag to enable/disable EDFilter functionality:
58  // if set to false, PATSingleVertexSelector selects the "one" event vertex,
59  // but does not reject any events
61  };
62 
63 } // namespace pat
64 
66 
67 PATSingleVertexSelector::Mode PATSingleVertexSelector::parseMode(const std::string &mode) const {
68  if (mode == "firstVertex") {
69  return First;
70  } else if (mode == "nearestToCandidate") {
71  return NearestToCand;
72  } else if (mode == "fromCandidate") {
73  return FromCand;
74  } else if (mode == "beamSpot") {
75  return FromBeamSpot;
76  } else {
77  throw cms::Exception("Configuration")
78  << "PATSingleVertexSelector: Mode '" << mode << "' not recognized or not supported.\n";
79  }
80 }
81 
83  : vtxPreselection_(iConfig.existsAs<std::string>("vertexPreselection")
84  ? iConfig.getParameter<std::string>("vertexPreselection")
85  : std::string(" 1 == 1 ")),
86  candPreselection_(iConfig.existsAs<std::string>("candidatePreselection")
87  ? iConfig.getParameter<std::string>("candidatePreselection")
88  : std::string(" 1 == 1 ")),
89  doFilterEvents_(false) {
90  using namespace std;
91 
92  modes_.push_back(parseMode(iConfig.getParameter<std::string>("mode")));
93  if (iConfig.exists("fallbacks")) {
94  vector<string> modes = iConfig.getParameter<vector<string>>("fallbacks");
95  for (vector<string>::const_iterator it = modes.begin(), ed = modes.end(); it != ed; ++it) {
96  modes_.push_back(parseMode(*it));
97  }
98  }
100  verticesToken_ = consumes<vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vertices"));
101  }
104  edm::vector_transform(iConfig.getParameter<vector<edm::InputTag>>("candidates"),
105  [this](edm::InputTag const &tag) { return consumes<edm::View<reco::Candidate>>(tag); });
106  }
107  if (hasMode_(FromBeamSpot)) {
108  beamSpotToken_ = consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"));
109  }
110 
111  if (iConfig.exists("filter"))
112  doFilterEvents_ = iConfig.getParameter<bool>("filter");
113 
114  produces<vector<reco::Vertex>>();
115 }
116 
118 
120  return (std::find(modes_.begin(), modes_.end(), mode) != modes_.end());
121 }
122 
124  using namespace edm;
125  using namespace std;
126 
127  // Clear
128  selVtxs_.clear();
130 
131  // Gather data from the Event
132  // -- vertex data --
135  iEvent.getByToken(verticesToken_, vertices);
136  for (vector<reco::Vertex>::const_iterator itv = vertices->begin(), edv = vertices->end(); itv != edv; ++itv) {
137  if (!(vtxPreselection_(*itv)))
138  continue;
139  selVtxs_.push_back(reco::VertexRef(vertices, std::distance(vertices->begin(), itv)));
140  }
141  }
142  // -- candidate data --
144  vector<pair<double, reco::CandidatePtr>> cands;
145  for (vector<edm::EDGetTokenT<edm::View<reco::Candidate>>>::const_iterator itt = candidatesToken_.begin(),
146  edt = candidatesToken_.end();
147  itt != edt;
148  ++itt) {
149  Handle<View<reco::Candidate>> theseCands;
150  iEvent.getByToken(*itt, theseCands);
151  for (View<reco::Candidate>::const_iterator itc = theseCands->begin(), edc = theseCands->end(); itc != edc;
152  ++itc) {
153  if (!(candPreselection_(*itc)))
154  continue;
155  cands.push_back(pair<double, reco::CandidatePtr>(
156  -itc->pt(), reco::CandidatePtr(theseCands, std::distance(theseCands->begin(), itc))));
157  }
158  }
159  if (!cands.empty())
160  bestCand_ = cands.front().second;
161  }
162 
163  bool passes = false;
164  std::unique_ptr<vector<reco::Vertex>> result;
165  // Run main mode + possible fallback modes
166  for (std::vector<Mode>::const_iterator itm = modes_.begin(), endm = modes_.end(); itm != endm; ++itm) {
167  result = filter_(*itm, iEvent, iSetup);
168  // Check if we got any vertices. If so, take them.
169  if (!result->empty()) {
170  passes = true;
171  break;
172  }
173  }
174  iEvent.put(std::move(result));
175  // Check if we want to apply the EDFilter
176  if (doFilterEvents_)
177  return passes;
178  else
179  return true;
180 }
181 
182 std::unique_ptr<std::vector<reco::Vertex>> PATSingleVertexSelector::filter_(Mode mode,
183  const edm::Event &iEvent,
184  const edm::EventSetup &iSetup) {
185  using namespace edm;
186  using namespace std;
187  auto result = std::make_unique<std::vector<reco::Vertex>>();
188  switch (mode) {
189  case First: {
190  if (selVtxs_.empty())
191  return result;
192  result->push_back(*selVtxs_.front());
193  return result;
194  }
195  case FromCand: {
196  if (bestCand_.isNull())
197  return result;
199  auto const &bestCandDeref = *bestCand_;
200  if (typeid(bestCandDeref) == typeid(reco::VertexCompositeCandidate)) {
201  vtx = reco::Vertex(bestCand_->vertex(),
202  bestCand_->vertexCovariance(),
203  bestCand_->vertexChi2(),
204  bestCand_->vertexNdof(),
205  bestCand_->numberOfDaughters());
206  } else {
207  vtx = reco::Vertex(bestCand_->vertex(), reco::Vertex::Error(), 0, 0, 0);
208  }
209  result->push_back(vtx);
210  return result;
211  }
212  case NearestToCand: {
213  if (selVtxs_.empty() || (bestCand_.isNull()))
214  return result;
216  float dzmin = 9999.0;
217  for (auto itv = selVtxs_.begin(), edv = selVtxs_.end(); itv != edv; ++itv) {
218  float dz = std::abs((*itv)->z() - bestCand_->vz());
219  if (dz < dzmin) {
220  dzmin = dz;
221  which = *itv;
222  }
223  }
224  if (which.isNonnull()) // actually it should not happen, but better safe than sorry
225  result->push_back(*which);
226  return result;
227  }
228  case FromBeamSpot: {
230  iEvent.getByToken(beamSpotToken_, beamSpot);
231  reco::Vertex bs(beamSpot->position(), beamSpot->covariance3D(), 0, 0, 0);
232  result->push_back(bs);
233  return result;
234  }
235  default:
236  // Return an empty vector signifying no vertices found.
237  return result;
238  }
239 }
240 
Produces a list containing a single vertex selected by some criteria.
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
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:44
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
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
Definition: HeavyIon.h:7
std::vector< reco::VertexRef > selVtxs_
int iEvent
Definition: GenABIO.cc:224
bool isNull() const
Checks for null.
Definition: Ptr.h:142
Mode parseMode(const std::string &name) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< edm::EDGetTokenT< edm::View< reco::Candidate > > > candidatesToken_
bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
PATSingleVertexSelector(const edm::ParameterSet &iConfig)
edm::Ptr< Candidate > CandidatePtr
persistent reference to an object in a collection of Candidate objects
Definition: CandidateFwd.h:25
StringCutObjectSelector< reco::Vertex > VtxSel
StringCutObjectSelector< reco::Candidate > CandSel
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
def which(cmd)
Definition: eostools.py:336
std::unique_ptr< std::vector< reco::Vertex > > filter_(Mode mode, const edm::Event &iEvent, const edm::EventSetup &iSetup)
def move(src, dest)
Definition: eostools.py:511