CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/GeneratorInterface/LHEInterface/plugins/LHE2HepMCConverter.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    LHE2HepMCConverter
00004 // Class:      LHE2HepMCConverter
00005 // 
00013 //
00014 // Original Author:  Piergiulio Lenzi,40 1-B01,+41227671638,
00015 //         Created:  Wed Aug 31 19:02:24 CEST 2011
00016 // $Id: LHE2HepMCConverter.cc,v 1.1 2011/09/06 12:58:42 fabiocos Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 #include <boost/shared_ptr.hpp>
00025 
00026 // user include files
00027 #include "FWCore/Framework/interface/Frameworkfwd.h"
00028 #include "FWCore/Framework/interface/EDProducer.h"
00029 
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Framework/interface/Run.h"
00032 #include "FWCore/Framework/interface/MakerMacros.h"
00033 
00034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00035 #include "FWCore/Utilities/interface/InputTag.h"
00036 #include "DataFormats/Common/interface/Handle.h"
00037 
00038 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
00039 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
00040 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00041 
00042 //
00043 // class declaration
00044 //
00045 
00046 class LHE2HepMCConverter : public edm::EDProducer {
00047    public:
00048       explicit LHE2HepMCConverter(const edm::ParameterSet&);
00049       ~LHE2HepMCConverter();
00050 
00051       static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
00052 
00053    protected:
00054 
00055   //      lhef::LHERunInfo *lheRunInfo() { return lheRunInfo_.get(); }
00056 
00057    private:
00058       virtual void beginJob() ;
00059       virtual void produce(edm::Event&, const edm::EventSetup&);
00060       virtual void endJob() ;
00061       
00062       virtual void beginRun(edm::Run&, edm::EventSetup const&);
00063       virtual void endRun(edm::Run&, edm::EventSetup const&);
00064       virtual void beginLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&);
00065       virtual void endLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&);
00066 
00067       // ----------member data ---------------------------
00068       edm::InputTag _lheEventSrcTag;
00069       edm::InputTag _lheRunSrcTag;
00070       const LHERunInfoProduct* _lheRunSrc;
00071 
00072   //      boost::shared_ptr<lhef::LHERunInfo> lheRunInfo_;
00073 
00074 };
00075 
00076 //
00077 // constants, enums and typedefs
00078 //
00079 
00080 
00081 //
00082 // static data member definitions
00083 //
00084 
00085 //
00086 // constructors and destructor
00087 //
00088 LHE2HepMCConverter::LHE2HepMCConverter(const edm::ParameterSet& iConfig):
00089 _lheRunSrc(0)
00090 {
00091    //register your products
00092    produces<edm::HepMCProduct>();
00093 
00094    _lheEventSrcTag = iConfig.getParameter<edm::InputTag>("LHEEventProduct");
00095    _lheRunSrcTag   = iConfig.getParameter<edm::InputTag>("LHERunInfoProduct");
00096 
00097 }
00098 
00099 
00100 LHE2HepMCConverter::~LHE2HepMCConverter()
00101 {
00102  
00103    // do anything here that needs to be done at desctruction time
00104    // (e.g. close files, deallocate resources etc.)
00105 
00106 }
00107 
00108 
00109 //
00110 // member functions
00111 //
00112 
00113 // ------------ method called to produce the data  ------------
00114 void
00115 LHE2HepMCConverter::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00116 {
00117    using namespace edm;
00118    Handle<LHEEventProduct> lheEventSrc;
00119    iEvent.getByLabel(_lheEventSrcTag, lheEventSrc);
00120 
00121    HepMC::GenEvent *evt = new HepMC::GenEvent();
00122    HepMC::GenVertex* v = new HepMC::GenVertex();
00123    evt->add_vertex(v);
00124    if (_lheRunSrc){
00125      HepMC::FourVector beam1(0, 0, _lheRunSrc->heprup().EBMUP.first, _lheRunSrc->heprup().EBMUP.first);
00126      HepMC::GenParticle* gp1 = new HepMC::GenParticle(beam1, _lheRunSrc->heprup().IDBMUP.first, 4);
00127      v->add_particle_in(gp1);
00128      HepMC::FourVector beam2(0, 0, _lheRunSrc->heprup().EBMUP.second, _lheRunSrc->heprup().EBMUP.second);
00129      HepMC::GenParticle* gp2 = new HepMC::GenParticle(beam2, _lheRunSrc->heprup().IDBMUP.second, 4);
00130      v->add_particle_in(gp2);
00131      evt->set_beam_particles(gp1, gp2);
00132    } else {
00133      LogWarning("LHE2HepMCConverter") << "Could not retrieve the LHERunInfoProduct for this event. You'll miss the beam particles in your HepMC product." ; 
00134    }
00135 
00136 
00137    for (int i = 0; i < lheEventSrc->hepeup().NUP; ++i) {
00138       if (lheEventSrc->hepeup().ISTUP[i] != 1) {
00139        //cout << reader->hepeup.ISTUP[i] << ", " << reader->hepeup.IDUP[i] << endl;
00140        continue;
00141       }
00142       HepMC::FourVector p(lheEventSrc->hepeup().PUP[i][0], lheEventSrc->hepeup().PUP[i][1],
00143                           lheEventSrc->hepeup().PUP[i][2], lheEventSrc->hepeup().PUP[i][3]);
00144       HepMC::GenParticle* gp = new HepMC::GenParticle(p, lheEventSrc->hepeup().IDUP[i], 1);
00145       gp->set_generated_mass(lheEventSrc->hepeup().PUP[i][4]);
00146       v->add_particle_out(gp);
00147    } 
00148 
00149    std::auto_ptr<HepMCProduct> pOut(new HepMCProduct(evt));
00150    iEvent.put(pOut);
00151 
00152 }
00153 
00154 // ------------ method called once each job just before starting event loop  ------------
00155 void 
00156 LHE2HepMCConverter::beginJob()
00157 {
00158 }
00159 
00160 // ------------ method called once each job just after ending the event loop  ------------
00161 void 
00162 LHE2HepMCConverter::endJob() {
00163 }
00164 
00165 // ------------ method called when starting to processes a run  ------------
00166 void 
00167 LHE2HepMCConverter::beginRun(edm::Run& iRun, edm::EventSetup const&)
00168 {
00169   
00170   edm::Handle<LHERunInfoProduct> lheRunSrcHandle;
00171   iRun.getByLabel(_lheRunSrcTag, lheRunSrcHandle);
00172   if (lheRunSrcHandle.isValid()) {
00173     _lheRunSrc = lheRunSrcHandle.product();
00174   }
00175   else {
00176     if (_lheRunSrcTag.label() != "source" ) {
00177       iRun.getByLabel("source", lheRunSrcHandle);
00178       if (lheRunSrcHandle.isValid()) {
00179         _lheRunSrc = lheRunSrcHandle.product();
00180         edm::LogInfo("LHE2HepMCConverter") << "Taking LHERunInfoproduct from source";
00181       }
00182       else
00183         edm::LogWarning("LHE2HepMCConverter") << "No LHERunInfoProduct from source"; 
00184     }
00185 
00186   }
00187 
00188 }
00189 
00190 // ------------ method called when ending the processing of a run  ------------
00191 void 
00192 LHE2HepMCConverter::endRun(edm::Run&, edm::EventSetup const&)
00193 {
00194 }
00195 
00196 // ------------ method called when starting to processes a luminosity block  ------------
00197 void 
00198 LHE2HepMCConverter::beginLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&)
00199 {
00200 }
00201 
00202 // ------------ method called when ending the processing of a luminosity block  ------------
00203 void 
00204 LHE2HepMCConverter::endLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&)
00205 {
00206 }
00207 
00208 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
00209 void
00210 LHE2HepMCConverter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
00211   //The following says we do not know what parameters are allowed so do no validation
00212   // Please change this to state exactly what you do use, even if it is no parameters
00213   edm::ParameterSetDescription desc;
00214   desc.setUnknown();
00215   descriptions.addDefault(desc);
00216 }
00217 
00218 //define this as a plug-in
00219 DEFINE_FWK_MODULE(LHE2HepMCConverter);