CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/SimTracker/TrackHistory/plugins/VertexHistoryAnalyzer.cc

Go to the documentation of this file.
00001 /*
00002  *  VertexHistoryAnalyzer.C
00003  *
00004  *  Created by Victor Eduardo Bazterra on 5/31/07.
00005  *
00006  */
00007 
00008 // system include files
00009 #include <iostream>
00010 #include <memory>
00011 #include <string>
00012 #include <sstream>
00013 #include <vector>
00014 
00015 // user include files
00016 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00017 #include "DataFormats/VertexReco/interface/Vertex.h"
00018 
00019 #include "FWCore/Framework/interface/Frameworkfwd.h"
00020 #include "FWCore/Framework/interface/EDAnalyzer.h"
00021 #include "FWCore/Framework/interface/Event.h"
00022 #include "FWCore/Framework/interface/ESHandle.h"
00023 #include "FWCore/Framework/interface/EventSetup.h"
00024 #include "FWCore/Framework/interface/MakerMacros.h"
00025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00026 
00027 #include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
00028 #include "SimTracker/TrackHistory/interface/VertexClassifier.h"
00029 
00030 //
00031 // class decleration
00032 //
00033 
00034 class VertexHistoryAnalyzer : public edm::EDAnalyzer
00035 {
00036 public:
00037 
00038     explicit VertexHistoryAnalyzer(const edm::ParameterSet&);
00039 
00040 private:
00041 
00042   virtual void beginRun(const edm::Run&,const edm::EventSetup&);
00043     virtual void beginJob() ;
00044     virtual void analyze(const edm::Event&, const edm::EventSetup&);
00045 
00046     // Member data
00047 
00048     VertexClassifier classifier_;
00049 
00050     edm::InputTag vertexProducer_;
00051 
00052     edm::ESHandle<ParticleDataTable> pdt_;
00053 
00054     std::string particleString(int) const;
00055 
00056     std::string vertexString(
00057         TrackingParticleRefVector,
00058         TrackingParticleRefVector
00059     ) const;
00060 
00061     std::string vertexString(
00062         HepMC::GenVertex::particles_in_const_iterator,
00063         HepMC::GenVertex::particles_in_const_iterator,
00064         HepMC::GenVertex::particles_out_const_iterator,
00065         HepMC::GenVertex::particles_out_const_iterator
00066     ) const;
00067 };
00068 
00069 
00070 VertexHistoryAnalyzer::VertexHistoryAnalyzer(const edm::ParameterSet& config) : classifier_(config)
00071 {
00072     vertexProducer_ = config.getUntrackedParameter<edm::InputTag> ("vertexProducer");
00073 }
00074 
00075 
00076 void VertexHistoryAnalyzer::analyze(const edm::Event& event, const edm::EventSetup& setup)
00077 {
00078     // Set the classifier for a new event
00079     classifier_.newEvent(event, setup);
00080 
00081     // Vertex collection
00082     edm::Handle<edm::View<reco::Vertex> > vertexCollection;
00083     event.getByLabel(vertexProducer_, vertexCollection);
00084 
00085     // Get a constant reference to the track history associated to the classifier
00086     VertexHistory const & tracer = classifier_.history();
00087 
00088     // Loop over the track collection.
00089     for (std::size_t index = 0; index < vertexCollection->size(); index++)
00090     {
00091         std::cout << std::endl << "History for vertex #" << index << " : " << std::endl;
00092 
00093         // Classify the track and detect for fakes
00094         if ( !classifier_.evaluate( reco::VertexBaseRef(vertexCollection, index) ).is(VertexClassifier::Fake) )
00095         {
00096             // Get the list of TrackingParticles associated to
00097             VertexHistory::SimParticleTrail simParticles(tracer.simParticleTrail());
00098 
00099             // Loop over all simParticles
00100             for (std::size_t hindex=0; hindex<simParticles.size(); hindex++)
00101             {
00102                 std::cout << "  simParticles [" << hindex << "] : "
00103                           << particleString(simParticles[hindex]->pdgId())
00104                           << std::endl;
00105             }
00106 
00107             // Get the list of TrackingVertexes associated to
00108             VertexHistory::SimVertexTrail simVertexes(tracer.simVertexTrail());
00109 
00110             // Loop over all simVertexes
00111             if ( !simVertexes.empty() )
00112             {
00113                 for (std::size_t hindex=0; hindex<simVertexes.size(); hindex++)
00114                 {
00115                     std::cout << "  simVertex    [" << hindex << "] : "
00116                               << vertexString(
00117                                   simVertexes[hindex]->sourceTracks(),
00118                                   simVertexes[hindex]->daughterTracks()
00119                               )
00120                               << std::endl;
00121                 }
00122             }
00123             else
00124                 std::cout << "  simVertex no found" << std::endl;
00125 
00126             // Get the list of GenParticles associated to
00127             VertexHistory::GenParticleTrail genParticles(tracer.genParticleTrail());
00128 
00129             // Loop over all genParticles
00130             for (std::size_t hindex=0; hindex<genParticles.size(); hindex++)
00131             {
00132                 std::cout << "  genParticles [" << hindex << "] : "
00133                           << particleString(genParticles[hindex]->pdg_id())
00134                           << std::endl;
00135             }
00136 
00137             // Get the list of TrackingVertexes associated to
00138             VertexHistory::GenVertexTrail genVertexes(tracer.genVertexTrail());
00139 
00140             // Loop over all simVertexes
00141             if ( !genVertexes.empty() )
00142             {
00143                 for (std::size_t hindex=0; hindex<genVertexes.size(); hindex++)
00144                 {
00145                     std::cout << "  genVertex    [" << hindex << "] : "
00146                               << vertexString(
00147                                   genVertexes[hindex]->particles_in_const_begin(),
00148                                   genVertexes[hindex]->particles_in_const_end(),
00149                                   genVertexes[hindex]->particles_out_const_begin(),
00150                                   genVertexes[hindex]->particles_out_const_end()
00151                               )
00152                               << std::endl;
00153                 }
00154             }
00155             else
00156                 std::cout << "  genVertex no found" << std::endl;
00157         }
00158         else
00159             std::cout << "  fake vertex" << std::endl;
00160 
00161         std::cout << "  vertex categories : " << classifier_;
00162         std::cout << std::endl;
00163     }
00164 }
00165 
00166 
00167 void
00168 VertexHistoryAnalyzer::beginRun(const edm::Run& run, const edm::EventSetup& setup)
00169 {
00170     // Get the particles table.
00171     setup.getData( pdt_ );
00172 }
00173 
00174 void
00175 VertexHistoryAnalyzer::beginJob()
00176 {
00177 
00178 }
00179 
00180 
00181 std::string VertexHistoryAnalyzer::particleString(int pdgId) const
00182 {
00183     ParticleData const * pid;
00184 
00185     std::ostringstream vDescription;
00186 
00187     HepPDT::ParticleID particleType(pdgId);
00188 
00189     if (particleType.isValid())
00190     {
00191         pid = pdt_->particle(particleType);
00192         if (pid)
00193             vDescription << pid->name();
00194         else
00195             vDescription << pdgId;
00196     }
00197     else
00198         vDescription << pdgId;
00199 
00200     return vDescription.str();
00201 }
00202 
00203 
00204 std::string VertexHistoryAnalyzer::vertexString(
00205     TrackingParticleRefVector in,
00206     TrackingParticleRefVector out
00207 ) const
00208 {
00209     ParticleData const * pid;
00210 
00211     std::ostringstream vDescription;
00212 
00213     for (std::size_t j = 0; j < in.size(); j++)
00214     {
00215         if (!j) vDescription << "(";
00216 
00217         HepPDT::ParticleID particleType(in[j]->pdgId());
00218 
00219         if (particleType.isValid())
00220         {
00221             pid = pdt_->particle(particleType);
00222             if (pid)
00223                 vDescription << pid->name();
00224             else
00225                 vDescription << in[j]->pdgId();
00226         }
00227         else
00228             vDescription << in[j]->pdgId();
00229 
00230         if (j == in.size() - 1) vDescription << ")";
00231         else vDescription << ",";
00232     }
00233 
00234     vDescription << "->";
00235 
00236     for (std::size_t j = 0; j < out.size(); j++)
00237     {
00238         if (!j) vDescription << "(";
00239 
00240         HepPDT::ParticleID particleType(out[j]->pdgId());
00241 
00242         if (particleType.isValid())
00243         {
00244             pid = pdt_->particle(particleType);
00245             if (pid)
00246                 vDescription << pid->name();
00247             else
00248                 vDescription << out[j]->pdgId();
00249         }
00250         else
00251             vDescription << out[j]->pdgId();
00252 
00253         if (j == out.size() - 1) vDescription << ")";
00254         else vDescription << ",";
00255     }
00256 
00257     return vDescription.str();
00258 }
00259 
00260 
00261 std::string VertexHistoryAnalyzer::vertexString(
00262     HepMC::GenVertex::particles_in_const_iterator in_begin,
00263     HepMC::GenVertex::particles_in_const_iterator in_end,
00264     HepMC::GenVertex::particles_out_const_iterator out_begin,
00265     HepMC::GenVertex::particles_out_const_iterator out_end
00266 ) const
00267 {
00268     ParticleData const * pid;
00269 
00270     std::ostringstream vDescription;
00271 
00272     std::size_t j = 0;
00273 
00274     HepMC::GenVertex::particles_in_const_iterator in, itmp;
00275 
00276     for (in = in_begin; in != in_end; in++, j++)
00277     {
00278         if (!j) vDescription << "(";
00279 
00280         HepPDT::ParticleID particleType((*in)->pdg_id());
00281 
00282         if (particleType.isValid())
00283         {
00284             pid = pdt_->particle(particleType);
00285             if (pid)
00286                 vDescription << pid->name();
00287             else
00288                 vDescription << (*in)->pdg_id();
00289         }
00290         else
00291             vDescription << (*in)->pdg_id();
00292 
00293         itmp = in;
00294 
00295         if (++itmp == in_end) vDescription << ")";
00296         else vDescription << ",";
00297     }
00298 
00299     vDescription << "->";
00300     j = 0;
00301 
00302     HepMC::GenVertex::particles_out_const_iterator out, otmp;
00303 
00304     for (out = out_begin; out != out_end; out++, j++)
00305     {
00306         if (!j) vDescription << "(";
00307 
00308         HepPDT::ParticleID particleType((*out)->pdg_id());
00309 
00310         if (particleType.isValid())
00311         {
00312             pid = pdt_->particle(particleType);
00313             if (pid)
00314                 vDescription << pid->name();
00315             else
00316                 vDescription << (*out)->pdg_id();
00317         }
00318         else
00319             vDescription << (*out)->pdg_id();
00320 
00321         otmp = out;
00322 
00323         if (++otmp == out_end) vDescription << ")";
00324         else vDescription << ",";
00325     }
00326 
00327     return vDescription.str();
00328 }
00329 
00330 
00331 DEFINE_FWK_MODULE(VertexHistoryAnalyzer);