CMS 3D CMS Logo

AlpgenProducer.cc

Go to the documentation of this file.
00001 /*
00002  *  $Date: 2008/12/05 20:41:55 $
00003  *  $Revision: 1.5 $
00004  *  
00005  *  Filip Moorgat & Hector Naves 
00006  *  26/10/05
00007  * 
00008  *  Patrick Janot : added the PYTHIA card reading
00009  *
00010  *  Serge SLabospitsky : added Alpgen reading tools 
00011  */
00012 
00013 #include "GeneratorInterface/AlpgenInterface/interface/AlpgenProducer.h"
00014 #include "GeneratorInterface/AlpgenInterface/interface/PYR.h"
00015 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
00016 #include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
00017 #include "SimDataFormats/GeneratorProducts/interface/LHECommonBlocks.h"
00018 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
00019 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
00020 #include "FWCore/Framework/interface/Event.h"
00021 #include "FWCore/Framework/interface/Run.h"
00022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00023 #include "FWCore/ServiceRegistry/interface/Service.h"
00024 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00025 
00026 #include "time.h"
00027 
00028 using namespace edm; 
00029 using namespace std;
00030 
00031 // Generator modifications
00032 // ***********************
00033 #include "HepMC/PythiaWrapper6_2.h"
00034 #include "HepMC/IO_HEPEVT.h"
00035 
00036 //#include "GeneratorInterface/CommonInterface/interface/PretauolaWrapper.h"
00037 //#include "CLHEP/HepMC/ConvertHEPEVT.h"
00038 //#include "CLHEP/HepMC/CBhepevt.h"
00039 
00040 #include "GeneratorInterface/CommonInterface/interface/PythiaCMS.h"
00041 #include "GeneratorInterface/CommonInterface/interface/Txgive.h"
00042 
00043 HepMC::IO_HEPEVT conv2;
00044 // ***********************
00045 
00046 
00047 //used for defaults
00048   static const unsigned long kNanoSecPerSec = 1000000000;
00049   static const unsigned long kAveEventPerSec = 200;
00050 
00051 AlpgenProducer::AlpgenProducer( const ParameterSet & pset) :
00052   EDProducer(), evt(0), 
00053   pythiaPylistVerbosity_ (pset.getUntrackedParameter<int>("pythiaPylistVerbosity",0)),
00054   pythiaHepMCVerbosity_ (pset.getUntrackedParameter<bool>("pythiaHepMCVerbosity",false)),
00055   maxEventsToPrint_ (pset.getUntrackedParameter<int>("maxEventsToPrint",1)),
00056 // JMM experimenting
00057   fileNames_ (pset.getUntrackedParameter<std::vector<std::string> >("fileNames")),
00058   eventsRead_(0),
00059   lheAlpgenUnwParHeader("AlpgenUnwParFile")  
00060 // end JMM experimenting
00061 {
00062   
00063   fileName_ = fileNames_[0];
00064   // strip the file: 
00065   if ( fileName_.find("file:") || fileName_.find("rfio:")){
00066     fileName_.erase(0,5);
00067   }   
00068 
00069   // open the .unw file to store additional 
00070   // informations in the AlpgenInfoProduct
00071 //  unwfile = new ifstream((fileName_+".unw").c_str());
00072   // get the number of input events from  _unw.par files
00073   char buffer[256];
00074   ifstream reader((fileName_+"_unw.par").c_str());
00075   char sNev[80];
00076   lheAlpgenUnwParHeader.addLine("\n");
00077   while ( reader.getline (buffer,256) ) {
00078     istringstream is(buffer);
00079     lheAlpgenUnwParHeader.addLine(std::string(buffer) + "\n");
00080     is >> sNev;
00081     Nev_ = atoi(sNev);
00082   }
00083 
00084 // JMM experimenting
00085 #ifdef NEVER
00086   //check that N(asked events) <= N(input events)
00087   if(maxEvents()>Nev_) {
00088     cout << "ALPGEN warning: Number of events requested > Number of unweighted events." << endl;
00089     cout << "                Execution will stop after processing the last unweighted event" << endl;  
00090   }
00091 
00092   if(maxEvents() != -1 && maxEvents() < Nev_) // stop at N(asked events) if N(asked events)<N(input events)
00093     Nev_ = maxEvents();
00094 #endif
00095 // end JMM experimenting
00096   
00097   // PYLIST Verbosity Level
00098   // Valid PYLIST arguments are: 1, 2, 3, 5, 7, 11, 12, 13
00099   pythiaPylistVerbosity_ = pset.getUntrackedParameter<int>("pythiaPylistVerbosity",0);
00100   
00101   // HepMC event verbosity Level
00102   pythiaHepMCVerbosity_ = pset.getUntrackedParameter<bool>("pythiaHepMCVerbosity",false);
00103   
00104   //Max number of events printed on verbosity level 
00105   maxEventsToPrint_ = pset.getUntrackedParameter<int>("maxEventsToPrint",0);
00106   
00107   // Set PYTHIA parameters in a single ParameterSet
00108   {
00109     ParameterSet pythia_params = 
00110       pset.getParameter<ParameterSet>("PythiaParameters") ;
00111     
00112     // Read the PYTHIA parameters for each set of parameters
00113     vector<string> pars = 
00114       pythia_params.getParameter<vector<string> >("pythia");
00115     
00116     // Loop over all parameters and stop in case of mistake
00117     for( vector<string>::const_iterator  
00118            itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
00119       static string sRandomValueSetting("MRPY(1)");
00120       if( 0 == itPar->compare(0,sRandomValueSetting.size(),sRandomValueSetting) ) {
00121         throw edm::Exception(edm::errors::Configuration,"PythiaError")
00122           <<" attempted to set random number seed.";
00123       }
00124       if( ! call_pygive(*itPar) ) {
00125         throw edm::Exception(edm::errors::Configuration,"PythiaError") 
00126           <<" pythia did not accept the following \""<<*itPar<<"\"";
00127       }
00128     }
00129   }
00130 
00131   // Read the Alpgen parameters
00132 
00133   // read External Generator parameters
00134   {   ParameterSet generator_params = 
00135         pset.getParameter<ParameterSet>("GeneratorParameters") ;
00136   vector<string> pars = 
00137     generator_params.getParameter<vector<string> >("generator");
00138   for( vector<string>::const_iterator  
00139          itPar = pars.begin(); itPar != pars.end(); ++itPar ) 
00140     {
00141       call_txgive(*itPar);          
00142     }
00143   // giving to txgive a string with the alpgen filename
00144   string tmpstring = "UNWFILE = " + fileName_;
00145   call_txgive(tmpstring);
00146   }
00147   
00148   
00149   //In the future, we will get the random number seed on each event and tell 
00150   // pythia to use that new seed
00151   edm::Service<RandomNumberGenerator> rng;
00152   randomEngine = fRandomEngine = &(rng->getEngine());
00153   uint32_t seed = rng->mySeed();
00154   ostringstream sRandomSet;
00155   sRandomSet <<"MRPY(1)="<<seed;
00156   call_pygive(sRandomSet.str());
00157   
00158   //  call_pretauola(-1);     // TAUOLA initialization
00159   call_pyinit( "USER", "p", "p", 14000. );
00160   
00161   cout << endl; // Stetically add for the output
00162   //********                                      
00163   
00164   produces<HepMCProduct>();
00165   produces<LHEEventProduct>();
00166 
00167 //  produces<AlpWgtFileInfoProduct, edm::InRun>();
00168   produces<LHERunInfoProduct, edm::InRun>();
00169 }
00170 
00171 
00172 AlpgenProducer::~AlpgenProducer(){
00173   call_pystat(1);
00174   //  call_pretauola(1);  // output from TAUOLA 
00175   alpgen_end();
00176   clear(); 
00177 }
00178 
00179 void AlpgenProducer::clear() {
00180   
00181 }
00182 
00183 void AlpgenProducer::beginRun(Run & r) {
00184   // get the LHE init information from Fortran code
00185   lhef::HEPRUP heprup;
00186   lhef::CommonBlocks::readHEPRUP(&heprup);
00187   auto_ptr<LHERunInfoProduct> runInfo(new LHERunInfoProduct(heprup));
00188 
00189   // information on weighted events
00190   LHERunInfoProduct::Header lheAlpgenWgtHeader("AlpgenWgtFile");
00191   lheAlpgenWgtHeader.addLine("\n");
00192   ifstream wgtascii((fileName_+".wgt").c_str());
00193   char buffer[512];
00194   while(wgtascii.getline(buffer,512)) {
00195     lheAlpgenWgtHeader.addLine(std::string(buffer) + "\n");
00196   }
00197 
00198   // comments on top
00199   LHERunInfoProduct::Header comments;
00200   comments.addLine("\n");
00201   comments.addLine("\tExtracted by AlpgenInterface\n");
00202 
00203   // build the final Run info object
00204   runInfo->addHeader(comments);
00205   runInfo->addHeader(lheAlpgenUnwParHeader);
00206   runInfo->addHeader(lheAlpgenWgtHeader);
00207   r.put(runInfo);
00208 }
00209 
00210 void AlpgenProducer::produce(Event & e, const EventSetup& es) {
00211   
00212   // exit if N(events asked) has been exceeded
00213   if(e.id().event()> Nev_) {
00214     throw cms::Exception("Generator") << "Can't produce event because _unw.par file is over.";
00215   } else {
00216     
00217     auto_ptr<HepMCProduct> bare_product(new HepMCProduct());  
00218     
00219 //    // Additional information from unweighted file
00220 //    auto_ptr<AlpgenInfoProduct> alp_product(new AlpgenInfoProduct());
00221 
00222     // Extract from .unw file the info for AlpgenInfoProduct
00223     
00224 //    char buffer[512];
00225 //    if(unwfile->getline(buffer,512)) {
00226 //      alp_product->EventInfo(buffer);
00227 //    }
00228 //    if(unwfile->getline(buffer,512)) 
00229 //      alp_product->InPartonInfo(buffer);
00230 //    if(unwfile->getline(buffer,512)) 
00231 //      alp_product->InPartonInfo(buffer);
00232 //    for(int i_out = 0; i_out <  alp_product->nTot()-2; i_out++) {
00233 //      if(unwfile->getline(buffer,512)) 
00234 //      alp_product->OutPartonInfo(buffer);
00235 //    }
00236 
00237     call_pyevnt();      // generate one event with Pythia
00238     //        call_pretauola(0);  // tau-lepton decays with TAUOLA 
00239 
00240     // fill the parton level LHE event information
00241     lhef::HEPEUP hepeup;
00242     lhef::CommonBlocks::readHEPEUP(&hepeup);
00243     hepeup.AQEDUP = hepeup.AQCDUP = -1.0; // alphas are not saved by Alpgen
00244     for(int i = 0; i < hepeup.NUP; i++)
00245       hepeup.SPINUP[i] = -9;    // Alpgen does not store spin information
00246     auto_ptr<LHEEventProduct> lheEvent(new LHEEventProduct(hepeup));
00247     // Moved to later, first we have to make sure that the event generated by Pythia is OK.
00248     // e.put(lheEvent);
00249 
00250     // The way this is implemented, call_pyevnt() will keep reading events from the .unw until
00251     // one of them is able to pass the matching veto. If it can't read a last event, it will
00252     // simply continue with whatever event was in memory. In this case, there are two possibilities:
00253     // A) The event passed the matching veto. There is meaningful information in HEPEUP. Continue.
00254     // (Will fail next Event with HEPEUP.NUP == 0. Harmless.)
00255     // B) The event didn't pass. HEPEUP.NUP == 0. This should never happen, and we should abort here.
00256     if(hepeup.NUP==0) {
00257       edm::LogInfo("Generator|TooLittleData") << "ALPGEN warning: last unweighted event reached.\n"
00258                                               << "                (hepeup.NUP == 0)\n"
00259                                               << "                The event number " << e.id().event() << " will not be written to disk.";  
00260       throw cms::Exception("Generator") << "Can't produce event because _unw.par file is over.";
00261     }
00262     
00263     call_pyhepc( 1 );
00264     //    HepMC::GenEvent* evt = conv2.getGenEventfromHEPEVT();
00265     HepMC::GenEvent* evt = conv2.read_next_event();
00266     
00267     evt->set_signal_process_id(pypars.msti[0]);
00268     ++eventsRead_;
00269     evt->set_event_number(eventsRead_);
00270     
00271     int id1 = pyint1.mint[14];
00272     int id2 = pyint1.mint[15];
00273     if ( id1 == 21 ) id1 = 0;
00274     if ( id2 == 21 ) id2 = 0; 
00275     double x1 = pyint1.vint[40];
00276     double x2 = pyint1.vint[41];  
00277     double Q  = pyint1.vint[50];
00278     double pdf1 = pyint1.vint[38];
00279     pdf1 /= x1 ;
00280     double pdf2 = pyint1.vint[39];
00281     pdf2 /= x2 ;
00282     evt->set_pdf_info( HepMC::PdfInfo(id1,id2,x1,x2,Q,pdf1,pdf2) ) ;
00283    
00284     evt->weights().push_back( pyint1.vint[96] );
00285 
00286     //******** Verbosity ********
00287     
00288     if(e.id().event() <= maxEventsToPrint_ &&
00289        (pythiaPylistVerbosity_ || pythiaHepMCVerbosity_)) {
00290       
00291       // Prints PYLIST info
00292       if(pythiaPylistVerbosity_) {
00293         call_pylist(pythiaPylistVerbosity_);
00294       }
00295       
00296       // Prints HepMC event
00297       if(pythiaHepMCVerbosity_) {
00298         cout << "Event process = " << pypars.msti[0] << endl 
00299              << "----------------------" << endl;
00300         evt->print();
00301       }
00302     }
00303 
00304     if(evt)  bare_product->addHepMCData(evt );
00305 
00306     // Last check to make sure there are no empty events. This DOES NOT
00307     // supersedes the AlpgenEmptyEventFilter, because this is a Producer,
00308     // not a source. So, it can only return, instead of returning false.
00309     // Should we take extra care here?
00310 
00311     if(!(evt->particles_empty())) {
00312       e.put(lheEvent);
00313       e.put(bare_product);
00314     }
00315     
00316     return;
00317   }
00318 }
00319 
00320 bool 
00321 AlpgenProducer::call_pygive(const std::string& iParm ) {
00322 
00323   int numWarn = pydat1.mstu[26]; //# warnings
00324   int numErr = pydat1.mstu[22];// # errors
00325   
00326 //call the fortran routine pygive with a fortran string
00327   PYGIVE( iParm.c_str(), iParm.length() );  
00328   //  PYGIVE( iParm );  
00329 //if an error or warning happens it is problem
00330   return pydat1.mstu[26] == numWarn && pydat1.mstu[22] == numErr;   
00331 }
00332 //------------
00333 bool 
00334 AlpgenProducer::call_txgive(const std::string& iParm ) 
00335    {
00336     //call the fortran routine txgive with a fortran string
00337     TXGIVE( iParm.c_str(), iParm.length() );  
00338     return 1;  
00339    }

Generated on Tue Jun 9 17:36:53 2009 for CMSSW by  doxygen 1.5.4