CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/IOMC/ParticleGuns/src/FileRandomKEThetaGunProducer.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include "IOMC/ParticleGuns/interface/FileRandomKEThetaGunProducer.h"
00004 
00005 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00006 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
00007 
00008 #include "FWCore/Framework/interface/Event.h"
00009 #include "FWCore/Utilities/interface/Exception.h"
00010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 
00013 using namespace edm;
00014 
00015 FileRandomKEThetaGunProducer::FileRandomKEThetaGunProducer(const edm::ParameterSet& pset) :
00016   FlatBaseThetaGunProducer(pset) {
00017 
00018   edm::ParameterSet defpset ;
00019   edm::ParameterSet pgun_params = 
00020     pset.getParameter<edm::ParameterSet>("PGunParameters") ;
00021   
00022   edm::FileInPath fp   = pgun_params.getParameter<edm::FileInPath>("File");
00023   std::string     file = fp.fullPath();
00024   particleN            = pgun_params.getParameter<int>("Particles");
00025   if (particleN <= 0) particleN = 1;
00026   edm::LogInfo("FlatThetaGun") << "Internal FileRandomKEThetaGun is initialzed"
00027                                << " with data read from " << file << " and "
00028                                << particleN << " particles created/event";
00029   ifstream is(file.c_str(), std::ios::in);
00030   if (is) {
00031     double energy, elem, sum=0;
00032     while (!is.eof()) {
00033       is >> energy >> elem;
00034       kineticE.push_back(0.001*energy);
00035       fdistn.push_back(elem);
00036       sum += elem;
00037       if (fVerbosity > 0) LogDebug("FlatThetaGun") << "KE " << energy
00038                                                    <<" GeV Count rate " <<elem;
00039     }
00040     is.close();
00041     double last = 0;
00042     for (unsigned int i=0; i<fdistn.size(); i++) {
00043       fdistn[i] /= sum;
00044       fdistn[i] += last;
00045       last       = fdistn[i];
00046       if (fVerbosity > 0) LogDebug("FlatThetaGun") << "Bin [" << i << "]: KE "
00047                                                    << kineticE[i] << " Distn "
00048                                                    << fdistn[i];
00049     }
00050   }
00051   if (kineticE.size() < 2) 
00052     throw cms::Exception("FileNotFound","Not enough data point found in file ")
00053       <<  file << "\n";
00054 
00055   
00056   produces<HepMCProduct>();
00057   produces<GenEventInfoProduct>();
00058 }
00059 
00060 FileRandomKEThetaGunProducer::~FileRandomKEThetaGunProducer() {}
00061 
00062 void FileRandomKEThetaGunProducer::produce(edm::Event & e, const edm::EventSetup& es) {
00063 
00064   if (fVerbosity > 0) 
00065     LogDebug("FlatThetaGun") << "FileRandomKEThetaGunProducer : Begin New Event Generation"; 
00066    
00067   // event loop (well, another step in it...)
00068           
00069   // no need to clean up GenEvent memory - done in HepMCProduct
00070   
00071   // here re-create fEvt (memory)
00072   //
00073   fEvt = new HepMC::GenEvent() ;
00074    
00075   // now actualy, cook up the event from PDGTable and gun parameters
00076   //
00077 
00078   // 1st, primary vertex
00079   //
00080   HepMC::GenVertex* Vtx = new HepMC::GenVertex( HepMC::FourVector(0.,0.,0.));
00081    
00082   // loop over particles
00083   //
00084   int barcode = 1;
00085   for (int ip=0; ip<particleN; ip++) {
00086     double keMin=kineticE[0], keMax=kineticE[1];
00087     double rMin=fdistn[0], rMax=fdistn[1];
00088     double r1 = fRandomGenerator->fire();
00089     for (unsigned int ii=kineticE.size()-2; ii>0; --ii) {
00090       if (r1 > fdistn[ii]) {
00091         keMin = kineticE[ii]; keMax = kineticE[ii+1]; 
00092         rMin  = fdistn[ii];   rMax  = fdistn[ii+1]; break;
00093       }
00094     }
00095     double ke    = (keMin*(rMax-r1) + keMax*(r1-rMin))/(rMax-rMin);
00096     if (fVerbosity > 1) 
00097       LogDebug("FlatThetaGun") << "FileRandomKEThetaGunProducer: KE " << ke
00098                                << " in range " << keMin << ":" << keMax 
00099                                << " with " << r1 <<" in " << rMin <<":" <<rMax;
00100     double theta = fRandomGenerator->fire(fMinTheta, fMaxTheta);
00101     double phi   = fRandomGenerator->fire(fMinPhi, fMaxPhi);
00102     int PartID   = fPartIDs[0];
00103     const HepPDT::ParticleData* 
00104       PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID))) ;
00105     double mass   = PData->mass().value() ;
00106     double energy = ke + mass;
00107     double mom2   = ke*ke + 2.*ke*mass ;
00108     double mom    = std::sqrt(mom2);
00109     double px     = mom*sin(theta)*cos(phi);
00110     double py     = mom*sin(theta)*sin(phi);
00111     double pz     = mom*cos(theta);
00112 
00113     HepMC::FourVector p(px,py,pz,energy) ;
00114     HepMC::GenParticle* Part =  new HepMC::GenParticle(p,PartID,1);
00115     Part->suggest_barcode( barcode ) ;
00116     barcode++ ;
00117     Vtx->add_particle_out(Part);
00118        
00119   }
00120   fEvt->add_vertex(Vtx) ;
00121   fEvt->set_event_number(e.id().event()) ;
00122   fEvt->set_signal_process_id(20) ;  
00123    
00124    
00125   if (fVerbosity > 0) {
00126     fEvt->print() ;  
00127   }  
00128 
00129   std::auto_ptr<HepMCProduct> BProduct(new HepMCProduct()) ;
00130   BProduct->addHepMCData( fEvt );
00131   e.put(BProduct);
00132 
00133   std::auto_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
00134   e.put(genEventInfo);
00135 
00136   if ( fVerbosity > 0 ) 
00137     LogDebug("FlatThetaGun") << "FileRandomKEThetaGunProducer : Event Generation Done";
00138 }