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  } else fitOK = false;
187  if ( fitOK ) thePV = transVtx;
188  }
189  reco::VertexRef vtxRef = reco::VertexRef(vertexRefProd_out, vertexCollection_out->size());
190  vertexCollection_out->push_back(thePV);
191  avPFTauPV->setValue(iPFTau, vtxRef);
192  }
193  }
194  iEvent.put(std::move(vertexCollection_out),"PFTauPrimaryVertices");
195  iEvent.put(std::move(avPFTauPV));
196 }
197 
199  // PFTauPrimaryVertexProducerBase
201 
202  {
204  vpsd1.add<edm::InputTag>("discriminator");
205  vpsd1.add<double>("selectionCut");
206  desc.addVPSet("discriminators", vpsd1);
207  }
208 
209  {
210  edm::ParameterSetDescription pset_signalQualityCuts;
211  pset_signalQualityCuts.add<double>("maxDeltaZ", 0.4);
212  pset_signalQualityCuts.add<double>("minTrackPt", 0.5);
213  pset_signalQualityCuts.add<double>("minTrackVertexWeight", -1.0);
214  pset_signalQualityCuts.add<double>("maxTrackChi2", 100.0);
215  pset_signalQualityCuts.add<unsigned int>("minTrackPixelHits", 0);
216  pset_signalQualityCuts.add<double>("minGammaEt", 1.0);
217  pset_signalQualityCuts.add<unsigned int>("minTrackHits", 3);
218  pset_signalQualityCuts.add<double>("minNeutralHadronEt", 30.0);
219  pset_signalQualityCuts.add<double>("maxTransverseImpactParameter", 0.1);
220  pset_signalQualityCuts.addOptional<bool>("useTracksInsteadOfPFHadrons");
221 
222  edm::ParameterSetDescription pset_vxAssocQualityCuts;
223  pset_vxAssocQualityCuts.add<double>("minTrackPt", 0.5);
224  pset_vxAssocQualityCuts.add<double>("minTrackVertexWeight", -1.0);
225  pset_vxAssocQualityCuts.add<double>("maxTrackChi2", 100.0);
226  pset_vxAssocQualityCuts.add<unsigned int>("minTrackPixelHits", 0);
227  pset_vxAssocQualityCuts.add<double>("minGammaEt", 1.0);
228  pset_vxAssocQualityCuts.add<unsigned int>("minTrackHits", 3);
229  pset_vxAssocQualityCuts.add<double>("maxTransverseImpactParameter", 0.1);
230  pset_vxAssocQualityCuts.addOptional<bool>("useTracksInsteadOfPFHadrons");
231 
232  edm::ParameterSetDescription pset_isolationQualityCuts;
233  pset_isolationQualityCuts.add<double>("maxDeltaZ", 0.2);
234  pset_isolationQualityCuts.add<double>("minTrackPt", 1.0);
235  pset_isolationQualityCuts.add<double>("minTrackVertexWeight", -1.0);
236  pset_isolationQualityCuts.add<double>("maxTrackChi2", 100.0);
237  pset_isolationQualityCuts.add<unsigned int>("minTrackPixelHits", 0);
238  pset_isolationQualityCuts.add<double>("minGammaEt", 1.5);
239  pset_isolationQualityCuts.add<unsigned int>("minTrackHits", 8);
240  pset_isolationQualityCuts.add<double>("maxTransverseImpactParameter", 0.03);
241  pset_isolationQualityCuts.addOptional<bool>("useTracksInsteadOfPFHadrons");
242 
243  edm::ParameterSetDescription pset_qualityCuts;
244  pset_qualityCuts.add<edm::ParameterSetDescription>("signalQualityCuts", pset_signalQualityCuts);
245  pset_qualityCuts.add<edm::ParameterSetDescription>("vxAssocQualityCuts", pset_vxAssocQualityCuts);
246  pset_qualityCuts.add<edm::ParameterSetDescription>("isolationQualityCuts", pset_isolationQualityCuts);
247  pset_qualityCuts.add<std::string>("leadingTrkOrPFCandOption", "leadPFCand");
248  pset_qualityCuts.add<std::string>("pvFindingAlgo", "closestInDeltaZ");
249  pset_qualityCuts.add<edm::InputTag>("primaryVertexSrc", edm::InputTag("offlinePrimaryVertices"));
250  pset_qualityCuts.add<bool>("vertexTrackFiltering", false);
251  pset_qualityCuts.add<bool>("recoverLeadingTrk", false);
252 
253  desc.add<edm::ParameterSetDescription>("qualityCuts", pset_qualityCuts);
254  }
255 
256  desc.add<std::string>("cut", "pt > 18.0 & abs(eta)<2.3");
257  desc.add<int>("Algorithm", 0);
258  desc.add<bool>("RemoveElectronTracks", false);
259  desc.add<bool>("RemoveMuonTracks", false);
260  desc.add<bool>("useBeamSpot", true);
261  desc.add<bool>("useSelectedTaus", false);
262  desc.add<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
263  desc.add<edm::InputTag>("ElectronTag", edm::InputTag("MyElectrons"));
264  desc.add<edm::InputTag>("PFTauTag", edm::InputTag("hpsPFTauProducer"));
265  desc.add<edm::InputTag>("MuonTag", edm::InputTag("MyMuons"));
266  desc.add<edm::InputTag>("PVTag", edm::InputTag("offlinePrimaryVertices"));
267 
268  return desc;
269 }
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
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_
edm::EDGetTokenT< edm::View< reco::Electron > > electronToken_
def move(src, dest)
Definition: eostools.py:511