CMS 3D CMS Logo

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())
22  return &*hit;
23  return nullptr;
24  }
25 
26  std::vector<const PCaloHit*> getSimHits(const PCaloHitContainer& fCollection, DetId fId) {
27  std::vector<const PCaloHit*> result;
28  for (unsigned i = 0; i < fCollection.size(); ++i) {
29  if (fCollection[i].id() == fId.rawId()) {
30  result.push_back(&(fCollection[i]));
31  }
32  }
33  return result;
34  }
35 } // namespace
36 
38  : mEvent(&fEvent),
39  mEBRecHitCollection(nullptr),
40  mEERecHitCollection(nullptr),
41  mHBHERecHitCollection(nullptr),
42  mHORecHitCollection(nullptr),
43  mHFRecHitCollection(nullptr),
44  mEBSimHitCollection(nullptr),
45  mEESimHitCollection(nullptr),
46  mHcalSimHitCollection(nullptr),
47  mSimTrackCollection(nullptr),
48  mSimVertexCollection(nullptr),
49  mGenParticleCollection(nullptr) {
50  input_ebrechits_token_ = iC.mayConsume<EBRecHitCollection>(edm::InputTag("ecalRecHit:EcalRecHitsEB"));
51  input_eerechits_token_ = iC.mayConsume<EERecHitCollection>(edm::InputTag("ecalRecHit:EcalRecHitsEE"));
52  input_hbherechits_token_ = iC.mayConsume<HBHERecHitCollection>(edm::InputTag("hbhereco"));
53  input_horechits_token_ = iC.mayConsume<HORecHitCollection>(edm::InputTag("horeco"));
54  input_hfrechits_token_ = iC.mayConsume<HFRecHitCollection>(edm::InputTag("hfreco"));
55  input_pcalohits_ebcal_token_ = iC.mayConsume<edm::PCaloHitContainer>(edm::InputTag("g4SimHits:EcalHitsEB"));
56  input_pcalohits_eecal_token_ = iC.mayConsume<edm::PCaloHitContainer>(edm::InputTag("g4SimHits:EcalHitsEE"));
57  input_pcalohits_hcal_token_ = iC.mayConsume<edm::PCaloHitContainer>(edm::InputTag("g4SimHits:HcalHits"));
58  input_simtrack_token_ = iC.mayConsume<edm::SimTrackContainer>(edm::InputTag("g4SimHits"));
59  input_simvertex_token_ = iC.mayConsume<edm::SimVertexContainer>(edm::InputTag("g4SimHits"));
60  input_cands_token_ = iC.mayConsume<reco::CandidateCollection>(edm::InputTag("genParticleCandidates"));
61 }
62 
64 
66  if (!mEBRecHitCollection) {
70  }
71  return mEBRecHitCollection;
72 }
74  if (!mEERecHitCollection) {
78  }
79  return mEERecHitCollection;
80 }
82  if (!mHBHERecHitCollection) {
86  }
87  return mHBHERecHitCollection;
88 }
90  if (!mHORecHitCollection) {
94  }
95  return mHORecHitCollection;
96 }
98  if (!mHFRecHitCollection) {
102  }
103  return mHFRecHitCollection;
104 }
106  if (!mEBSimHitCollection) {
110  }
111  return mEBSimHitCollection;
112 }
114  if (!mEESimHitCollection) {
118  }
119  return mEESimHitCollection;
120 }
122  if (!mHcalSimHitCollection) {
126  }
127  return mHcalSimHitCollection;
128 }
130  if (!mSimTrackCollection) {
134  }
135  return mSimTrackCollection;
136 }
138  if (!mSimVertexCollection) {
142  }
143  return mSimVertexCollection;
144 }
146  if (!mGenParticleCollection) {
150  }
151  return mGenParticleCollection;
152 }
153 
155 std::vector<const CaloTower*> JetMatchingTools::getConstituents(const reco::CaloJet& fJet) {
156  std::vector<const CaloTower*> result;
157  std::vector<CaloTowerPtr> constituents = fJet.getCaloConstituents();
158  for (unsigned i = 0; i < constituents.size(); ++i)
159  result.push_back(&*(constituents[i]));
160  return result;
161 }
162 
164 std::vector<JetMatchingTools::JetConstituent> JetMatchingTools::getConstituentHits(const CaloTower& fTower) {
165  std::vector<JetConstituent> result;
166 
167  for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
168  DetId id = fTower.constituent(i);
169 
170  if (id.det() == DetId::Ecal) {
171  const EcalRecHit* hit = nullptr;
172 
173  if ((EcalSubdetector)id.subdetId() == EcalBarrel) {
174  hit = getHit(*getEBRecHitCollection(), id);
175  } else if ((EcalSubdetector)id.subdetId() == EcalEndcap) {
176  hit = getHit(*getEERecHitCollection(), id);
177  }
178 
179  assert(hit != nullptr);
180  if (hit)
181  result.push_back(JetConstituent(*hit));
182  else
183  std::cerr << "Can not find rechit for id " << id.rawId() << std::endl;
184  } else if (id.det() == DetId::Hcal) {
185  const CaloRecHit* hit = nullptr;
186 
187  if ((HcalSubdetector)id.subdetId() == HcalBarrel || (HcalSubdetector)id.subdetId() == HcalEndcap) {
188  hit = getHit(*getHBHERecHitCollection(), id);
189  } else if ((HcalSubdetector)id.subdetId() == HcalOuter) {
190  hit = getHit(*getHORecHitCollection(), id);
191  }
192  if ((HcalSubdetector)id.subdetId() == HcalForward) {
193  hit = getHit(*getHFRecHitCollection(), id);
194  }
195 
196  if (hit)
197  result.push_back(JetConstituent(*hit));
198  else
199  std::cerr << "Can not find rechit for id " << id.rawId() << std::endl;
200  }
201  }
202 
203  return result;
204 }
205 
207 std::vector<DetId> JetMatchingTools::getConstituentIds(const CaloTower& fTower) {
208  std::vector<DetId> result;
209  for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
210  DetId id = fTower.constituent(i);
211  result.push_back(id);
212  }
213  return result;
214 }
216 std::vector<const PCaloHit*> JetMatchingTools::getPCaloHits(DetId fId) {
217  std::vector<const PCaloHit*> result;
218  if (fId.det() == DetId::Ecal) {
219  if ((EcalSubdetector)fId.subdetId() == EcalBarrel) {
220  result = getSimHits(*getEBSimHitCollection(), fId);
221  } else if ((EcalSubdetector)fId.subdetId() == EcalEndcap) {
222  result = getSimHits(*getEESimHitCollection(), fId);
223  }
224  } else if (fId.det() == DetId::Hcal) {
225  result = getSimHits(*getHcalSimHitCollection(), fId);
226  }
227  return result;
228 }
230 int JetMatchingTools::getTrackId(const PCaloHit& fHit) { return fHit.geantTrackId(); }
232 const SimTrack* JetMatchingTools::getTrack(unsigned fSimTrackId) {
233  for (unsigned i = 0; i < getSimTrackCollection()->size(); ++i) {
234  if ((*getSimTrackCollection())[i].trackId() == fSimTrackId)
235  return &(*getSimTrackCollection())[i];
236  }
237  return nullptr;
238 }
240 int JetMatchingTools::generatorId(unsigned fSimTrackId) {
241  const SimTrack* track = getTrack(fSimTrackId);
242  if (!track)
243  return -1;
244  while (track->noGenpart()) {
245  if (track->noVertex()) {
246  std::cerr << "JetMatchingTools::generatorId-> No vertex for track " << *track << std::endl;
247  return -1;
248  }
249  const SimVertex* vertex = &((*getSimVertexCollection())[track->vertIndex()]);
250  if (vertex->noParent()) {
251  std::cerr << "JetMatchingTools::generatorId-> No track for vertex " << *vertex << std::endl;
252  return -1;
253  }
254  track = getTrack(vertex->parentIndex());
255  }
256  return track->genpartIndex();
257 }
258 
261  if (fGeneratorId > int(getGenParticlesCollection()->size())) {
262  std::cerr << "JetMatchingTools::getGenParticle-> requested index " << fGeneratorId
263  << " is grater then container size " << getGenParticlesCollection()->size() << std::endl;
264  return nullptr;
265  }
267  &(*getGenParticlesCollection())[fGeneratorId - 1]); // knowhow: index is shifted by 1
268 }
269 
271 std::vector<const reco::GenParticle*> JetMatchingTools::getGenParticles(const reco::CaloJet& fJet, bool fVerbose) {
272  std::set<const reco::GenParticle*> result;
273  // follow the chain
274  std::vector<const CaloTower*> towers = getConstituents(fJet);
275  for (unsigned itower = 0; itower < towers.size(); ++itower) {
276  std::vector<DetId> detids = getConstituentIds(*(towers[itower]));
277  for (unsigned iid = 0; iid < detids.size(); ++iid) {
278  std::vector<const PCaloHit*> phits = getPCaloHits(detids[iid]);
279  for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
280  int trackId = getTrackId(*(phits[iphit]));
281  if (trackId >= 0) {
282  int genId = generatorId(trackId);
283  if (genId >= 0) {
284  const reco::GenParticle* genPart = getGenParticle(genId);
285  if (genPart) {
286  result.insert(genPart);
287  } else if (fVerbose) {
288  std::cerr << "JetMatchingTools::getGenParticles-> Can not convert genId " << genId << " to GenParticle"
289  << std::endl;
290  }
291  } else if (fVerbose) {
292  std::cerr << "JetMatchingTools::getGenParticles-> Can not convert trackId " << trackId << " to genId"
293  << std::endl;
294  }
295  } else if (fVerbose) {
296  std::cerr << "JetMatchingTools::getGenParticles-> Unknown trackId for PCaloHit " << *(phits[iphit])
297  << std::endl;
298  }
299  }
300  }
301  }
302  return std::vector<const reco::GenParticle*>(result.begin(), result.end());
303 }
304 
306 std::vector<const reco::GenParticle*> JetMatchingTools::getGenParticles(const reco::GenJet& fJet) {
307  return fJet.getGenConstituents();
308 }
309 
312  double totalEnergy = 0;
313  double lostEnergy = 0;
314  // follow the chain
315  std::vector<const CaloTower*> towers = getConstituents(fJet);
316  for (unsigned itower = 0; itower < towers.size(); ++itower) {
317  std::vector<JetConstituent> recHits = getConstituentHits(*(towers[itower]));
318  for (unsigned ihit = 0; ihit < recHits.size(); ++ihit) {
319  double foundSimEnergy = 0;
320  double lostSimEnergy = 0;
321  std::vector<const PCaloHit*> phits = getPCaloHits(recHits[ihit].id);
322  for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
323  double simEnergy = phits[iphit]->energy();
324  int trackId = getTrackId(*(phits[iphit]));
325  if (trackId < 0 || generatorId(trackId) < 0)
326  lostSimEnergy += simEnergy;
327  else
328  foundSimEnergy += simEnergy;
329  }
330  if (foundSimEnergy > 0 || lostSimEnergy > 0) {
331  totalEnergy += recHits[ihit].energy;
332  lostEnergy += recHits[ihit].energy * lostSimEnergy / (foundSimEnergy + lostSimEnergy);
333  }
334  }
335  }
336  return lostEnergy / totalEnergy;
337 }
338 
340 double JetMatchingTools::overlapEnergyFraction(const std::vector<const reco::GenParticle*>& fObject,
341  const std::vector<const reco::GenParticle*>& fReference) const {
342  if (fObject.empty())
343  return 0;
344  double totalEnergy = 0;
345  double overlapEnergy = 0;
346  for (unsigned i = 0; i < fObject.size(); ++i) {
347  totalEnergy += fObject[i]->energy();
348  if (find(fReference.begin(), fReference.end(), fObject[i]) != fReference.end())
349  overlapEnergy += fObject[i]->energy();
350  }
351  return overlapEnergy / totalEnergy;
352 }
size
Write out results.
virtual std::vector< CaloTowerPtr > getCaloConstituents() const
get all constituents
Definition: CaloJet.cc:78
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:27
edm::EDGetTokenT< edm::PCaloHitContainer > input_pcalohits_ebcal_token_
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
const HFRecHitCollection * getHFRecHitCollection()
const reco::CandidateCollection * mGenParticleCollection
double overlapEnergyFraction(const std::vector< const reco::GenParticle *> &fObject, const std::vector< const reco::GenParticle *> &fReference) const
energy overlap
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
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:19
assert(be >=bs)
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
edm::EDGetTokenT< reco::CandidateCollection > input_cands_token_
edm::EDGetTokenT< EBRecHitCollection > input_ebrechits_token_
const edm::SimTrackContainer * mSimTrackCollection
const edm::PCaloHitContainer * getEESimHitCollection()
TupleMultiplicity< TrackerTraits > const *__restrict__ TrackingRecHitSoAConstView< TrackerTraits > TrackerTraits::tindex_type *__restrict__ double *__restrict__ phits
const edm::PCaloHitContainer * getEBSimHitCollection()
const EERecHitCollection * mEERecHitCollection
JetMatchingTools(const edm::Event &fEvent, edm::ConsumesCollector &&iC)
size_type size() const
Definition: OwnVector.h:300
const EBRecHitCollection * getEBRecHitCollection()
HcalSubdetector
Definition: HcalAssistant.h:31
int geantTrackId() const
Definition: PCaloHit.h:33
Jets made from MC generator particles.
Definition: GenJet.h:23
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
const EERecHitCollection * getEERecHitCollection()
const HORecHitCollection * getHORecHitCollection()
const HBHERecHitCollection * getHBHERecHitCollection()
DetId constituent(size_t i) const
Definition: CaloTower.h:126
edm::EDGetTokenT< HFRecHitCollection > input_hfrechits_token_
Definition: DetId.h:17
edm::EDGetTokenT< HORecHitCollection > input_horechits_token_
const edm::SimVertexContainer * mSimVertexCollection
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
edm::EDGetTokenT< edm::PCaloHitContainer > input_pcalohits_eecal_token_
edm::EDGetTokenT< edm::SimTrackContainer > input_simtrack_token_
size_t constituentsSize() const
Definition: CaloTower.h:125
std::vector< SimVertex > SimVertexContainer
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
HLT enums.
virtual std::vector< const GenParticle * > getGenConstituents() const
get all constituents
Definition: GenJet.cc:51
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:28
const HORecHitCollection * mHORecHitCollection
edm::EDGetTokenT< HBHERecHitCollection > input_hbherechits_token_
int getTrackId(const PCaloHit &fHit)
GEANT track ID.
EcalSubdetector
long double T
std::vector< SimTrack > SimTrackContainer
edm::EDGetTokenT< edm::PCaloHitContainer > input_pcalohits_hcal_token_
const HFRecHitCollection * mHFRecHitCollection
const HBHERecHitCollection * mHBHERecHitCollection