CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
74  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
75  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
76  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
77 
78  // ----------member data ---------------------------
84 };
85 
86 //
87 // constructors and destructor
88 //
90  : jets_(consumes<edm::View<pat::Jet>>(params.getParameter<edm::InputTag>("jets"))),
91  muons_(consumes<edm::View<pat::Muon>>(params.getParameter<edm::InputTag>("muons"))),
92  electrons_(consumes<edm::View<pat::Electron>>(params.getParameter<edm::InputTag>("electrons"))),
93  taus_(consumes<edm::View<pat::Tau>>(params.getParameter<edm::InputTag>("taus"))),
94  photons_(consumes<edm::View<pat::Photon>>(params.getParameter<edm::InputTag>("photons")))
95 
96 {
97  produces<std::vector<pat::Jet>>("jets");
98  produces<std::vector<pat::Muon>>("muons");
99  produces<std::vector<pat::Electron>>("electrons");
100  produces<std::vector<pat::Tau>>("taus");
101  produces<std::vector<pat::Photon>>("photons");
102 }
103 
105  // do anything here that needs to be done at destruction time
106  // (e.g. close files, deallocate resources etc.)
107 }
108 
109 //
110 // member functions
111 //
112 
113 // ------------ method called to produce the data ------------
114 
116 template <class C1, class C2, class C3, class C4>
117 void PATObjectCrossLinker::matchOneToMany(const C1& refProdOne,
118  C2& itemsOne,
119  const std::string& nameOne,
120  const C3& refProdMany,
121  C4& itemsMany,
122  const std::string& nameMany) {
123  size_t ji = 0;
124  for (auto& j : itemsOne) {
125  edm::PtrVector<reco::Candidate> overlaps(refProdMany.id());
126  size_t mi = 0;
127  for (auto& m : itemsMany) {
128  if (matchByCommonSourceCandidatePtr(j, m) && (!m.hasUserCand(nameOne))) {
129  m.addUserCand(nameOne, reco::CandidatePtr(refProdOne.id(), ji, refProdOne.productGetter()));
130  overlaps.push_back(reco::CandidatePtr(refProdMany.id(), mi, refProdMany.productGetter()));
131  }
132  mi++;
133  }
134  j.setOverlaps(nameMany, overlaps);
135  ji++;
136  }
137 }
138 
139 template <class C1, class C2, class C3, class C4>
141  C2& itemsOne,
142  const std::string& nameOne,
143  const C3& refProdMany,
144  C4& itemsMany,
145  const std::string& nameMany) {
146  size_t ji = 0;
147  for (auto& j : itemsOne) {
148  edm::PtrVector<reco::Candidate> overlaps(refProdMany.id());
149  size_t mi = 0;
150  for (auto& m : itemsMany) {
151  if (matchByCommonParentSuperClusterRef(j, m) && (!m.hasUserCand(nameOne))) {
152  m.addUserCand(nameOne, reco::CandidatePtr(refProdOne.id(), ji, refProdOne.productGetter()));
153  overlaps.push_back(reco::CandidatePtr(refProdMany.id(), mi, refProdMany.productGetter()));
154  }
155  mi++;
156  }
157  j.setOverlaps(nameMany, overlaps);
158  ji++;
159  }
160 }
161 
163  using namespace edm;
165  iEvent.getByToken(jets_, jetsIn);
166  auto jets = std::make_unique<std::vector<pat::Jet>>();
167  for (const auto& j : *jetsIn)
168  jets->push_back(j);
169  auto jetRefProd = iEvent.getRefBeforePut<std::vector<pat::Jet>>("jets");
170 
172  iEvent.getByToken(muons_, muonsIn);
173  auto muons = std::make_unique<std::vector<pat::Muon>>();
174  for (const auto& m : *muonsIn)
175  muons->push_back(m);
176  auto muRefProd = iEvent.getRefBeforePut<std::vector<pat::Muon>>("muons");
177 
179  iEvent.getByToken(electrons_, electronsIn);
180  auto electrons = std::make_unique<std::vector<pat::Electron>>();
181  for (const auto& e : *electronsIn)
182  electrons->push_back(e);
183  auto eleRefProd = iEvent.getRefBeforePut<std::vector<pat::Electron>>("electrons");
184 
186  iEvent.getByToken(taus_, tausIn);
187  auto taus = std::make_unique<std::vector<pat::Tau>>();
188  for (const auto& t : *tausIn)
189  taus->push_back(t);
190  auto tauRefProd = iEvent.getRefBeforePut<std::vector<pat::Tau>>("taus");
191 
193  iEvent.getByToken(photons_, photonsIn);
194  auto photons = std::make_unique<std::vector<pat::Photon>>();
195  for (const auto& p : *photonsIn)
196  photons->push_back(p);
197  auto phRefProd = iEvent.getRefBeforePut<std::vector<pat::Photon>>("photons");
198 
199  matchOneToMany(jetRefProd, *jets, "jet", muRefProd, *muons, "muons");
200  matchOneToMany(jetRefProd, *jets, "jet", eleRefProd, *electrons, "electrons");
201  matchOneToMany(jetRefProd, *jets, "jet", tauRefProd, *taus, "taus");
202  matchOneToMany(jetRefProd, *jets, "jet", phRefProd, *photons, "photons");
203 
204  matchElectronToPhoton(eleRefProd, *electrons, "electron", phRefProd, *photons, "photons");
205 
206  iEvent.put(std::move(jets), "jets");
207  iEvent.put(std::move(muons), "muons");
208  iEvent.put(std::move(electrons), "electrons");
209  iEvent.put(std::move(taus), "taus");
210  iEvent.put(std::move(photons), "photons");
211 }
212 
213 // ------------ method called once each stream before processing any runs, lumis or events ------------
215 
216 // ------------ method called once each stream after processing all runs, lumis and events ------------
218 
219 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
221  //The following says we do not know what parameters are allowed so do no validation
222  // Please change this to state exactly what you do use, even if it is no parameters
224  desc.setUnknown();
225  descriptions.addDefault(desc);
226 }
227 
228 //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)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
constexpr char Photon[]
Definition: modules.cc:14
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#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_
bool matchByCommonParentSuperClusterRef(const C1 &c1, const C2 &c2)
Definition: MatchingUtils.h:21
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
vector< PseudoJet > jets
const edm::EDGetTokenT< edm::View< pat::Jet > > jets_
def move
Definition: eostools.py:511
const edm::EDGetTokenT< edm::View< pat::Electron > > electrons_
RefProd< PROD > getRefBeforePut()
Definition: Event.h:158
void matchElectronToPhoton(const C1 &refProdOne, C2 &itemsOne, const std::string &nameOne, const C3 &refProdMany, C4 &itemsMany, const std::string &nameMany)
constexpr char Jet[]
Definition: modules.cc:9
const edm::EDGetTokenT< edm::View< pat::Tau > > taus_
tuple muons
Definition: patZpeak.py:41
const edm::EDGetTokenT< edm::View< pat::Photon > > photons_
constexpr char Electron[]
Definition: modules.cc:12
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: PtrVectorBase.h:63