CMS 3D CMS Logo

Pythia8Source.cc

Go to the documentation of this file.
00001 /*
00002  *  Mikhail Kirsanov
00003  *  04/05/07
00004  */
00005 
00006 #include "GeneratorInterface/Pythia8Interface/interface/Pythia8Source.h"
00007 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
00008 #include "SimDataFormats/HepMCProduct/interface/GenInfoProduct.h"
00009 #include "FWCore/Framework/interface/Event.h"
00010 #include "FWCore/Framework/interface/Run.h"
00011 #include "FWCore/ServiceRegistry/interface/Service.h"
00012 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00013 #include "CLHEP/Random/JamesRandom.h"
00014 #include "CLHEP/Random/RandFlat.h"
00015 
00016 #include <iostream>
00017 #include "time.h"
00018 
00019 #include "Basics.h"
00020 
00021 using namespace edm;
00022 using namespace std;
00023 
00024 //#define TXGIVE txgive_
00025 //extern "C" {
00026 //  void TXGIVE(const char*,int length);
00027 //}
00028 
00029 //#define TXGIVE_INIT txgive_init_
00030 //extern "C" {
00031 //  void TXGIVE_INIT();
00032 //}
00033 
00034 
00035 //used for defaults
00036   static const unsigned long kNanoSecPerSec = 1000000000;
00037   static const unsigned long kAveEventPerSec = 200;
00038 
00039 Pythia8Source::Pythia8Source( const ParameterSet & pset, 
00040                             InputSourceDescription const& desc ) :
00041   GeneratedInputSource(pset, desc),
00042   pythiaPylistVerbosity_ (pset.getUntrackedParameter<int>("pythiaPylistVerbosity",0)),
00043   pythiaHepMCVerbosity_ (pset.getUntrackedParameter<bool>("pythiaHepMCVerbosity",false)),
00044   maxEventsToPrint_ (pset.getUntrackedParameter<int>("maxEventsToPrint",1)),
00045   comenergy(pset.getUntrackedParameter<double>("comEnergy",14000.))
00046   
00047 {
00048   
00049   cout << "Pythia8Source: initializing Pythia. " << endl;
00050   
00051   
00052   // PYLIST Verbosity Level
00053   // Valid PYLIST arguments are: 1, 2, 3, 5, 7, 11, 12, 13
00054   pythiaPylistVerbosity_ = pset.getUntrackedParameter<int>("pythiaPylistVerbosity",0);
00055   cout << "Pythia PYLIST verbosity level = " << pythiaPylistVerbosity_ << endl;
00056   
00057   // HepMC event verbosity Level
00058   pythiaHepMCVerbosity_ = pset.getUntrackedParameter<bool>("pythiaHepMCVerbosity",false);
00059   cout << "Pythia HepMC verbosity = " << pythiaHepMCVerbosity_ << endl; 
00060 
00061   //Max number of events printed on verbosity level 
00062   maxEventsToPrint_ = pset.getUntrackedParameter<int>("maxEventsToPrint",0);
00063   cout << "Number of events to be printed = " << maxEventsToPrint_ << endl;
00064 
00065 
00066   //In the future, we will get the random number seed on each event and tell 
00067   // pythia to use that new seed
00068     cout << "----------------------------------------------" << endl;
00069     cout << "Setting Pythia8 random number seed " << endl;
00070     cout << "----------------------------------------------" << endl;
00071   edm::Service<RandomNumberGenerator> rng;
00072   uint32_t seed = rng->mySeed();
00073   Pythia8::Rndm::init(seed);
00074 
00075     // Generator. Process selection. LHC initialization. Histogram.
00076   pythia = new Pythia8::Pythia;
00077   pythia8event = &(pythia->event);
00078 
00079   // Set PYTHIA parameters in a single ParameterSet
00080   ParameterSet pythia_params =
00081     pset.getParameter<ParameterSet>("PythiaParameters") ;
00082 
00083   // The parameter sets to be read (default, min bias, user ...) in the
00084   // proper order.
00085   vector<string> setNames =
00086     pythia_params.getParameter<vector<string> >("parameterSets");
00087 
00088   // Loop over the sets
00089   for ( unsigned i=0; i<setNames.size(); ++i ) {
00090 
00091     string mySet = setNames[i];
00092 
00093     // Read the PYTHIA parameters for each set of parameters
00094     vector<string> pars =
00095       pythia_params.getParameter<vector<string> >(mySet);
00096 
00097     if (mySet != "CSAParameters") {
00098       cout << "----------------------------------------------" << endl;
00099       cout << "Read PYTHIA parameter set " << mySet << endl;
00100       cout << "----------------------------------------------" << endl;
00101 
00102       // Loop over all parameters and stop in case of mistake
00103       for( vector<string>::const_iterator
00104            itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
00105 
00106         if ( ! pythia->readString(*itPar) ) {
00107           throw edm::Exception(edm::errors::Configuration,"PythiaError") 
00108           <<" pythia8 did not accept the following \""<<*itPar<<"\"";
00109         }
00110 
00111       }
00112 
00113     } else {
00114       cout << " mySet == CSAParameters, not valid for Pythia8 " << endl;
00115       exit(1);
00116     }
00117 
00118   }
00119 
00120   pythia->init( 2212, 2212, comenergy);
00121 
00122   pythia->settings.listChanged();
00123 
00124   ToHepMC = new HepMC::I_Pythia8;
00125 //  ToHepMC->set_crash_on_problem();
00126 
00127   cout << endl; // Stetically add for the output
00128   //********                                      
00129   
00130   produces<HepMCProduct>();
00131   produces<GenInfoProduct, edm::InRun>();
00132 
00133   cout << "Pythia8Source: starting event generation ... " << endl;
00134 
00135 }
00136 
00137 
00138 Pythia8Source::~Pythia8Source(){
00139   cout << "PythiaSource: event generation done. " << endl;
00140   pythia->statistics();
00141   delete pythia;
00142   delete ToHepMC;
00143   clear();
00144 }
00145 
00146 void Pythia8Source::clear() {
00147  
00148 }
00149 
00150 
00151 void Pythia8Source::endRun(Run & r) {
00152 
00153  double cs = pythia->info.sigmaGen(); // cross section in mb
00154  auto_ptr<GenInfoProduct> giprod (new GenInfoProduct());
00155  giprod->set_cross_section(cs);
00156 // giprod->set_external_cross_section(extCrossSect);
00157 // giprod->set_filter_efficiency(extFilterEff);
00158  r.put(giprod);
00159 
00160 }
00161 
00162 
00163 bool Pythia8Source::produce(Event & e) {
00164 
00165     auto_ptr<HepMCProduct> bare_product(new HepMCProduct());  
00166     //cout << "
00167     //********                                         
00168     //
00169     
00170     if (!pythia->next()) return false;  // generate one event with Pythia
00171 
00172     HepMC::GenEvent* hepmcevt = new HepMC::GenEvent();
00173     ToHepMC->fill_next_event( *pythia8event, hepmcevt );
00174 
00175     
00176 //    hepmcevt->set_signal_process_id(pypars.msti[0]);
00177     hepmcevt->set_signal_process_id(pythia->info.code());
00178 //    hepmcevt->set_event_scale(pypars.pari[16]);
00179     hepmcevt->set_event_scale(pythia->info.pTHat());
00180     hepmcevt->set_event_number(numberEventsInRun() - remainingEvents() - 1);
00181 
00182     int id1 = pythia->info.id1();
00183     int id2 = pythia->info.id2();
00184     if ( id1 == 21 ) id1 = 0;
00185     if ( id2 == 21 ) id2 = 0;
00186     double x1 = pythia->info.x1();
00187     double x2 = pythia->info.x2();
00188     double Q  = pythia->info.QRen();
00189     double pdf1 = pythia->info.pdf1()/pythia->info.x1();
00190     double pdf2 = pythia->info.pdf2()/pythia->info.x2();
00191     hepmcevt->set_pdf_info( HepMC::PdfInfo(id1,id2,x1,x2,Q,pdf1,pdf2) ) ;
00192 
00193     hepmcevt->weights().push_back( pythia->info.weight() );
00194 
00195     //******** Verbosity ********
00196     
00197     if(event() <= maxEventsToPrint_ &&
00198        (pythiaPylistVerbosity_ || pythiaHepMCVerbosity_) ) {
00199 
00200       // Prints PYLIST info
00201       if(pythiaPylistVerbosity_) {
00202 //      call_pylist(pythiaPylistVerbosity_);
00203         pythia->info.list();
00204         pythia->event.list();
00205     }
00206       
00207       // Prints HepMC event
00208       if(pythiaHepMCVerbosity_) {
00209 //  cout << "Event process = " << pypars.msti[0] << endl 
00210     cout << "Event process = " << pythia->info.code() << endl
00211         << "----------------------" << endl;
00212         hepmcevt->print();
00213       }
00214     }
00215     
00216 
00217     if(hepmcevt)  bare_product->addHepMCData(hepmcevt);
00218 
00219     e.put(bare_product);
00220 
00221     return true;
00222 }
00223 
00224 //bool 
00225 //Pythia8Source::call_pygive(const std::string& iParm ) {
00226 
00227 //  int numWarn = pydat1.mstu[26]; //# warnings
00228 //  int numErr = pydat1.mstu[22];// # errors
00229   
00230 //call the fortran routine pygive with a fortran string
00231 //  PYGIVE( iParm.c_str(), iParm.length() );  
00232   //  PYGIVE( iParm );  
00233 //if an error or warning happens it is problem
00234 //  return pydat1.mstu[26] == numWarn && pydat1.mstu[22] == numErr;   
00235  
00236 //}
00237 
00238 bool 
00239 Pythia8Source::call_txgive(const std::string& iParm ) {
00240   
00241 //   TXGIVE( iParm.c_str(), iParm.length() );
00242 //   cout << "     " <<  iParm.c_str() << endl; 
00243 
00244         return 1;  
00245 }
00246 
00247 bool 
00248 Pythia8Source::call_txgive_init() {
00249   
00250 //   TXGIVE_INIT();
00251 //   cout << "  Setting CSA defaults.   "   << endl; 
00252 
00253         return 1;  
00254 }

Generated on Tue Jun 9 17:37:09 2009 for CMSSW by  doxygen 1.5.4