CMS 3D CMS Logo

PFTauPrimaryVertexProducer.cc
Go to the documentation of this file.
1 /* class PFTauPrimaryVertexProducer
2  * EDProducer of the
3  * authors: Ian M. Nugent
4  * This work is based on the impact parameter work by Rosamaria Venditti and reconstructing the 3 prong taus.
5  * The idea of the fully reconstructing the tau using a kinematic fit comes from
6  * Lars Perchalla and Philip Sauerland Theses under Achim Stahl supervision. This
7  * work was continued by Ian M. Nugent and Vladimir Cherepanov.
8  * Thanks goes to Christian Veelken and Evan Klose Friis for their help and suggestions.
9  */
10 
11 
21 
27 
42 
46 
49 #include <memory>
50 #include <boost/foreach.hpp>
51 #include <TFormula.h>
52 
53 #include <memory>
54 
55 using namespace reco;
56 using namespace edm;
57 using namespace std;
58 
60  public:
61  enum Alg{useInputPV=0, useFontPV};
62 
63  struct DiscCutPair{
64  DiscCutPair():discr_(nullptr),cutFormula_(nullptr){}
65  ~DiscCutPair(){delete cutFormula_;}
68  double cut_;
69  TFormula* cutFormula_;
70  };
71  typedef std::vector<DiscCutPair*> DiscCutPairVec;
72 
73  explicit PFTauPrimaryVertexProducer(const edm::ParameterSet& iConfig);
74  ~PFTauPrimaryVertexProducer() override;
75  void produce(edm::Event&,const edm::EventSetup&) override;
76 
77  private:
94  DiscCutPairVec discriminators_;
95  std::auto_ptr<StringCutObjectSelector<reco::PFTau> > cut_;
96  std::auto_ptr<tau::RecoTauVertexAssociator> vertexAssociator_;
97 };
98 
100  PFTauTag_(iConfig.getParameter<edm::InputTag>("PFTauTag")),
101  PFTauToken_(consumes<std::vector<reco::PFTau> >(PFTauTag_)),
102  ElectronTag_(iConfig.getParameter<edm::InputTag>("ElectronTag")),
103  ElectronToken_(consumes<std::vector<reco::Electron> >(ElectronTag_)),
104  MuonTag_(iConfig.getParameter<edm::InputTag>("MuonTag")),
105  MuonToken_(consumes<std::vector<reco::Muon> >(MuonTag_)),
106  PVTag_(iConfig.getParameter<edm::InputTag>("PVTag")),
107  PVToken_(consumes<reco::VertexCollection>(PVTag_)),
108  beamSpotTag_(iConfig.getParameter<edm::InputTag>("beamSpot")),
109  beamSpotToken_(consumes<reco::BeamSpot>(beamSpotTag_)),
110  Algorithm_(iConfig.getParameter<int>("Algorithm")),
111  qualityCutsPSet_(iConfig.getParameter<edm::ParameterSet>("qualityCuts")),
112  useBeamSpot_(iConfig.getParameter<bool>("useBeamSpot")),
113  useSelectedTaus_(iConfig.getParameter<bool>("useSelectedTaus")),
114  RemoveMuonTracks_(iConfig.getParameter<bool>("RemoveMuonTracks")),
115  RemoveElectronTracks_(iConfig.getParameter<bool>("RemoveElectronTracks"))
116 {
118  std::vector<edm::ParameterSet> discriminators =iConfig.getParameter<std::vector<edm::ParameterSet> >("discriminators");
119  // Build each of our cuts
120  BOOST_FOREACH(const edm::ParameterSet &pset, discriminators) {
121  DiscCutPair* newCut = new DiscCutPair();
122  newCut->inputToken_ =consumes<reco::PFTauDiscriminator>(pset.getParameter<edm::InputTag>("discriminator"));
123 
124  if ( pset.existsAs<std::string>("selectionCut") ) newCut->cutFormula_ = new TFormula("selectionCut", pset.getParameter<std::string>("selectionCut").data());
125  else newCut->cut_ = pset.getParameter<double>("selectionCut");
126  discriminators_.push_back(newCut);
127  }
128  // Build a string cut if desired
129  if (iConfig.exists("cut")) cut_.reset(new StringCutObjectSelector<reco::PFTau>(iConfig.getParameter<std::string>( "cut" )));
131  produces<edm::AssociationVector<PFTauRefProd, std::vector<reco::VertexRef> > >();
132  produces<VertexCollection>("PFTauPrimaryVertices");
133 
134  vertexAssociator_.reset(new tau::RecoTauVertexAssociator(qualityCutsPSet_,consumesCollector()));
135 }
136 
138 
140  // Obtain Collections
141  edm::ESHandle<TransientTrackBuilder> transTrackBuilder;
142  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",transTrackBuilder);
143 
145  iEvent.getByToken(PFTauToken_,Tau);
146 
148  iEvent.getByToken(ElectronToken_,Electron);
149 
151  iEvent.getByToken(MuonToken_,Mu);
152 
154  iEvent.getByToken(PVToken_,PV);
155 
157  iEvent.getByToken(beamSpotToken_,beamSpot);
158 
159  // Set Association Map
160  auto AVPFTauPV = std::make_unique<edm::AssociationVector<PFTauRefProd, std::vector<reco::VertexRef>>>(PFTauRefProd(Tau));
161  auto VertexCollection_out = std::make_unique<VertexCollection>();
162  reco::VertexRefProd VertexRefProd_out = iEvent.getRefBeforePut<reco::VertexCollection>("PFTauPrimaryVertices");
163 
164  // Load each discriminator
165  BOOST_FOREACH(DiscCutPair *disc, discriminators_) {
167  iEvent.getByToken(disc->inputToken_, discr);
168  disc->discr_ = &(*discr);
169  }
170 
171  // Set event for VerexAssociator if needed
173  vertexAssociator_->setEvent(iEvent);
174 
175  // For each Tau Run Algorithim
176  if(Tau.isValid()){
177  for(reco::PFTauCollection::size_type iPFTau = 0; iPFTau < Tau->size(); iPFTau++) {
178  reco::PFTauRef tau(Tau, iPFTau);
179  reco::Vertex thePV;
180  if(useInputPV==Algorithm_){
181  thePV =(*( vertexAssociator_->associatedVertex(*tau)));
182  }
183  else if(useFontPV==Algorithm_){
184  thePV=PV->front();
185  }
187  // Check if it passed all the discrimiantors
188  bool passed(true);
189  BOOST_FOREACH(const DiscCutPair* disc, discriminators_) {
190  // Check this discriminator passes
191  bool passedDisc = true;
192  if ( disc->cutFormula_ )passedDisc = (disc->cutFormula_->Eval((*disc->discr_)[tau]) > 0.5);
193  else passedDisc = ((*disc->discr_)[tau] > disc->cut_);
194  if ( !passedDisc ){passed = false; break;}
195  }
196  if (passed && cut_.get()){passed = (*cut_)(*tau);}
197  if (passed){
198  std::vector<reco::TrackBaseRef> SignalTracks;
199  for(reco::PFTauCollection::size_type jPFTau = 0; jPFTau < Tau->size(); jPFTau++) {
200  if(useSelectedTaus_ || iPFTau==jPFTau){
201  reco::PFTauRef RefPFTau(Tau, jPFTau);
203  // Get tracks from PFTau daugthers
204  const std::vector<edm::Ptr<reco::PFCandidate> > cands = RefPFTau->signalPFChargedHadrCands();
205  for (std::vector<edm::Ptr<reco::PFCandidate> >::const_iterator iter = cands.begin(); iter!=cands.end(); iter++){
206  if(iter->get()->trackRef().isNonnull()) SignalTracks.push_back(reco::TrackBaseRef(iter->get()->trackRef()));
207  else if(iter->get()->gsfTrackRef().isNonnull()){SignalTracks.push_back(reco::TrackBaseRef(((iter)->get()->gsfTrackRef())));}
208  }
209  }
210  }
211  // Get Muon tracks
212  if(RemoveMuonTracks_){
213 
214  if(Mu.isValid()) {
215  for(reco::MuonCollection::size_type iMuon = 0; iMuon< Mu->size(); iMuon++){
216  reco::MuonRef RefMuon(Mu, iMuon);
217  if(RefMuon->track().isNonnull()) SignalTracks.push_back(reco::TrackBaseRef(RefMuon->track()));
218  }
219  }
220  }
221  // Get Electron Tracks
223  if(Electron.isValid()) {
224  for(reco::ElectronCollection::size_type iElectron = 0; iElectron<Electron->size(); iElectron++){
225  reco::ElectronRef RefElectron(Electron, iElectron);
226  if(RefElectron->track().isNonnull()) SignalTracks.push_back(reco::TrackBaseRef(RefElectron->track()));
227  }
228  }
229  }
231  // Get Non-Tau tracks
232  reco::TrackCollection nonTauTracks;
233  for(std::vector<reco::TrackBaseRef>::const_iterator vtxTrkRef=thePV.tracks_begin();vtxTrkRef<thePV.tracks_end();vtxTrkRef++){
234  bool matched = false;
235  for (unsigned int sigTrk = 0; sigTrk < SignalTracks.size(); sigTrk++) {
236  if ( (*vtxTrkRef) == SignalTracks[sigTrk] ) {
237  matched = true;
238  }
239  }
240  if ( !matched ) nonTauTracks.push_back(**vtxTrkRef);
241  }
243  // Refit the vertex
244  TransientVertex transVtx;
245  std::vector<reco::TransientTrack> transTracks;
246  for (reco::TrackCollection::iterator iter=nonTauTracks.begin(); iter!=nonTauTracks.end(); ++iter){
247  transTracks.push_back(transTrackBuilder->build(*iter));
248  }
249  bool FitOk(true);
250  if ( transTracks.size() >= 2 ) {
252  avf.setWeightThreshold(0.1); //weight per track. allow almost every fit, else --> exception
253  try {
254  if ( !useBeamSpot_ ){
255  transVtx = avf.vertex(transTracks);
256  } else {
257  transVtx = avf.vertex(transTracks, *beamSpot);
258  }
259  } catch (...) {
260  FitOk = false;
261  }
262  } else FitOk = false;
263  if ( FitOk ) thePV = transVtx;
264  }
265  VertexRef VRef = reco::VertexRef(VertexRefProd_out, VertexCollection_out->size());
266  VertexCollection_out->push_back(thePV);
267  AVPFTauPV->setValue(iPFTau, VRef);
268  }
269  }
270  iEvent.put(std::move(VertexCollection_out),"PFTauPrimaryVertices");
271  iEvent.put(std::move(AVPFTauPV));
272 }
273 
void produce(edm::Event &, const edm::EventSetup &) override
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.cc:81
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< reco::PFTauDiscriminator > inputToken_
edm::EDGetTokenT< std::vector< reco::PFTau > > PFTauToken_
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
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
#define nullptr
edm::RefProd< PFTauCollection > PFTauRefProd
references to PFTau collection
Definition: PFTauFwd.h:15
uint16_t size_type
edm::EDGetTokenT< reco::VertexCollection > PVToken_
int iEvent
Definition: GenABIO.cc:230
Definition: Muon.py:1
edm::EDGetTokenT< std::vector< reco::Muon > > MuonToken_
std::auto_ptr< tau::RecoTauVertexAssociator > vertexAssociator_
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:167
std::auto_ptr< StringCutObjectSelector< reco::PFTau > > cut_
Definition: L1GtObject.h:30
std::vector< DiscCutPair * > DiscCutPairVec
fixed size matrix
HLT enums.
PFTauPrimaryVertexProducer(const edm::ParameterSet &iConfig)
T get() const
Definition: EventSetup.h:63
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.cc:76
def move(src, dest)
Definition: eostools.py:510
edm::EDGetTokenT< std::vector< reco::Electron > > ElectronToken_