CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/PhysicsTools/PatAlgos/plugins/CandidateSummaryTable.cc

Go to the documentation of this file.
00001 //
00002 // $Id: CandidateSummaryTable.cc,v 1.4 2010/02/20 21:00:15 wmtan Exp $
00003 //
00004 
00016 #include "FWCore/Framework/interface/EDAnalyzer.h"
00017 #include "FWCore/Framework/interface/Event.h"
00018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00019 #include "FWCore/Utilities/interface/InputTag.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 #include <iomanip>
00022 
00023 #include "DataFormats/Common/interface/View.h"
00024 #include "DataFormats/Candidate/interface/Candidate.h"
00025 
00026 namespace pat {
00027   class CandidateSummaryTable : public edm::EDAnalyzer {
00028     public:
00029       explicit CandidateSummaryTable(const edm::ParameterSet & iConfig);
00030       ~CandidateSummaryTable();  
00031 
00032       virtual void analyze(const edm::Event & iEvent, const edm::EventSetup & iSetup);
00033       virtual void endJob();
00034 
00035     private:
00036       struct Record {
00037         edm::InputTag src;
00038         size_t present, empty, min, max, total;
00039         Record(edm::InputTag tag) : src(tag), present(0), empty(0), min(0), max(0), total(0) {}
00040 
00041         void update(const edm::View<reco::Candidate> &items) {
00042             present++; 
00043             size_t size = items.size();
00044             if (size == 0) {
00045                 empty++;
00046             } else  { 
00047                 if (min > size) min = size;
00048                 if (max < size) max = size;
00049             }
00050             total += size;
00051         }
00052       };
00053       std::vector<Record> collections_;
00054       size_t totalEvents_;
00055       bool perEvent_, perJob_;
00056       std::string self_, logName_;
00057       bool dumpItems_;
00058   };
00059 
00060 } // namespace
00061 
00062 pat::CandidateSummaryTable::CandidateSummaryTable(const edm::ParameterSet & iConfig) :
00063     totalEvents_(0),
00064     perEvent_(iConfig.getUntrackedParameter<bool>("perEvent", false)),
00065     perJob_(iConfig.getUntrackedParameter<bool>("perJob", true)),
00066     self_(iConfig.getParameter<std::string>("@module_label")),
00067     logName_(iConfig.getUntrackedParameter<std::string>("logName")),
00068     dumpItems_(iConfig.getUntrackedParameter<bool>("dumpItems", false))
00069 {
00070     std::vector<edm::InputTag> inputs = iConfig.getParameter<std::vector<edm::InputTag> >("candidates");
00071     for (std::vector<edm::InputTag>::const_iterator it = inputs.begin(); it != inputs.end(); ++it) {
00072         collections_.push_back(Record(*it));
00073     }
00074 }
00075 
00076 pat::CandidateSummaryTable::~CandidateSummaryTable() {
00077 }
00078 
00079 void 
00080 pat::CandidateSummaryTable::analyze(const edm::Event & iEvent, const edm::EventSetup & iSetup) {
00081   using namespace edm;
00082   using std::setw; using std::left; using std::right; using std::setprecision;
00083 
00084   Handle<View<reco::Candidate> > candidates;
00085   if (perEvent_) {
00086         LogInfo(logName_) << "Per Event Table " << logName_ <<
00087                              " (" << self_ << ", run:event " << iEvent.id().run() << ":" << iEvent.id().event() << ")";
00088   }
00089   totalEvents_++;
00090   for (std::vector<Record>::iterator it = collections_.begin(), ed = collections_.end(); it != ed; ++it) {
00091     iEvent.getByLabel(it->src, candidates);
00092     if (!candidates.failedToGet()) it->update(*candidates);
00093     if (perEvent_) {
00094         LogVerbatim(logName_) << "    " << setw(30) << left  << it->src.encode() << right;
00095         if (dumpItems_) {
00096             size_t i = 0;
00097             std::ostringstream oss;
00098             for (View<reco::Candidate>::const_iterator cand = candidates->begin(), endc = candidates->end(); cand != endc; ++cand, ++i) {
00099                 oss << "      [" << setw(3) << i << "]" << 
00100                         "  pt "  << setw(7) << setprecision(5) << cand->pt()  <<
00101                         "  eta " << setw(7) << setprecision(5) << cand->eta() <<
00102                         "  phi " << setw(7) << setprecision(5) << cand->phi() <<
00103                         "  et "  << setw(7) << setprecision(5) << cand->et()  <<
00104                         "  phi " << setw(7) << setprecision(5) << cand->phi() <<
00105                         "  charge " << setw(2) << cand->charge() <<
00106                         "  id "     << setw(7) << cand->pdgId() <<
00107                         "  st "     << setw(7) << cand->status() << "\n";
00108             }
00109             LogVerbatim(logName_) << oss.str();
00110         }
00111     }
00112   }
00113   if (perEvent_) LogInfo(logName_) << "" ;  // add an empty line
00114 }
00115 
00116 
00117 void 
00118 pat::CandidateSummaryTable::endJob() { 
00119     using std::setw; using std::left; using std::right; using std::setprecision;
00120     if (perJob_) {
00121         std::ostringstream oss;
00122         oss << "Summary Table " << logName_ << " (" << self_ << ", events total " << totalEvents_ << ")\n";
00123         for (std::vector<Record>::iterator it = collections_.begin(), ed = collections_.end(); it != ed; ++it) {
00124             oss << "    " << setw(30) << left  << it->src.encode() << right << 
00125                 "  present " << setw(7) << it->present << " (" << setw(4) << setprecision(3) << (it->present*100.0/totalEvents_) << "%)" << 
00126                 "  empty "   << setw(7) << it->empty   << " (" << setw(4) << setprecision(3) << (it->empty*100.0/totalEvents_)   << "%)" << 
00127                 "  min "     << setw(7) << it->min     <<  
00128                 "  max "     << setw(7) << it->max     <<  
00129                 "  total "   << setw(7) << it->total   <<
00130                 "  avg "     << setw(5) << setprecision(3) << (it->total/double(totalEvents_)) << "\n";
00131         } 
00132         oss << "\n";
00133         edm::LogVerbatim(logName_) << oss.str();
00134     }
00135 }
00136 
00137 #include "FWCore/Framework/interface/MakerMacros.h"
00138 using pat::CandidateSummaryTable;
00139 DEFINE_FWK_MODULE(CandidateSummaryTable);