CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/GeneratorInterface/BeamHaloGenerator/src/BeamHaloProducer.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <time.h>
00003 
00004 #include "FWCore/Framework/interface/Event.h"
00005 #include "FWCore/Framework/interface/Run.h"
00006 #include "FWCore/ServiceRegistry/interface/Service.h"
00007 #include "FWCore/Utilities/interface/Exception.h"
00008 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00009 
00010 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00011 #include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
00012 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
00013 
00014 #include "GeneratorInterface/BeamHaloGenerator/interface/BeamHaloProducer.h"
00015 #include "GeneratorInterface/BeamHaloGenerator/interface/PYR.h"
00016 
00017 using namespace edm;
00018 using namespace std;
00019 
00020 #include "HepMC/IO_HEPEVT.h"
00021 #include "HepMC/HEPEVT_Wrapper.h"
00022 // #include "HepMC/ConvertHEPEVT.h"
00023 // #include "HepMC/CBhepevt.h"
00024 #include "HepMC/WeightContainer.h"
00025 
00026 
00027 #define KI_BHG_INIT ki_bhg_init_
00028 extern "C" {
00029   void KI_BHG_INIT(long& seed);
00030 }
00031 
00032 #define BHSETPARAM bhsetparam_
00033 extern "C" {
00034   void BHSETPARAM(int* iparam, float* fparam, const char* cparam, int length);
00035 }
00036 
00037 #define KI_BHG_FILL ki_bhg_fill_
00038 extern "C" {
00039     void KI_BHG_FILL(int& iret, float& weight);
00040 }
00041 
00042 #define KI_BHG_STAT ki_bhg_stat_
00043 extern "C" {
00044     void KI_BHG_STAT(int &iret);
00045 }
00046 
00047 
00048 // HepMC::ConvertHEPEVT conv;
00049 //include "HepMC/HEPEVT_Wrapper.h"
00050 static HepMC::HEPEVT_Wrapper wrapper;
00051 static HepMC::IO_HEPEVT conv;
00052 
00053 
00054 BeamHaloProducer::~BeamHaloProducer() {
00055         int iret=0;
00056         call_ki_bhg_stat(iret);
00057 }
00058 
00059 
00060 BeamHaloProducer::BeamHaloProducer( const ParameterSet & pset) :
00061         evt(0)
00062 {
00063 
00064    int iparam[8];
00065    float fparam[4];
00066    std::string cparam;
00067  // -- from bhgctrl.inc
00068    iparam[0]  = pset.getUntrackedParameter<int>("GENMOD");
00069    iparam[1]  = pset.getUntrackedParameter<int>("LHC_B1");
00070    iparam[2]  = pset.getUntrackedParameter<int>("LHC_B2");
00071    iparam[3]  = pset.getUntrackedParameter<int>("IW_MUO");
00072    iparam[4]  = pset.getUntrackedParameter<int>("IW_HAD");
00073    iparam[5]  = 9999999;
00074    iparam[6]  = pset.getUntrackedParameter<int>("OFFSET",0);
00075    iparam[7]  = pset.getUntrackedParameter<int>("shift_bx");
00076    
00077    fparam[0]  = (float)pset.getUntrackedParameter<double>("EG_MIN");
00078    fparam[1]  = (float)pset.getUntrackedParameter<double>("EG_MAX");
00079 
00080    fparam[2] = (float)pset.getUntrackedParameter<double>("BXNS");
00081    fparam[3]  = (float)pset.getUntrackedParameter<double>("W0",1.0);
00082 
00083    cparam     = pset.getUntrackedParameter<std::string>("G3FNAME","input.txt");
00084    call_bh_set_parameters(iparam,fparam,cparam);
00085 
00086 
00087 // -- Seed for randomnumbers
00088     Service<RandomNumberGenerator> rng;
00089     _BeamHalo_randomEngine = &(rng->getEngine());
00090     long seed = (long)(rng->mySeed());
00091 
00092 
00093 // -- initialisation
00094    call_ki_bhg_init(seed);
00095 
00096 
00097   produces<HepMCProduct>();
00098   produces<GenEventInfoProduct>();
00099   produces<GenRunInfoProduct, InRun>();
00100 
00101   cout << "BeamHaloProducer: starting event generation ... " << endl;
00102 }
00103 
00104 
00105 void BeamHaloProducer::clear()
00106 {
00107 }
00108 
00109 void BeamHaloProducer::produce(Event & e, const EventSetup & es) {
00110         // cout << "in produce " << endl;
00111 
00112   //            auto_ptr<HepMCProduct> bare_product(new HepMCProduct());
00113 
00114         // cout << "apres autoptr " << endl;
00115 
00116         int iret=0;
00117         float weight = 0;
00118         call_ki_bhg_fill(iret, weight);
00119 
00120 // Throw an exception if call_ki_bhg_fill(...) fails.  Use the EventCorruption
00121 // exception since it maps onto SkipEvent which is what we want to do here.
00122 
00123         if( iret < 0 )
00124           throw edm::Exception(edm::errors::EventCorruption)
00125             << "BeamHaloProducer: function call_ki_bhg_fill returned " << iret << endl;
00126 
00127         // cout << "apres fortran " << endl;
00128 
00129 
00130         // HepMC::GenEvent* evt = conv.getGenEventfromHEPEVT();
00131         //      HepMC::GenEvent* evt = conv.read_next_event();  seems to be broken (?)
00132   evt = new HepMC::GenEvent();
00133 
00134   for (int theindex = 1; theindex<=wrapper.number_entries(); theindex++) {
00135   HepMC::GenVertex* Vtx = new  HepMC::GenVertex(HepMC::FourVector(wrapper.x(theindex),wrapper.y(theindex),wrapper.z(theindex),wrapper.t(theindex)));
00136   HepMC::FourVector p(wrapper.px(theindex),wrapper.py(theindex),wrapper.pz(theindex),wrapper.e(theindex));
00137   HepMC::GenParticle* Part = 
00138     new HepMC::GenParticle(p,wrapper.id(theindex),wrapper.status(theindex));
00139   Vtx->add_particle_out(Part); 
00140   evt->add_vertex(Vtx);
00141   }
00142 
00143   evt->set_event_number(e.id().event());
00144 
00145         HepMC::WeightContainer& weights = evt -> weights();
00146         weights.push_back(weight);
00147         //      evt->print();
00148   std::auto_ptr<HepMCProduct> CMProduct(new HepMCProduct());
00149   if (evt) CMProduct->addHepMCData(evt);
00150   e.put(CMProduct);
00151 
00152   auto_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(evt));
00153   e.put(genEventInfo);
00154 }
00155 
00156 void BeamHaloProducer::endRun( Run &run, const EventSetup& es )
00157 {
00158    // just create an empty product
00159    // to keep the EventContent definitions happy
00160    // later on we might put the info into the run info that this is a PGun
00161    auto_ptr<GenRunInfoProduct> genRunInfo( new GenRunInfoProduct() );
00162    run.put( genRunInfo );
00163 }
00164 
00165 bool BeamHaloProducer::call_bh_set_parameters(int* ival, float* fval, const std::string cval_string) {
00166   BHSETPARAM(ival,fval,cval_string.c_str(),cval_string.length());
00167         return true;
00168 }
00169 
00170 bool BeamHaloProducer::call_ki_bhg_init(long& seed) {
00171         KI_BHG_INIT(seed);
00172         return true;
00173 }
00174 
00175 bool BeamHaloProducer::call_ki_bhg_fill(int& iret, float& weight) {
00176         KI_BHG_FILL(iret,weight);
00177         return true;
00178 }
00179 
00180 bool BeamHaloProducer::call_ki_bhg_stat(int& iret) {
00181         KI_BHG_STAT(iret);
00182         return true;
00183 }