Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:47

0001 #ifndef PhysicsTools_PatAlgos_interface_VertexingHelper_h
0002 #define PhysicsTools_PatAlgos_interface_VertexingHelper_h
0003 /**
0004   \class    pat::helper::VertexingHelper VertexingHelper.h "PhysicsTools/PatAlgos/interface/VertexingHelper.h"
0005   \brief    Produces and/or checks pat::VertexAssociation's
0006 
0007    The VertexingHelper produces pat::VertexAssociation, or reads them from the event,
0008    and can use them to select if a candidate is good or not.
0009 
0010   \author   Giovanni Petrucciani
0011   \version  $Id: VertexingHelper.h,v 1.3 2008/06/06 14:13:40 gpetrucc Exp $
0012 */
0013 
0014 #include "DataFormats/PatCandidates/interface/Vertexing.h"
0015 #include "DataFormats/Common/interface/ValueMap.h"
0016 #include "PhysicsTools/PatUtils/interface/VertexAssociationSelector.h"
0017 
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/Framework/interface/Event.h"
0020 #include "FWCore/Framework/interface/ConsumesCollector.h"
0021 
0022 #include "FWCore/Framework/interface/EventSetup.h"
0023 #include "FWCore/Framework/interface/ESHandle.h"
0024 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
0025 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
0026 
0027 #include "PhysicsTools/UtilAlgos/interface/ParameterAdapter.h"
0028 namespace reco {
0029   namespace modules {
0030     /// Helper struct to convert from ParameterSet to ElectronSelection
0031     template <>
0032     struct ParameterAdapter<pat::VertexAssociationSelector> {
0033       static pat::VertexAssociationSelector make(const edm::ParameterSet &iConfig) {
0034         pat::VertexAssociationSelector::Config assoconf;
0035         if (iConfig.existsAs<double>("deltaZ"))
0036           assoconf.dZ = iConfig.getParameter<double>("deltaZ");
0037         if (iConfig.existsAs<double>("deltaR"))
0038           assoconf.dR = iConfig.getParameter<double>("deltaR");
0039         if (iConfig.existsAs<double>("sigmasZ"))
0040           assoconf.sigmasZ = iConfig.getParameter<double>("sigmasZ");
0041         if (iConfig.existsAs<double>("sigmasR"))
0042           assoconf.sigmasR = iConfig.getParameter<double>("sigmasR");
0043         return pat::VertexAssociationSelector(assoconf);
0044       }
0045     };
0046   }  // namespace modules
0047 }  // namespace reco
0048 
0049 namespace pat {
0050   namespace helper {
0051     class VertexingHelper {
0052     public:
0053       VertexingHelper() : enabled_(false) {}
0054       VertexingHelper(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC);
0055 
0056       /// returns true if this was given a non dummy configuration
0057       bool enabled() const { return enabled_; }
0058 
0059       /// To be called for each new event, reads in the vertex collection
0060       void newEvent(const edm::Event &event);
0061 
0062       /// To be called for each new event, reads in the vertex collection and the tracking info
0063       /// You need this if 'useTrack' is true
0064       void newEvent(const edm::Event &event, const edm::EventSetup &setup);
0065 
0066       /// Return true if this candidate is associated to a valid vertex
0067       /// AnyCandRef should be a Ref<>, RefToBase<> or Ptr to a Candidate object
0068       template <typename AnyCandRef>
0069       pat::VertexAssociation operator()(const AnyCandRef &) const;
0070 
0071     private:
0072       /// true if it has non null configuration
0073       bool enabled_;
0074 
0075       /// true if it's just reading the associations from the event
0076       bool playback_;
0077 
0078       /// selector of associations
0079       pat::VertexAssociationSelector assoSelector_;
0080 
0081       //-------- Tools for production of vertex associations -------
0082       edm::EDGetTokenT<reco::VertexCollection> verticesToken_;
0083       edm::Handle<reco::VertexCollection> vertexHandle_;
0084       /// use tracks inside candidates
0085       bool useTracks_;
0086       edm::ESGetToken<TransientTrackBuilder, TransientTrackRecord> ttToken_;
0087       edm::ESHandle<TransientTrackBuilder> ttBuilder_;
0088 
0089       //--------- Tools for reading vertex associations (playback mode) -----
0090       edm::EDGetTokenT<edm::ValueMap<pat::VertexAssociation> > vertexAssociationsToken_;
0091       edm::Handle<edm::ValueMap<pat::VertexAssociation> > vertexAssoMap_;
0092 
0093       /// Get out the track from the Candidate / RecoCandidate / PFCandidate
0094       reco::TrackBaseRef getTrack_(const reco::Candidate &c) const;
0095 
0096       /// Try to associated this candidate to a vertex.
0097       /// If no association is found passing all cuts, return a null association
0098       pat::VertexAssociation associate(const reco::Candidate &) const;
0099 
0100     };  // class
0101 
0102     template <typename AnyCandRef>
0103     pat::VertexAssociation pat::helper::VertexingHelper::operator()(const AnyCandRef &cand) const {
0104       if (playback_) {
0105         const pat::VertexAssociation &assoc = (*vertexAssoMap_)[cand];
0106         return assoSelector_(assoc) ? assoc : pat::VertexAssociation();
0107       } else {
0108         return associate(*cand);
0109       }
0110     }
0111 
0112   }  // namespace helper
0113 }  // namespace pat
0114 
0115 #endif