00001
00017 #include "GeneratorInterface/MadGraphInterface/interface/MadGraphSource.h"
00018 #include "SimDataFormats/GeneratorProducts/interface/LHECommonBlocks.h"
00019 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
00020 #include "FWCore/Framework/interface/Event.h"
00021 #include "FWCore/ServiceRegistry/interface/Service.h"
00022 #include "GeneratorInterface/MadGraphInterface/interface/PYR.h"
00023 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00025
00026 #include <iostream>
00027 #include <fstream>
00028 #include <cstring>
00029 #include <cstdio>
00030 #include "time.h"
00031
00032
00033 #include "HepMC/PythiaWrapper6_2.h"
00034
00035 #include "HepMC/IO_HEPEVT.h"
00036
00037
00038
00039 #include "GeneratorInterface/MadGraphInterface/interface/MCDBInterface.h"
00040
00041 #define PYGIVE pygive_
00042 extern "C" {
00043 void PYGIVE(const char*,int length);
00044 }
00045
00046 extern "C"{
00047 void eventtree_();
00048 }
00049
00050 #define TXGIVE txgive_
00051 extern "C" {
00052 void TXGIVE(const char*,int length);
00053 }
00054
00055 #define TXGIVE_INIT txgive_init_
00056 extern "C" {
00057 void TXGIVE_INIT();
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 extern "C" {
00069 extern struct MEMAIN{
00070 double etcjet;
00071 double rclmax;
00072 double etaclmax;
00073 double qcut;
00074 double clfact;
00075 int maxjets;
00076 int minjets;
00077 int iexcfile;
00078 int ktsche;
00079 int nexcres;
00080 int excres[30];
00081 }memain_;
00082 }
00083
00084
00085
00086
00087
00088 extern "C" {
00089 extern struct SOURCEPRS {
00090 int minimalLH;
00091 int externalLH;
00092 int validLH;
00093 } sourceprs_;
00094 }
00095
00096
00097 extern "C" {
00098 void mgopen_(const char *fname, int len);
00099 void mgclos_();
00100 }
00101
00102
00103 static const unsigned long kNanoSecPerSec = 1000000000;
00104 static const unsigned long kAveEventPerSec = 200;
00105
00106 using namespace edm;
00107 using namespace std;
00108
00109 MadGraphSource::MadGraphSource( const ParameterSet & pset, InputSourceDescription const& desc)
00110 : ExternalInputSource (pset, desc, false), evt(0),
00111 pythiaPylistVerbosity_ (pset.getUntrackedParameter<int>("pythiaPylistVerbosity",0)),
00112 pythiaHepMCVerbosity_ (pset.getUntrackedParameter<bool>("pythiaHepMCVerbosity",false)),
00113 maxEventsToPrint_ (pset.getUntrackedParameter<int>("maxEventsToPrint",0)),
00114 MGfile_ (fileNames()[0]),
00115 getInputFromMCDB_ (pset.getUntrackedParameter<bool>("getInputFromMCDB",false)),
00116 MCDBArticleID_ (pset.getParameter<int>("MCDBArticleID")),
00117 firstEvent_(pset.getUntrackedParameter<unsigned int>("firstEvent", 0)),
00118 lhe_event_counter_(0),MEMAIN_etaclmax(pset.getUntrackedParameter<double>("MEMAIN_etaclmax",0.)),
00119 MEMAIN_qcut(pset.getUntrackedParameter<double>("MEMAIN_qcut",0.)),
00120 MEMAIN_iexcfile(pset.getUntrackedParameter<unsigned int>("MEMAIN_iexcfile",0)),
00121 MEMAIN_maxjets(pset.getUntrackedParameter<int>("MEMAIN_maxjets",-1)),
00122 produceEventTreeFile_ (pset.getUntrackedParameter<bool>("produceEventTreeFile",false)),
00123 minimalLH_(pset.getUntrackedParameter<bool>("minimalLH",false)),
00124 useExternalGenerators_(false),
00125 useTauola_(false),
00126 useTauolaPolarization_(false)
00127 {
00128
00129 edm::LogInfo("Generator|MadGraph")<<" initializing MadGraphSource";
00130 pdf_info = new HepMC::PdfInfo();
00131
00132 std::ifstream file;
00133 std::ofstream ofile;
00134
00135
00136 if (getInputFromMCDB_) mcdbGetInputFile(MGfile_, MCDBArticleID_);
00137
00138
00139 if ( MGfile_.find("file:") || MGfile_.find("rfio:")){
00140 MGfile_.erase(0,5);
00141 }
00142
00143 file.open(MGfile_.c_str(),std::ios::in);
00144 if(!file){
00145 edm::LogError("GeneratorError|OpenMadGraphFileError")<< "Error: Cannot open MadGraph input file";
00146 throw edm::Exception(edm::errors::Configuration,"OpenMadGraphFileError")
00147 <<" Cannot open MadGraph input file, check file name and path.";
00148 }else{
00149 edm::LogInfo("Generator|MadGraph")<<"Opened MadGraph file successfully!";
00150 file.close();
00151 }
00152
00153 edm::LogInfo("Generator|MadGraph")<< "MadGraph input file is " << MGfile_;
00154
00155 mgopen_(MGfile_.c_str(), MGfile_.size());
00156
00157
00158
00159
00160
00161
00162
00163
00164 sourceprs_.minimalLH = minimalLH_;
00165 if(minimalLH_) edm::LogInfo("Generator|MadGraph")<<" ----- Using minimal Les Houches Accord functionality - ignoring MadGraph specifics.";
00166
00167
00168 memain_.etcjet=0.;
00169 memain_.rclmax=0.;
00170 memain_.etaclmax=0.;
00171 memain_.qcut=0.;
00172 memain_.clfact=0.;
00173 memain_.maxjets=0;
00174 memain_.minjets=0;
00175 memain_.iexcfile=0;
00176 memain_.ktsche=0;
00177
00178 memain_.iexcfile=MEMAIN_iexcfile;
00179 memain_.etaclmax=MEMAIN_etaclmax;
00180 memain_.qcut=MEMAIN_qcut;
00181
00182 edm::LogInfo("Generator|MadGraph")<<"MEMAIN before ME2pythia initialization - etcjet ="<<memain_.etcjet<<" rclmax ="<<memain_.rclmax<<" etaclmax ="<<memain_.etaclmax<<" qcut ="<<memain_.qcut<<" clfact ="<<memain_.clfact<<" maxjets ="<<memain_.maxjets<<" minjets ="<<memain_.minjets<<" iexcfile ="<<memain_.iexcfile<<" ktsche ="<<memain_.ktsche;
00183
00184 edm::LogInfo("Generator|MadGraph")<<"MadGraphSource: initializing Pythia.";
00185
00186
00187 edm::LogInfo("Generator|MadGraph")<<"Pythia PYLIST verbosity level = " << pythiaPylistVerbosity_;
00188
00189 edm::LogInfo("Generator|MadGraph")<<"Pythia HepMC verbosity = " << pythiaHepMCVerbosity_;
00190
00191 edm::LogInfo("Generator|MadGraph")<<"Number of events to be printed = " << maxEventsToPrint_;
00192
00193 edm::LogInfo("Generator|MadGraph")<<"firstEvent / maxEvents = "<<firstEvent_<<" / "<< maxEvents();
00194
00195
00196 ParameterSet pythia_params = pset.getParameter<ParameterSet>("PythiaParameters") ;
00197
00198
00199 std::vector<std::string> setNames = pythia_params.getParameter<std::vector<std::string> >("parameterSets");
00200
00201
00202 for ( unsigned i=0; i<setNames.size(); ++i ) {
00203 std::string mySet = setNames[i];
00204
00205
00206 std::vector<std::string> pars =
00207 pythia_params.getParameter<std::vector<std::string> >(mySet);
00208
00209 edm::LogInfo("Generator|MadGraph")<<"----------------------------------------------";
00210 edm::LogInfo("Generator|MadGraph")<<"Read PYTHIA parameter set " << mySet;
00211 edm::LogInfo("Generator|MadGraph")<<"----------------------------------------------";
00212
00213
00214 for( std::vector<std::string>::const_iterator
00215 itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
00216 static std::string sRandomValueSetting("MRPY(1)");
00217 if( 0 == itPar->compare(0,sRandomValueSetting.size(),sRandomValueSetting) ) {
00218 throw edm::Exception(edm::errors::Configuration,"PythiaError")
00219 <<" attempted to set random number using pythia command 'MRPY(1)' this is not allowed.\n Please use the RandomNumberGeneratorService to set the random number seed.";
00220 }
00221 if( ! call_pygive(*itPar) ) {
00222 throw edm::Exception(edm::errors::Configuration,"PythiaError")
00223 <<" pythia did not accept the following \""<<*itPar<<"\"";
00224 }
00225 }
00226 }
00227
00228
00229 edm::LogInfo("Generator|MadGraph")<<"----------------------------------------------";
00230 edm::LogInfo("Generator|MadGraph")<<"Setting Pythia random number seed";
00231 edm::LogInfo("Generator|MadGraph")<<"----------------------------------------------";
00232 edm::Service<RandomNumberGenerator> rng;
00233 randomEngine = &(rng->getEngine());
00234 uint32_t seed = rng->mySeed();
00235 std::ostringstream sRandomSet;
00236 sRandomSet <<"MRPY(1)="<<seed;
00237 call_pygive(sRandomSet.str());
00238
00239 call_pyinit( "USER", "", "", 0.);
00240
00241
00242 if( (MEMAIN_maxjets>0) && (MEMAIN_maxjets > memain_.maxjets)){
00243 edm::LogWarning("Generator|MadGraph")<<"Enforcing highest multiplicity through CMSSW configuration to be maxjets="<<MEMAIN_maxjets<<" instead of the one calculated from the ME2pythia which is maxjets="<<memain_.maxjets;
00244 memain_.maxjets=MEMAIN_maxjets;
00245 }
00246
00247
00248
00249
00250 useExternalGenerators_ = pset.getUntrackedParameter<bool>("UseExternalGenerators",false);
00251
00252
00253
00254 if ( useExternalGenerators_ ) {
00255
00256 ParameterSet ext_gen_params =
00257 pset.getParameter<ParameterSet>("ExternalGenerators") ;
00258 vector<string> extGenNames =
00259 ext_gen_params.getParameter< vector<string> >("parameterSets");
00260 for (unsigned int ip=0; ip<extGenNames.size(); ++ip )
00261 {
00262 string curSet = extGenNames[ip];
00263 ParameterSet gen_par_set =
00264 ext_gen_params.getUntrackedParameter< ParameterSet >(curSet);
00265
00266
00267
00268
00269
00270 if ( curSet == "Tauola" )
00271 {
00272 useTauola_ = true;
00273 if ( useTauola_ ) {
00274 cout << "--> use TAUOLA" << endl;
00275 }
00276 useTauolaPolarization_ = gen_par_set.getParameter<bool>("UseTauolaPolarization");
00277 if ( useTauolaPolarization_ )
00278 {
00279 cout << "(Polarization effects enabled)" << endl;
00280 tauola_.enablePolarizationEffects();
00281 }
00282 else
00283 {
00284 cout << "(Polarization effects disabled)" << endl;
00285 tauola_.disablePolarizationEffects();
00286 }
00287 vector<string> cards = gen_par_set.getParameter< vector<string> >("InputCards");
00288 cout << "----------------------------------------------" << endl;
00289 cout << "Initializing Tauola" << endl;
00290 for( vector<string>::const_iterator
00291 itPar = cards.begin(); itPar != cards.end(); ++itPar )
00292 {
00293
00294 TXGIVE( (*itPar).c_str(), (*itPar).length() );
00295 cout << " " << (*itPar).c_str() << endl;
00296 }
00297 tauola_.initialize();
00298
00299 }
00300 }
00301
00302 }
00303
00304
00305 edm::LogInfo("Generator|MadGraph")<<"MEMAIN after ME2pythia initialization - etcjet ="<<memain_.etcjet<<" rclmax ="<<memain_.rclmax<<" etaclmax ="<<memain_.etaclmax<<" qcut ="<<memain_.qcut<<" clfact ="<<memain_.clfact<<" maxjets ="<<memain_.maxjets<<" minjets ="<<memain_.minjets<<" iexcfile ="<<memain_.iexcfile<<" ktsche ="<<memain_.ktsche;
00306
00307 produces<HepMCProduct>();
00308 edm::LogInfo("Generator|MadGraph")<<"starting event generation ...";
00309 }
00310
00311
00312 MadGraphSource::~MadGraphSource(){
00313 edm::LogInfo("Generator|MadGraph")<<"event generation done.";
00314 delete pdf_info;
00315 clear();
00316 mgclos_();
00317
00318 }
00319
00320 void MadGraphSource::clear() {
00321 }
00322
00323 void MadGraphSource::endRun(Run & r) {
00324
00325 call_pystat(1);
00326 if ( useTauola_ ) {
00327 tauola_.print();
00328
00329 }
00330
00331 }
00332
00333
00334 bool MadGraphSource::produce(Event & e) {
00335 std::auto_ptr<HepMCProduct> bare_product(new HepMCProduct());
00336
00337
00338 for(;lhe_event_counter_<firstEvent_; ++lhe_event_counter_){
00339 call_pyevnt();
00340 call_pyhepc( 1 );
00341 edm::LogWarning("Generator|MadGraph")<<"skipping LHE event "<<lhe_event_counter_;
00342 if(hepeup_.nup==0)
00343 {
00344 edm::LogInfo("Generator|MadGraph")<<
00345 "The interface signalled end of Les Houches file. Apparently, firstEvent setting is greater than the number of LHE events. Finishing event processing here.";
00346 return false;
00347 }
00348 }
00349
00350 call_pyevnt();
00351
00352 if(hepeup_.nup==0){
00353 edm::LogInfo("Generator|MadGraph")<<"The interface signalled end of Les Houches file. Finishing event processing here.";
00354 return false;
00355 }
00356
00357 if ( useTauola_ ) {
00358 tauola_.processEvent();
00359
00360 }
00361
00362 call_pyhepc( 1 );
00363
00364 if(produceEventTreeFile_) eventtree_();
00365
00366 HepMC::IO_HEPEVT conv;
00367 HepMC::GenEvent* evt = conv.read_next_event();
00368 evt->set_signal_process_id(pypars.msti[0]);
00369 evt->set_event_number(numberEventsInRun() - remainingEvents() - 1);
00370
00371
00372 int id_1 = pypars.msti[14];
00373 int id_2 = pypars.msti[15];
00374 if (id_1 == 21) id_1 = 0;
00375 if (id_2 == 21) id_2 = 0;
00376 pdf_info->set_id1(id_1);
00377 pdf_info->set_id2(id_2);
00378 pdf_info->set_x1(pypars.pari[32]);
00379 pdf_info->set_x2(pypars.pari[33]);
00380 pdf_info->set_scalePDF(pypars.pari[20]);
00381 pdf_info->set_pdf1(pypars.pari[28]);
00382 pdf_info->set_pdf2(pypars.pari[29]);
00383 evt->set_pdf_info( *pdf_info);
00384
00385
00386 if(event() <= maxEventsToPrint_ && (pythiaPylistVerbosity_ || pythiaHepMCVerbosity_)) {
00387
00388 if(pythiaPylistVerbosity_) {
00389 call_pylist(pythiaPylistVerbosity_);
00390 }
00391
00392 if(pythiaHepMCVerbosity_) {
00393 edm::LogInfo("Generator|MadGraph")<<"Event process = " << pypars.msti[0];
00394 evt->print();
00395 }
00396 }
00397
00398
00399
00400
00401
00402
00403
00404 if(evt) bare_product->addHepMCData(evt );
00405 e.put(bare_product);
00406 return true;
00407 }
00408
00409 bool MadGraphSource::call_pygive(const std::string& iParm ) {
00410 int numWarn = pydat1.mstu[26];
00411 int numErr = pydat1.mstu[22];
00412
00413 PYGIVE( iParm.c_str(), iParm.length() );
00414
00415
00416 return pydat1.mstu[26] == numWarn && pydat1.mstu[22] == numErr;
00417 }
00418