Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /**
0002   \class    pat::PATVertexAssociationProducer PATVertexAssociationProducer.h "PhysicsTools/PatAlgos/interface/PATVertexAssociationProducer.h"
0003   \brief    Produces VertexAssociation and a ValueMap to the originating
0004             reco jets
0005 
0006    The PATVertexAssociationProducer produces a set of vertex associations for one or more
0007    collection of Candidates, and saves them in a ValueMap in the event.
0008 
0009    These can be retrieved in PAT Layer 1 to be embedded in PAT Objects
0010 
0011   \author   Giovanni Petrucciani
0012   \version  $Id: VertexAssociationProducer.cc,v 1.2 2010/02/20 21:00:29 wmtan Exp $
0013 */
0014 
0015 #include "FWCore/Framework/interface/stream/EDProducer.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/Utilities/interface/InputTag.h"
0019 
0020 #include "DataFormats/Common/interface/ValueMap.h"
0021 #include "DataFormats/Common/interface/View.h"
0022 #include "DataFormats/PatCandidates/interface/Vertexing.h"
0023 #include "PhysicsTools/PatAlgos/interface/VertexingHelper.h"
0024 
0025 namespace pat {
0026 
0027   class PATVertexAssociationProducer : public edm::stream::EDProducer<> {
0028     typedef edm::ValueMap<pat::VertexAssociation> VertexAssociationMap;
0029 
0030   public:
0031     explicit PATVertexAssociationProducer(const edm::ParameterSet& iConfig);
0032     ~PATVertexAssociationProducer() override;
0033 
0034     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0035 
0036   private:
0037     typedef std::vector<edm::InputTag> VInputTag;
0038     // configurables
0039     std::vector<edm::InputTag> particles_;
0040     std::vector<edm::EDGetTokenT<edm::View<reco::Candidate> > > particlesTokens_;
0041     pat::helper::VertexingHelper vertexing_;
0042   };
0043 
0044 }  // namespace pat
0045 
0046 using pat::PATVertexAssociationProducer;
0047 
0048 PATVertexAssociationProducer::PATVertexAssociationProducer(const edm::ParameterSet& iConfig)
0049     : particles_(iConfig.existsAs<VInputTag>("candidates")
0050                      ?  // if it's a VInputTag
0051                      iConfig.getParameter<VInputTag>("candidates")
0052                      : VInputTag(1, iConfig.getParameter<edm::InputTag>("candidates"))),
0053       vertexing_(iConfig, consumesCollector()) {
0054   for (VInputTag::const_iterator it = particles_.begin(), end = particles_.end(); it != end; ++it) {
0055     particlesTokens_.push_back(consumes<edm::View<reco::Candidate> >(*it));
0056   }
0057   produces<VertexAssociationMap>();
0058 }
0059 
0060 PATVertexAssociationProducer::~PATVertexAssociationProducer() {}
0061 
0062 void PATVertexAssociationProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0063   using namespace edm;
0064   using namespace std;
0065   // read in vertices and EventSetup
0066   vertexing_.newEvent(iEvent, iSetup);
0067 
0068   // prepare room and tools for output
0069   auto result = std::make_unique<VertexAssociationMap>();
0070   VertexAssociationMap::Filler filler(*result);
0071   vector<pat::VertexAssociation> assos;
0072 
0073   // loop on input tags
0074   for (std::vector<edm::EDGetTokenT<edm::View<reco::Candidate> > >::const_iterator it = particlesTokens_.begin(),
0075                                                                                    end = particlesTokens_.end();
0076        it != end;
0077        ++it) {
0078     // read candidates
0079     Handle<View<reco::Candidate> > cands;
0080     iEvent.getByToken(*it, cands);
0081     assos.clear();
0082     assos.reserve(cands->size());
0083     // loop on candidates
0084     for (size_t i = 0, n = cands->size(); i < n; ++i) {
0085       assos.push_back(vertexing_(cands->refAt(i)));
0086     }
0087     // insert into ValueMap
0088     filler.insert(cands, assos.begin(), assos.end());
0089   }
0090 
0091   // do the real filling
0092   filler.fill();
0093 
0094   // put our produced stuff in the event
0095   iEvent.put(std::move(result));
0096 }
0097 
0098 #include "FWCore/Framework/interface/MakerMacros.h"
0099 
0100 DEFINE_FWK_MODULE(PATVertexAssociationProducer);