CMS 3D CMS Logo

PFTauPrimaryVertexProducerBase.cc
Go to the documentation of this file.
2 
6 
11 
15 
17 
18 #include <memory>
19 
21  pftauToken_(consumes<std::vector<reco::PFTau> >(iConfig.getParameter<edm::InputTag>("PFTauTag"))),
22  electronToken_(consumes<edm::View<reco::Electron> >(iConfig.getParameter<edm::InputTag>("ElectronTag"))),
23  muonToken_(consumes<edm::View<reco::Muon> >(iConfig.getParameter<edm::InputTag>("MuonTag"))),
24  pvToken_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("PVTag"))),
25  beamSpotToken_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))),
26  algorithm_(iConfig.getParameter<int>("Algorithm")),
27  qualityCutsPSet_(iConfig.getParameter<edm::ParameterSet>("qualityCuts")),
28  useBeamSpot_(iConfig.getParameter<bool>("useBeamSpot")),
29  useSelectedTaus_(iConfig.getParameter<bool>("useSelectedTaus")),
30  removeMuonTracks_(iConfig.getParameter<bool>("RemoveMuonTracks")),
31  removeElectronTracks_(iConfig.getParameter<bool>("RemoveElectronTracks"))
32 {
34  std::vector<edm::ParameterSet> discriminators = iConfig.getParameter<std::vector<edm::ParameterSet> >("discriminators");
35  // Build each of our cuts
36  for(auto const& pset : discriminators) {
37  DiscCutPair* newCut = new DiscCutPair();
38  newCut->inputToken_ =consumes<reco::PFTauDiscriminator>(pset.getParameter<edm::InputTag>("discriminator"));
39 
40  if ( pset.existsAs<std::string>("selectionCut") ) newCut->cutFormula_ = new TFormula("selectionCut", pset.getParameter<std::string>("selectionCut").data());
41  else newCut->cut_ = pset.getParameter<double>("selectionCut");
42  discriminators_.push_back(newCut);
43  }
44  // Build a string cut if desired
45  if (iConfig.exists("cut")) cut_.reset(new StringCutObjectSelector<reco::PFTau>(iConfig.getParameter<std::string>( "cut" )));
47  produces<edm::AssociationVector<reco::PFTauRefProd, std::vector<reco::VertexRef> > >();
48  produces<reco::VertexCollection>("PFTauPrimaryVertices");
49 
51 }
52 
54 
55 namespace {
57  const reco::PFCandidate* pfCandPtr = dynamic_cast<const reco::PFCandidate*>(&cand);
58  if (pfCandPtr) {
59  if ( pfCandPtr->trackRef().isNonnull() ) return edm::refToPtr(pfCandPtr->trackRef());
60  else if ( pfCandPtr->gsfTrackRef().isNonnull() ) return edm::refToPtr(pfCandPtr->gsfTrackRef());
61  else return edm::Ptr<reco::TrackBase>();
62  }
63  const pat::PackedCandidate* pCand = dynamic_cast<const pat::PackedCandidate*>(&cand);
64  if (pCand && pCand->hasTrackDetails()) {
65  const reco::TrackBase* trkPtr = pCand->bestTrack();
66  return edm::Ptr<reco::TrackBase>(trkPtr,0);
67  }
69  }
70 }
71 
73 
74  beginEvent(iEvent, iSetup);
75 
76  // Obtain Collections
77  edm::ESHandle<TransientTrackBuilder> transTrackBuilder;
78  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",transTrackBuilder);
79 
81  iEvent.getByToken(pftauToken_,pfTaus);
82 
84  if(removeElectronTracks_) iEvent.getByToken(electronToken_,electrons);
85 
87  if(removeMuonTracks_) iEvent.getByToken(muonToken_,muons);
88 
90  iEvent.getByToken(pvToken_,vertices);
91 
93  if(useBeamSpot_) iEvent.getByToken(beamSpotToken_,beamSpot);
94 
95  // Set Association Map
96  auto avPFTauPV = std::make_unique<edm::AssociationVector<reco::PFTauRefProd, std::vector<reco::VertexRef>>>(reco::PFTauRefProd(pfTaus));
97  auto vertexCollection_out = std::make_unique<reco::VertexCollection>();
98  reco::VertexRefProd vertexRefProd_out = iEvent.getRefBeforePut<reco::VertexCollection>("PFTauPrimaryVertices");
99 
100  // Load each discriminator
101  for(auto& disc : discriminators_) {
103  iEvent.getByToken(disc->inputToken_, discr);
104  disc->discr_ = &(*discr);
105  }
106 
107  // Set event for VerexAssociator if needed
109  vertexAssociator_->setEvent(iEvent);
110 
111  // For each Tau Run Algorithim
112  if(pfTaus.isValid()){
113  for(reco::PFTauCollection::size_type iPFTau = 0; iPFTau < pfTaus->size(); iPFTau++) {
114  reco::PFTauRef tau(pfTaus, iPFTau);
115  reco::VertexRef thePVRef;
116  if(useInputPV==algorithm_){
117  thePVRef = vertexAssociator_->associatedVertex(*tau);
118  }
119  else if(useFrontPV==algorithm_){
120  thePVRef = reco::VertexRef(vertices,0);
121  }
122  reco::Vertex thePV = *thePVRef;
124  // Check if it passed all the discrimiantors
125  bool passed(true);
126  for(auto const& disc : discriminators_) {
127  // Check this discriminator passes
128  bool passedDisc = true;
129  if ( disc->cutFormula_ )passedDisc = (disc->cutFormula_->Eval((*disc->discr_)[tau]) > 0.5);
130  else passedDisc = ((*disc->discr_)[tau] > disc->cut_);
131  if ( !passedDisc ){passed = false; break;}
132  }
133  if (passed && cut_.get()){passed = (*cut_)(*tau);}
134  if (passed){
135  std::vector<edm::Ptr<reco::TrackBase> > signalTracks;
136  for(reco::PFTauCollection::size_type jPFTau = 0; jPFTau < pfTaus->size(); jPFTau++) {
137  if(useSelectedTaus_ || iPFTau==jPFTau){
138  reco::PFTauRef pfTauRef(pfTaus, jPFTau);
140  // Get tracks from PFTau daughters
141  for(const auto& pfcand : pfTauRef->signalChargedHadrCands()) {
142  if(pfcand.isNull()) continue;
143  const edm::Ptr<reco::TrackBase>& trackPtr = getTrack(*pfcand);
144  if(trackPtr.isNonnull()) signalTracks.push_back(trackPtr);
145  }
146  }
147  }
148  // Get Muon tracks
149  if(removeMuonTracks_){
150  if(muons.isValid()) {
151  for(const auto& muon: *muons){
152  if(muon.track().isNonnull()) signalTracks.push_back(edm::refToPtr(muon.track()));
153  }
154  }
155  }
156  // Get Electron Tracks
158  if(electrons.isValid()) {
159  for(const auto& electron: *electrons){
160  if(electron.track().isNonnull()) signalTracks.push_back(edm::refToPtr(electron.track()));
161  if(electron.gsfTrack().isNonnull()) signalTracks.push_back(edm::refToPtr(electron.gsfTrack()));
162  }
163  }
164  }
166  // Get Non-Tau tracks
167  std::vector<const reco::Track*> nonTauTracks;
168  nonTauTracksInPV(thePVRef,signalTracks,nonTauTracks);
169 
171  // Refit the vertex
172  TransientVertex transVtx;
173  std::vector<reco::TransientTrack> transTracks;
174  for(const auto track: nonTauTracks){
175  transTracks.push_back(transTrackBuilder->build(*track));
176  }
177  bool fitOK(true);
178  if ( transTracks.size() >= 2 ) {
180  avf.setWeightThreshold(0.1); //weight per track. allow almost every fit, else --> exception
181  if ( !useBeamSpot_ ){
182  transVtx = avf.vertex(transTracks);
183  } else {
184  transVtx = avf.vertex(transTracks, *beamSpot);
185  }
186  if (!transVtx.isValid()) {
187  fitOK = false;
188  } else {
189  //MB: protect against rare cases when transVtx is valid but its position is ill-defined
190  if (!std::isfinite(transVtx.position().z())) //MB: it is enough to check one coordinate (?)
191  fitOK = false;
192  }
193  } else fitOK = false;
194  if ( fitOK ) thePV = transVtx;
195  }
196  reco::VertexRef vtxRef = reco::VertexRef(vertexRefProd_out, vertexCollection_out->size());
197  vertexCollection_out->push_back(thePV);
198  avPFTauPV->setValue(iPFTau, vtxRef);
199  }
200  }
201  iEvent.put(std::move(vertexCollection_out),"PFTauPrimaryVertices");
202  iEvent.put(std::move(avPFTauPV));
203 }
204 
206  // PFTauPrimaryVertexProducerBase
208 
209  {
211  vpsd1.add<edm::InputTag>("discriminator");
212  vpsd1.add<double>("selectionCut");
213  desc.addVPSet("discriminators", vpsd1);
214  }
215 
216  {
217  edm::ParameterSetDescription pset_signalQualityCuts;
218  pset_signalQualityCuts.add<double>("maxDeltaZ", 0.4);
219  pset_signalQualityCuts.add<double>("minTrackPt", 0.5);
220  pset_signalQualityCuts.add<double>("minTrackVertexWeight", -1.0);
221  pset_signalQualityCuts.add<double>("maxTrackChi2", 100.0);
222  pset_signalQualityCuts.add<unsigned int>("minTrackPixelHits", 0);
223  pset_signalQualityCuts.add<double>("minGammaEt", 1.0);
224  pset_signalQualityCuts.add<unsigned int>("minTrackHits", 3);
225  pset_signalQualityCuts.add<double>("minNeutralHadronEt", 30.0);
226  pset_signalQualityCuts.add<double>("maxTransverseImpactParameter", 0.1);
227  pset_signalQualityCuts.addOptional<bool>("useTracksInsteadOfPFHadrons");
228 
229  edm::ParameterSetDescription pset_vxAssocQualityCuts;
230  pset_vxAssocQualityCuts.add<double>("minTrackPt", 0.5);
231  pset_vxAssocQualityCuts.add<double>("minTrackVertexWeight", -1.0);
232  pset_vxAssocQualityCuts.add<double>("maxTrackChi2", 100.0);
233  pset_vxAssocQualityCuts.add<unsigned int>("minTrackPixelHits", 0);
234  pset_vxAssocQualityCuts.add<double>("minGammaEt", 1.0);
235  pset_vxAssocQualityCuts.add<unsigned int>("minTrackHits", 3);
236  pset_vxAssocQualityCuts.add<double>("maxTransverseImpactParameter", 0.1);
237  pset_vxAssocQualityCuts.addOptional<bool>("useTracksInsteadOfPFHadrons");
238 
239  edm::ParameterSetDescription pset_isolationQualityCuts;
240  pset_isolationQualityCuts.add<double>("maxDeltaZ", 0.2);
241  pset_isolationQualityCuts.add<double>("minTrackPt", 1.0);
242  pset_isolationQualityCuts.add<double>("minTrackVertexWeight", -1.0);
243  pset_isolationQualityCuts.add<double>("maxTrackChi2", 100.0);
244  pset_isolationQualityCuts.add<unsigned int>("minTrackPixelHits", 0);
245  pset_isolationQualityCuts.add<double>("minGammaEt", 1.5);
246  pset_isolationQualityCuts.add<unsigned int>("minTrackHits", 8);
247  pset_isolationQualityCuts.add<double>("maxTransverseImpactParameter", 0.03);
248  pset_isolationQualityCuts.addOptional<bool>("useTracksInsteadOfPFHadrons");
249 
250  edm::ParameterSetDescription pset_qualityCuts;
251  pset_qualityCuts.add<edm::ParameterSetDescription>("signalQualityCuts", pset_signalQualityCuts);
252  pset_qualityCuts.add<edm::ParameterSetDescription>("vxAssocQualityCuts", pset_vxAssocQualityCuts);
253  pset_qualityCuts.add<edm::ParameterSetDescription>("isolationQualityCuts", pset_isolationQualityCuts);
254  pset_qualityCuts.add<std::string>("leadingTrkOrPFCandOption", "leadPFCand");
255  pset_qualityCuts.add<std::string>("pvFindingAlgo", "closestInDeltaZ");
256  pset_qualityCuts.add<edm::InputTag>("primaryVertexSrc", edm::InputTag("offlinePrimaryVertices"));
257  pset_qualityCuts.add<bool>("vertexTrackFiltering", false);
258  pset_qualityCuts.add<bool>("recoverLeadingTrk", false);
259 
260  desc.add<edm::ParameterSetDescription>("qualityCuts", pset_qualityCuts);
261  }
262 
263  desc.add<std::string>("cut", "pt > 18.0 & abs(eta)<2.3");
264  desc.add<int>("Algorithm", 0);
265  desc.add<bool>("RemoveElectronTracks", false);
266  desc.add<bool>("RemoveMuonTracks", false);
267  desc.add<bool>("useBeamSpot", true);
268  desc.add<bool>("useSelectedTaus", false);
269  desc.add<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
270  desc.add<edm::InputTag>("ElectronTag", edm::InputTag("MyElectrons"));
271  desc.add<edm::InputTag>("PFTauTag", edm::InputTag("hpsPFTauProducer"));
272  desc.add<edm::InputTag>("MuonTag", edm::InputTag("MyMuons"));
273  desc.add<edm::InputTag>("PVTag", edm::InputTag("offlinePrimaryVertices"));
274 
275  return desc;
276 }
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
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
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
virtual void nonTauTracksInPV(const reco::VertexRef &, const std::vector< edm::Ptr< reco::TrackBase > > &, std::vector< const reco::Track * > &)=0
edm::EDGetTokenT< reco::VertexCollection > pvToken_
reco::TransientTrack build(const reco::Track *p) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &) const override
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
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 &)
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
int iEvent
Definition: GenABIO.cc:224
Definition: Muon.py:1
GlobalPoint position() const
T z() const
Definition: PV3DBase.h:64
edm::EDGetTokenT< std::vector< reco::PFTau > > pftauToken_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
edm::Ref< VertexCollection > VertexRef
persistent reference to a Vertex
Definition: VertexFwd.h:13
RefProd< PROD > getRefBeforePut()
Definition: Event.h:150
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:168
static const TrackGhostTrackState * getTrack(const BasicGhostTrackState *basic)
std::unique_ptr< reco::tau::RecoTauVertexAssociator > vertexAssociator_
PFTauPrimaryVertexProducerBase(const edm::ParameterSet &iConfig)
static edm::ParameterSetDescription getDescriptionsBase()
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
HLT enums.
T get() const
Definition: EventSetup.h:71
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:480
void produce(edm::Event &, const edm::EventSetup &) override
std::unique_ptr< StringCutObjectSelector< reco::PFTau > > cut_
edm::EDGetTokenT< reco::PFTauDiscriminator > inputToken_
bool isValid() const
edm::EDGetTokenT< edm::View< reco::Electron > > electronToken_
def move(src, dest)
Definition: eostools.py:511