00001 #ifndef UtilAlgos_NtpProducer_h 00002 #define UtilAlgos_NtpProducer_h 00003 00013 #include "FWCore/Framework/interface/EDProducer.h" 00014 #include "FWCore/Framework/interface/Event.h" 00015 #include "FWCore/ParameterSet/interface/ParameterSet.h" 00016 #include "FWCore/ParameterSet/interface/InputTag.h" 00017 #include "PhysicsTools/Utilities/interface/StringObjectFunction.h" 00018 00019 template<typename C> 00020 class NtpProducer : public edm::EDProducer { 00021 public: 00023 NtpProducer( const edm::ParameterSet& ); 00025 ~NtpProducer(); 00026 00027 protected: 00029 virtual void produce( edm::Event&, const edm::EventSetup& ); 00030 00031 private: 00033 edm::InputTag src_; 00035 std::vector<std::pair<std::string, StringObjectFunction<typename C::value_type> > > tags_; 00036 }; 00037 00038 template<typename C> 00039 NtpProducer<C>::NtpProducer( const edm::ParameterSet& par ) : 00040 src_( par.template getParameter<edm::InputTag>( "src" ) ) { 00041 std::vector<edm::ParameterSet> variables = 00042 par.template getParameter<std::vector<edm::ParameterSet> >("variables"); 00043 std::vector<edm::ParameterSet>::const_iterator 00044 q = variables.begin(), end = variables.end(); 00045 for (; q!=end; ++q) { 00046 std::string tag = q->getUntrackedParameter<std::string>("tag"); 00047 StringObjectFunction<typename C::value_type> quantity(q->getUntrackedParameter<std::string>("quantity")); 00048 tags_.push_back(std::make_pair(tag, quantity)); 00049 produces<std::vector<float> >(tag).setBranchAlias(tag); 00050 } 00051 } 00052 00053 template<typename C> 00054 NtpProducer<C>::~NtpProducer() { 00055 } 00056 00057 template<typename C> 00058 void NtpProducer<C>::produce( edm::Event& iEvent, const edm::EventSetup& ) { 00059 edm::Handle<C> coll; 00060 iEvent.getByLabel(src_, coll); 00061 00062 typename std::vector<std::pair<std::string, StringObjectFunction<typename C::value_type> > >::const_iterator 00063 q = tags_.begin(), end = tags_.end(); 00064 for(;q!=end; ++q) { 00065 std::auto_ptr<std::vector<float> > x(new std::vector<float>); 00066 x->reserve(coll->size()); 00067 for (typename C::const_iterator elem=coll->begin(); elem!=coll->end(); ++elem ) { 00068 x->push_back(q->second(*elem)); 00069 } 00070 iEvent.put(x, q->first); 00071 } 00072 } 00073 00074 #endif