Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:59

0001 #include <string>
0002 #include <memory>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/global/EDProducer.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "DataFormats/Common/interface/ValueMap.h"
0009 
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "DataFormats/VertexReco/interface/Vertex.h"
0013 #include "DataFormats/Common/interface/ValueMap.h"
0014 #include "DataFormats/Math/interface/libminifloat.h"
0015 
0016 namespace pat {
0017   class PATVertexSlimmer : public edm::global::EDProducer<> {
0018   public:
0019     explicit PATVertexSlimmer(const edm::ParameterSet&);
0020     ~PATVertexSlimmer() override;
0021 
0022     void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0023 
0024   private:
0025     const edm::EDGetTokenT<std::vector<reco::Vertex>> src_;
0026     const edm::EDGetTokenT<edm::ValueMap<float>> score_;
0027     const bool rekeyScores_;
0028   };
0029 }  // namespace pat
0030 
0031 pat::PATVertexSlimmer::PATVertexSlimmer(const edm::ParameterSet& iConfig)
0032     : src_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("src"))),
0033       score_(mayConsume<edm::ValueMap<float>>(
0034           iConfig.existsAs<edm::InputTag>("score") ? iConfig.getParameter<edm::InputTag>("score") : edm::InputTag())),
0035       rekeyScores_(iConfig.existsAs<edm::InputTag>("score")) {
0036   produces<std::vector<reco::Vertex>>();
0037   if (rekeyScores_)
0038     produces<edm::ValueMap<float>>();
0039 }
0040 
0041 pat::PATVertexSlimmer::~PATVertexSlimmer() {}
0042 
0043 void pat::PATVertexSlimmer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0044   edm::Handle<std::vector<reco::Vertex>> vertices;
0045   iEvent.getByToken(src_, vertices);
0046   auto outPtr = std::make_unique<std::vector<reco::Vertex>>();
0047 
0048   outPtr->reserve(vertices->size());
0049   for (unsigned int i = 0, n = vertices->size(); i < n; ++i) {
0050     const reco::Vertex& v = (*vertices)[i];
0051     auto co = v.covariance4D();
0052     if (i > 0) {
0053       for (size_t j = 0; j < 4; j++) {
0054         for (size_t k = j; k < 4; k++) {
0055           co(j, k) = MiniFloatConverter::reduceMantissaToNbits<10>(co(j, k));
0056         }
0057       }
0058     }
0059     outPtr->push_back(reco::Vertex(v.position(), co, v.t(), v.chi2(), v.ndof(), 0));
0060   }
0061 
0062   auto oh = iEvent.put(std::move(outPtr));
0063   if (rekeyScores_) {
0064     edm::Handle<edm::ValueMap<float>> scores;
0065     iEvent.getByToken(score_, scores);
0066     auto vertexScoreOutput = std::make_unique<edm::ValueMap<float>>();
0067     edm::ValueMap<float>::const_iterator idIt = scores->begin();
0068     for (; idIt != scores->end(); idIt++) {
0069       if (idIt.id() == vertices.id())
0070         break;
0071     }
0072     // std::find_if(scores->begin(), scores->end(), [vertices] (const edm::ValueMap<float>::const_iterator& s) { return s.id() == vertices.id(); } );
0073     edm::ValueMap<float>::Filler vertexScoreFiller(*vertexScoreOutput);
0074     vertexScoreFiller.insert(oh, idIt.begin(), idIt.end());
0075     vertexScoreFiller.fill();
0076     iEvent.put(std::move(vertexScoreOutput));
0077   }
0078 }
0079 
0080 using pat::PATVertexSlimmer;
0081 #include "FWCore/Framework/interface/MakerMacros.h"
0082 DEFINE_FWK_MODULE(PATVertexSlimmer);