CMS 3D CMS Logo

TrackAndVertexUnpacker.cc
Go to the documentation of this file.
1 
13 
14 //#include "DataFormats/Common/interface/ValueMap.h"
15 //#include "DataFormats/Common/interface/View.h"
16 //#include "DataFormats/PatCandidates/interface/Vertexing.h"
17 
22 
23 //#include "PhysicsTools/PatAlgos/interface/VertexingHelper.h"
24 
25 
26 namespace pat {
27 
29 
30 
31  public:
32 
33  explicit PATTrackAndVertexUnpacker(const edm::ParameterSet & iConfig);
34  ~PATTrackAndVertexUnpacker() override;
35 
36  void produce(edm::StreamID, edm::Event & iEvent, const edm::EventSetup& iSetup) const override;
37 
38  private:
39  typedef std::vector<edm::InputTag> VInputTag;
40  // configurables
46 
47  };
48 
49 }
50 
52 
54  Cands_(consumes< std::vector<pat::PackedCandidate> >(iConfig.getParameter<edm::InputTag>("packedCandidates"))),
55  PVs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("slimmedVertices"))),
56  SVs_(consumes<reco::VertexCompositePtrCandidateCollection>(iConfig.getParameter<edm::InputTag>("slimmedSecondaryVertices"))),
57  AdditionalTracks_(consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("additionalTracks")))
58 {
59  produces<reco::TrackCollection>();
60  produces<reco::VertexCollection>();
61  produces<reco::VertexCollection>("secondary");
62 }
63 
64 
66 }
67 
68 
70  using namespace edm; using namespace std; using namespace reco;
72  iEvent.getByToken(Cands_, cands);
74  iEvent.getByToken(PVs_, pvs);
76  iEvent.getByToken(SVs_, svs);
78  iEvent.getByToken(AdditionalTracks_, addTracks);
79 
80  auto outTks = std::make_unique<std::vector<reco::Track>>();
81  std::map<unsigned int, std::vector<unsigned int> > asso;
82  std::map<unsigned int, unsigned int> trackKeys;
83  unsigned int j=0;
84  for(unsigned int i=0;i<cands->size();i++) {
85  const pat::PackedCandidate & c = (*cands)[i];
86  if(c.hasTrackDetails() && c.charge() != 0 && c.numberOfHits()> 0){
87  outTks->push_back(c.pseudoTrack());
88  for(size_t ipv=0;ipv< pvs->size(); ++ipv) {
90  asso[ipv].push_back(j);
91  }
92  trackKeys[i]=j;
93  j++;
94  }
95  }
96 
97  int offsetAdd=j;
98  for(unsigned int i = 0; i < addTracks->size(); i++) {
99  if( (*addTracks)[i].hasTrackDetails() ){
100  outTks->push_back((*addTracks)[i].pseudoTrack());
101  for(size_t ipv=0;ipv< pvs->size(); ++ipv) {
102  if((*addTracks)[i].fromPV(ipv)==pat::PackedCandidate::PVUsedInFit)
103  asso[ipv].push_back(j);
104  }
105  j++;
106  }
107  }
108 
110 
111  auto outPv = std::make_unique<std::vector<reco::Vertex>>();
112 
113  for(size_t ipv=0;ipv< pvs->size(); ++ipv) {
114  reco::Vertex pv = (*pvs)[ipv];
115  for(unsigned int i=0;i<asso[ipv].size();i++)
116  {
117  TrackRef r(oh,asso[ipv][i]);
118  TrackBaseRef rr(r);
119  pv.add(rr);
120  }
121  outPv->push_back(pv);
122  }
123  iEvent.put(std::move(outPv));
124 
125  auto outSv = std::make_unique<std::vector<reco::Vertex>>();
126  for(size_t i=0;i< svs->size(); i++) {
127  const reco::VertexCompositePtrCandidate &sv = (*svs)[i];
128  outSv->push_back(reco::Vertex(sv.vertex(),sv.vertexCovariance(),sv.vertexChi2(),sv.vertexNdof(),0));
129  for(size_t j=0;j<sv.numberOfDaughters();j++){
130  TrackRef r;
131  if(sv.daughterPtr(j).id() == cands.id()) {
132  r= TrackRef(oh,trackKeys[sv.daughterPtr(j).key()]); // use trackKeys because cand->track has gaps from neutral
133  } else {
134 // std::cout << "vertex " << i << " using lost Track " << sv.daughterPtr(j).key() << " " << offsetAdd+sv.daughterPtr(j).key() << std::endl;
135  r=TrackRef(oh,offsetAdd+sv.daughterPtr(j).key()); // use directly the key because addTracks is only charged
136  }
137  TrackBaseRef rr(r);
138  outSv->back().add(rr);
139 
140  }
141  }
142 
143  iEvent.put(std::move(outSv),"secondary");
144 
145 }
146 
147 
149 
const edm::EDGetTokenT< reco::VertexCollection > PVs_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
int numberOfHits() const
key_type key() const
Definition: Ptr.h:185
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const edm::EDGetTokenT< std::vector< pat::PackedCandidate > > Cands_
std::vector< pat::PackedCandidate > PackedCandidateCollection
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
void produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
std::vector< VertexCompositePtrCandidate > VertexCompositePtrCandidateCollection
collection of Candidate objects
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
size_t numberOfDaughters() const override
number of daughters
int charge() const override
electric charge
int iEvent
Definition: GenABIO.cc:230
const PVAssoc fromPV(size_t ipv=0) const
double vertexCovariance(int i, int j) const override
(i, j)-th element of error matrix, i, j = 0, ... 3
const Point & vertex() const override
vertex position (overwritten by PF...)
def pv(vc)
Definition: MetAnalyzer.py:6
virtual const reco::Track & pseudoTrack() const
Return reference to a pseudo track made with candidate kinematics, parameterized error for eta...
const edm::EDGetTokenT< std::vector< pat::PackedCandidate > > AdditionalTracks_
bool hasTrackDetails() const
Return true if a bestTrack can be extracted from this Candidate.
void add(const TrackBaseRef &r, float w=1.0)
add a reference to a Track
const edm::EDGetTokenT< reco::VertexCompositePtrCandidateCollection > SVs_
virtual CandidatePtr daughterPtr(size_type i) const
reference to daughter at given position
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:20
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:180
fixed size matrix
HLT enums.
std::vector< edm::InputTag > VInputTag
PATTrackAndVertexUnpacker(const edm::ParameterSet &iConfig)
double vertexChi2() const override
chi-squares
def move(src, dest)
Definition: eostools.py:510