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 
17 
18 using namespace edm;
19 
20 namespace {
21 
22  template <class T>
23  const CaloRecHit* getHit (const T& fCollection, DetId fId) {
24  typename T::const_iterator hit = fCollection.find (fId);
25  if (hit != fCollection.end()) return &*hit;
26  return 0;
27  }
28 
29  std::vector <const PCaloHit*> getSimHits (const PCaloHitContainer& fCollection, DetId fId) {
30  std::vector <const PCaloHit*> result;
31  for (unsigned i = 0; i < fCollection.size (); ++i) {
32  if (fCollection[i].id() == fId.rawId()) {
33  result.push_back (&(fCollection[i]));
34  }
35  }
36  return result;
37  }
38 }
39 
41  : mEvent (&fEvent),
42  mEBRecHitCollection (0),
43  mEERecHitCollection (0),
44  mHBHERecHitCollection (0),
45  mHORecHitCollection (0),
46  mHFRecHitCollection (0),
47  mEBSimHitCollection (0),
48  mEESimHitCollection (0),
49  mHcalSimHitCollection (0),
50  mSimTrackCollection (0),
51  mSimVertexCollection (0),
52  mGenParticleCollection (0)
53 {}
54 
56 
58  if (!mEBRecHitCollection) {
60  mEvent->getByLabel (edm::InputTag ("ecalRecHit:EcalRecHitsEB"), recHits);
61  mEBRecHitCollection = &*recHits;
62  }
63  return mEBRecHitCollection;
64 }
66  if (!mEERecHitCollection) {
68  mEvent->getByLabel (edm::InputTag ("ecalRecHit:EcalRecHitsEE"), recHits);
69  mEERecHitCollection = &*recHits;
70  }
71  return mEERecHitCollection;
72 }
74  if (!mHBHERecHitCollection) {
76  mEvent->getByLabel (edm::InputTag ("hbhereco"), recHits);
77  mHBHERecHitCollection = &*recHits;
78  }
79  return mHBHERecHitCollection;
80 }
82  if (!mHORecHitCollection) {
84  mEvent->getByLabel (edm::InputTag ("horeco"), recHits);
85  mHORecHitCollection = &*recHits;
86  }
87  return mHORecHitCollection;
88 }
90  if (!mHFRecHitCollection) {
92  mEvent->getByLabel (edm::InputTag ("hfreco"), recHits);
93  mHFRecHitCollection = &*recHits;
94  }
95  return mHFRecHitCollection;
96 }
98  if (!mEBSimHitCollection) {
100  mEvent->getByLabel (edm::InputTag ("g4SimHits:EcalHitsEB"), simHits);
102  }
103  return mEBSimHitCollection;
104 }
106  if (!mEESimHitCollection) {
108  mEvent->getByLabel (edm::InputTag ("g4SimHits:EcalHitsEE"), simHits);
110  }
111  return mEESimHitCollection;
112 }
114  if (!mHcalSimHitCollection) {
116  mEvent->getByLabel (edm::InputTag ("g4SimHits:HcalHits"), simHits);
118  }
119  return mHcalSimHitCollection;
120 }
122  if (!mSimTrackCollection) {
124  mEvent->getByLabel (edm::InputTag ("g4SimHits"), simHits);
126  }
127  return mSimTrackCollection;
128 }
130  if (!mSimVertexCollection) {
132  mEvent->getByLabel (edm::InputTag ("g4SimHits"), simHits);
134  }
135  return mSimVertexCollection;
136 }
138  if (!mGenParticleCollection) {
140  mEvent->getByLabel (edm::InputTag ("genParticleCandidates"), handle);
142  }
143  return mGenParticleCollection;
144 }
145 
147 std::vector <const CaloTower*> JetMatchingTools::getConstituents (const reco::CaloJet& fJet ) {
148  std::vector <const CaloTower*> result;
149  std::vector<CaloTowerPtr> constituents = fJet.getCaloConstituents ();
150  for (unsigned i = 0; i < constituents.size(); ++i) result.push_back (&*(constituents[i]));
151  return result;
152 }
154 std::vector <const CaloRecHit*> JetMatchingTools::getConstituents (const CaloTower& fTower) {
155  std::vector <const CaloRecHit*> result;
156  for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
157  DetId id = fTower.constituent (i);
158  const CaloRecHit* hit = 0;
159  if (id.det () == DetId::Ecal) {
160  if ((EcalSubdetector) id.subdetId () == EcalBarrel) {
161  hit = getHit (*getEBRecHitCollection (), id);
162  }
163  else if ((EcalSubdetector) id.subdetId () == EcalEndcap) {
164  hit = getHit (*getEERecHitCollection (), id);
165  }
166  }
167  else if (id.det () == DetId::Hcal) {
168  if ((HcalSubdetector) id.subdetId () == HcalBarrel || (HcalSubdetector) id.subdetId () == HcalEndcap) {
169  hit = getHit (*getHBHERecHitCollection (), id);
170  }
171  else if ((HcalSubdetector) id.subdetId () == HcalOuter) {
172  hit = getHit (*getHORecHitCollection (), id);
173  }
174  if ((HcalSubdetector) id.subdetId () == HcalForward) {
175  hit = getHit (*getHFRecHitCollection (), id);
176  }
177  }
178  if (hit) result.push_back (hit);
179  else std::cerr << "Can not find rechit for id " << id.rawId () << std::endl;
180  }
181  return result;
182 }
183 
185 std::vector <DetId> JetMatchingTools::getConstituentIds (const CaloTower& fTower) {
186  std::vector <DetId> result;
187  for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
188  DetId id = fTower.constituent (i);
189  result.push_back (id);
190  }
191  return result;
192 }
194 std::vector <const PCaloHit*> JetMatchingTools::getPCaloHits (DetId fId) {
195  std::vector <const PCaloHit*> result;
196  if (fId.det () == DetId::Ecal) {
197  if ((EcalSubdetector) fId.subdetId () == EcalBarrel) {
198  result = getSimHits (*getEBSimHitCollection (), fId);
199  }
200  else if ((EcalSubdetector) fId.subdetId () == EcalEndcap) {
201  result = getSimHits (*getEESimHitCollection (), fId);
202  }
203  }
204  else if (fId.det () == DetId::Hcal) {
205  result = getSimHits (*getHcalSimHitCollection (), fId);
206  }
207  return result;
208 }
211  return fHit.geantTrackId ();
212 }
214 const SimTrack* JetMatchingTools::getTrack (unsigned fSimTrackId) {
215  for (unsigned i = 0; i < getSimTrackCollection ()->size (); ++i) {
216  if ((*getSimTrackCollection ())[i].trackId() == fSimTrackId) return &(*getSimTrackCollection ())[i];
217  }
218  return 0;
219 }
221 int JetMatchingTools::generatorId (unsigned fSimTrackId) {
222  const SimTrack* track = getTrack (fSimTrackId);
223  if (!track) return -1;
224  while (track->noGenpart ()) {
225  if (track->noVertex ()) {
226  std::cerr << "JetMatchingTools::generatorId-> No vertex for track " << *track << std::endl;
227  return -1;
228  }
229  const SimVertex* vertex = &((*getSimVertexCollection ())[track->vertIndex ()]);
230  if (vertex->noParent()) {
231  std::cerr << "JetMatchingTools::generatorId-> No track for vertex " << *vertex << std::endl;
232  return -1;
233  }
234  track = getTrack (vertex->parentIndex ());
235  }
236  return track->genpartIndex ();
237 }
238 
241  if (fGeneratorId > int (getGenParticlesCollection ()->size())) {
242  std::cerr << "JetMatchingTools::getGenParticle-> requested index " << fGeneratorId << " is grater then container size " << getGenParticlesCollection ()->size() << std::endl;
243  return 0;
244  }
245  return reco::GenJet::genParticle ( &(*getGenParticlesCollection ())[fGeneratorId-1]); // knowhow: index is shifted by 1
246 }
247 
249 std::vector <const reco::GenParticle*> JetMatchingTools::getGenParticles (const reco::CaloJet& fJet, bool fVerbose) {
250  std::set <const reco::GenParticle*> result;
251  // follow the chain
252  std::vector <const CaloTower*> towers = getConstituents (fJet) ;
253  for (unsigned itower = 0; itower < towers.size (); ++itower) {
254  std::vector <DetId> detids = getConstituentIds (*(towers[itower])) ;
255  for (unsigned iid = 0; iid < detids.size(); ++iid) {
256  std::vector <const PCaloHit*> phits = getPCaloHits (detids[iid]);
257  for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
258  int trackId = getTrackId (*(phits[iphit]));
259  if (trackId >= 0) {
260  int genId = generatorId (trackId);
261  if (genId >= 0) {
262  const reco::GenParticle* genPart = getGenParticle (genId);
263  if (genPart) {
264  result.insert (genPart);
265  }
266  else if (fVerbose) {
267  std::cerr << "JetMatchingTools::getGenParticles-> Can not convert genId " << genId << " to GenParticle" << std::endl;
268  }
269  }
270  else if (fVerbose) {
271  std::cerr << "JetMatchingTools::getGenParticles-> Can not convert trackId " << trackId << " to genId" << std::endl;
272  }
273  }
274  else if (fVerbose) {
275  std::cerr << "JetMatchingTools::getGenParticles-> Unknown trackId for PCaloHit " << *(phits[iphit]) << std::endl;
276  }
277  }
278  }
279  }
280  return std::vector <const reco::GenParticle*> (result.begin (), result.end());
281 }
282 
284 std::vector <const reco::GenParticle*> JetMatchingTools::getGenParticles (const reco::GenJet& fJet) {
285  return fJet.getGenConstituents ();
286 }
287 
290  double totalEnergy = 0;
291  double lostEnergy = 0;
292  // follow the chain
293  std::vector <const CaloTower*> towers = getConstituents (fJet) ;
294  for (unsigned itower = 0; itower < towers.size (); ++itower) {
295  std::vector <const CaloRecHit*> recHits = getConstituents (*(towers[itower]));
296  for (unsigned ihit = 0; ihit < recHits.size(); ++ihit) {
297  double foundSimEnergy = 0;
298  double lostSimEnergy = 0;
299  std::vector <const PCaloHit*> phits = getPCaloHits (recHits[ihit]->detid());
300  for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
301  double simEnergy = phits[iphit]->energy ();
302  int trackId = getTrackId (*(phits[iphit]));
303  if (trackId < 0 || generatorId (trackId) < 0) lostSimEnergy += simEnergy;
304  else foundSimEnergy += simEnergy;
305  }
306  if (foundSimEnergy > 0 || lostSimEnergy > 0) {
307  totalEnergy += recHits[ihit]->energy ();
308  lostEnergy += recHits[ihit]->energy () * lostSimEnergy / (foundSimEnergy + lostSimEnergy);
309  }
310  }
311  }
312  return lostEnergy / totalEnergy;
313 }
314 
316 double JetMatchingTools::overlapEnergyFraction (const std::vector <const reco::GenParticle*>& fObject,
317  const std::vector <const reco::GenParticle*>& fReference) const {
318  if (fObject.empty()) return 0;
319  double totalEnergy = 0;
320  double overlapEnergy = 0;
321  for (unsigned i = 0; i < fObject.size(); ++i) {
322  totalEnergy += fObject [i]->energy();
323  if (find (fReference.begin(), fReference.end(), fObject [i]) != fReference.end ()) overlapEnergy += fObject [i]->energy();
324  }
325  return overlapEnergy / totalEnergy;
326 }
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
size_t constituentsSize() const
Definition: CaloTower.h:72
const edm::SimVertexContainer * getSimVertexCollection()
const reco::CandidateCollection * getGenParticlesCollection()
const edm::PCaloHitContainer * mEBSimHitCollection
std::vector< DetId > getConstituentIds(const CaloTower &fTower)
get cells contributing to the tower
const HFRecHitCollection * getHFRecHitCollection()
DetId constituent(size_t i) const
Definition: CaloTower.h:73
const reco::CandidateCollection * mGenParticleCollection
size_type size() const
Definition: OwnVector.h:247
const reco::GenParticle * getGenParticle(int fGeneratorId)
GenParticle.
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
const edm::SimTrackContainer * mSimTrackCollection
const edm::PCaloHitContainer * getEESimHitCollection()
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
JetMatchingTools(const edm::Event &fEvent)
bool noGenpart() const
Definition: SimTrack.h:34
int parentIndex() const
Definition: SimVertex.h:33
const edm::PCaloHitContainer * getEBSimHitCollection()
const EERecHitCollection * mEERecHitCollection
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
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
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
Definition: DetId.h:18
const edm::SimVertexContainer * mSimVertexCollection
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()
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
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
tuple size
Write out results.
const HFRecHitCollection * mHFRecHitCollection
const HBHERecHitCollection * mHBHERecHitCollection