CMS 3D CMS Logo

AlpgenSource.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <sstream>
4 #include <fstream>
5 #include <string>
6 #include <memory>
7 #include <cmath>
8 
9 #include <boost/algorithm/string/classification.hpp>
10 #include <boost/algorithm/string/split.hpp>
11 
20 
25 
28 
30 public:
33 
35  ~AlpgenSource() override;
36 
37 private:
39  void produce(edm::Event &event) override;
40  void beginRun(edm::Run &run) override;
41 
43  template <typename T>
46  template <typename T>
47  T getParameter(AlpgenHeader::Parameter index, const T &defValue) const;
48 
52 
56  unsigned int processID() const;
57 
59  bool readAlpgenEvent(lhef::HEPEUP &hepeup);
60 
63 
65  std::unique_ptr<std::ifstream> inputFile_;
66 
68  unsigned long skipEvents_;
69 
71  unsigned long nEvent_;
72 
77 
78  std::unique_ptr<lhef::HEPEUP> hepeup_;
79 
82 
85 
90 };
91 
93  : edm::ProducerSourceFromFiles(params, desc, false),
94  skipEvents_(params.getUntrackedParameter<unsigned int>("skipEvents", 0)),
95  nEvent_(0),
96  lheAlpgenUnwParHeader("AlpgenUnwParFile"),
97  extraHeaderFileName_(params.getUntrackedParameter<std::string>("extraHeaderFileName", "")),
98  extraHeaderName_(params.getUntrackedParameter<std::string>("extraHeaderName", "")),
99  writeAlpgenWgtFile(params.getUntrackedParameter<bool>("writeAlpgenWgtFile", true)),
100  writeAlpgenParFile(params.getUntrackedParameter<bool>("writeAlpgenParFile", true)),
101  writeExtraHeader(params.getUntrackedParameter<bool>("writeExtraHeader", false)) {
102  std::vector<std::string> allFileNames = fileNames();
103 
104  // Only one filename
105  if (allFileNames.size() != 1)
106  throw cms::Exception("Generator|AlpgenInterface") << "AlpgenSource needs exactly one file specified "
107  "for now."
108  << std::endl;
109 
110  fileName_ = allFileNames[0];
111 
112  // Strip the "file:" prefix
113  if (fileName_.find("file:") != 0)
114  throw cms::Exception("Generator|AlpgenInterface") << "AlpgenSource only supports the file: scheme "
115  "for now."
116  << std::endl;
117  fileName_.erase(0, 5);
118 
119  // Open the _unw.par file to store additional
120  // informations in the LHERunInfoProduct
121  std::ifstream reader((fileName_ + "_unw.par").c_str());
122  if (!reader.good())
123  throw cms::Exception("Generator|AlpgenInterface")
124  << "AlpgenSource was unable to open the file \"" << fileName_ << "_unw.par\"." << std::endl;
125 
126  // A full copy of the _unw.par file in the LHE header.
127  char buffer[256];
128  while (reader.getline(buffer, sizeof buffer))
129  lheAlpgenUnwParHeader.addLine(std::string(buffer) + "\n");
130 
131  // Parse that header to setup an Alpgen header,
132  // which will be used in the production itself.
134  throw cms::Exception("Generator|AlpgenInterface") << "AlpgenSource was unable to parse the Alpgen "
135  << "unweighted parameter file." << std::endl;
136 
137  // Declare the products.
138  produces<LHERunInfoProduct, edm::Transition::BeginRun>();
139  produces<LHEEventProduct>();
140 }
141 
143 
145  std::ostringstream ss;
146  ss << std::setw(9) << pdgId << " " << std::scientific << std::setprecision(9) << header.masses[mass] << " # "
147  << comment << std::endl;
148  return ss.str();
149 }
150 
152  // At this point, the lheUnwParHeader has the full contents of the _unw.par
153  // file. So we can get the HEPRUP information from the LHE header itself.
154  lhef::HEPRUP heprup;
155 
156  // Get basic run information.
157  // Beam identity.
158  heprup.IDBMUP.first = 2212;
159  switch (getParameter<int>(AlpgenHeader::ih2)) {
160  case 1:
161  heprup.IDBMUP.second = 2212;
162  break;
163  case -1:
164  heprup.IDBMUP.second = -2212;
165  break;
166  default:
167  throw cms::Exception("Generator|AlpgenInterface") << "AlpgenSource was unable to understand the ih2 "
168  << "parameter." << std::endl;
169  }
170 
171  // Beam energy.
172  heprup.EBMUP.second = heprup.EBMUP.first = getParameter<double>(AlpgenHeader::ebeam);
173 
174  // PDF info. Initially, Alpgen doesn't fill it.
175  heprup.PDFGUP.first = -1;
176  heprup.PDFGUP.second = -1;
177  heprup.PDFSUP.first = -1;
178  heprup.PDFSUP.second = -1;
179 
180  // Unweighted events.
181  heprup.IDWTUP = 3;
182 
183  // Only one process.
184  heprup.resize(1);
185 
186  // Cross section and error.
187  heprup.XSECUP[0] = header.xsec;
188  heprup.XERRUP[0] = header.xsecErr;
189 
190  // Maximum weight.
191  heprup.XMAXUP[0] = header.xsec;
192 
193  // Process code for Pythia.
194  heprup.LPRUP[0] = processID();
195 
196  // Comments on top.
198  comments.addLine("\n");
199  comments.addLine("\tExtracted by AlpgenInterface\n");
200 
201  // Add SLHA header containing particle masses from Alpgen.
202  // Pythia6Hadronisation will feed the masses to Pythia automatically.
203  LHERunInfoProduct::Header slha("slha");
204  slha.addLine("\n# SLHA header containing masses from Alpgen\n");
205  slha.addLine("Block MASS # Mass spectrum (kinematic masses)\n");
206  slha.addLine("# PDG Mass\n");
207  slha.addLine(slhaMassLine(4, AlpgenHeader::mc, "charm pole mass"));
208  slha.addLine(slhaMassLine(5, AlpgenHeader::mb, "bottom pole mass"));
209  slha.addLine(slhaMassLine(6, AlpgenHeader::mt, "top pole mass"));
210  slha.addLine(slhaMassLine(23, AlpgenHeader::mz, "Z mass"));
211  slha.addLine(slhaMassLine(24, AlpgenHeader::mw, "W mass"));
212  slha.addLine(slhaMassLine(25, AlpgenHeader::mh, "H mass"));
213 
214  char buffer[512];
215 
216  // We also add the information on weighted events.
217  LHERunInfoProduct::Header lheAlpgenWgtHeader("AlpgenWgtFile");
218  if (writeAlpgenWgtFile) {
219  std::ifstream wgtascii((fileName_ + ".wgt").c_str());
220  while (wgtascii.getline(buffer, 512)) {
221  lheAlpgenWgtHeader.addLine(std::string(buffer) + "\n");
222  }
223  }
224 
225  LHERunInfoProduct::Header lheAlpgenParHeader("AlpgenParFile");
226  if (writeAlpgenParFile) {
227  std::ifstream parascii((fileName_ + ".par").c_str());
228  while (parascii.getline(buffer, 512)) {
229  lheAlpgenParHeader.addLine(std::string(buffer) + "\n");
230  }
231  }
232 
233  // If requested by the user, we also add any specific header provided.
234  // Nota bene: the header is put in the LHERunInfoProduct AS IT WAS GIVEN.
235  // That means NO CROSS-CHECKS WHATSOEVER. Use with care.
237  if (writeExtraHeader) {
238  std::ifstream extraascii(extraHeaderFileName_.c_str());
239  while (extraascii.getline(buffer, 512)) {
240  extraHeader.addLine(std::string(buffer) + "\n");
241  }
242  }
243 
244  // Build the final Run info object. Backwards-compatible order.
245  std::unique_ptr<LHERunInfoProduct> runInfo(new LHERunInfoProduct(heprup));
246  runInfo->addHeader(comments);
247  runInfo->addHeader(lheAlpgenUnwParHeader);
248  if (writeAlpgenWgtFile)
249  runInfo->addHeader(lheAlpgenWgtHeader);
250  if (writeAlpgenParFile)
251  runInfo->addHeader(lheAlpgenParHeader);
252  runInfo->addHeader(slha);
253  if (writeExtraHeader)
254  runInfo->addHeader(extraHeader);
255  run.put(std::move(runInfo));
256 
257  // Open the .unw file in the heap, and set the global pointer to it.
258  inputFile_.reset(new std::ifstream((fileName_ + ".unw").c_str()));
259  if (!inputFile_->good())
260  throw cms::Exception("Generator|AlpgenInterface")
261  << "AlpgenSource was unable to open the file \"" << fileName_ << ".unw\"." << std::endl;
262 }
263 
264 template <typename T>
266  std::map<AlpgenHeader::Parameter, double>::const_iterator pos = header.params.find(index);
267  if (pos == header.params.end())
268  throw cms::Exception("Generator|AlpgenInterface")
269  << "Requested Alpgen parameter \"" << AlpgenHeader::parameterName(index)
270  << "\" "
271  "not found in Alpgen parameter file."
272  << std::endl;
273 
274  return T(pos->second);
275 }
276 
277 template <typename T>
279  std::map<AlpgenHeader::Parameter, double>::const_iterator pos = header.params.find(index);
280  if (pos == header.params.end())
281  return defValue;
282  else
283  return T(pos->second);
284 }
285 
286 unsigned int AlpgenSource::processID() const {
287  // return 661; // The original, old thing.
288  // digits #XYZ: X = ihrd, Y = ihvy, Z = njets
289  return header.ihrd * 100 + getParameter<unsigned int>(AlpgenHeader::ihvy, 0) * 10 +
290  getParameter<unsigned int>(AlpgenHeader::njets, 0);
291 }
292 
294  char buffer[512];
295  double dummy;
296  int nPart;
297  double sWgtRes;
298  double sQ;
299 
300  inputFile_->getline(buffer, sizeof buffer);
301  if (!inputFile_->good())
302  return false;
303 
304  std::istringstream ls(buffer);
305 
306  // Event number and process don't matter (or do they?)
307  ls >> dummy >> dummy;
308 
309  // Number of particles in the record, sample's average weight and Q scale
310  ls >> nPart >> sWgtRes >> sQ;
311 
312  if (ls.bad() || nPart < 1 || nPart > 1000)
313  return false;
314 
315  // Make room for the particles listed in the Alpgen file
316  hepeup.resize(nPart);
317 
318  // Scales, weights and process ID.
319  hepeup.SCALUP = sQ;
320  hepeup.XWGTUP = sWgtRes;
321  hepeup.IDPRUP = processID();
322 
323  // Incoming lines
324  for (int i = 0; i != 2; ++i) {
325  inputFile_->getline(buffer, sizeof buffer);
326  std::istringstream ls(buffer);
327  int flavour;
328  ls >> flavour;
329  int colour1;
330  ls >> colour1;
331  int colour2;
332  ls >> colour2;
333  double zMomentum;
334  ls >> zMomentum;
335 
336  if (inputFile_->bad())
337  return false;
338 
339  // Setting the HEPEUP of the incoming lines.
340  hepeup.IDUP[i] = flavour;
341  hepeup.ISTUP[i] = -1;
342  hepeup.MOTHUP[i].first = 0;
343  hepeup.MOTHUP[i].second = 0;
344  hepeup.PUP[i][0] = 0.;
345  hepeup.PUP[i][1] = 0.;
346  hepeup.PUP[i][2] = zMomentum;
347  hepeup.PUP[i][3] = std::fabs(zMomentum);
348  hepeup.PUP[i][4] = 0.;
349  if (colour1)
350  colour1 += 500;
351  if (colour2)
352  colour2 += 500;
353  hepeup.ICOLUP[i].first = colour1;
354  hepeup.ICOLUP[i].second = colour2;
355  }
356 
357  // Outgoing lines
358  for (int i = 2; i != nPart; ++i) {
359  inputFile_->getline(buffer, sizeof buffer);
360  std::istringstream ls(buffer);
361  int flavour;
362  ls >> flavour;
363  int colour1;
364  ls >> colour1;
365  int colour2;
366  ls >> colour2;
367  double px, py, pz, mass;
368  ls >> px >> py >> pz >> mass;
369  double energy = std::sqrt(px * px + py * py + pz * pz + mass * mass);
370 
371  if (inputFile_->bad())
372  return false;
373 
374  // Setting the HEPEUP of the outgoing lines.
375  hepeup.IDUP[i] = flavour;
376  hepeup.ISTUP[i] = 1;
377  hepeup.MOTHUP[i].first = 1;
378  hepeup.MOTHUP[i].second = 2;
379  hepeup.PUP[i][0] = px;
380  hepeup.PUP[i][1] = py;
381  hepeup.PUP[i][2] = pz;
382  hepeup.PUP[i][3] = energy;
383  hepeup.PUP[i][4] = mass;
384  if (colour1)
385  colour1 += 500;
386  if (colour2)
387  colour2 += 500;
388  hepeup.ICOLUP[i].first = colour1;
389  hepeup.ICOLUP[i].second = colour2;
390  }
391 
392  return true;
393 }
394 
396  // The LHE Event Record
397  hepeup_.reset(new lhef::HEPEUP);
398 
399  lhef::HEPEUP &hepeup = *hepeup_;
400 
401  // Read the .unw file until it is over.
402  for (;;) {
403  if (!readAlpgenEvent(hepeup)) {
404  if (inputFile_->eof())
405  return false;
406 
407  throw cms::Exception("Generator|AlpgenInterface")
408  << "AlpgenSource is not able read event no. " << nEvent_ << std::endl;
409  }
410 
411  nEvent_++;
412  if (skipEvents_ > 0)
413  skipEvents_--;
414  else
415  break;
416  }
417  return true;
418 }
419 
421  // Here are the Alpgen routines for filling up the rest
422  // of the LHE Event Record. The .unw file has the information
423  // in a compressed way, e.g. it doesn't list the W boson -
424  // one has to reconstruct it from the e nu pair.
425  lhef::HEPEUP &hepeup = *hepeup_;
426 
427  switch (header.ihrd) {
428  case 1:
429  case 2:
430  case 3:
431  case 4:
432  case 10:
433  case 14:
434  case 15:
435  alpgen::fixEventWZ(hepeup);
436  break;
437  case 5:
439  break;
440  case 6:
441  alpgen::fixEventTTbar(hepeup);
442  break;
443  case 8:
445  break;
446  case 13:
448  break;
449  case 7:
450  case 9:
451  case 11:
452  case 12:
453  case 16:
454  // No fixes needed.
455  break;
456 
457  default:
458  throw cms::Exception("Generator|AlpgenInterface") << "Unrecognized IHRD process code" << std::endl;
459  }
460 
461  // Create the LHEEventProduct and put it into the Event.
462  std::unique_ptr<LHEEventProduct> lheEvent(new LHEEventProduct(hepeup));
463  event.put(std::move(lheEvent));
464 
465  hepeup_.reset();
466 }
467 
void resize(int nrup)
Definition: LesHouches.h:44
std::map< Parameter, double > params
Definition: AlpgenHeader.h:65
AlpgenSource(const edm::ParameterSet &params, const edm::InputSourceDescription &desc)
Constructor.
Definition: AlpgenSource.cc:92
bool parse(const std::vector< std::string >::const_iterator &begin, const std::vector< std::string >::const_iterator &end)
Definition: AlpgenHeader.cc:62
std::vector< std::pair< int, int > > ICOLUP
Definition: LesHouches.h:240
std::pair< double, double > EBMUP
Definition: LesHouches.h:82
AlpgenHeader header
Alpgen _unw.par file as an AlpgenHeader.
Definition: AlpgenSource.cc:76
std::pair< int, int > IDBMUP
Definition: LesHouches.h:77
reader
Definition: DQM.py:105
std::unique_ptr< std::ifstream > inputFile_
Pointer to the input file.
Definition: AlpgenSource.cc:65
static std::string parameterName(Parameter index)
const_iterator end() const
bool writeAlpgenParFile
Definition: AlpgenSource.cc:88
bool writeExtraHeader
Definition: AlpgenSource.cc:89
ProducerSourceFromFiles(ParameterSet const &pset, InputSourceDescription const &desc, bool realData)
#define DEFINE_FWK_INPUT_SOURCE(type)
bool setRunAndEventInfo(edm::EventID &, edm::TimeValue_t &, edm::EventAuxiliary::ExperimentType &) override
unsigned long nEvent_
Number of events.
Definition: AlpgenSource.cc:71
unsigned long skipEvents_
Number of events to skip.
Definition: AlpgenSource.cc:68
std::vector< std::string > const & fileNames() const
Definition: FromFiles.h:22
std::pair< int, int > PDFGUP
Definition: LesHouches.h:88
bool writeAlpgenWgtFile
configuration flags
Definition: AlpgenSource.cc:87
std::unique_ptr< lhef::HEPEUP > hepeup_
Definition: AlpgenSource.cc:78
void fixEventWZ(lhef::HEPEUP &hepeup)
Fixes Event Record for ihrd = 1,2,3,4,10,14,15.
LHERunInfoProduct::Header lheAlpgenUnwParHeader
Alpgen _unw.par file as a LHE header.
Definition: AlpgenSource.cc:74
void fixEventMultiBoson(lhef::HEPEUP &hepeup)
Fixes Event Record for ihrd = 5.
void fixEventHiggsTTbar(lhef::HEPEUP &hepeup)
Fixes Event Record for ihrd = 8.
void resize(int nup)
Definition: LesHouches.h:161
std::string extraHeaderFileName_
Name of the extra header file.
Definition: AlpgenSource.cc:81
T sqrt(T t)
Definition: SSEVec.h:19
std::vector< FiveVector > PUP
Definition: LesHouches.h:246
void addLine(const std::string &line)
std::vector< int > ISTUP
Definition: LesHouches.h:228
~AlpgenSource() override
Destructor.
std::vector< std::pair< int, int > > MOTHUP
Definition: LesHouches.h:234
unsigned long long TimeValue_t
Definition: Timestamp.h:28
void produce(edm::Event &event) override
std::vector< int > IDUP
Definition: LesHouches.h:223
std::vector< double > XERRUP
Definition: LesHouches.h:118
std::string fileName_
Name of the input file.
Definition: AlpgenSource.cc:62
std::vector< double > XMAXUP
Definition: LesHouches.h:123
def ls(path, rec=False)
Definition: eostools.py:349
TString nPart(Int_t part, TString string, TString delimit=";", Bool_t removerest=true)
std::string slhaMassLine(int pdgId, AlpgenHeader::Masses mass, const std::string &comment) const
std::string extraHeaderName_
Name given to the extra header.
Definition: AlpgenSource.cc:84
void fixEventSingleTop(lhef::HEPEUP &hepeup, double mb, int itopprc)
Fixes Event Record for ihrd = 13.
void beginRun(edm::Run &run) override
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:108
std::pair< int, int > PDFSUP
Definition: LesHouches.h:94
unsigned int ihrd
Definition: AlpgenHeader.h:66
HLT enums.
T getParameter(AlpgenHeader::Parameter index) const
Function to get parameter by name from AlpgenHeader.
double xsecErr
Definition: AlpgenHeader.h:68
void fixEventTTbar(lhef::HEPEUP &hepeup)
Fixes Event Record for ihrd = 6.
std::vector< double > XSECUP
Definition: LesHouches.h:112
long double T
double XWGTUP
Definition: LesHouches.h:194
double masses[MASS_MAX]
Definition: AlpgenHeader.h:71
bool readAlpgenEvent(lhef::HEPEUP &hepeup)
Read an event and put it into the HEPEUP.
def move(src, dest)
Definition: eostools.py:511
const_iterator begin() const
Definition: event.py:1
Definition: Run.h:45
std::vector< int > LPRUP
Definition: LesHouches.h:128
double SCALUP
Definition: LesHouches.h:208
unsigned int processID() const
#define comment(par)
Definition: vmac.h:163