CMS 3D CMS Logo

PFTauPrimaryVertexProducerBase.cc
Go to the documentation of this file.
2 
5 
8 
12 
14 
15 #include <memory>
16 
18  : pftauToken_(consumes<std::vector<reco::PFTau>>(iConfig.getParameter<edm::InputTag>("PFTauTag"))),
19  electronToken_(consumes<edm::View<reco::Electron>>(iConfig.getParameter<edm::InputTag>("ElectronTag"))),
20  muonToken_(consumes<edm::View<reco::Muon>>(iConfig.getParameter<edm::InputTag>("MuonTag"))),
21  pvToken_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("PVTag"))),
22  beamSpotToken_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))),
23  transTrackBuilderToken_(esConsumes(edm::ESInputTag("", "TransientTrackBuilder"))),
24  algorithm_(iConfig.getParameter<int>("Algorithm")),
25  qualityCutsPSet_(iConfig.getParameter<edm::ParameterSet>("qualityCuts")),
26  useBeamSpot_(iConfig.getParameter<bool>("useBeamSpot")),
27  useSelectedTaus_(iConfig.getParameter<bool>("useSelectedTaus")),
28  removeMuonTracks_(iConfig.getParameter<bool>("RemoveMuonTracks")),
29  removeElectronTracks_(iConfig.getParameter<bool>("RemoveElectronTracks")) {
31  std::vector<edm::ParameterSet> discriminators =
32  iConfig.getParameter<std::vector<edm::ParameterSet>>("discriminators");
33  // Build each of our cuts
34  for (auto const& pset : discriminators) {
35  DiscCutPair* newCut = new DiscCutPair();
36  newCut->inputToken_ = consumes<reco::PFTauDiscriminator>(pset.getParameter<edm::InputTag>("discriminator"));
37 
38  if (pset.existsAs<std::string>("selectionCut"))
39  newCut->cutFormula_ = new TFormula("selectionCut", pset.getParameter<std::string>("selectionCut").data());
40  else
41  newCut->cut_ = pset.getParameter<double>("selectionCut");
42  discriminators_.push_back(newCut);
43  }
44  // Build a string cut if desired
45  if (iConfig.exists("cut"))
46  cut_ = std::make_unique<StringCutObjectSelector<reco::PFTau>>(iConfig.getParameter<std::string>("cut"));
48  produces<edm::AssociationVector<reco::PFTauRefProd, std::vector<reco::VertexRef>>>();
49  produces<reco::VertexCollection>("PFTauPrimaryVertices");
50 
51  vertexAssociator_ = std::make_unique<reco::tau::RecoTauVertexAssociator>(qualityCutsPSet_, consumesCollector());
52 }
53 
55 
56 namespace {
58  const reco::PFCandidate* pfCandPtr = dynamic_cast<const reco::PFCandidate*>(&cand);
59  if (pfCandPtr) {
60  if (pfCandPtr->trackRef().isNonnull())
61  return edm::refToPtr(pfCandPtr->trackRef());
62  else if (pfCandPtr->gsfTrackRef().isNonnull())
63  return edm::refToPtr(pfCandPtr->gsfTrackRef());
64  else
66  }
67  const pat::PackedCandidate* pCand = dynamic_cast<const pat::PackedCandidate*>(&cand);
68  if (pCand && pCand->hasTrackDetails()) {
69  const reco::TrackBase* trkPtr = pCand->bestTrack();
70  return edm::Ptr<reco::TrackBase>(trkPtr, 0);
71  }
73  }
74 } // namespace
75 
77  beginEvent(iEvent, iSetup);
78 
79  // Obtain Collections
80  auto const& transTrackBuilder = iSetup.getData(transTrackBuilderToken_);
81 
83  iEvent.getByToken(pftauToken_, pfTaus);
84 
87  iEvent.getByToken(electronToken_, electrons);
88 
91  iEvent.getByToken(muonToken_, muons);
92 
94  iEvent.getByToken(pvToken_, vertices);
95 
97  if (useBeamSpot_)
98  iEvent.getByToken(beamSpotToken_, beamSpot);
99 
100  // Set Association Map
101  auto avPFTauPV = std::make_unique<edm::AssociationVector<reco::PFTauRefProd, std::vector<reco::VertexRef>>>(
102  reco::PFTauRefProd(pfTaus));
103  auto vertexCollection_out = std::make_unique<reco::VertexCollection>();
104  reco::VertexRefProd vertexRefProd_out = iEvent.getRefBeforePut<reco::VertexCollection>("PFTauPrimaryVertices");
105 
106  // Load each discriminator
107  for (auto& disc : discriminators_) {
109  iEvent.getByToken(disc->inputToken_, discr);
110  disc->discr_ = &(*discr);
111  }
112 
113  // Set event for VerexAssociator if needed
114  if (useInputPV == algorithm_)
115  vertexAssociator_->setEvent(iEvent);
116 
117  // For each Tau Run Algorithim
118  if (pfTaus.isValid()) {
119  for (reco::PFTauCollection::size_type iPFTau = 0; iPFTau < pfTaus->size(); iPFTau++) {
120  reco::PFTauRef tau(pfTaus, iPFTau);
121  reco::VertexRef thePVRef;
122  if (useInputPV == algorithm_) {
123  thePVRef = vertexAssociator_->associatedVertex(*tau);
124  } else if (useFrontPV == algorithm_) {
125  thePVRef = reco::VertexRef(vertices, 0);
126  }
127  reco::Vertex thePV = *thePVRef;
129  // Check if it passed all the discrimiantors
130  bool passed(true);
131  for (auto const& disc : discriminators_) {
132  // Check this discriminator passes
133  bool passedDisc = true;
134  if (disc->cutFormula_)
135  passedDisc = (disc->cutFormula_->Eval((*disc->discr_)[tau]) > 0.5);
136  else
137  passedDisc = ((*disc->discr_)[tau] > disc->cut_);
138  if (!passedDisc) {
139  passed = false;
140  break;
141  }
142  }
143  if (passed && cut_.get()) {
144  passed = (*cut_)(*tau);
145  }
146  if (passed) {
147  std::vector<edm::Ptr<reco::TrackBase>> signalTracks;
148  for (reco::PFTauCollection::size_type jPFTau = 0; jPFTau < pfTaus->size(); jPFTau++) {
149  if (useSelectedTaus_ || iPFTau == jPFTau) {
150  reco::PFTauRef pfTauRef(pfTaus, jPFTau);
152  // Get tracks from PFTau daughters
153  for (const auto& pfcand : pfTauRef->signalChargedHadrCands()) {
154  if (pfcand.isNull())
155  continue;
156  const edm::Ptr<reco::TrackBase>& trackPtr = getTrack(*pfcand);
157  if (trackPtr.isNonnull())
158  signalTracks.push_back(trackPtr);
159  }
160  }
161  }
162  // Get Muon tracks
163  if (removeMuonTracks_) {
164  if (muons.isValid()) {
165  for (const auto& muon : *muons) {
166  if (muon.track().isNonnull())
167  signalTracks.push_back(edm::refToPtr(muon.track()));
168  }
169  }
170  }
171  // Get Electron Tracks
172  if (removeElectronTracks_) {
173  if (electrons.isValid()) {
174  for (const auto& electron : *electrons) {
175  if (electron.track().isNonnull())
176  signalTracks.push_back(edm::refToPtr(electron.track()));
177  if (electron.gsfTrack().isNonnull())
178  signalTracks.push_back(edm::refToPtr(electron.gsfTrack()));
179  }
180  }
181  }
183  // Get Non-Tau tracks
184  std::vector<const reco::Track*> nonTauTracks;
185  nonTauTracksInPV(thePVRef, signalTracks, nonTauTracks);
186 
188  // Refit the vertex
189  TransientVertex transVtx;
190  std::vector<reco::TransientTrack> transTracks;
191  transTracks.reserve(nonTauTracks.size());
192  for (const auto track : nonTauTracks) {
193  transTracks.push_back(transTrackBuilder.build(*track));
194  }
195  bool fitOK(true);
196  if (transTracks.size() >= 2) {
198  avf.setWeightThreshold(0.1); //weight per track. allow almost every fit, else --> exception
199  if (!useBeamSpot_) {
200  transVtx = avf.vertex(transTracks);
201  } else {
202  transVtx = avf.vertex(transTracks, *beamSpot);
203  }
204  if (!transVtx.isValid()) {
205  fitOK = false;
206  } else {
207  //MB: protect against rare cases when transVtx is valid but its position is ill-defined
208  if (!std::isfinite(transVtx.position().z())) //MB: it is enough to check one coordinate (?)
209  fitOK = false;
210  }
211  } else
212  fitOK = false;
213  if (fitOK)
214  thePV = transVtx;
215  }
216  reco::VertexRef vtxRef = reco::VertexRef(vertexRefProd_out, vertexCollection_out->size());
217  vertexCollection_out->push_back(thePV);
218  avPFTauPV->setValue(iPFTau, vtxRef);
219  }
220  }
221  iEvent.put(std::move(vertexCollection_out), "PFTauPrimaryVertices");
222  iEvent.put(std::move(avPFTauPV));
223 }
224 
226  // PFTauPrimaryVertexProducerBase
228 
229  {
231  vpsd1.add<edm::InputTag>("discriminator");
232  vpsd1.add<double>("selectionCut");
233  desc.addVPSet("discriminators", vpsd1);
234  }
235 
236  edm::ParameterSetDescription desc_qualityCuts;
238  desc.add<edm::ParameterSetDescription>("qualityCuts", desc_qualityCuts);
239 
240  desc.add<std::string>("cut", "pt > 18.0 & abs(eta)<2.3");
241  desc.add<int>("Algorithm", 0);
242  desc.add<bool>("RemoveElectronTracks", false);
243  desc.add<bool>("RemoveMuonTracks", false);
244  desc.add<bool>("useBeamSpot", true);
245  desc.add<bool>("useSelectedTaus", false);
246  desc.add<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
247  desc.add<edm::InputTag>("ElectronTag", edm::InputTag("MyElectrons"));
248  desc.add<edm::InputTag>("PFTauTag", edm::InputTag("hpsPFTauProducer"));
249  desc.add<edm::InputTag>("MuonTag", edm::InputTag("MyMuons"));
250  desc.add<edm::InputTag>("PVTag", edm::InputTag("offlinePrimaryVertices"));
251 
252  return desc;
253 }
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:469
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
Ptr< typename C::value_type > refToPtr(Ref< C, typename C::value_type, refhelper::FindUsingAdvance< C, typename C::value_type > > const &ref)
Definition: RefToPtr.h:18
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
GlobalPoint position() const
static void fillDescriptions(edm::ParameterSetDescription &descriptions)
Declare all parameters read from python config file.
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_
T z() const
Definition: PV3DBase.h:61
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::EDGetTokenT< reco::VertexCollection > pvToken_
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &) const override
std::vector< Vertex > VertexCollection
Definition: Vertex.h:31
edm::RefProd< PFTauCollection > PFTauRefProd
references to PFTau collection
Definition: PFTauFwd.h:15
uint16_t size_type
virtual void beginEvent(const edm::Event &, const edm::EventSetup &)
edm::ESGetToken< TransientTrackBuilder, TransientTrackRecord > transTrackBuilderToken_
int iEvent
Definition: GenABIO.cc:224
Definition: Muon.py:1
bool isValid() const
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:146
edm::EDGetTokenT< std::vector< reco::PFTau > > pftauToken_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
const reco::Track * bestTrack() const override
return a pointer to the track if present. otherwise, return a null pointer
static const TrackGhostTrackState * getTrack(const BasicGhostTrackState *basic)
std::unique_ptr< reco::tau::RecoTauVertexAssociator > vertexAssociator_
virtual void nonTauTracksInPV(const reco::VertexRef &, const std::vector< edm::Ptr< reco::TrackBase > > &, std::vector< const reco::Track *> &)=0
PFTauPrimaryVertexProducerBase(const edm::ParameterSet &iConfig)
bool isValid() const
Definition: HandleBase.h:70
static edm::ParameterSetDescription getDescriptionsBase()
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
fixed size matrix
HLT enums.
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:437
void produce(edm::Event &, const edm::EventSetup &) override
std::unique_ptr< StringCutObjectSelector< reco::PFTau > > cut_
edm::EDGetTokenT< reco::PFTauDiscriminator > inputToken_
edm::EDGetTokenT< edm::View< reco::Electron > > electronToken_
def move(src, dest)
Definition: eostools.py:511
bool hasTrackDetails() const
Return true if a bestTrack can be extracted from this Candidate.