CMS 3D CMS Logo

PATObjectCrossLinker.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: PATObjectCrossLinker
5 //
13 //
14 // Original Author: Andrea Rizzi
15 // Created: Mon, 28 Aug 2017 09:26:39 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
28 
31 
37 
39 
41 //
42 // class declaration
43 //
44 
46 public:
47  explicit PATObjectCrossLinker(const edm::ParameterSet&);
48  ~PATObjectCrossLinker() override;
49 
50  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
51 
52 private:
53  void beginStream(edm::StreamID) override;
54  void produce(edm::Event&, const edm::EventSetup&) override;
55  void endStream() override;
56 
57  template <class C1, class C2, class C3, class C4>
58  void matchOneToMany(const C1& refProdOne,
59  C2& itemsOne,
60  const std::string& nameOne,
61  const C3& refProdMany,
62  C4& itemsMany,
63  const std::string& nameMany);
64 
65  template <class C1, class C2, class C3, class C4>
66  void matchElectronToPhoton(const C1& refProdOne,
67  C2& itemsOne,
68  const std::string& nameOne,
69  const C3& refProdMany,
70  C4& itemsMany,
71  const std::string& nameMany);
72 
73  template <class C1, class C2, class C3, class C4>
74  void matchLowPtToElectron(const C1& refProdOne,
75  C2& itemsOne,
76  const std::string& nameOne,
77  const C3& refProdMany,
78  C4& itemsMany,
79  const std::string& nameMany);
80 
81  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
82  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
83  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
84  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
85 
86  // ----------member data ---------------------------
94 };
95 
96 //
97 // constructors and destructor
98 //
100  : jets_(consumes<edm::View<pat::Jet>>(params.getParameter<edm::InputTag>("jets"))),
101  muons_(consumes<edm::View<pat::Muon>>(params.getParameter<edm::InputTag>("muons"))),
102  electrons_(consumes<edm::View<pat::Electron>>(params.getParameter<edm::InputTag>("electrons"))),
103  lowPtElectronsTag_(params.getParameter<edm::InputTag>("lowPtElectrons")),
104  lowPtElectrons_(mayConsume<edm::View<pat::Electron>>(lowPtElectronsTag_)),
105  taus_(consumes<edm::View<pat::Tau>>(params.getParameter<edm::InputTag>("taus"))),
106  photons_(consumes<edm::View<pat::Photon>>(params.getParameter<edm::InputTag>("photons")))
107 
108 {
109  produces<std::vector<pat::Jet>>("jets");
110  produces<std::vector<pat::Muon>>("muons");
111  produces<std::vector<pat::Electron>>("electrons");
112  if (!lowPtElectronsTag_.label().empty())
113  produces<std::vector<pat::Electron>>("lowPtElectrons");
114  produces<std::vector<pat::Tau>>("taus");
115  produces<std::vector<pat::Photon>>("photons");
116 }
117 
119  // do anything here that needs to be done at destruction time
120  // (e.g. close files, deallocate resources etc.)
121 }
122 
123 //
124 // member functions
125 //
126 
127 // ------------ method called to produce the data ------------
128 
130 template <class C1, class C2, class C3, class C4>
131 void PATObjectCrossLinker::matchOneToMany(const C1& refProdOne,
132  C2& itemsOne,
133  const std::string& nameOne,
134  const C3& refProdMany,
135  C4& itemsMany,
136  const std::string& nameMany) {
137  size_t ji = 0;
138  for (auto& j : itemsOne) {
139  edm::PtrVector<reco::Candidate> overlaps(refProdMany.id());
140  size_t mi = 0;
141  for (auto& m : itemsMany) {
142  if (matchByCommonSourceCandidatePtr(j, m) && (!m.hasUserCand(nameOne))) {
143  m.addUserCand(nameOne, reco::CandidatePtr(refProdOne.id(), ji, refProdOne.productGetter()));
144  overlaps.push_back(reco::CandidatePtr(refProdMany.id(), mi, refProdMany.productGetter()));
145  }
146  mi++;
147  }
148  j.setOverlaps(nameMany, overlaps);
149  ji++;
150  }
151 }
152 
153 template <class C1, class C2, class C3, class C4>
155  C2& itemsOne,
156  const std::string& nameOne,
157  const C3& refProdMany,
158  C4& itemsMany,
159  const std::string& nameMany) {
160  size_t ji = 0;
161  for (auto& j : itemsOne) {
162  edm::PtrVector<reco::Candidate> overlaps(refProdMany.id());
163  size_t mi = 0;
164  for (auto& m : itemsMany) {
165  if (matchByCommonParentSuperClusterRef(j, m) && (!m.hasUserCand(nameOne))) {
166  m.addUserCand(nameOne, reco::CandidatePtr(refProdOne.id(), ji, refProdOne.productGetter()));
167  overlaps.push_back(reco::CandidatePtr(refProdMany.id(), mi, refProdMany.productGetter()));
168  }
169  mi++;
170  }
171  j.setOverlaps(nameMany, overlaps);
172  ji++;
173  }
174 }
175 
176 template <class C1, class C2, class C3, class C4>
178  C2& itemsOne,
179  const std::string& nameOne,
180  const C3& refProdMany,
181  C4& itemsMany,
182  const std::string& nameMany) {
183  size_t ji = 0;
184  for (auto& j : itemsOne) {
185  std::vector<std::pair<size_t, float>> idxs;
186  size_t mi = 0;
187  for (auto& m : itemsMany) {
188  float dr2 = deltaR2(m, j);
189  if (dr2 < 1.e-6) { // deltaR < 1.e-3
190  m.addUserCand(nameOne, reco::CandidatePtr(refProdOne.id(), ji, refProdOne.productGetter()));
191  idxs.push_back(std::make_pair(mi, dr2));
192  }
193  mi++;
194  }
195  std::sort(idxs.begin(), idxs.end(), [](auto& left, auto& right) { return left.second < right.second; });
196 
197  edm::PtrVector<reco::Candidate> overlaps(refProdMany.id());
198  for (auto idx : idxs) {
199  overlaps.push_back(reco::CandidatePtr(refProdMany.id(), idx.first, refProdMany.productGetter()));
200  }
201  j.setOverlaps(nameMany, overlaps);
202  ji++;
203  }
204 }
205 
207  using namespace edm;
209  iEvent.getByToken(jets_, jetsIn);
210  auto jets = std::make_unique<std::vector<pat::Jet>>();
211  for (const auto& j : *jetsIn)
212  jets->push_back(j);
213  auto jetRefProd = iEvent.getRefBeforePut<std::vector<pat::Jet>>("jets");
214 
216  iEvent.getByToken(muons_, muonsIn);
217  auto muons = std::make_unique<std::vector<pat::Muon>>();
218  for (const auto& m : *muonsIn)
219  muons->push_back(m);
220  auto muRefProd = iEvent.getRefBeforePut<std::vector<pat::Muon>>("muons");
221 
223  iEvent.getByToken(electrons_, electronsIn);
224  auto electrons = std::make_unique<std::vector<pat::Electron>>();
225  for (const auto& e : *electronsIn)
226  electrons->push_back(e);
227  auto eleRefProd = iEvent.getRefBeforePut<std::vector<pat::Electron>>("electrons");
228 
229  edm::Handle<edm::View<pat::Electron>> lowPtElectronsIn;
230  auto lowPtElectrons = std::make_unique<std::vector<pat::Electron>>();
231  if (!lowPtElectronsTag_.label().empty()) {
232  iEvent.getByToken(lowPtElectrons_, lowPtElectronsIn);
233  for (const auto& e : *lowPtElectronsIn) {
234  lowPtElectrons->push_back(e);
235  }
236  }
237 
239  iEvent.getByToken(taus_, tausIn);
240  auto taus = std::make_unique<std::vector<pat::Tau>>();
241  for (const auto& t : *tausIn)
242  taus->push_back(t);
243  auto tauRefProd = iEvent.getRefBeforePut<std::vector<pat::Tau>>("taus");
244 
246  iEvent.getByToken(photons_, photonsIn);
247  auto photons = std::make_unique<std::vector<pat::Photon>>();
248  for (const auto& p : *photonsIn)
249  photons->push_back(p);
250  auto phRefProd = iEvent.getRefBeforePut<std::vector<pat::Photon>>("photons");
251 
252  matchOneToMany(jetRefProd, *jets, "jet", muRefProd, *muons, "muons");
253  matchOneToMany(jetRefProd, *jets, "jet", eleRefProd, *electrons, "electrons");
254  matchOneToMany(jetRefProd, *jets, "jet", tauRefProd, *taus, "taus");
255  matchOneToMany(jetRefProd, *jets, "jet", phRefProd, *photons, "photons");
256 
257  matchElectronToPhoton(eleRefProd, *electrons, "electron", phRefProd, *photons, "photons");
258  if (!lowPtElectronsTag_.label().empty()) {
259  auto lowPtEleRefProd = iEvent.getRefBeforePut<std::vector<pat::Electron>>("lowPtElectrons");
260  matchLowPtToElectron(lowPtEleRefProd, *lowPtElectrons, "lowPtElectron", eleRefProd, *electrons, "electrons");
261  }
262 
263  iEvent.put(std::move(jets), "jets");
264  iEvent.put(std::move(muons), "muons");
265  iEvent.put(std::move(electrons), "electrons");
266  if (!lowPtElectronsTag_.label().empty())
267  iEvent.put(std::move(lowPtElectrons), "lowPtElectrons");
268  iEvent.put(std::move(taus), "taus");
269  iEvent.put(std::move(photons), "photons");
270 }
271 
272 // ------------ method called once each stream before processing any runs, lumis or events ------------
274 
275 // ------------ method called once each stream after processing all runs, lumis and events ------------
277 
278 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
280  //The following says we do not know what parameters are allowed so do no validation
281  // Please change this to state exactly what you do use, even if it is no parameters
283  desc.setUnknown();
284  descriptions.addDefault(desc);
285 }
286 
287 //define this as a plug-in
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void matchOneToMany(const C1 &refProdOne, C2 &itemsOne, const std::string &nameOne, const C3 &refProdMany, C4 &itemsMany, const std::string &nameMany)
Definition: Photon.py:1
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: PtrVectorBase.h:63
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void produce(edm::Event &, const edm::EventSetup &) override
void beginStream(edm::StreamID) override
const edm::EDGetTokenT< edm::View< pat::Muon > > muons_
std::string const & label() const
Definition: InputTag.h:36
bool matchByCommonParentSuperClusterRef(const C1 &c1, const C2 &c2)
Definition: MatchingUtils.h:21
edm::EDGetTokenT< edm::View< pat::Electron > > lowPtElectrons_
Definition: HeavyIon.h:7
lowPtElectrons
Definition: nano_cff.py:42
int iEvent
Definition: GenABIO.cc:224
PATObjectCrossLinker(const edm::ParameterSet &)
void addDefault(ParameterSetDescription const &psetDescription)
bool matchByCommonSourceCandidatePtr(const C1 &c1, const C2 &c2)
Definition: MatchingUtils.h:9
Definition: Muon.py:1
Definition: Jet.py:1
const edm::EDGetTokenT< edm::View< pat::Jet > > jets_
const edm::EDGetTokenT< edm::View< pat::Electron > > electrons_
void matchElectronToPhoton(const C1 &refProdOne, C2 &itemsOne, const std::string &nameOne, const C3 &refProdMany, C4 &itemsMany, const std::string &nameMany)
Definition: Tau.py:1
void matchLowPtToElectron(const C1 &refProdOne, C2 &itemsOne, const std::string &nameOne, const C3 &refProdMany, C4 &itemsMany, const std::string &nameMany)
edm::InputTag lowPtElectronsTag_
const edm::EDGetTokenT< edm::View< pat::Tau > > taus_
HLT enums.
const edm::EDGetTokenT< edm::View< pat::Photon > > photons_
def move(src, dest)
Definition: eostools.py:511