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 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
29 
32 
38 
40 
42 //
43 // class declaration
44 //
45 
47  public:
48  explicit PATObjectCrossLinker(const edm::ParameterSet&);
49  ~PATObjectCrossLinker() override;
50 
51  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
52 
53  private:
54  void beginStream(edm::StreamID) override;
55  void produce(edm::Event&, const edm::EventSetup&) override;
56  void endStream() override;
57 
58  template < class C1, class C2,class C3, class C4>
59  void matchOneToMany(const C1 & refProdOne, C2 & itemsOne, const std::string & nameOne,
60  const C3 & refProdMany, C4& itemsMany, const std::string & nameMany);
61 
62  template < class C1, class C2,class C3,class C4>
63  void matchElectronToPhoton(const C1 & refProdOne, C2 & itemsOne, const std::string & nameOne,
64  const C3 & refProdMany, C4& itemsMany, const std::string & nameMany);
65 
66  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
67  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
68  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
69  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
70 
71  // ----------member data ---------------------------
77 
78 
79 };
80 
81 //
82 // constructors and destructor
83 //
85  jets_(consumes<edm::View<pat::Jet>>( params.getParameter<edm::InputTag>("jets") )),
86  muons_(consumes<edm::View<pat::Muon>>( params.getParameter<edm::InputTag>("muons") )),
87  electrons_(consumes<edm::View<pat::Electron>>( params.getParameter<edm::InputTag>("electrons") )),
88  taus_(consumes<edm::View<pat::Tau>>( params.getParameter<edm::InputTag>("taus") )),
89  photons_(consumes<edm::View<pat::Photon>>( params.getParameter<edm::InputTag>("photons") ))
90 
91 {
92  produces<std::vector<pat::Jet>>("jets");
93  produces<std::vector<pat::Muon>>("muons");
94  produces<std::vector<pat::Electron>>("electrons");
95  produces<std::vector<pat::Tau>>("taus");
96  produces<std::vector<pat::Photon>>("photons");
97 
98 }
99 
100 
102 {
103 
104  // do anything here that needs to be done at destruction time
105  // (e.g. close files, deallocate resources etc.)
106 
107 }
108 
109 
110 //
111 // member functions
112 //
113 
114 // ------------ method called to produce the data ------------
115 
117 template < class C1, class C2,class C3,class C4>
118 void PATObjectCrossLinker::matchOneToMany(const C1 & refProdOne, C2 & itemsOne, const std::string & nameOne,
119  const C3 & refProdMany, C4& itemsMany, const std::string & nameMany)
120 {
121  size_t ji=0;
122  for(auto & j: itemsOne) {
123  edm::PtrVector<reco::Candidate> overlaps(refProdMany.id());
124  size_t mi=0;
125  for(auto & m: itemsMany){
126  if(matchByCommonSourceCandidatePtr(j,m) && (!m.hasUserCand(nameOne))){
127  m.addUserCand(nameOne,reco::CandidatePtr(refProdOne.id(), ji, refProdOne.productGetter()));
128  overlaps.push_back(reco::CandidatePtr(refProdMany.id(), mi, refProdMany.productGetter()));
129  }
130  mi++;
131  }
132  j.setOverlaps(nameMany,overlaps);
133  ji++;
134  }
135 }
136 
137 template < class C1, class C2,class C3,class C4>
138 void PATObjectCrossLinker::matchElectronToPhoton(const C1 & refProdOne, C2 & itemsOne, const std::string & nameOne,
139  const C3 & refProdMany, C4& itemsMany, const std::string & nameMany)
140 {
141  size_t ji=0;
142  for(auto & j: itemsOne) {
143  edm::PtrVector<reco::Candidate> overlaps(refProdMany.id());
144  size_t mi=0;
145  for(auto & m: itemsMany){
146  if(matchByCommonParentSuperClusterRef(j,m) && (!m.hasUserCand(nameOne))){
147  m.addUserCand(nameOne,reco::CandidatePtr(refProdOne.id(), ji, refProdOne.productGetter()));
148  overlaps.push_back(reco::CandidatePtr(refProdMany.id(), mi, refProdMany.productGetter()));
149  }
150  mi++;
151  }
152  j.setOverlaps(nameMany,overlaps);
153  ji++;
154  }
155 }
156 
157 
158 void
160 {
161  using namespace edm;
163  iEvent.getByToken(jets_, jetsIn);
164  auto jets = std::make_unique<std::vector<pat::Jet>>();
165  for(const auto & j : *jetsIn) jets->push_back(j);
166  auto jetRefProd = iEvent.getRefBeforePut< std::vector<pat::Jet> >("jets");
167 
169  iEvent.getByToken(muons_, muonsIn);
170  auto muons = std::make_unique<std::vector<pat::Muon>>();
171  for(const auto & m : *muonsIn) muons->push_back(m);
172  auto muRefProd = iEvent.getRefBeforePut< std::vector<pat::Muon> >("muons");
173 
175  iEvent.getByToken(electrons_, electronsIn);
176  auto electrons = std::make_unique<std::vector<pat::Electron>>();
177  for(const auto & e : *electronsIn) electrons->push_back(e);
178  auto eleRefProd = iEvent.getRefBeforePut< std::vector<pat::Electron> >("electrons");
179 
181  iEvent.getByToken(taus_, tausIn);
182  auto taus = std::make_unique<std::vector<pat::Tau>>();
183  for(const auto & t : *tausIn) taus->push_back(t);
184  auto tauRefProd = iEvent.getRefBeforePut< std::vector<pat::Tau> >("taus");
185 
187  iEvent.getByToken(photons_, photonsIn);
188  auto photons = std::make_unique<std::vector<pat::Photon>>();
189  for(const auto & p : *photonsIn) photons->push_back(p);
190  auto phRefProd = iEvent.getRefBeforePut< std::vector<pat::Photon> >("photons");
191 
192  matchOneToMany(jetRefProd,*jets,"jet",muRefProd,*muons,"muons");
193  matchOneToMany(jetRefProd,*jets,"jet",eleRefProd,*electrons,"electrons");
194  matchOneToMany(jetRefProd,*jets,"jet",tauRefProd,*taus,"taus");
195  matchOneToMany(jetRefProd,*jets,"jet",phRefProd,*photons,"photons");
196 
197  matchElectronToPhoton(eleRefProd,*electrons,"electron",phRefProd,*photons,"photons");
198 
199  iEvent.put(std::move(jets),"jets");
200  iEvent.put(std::move(muons),"muons");
201  iEvent.put(std::move(electrons),"electrons");
202  iEvent.put(std::move(taus),"taus");
203  iEvent.put(std::move(photons),"photons");
204 
205 }
206 
207 // ------------ method called once each stream before processing any runs, lumis or events ------------
208 void
210 {
211 }
212 
213 // ------------ method called once each stream after processing all runs, lumis and events ------------
214 void
216 }
217 
218 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
219 void
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:125
Definition: Photon.py:1
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
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:20
Definition: HeavyIon.h:7
int iEvent
Definition: GenABIO.cc:224
PATObjectCrossLinker(const edm::ParameterSet &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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
vector< PseudoJet > jets
const edm::EDGetTokenT< edm::View< pat::Jet > > jets_
const edm::EDGetTokenT< edm::View< pat::Electron > > electrons_
RefProd< PROD > getRefBeforePut()
Definition: Event.h:150
void matchElectronToPhoton(const C1 &refProdOne, C2 &itemsOne, const std::string &nameOne, const C3 &refProdMany, C4 &itemsMany, const std::string &nameMany)
Definition: Tau.py:1
const edm::EDGetTokenT< edm::View< pat::Tau > > taus_
HLT enums.
const edm::EDGetTokenT< edm::View< pat::Photon > > photons_
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: PtrVectorBase.h:62
def move(src, dest)
Definition: eostools.py:511