CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/CommonTools/UtilAlgos/interface/ObjectCounter.h

Go to the documentation of this file.
00001 #ifndef UtilAlgos_ObjectCounter_h
00002 #define UtilAlgos_ObjectCounter_h
00003 
00018 #include "FWCore/Framework/interface/EDAnalyzer.h"
00019 #include "FWCore/Framework/interface/Event.h"
00020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00021 #include "FWCore/Framework/interface/TypeID.h"
00022 #include <iostream>
00023 #include <cmath>
00024 
00025 template<typename C>
00026 class ObjectCounter : public edm::EDAnalyzer {
00027 public:
00029   explicit ObjectCounter( const edm::ParameterSet& );
00031   void endJob();
00032 
00033 private:
00035   virtual void analyze( const edm::Event&, const edm::EventSetup& );
00037   std::string src_;
00039   bool verbose_;
00041   unsigned long n_, nSum_, n2Sum_;
00042 };
00043 
00044 template<typename C>
00045 ObjectCounter<C>::ObjectCounter( const edm::ParameterSet& par ) : 
00046   src_( par.template getParameter<std::string>( "src" ) ), 
00047   verbose_( par.template getUntrackedParameter<bool>( "verbose", true ) ),
00048   n_( 0 ), nSum_( 0 ), n2Sum_( 0 ) {
00049 }
00050 
00051 template<typename C>
00052 void ObjectCounter<C>::endJob() {
00053   double n = 0, n2 = 0, s;
00054   if ( n_!= 0 ) {
00055     n = double( nSum_ ) / n_;
00056     n2 = double ( n2Sum_ ) / n_;
00057   }
00058   s = sqrt( n2 - n * n );
00059   if ( verbose_ ) {
00060     edm::TypeID id( typeid( typename C::value_type ) );
00061     std::cout << ">>> collection \"" << src_ << "\" contains (" 
00062               << n << " +/- " << s << ") "  
00063               << id.friendlyClassName() << " objects" << std::endl;
00064   }
00065 }
00066 
00067 template<typename C>
00068 void ObjectCounter<C>::analyze( const edm::Event& evt, const edm::EventSetup& ) {
00069   edm::Handle<C> h;
00070   evt.getByLabel( src_, h );
00071   if (!h.isValid()) {
00072     std::cerr << ">>> product: " << src_ << " not found" << std::endl;
00073   } else {
00074     int n = h->size();
00075     nSum_ += n;
00076     n2Sum_ += ( n * n );
00077   }
00078   ++ n_;
00079 }
00080 
00081 #endif