00001 00008 #include "FWCore/Framework/interface/EDProducer.h" 00009 #include "FWCore/ParameterSet/interface/InputTag.h" 00010 #include "DataFormats/Candidate/interface/CandidateFwd.h" 00011 #include <vector> 00012 00013 namespace edm { class ParameterSet; } 00014 namespace HepMC { class GenParticle; class GenEvent; } 00015 00016 class GenEventWeightProducer : public edm::EDProducer { 00017 public: 00019 GenEventWeightProducer( const edm::ParameterSet & ); 00020 00021 private: 00022 void produce( edm::Event& evt, const edm::EventSetup& es ); 00023 edm::InputTag src_; 00024 }; 00025 00026 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h" 00027 #include "DataFormats/Common/interface/Handle.h" 00028 #include "FWCore/Framework/interface/Event.h" 00029 #include "FWCore/Utilities/interface/EDMException.h" 00030 #include "FWCore/ParameterSet/interface/ParameterSet.h" 00031 using namespace edm; 00032 using namespace std; 00033 using namespace HepMC; 00034 00035 GenEventWeightProducer::GenEventWeightProducer( const ParameterSet & p ) : 00036 src_( p.getParameter<InputTag>( "src" ) ) { 00037 produces<double>(); 00038 } 00039 00040 00041 void GenEventWeightProducer::produce( Event& evt, const EventSetup& es ) { 00042 Handle<HepMCProduct> mc; 00043 evt.getByLabel( src_, mc ); 00044 const GenEvent * genEvt = mc->GetEvent(); 00045 if( genEvt == 0 ) 00046 throw edm::Exception( edm::errors::InvalidReference ) 00047 << "HepMC has null pointer to GenEvent" << endl; 00048 auto_ptr<double> weight( new double(1) ); 00049 HepMC::WeightContainer wc = genEvt->weights(); 00050 if ( wc.size() > 0 ) (*weight) = wc[ 0 ]; 00051 evt.put( weight ); 00052 } 00053 00054 #include "FWCore/Framework/interface/MakerMacros.h" 00055 00056 DEFINE_FWK_MODULE( GenEventWeightProducer ); 00057