CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JetMatchingTools.cc
Go to the documentation of this file.
1 #include <set>
2 
4 
7 
14 
15 using namespace edm;
16 
17 namespace {
18  template <class T>
19  const typename T::value_type* getHit (const T& fCollection, DetId fId) {
20  typename T::const_iterator hit = fCollection.find (fId);
21  if (hit != fCollection.end()) return &*hit;
22  return NULL;
23  }
24 
25  std::vector <const PCaloHit*> getSimHits (const PCaloHitContainer& fCollection, DetId fId) {
26  std::vector <const PCaloHit*> result;
27  for (unsigned i = 0; i < fCollection.size (); ++i) {
28  if (fCollection[i].id() == fId.rawId()) {
29  result.push_back (&(fCollection[i]));
30  }
31  }
32  return result;
33  }
34 }
35 
37  : mEvent (&fEvent),
38  mEBRecHitCollection (0),
39  mEERecHitCollection (0),
40  mHBHERecHitCollection (0),
41  mHORecHitCollection (0),
42  mHFRecHitCollection (0),
43  mEBSimHitCollection (0),
44  mEESimHitCollection (0),
45  mHcalSimHitCollection (0),
46  mSimTrackCollection (0),
47  mSimVertexCollection (0),
48  mGenParticleCollection (0)
49 {
50 
51  input_ebrechits_token_ = iC.mayConsume<EBRecHitCollection>(edm::InputTag ("ecalRecHit:EcalRecHitsEB"));
52  input_eerechits_token_ = iC.mayConsume<EERecHitCollection>(edm::InputTag ("ecalRecHit:EcalRecHitsEE"));
53  input_hbherechits_token_ = iC.mayConsume<HBHERecHitCollection>(edm::InputTag ("hbhereco"));
54  input_horechits_token_ = iC.mayConsume<HORecHitCollection>(edm::InputTag ("horeco"));
55  input_hfrechits_token_ = iC.mayConsume<HFRecHitCollection>(edm::InputTag ("hfreco"));
56  input_pcalohits_ebcal_token_ = iC.mayConsume<edm::PCaloHitContainer>(edm::InputTag ("g4SimHits:EcalHitsEB"));
57  input_pcalohits_eecal_token_ = iC.mayConsume<edm::PCaloHitContainer>(edm::InputTag ("g4SimHits:EcalHitsEE"));
58  input_pcalohits_hcal_token_ = iC.mayConsume<edm::PCaloHitContainer>(edm::InputTag ("g4SimHits:HcalHits"));
59  input_simtrack_token_ = iC.mayConsume<edm::SimTrackContainer>(edm::InputTag ("g4SimHits"));
60  input_simvertex_token_ = iC.mayConsume<edm::SimVertexContainer>(edm::InputTag ("g4SimHits"));
61  input_cands_token_ = iC.mayConsume<reco::CandidateCollection>(edm::InputTag ("genParticleCandidates"));
62 
63 }
64 
66 
68  if (!mEBRecHitCollection) {
71  mEBRecHitCollection = &*recHits;
72  }
73  return mEBRecHitCollection;
74 }
76  if (!mEERecHitCollection) {
79  mEERecHitCollection = &*recHits;
80  }
81  return mEERecHitCollection;
82 }
84  if (!mHBHERecHitCollection) {
87  mHBHERecHitCollection = &*recHits;
88  }
89  return mHBHERecHitCollection;
90 }
92  if (!mHORecHitCollection) {
95  mHORecHitCollection = &*recHits;
96  }
97  return mHORecHitCollection;
98 }
100  if (!mHFRecHitCollection) {
103  mHFRecHitCollection = &*recHits;
104  }
105  return mHFRecHitCollection;
106 }
108  if (!mEBSimHitCollection) {
112  }
113  return mEBSimHitCollection;
114 }
116  if (!mEESimHitCollection) {
120  }
121  return mEESimHitCollection;
122 }
124  if (!mHcalSimHitCollection) {
128  }
129  return mHcalSimHitCollection;
130 }
132  if (!mSimTrackCollection) {
136  }
137  return mSimTrackCollection;
138 }
140  if (!mSimVertexCollection) {
144  }
145  return mSimVertexCollection;
146 }
148  if (!mGenParticleCollection) {
152  }
153  return mGenParticleCollection;
154 }
155 
157 std::vector <const CaloTower*> JetMatchingTools::getConstituents (const reco::CaloJet& fJet ) {
158  std::vector <const CaloTower*> result;
159  std::vector<CaloTowerPtr> constituents = fJet.getCaloConstituents ();
160  for (unsigned i = 0; i < constituents.size(); ++i) result.push_back (&*(constituents[i]));
161  return result;
162 }
163 
165 std::vector<JetMatchingTools::JetConstituent> JetMatchingTools::getConstituentHits (const CaloTower& fTower) {
166  std::vector<JetConstituent> result;
167 
168  for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
169  DetId id = fTower.constituent (i);
170 
171  if (id.det () == DetId::Ecal) {
172  const EcalRecHit *hit = NULL;
173 
174  if ((EcalSubdetector) id.subdetId () == EcalBarrel) {
175  hit = getHit (*getEBRecHitCollection (), id);
176  }
177  else if ((EcalSubdetector) id.subdetId () == EcalEndcap) {
178  hit = getHit (*getEERecHitCollection (), id);
179  }
180 
181  assert(hit != NULL);
182  if (hit) result.push_back(JetConstituent(*hit));
183  else std::cerr << "Can not find rechit for id " << id.rawId () << std::endl;
184  } else if (id.det () == DetId::Hcal) {
185  const CaloRecHit* hit = NULL;
186 
187  if ((HcalSubdetector) id.subdetId () == HcalBarrel || (HcalSubdetector) id.subdetId () == HcalEndcap) {
188  hit = getHit (*getHBHERecHitCollection (), id);
189  }
190  else if ((HcalSubdetector) id.subdetId () == HcalOuter) {
191  hit = getHit (*getHORecHitCollection (), id);
192  }
193  if ((HcalSubdetector) id.subdetId () == HcalForward) {
194  hit = getHit (*getHFRecHitCollection (), id);
195  }
196 
197  if (hit) result.push_back(JetConstituent(*hit));
198  else std::cerr << "Can not find rechit for id " << id.rawId () << std::endl;
199  }
200  }
201 
202  return result;
203 }
204 
206 std::vector <DetId> JetMatchingTools::getConstituentIds (const CaloTower& fTower) {
207  std::vector <DetId> result;
208  for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
209  DetId id = fTower.constituent (i);
210  result.push_back (id);
211  }
212  return result;
213 }
215 std::vector <const PCaloHit*> JetMatchingTools::getPCaloHits (DetId fId) {
216  std::vector <const PCaloHit*> result;
217  if (fId.det () == DetId::Ecal) {
218  if ((EcalSubdetector) fId.subdetId () == EcalBarrel) {
219  result = getSimHits (*getEBSimHitCollection (), fId);
220  }
221  else if ((EcalSubdetector) fId.subdetId () == EcalEndcap) {
222  result = getSimHits (*getEESimHitCollection (), fId);
223  }
224  }
225  else if (fId.det () == DetId::Hcal) {
226  result = getSimHits (*getHcalSimHitCollection (), fId);
227  }
228  return result;
229 }
232  return fHit.geantTrackId ();
233 }
235 const SimTrack* JetMatchingTools::getTrack (unsigned fSimTrackId) {
236  for (unsigned i = 0; i < getSimTrackCollection ()->size (); ++i) {
237  if ((*getSimTrackCollection ())[i].trackId() == fSimTrackId) return &(*getSimTrackCollection ())[i];
238  }
239  return 0;
240 }
242 int JetMatchingTools::generatorId (unsigned fSimTrackId) {
243  const SimTrack* track = getTrack (fSimTrackId);
244  if (!track) return -1;
245  while (track->noGenpart ()) {
246  if (track->noVertex ()) {
247  std::cerr << "JetMatchingTools::generatorId-> No vertex for track " << *track << std::endl;
248  return -1;
249  }
250  const SimVertex* vertex = &((*getSimVertexCollection ())[track->vertIndex ()]);
251  if (vertex->noParent()) {
252  std::cerr << "JetMatchingTools::generatorId-> No track for vertex " << *vertex << std::endl;
253  return -1;
254  }
255  track = getTrack (vertex->parentIndex ());
256  }
257  return track->genpartIndex ();
258 }
259 
262  if (fGeneratorId > int (getGenParticlesCollection ()->size())) {
263  std::cerr << "JetMatchingTools::getGenParticle-> requested index " << fGeneratorId << " is grater then container size " << getGenParticlesCollection ()->size() << std::endl;
264  return 0;
265  }
266  return reco::GenJet::genParticle ( &(*getGenParticlesCollection ())[fGeneratorId-1]); // knowhow: index is shifted by 1
267 }
268 
270 std::vector <const reco::GenParticle*> JetMatchingTools::getGenParticles (const reco::CaloJet& fJet, bool fVerbose) {
271  std::set <const reco::GenParticle*> result;
272  // follow the chain
273  std::vector <const CaloTower*> towers = getConstituents (fJet) ;
274  for (unsigned itower = 0; itower < towers.size (); ++itower) {
275  std::vector <DetId> detids = getConstituentIds (*(towers[itower])) ;
276  for (unsigned iid = 0; iid < detids.size(); ++iid) {
277  std::vector <const PCaloHit*> phits = getPCaloHits (detids[iid]);
278  for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
279  int trackId = getTrackId (*(phits[iphit]));
280  if (trackId >= 0) {
281  int genId = generatorId (trackId);
282  if (genId >= 0) {
283  const reco::GenParticle* genPart = getGenParticle (genId);
284  if (genPart) {
285  result.insert (genPart);
286  }
287  else if (fVerbose) {
288  std::cerr << "JetMatchingTools::getGenParticles-> Can not convert genId " << genId << " to GenParticle" << std::endl;
289  }
290  }
291  else if (fVerbose) {
292  std::cerr << "JetMatchingTools::getGenParticles-> Can not convert trackId " << trackId << " to genId" << std::endl;
293  }
294  }
295  else if (fVerbose) {
296  std::cerr << "JetMatchingTools::getGenParticles-> Unknown trackId for PCaloHit " << *(phits[iphit]) << std::endl;
297  }
298  }
299  }
300  }
301  return std::vector <const reco::GenParticle*> (result.begin (), result.end());
302 }
303 
305 std::vector <const reco::GenParticle*> JetMatchingTools::getGenParticles (const reco::GenJet& fJet) {
306  return fJet.getGenConstituents ();
307 }
308 
311  double totalEnergy = 0;
312  double lostEnergy = 0;
313  // follow the chain
314  std::vector <const CaloTower*> towers = getConstituents (fJet) ;
315  for (unsigned itower = 0; itower < towers.size (); ++itower) {
316  std::vector<JetConstituent> recHits = getConstituentHits(*(towers[itower]));
317  for (unsigned ihit = 0; ihit < recHits.size(); ++ihit) {
318  double foundSimEnergy = 0;
319  double lostSimEnergy = 0;
320  std::vector <const PCaloHit*> phits = getPCaloHits (recHits[ihit].id);
321  for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
322  double simEnergy = phits[iphit]->energy ();
323  int trackId = getTrackId (*(phits[iphit]));
324  if (trackId < 0 || generatorId (trackId) < 0) lostSimEnergy += simEnergy;
325  else foundSimEnergy += simEnergy;
326  }
327  if (foundSimEnergy > 0 || lostSimEnergy > 0) {
328  totalEnergy += recHits[ihit].energy;
329  lostEnergy += recHits[ihit].energy * lostSimEnergy / (foundSimEnergy + lostSimEnergy);
330  }
331  }
332  }
333  return lostEnergy / totalEnergy;
334 }
335 
337 double JetMatchingTools::overlapEnergyFraction (const std::vector <const reco::GenParticle*>& fObject,
338  const std::vector <const reco::GenParticle*>& fReference) const {
339  if (fObject.empty()) return 0;
340  double totalEnergy = 0;
341  double overlapEnergy = 0;
342  for (unsigned i = 0; i < fObject.size(); ++i) {
343  totalEnergy += fObject [i]->energy();
344  if (find (fReference.begin(), fReference.end(), fObject [i]) != fReference.end ()) overlapEnergy += fObject [i]->energy();
345  }
346  return overlapEnergy / totalEnergy;
347 }
int i
Definition: DBlmapReader.cc:9
const edm::PCaloHitContainer * mHcalSimHitCollection
std::vector< const PCaloHit * > getPCaloHits(DetId fId)
get PCaloHits contributing to the detId
int generatorId(unsigned fSimTrackId)
Generator ID.
std::vector< PCaloHit > PCaloHitContainer
const EBRecHitCollection * mEBRecHitCollection
Jets made from CaloTowers.
Definition: CaloJet.h:29
edm::EDGetTokenT< edm::PCaloHitContainer > input_pcalohits_ebcal_token_
size_t constituentsSize() const
Definition: CaloTower.h:89
const edm::SimVertexContainer * getSimVertexCollection()
const reco::CandidateCollection * getGenParticlesCollection()
const edm::PCaloHitContainer * mEBSimHitCollection
edm::EDGetTokenT< edm::SimVertexContainer > input_simvertex_token_
std::vector< DetId > getConstituentIds(const CaloTower &fTower)
get cells contributing to the tower
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
const HFRecHitCollection * getHFRecHitCollection()
DetId constituent(size_t i) const
Definition: CaloTower.h:90
const reco::CandidateCollection * mGenParticleCollection
assert(m_qm.get())
size_type size() const
Definition: OwnVector.h:254
#define NULL
Definition: scimark2.h:8
const reco::GenParticle * getGenParticle(int fGeneratorId)
GenParticle.
edm::EDGetTokenT< EERecHitCollection > input_eerechits_token_
const edm::PCaloHitContainer * mEESimHitCollection
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
virtual std::vector< CaloTowerPtr > getCaloConstituents() const
get all constituents
Definition: CaloJet.cc:93
edm::EDGetTokenT< reco::CandidateCollection > input_cands_token_
edm::EDGetTokenT< EBRecHitCollection > input_ebrechits_token_
const edm::SimTrackContainer * mSimTrackCollection
const edm::PCaloHitContainer * getEESimHitCollection()
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
bool noGenpart() const
Definition: SimTrack.h:34
int parentIndex() const
Definition: SimVertex.h:33
const edm::PCaloHitContainer * getEBSimHitCollection()
const EERecHitCollection * mEERecHitCollection
JetMatchingTools(const edm::Event &fEvent, edm::ConsumesCollector &&iC)
tuple result
Definition: query.py:137
int genpartIndex() const
index of the corresponding Generator particle in the Event container (-1 if no Genpart) ...
Definition: SimTrack.h:33
const EBRecHitCollection * getEBRecHitCollection()
int geantTrackId() const
Definition: PCaloHit.h:39
tuple handle
Definition: patZpeak.py:22
HcalSubdetector
Definition: HcalAssistant.h:31
bool noVertex() const
Definition: SimTrack.h:30
Jets made from MC generator particles.
Definition: GenJet.h:24
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
Container::value_type value_type
virtual std::vector< const GenParticle * > getGenConstituents() const
get all constituents
Definition: GenJet.cc:58
const EERecHitCollection * getEERecHitCollection()
const HORecHitCollection * getHORecHitCollection()
const HBHERecHitCollection * getHBHERecHitCollection()
int vertIndex() const
index of the vertex in the Event container (-1 if no vertex)
Definition: SimTrack.h:29
edm::EDGetTokenT< HFRecHitCollection > input_hfrechits_token_
Definition: DetId.h:18
edm::EDGetTokenT< HORecHitCollection > input_horechits_token_
const edm::SimVertexContainer * mSimVertexCollection
edm::EDGetTokenT< edm::PCaloHitContainer > input_pcalohits_eecal_token_
edm::EDGetTokenT< edm::SimTrackContainer > input_simtrack_token_
std::vector< SimVertex > SimVertexContainer
tuple simHits
Definition: trackerHits.py:16
double lostEnergyFraction(const reco::CaloJet &fJet)
energy in broken links
const edm::SimTrackContainer * getSimTrackCollection()
std::vector< const reco::GenParticle * > getGenParticles(const reco::CaloJet &fJet, bool fVerbose=true)
GenParticles for CaloJet.
const edm::PCaloHitContainer * getHcalSimHitCollection()
std::vector< JetConstituent > getConstituentHits(const CaloTower &fTower)
get CaloRecHits contributing to the tower
const edm::Event * mEvent
std::vector< const CaloTower * > getConstituents(const reco::CaloJet &fJet)
get towers contributing to CaloJet
const SimTrack * getTrack(unsigned fSimTrackId)
convert trackId to SimTrack
static const GenParticle * genParticle(const reco::Candidate *fConstituent)
convert generic constituent to specific type
Definition: GenJet.cc:38
const HORecHitCollection * mHORecHitCollection
bool noParent() const
Definition: SimVertex.h:34
edm::EDGetTokenT< HBHERecHitCollection > input_hbherechits_token_
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
int getTrackId(const PCaloHit &fHit)
GEANT track ID.
EcalSubdetector
long double T
std::vector< SimTrack > SimTrackContainer
double overlapEnergyFraction(const std::vector< const reco::GenParticle * > &fObject, const std::vector< const reco::GenParticle * > &fReference) const
energy overlap
edm::EDGetTokenT< edm::PCaloHitContainer > input_pcalohits_hcal_token_
tuple size
Write out results.
const HFRecHitCollection * mHFRecHitCollection
const HBHERecHitCollection * mHBHERecHitCollection