CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/PhysicsTools/IsolationAlgos/interface/IsolationProducerNew.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/ValueMap.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include "FWCore/Framework/interface/Event.h"
00017 #include "DataFormats/Common/interface/Handle.h"
00018 #include "CommonTools/UtilAlgos/interface/MasterCollectionHelper.h"
00019 #include <vector>
00020 
00021 namespace helper {
00022 
00023   template<typename Alg>
00024   struct NullIsolationAlgorithmSetup {
00025     static void init( Alg &, const edm::EventSetup& ) { }
00026   };
00027 
00028   template<typename Alg>
00029   struct IsolationAlgorithmSetup {
00030     typedef NullIsolationAlgorithmSetup<Alg> type;
00031   };
00032 }
00033 
00034 namespace reco {
00035   namespace modulesNew {
00036 
00037     template <typename C1, typename C2, typename Alg, 
00038               typename OutputCollection = edm::ValueMap<float>,
00039               typename Setup = typename helper::IsolationAlgorithmSetup<Alg>::type>
00040     class IsolationProducer : public edm::EDProducer {
00041     public:
00042       IsolationProducer( const edm::ParameterSet & );
00043       ~IsolationProducer();
00044       
00045     private:
00046       void produce( edm::Event&, const edm::EventSetup& );
00047       edm::InputTag src_, elements_;
00048       Alg alg_;
00049     };
00050     
00051     template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
00052     IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::IsolationProducer( const edm::ParameterSet & cfg ) :
00053       src_( cfg.template getParameter<edm::InputTag>( "src" ) ),
00054       elements_( cfg.template getParameter<edm::InputTag>( "elements" ) ),
00055       alg_( reco::modules::make<Alg>( cfg ) ) {
00056       produces<OutputCollection>();
00057     }
00058     
00059     template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
00060     IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::~IsolationProducer() {
00061     }
00062     
00063     template <typename C1, typename C2, typename Alg, typename OutputCollection, typename Setup>
00064     void IsolationProducer<C1, C2, Alg, OutputCollection, Setup>::produce( edm::Event& evt, const edm::EventSetup& es ) {
00065       using namespace edm;
00066       using namespace std;
00067       Handle<C1> src;
00068       Handle<C2> elements;
00069       evt.getByLabel(src_, src);
00070       evt.getByLabel(elements_, elements);
00071       
00072       Setup::init(alg_, es);
00073       
00074       ::helper::MasterCollection<C1> master(src);
00075       auto_ptr<OutputCollection> isolations(new OutputCollection);
00076       if(src->size()!= 0) {
00077         typename OutputCollection::Filler filler(*isolations);
00078         vector<double> iso(master.size(),-1);
00079         size_t i = 0;
00080         for( typename C1::const_iterator lep = src->begin(); lep != src->end(); ++ lep )
00081           iso[master.index(i++)] = alg_(*lep, *elements); 
00082         filler.insert(master.get(), iso.begin(), iso.end());
00083         filler.fill();
00084       }
00085       evt.put( isolations );
00086     }
00087 
00088   }
00089 }
00090 
00091 #endif