CMS 3D CMS Logo

PATSingleVertexSelector.cc
Go to the documentation of this file.
5 
7 
8 #include <algorithm>
9 
11 
12 PATSingleVertexSelector::Mode PATSingleVertexSelector::parseMode(const std::string &mode) const {
13  if (mode == "firstVertex") {
14  return First;
15  } else if (mode == "nearestToCandidate") {
16  return NearestToCand;
17  } else if (mode == "fromCandidate") {
18  return FromCand;
19  } else if (mode == "beamSpot") {
20  return FromBeamSpot;
21  } else {
22  throw cms::Exception("Configuration")
23  << "PATSingleVertexSelector: Mode '" << mode << "' not recognized or not supported.\n";
24  }
25 }
26 
28  : vtxPreselection_(iConfig.existsAs<std::string>("vertexPreselection")
29  ? iConfig.getParameter<std::string>("vertexPreselection")
30  : std::string(" 1 == 1 ")),
31  candPreselection_(iConfig.existsAs<std::string>("candidatePreselection")
32  ? iConfig.getParameter<std::string>("candidatePreselection")
33  : std::string(" 1 == 1 ")),
34  doFilterEvents_(false) {
35  using namespace std;
36 
37  modes_.push_back(parseMode(iConfig.getParameter<std::string>("mode")));
38  if (iConfig.exists("fallbacks")) {
39  vector<string> modes = iConfig.getParameter<vector<string>>("fallbacks");
40  for (vector<string>::const_iterator it = modes.begin(), ed = modes.end(); it != ed; ++it) {
41  modes_.push_back(parseMode(*it));
42  }
43  }
45  verticesToken_ = consumes<vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vertices"));
46  }
49  edm::vector_transform(iConfig.getParameter<vector<edm::InputTag>>("candidates"),
50  [this](edm::InputTag const &tag) { return consumes<edm::View<reco::Candidate>>(tag); });
51  }
52  if (hasMode_(FromBeamSpot)) {
53  beamSpotToken_ = consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"));
54  }
55 
56  if (iConfig.exists("filter"))
57  doFilterEvents_ = iConfig.getParameter<bool>("filter");
58 
59  produces<vector<reco::Vertex>>();
60 }
61 
63 
65  return (std::find(modes_.begin(), modes_.end(), mode) != modes_.end());
66 }
67 
69  using namespace edm;
70  using namespace std;
71 
72  // Clear
73  selVtxs_.clear();
75 
76  // Gather data from the Event
77  // -- vertex data --
80  iEvent.getByToken(verticesToken_, vertices);
81  for (vector<reco::Vertex>::const_iterator itv = vertices->begin(), edv = vertices->end(); itv != edv; ++itv) {
82  if (!(vtxPreselection_(*itv)))
83  continue;
84  selVtxs_.push_back(reco::VertexRef(vertices, std::distance(vertices->begin(), itv)));
85  }
86  }
87  // -- candidate data --
89  vector<pair<double, reco::CandidatePtr>> cands;
90  for (vector<edm::EDGetTokenT<edm::View<reco::Candidate>>>::const_iterator itt = candidatesToken_.begin(),
91  edt = candidatesToken_.end();
92  itt != edt;
93  ++itt) {
94  Handle<View<reco::Candidate>> theseCands;
95  iEvent.getByToken(*itt, theseCands);
96  for (View<reco::Candidate>::const_iterator itc = theseCands->begin(), edc = theseCands->end(); itc != edc;
97  ++itc) {
98  if (!(candPreselection_(*itc)))
99  continue;
100  cands.push_back(pair<double, reco::CandidatePtr>(
101  -itc->pt(), reco::CandidatePtr(theseCands, std::distance(theseCands->begin(), itc))));
102  }
103  }
104  if (!cands.empty())
105  bestCand_ = cands.front().second;
106  }
107 
108  bool passes = false;
109  std::unique_ptr<vector<reco::Vertex>> result;
110  // Run main mode + possible fallback modes
111  for (std::vector<Mode>::const_iterator itm = modes_.begin(), endm = modes_.end(); itm != endm; ++itm) {
112  result = filter_(*itm, iEvent, iSetup);
113  // Check if we got any vertices. If so, take them.
114  if (!result->empty()) {
115  passes = true;
116  break;
117  }
118  }
119  iEvent.put(std::move(result));
120  // Check if we want to apply the EDFilter
121  if (doFilterEvents_)
122  return passes;
123  else
124  return true;
125 }
126 
127 std::unique_ptr<std::vector<reco::Vertex>> PATSingleVertexSelector::filter_(Mode mode,
128  const edm::Event &iEvent,
129  const edm::EventSetup &iSetup) {
130  using namespace edm;
131  using namespace std;
132  auto result = std::make_unique<std::vector<reco::Vertex>>();
133  switch (mode) {
134  case First: {
135  if (selVtxs_.empty())
136  return result;
137  result->push_back(*selVtxs_.front());
138  return result;
139  }
140  case FromCand: {
141  if (bestCand_.isNull())
142  return result;
144  auto const &bestCandDeref = *bestCand_;
145  if (typeid(bestCandDeref) == typeid(reco::VertexCompositeCandidate)) {
146  vtx = reco::Vertex(bestCand_->vertex(),
147  bestCand_->vertexCovariance(),
148  bestCand_->vertexChi2(),
149  bestCand_->vertexNdof(),
150  bestCand_->numberOfDaughters());
151  } else {
152  vtx = reco::Vertex(bestCand_->vertex(), reco::Vertex::Error(), 0, 0, 0);
153  }
154  result->push_back(vtx);
155  return result;
156  }
157  case NearestToCand: {
158  if (selVtxs_.empty() || (bestCand_.isNull()))
159  return result;
161  float dzmin = 9999.0;
162  for (auto itv = selVtxs_.begin(), edv = selVtxs_.end(); itv != edv; ++itv) {
163  float dz = std::abs((*itv)->z() - bestCand_->vz());
164  if (dz < dzmin) {
165  dzmin = dz;
166  which = *itv;
167  }
168  }
169  if (which.isNonnull()) // actually it should not happen, but better safe than sorry
170  result->push_back(*which);
171  return result;
172  }
173  case FromBeamSpot: {
175  iEvent.getByToken(beamSpotToken_, beamSpot);
176  reco::Vertex bs(beamSpot->position(), beamSpot->covariance3D(), 0, 0, 0);
177  result->push_back(bs);
178  return result;
179  }
180  default:
181  // Return an empty vector signifying no vertices found.
182  return result;
183  }
184 }
185 
pat::PATSingleVertexSelector::hasMode_
bool hasMode_(Mode mode) const
Definition: PATSingleVertexSelector.cc:64
pat::PATSingleVertexSelector::FromBeamSpot
Definition: PATSingleVertexSelector.h:36
pwdgSkimBPark_cfi.beamSpot
beamSpot
Definition: pwdgSkimBPark_cfi.py:5
pat::PATSingleVertexSelector::beamSpotToken_
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
Definition: PATSingleVertexSelector.h:52
pat::PATSingleVertexSelector::modes_
std::vector< Mode > modes_
Definition: PATSingleVertexSelector.h:47
funct::false
false
Definition: Factorize.h:29
pat::PATSingleVertexSelector::verticesToken_
edm::EDGetTokenT< std::vector< reco::Vertex > > verticesToken_
Definition: PATSingleVertexSelector.h:48
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
reco::Vertex::Error
math::Error< dimension >::type Error
covariance error matrix (3x3)
Definition: Vertex.h:44
pat::PATSingleVertexSelector::~PATSingleVertexSelector
~PATSingleVertexSelector() override
Definition: PATSingleVertexSelector.cc:62
ALCARECOPromptCalibProdSiPixelAli0T_cff.mode
mode
Definition: ALCARECOPromptCalibProdSiPixelAli0T_cff.py:96
pat::PATSingleVertexSelector::NearestToCand
Definition: PATSingleVertexSelector.h:36
reco::VertexCompositeCandidate
Definition: VertexCompositeCandidate.h:16
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
edm::Handle
Definition: AssociativeIterator.h:50
pat::PATSingleVertexSelector::PATSingleVertexSelector
PATSingleVertexSelector(const edm::ParameterSet &iConfig)
Definition: PATSingleVertexSelector.cc:27
edm::Ref< VertexCollection >
pat::PATSingleVertexSelector::bestCand_
reco::CandidatePtr bestCand_
Definition: PATSingleVertexSelector.h:55
MakerMacros.h
cms::cuda::bs
bs
Definition: HistoContainer.h:127
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
PATSingleVertexSelector.h
BeamSpot.h
VertexCompositeCandidate.h
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
pat::PATSingleVertexSelector::parseMode
Mode parseMode(const std::string &name) const
Definition: PATSingleVertexSelector.cc:12
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::vector_transform
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
pat::PATSingleVertexSelector
Produces a list containing a single vertex selected by some criteria.
Definition: PATSingleVertexSelector.h:28
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
edm::View
Definition: CaloClusterFwd.h:14
HLT_FULL_cff.cands
cands
Definition: HLT_FULL_cff.py:15146
edm::ParameterSet
Definition: ParameterSet.h:47
pat::PATSingleVertexSelector::vtxPreselection_
const VtxSel vtxPreselection_
Definition: PATSingleVertexSelector.h:50
iEvent
int iEvent
Definition: GenABIO.cc:224
pat::PATSingleVertexSelector::filter
bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: PATSingleVertexSelector.cc:68
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::EventSetup
Definition: EventSetup.h:58
pat::PATSingleVertexSelector::First
Definition: PATSingleVertexSelector.h:36
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
HltBtagValidation_cff.Vertex
Vertex
Definition: HltBtagValidation_cff.py:32
extraflags_cff.vtx
vtx
Definition: extraflags_cff.py:18
PVValHelper::dz
Definition: PVValidationHelpers.h:51
transform.h
Exception
Definition: hltDiff.cc:245
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::View::const_iterator
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
pat::PATSingleVertexSelector::doFilterEvents_
bool doFilterEvents_
Definition: PATSingleVertexSelector.h:60
pat::PATSingleVertexSelector::Mode
Mode
Definition: PATSingleVertexSelector.h:36
mps_fire.result
result
Definition: mps_fire.py:311
reco::CandidatePtr
edm::Ptr< Candidate > CandidatePtr
persistent reference to an object in a collection of Candidate objects
Definition: CandidateFwd.h:25
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
View.h
pat::PATSingleVertexSelector::filter_
std::unique_ptr< std::vector< reco::Vertex > > filter_(Mode mode, const edm::Event &iEvent, const edm::EventSetup &iSetup)
Definition: PATSingleVertexSelector.cc:127
edm::Event
Definition: Event.h:73
pat::PATSingleVertexSelector::selVtxs_
std::vector< reco::VertexRef > selVtxs_
Definition: PATSingleVertexSelector.h:54
HLT_FULL_cff.distance
distance
Definition: HLT_FULL_cff.py:7733
pat::PATSingleVertexSelector::candidatesToken_
std::vector< edm::EDGetTokenT< edm::View< reco::Candidate > > > candidatesToken_
Definition: PATSingleVertexSelector.h:49
eostools.which
def which(cmd)
Definition: eostools.py:336
pat::PATSingleVertexSelector::FromCand
Definition: PATSingleVertexSelector.h:36
edm::Ptr::isNull
bool isNull() const
Checks for null.
Definition: Ptr.h:142
edm::InputTag
Definition: InputTag.h:15
reco::Vertex
Definition: Vertex.h:35
pat::PATSingleVertexSelector::candPreselection_
const CandSel candPreselection_
Definition: PATSingleVertexSelector.h:51
pwdgSkimBPark_cfi.vertices
vertices
Definition: pwdgSkimBPark_cfi.py:7