CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/PhysicsTools/PatAlgos/src/VertexingHelper.cc

Go to the documentation of this file.
00001 #include "PhysicsTools/PatAlgos/interface/VertexingHelper.h"
00002 #include <algorithm>
00003 
00004 #include <iostream> 
00005 
00006 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
00007 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00008 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00009 #include "DataFormats/RecoCandidate/interface/RecoCandidate.h"
00010 
00011 pat::helper::VertexingHelper::VertexingHelper(const edm::ParameterSet &iConfig) 
00012 {
00013     if (!iConfig.empty()) { 
00014         enabled_ = true;
00015         if ( iConfig.existsAs<edm::InputTag>("vertexAssociations") == iConfig.existsAs<edm::InputTag>("vertices")) {
00016             throw cms::Exception("Configuration") <<
00017                 "VertexingHelper: you must configure either 'vertices' (to produce associations) or 'vertexAssociations' (to read them from disk), " <<
00018                 "you can't specify both, nor you can specify none!\n";
00019         }
00020             
00021         if (iConfig.existsAs<edm::InputTag>("vertexAssociations")) {
00022             playback_ = true;
00023             vertexAssociations_ = iConfig.getParameter<edm::InputTag>("vertexAssociations");
00024         }
00025         if (iConfig.existsAs<edm::InputTag>("vertices")) { // vertex have been specified, so run on the fly
00026             playback_ = false;
00027             vertices_ = iConfig.getParameter<edm::InputTag>("vertices");
00028             // ------ MODE ------------------
00029             useTracks_ = iConfig.getParameter<bool>("useTracks");
00030             // ------ CUTS (fully optional) ------------------
00031         }
00032         assoSelector_ = reco::modules::make<pat::VertexAssociationSelector>(iConfig);
00033     } else {
00034         enabled_ = false;
00035     }
00036 }
00037 
00038 void
00039 pat::helper::VertexingHelper::newEvent(const edm::Event &iEvent) {
00040     if (playback_) {
00041         iEvent.getByLabel(vertexAssociations_, vertexAssoMap_);
00042     } else {
00043         iEvent.getByLabel(vertices_, vertexHandle_);
00044     }
00045 }
00046 
00047 void
00048 pat::helper::VertexingHelper::newEvent(const edm::Event &iEvent, const edm::EventSetup & iSetup) {
00049     newEvent(iEvent);
00050     if (!playback_) iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", ttBuilder_);
00051 }
00052 
00053 
00054 pat::VertexAssociation
00055 pat::helper::VertexingHelper::associate(const reco::Candidate &c) const {
00056     if (playback_) throw cms::Exception("Configuration") << "VertexingHelper: if this module was configured to read associations from the event," << 
00057                                                             " you must use 'operator()' passing a candidate ref, and not 'associate()' directly!\n";
00058 
00059     reco::VertexCollection::const_iterator vtx, end;
00060     size_t ivtx;
00061     reco::TrackBaseRef tk; 
00062     reco::TransientTrack tt;
00063     if (useTracks_) { 
00064         if (!ttBuilder_.isValid()) throw cms::Exception("Configuration") << "VertexingHelper: If you use 'useTracks', you must call newEvent(iEvent,iSetup)!\n";
00065         tk = getTrack_(c); 
00066         if (tk.isNull()) return pat::VertexAssociation(); 
00067         tt = ttBuilder_->build(*tk);
00068     }
00069     for (vtx = vertexHandle_->begin(), end = vertexHandle_->end(), ivtx = 0; vtx != end; ++vtx, ++ivtx) {
00070         pat::VertexAssociation association(reco::VertexRef(vertexHandle_, ivtx), tk);
00071         if (useTracks_ == false) {
00072             association.setDistances(c.vertex(), vtx->position(), vtx->error());
00073         } else {
00074             GlobalPoint vtxGP(vtx->x(), vtx->y(), vtx->z()); // need to convert XYZPoint to GlobalPoint
00075             TrajectoryStateClosestToPoint tscp = tt.trajectoryStateClosestToPoint(vtxGP); 
00076             GlobalPoint          trackPos = tscp.theState().position();
00077             AlgebraicSymMatrix33 trackErr = tscp.theState().cartesianError().matrix().Sub<AlgebraicSymMatrix33>(0,0);
00078             association.setDistances(trackPos, vtx->position(), trackErr + vtx->error());
00079         }
00080         if (assoSelector_(association)) return association;
00081     }
00082     return pat::VertexAssociation();
00083 }
00084 
00085 reco::TrackBaseRef pat::helper::VertexingHelper::getTrack_(const reco::Candidate &c) const  {
00086     const reco::RecoCandidate   *rc  = dynamic_cast<const reco::RecoCandidate *>(&c);
00087     if (rc  != 0)  { return rc->bestTrackRef(); }
00088     const reco::PFCandidate *pfc = dynamic_cast<const reco::PFCandidate *>(&c);
00089     if (pfc != 0) { return reco::TrackBaseRef(pfc->trackRef()); }
00090 
00091     return reco::TrackBaseRef();
00092 }