CMS 3D CMS Logo

PFMatchedCandidateRefExtractor.cc
Go to the documentation of this file.
6 
12 
14 
16 
17 public:
18  explicit PFMatchedCandidateRefExtractor(const edm::ParameterSet & iConfig);
20 
21  virtual void produce(edm::StreamID iID, edm::Event & iEvent, const edm::EventSetup & iSetup) const override;
22 
23 private:
24 
29 
31 
32 };
33 
34 
36  col1Token_(consumes<edm::View<reco::Candidate> >( iConfig.getParameter<edm::InputTag>("col1") )),
37  col2Token_(consumes<edm::View<reco::Candidate> >( iConfig.getParameter<edm::InputTag>("col2") )),
38  moduleLabel_(iConfig.getParameter<std::string>("@module_label"))
39 {
40  //register products
41 
42  extractPFCands_=iConfig.getParameter<bool>("extractPFCandidates");
43 
44  produces<edm::PtrVector<reco::Candidate> >("col1");
45  produces<edm::PtrVector<reco::Candidate> >("col2");
46  if(extractPFCands_) {
47  pfCandToken_=mayConsume<edm::View<reco::PFCandidate> >( iConfig.getParameter<edm::InputTag>("pfCandCollection") );
48  produces<edm::PtrVector<reco::PFCandidate> >("pfCandCol1");
49  produces<edm::PtrVector<reco::PFCandidate> >("pfCandCol2");
50  }
51 
52 }
53 
54 
56 }
57 
58 
59 // ------------ method called to produce the data ------------
60 void
62 {
63 
64 
65  std::unique_ptr<edm::PtrVector<reco::Candidate> > outcol1(new edm::PtrVector<reco::Candidate>());
66  std::unique_ptr<edm::PtrVector<reco::Candidate> > outcol2(new edm::PtrVector<reco::Candidate>());
67 
68  std::unique_ptr<edm::PtrVector<reco::PFCandidate> > outPFCandCol1(new edm::PtrVector<reco::PFCandidate>());
69  std::unique_ptr<edm::PtrVector<reco::PFCandidate> > outPFCandCol2(new edm::PtrVector<reco::PFCandidate>());
70 
74 
75  iEvent.getByToken( col1Token_, col1Handle );
76  iEvent.getByToken( col2Token_, col2Handle );
77  if(extractPFCands_) {
78  iEvent.getByToken( pfCandToken_, pfCandHandle );
79  }
80 
81  for(size_t iC1=0;iC1<col1Handle->size();iC1++) {
82  for(size_t iC2=0;iC2<col2Handle->size();iC2++) {
83 
84  bool matched=(col1Handle->ptrAt(iC1)==col2Handle->ptrAt(iC2));
85  if(!matched &&
86  deltaR2(col1Handle->ptrAt(iC1)->p4(), col2Handle->ptrAt(iC2)->p4() )< 0.000001) {
87  matched=true;
88  }
89 
90  if(matched) {
91  outcol1->push_back( col1Handle->ptrAt(iC1) );
92  outcol2->push_back( col2Handle->ptrAt(iC2) );
93 
94  if(!extractPFCands_) continue;
95  std::set<reco::CandidatePtr> sc1s;
96  std::set<reco::CandidatePtr> sc2s;
97  for(size_t ics1=0;ics1<col1Handle->ptrAt(iC1)->numberOfSourceCandidatePtrs();
98  ics1++ ) {
99  sc1s.insert( col1Handle->ptrAt(iC1)->sourceCandidatePtr(ics1));
100  }
101  for(size_t ics2=0;ics2<col2Handle->ptrAt(iC2)->numberOfSourceCandidatePtrs();
102  ics2++ ) {
103  sc2s.insert( col2Handle->ptrAt(iC2)->sourceCandidatePtr(ics2));
104  }
105 
106  for(size_t ic=0;ic<pfCandHandle->size(); ++ic) {
107  reco::PFCandidatePtr c = pfCandHandle->ptrAt(ic);
108 
109  bool match1=(sc1s.find(c)!=sc1s.end());
110  bool match2=(sc2s.find(c)!=sc2s.end());
111 
112  if(!match1) {//recovery when pfcandidate sources are not equivalent
113  for(size_t ics1=0;ics1<sc1s.size();ics1++) {
114  if(deltaR2(c->p4(),
115  col1Handle->ptrAt(iC1)->sourceCandidatePtr(ics1)->p4())<0.0000001) { //tight dR, pfcandidates should be the same
116  match1=true;
117  break;
118  }
119  }
120  }
121  if(!match2) {//recovery when pfcandidate sources are not equivalent
122  for(size_t ics2=0;ics2<sc2s.size();ics2++) {
123  if(deltaR2(c->p4(),
124  col2Handle->ptrAt(iC2)->sourceCandidatePtr(ics2)->p4())<0.00001) { //tight dR
125  match2=true;
126  break;
127  }
128  }
129  }
130 
131  if(match1) {
132  outPFCandCol1->push_back(c);
133  }
134  if(match2) {
135  outPFCandCol2->push_back(c);
136  }
137  } //pfcand loop
138 
139  } //matching
140  } //col2
141  } //col1
142 
143  iEvent.put(std::move(outcol1),"col1");
144  iEvent.put(std::move(outcol2),"col2");
145  if(extractPFCands_) {
146  iEvent.put(std::move(outPFCandCol1),"pfCandCol1");
147  iEvent.put(std::move(outPFCandCol2),"pfCandCol2");
148  }
149 
150 }
151 
152 //define this as a plug-in
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
virtual void produce(edm::StreamID iID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
PFMatchedCandidateRefExtractor(const edm::ParameterSet &iConfig)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< edm::View< reco::Candidate > > col1Token_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:230
edm::EDGetTokenT< edm::View< reco::Candidate > > col2Token_
edm::EDGetTokenT< edm::View< reco::PFCandidate > > pfCandToken_
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
fixed size matrix
HLT enums.
def move(src, dest)
Definition: eostools.py:510