00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00032
00033 #include "HepMC/PythiaWrapper6_2.h"
00034 #include "HepMC/IO_HEPEVT.h"
00035
00036
00037
00038
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
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
00057 fileNames_ (pset.getUntrackedParameter<std::vector<std::string> >("fileNames")),
00058 eventsRead_(0),
00059 lheAlpgenUnwParHeader("AlpgenUnwParFile")
00060
00061 {
00062
00063 fileName_ = fileNames_[0];
00064
00065 if ( fileName_.find("file:") || fileName_.find("rfio:")){
00066 fileName_.erase(0,5);
00067 }
00068
00069
00070
00071
00072
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
00085 #ifdef NEVER
00086
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_)
00093 Nev_ = maxEvents();
00094 #endif
00095
00096
00097
00098
00099 pythiaPylistVerbosity_ = pset.getUntrackedParameter<int>("pythiaPylistVerbosity",0);
00100
00101
00102 pythiaHepMCVerbosity_ = pset.getUntrackedParameter<bool>("pythiaHepMCVerbosity",false);
00103
00104
00105 maxEventsToPrint_ = pset.getUntrackedParameter<int>("maxEventsToPrint",0);
00106
00107
00108 {
00109 ParameterSet pythia_params =
00110 pset.getParameter<ParameterSet>("PythiaParameters") ;
00111
00112
00113 vector<string> pars =
00114 pythia_params.getParameter<vector<string> >("pythia");
00115
00116
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
00132
00133
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
00144 string tmpstring = "UNWFILE = " + fileName_;
00145 call_txgive(tmpstring);
00146 }
00147
00148
00149
00150
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
00159 call_pyinit( "USER", "p", "p", 14000. );
00160
00161 cout << endl;
00162
00163
00164 produces<HepMCProduct>();
00165 produces<LHEEventProduct>();
00166
00167
00168 produces<LHERunInfoProduct, edm::InRun>();
00169 }
00170
00171
00172 AlpgenProducer::~AlpgenProducer(){
00173 call_pystat(1);
00174
00175 alpgen_end();
00176 clear();
00177 }
00178
00179 void AlpgenProducer::clear() {
00180
00181 }
00182
00183 void AlpgenProducer::beginRun(Run & r) {
00184
00185 lhef::HEPRUP heprup;
00186 lhef::CommonBlocks::readHEPRUP(&heprup);
00187 auto_ptr<LHERunInfoProduct> runInfo(new LHERunInfoProduct(heprup));
00188
00189
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
00199 LHERunInfoProduct::Header comments;
00200 comments.addLine("\n");
00201 comments.addLine("\tExtracted by AlpgenInterface\n");
00202
00203
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
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
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 call_pyevnt();
00238
00239
00240
00241 lhef::HEPEUP hepeup;
00242 lhef::CommonBlocks::readHEPEUP(&hepeup);
00243 hepeup.AQEDUP = hepeup.AQCDUP = -1.0;
00244 for(int i = 0; i < hepeup.NUP; i++)
00245 hepeup.SPINUP[i] = -9;
00246 auto_ptr<LHEEventProduct> lheEvent(new LHEEventProduct(hepeup));
00247
00248
00249
00250
00251
00252
00253
00254
00255
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
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
00287
00288 if(e.id().event() <= maxEventsToPrint_ &&
00289 (pythiaPylistVerbosity_ || pythiaHepMCVerbosity_)) {
00290
00291
00292 if(pythiaPylistVerbosity_) {
00293 call_pylist(pythiaPylistVerbosity_);
00294 }
00295
00296
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
00307
00308
00309
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];
00324 int numErr = pydat1.mstu[22];
00325
00326
00327 PYGIVE( iParm.c_str(), iParm.length() );
00328
00329
00330 return pydat1.mstu[26] == numWarn && pydat1.mstu[22] == numErr;
00331 }
00332
00333 bool
00334 AlpgenProducer::call_txgive(const std::string& iParm )
00335 {
00336
00337 TXGIVE( iParm.c_str(), iParm.length() );
00338 return 1;
00339 }