CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/QCDAnalysis/ChargedHadronSpectra/src/PlotSimTracks.cc

Go to the documentation of this file.
00001 #include "QCDAnalysis/ChargedHadronSpectra/interface/PlotSimTracks.h"
00002 #include "QCDAnalysis/ChargedHadronSpectra/interface/PlotUtils.h"
00003 #include "QCDAnalysis/ChargedHadronSpectra/interface/HitInfo.h"
00004 
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 #include "FWCore/Framework/interface/Event.h"
00008 
00009 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00010 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00011 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00012 
00013 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00014 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00015 
00016 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
00017 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00018 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00019 
00020 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00021 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00022 
00023 // Ecal
00024 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00025 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00026 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00027 
00028 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00029 
00030 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00031 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00032 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00033 #include "DataFormats/EcalDetId/interface/ESDetId.h"
00034 
00035 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00036 
00037 using namespace std;
00038 
00039 /*****************************************************************************/
00040 struct sortByPabs
00041 {
00042   bool operator() (const PSimHit& a, const PSimHit& b) const
00043   {
00044     return (a.pabs() > b.pabs());
00045   }
00046 };
00047 
00048 /*****************************************************************************/
00049 struct sortByTof
00050 {
00051   bool operator() (const PSimHit& a, const PSimHit& b) const
00052   {
00053     return (a.tof() < b.tof());
00054   }
00055 };
00056 
00057 /*****************************************************************************/
00058 PlotSimTracks::PlotSimTracks
00059   (const edm::EventSetup& es, ofstream& file_) : file(file_)
00060 {
00061   // Get tracker geometry
00062   edm::ESHandle<TrackerGeometry> trackerHandle;
00063   es.get<TrackerDigiGeometryRecord>().get(trackerHandle);
00064   theTracker = trackerHandle.product();
00065  
00066   // Get calorimetry
00067   edm::ESHandle<CaloGeometry> calo;
00068   es.get<CaloGeometryRecord>().get(calo);
00069   theCaloGeometry = (const CaloGeometry*)calo.product();
00070 }
00071 
00072 /*****************************************************************************/
00073 PlotSimTracks::~PlotSimTracks()
00074 {
00075 }
00076 
00077 /*****************************************************************************/
00078 void PlotSimTracks::printSimTracks(const edm::Event& ev)
00079 {
00080   // Tracker
00081   edm::Handle<TrackingParticleCollection> simTrackHandle;
00082   ev.getByLabel("mergedtruth",            simTrackHandle);
00083   const TrackingParticleCollection* simTracks = simTrackHandle.product();
00084 
00085   // Ecal
00086   edm::Handle<edm::PCaloHitContainer>      simHitsBarrel;
00087   ev.getByLabel("g4SimHits", "EcalHitsEB", simHitsBarrel);
00088    
00089 //  edm::Handle<edm::PCaloHitContainer>      simHitsPreshower;
00090 //  ev.getByLabel("g4SimHits", "EcalHitsES", simHitsPreshower);
00091 
00092   edm::Handle<edm::PCaloHitContainer>      simHitsEndcap;
00093   ev.getByLabel("g4SimHits", "EcalHitsEE", simHitsEndcap);
00094 
00095 // FIXME
00096 /*
00097   {
00098   edm::Handle<edm::SimTrackContainer>  simTracks;
00099   ev.getByType<edm::SimTrackContainer>(simTracks);
00100   std::cerr << " SSSSS " << simTracks.product()->size() << std::endl;
00101 
00102   for(edm::SimTrackContainer::const_iterator t = simTracks.product()->begin();
00103                                              t!= simTracks.product()->end(); t++)
00104   {
00105     std::cerr << " simTrack " << t - simTracks.product()->begin()
00106          << " " << t->type()
00107          << " " << t->charge()
00108          << " " << t->vertIndex()
00109          << " " << t->genpartIndex()
00110          << " " << t->momentum().x()
00111          << " " << t->momentum().y()
00112          << " " << t->momentum().z()
00113          << std::endl;
00114   } 
00115 
00116   }
00117 */
00118 
00119   const CaloSubdetectorGeometry* geom;
00120 
00121   // Utilities
00122   PlotUtils plotUtils;
00123 
00124   file << ", If[st, {RGBColor[0.5,0.5,0.5]";
00125 
00126   for(TrackingParticleCollection::const_iterator simTrack = simTracks->begin();
00127                                                  simTrack!= simTracks->end();
00128                                                  simTrack++)
00129   {
00130     std::vector<PSimHit> simHits;
00131 
00132     simHits = simTrack->trackPSimHit();
00133 
00134     // reorder with help of momentum
00135     sort(simHits.begin(), simHits.end(), sortByPabs());
00136 
00137     for(std::vector<PSimHit>::const_iterator simHit = simHits.begin();
00138                                         simHit!= simHits.end(); simHit++)
00139     {
00140       DetId id = DetId(simHit->detUnitId());
00141 
00142       if(theTracker->idToDetUnit(id) != 0)
00143       {  
00144       GlobalPoint  p1 =
00145         theTracker->idToDetUnit(id)->toGlobal(simHit->localPosition()); 
00146       GlobalVector v1 =
00147         theTracker->idToDetUnit(id)->toGlobal(simHit->localDirection());
00148 
00149       // simHit
00150       file << ", Point[{" << p1.x() << "," << p1.y() << ",(" << p1.z() << "-zs)*mz}]"
00151            << std::endl;
00152       file << ", Text[StyleForm[\"s\", URL->\"SimHit | Ekin="
00153            << simTrack->energy() - simTrack->mass()
00154            << " GeV | parent: source="
00155            << simTrack->parentVertex()->nSourceTracks() 
00156            << " daughter=" << simTrack->parentVertex()->nDaughterTracks()
00157            << HitInfo::getInfo(*simHit) << "\"], {"
00158            << p1.x() << "," << p1.y() << ",(" << p1.z() << "-zs)*mz}, {1,1}]"
00159            << std::endl;
00160 
00161       // det
00162       double x = theTracker->idToDet(id)->surface().bounds().width() /2;
00163       double y = theTracker->idToDet(id)->surface().bounds().length()/2;
00164       double z = 0.;
00165   
00166       GlobalPoint p00 =  theTracker->idToDet(id)->toGlobal(LocalPoint(-x,-y,z));
00167       GlobalPoint p01 =  theTracker->idToDet(id)->toGlobal(LocalPoint(-x, y,z));
00168       GlobalPoint p10 =  theTracker->idToDet(id)->toGlobal(LocalPoint( x,-y,z));
00169       GlobalPoint p11 =  theTracker->idToDet(id)->toGlobal(LocalPoint( x, y,z));
00170 
00171       if(theTracker->idToDet(id)->subDetector() ==
00172            GeomDetEnumerators::PixelBarrel ||
00173          theTracker->idToDet(id)->subDetector() ==
00174            GeomDetEnumerators::PixelEndcap)
00175         file << ", If[sd, {RGBColor[0.6,0.6,0.6], ";
00176       else
00177         file << ", If[sd, {RGBColor[0.8,0.8,0.8], ";
00178 
00179       file       <<"Line[{{"<< p00.x()<<","<<p00.y()<<",("<<p00.z()<<"-zs)*mz}, "
00180                        <<"{"<< p01.x()<<","<<p01.y()<<",("<<p01.z()<<"-zs)*mz}, "
00181                        <<"{"<< p11.x()<<","<<p11.y()<<",("<<p11.z()<<"-zs)*mz}, "
00182                        <<"{"<< p10.x()<<","<<p10.y()<<",("<<p10.z()<<"-zs)*mz}, "
00183                        <<"{"<< p00.x()<<","<<p00.y()<<",("<<p00.z()<<"-zs)*mz}}]}]"
00184         << std::endl;
00185 
00186       if(simHit == simHits.begin()) // vertex to first point
00187       {
00188         GlobalPoint p0(simTrack->vertex().x(),
00189                        simTrack->vertex().y(),
00190                        simTrack->vertex().z());
00191         plotUtils.printHelix(p0,p1,v1, file, simTrack->charge());
00192       }
00193 
00194       if(simHit+1 != simHits.end()) // if not last
00195       {
00196         DetId id = DetId((simHit+1)->detUnitId());
00197         GlobalPoint  p2 =
00198           theTracker->idToDetUnit(id)->toGlobal((simHit+1)->localPosition());
00199         GlobalVector v2 =
00200           theTracker->idToDetUnit(id)->toGlobal((simHit+1)->localDirection());
00201 
00202         plotUtils.printHelix(p1,p2,v2, file, simTrack->charge());
00203       }
00204 
00205       // Continue to Ecal
00206       if(simHit+1 == simHits.end()) // if last
00207       {
00208         DetId id = DetId(simHit->detUnitId());
00209         GlobalPoint p =
00210           theTracker->idToDetUnit(id)->toGlobal(simHit->localPosition());
00211 
00212         // EB
00213         geom = theCaloGeometry->getSubdetectorGeometry(DetId::Ecal,EcalBarrel);
00214 
00215         for(edm::PCaloHitContainer::const_iterator
00216               simHit = simHitsBarrel->begin();
00217               simHit!= simHitsBarrel->end(); simHit++)
00218           if(simHit->geantTrackId() == static_cast<int>(simTrack->g4Track_begin()->trackId()) && //the sign of trackId tells whether there was a match  
00219            simHit->energy() > 0.060)
00220         {
00221           EBDetId detId(simHit->id());
00222           const CaloCellGeometry* cell = geom->getGeometry(detId);
00223 
00224           if(cell != 0)
00225           file << ", Line[{{" << p.x()
00226                        << "," << p.y()
00227                        << ",(" << p.z() <<"-zs)*mz}"
00228                << ", {" << cell->getPosition().x() << ","
00229                         << cell->getPosition().y() << ",("
00230                         << cell->getPosition().z() << "-zs)*mz}}]" << std::endl;
00231         }
00232 
00233         // ES
00234 
00235         // EE
00236         geom = theCaloGeometry->getSubdetectorGeometry(DetId::Ecal,EcalEndcap);
00237 
00238         for(edm::PCaloHitContainer::const_iterator
00239               simHit = simHitsEndcap->begin();
00240               simHit!= simHitsEndcap->end(); simHit++)
00241           if(simHit->geantTrackId() == static_cast<int>(simTrack->g4Track_begin()->trackId()) && //the sign of trackId tells whether there was a match
00242            simHit->energy() > 0.060)
00243         {
00244           EEDetId detId(simHit->id());
00245           const CaloCellGeometry* cell = geom->getGeometry(detId);
00246 
00247           if(cell != 0)
00248           file << ", Line[{{" << p.x()
00249                        << "," << p.y()
00250                        << ",(" << p.z() << "-zs)*mz}"
00251                << ", {" << cell->getPosition().x() << ","
00252                         << cell->getPosition().y() << ",("
00253                         << cell->getPosition().z() << "-zs)*mz}}]" << std::endl;
00254         }
00255       }
00256       }
00257     }
00258   }
00259 
00260   file << "}]";
00261 }
00262