CMS 3D CMS Logo

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