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:
97  DiscCutPairVec discriminators_;
98  std::auto_ptr<StringCutObjectSelector<reco::PFTau> > cut_;
99  std::auto_ptr<tau::RecoTauVertexAssociator> vertexAssociator_;
100 };
101 
103  PFTauTag_(iConfig.getParameter<edm::InputTag>("PFTauTag")),
104  PFTauToken_(consumes<std::vector<reco::PFTau> >(PFTauTag_)),
105  ElectronTag_(iConfig.getParameter<edm::InputTag>("ElectronTag")),
106  ElectronToken_(consumes<std::vector<reco::Electron> >(ElectronTag_)),
107  MuonTag_(iConfig.getParameter<edm::InputTag>("MuonTag")),
108  MuonToken_(consumes<std::vector<reco::Muon> >(MuonTag_)),
109  PVTag_(iConfig.getParameter<edm::InputTag>("PVTag")),
110  PVToken_(consumes<reco::VertexCollection>(PVTag_)),
111  beamSpotTag_(iConfig.getParameter<edm::InputTag>("beamSpot")),
112  beamSpotToken_(consumes<reco::BeamSpot>(beamSpotTag_)),
113  TrackCollectionTag_(iConfig.getParameter<edm::InputTag>("TrackCollectionTag")),
114  TrackCollectionToken_(consumes<reco::TrackCollection>(TrackCollectionTag_)),
115  Algorithm_(iConfig.getParameter<int>("Algorithm")),
116  qualityCutsPSet_(iConfig.getParameter<edm::ParameterSet>("qualityCuts")),
117  useBeamSpot_(iConfig.getParameter<bool>("useBeamSpot")),
118  useSelectedTaus_(iConfig.getParameter<bool>("useSelectedTaus")),
119  RemoveMuonTracks_(iConfig.getParameter<bool>("RemoveMuonTracks")),
120  RemoveElectronTracks_(iConfig.getParameter<bool>("RemoveElectronTracks"))
121 {
123  std::vector<edm::ParameterSet> discriminators =iConfig.getParameter<std::vector<edm::ParameterSet> >("discriminators");
124  // Build each of our cuts
125  BOOST_FOREACH(const edm::ParameterSet &pset, discriminators) {
126  DiscCutPair* newCut = new DiscCutPair();
127  newCut->inputToken_ =consumes<reco::PFTauDiscriminator>(pset.getParameter<edm::InputTag>("discriminator"));
128 
129  if ( pset.existsAs<std::string>("selectionCut") ) newCut->cutFormula_ = new TFormula("selectionCut", pset.getParameter<std::string>("selectionCut").data());
130  else newCut->cut_ = pset.getParameter<double>("selectionCut");
131  discriminators_.push_back(newCut);
132  }
133  // Build a string cut if desired
134  if (iConfig.exists("cut")) cut_.reset(new StringCutObjectSelector<reco::PFTau>(iConfig.getParameter<std::string>( "cut" )));
136  produces<edm::AssociationVector<PFTauRefProd, std::vector<reco::VertexRef> > >();
137  produces<VertexCollection>("PFTauPrimaryVertices");
138 
140 }
141 
143 
145  // Obtain Collections
146  edm::ESHandle<TransientTrackBuilder> transTrackBuilder;
147  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",transTrackBuilder);
148 
150  iEvent.getByToken(PFTauToken_,Tau);
151 
153  iEvent.getByToken(ElectronToken_,Electron);
154 
156  iEvent.getByToken(MuonToken_,Mu);
157 
159  iEvent.getByToken(PVToken_,PV);
160 
162  iEvent.getByToken(beamSpotToken_,beamSpot);
163 
165  iEvent.getByToken(TrackCollectionToken_,trackCollection);
166 
167  // Set Association Map
168  auto AVPFTauPV = std::make_unique<edm::AssociationVector<PFTauRefProd, std::vector<reco::VertexRef>>>(PFTauRefProd(Tau));
169  auto VertexCollection_out = std::make_unique<VertexCollection>();
170  reco::VertexRefProd VertexRefProd_out = iEvent.getRefBeforePut<reco::VertexCollection>("PFTauPrimaryVertices");
171 
172  // Load each discriminator
173  BOOST_FOREACH(DiscCutPair *disc, discriminators_) {
175  iEvent.getByToken(disc->inputToken_, discr);
176  disc->discr_ = &(*discr);
177  }
178 
179  // Set event for VerexAssociator if needed
181  vertexAssociator_->setEvent(iEvent);
182 
183  // For each Tau Run Algorithim
184  if(Tau.isValid()){
185  for(reco::PFTauCollection::size_type iPFTau = 0; iPFTau < Tau->size(); iPFTau++) {
186  reco::PFTauRef tau(Tau, iPFTau);
187  reco::Vertex thePV;
188  if(useInputPV==Algorithm_){
189  thePV =(*( vertexAssociator_->associatedVertex(*tau)));
190  }
191  else if(useFontPV==Algorithm_){
192  thePV=PV->front();
193  }
195  // Check if it passed all the discrimiantors
196  bool passed(true);
197  BOOST_FOREACH(const DiscCutPair* disc, discriminators_) {
198  // Check this discriminator passes
199  bool passedDisc = true;
200  if ( disc->cutFormula_ )passedDisc = (disc->cutFormula_->Eval((*disc->discr_)[tau]) > 0.5);
201  else passedDisc = ((*disc->discr_)[tau] > disc->cut_);
202  if ( !passedDisc ){passed = false; break;}
203  }
204  if (passed && cut_.get()){passed = (*cut_)(*tau);}
205  if (passed){
206  std::vector<reco::TrackBaseRef> SignalTracks;
207  for(reco::PFTauCollection::size_type jPFTau = 0; jPFTau < Tau->size(); jPFTau++) {
208  if(useSelectedTaus_ || iPFTau==jPFTau){
209  reco::PFTauRef RefPFTau(Tau, jPFTau);
211  // Get tracks from PFTau daugthers
212  const std::vector<edm::Ptr<reco::PFCandidate> > cands = RefPFTau->signalPFChargedHadrCands();
213  for (std::vector<edm::Ptr<reco::PFCandidate> >::const_iterator iter = cands.begin(); iter!=cands.end(); iter++){
214  if(iter->get()->trackRef().isNonnull()) SignalTracks.push_back(reco::TrackBaseRef(iter->get()->trackRef()));
215  else if(iter->get()->gsfTrackRef().isNonnull()){SignalTracks.push_back(reco::TrackBaseRef(((iter)->get()->gsfTrackRef())));}
216  }
217  }
218  }
219  // Get Muon tracks
220  if(RemoveMuonTracks_){
221 
222  if(Mu.isValid()) {
223  for(reco::MuonCollection::size_type iMuon = 0; iMuon< Mu->size(); iMuon++){
224  reco::MuonRef RefMuon(Mu, iMuon);
225  if(RefMuon->track().isNonnull()) SignalTracks.push_back(reco::TrackBaseRef(RefMuon->track()));
226  }
227  }
228  }
229  // Get Electron Tracks
231  if(Electron.isValid()) {
232  for(reco::ElectronCollection::size_type iElectron = 0; iElectron<Electron->size(); iElectron++){
233  reco::ElectronRef RefElectron(Electron, iElectron);
234  if(RefElectron->track().isNonnull()) SignalTracks.push_back(reco::TrackBaseRef(RefElectron->track()));
235  }
236  }
237  }
239  // Get Non-Tau tracks
240  reco::TrackCollection nonTauTracks;
241 // if (trackCollection.isValid()) {
242 // // remove tau tracks and only tracks associated with the vertex
243 // unsigned int idx = 0;
244 // for (reco::TrackCollection::const_iterator iTrk = trackCollection->begin(); iTrk != trackCollection->end(); ++iTrk, idx++) {
245 // reco::TrackRef tmpRef(trackCollection, idx);
246 // reco::TrackRef tmpRefForBase=tmpRef;
247 // bool isSigTrk = false;
248 // bool fromVertex=false;
249 // for (unsigned int sigTrk = 0; sigTrk < SignalTracks.size(); sigTrk++) {
250 // if (reco::TrackBaseRef(tmpRefForBase)==SignalTracks.at(sigTrk)){isSigTrk = true; break;}
251 // }
252 // if (!isSigTrk) nonSigTracks.push_back(*iTrk);
253 // }
254 // }
255 
256  for(std::vector<reco::TrackBaseRef>::const_iterator vtxTrkRef=thePV.tracks_begin();vtxTrkRef<thePV.tracks_end();vtxTrkRef++){
257  bool matched = false;
258  for (unsigned int sigTrk = 0; sigTrk < SignalTracks.size(); sigTrk++) {
259  if ( (*vtxTrkRef) == SignalTracks[sigTrk] ) {
260  matched = true;
261  }
262  }
263  if ( !matched ) nonTauTracks.push_back(**vtxTrkRef);
264  }
266  // Refit the vertex
267  TransientVertex transVtx;
268  std::vector<reco::TransientTrack> transTracks;
269  for (reco::TrackCollection::iterator iter=nonTauTracks.begin(); iter!=nonTauTracks.end(); ++iter){
270  transTracks.push_back(transTrackBuilder->build(*iter));
271  }
272  bool FitOk(true);
273  if ( transTracks.size() >= 2 ) {
275  avf.setWeightThreshold(0.1); //weight per track. allow almost every fit, else --> exception
276  try {
277  if ( !useBeamSpot_ ){
278  transVtx = avf.vertex(transTracks);
279  } else {
280  transVtx = avf.vertex(transTracks, *beamSpot);
281  }
282  } catch (...) {
283  FitOk = false;
284  }
285  } else FitOk = false;
286  if ( FitOk ) thePV = transVtx;
287  }
288  VertexRef VRef = reco::VertexRef(VertexRefProd_out, VertexCollection_out->size());
289  VertexCollection_out->push_back(thePV);
290  AVPFTauPV->setValue(iPFTau, VRef);
291  }
292  }
293  iEvent.put(std::move(VertexCollection_out),"PFTauPrimaryVertices");
294  iEvent.put(std::move(AVPFTauPV));
295 }
296 
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:127
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:508
#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_
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
std::auto_ptr< tau::RecoTauVertexAssociator > vertexAssociator_
edm::EDGetTokenT< reco::TrackCollection > TrackCollectionToken_
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:147
std::auto_ptr< StringCutObjectSelector< reco::PFTau > > cut_
Definition: L1GtObject.h:30
const T & get() const
Definition: EventSetup.h:55
std::vector< DiscCutPair * > DiscCutPairVec
fixed size matrix
HLT enums.
PFTauPrimaryVertexProducer(const edm::ParameterSet &iConfig)
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_