CMS 3D CMS Logo

TruthTOA.cc

Go to the documentation of this file.
00001 /*
00002  *  TruthTOA.C
00003  *
00004  *  Created by Victor Eduardo Bazterra on 5/31/07.
00005  *  Copyright 2007 __MyCompanyName__. All rights reserved.
00006  *
00007  */
00008 
00009 #include "TFile.h"
00010 #include "TH1D.h"
00011 
00012 // system include files
00013 #include <iostream>
00014 #include <map>
00015 #include <memory>
00016 #include <string>
00017 #include <vector>
00018 
00019 // user include files
00020 
00021 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00022 #include "DataFormats/VertexReco/interface/Vertex.h"
00023 
00024 #include "FWCore/Framework/interface/Frameworkfwd.h"
00025 #include "FWCore/Framework/interface/EDAnalyzer.h"
00026 #include "FWCore/Framework/interface/Event.h"
00027 #include "FWCore/Framework/interface/ESHandle.h"
00028 #include "FWCore/Framework/interface/EventSetup.h"
00029 #include "FWCore/Framework/interface/MakerMacros.h"
00030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00031 
00032 #include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
00033 #include "SimTracker/TrackHistory/interface/TrackHistory.h"
00034 
00035 //
00036 // class decleration
00037 //
00038 
00039 class TruthTOA : public edm::EDAnalyzer
00040 {
00041 
00042 public:
00043 
00044     explicit TruthTOA(const edm::ParameterSet&);
00045     ~TruthTOA();
00046 
00047 private:
00048 
00049     virtual void beginJob(const edm::EventSetup&) ;
00050     virtual void analyze(const edm::Event&, const edm::EventSetup&);
00051     virtual void endJob();
00052 
00053     // Member data
00054 
00055     int matchedHits_;
00056 
00057     typedef std::set<int> sint;
00058     typedef std::vector<double> vdouble;
00059     typedef std::vector<std::string> vstring;
00060     typedef std::vector<vstring> vvstring;
00061     typedef std::vector<edm::ParameterSet> vParameterSet;
00062 
00063     edm::InputTag trackingTruth_;
00064 
00065     std::string rootFile_;
00066     bool status_, antiparticles_;
00067 
00068     TrackHistory tracer_;
00069 
00070     vvstring vetoList_;
00071 
00072     // Track origin
00073 
00074     struct counter_info_t
00075     {
00076         int pdgId;
00077         int tracks;
00078 
00079         counter_info_t(int pdgId_, int tracks_)
00080         {
00081             pdgId = pdgId_;
00082             tracks = tracks_;
00083         }
00084 
00085         bool operator< (const counter_info_t & i) const
00086         {
00087             if (pdgId < i.pdgId)
00088                 return true;
00089             else if (pdgId > i.pdgId)
00090                 return false;
00091 
00092             if (tracks < i.tracks)
00093                 return true;
00094 
00095             return false;
00096         }
00097     };
00098 
00099     typedef std::map<int,int>  counter_index_t;
00100     typedef std::pair<int,int> counter_index_pair_t;
00101 
00102     typedef std::map<int,std::size_t>  counter_buffer_t;
00103     typedef std::pair<int,std::size_t> counter_buffer_pair_t;
00104 
00105     typedef std::map<counter_info_t,std::size_t>  counter_t;
00106     typedef std::pair<counter_info_t,std::size_t> counter_pair_t;
00107 
00108     counter_t counter_;
00109     counter_index_t counter_index_;
00110     counter_buffer_t counter_buffer_;
00111 
00112     edm::ESHandle<ParticleDataTable> pdt_;
00113 
00114     void InitCounter();
00115     void UpdateCounter();
00116     void Count(int barcode=0,int pdgId=0);
00117 
00118 };
00119 
00120 
00121 TruthTOA::TruthTOA(const edm::ParameterSet& config) : tracer_(config)
00122 {
00123     trackingTruth_ = config.getUntrackedParameter<edm::InputTag> ( "trackingTruth" );
00124 
00125     matchedHits_ = config.getUntrackedParameter<int> ( "matchedHits" );
00126 
00127     rootFile_ = config.getUntrackedParameter<std::string> ( "rootFile" );
00128 
00129     antiparticles_ = config.getUntrackedParameter<bool> ( "antiparticles" );
00130 
00131     status_ = config.getUntrackedParameter<bool> ( "status2" );
00132 
00133     edm::ParameterSet pset = config.getUntrackedParameter<edm::ParameterSet> ( "veto" );
00134 
00135     vstring vetoListNames = pset.getParameterNames();
00136 
00137     vetoList_.reserve(vetoListNames.size());
00138 
00139     for (std::size_t i=0; i < vetoListNames.size(); i++)
00140         vetoList_.push_back(pset.getUntrackedParameter<vstring> (vetoListNames[i]));
00141 }
00142 
00143 
00144 TruthTOA::~TruthTOA() { }
00145 
00146 
00147 void
00148 TruthTOA::analyze(const edm::Event& event, const edm::EventSetup& setup)
00149 {
00150     // Tracking particle information
00151     edm::Handle<TrackingParticleCollection>  TPCollection;
00152     event.getByLabel(trackingTruth_, TPCollection);
00153 
00154     // Set the history depth
00155     if (status_)
00156         tracer_.depth(-2);
00157     else
00158         tracer_.depth(-1);
00159 
00160     // Initialize and reset the temporal counters
00161     InitCounter();
00162 
00163     // Loop over the track collection.
00164     for (std::size_t index = 0; index < TPCollection->size(); index++)
00165     {
00166         // Get a pointer to a track per each track in collection
00167         TrackingParticleRef track(TPCollection, index);
00168 
00169         // If the track is not fake then get the orginal particles
00170         if ((*track).matchedHit() >= matchedHits_)
00171         {
00172             if ( tracer_.evaluate(track) )
00173             {
00174                 const HepMC::GenParticle * particle = tracer_.genParticle();
00175                 // If the origin can be determined then take the first particle as the original
00176                 if (particle)
00177                     Count(particle->barcode(), particle->pdg_id());
00178             }
00179             else
00180                 Count(0,0);
00181         }
00182     }
00183     UpdateCounter();
00184 }
00185 
00186 
00187 void
00188 TruthTOA::beginJob(const edm::EventSetup& setup)
00189 {
00190     // Get the particles table.
00191     setup.getData( pdt_ );
00192 }
00193 
00194 
00195 void
00196 TruthTOA::endJob()
00197 {
00198     TFile file(rootFile_.c_str(), "RECREATE");
00199     file.cd();
00200 
00201     double vetoedVals;
00202     double totalVals;
00203 
00204     std::cout << "List of all long lived particle found" << std::endl;
00205     for ( counter_t::iterator it = counter_.begin(); it != counter_.end(); it++)
00206     {
00207         if ( !it->first.pdgId )
00208             std::cout << " fake tracks -> " << it->second << std::endl;
00209         else
00210             std::cout << " particle " <<pdt_->particle(HepPDT::ParticleID(it->first.pdgId))->name();
00211         std::cout << " associated to " << it->first.tracks << " tracks -> " << it->second << std::endl;
00212     }
00213 
00214     std::multimap<double, std::string> particleTypes;
00215 
00216     for (std::size_t cid = 0; cid < vetoList_.size(); cid++)
00217     {
00218         particleTypes.clear();
00219         vetoedVals = 0;
00220         totalVals = 0;
00221 
00222         for (counter_t::iterator it = counter_.begin(); it != counter_.end(); it++)
00223         {
00224             std::ostringstream particle;
00225 
00226             if ( !it->first.pdgId )
00227                 particle << "Fake" << std::endl;
00228             else
00229                 particle << pdt_->particle(HepPDT::ParticleID(it->first.pdgId))->name();
00230 
00231             if (
00232                 std::find (
00233                     vetoList_[cid].begin(),
00234                     vetoList_[cid].end(),
00235                     particle.str()
00236                 ) == vetoList_[cid].end()
00237             )
00238             {
00239                 particle << "#" << it->first.tracks;
00240                 particleTypes.insert(std::pair<double,std::string>(it->second, particle.str()));
00241                 totalVals += it->second;
00242             }
00243             else
00244             {
00245                 vetoedVals += it->second;
00246                 totalVals += it->second;
00247             }
00248         }
00249 
00250         std::cout << "Veto list #" << cid << std::endl;
00251         std::cout << " number of vetoed tracks " << vetoedVals << std::endl;
00252         std::cout << " total number of tracks " << totalVals << std::endl;
00253         std::cout << " % of vetoed tracks " << ((vetoedVals/totalVals) * 100) << std::endl;
00254 
00255         std::ostringstream hName, hTitle;
00256 
00257         hTitle << "Track origins for the whole track collection";
00258         hName  << "TrackingParticleCollection";
00259 
00260         for (std::size_t v=0; v < vetoList_[cid].size(); v++)
00261         {
00262             hTitle << "_" << vetoList_[cid][v];
00263             hName << "_" << vetoList_[cid][v];
00264         }
00265 
00266         // creating and saving the pie
00267         TH1D histogram(
00268             hName.str().c_str(),
00269             hTitle.str().c_str(),
00270             particleTypes.size(),
00271             0.,
00272             Double_t(particleTypes.size())
00273         );
00274 
00275         // creating and saving the histogram
00276         int i = 1;
00277         std::cout << "Particle size " <<  particleTypes.size() << std::endl;
00278 
00279         std::map<double, std::string>::const_iterator it;
00280 
00281         for (it = particleTypes.begin(); it != particleTypes.end(); it++, i++)
00282         {
00283             histogram.GetXaxis()->SetBinLabel(i, it->second.c_str());
00284             histogram.SetBinContent(i, Double_t(it->first));
00285         }
00286 
00287         histogram.Write();
00288         file.Flush();
00289     }
00290 }
00291 
00292 void TruthTOA::InitCounter()
00293 {
00294     counter_buffer_.clear();
00295     counter_index_.clear();
00296 }
00297 
00298 void TruthTOA::Count(int barcode, int pdgId)
00299 {
00300     counter_buffer_t::iterator it = counter_buffer_.find(barcode);
00301 
00302     if ( it != counter_buffer_.end() )
00303     {
00304         it->second++;
00305     }
00306     else
00307     {
00308         counter_buffer_.insert( counter_buffer_pair_t(barcode, 1) );
00309         counter_index_.insert( counter_index_pair_t(barcode, pdgId) );
00310     }
00311 }
00312 
00313 
00314 void TruthTOA::UpdateCounter()
00315 {
00316     counter_buffer_t::const_iterator csi = counter_buffer_.begin();
00317 
00318     std::size_t particleType;
00319 
00320     for (; csi != counter_buffer_.end(); csi++)
00321     {
00322         if (antiparticles_)
00323             particleType = counter_index_[csi->first];
00324         else
00325             particleType = abs(counter_index_[csi->first]);
00326         if ( !particleType )
00327         {
00328             counter_info_t info (particleType, 1);
00329             counter_t::iterator ci = counter_.find( info );
00330             if ( ci != counter_.end() )
00331                 ci->second += csi->second;
00332             else
00333                 counter_.insert( counter_pair_t (info, csi->second) );
00334         }
00335         else
00336         {
00337             counter_info_t info = counter_info_t (particleType, csi->second);
00338             counter_t::iterator ci = counter_.find( info );
00339             if ( ci != counter_.end() )
00340                 ci->second++;
00341             else
00342                 counter_.insert( counter_pair_t (info, 1) );
00343         }
00344     }
00345 }
00346 
00347 //define this as a plug-in
00348 DEFINE_ANOTHER_FWK_MODULE(TruthTOA);
00349 

Generated on Tue Jun 9 17:48:02 2009 for CMSSW by  doxygen 1.5.4