CMS 3D CMS Logo

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