CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/RecoJets/JetProducers/src/JetMatchingTools.cc

Go to the documentation of this file.
00001 #include <set>
00002 
00003 #include "RecoJets/JetProducers/interface/JetMatchingTools.h"
00004 
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "DataFormats/Common/interface/Handle.h"
00007 
00008 #include "DataFormats/JetReco/interface/CaloJet.h"
00009 #include "DataFormats/JetReco/interface/GenJet.h"
00010 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00011 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00012 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00013 #include "DataFormats/CaloTowers/interface/CaloTowerCollection.h"
00014 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00015 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00016 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00017 
00018 using namespace edm;
00019 
00020 namespace {
00021 
00022   template <class T>
00023   const CaloRecHit* getHit (const T& fCollection, DetId fId) {
00024     typename T::const_iterator hit = fCollection.find (fId);
00025     if (hit != fCollection.end()) return &*hit;
00026     return 0;
00027   }
00028 
00029   std::vector <const PCaloHit*> getSimHits (const PCaloHitContainer& fCollection, DetId fId) {
00030     std::vector <const PCaloHit*> result;
00031     for (unsigned i = 0; i < fCollection.size (); ++i) {
00032       if (fCollection[i].id() == fId.rawId()) {
00033         result.push_back (&(fCollection[i]));
00034       }
00035     }
00036     return result;
00037   }
00038 }
00039 
00040 JetMatchingTools::JetMatchingTools (const edm::Event& fEvent) 
00041   : mEvent (&fEvent),
00042     mEBRecHitCollection (0),
00043     mEERecHitCollection (0),
00044     mHBHERecHitCollection (0),
00045     mHORecHitCollection (0),
00046     mHFRecHitCollection (0),
00047     mEBSimHitCollection (0),
00048     mEESimHitCollection (0),
00049     mHcalSimHitCollection (0),
00050     mSimTrackCollection (0),
00051     mSimVertexCollection (0),
00052     mGenParticleCollection (0)
00053 {}
00054 
00055 JetMatchingTools::~JetMatchingTools () {}
00056 
00057 const EBRecHitCollection* JetMatchingTools::getEBRecHitCollection () {
00058   if (!mEBRecHitCollection) {
00059     edm::Handle<EBRecHitCollection> recHits;
00060     mEvent->getByLabel (edm::InputTag ("ecalRecHit:EcalRecHitsEB"), recHits);
00061     mEBRecHitCollection = &*recHits;
00062   }
00063   return mEBRecHitCollection;
00064 }
00065 const EERecHitCollection* JetMatchingTools::getEERecHitCollection () {
00066   if (!mEERecHitCollection) {
00067     edm::Handle<EERecHitCollection> recHits;
00068     mEvent->getByLabel (edm::InputTag ("ecalRecHit:EcalRecHitsEE"), recHits);
00069     mEERecHitCollection = &*recHits;
00070   }
00071   return mEERecHitCollection;
00072 }
00073 const HBHERecHitCollection* JetMatchingTools::getHBHERecHitCollection () {
00074   if (!mHBHERecHitCollection) {
00075     edm::Handle<HBHERecHitCollection> recHits;
00076     mEvent->getByLabel (edm::InputTag ("hbhereco"), recHits);
00077     mHBHERecHitCollection = &*recHits;
00078   }
00079   return mHBHERecHitCollection;
00080 }
00081 const HORecHitCollection* JetMatchingTools::getHORecHitCollection () {
00082   if (!mHORecHitCollection) {
00083     edm::Handle<HORecHitCollection> recHits;
00084     mEvent->getByLabel (edm::InputTag ("horeco"), recHits);
00085     mHORecHitCollection = &*recHits;
00086   }
00087   return mHORecHitCollection;
00088 }
00089 const HFRecHitCollection* JetMatchingTools::getHFRecHitCollection () {
00090   if (!mHFRecHitCollection) {
00091     edm::Handle<HFRecHitCollection> recHits;
00092     mEvent->getByLabel (edm::InputTag ("hfreco"), recHits);
00093     mHFRecHitCollection = &*recHits;
00094   }
00095   return mHFRecHitCollection;
00096 }
00097 const PCaloHitContainer* JetMatchingTools::getEBSimHitCollection () {
00098   if (!mEBSimHitCollection) {
00099     edm::Handle<PCaloHitContainer> simHits;
00100     mEvent->getByLabel (edm::InputTag ("g4SimHits:EcalHitsEB"), simHits);
00101     mEBSimHitCollection = &*simHits;
00102   }
00103   return mEBSimHitCollection;
00104 }
00105 const PCaloHitContainer* JetMatchingTools::getEESimHitCollection () {
00106   if (!mEESimHitCollection) {
00107     edm::Handle<PCaloHitContainer> simHits;
00108     mEvent->getByLabel (edm::InputTag ("g4SimHits:EcalHitsEE"), simHits);
00109     mEESimHitCollection = &*simHits;
00110   }
00111   return mEESimHitCollection;
00112 }
00113 const PCaloHitContainer* JetMatchingTools::getHcalSimHitCollection () {
00114   if (!mHcalSimHitCollection) {
00115     edm::Handle<PCaloHitContainer> simHits;
00116     mEvent->getByLabel (edm::InputTag ("g4SimHits:HcalHits"), simHits);
00117     mHcalSimHitCollection = &*simHits;
00118   }
00119   return mHcalSimHitCollection;
00120 }
00121 const SimTrackContainer* JetMatchingTools::getSimTrackCollection () {
00122   if (!mSimTrackCollection) {
00123     edm::Handle<SimTrackContainer> simHits;
00124     mEvent->getByLabel (edm::InputTag ("g4SimHits"), simHits);
00125     mSimTrackCollection = &*simHits;
00126   }
00127   return mSimTrackCollection;
00128 }
00129 const SimVertexContainer* JetMatchingTools::getSimVertexCollection () {
00130   if (!mSimVertexCollection) {
00131     edm::Handle<SimVertexContainer> simHits;
00132     mEvent->getByLabel (edm::InputTag ("g4SimHits"), simHits);
00133     mSimVertexCollection = &*simHits;
00134   }
00135   return mSimVertexCollection;
00136 }
00137 const reco::CandidateCollection* JetMatchingTools::getGenParticlesCollection () {
00138   if (!mGenParticleCollection) {
00139     edm::Handle<reco::CandidateCollection> handle;
00140     mEvent->getByLabel (edm::InputTag ("genParticleCandidates"), handle);
00141     mGenParticleCollection = &*handle;
00142   }
00143   return mGenParticleCollection;
00144 }
00145 
00147 std::vector <const CaloTower*> JetMatchingTools::getConstituents (const reco::CaloJet& fJet ) {
00148   std::vector <const CaloTower*> result;
00149   std::vector<CaloTowerPtr> constituents = fJet.getCaloConstituents ();
00150   for (unsigned i = 0; i < constituents.size(); ++i) result.push_back (&*(constituents[i]));
00151   return result;
00152 }
00154 std::vector <const CaloRecHit*> JetMatchingTools::getConstituents (const CaloTower& fTower) {
00155   std::vector <const CaloRecHit*> result;
00156   for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
00157     DetId id = fTower.constituent (i);
00158     const CaloRecHit* hit = 0;
00159     if (id.det () == DetId::Ecal) {
00160       if ((EcalSubdetector) id.subdetId () == EcalBarrel) {
00161         hit = getHit (*getEBRecHitCollection (), id);
00162       } 
00163       else if ((EcalSubdetector) id.subdetId () == EcalEndcap) {
00164         hit = getHit (*getEERecHitCollection (), id);
00165       }
00166     }
00167     else if (id.det () == DetId::Hcal) {
00168       if ((HcalSubdetector) id.subdetId () == HcalBarrel || (HcalSubdetector) id.subdetId () == HcalEndcap) {
00169         hit = getHit (*getHBHERecHitCollection (), id);
00170       }
00171       else if ((HcalSubdetector) id.subdetId () == HcalOuter) {
00172         hit = getHit (*getHORecHitCollection (), id);
00173       }
00174       if ((HcalSubdetector) id.subdetId () == HcalForward) {
00175         hit = getHit (*getHFRecHitCollection (), id);
00176       }
00177     }
00178     if (hit) result.push_back (hit);
00179     else std::cerr << "Can not find rechit for id " << id.rawId () << std::endl;
00180   } 
00181   return result;
00182 }
00183 
00185 std::vector <DetId> JetMatchingTools::getConstituentIds (const CaloTower& fTower) {
00186   std::vector <DetId> result;
00187   for (unsigned i = 0; i < fTower.constituentsSize(); ++i) {
00188     DetId id = fTower.constituent (i);
00189     result.push_back (id);
00190   }
00191   return result;
00192 }
00194 std::vector <const PCaloHit*> JetMatchingTools::getPCaloHits (DetId fId) {
00195   std::vector <const PCaloHit*> result;
00196   if (fId.det () == DetId::Ecal) {
00197     if ((EcalSubdetector) fId.subdetId () == EcalBarrel) {
00198       result = getSimHits (*getEBSimHitCollection (), fId);
00199     } 
00200     else if ((EcalSubdetector) fId.subdetId () == EcalEndcap) {
00201       result = getSimHits (*getEESimHitCollection (), fId);
00202     }
00203   }
00204   else if (fId.det () == DetId::Hcal) {
00205     result = getSimHits (*getHcalSimHitCollection (), fId);
00206   }
00207   return result;
00208 }
00210 int JetMatchingTools::getTrackId (const PCaloHit& fHit) {
00211   return fHit.geantTrackId ();
00212 }
00214 const SimTrack* JetMatchingTools::getTrack (unsigned fSimTrackId) {
00215   for (unsigned i = 0; i < getSimTrackCollection ()->size (); ++i) {
00216     if ((*getSimTrackCollection ())[i].trackId() == fSimTrackId) return &(*getSimTrackCollection ())[i];
00217   }
00218   return 0;
00219 }
00221 int JetMatchingTools::generatorId (unsigned fSimTrackId) {
00222   const SimTrack* track = getTrack (fSimTrackId);
00223   if (!track) return -1;
00224   while (track->noGenpart ()) {
00225     if (track->noVertex ()) {
00226       std::cerr << "JetMatchingTools::generatorId-> No vertex for track " << *track << std::endl;
00227       return -1;
00228     }
00229     const SimVertex* vertex = &((*getSimVertexCollection ())[track->vertIndex ()]);
00230     if (vertex->noParent()) {
00231       std::cerr << "JetMatchingTools::generatorId-> No track for vertex " << *vertex << std::endl;
00232       return -1;
00233     }
00234     track = getTrack (vertex->parentIndex ());
00235   }
00236   return track->genpartIndex ();
00237 }
00238 
00240 const reco::GenParticle* JetMatchingTools::getGenParticle (int fGeneratorId) {
00241   if (fGeneratorId > int (getGenParticlesCollection ()->size())) {
00242     std::cerr << "JetMatchingTools::getGenParticle-> requested index " << fGeneratorId << " is grater then container size " << getGenParticlesCollection ()->size() << std::endl;
00243     return 0;
00244   }
00245   return reco::GenJet::genParticle ( &(*getGenParticlesCollection ())[fGeneratorId-1]); // knowhow: index is shifted by 1
00246 }
00247 
00249 std::vector <const reco::GenParticle*> JetMatchingTools::getGenParticles (const reco::CaloJet& fJet, bool fVerbose) {
00250   std::set <const reco::GenParticle*> result;
00251   // follow the chain
00252   std::vector <const CaloTower*> towers = getConstituents (fJet) ;
00253   for (unsigned itower = 0; itower < towers.size (); ++itower) {
00254     std::vector <DetId> detids = getConstituentIds (*(towers[itower])) ;
00255     for (unsigned iid = 0; iid < detids.size(); ++iid) {
00256       std::vector <const PCaloHit*> phits = getPCaloHits (detids[iid]);
00257       for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
00258         int trackId = getTrackId (*(phits[iphit]));
00259         if (trackId >= 0) {
00260           int genId = generatorId (trackId);
00261           if (genId >= 0) {
00262             const reco::GenParticle* genPart = getGenParticle (genId);
00263             if (genPart) {
00264               result.insert (genPart);
00265             }
00266             else if (fVerbose) {
00267               std::cerr << "JetMatchingTools::getGenParticles-> Can not convert genId " << genId << " to GenParticle" << std::endl;
00268             }
00269           }
00270           else if (fVerbose) {
00271             std::cerr << "JetMatchingTools::getGenParticles-> Can not convert trackId " << trackId << " to genId" << std::endl;
00272           }
00273         }
00274         else if (fVerbose) {
00275           std::cerr << "JetMatchingTools::getGenParticles-> Unknown trackId for PCaloHit " << *(phits[iphit]) << std::endl;
00276         }
00277       }
00278     }
00279   }
00280   return std::vector <const reco::GenParticle*> (result.begin (), result.end());
00281 }
00282 
00284 std::vector <const reco::GenParticle*> JetMatchingTools::getGenParticles (const reco::GenJet& fJet) {
00285   return fJet.getGenConstituents ();
00286 }
00287 
00289 double JetMatchingTools::lostEnergyFraction (const reco::CaloJet& fJet ) {
00290   double totalEnergy = 0;
00291   double lostEnergy = 0;
00292   // follow the chain
00293   std::vector <const CaloTower*> towers = getConstituents (fJet) ;
00294   for (unsigned itower = 0; itower < towers.size (); ++itower) {
00295     std::vector <const CaloRecHit*> recHits = getConstituents (*(towers[itower]));
00296     for (unsigned ihit = 0; ihit < recHits.size(); ++ihit) {
00297       double foundSimEnergy = 0;
00298       double lostSimEnergy = 0;
00299       std::vector <const PCaloHit*> phits = getPCaloHits (recHits[ihit]->detid());
00300       for (unsigned iphit = 0; iphit < phits.size(); ++iphit) {
00301         double simEnergy = phits[iphit]->energy ();
00302         int trackId = getTrackId (*(phits[iphit]));
00303         if (trackId < 0 || generatorId (trackId) < 0)   lostSimEnergy += simEnergy;
00304         else   foundSimEnergy += simEnergy;
00305       }
00306       if (foundSimEnergy > 0 || lostSimEnergy > 0) {
00307         totalEnergy += recHits[ihit]->energy ();
00308         lostEnergy += recHits[ihit]->energy () * lostSimEnergy / (foundSimEnergy + lostSimEnergy);
00309       }
00310     }
00311   }
00312   return lostEnergy / totalEnergy;
00313 }
00314 
00316 double JetMatchingTools::overlapEnergyFraction (const std::vector <const reco::GenParticle*>& fObject, 
00317                                                 const std::vector <const reco::GenParticle*>& fReference) const {
00318   if (fObject.empty()) return 0;
00319   double totalEnergy = 0;
00320   double overlapEnergy = 0;
00321   for (unsigned i = 0; i < fObject.size(); ++i) {
00322     totalEnergy += fObject [i]->energy();
00323     if (find (fReference.begin(), fReference.end(), fObject [i]) != fReference.end ()) overlapEnergy += fObject [i]->energy();
00324   }
00325   return overlapEnergy / totalEnergy;
00326 }