CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/PhysicsTools/IsolationAlgos/interface/IsolationProducer.h

Go to the documentation of this file.
00001 #ifndef IsolationAlgos_IsolationProducer_h
00002 #define IsolationAlgos_IsolationProducer_h
00003 /* \class IsolationProducer<C1, C2, Algo>
00004  *
00005  * \author Francesco Fabozzi, INFN
00006  *
00007  * template class to store isolation
00008  *
00009  */
00010 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
00011 #include "FWCore/Framework/interface/EDProducer.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00013 #include "FWCore/Utilities/interface/InputTag.h"
00014 #include "DataFormats/Common/interface/AssociationVector.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include "FWCore/Framework/interface/Event.h"
00017 #include "DataFormats/Common/interface/Handle.h"
00018 #include <vector>
00019 
00020 namespace helper {
00021 
00022   template<typename Alg>
00023   struct NullIsolationAlgorithmSetup {
00024     static void init( Alg &, const edm::EventSetup& ) { }
00025   };
00026 
00027   template<typename Alg>
00028   struct IsolationAlgorithmSetup {
00029     typedef NullIsolationAlgorithmSetup<Alg> type;
00030   };
00031 }
00032 
00033 template <typename C1, typename C2, typename Alg, 
00034           typename OutputCollection = edm::AssociationVector<edm::RefProd<C1>, 
00035                                                              std::vector<typename Alg::value_type> >,
00036           typename Setup = typename helper::IsolationAlgorithmSetup<Alg>::type>
00037 class IsolationProducer : public edm::EDProducer {
00038 public:
00039   IsolationProducer( const edm::ParameterSet & );
00040   ~IsolationProducer();
00041 
00042 private:
00043   void produce( edm::Event&, const edm::EventSetup& );
00044   edm::InputTag src_, elements_;
00045   Alg alg_;
00046 };
00047 
00048 template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
00049 IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::IsolationProducer( const edm::ParameterSet & cfg ) :
00050   src_( cfg.template getParameter<edm::InputTag>( "src" ) ),
00051   elements_( cfg.template getParameter<edm::InputTag>( "elements" ) ),
00052   alg_( reco::modules::make<Alg>( cfg ) ) {
00053   produces<OutputCollection>();
00054 }
00055 
00056 template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
00057 IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::~IsolationProducer() {
00058 }
00059 
00060 template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
00061 void IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::produce( edm::Event& evt, const edm::EventSetup& es ) {
00062   using namespace edm;
00063   using namespace std;
00064   Handle<C1> src;
00065   Handle<C2> elements;
00066   evt.getByLabel( src_, src );
00067   evt.getByLabel( elements_, elements );
00068 
00069   Setup::init( alg_, es );
00070 
00071   typename OutputCollection::refprod_type ref( src );
00072   auto_ptr<OutputCollection> isolations( new OutputCollection( ref )  );
00073 
00074   size_t i = 0;
00075   for( typename C1::const_iterator lep = src->begin(); lep != src->end(); ++ lep ) {
00076     typename Alg::value_type iso= alg_(*lep,*elements); 
00077     isolations->setValue( i++, iso );
00078   }
00079   evt.put( isolations );
00080 }
00081 
00082 #endif