CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoTauTag/HLTProducers/src/VertexFromTrackProducer.cc

Go to the documentation of this file.
00001 #include "RecoTauTag/HLTProducers/interface/VertexFromTrackProducer.h"
00002 
00003 #include "DataFormats/VertexReco/interface/Vertex.h"
00004 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00005 #include "DataFormats/TrackReco/interface/Track.h"
00006 #include "DataFormats/Common/interface/Handle.h"
00007 #include "FWCore/Framework/interface/MakerMacros.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 
00010 #include "FWCore/Framework/interface/ESHandle.h"
00011 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00012 
00013 #include "DataFormats/Math/interface/Point3D.h"
00014 #include "DataFormats/Math/interface/Error.h"
00015 #include "DataFormats/RecoCandidate/interface/RecoCandidate.h"
00016 #include "DataFormats/HLTReco/interface/TriggerFilterObjectWithRefs.h"
00017 #include "DataFormats/EgammaCandidates/interface/Electron.h"
00018 #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
00019 
00020 //using namespace reco;
00021 
00022 //
00023 // constants, enums and typedefs
00024 //
00025 
00026 //
00027 // static data member definitions
00028 //
00029 
00030 //
00031 // constructors and destructor
00032 //
00033 VertexFromTrackProducer::VertexFromTrackProducer(const edm::ParameterSet& conf)
00034   : theConfig(conf)
00035 {
00036   edm::LogInfo("PVDebugInfo") 
00037     << "Initializing  VertexFromTrackProducer" << "\n";
00038   fVerbose = conf.getUntrackedParameter<bool>("verbose", false);
00039   trackLabel = conf.getParameter<edm::InputTag>("trackLabel");
00040   fIsRecoCandidate = conf.getParameter<bool>("isRecoCandidate");
00041   fUseBeamSpot = conf.getParameter<bool>("useBeamSpot");
00042   fUseVertex = conf.getParameter<bool>("useVertex");
00043   fUseTriggerFilterElectrons = conf.getParameter<bool>("useTriggerFilterElectrons");
00044   fUseTriggerFilterMuons = conf.getParameter<bool>("useTriggerFilterMuons");
00045   triggerFilterElectronsSrc = conf.getParameter<edm::InputTag>("triggerFilterElectronsSrc");
00046   triggerFilterMuonsSrc = conf.getParameter<edm::InputTag>("triggerFilterMuonsSrc");
00047   vertexLabel = conf.getParameter<edm::InputTag>("vertexLabel");
00048   beamSpotLabel = conf.getParameter<edm::InputTag>("beamSpotLabel");
00049  
00050   produces<reco::VertexCollection>();
00051 
00052 }
00053 
00054 
00055 VertexFromTrackProducer::~VertexFromTrackProducer() {}
00056 
00057 //
00058 // member functions
00059 //
00060 
00061 // ------------ method called to produce the data  ------------
00062 void
00063 VertexFromTrackProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00064 {
00065   using namespace edm;
00066 
00067   std::auto_ptr<reco::VertexCollection> result(new reco::VertexCollection);
00068   reco::VertexCollection vColl;
00069 
00070   math::XYZPoint vertexPoint;
00071   bool vertexAvailable = false;
00072 
00073   // get the BeamSpot
00074   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00075   iEvent.getByLabel(beamSpotLabel,recoBeamSpotHandle);
00076   if (recoBeamSpotHandle.isValid()){
00077     reco::BeamSpot beamSpot = *recoBeamSpotHandle;
00078     vertexPoint = beamSpot.position();
00079   }else{
00080     edm::LogError("UnusableBeamSpot") << "No beam spot found in Event";
00081   }
00082 
00083   if(fUseVertex)
00084   {
00085     // get the Vertex
00086     edm::Handle<edm::View<reco::Vertex> > recoVertexHandle;
00087     iEvent.getByLabel(vertexLabel,recoVertexHandle);
00088     if ((recoVertexHandle.isValid()) && (recoVertexHandle->size()>0)){
00089       reco::Vertex vertex = recoVertexHandle->at(0);
00090       vertexPoint = vertex.position();
00091       vertexAvailable = true;
00092     }else {
00093       edm::LogInfo("UnusableVertex")
00094         << "No vertex found in Event, beam spot used instaed" << "\n";
00095     }
00096   }
00097 
00098   const reco::Track* track = 0;
00099   if(fIsRecoCandidate)
00100   {
00101     edm::Handle<edm::View<reco::RecoCandidate> > candidateHandle;
00102     iEvent.getByLabel(trackLabel, candidateHandle);
00103     if ((candidateHandle.isValid())&&(candidateHandle->size()>0)){
00104       double maxpt=0.;
00105       unsigned i_maxpt=0;
00106       for (unsigned i = 0; i < candidateHandle->size(); ++i) {
00107         double pt=candidateHandle->ptrAt(i)->pt();
00108         if(pt>maxpt)
00109         {
00110           i_maxpt=i;
00111           maxpt=pt;
00112         }
00113       }
00114       track = dynamic_cast<const reco::Track*>(candidateHandle->ptrAt(i_maxpt)->bestTrack());
00115     }
00116   } 
00117   else if(fUseTriggerFilterElectrons) {
00118     edm::Handle<trigger::TriggerFilterObjectWithRefs> triggerfilter;
00119     iEvent.getByLabel(triggerFilterElectronsSrc, triggerfilter);
00120     std::vector<reco::ElectronRef> recocandidates;
00121     triggerfilter->getObjects(trigger::TriggerElectron,recocandidates);
00122     if ((recocandidates.size()>0)){
00123       double maxpt=0.;
00124       unsigned i_maxpt=0;
00125       for (unsigned i = 0; i < recocandidates.size(); ++i) {
00126         double pt=recocandidates.at(i)->pt();
00127         if(pt>maxpt) 
00128           {
00129             i_maxpt=i;
00130             maxpt=pt;
00131           }
00132         track = dynamic_cast<const reco::Track*>(recocandidates.at(i_maxpt)->bestTrack());
00133       }
00134     }
00135   }  
00136   else if(fUseTriggerFilterMuons) {
00137     edm::Handle<trigger::TriggerFilterObjectWithRefs> triggerfilter;
00138     iEvent.getByLabel(triggerFilterMuonsSrc, triggerfilter);
00139     std::vector<reco::RecoChargedCandidateRef> recocandidates;
00140     triggerfilter->getObjects(trigger::TriggerMuon,recocandidates);
00141     if ((recocandidates.size()>0)){
00142       double maxpt=0.;
00143       unsigned i_maxpt=0;
00144       for (unsigned i = 0; i < recocandidates.size(); ++i) {
00145         double pt=recocandidates.at(i)->pt();
00146         if(pt>maxpt) 
00147           {
00148             i_maxpt=i;
00149             maxpt=pt;
00150           }
00151         track = dynamic_cast<const reco::Track*>(recocandidates.at(i_maxpt)->bestTrack());
00152       }
00153     }
00154   }
00155   else {
00156     edm::Handle<edm::View<reco::Track> > trackHandle;
00157     iEvent.getByLabel(trackLabel, trackHandle);
00158     if ((trackHandle.isValid())&&(trackHandle->size()>0)){
00159       double maxpt=0.;
00160       unsigned i_maxpt=0;
00161       for (unsigned i = 0; i < trackHandle->size(); ++i) {
00162         double pt=trackHandle->ptrAt(i)->pt();
00163         if(pt>maxpt)
00164         {
00165           i_maxpt=i;
00166           maxpt=pt;
00167         }
00168       }
00169       track = dynamic_cast<const reco::Track*>(&*trackHandle->ptrAt(i_maxpt));
00170     }
00171   }
00172 
00173   if(track) {
00174     if(fUseBeamSpot || (fUseVertex && vertexAvailable) ) {
00175       vertexPoint.SetZ(vertexPoint.z()+track->dz(vertexPoint));
00176     }
00177     else {
00178       vertexPoint.SetZ(track->vz());
00179     }
00180   }
00181   math::Error<3>::type noErrors;
00182   reco::Vertex v(vertexPoint, noErrors);
00183   vColl.push_back(v);
00184 
00185   // provide beamspot or primary vertex if no candidate found
00186   //if(vColl.size()==0)
00187   //{
00188   //    math::Error<3>::type noErrors;
00189   //    reco::Vertex v(vertexPoint, noErrors);
00190   //    vColl.push_back(v);
00191   //}
00192 
00193   if(fVerbose){
00194     int ivtx=0;
00195     for(reco::VertexCollection::const_iterator v=vColl.begin(); 
00196         v!=vColl.end(); ++v){
00197       std::cout << "recvtx "<< ivtx++ 
00198                 << " x "  << std::setw(6) << v->position().x() 
00199                 << " dx " << std::setw(6) << v->xError()
00200                 << " y "  << std::setw(6) << v->position().y() 
00201                 << " dy " << std::setw(6) << v->yError()
00202                 << " z "  << std::setw(6) << v->position().z() 
00203                 << " dz " << std::setw(6) << v->zError()
00204                 << std::endl;
00205     }
00206   }
00207 
00208   
00209   *result = vColl;
00210   iEvent.put(result);
00211   
00212 }