Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:00

0001 /**
0002   \class 
0003   \brief
0004   \author   Andrea Rizzi
0005   \version  $Id: TrackAndVertexUnpacker.cc,v 1.2 2010/02/20 21:00:29 wmtan Exp $
0006 */
0007 
0008 #include "FWCore/Framework/interface/global/EDProducer.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 
0013 //#include "DataFormats/Common/interface/ValueMap.h"
0014 //#include "DataFormats/Common/interface/View.h"
0015 //#include "DataFormats/PatCandidates/interface/Vertexing.h"
0016 
0017 #include "DataFormats/VertexReco/interface/Vertex.h"
0018 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0019 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0020 #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h"
0021 
0022 //#include "PhysicsTools/PatAlgos/interface/VertexingHelper.h"
0023 
0024 namespace pat {
0025 
0026   class PATTrackAndVertexUnpacker : public edm::global::EDProducer<> {
0027   public:
0028     explicit PATTrackAndVertexUnpacker(const edm::ParameterSet& iConfig);
0029     ~PATTrackAndVertexUnpacker() override;
0030 
0031     void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0032 
0033   private:
0034     typedef std::vector<edm::InputTag> VInputTag;
0035     // configurables
0036     const edm::EDGetTokenT<std::vector<pat::PackedCandidate>> Cands_;
0037     const edm::EDGetTokenT<reco::VertexCollection> PVs_;
0038     const edm::EDGetTokenT<reco::VertexCompositePtrCandidateCollection> SVs_;
0039     const edm::EDGetTokenT<std::vector<pat::PackedCandidate>> AdditionalTracks_;
0040     //////    std::vector<edm::EDGetTokenT<edm::View<reco::Candidate> > > particlesTokens_;
0041   };
0042 
0043 }  // namespace pat
0044 
0045 using pat::PATTrackAndVertexUnpacker;
0046 
0047 PATTrackAndVertexUnpacker::PATTrackAndVertexUnpacker(const edm::ParameterSet& iConfig)
0048     : Cands_(consumes<std::vector<pat::PackedCandidate>>(iConfig.getParameter<edm::InputTag>("packedCandidates"))),
0049       PVs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("slimmedVertices"))),
0050       SVs_(consumes<reco::VertexCompositePtrCandidateCollection>(
0051           iConfig.getParameter<edm::InputTag>("slimmedSecondaryVertices"))),
0052       AdditionalTracks_(
0053           consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("additionalTracks"))) {
0054   produces<reco::TrackCollection>();
0055   produces<reco::VertexCollection>();
0056   produces<reco::VertexCollection>("secondary");
0057 }
0058 
0059 PATTrackAndVertexUnpacker::~PATTrackAndVertexUnpacker() {}
0060 
0061 void PATTrackAndVertexUnpacker::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0062   using namespace edm;
0063   using namespace std;
0064   using namespace reco;
0065   Handle<std::vector<pat::PackedCandidate>> cands;
0066   iEvent.getByToken(Cands_, cands);
0067   Handle<VertexCollection> pvs;
0068   iEvent.getByToken(PVs_, pvs);
0069   Handle<VertexCompositePtrCandidateCollection> svs;
0070   iEvent.getByToken(SVs_, svs);
0071   Handle<std::vector<pat::PackedCandidate>> addTracks;
0072   iEvent.getByToken(AdditionalTracks_, addTracks);
0073 
0074   auto outTks = std::make_unique<std::vector<reco::Track>>();
0075   std::map<unsigned int, std::vector<unsigned int>> asso;
0076   std::map<unsigned int, unsigned int> trackKeys;
0077   unsigned int j = 0;
0078   for (unsigned int i = 0; i < cands->size(); i++) {
0079     const pat::PackedCandidate& c = (*cands)[i];
0080     if (c.hasTrackDetails() && c.charge() != 0 && c.numberOfHits() > 0) {
0081       outTks->push_back(c.pseudoTrack());
0082       for (size_t ipv = 0; ipv < pvs->size(); ++ipv) {
0083         if (c.fromPV(ipv) == pat::PackedCandidate::PVUsedInFit)
0084           asso[ipv].push_back(j);
0085       }
0086       trackKeys[i] = j;
0087       j++;
0088     }
0089   }
0090 
0091   int offsetAdd = j;
0092   for (unsigned int i = 0; i < addTracks->size(); i++) {
0093     if ((*addTracks)[i].hasTrackDetails()) {
0094       outTks->push_back((*addTracks)[i].pseudoTrack());
0095       for (size_t ipv = 0; ipv < pvs->size(); ++ipv) {
0096         if ((*addTracks)[i].fromPV(ipv) == pat::PackedCandidate::PVUsedInFit)
0097           asso[ipv].push_back(j);
0098       }
0099       j++;
0100     }
0101   }
0102 
0103   edm::OrphanHandle<std::vector<reco::Track>> oh = iEvent.put(std::move(outTks));
0104 
0105   auto outPv = std::make_unique<std::vector<reco::Vertex>>();
0106 
0107   for (size_t ipv = 0; ipv < pvs->size(); ++ipv) {
0108     reco::Vertex pv = (*pvs)[ipv];
0109     for (unsigned int i = 0; i < asso[ipv].size(); i++) {
0110       TrackRef r(oh, asso[ipv][i]);
0111       TrackBaseRef rr(r);
0112       pv.add(rr);
0113     }
0114     outPv->push_back(pv);
0115   }
0116   iEvent.put(std::move(outPv));
0117 
0118   auto outSv = std::make_unique<std::vector<reco::Vertex>>();
0119   for (size_t i = 0; i < svs->size(); i++) {
0120     const reco::VertexCompositePtrCandidate& sv = (*svs)[i];
0121     outSv->push_back(reco::Vertex(sv.vertex(), sv.vertexCovariance(), sv.vertexChi2(), sv.vertexNdof(), 0));
0122     for (size_t j = 0; j < sv.numberOfDaughters(); j++) {
0123       TrackRef r;
0124       if (sv.daughterPtr(j).id() == cands.id()) {
0125         // use trackKeys because cand->track has gaps from neutral
0126         r = TrackRef(oh, trackKeys[sv.daughterPtr(j).key()]);
0127       } else {
0128         // use directly the key because addTracks is only charged
0129         r = TrackRef(oh, offsetAdd + sv.daughterPtr(j).key());
0130       }
0131       TrackBaseRef rr(r);
0132       outSv->back().add(rr);
0133     }
0134   }
0135 
0136   iEvent.put(std::move(outSv), "secondary");
0137 }
0138 
0139 #include "FWCore/Framework/interface/MakerMacros.h"
0140 
0141 DEFINE_FWK_MODULE(PATTrackAndVertexUnpacker);