Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-16 00:05:55

0001 #include "RecoTauTag/HLTProducers/interface/VertexFromTrackProducer.h"
0002 #include "FWCore/Framework/interface/MakerMacros.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0005 #include "DataFormats/Common/interface/Handle.h"
0006 #include "DataFormats/EgammaCandidates/interface/Electron.h"
0007 #include "DataFormats/Math/interface/Point3D.h"
0008 #include "DataFormats/Math/interface/Error.h"
0009 #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
0010 
0011 VertexFromTrackProducer::VertexFromTrackProducer(const edm::ParameterSet& conf)
0012     : trackToken(consumes<edm::View<reco::Track> >(conf.getParameter<edm::InputTag>("trackLabel"))),
0013       candidateToken(consumes<edm::View<reco::RecoCandidate> >(conf.getParameter<edm::InputTag>("trackLabel"))),
0014       triggerFilterElectronsSrc(consumes<trigger::TriggerFilterObjectWithRefs>(
0015           conf.getParameter<edm::InputTag>("triggerFilterElectronsSrc"))),
0016       triggerFilterMuonsSrc(
0017           consumes<trigger::TriggerFilterObjectWithRefs>(conf.getParameter<edm::InputTag>("triggerFilterMuonsSrc"))),
0018       vertexLabel(consumes<edm::View<reco::Vertex> >(conf.getParameter<edm::InputTag>("vertexLabel"))),
0019       beamSpotLabel(consumes<reco::BeamSpot>(conf.getParameter<edm::InputTag>("beamSpotLabel"))),
0020       fIsRecoCandidate(conf.getParameter<bool>("isRecoCandidate")),
0021       fUseBeamSpot(conf.getParameter<bool>("useBeamSpot")),
0022       fUseVertex(conf.getParameter<bool>("useVertex")),
0023       fUseTriggerFilterElectrons(conf.getParameter<bool>("useTriggerFilterElectrons")),
0024       fUseTriggerFilterMuons(conf.getParameter<bool>("useTriggerFilterMuons")),
0025       fVerbose(conf.getUntrackedParameter<bool>("verbose", false)) {
0026   edm::LogInfo("PVDebugInfo") << "Initializing  VertexFromTrackProducer"
0027                               << "\n";
0028 
0029   produces<reco::VertexCollection>();
0030 }
0031 
0032 void VertexFromTrackProducer::produce(edm::StreamID iStreamId,
0033                                       edm::Event& iEvent,
0034                                       const edm::EventSetup& iSetup) const {
0035   using namespace edm;
0036 
0037   std::unique_ptr<reco::VertexCollection> result(new reco::VertexCollection);
0038   reco::VertexCollection vColl;
0039 
0040   math::XYZPoint vertexPoint;
0041   bool vertexAvailable = false;
0042 
0043   // get the BeamSpot
0044   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
0045   iEvent.getByToken(beamSpotLabel, recoBeamSpotHandle);
0046   if (recoBeamSpotHandle.isValid()) {
0047     reco::BeamSpot beamSpot = *recoBeamSpotHandle;
0048     vertexPoint = beamSpot.position();
0049   } else {
0050     edm::LogError("UnusableBeamSpot") << "No beam spot found in Event";
0051   }
0052 
0053   if (fUseVertex) {
0054     // get the Vertex
0055     edm::Handle<edm::View<reco::Vertex> > recoVertexHandle;
0056     iEvent.getByToken(vertexLabel, recoVertexHandle);
0057     if ((recoVertexHandle.isValid()) && (!recoVertexHandle->empty())) {
0058       reco::Vertex vertex = recoVertexHandle->at(0);
0059       vertexPoint = vertex.position();
0060       vertexAvailable = true;
0061     } else {
0062       edm::LogInfo("UnusableVertex") << "No vertex found in Event, beam spot used instaed"
0063                                      << "\n";
0064     }
0065   }
0066 
0067   const reco::Track* track = nullptr;
0068   if (fIsRecoCandidate) {
0069     edm::Handle<edm::View<reco::RecoCandidate> > candidateHandle;
0070     iEvent.getByToken(candidateToken, candidateHandle);
0071     if ((candidateHandle.isValid()) && (!candidateHandle->empty())) {
0072       double maxpt = 0.;
0073       unsigned i_maxpt = 0;
0074       for (unsigned i = 0; i < candidateHandle->size(); ++i) {
0075         double pt = candidateHandle->ptrAt(i)->pt();
0076         if (pt > maxpt) {
0077           i_maxpt = i;
0078           maxpt = pt;
0079         }
0080       }
0081       track = dynamic_cast<const reco::Track*>(candidateHandle->ptrAt(i_maxpt)->bestTrack());
0082     }
0083   } else if (fUseTriggerFilterElectrons) {
0084     edm::Handle<trigger::TriggerFilterObjectWithRefs> triggerfilter;
0085     iEvent.getByToken(triggerFilterElectronsSrc, triggerfilter);
0086     std::vector<reco::ElectronRef> recocandidates;
0087     triggerfilter->getObjects(trigger::TriggerElectron, recocandidates);
0088     if ((!recocandidates.empty())) {
0089       double maxpt = 0.;
0090       unsigned i_maxpt = 0;
0091       for (unsigned i = 0; i < recocandidates.size(); ++i) {
0092         double pt = recocandidates.at(i)->pt();
0093         if (pt > maxpt) {
0094           i_maxpt = i;
0095           maxpt = pt;
0096         }
0097         track = dynamic_cast<const reco::Track*>(recocandidates.at(i_maxpt)->bestTrack());
0098       }
0099     }
0100   } else if (fUseTriggerFilterMuons) {
0101     edm::Handle<trigger::TriggerFilterObjectWithRefs> triggerfilter;
0102     iEvent.getByToken(triggerFilterMuonsSrc, triggerfilter);
0103     std::vector<reco::RecoChargedCandidateRef> recocandidates;
0104     triggerfilter->getObjects(trigger::TriggerMuon, recocandidates);
0105     if ((!recocandidates.empty())) {
0106       double maxpt = 0.;
0107       unsigned i_maxpt = 0;
0108       for (unsigned i = 0; i < recocandidates.size(); ++i) {
0109         double pt = recocandidates.at(i)->pt();
0110         if (pt > maxpt) {
0111           i_maxpt = i;
0112           maxpt = pt;
0113         }
0114         track = dynamic_cast<const reco::Track*>(recocandidates.at(i_maxpt)->bestTrack());
0115       }
0116     }
0117   } else {
0118     edm::Handle<edm::View<reco::Track> > trackHandle;
0119     iEvent.getByToken(trackToken, trackHandle);
0120     if ((trackHandle.isValid()) && (!trackHandle->empty())) {
0121       double maxpt = 0.;
0122       unsigned i_maxpt = 0;
0123       for (unsigned i = 0; i < trackHandle->size(); ++i) {
0124         double pt = trackHandle->ptrAt(i)->pt();
0125         if (pt > maxpt) {
0126           i_maxpt = i;
0127           maxpt = pt;
0128         }
0129       }
0130       track = dynamic_cast<const reco::Track*>(&*trackHandle->ptrAt(i_maxpt));
0131     }
0132   }
0133 
0134   if (track) {
0135     if (fUseBeamSpot || (fUseVertex && vertexAvailable)) {
0136       vertexPoint.SetZ(vertexPoint.z() + track->dz(vertexPoint));
0137     } else {
0138       vertexPoint.SetZ(track->vz());
0139     }
0140   }
0141   math::Error<3>::type noErrors;
0142   reco::Vertex v(vertexPoint, noErrors);
0143   vColl.push_back(v);
0144 
0145   // provide beamspot or primary vertex if no candidate found
0146   //if(vColl.size()==0)
0147   //{
0148   //    math::Error<3>::type noErrors;
0149   //    reco::Vertex v(vertexPoint, noErrors);
0150   //    vColl.push_back(v);
0151   //}
0152 
0153   if (fVerbose) {
0154     int ivtx = 0;
0155     edm::LogInfo("PVDebugInfo") << "Vertices by VertexFromTrackProducer: \n";
0156     for (reco::VertexCollection::const_iterator v = vColl.begin(); v != vColl.end(); ++v) {
0157       edm::LogInfo("PVDebugInfo") << "\t"
0158                                   << "recvtx " << ivtx++ << " x " << std::setw(6) << v->position().x() << " dx "
0159                                   << std::setw(6) << v->xError() << " y " << std::setw(6) << v->position().y() << " dy "
0160                                   << std::setw(6) << v->yError() << " z " << std::setw(6) << v->position().z() << " dz "
0161                                   << std::setw(6) << v->zError() << " \n ";
0162     }
0163   }
0164 
0165   *result = vColl;
0166   iEvent.put(std::move(result));
0167 }
0168 
0169 void VertexFromTrackProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0170   edm::ParameterSetDescription desc;
0171 
0172   desc.add<bool>("isRecoCandidate", false)
0173       ->setComment(
0174           "If isRecoCandidate=True \"trackLabel\" is used and assumed to be collection of candidates.\nOtherwise it is "
0175           "assumed that \"trackLabel\" is collection of tracks and is used when useTriggerFilterElectrons=False and "
0176           "useTriggerFilterMuons=False");
0177   desc.add<edm::InputTag>("trackLabel", edm::InputTag("hltL3MuonCandidates"))
0178       ->setComment("Collection of tracks or candidates");
0179   desc.add<bool>("useTriggerFilterElectrons", false)
0180       ->setComment("Use leading electron passing \"triggerFilterElectronsSrc\" filter to determine z vertex position");
0181   desc.add<edm::InputTag>("triggerFilterElectronsSrc",
0182                           edm::InputTag("hltEle20CaloIdVTCaloIsoTTrkIdTTrkIsoL1JetTrackIsoFilter"))
0183       ->setComment("Name of electron filter");
0184   desc.add<bool>("useTriggerFilterMuons", true)
0185       ->setComment("Use leading muon passing \"triggerFilterMuonsSrc\" filter to determine z vertex position");
0186   desc.add<edm::InputTag>("triggerFilterMuonsSrc", edm::InputTag("hltSingleMuIsoL3IsoFiltered15"))
0187       ->setComment("Name of muon filter");
0188   desc.add<bool>("useBeamSpot", true)->setComment("Use beam spot for x/y vertex position");
0189   desc.add<edm::InputTag>("beamSpotLabel", edm::InputTag("hltOnlineBeamSpot"))->setComment("Beamspot collection");
0190   desc.add<bool>("useVertex", true)
0191       ->setComment("Use vertex for x/y vertex position (beam spot is used when PV does not exit)");
0192   desc.add<edm::InputTag>("vertexLabel", edm::InputTag("hltPixelVertices"))->setComment("Vertex collection");
0193 
0194   desc.addUntracked<bool>("verbose", false)->setComment("Switch on/off verbosity");
0195   descriptions.setComment(
0196       "This module produces vertex with z-coordinate determined with the highest-Pt lepton track and x/y-coordinates "
0197       "taken from BeamSpot/Vertex");
0198   descriptions.add("hltVertexFromTrackProducer", desc);
0199 }
0200 
0201 #include "FWCore/Framework/interface/MakerMacros.h"
0202 DEFINE_FWK_MODULE(VertexFromTrackProducer);