CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
ExternalLHEProducer Class Reference

#include <Example/ExternalLHEProducer/src/ExternalLHEProducer.cc>

Inheritance diagram for ExternalLHEProducer:
edm::one::EDProducer< edm::BeginRunProducer, edm::EndRunProducer > edm::one::EDProducerBase edm::ProducerBase edm::EDConsumerBase edm::ProductRegistryHelper

Classes

class  FileCloseSentry
 

Public Member Functions

 ExternalLHEProducer (const edm::ParameterSet &iConfig)
 
 ~ExternalLHEProducer () override
 
- Public Member Functions inherited from edm::one::EDProducer< edm::BeginRunProducer, edm::EndRunProducer >
 EDProducer ()=default
 
SerialTaskQueueglobalLuminosityBlocksQueue () final
 
SerialTaskQueueglobalRunsQueue () final
 
bool hasAbilityToProduceInBeginLumis () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndRuns () const final
 
bool wantsGlobalLuminosityBlocks () const final
 
bool wantsGlobalRuns () const final
 
- Public Member Functions inherited from edm::one::EDProducerBase
 EDProducerBase ()
 
ModuleDescription const & moduleDescription () const
 
bool wantsStreamLuminosityBlocks () const
 
bool wantsStreamRuns () const
 
 ~EDProducerBase () override
 
- Public Member Functions inherited from edm::ProducerBase
void callWhenNewProductsRegistered (std::function< void(BranchDescription const &)> const &func)
 
std::vector< edm::ProductResolverIndex > const & indiciesForPutProducts (BranchType iBranchType) const
 
 ProducerBase ()
 
std::vector< edm::ProductResolverIndex > const & putTokenIndexToProductResolverIndex () const
 
void registerProducts (ProducerBase *, ProductRegistry *, ModuleDescription const &)
 
std::function< void(BranchDescription const &)> registrationCallback () const
 used by the fwk to register list of products More...
 
void resolvePutIndicies (BranchType iBranchType, ModuleToResolverIndicies const &iIndicies, std::string const &moduleLabel)
 
 ~ProducerBase () noexcept(false) override
 
- Public Member Functions inherited from edm::EDConsumerBase
std::vector< ConsumesInfoconsumesInfo () const
 
void convertCurrentProcessAlias (std::string const &processName)
 Convert "@currentProcess" in InputTag process names to the actual current process name. More...
 
 EDConsumerBase ()
 
 EDConsumerBase (EDConsumerBase const &)=delete
 
 EDConsumerBase (EDConsumerBase &&)=default
 
ESProxyIndex const * esGetTokenIndices (edm::Transition iTrans) const
 
ProductResolverIndexAndSkipBit indexFrom (EDGetToken, BranchType, TypeID const &) const
 
void itemsMayGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
void itemsToGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
std::vector< ProductResolverIndexAndSkipBit > const & itemsToGetFrom (BranchType iType) const
 
void labelsForToken (EDGetToken iToken, Labels &oLabels) const
 
void modulesWhoseProductsAreConsumed (std::vector< ModuleDescription const * > &modules, ProductRegistry const &preg, std::map< std::string, ModuleDescription const * > const &labelsToDesc, std::string const &processName) const
 
EDConsumerBase const & operator= (EDConsumerBase const &)=delete
 
EDConsumerBaseoperator= (EDConsumerBase &&)=default
 
bool registeredToConsume (ProductResolverIndex, bool, BranchType) const
 
bool registeredToConsumeMany (TypeID const &, BranchType) const
 
ProductResolverIndexAndSkipBit uncheckedIndexFrom (EDGetToken) const
 
void updateLookup (BranchType iBranchType, ProductResolverIndexHelper const &, bool iPrefetchMayGet)
 
void updateLookup (eventsetup::ESRecordsToProxyIndices const &)
 
virtual ~EDConsumerBase () noexcept(false)
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 
- Static Public Member Functions inherited from edm::one::EDProducerBase
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
static void prevalidate (ConfigurationDescriptions &descriptions)
 

Private Member Functions

void beginRunProduce (edm::Run &run, edm::EventSetup const &es) override
 
int closeDescriptors (int preserve)
 
void endRunProduce (edm::Run &, edm::EventSetup const &) override
 
void executeScript ()
 
void nextEvent ()
 
void preallocThreads (unsigned int) override
 
void produce (edm::Event &, const edm::EventSetup &) override
 
std::unique_ptr< std::string > readOutput ()
 

Private Attributes

std::vector< std::string > args_
 
uint32_t nEvents_
 
uint32_t npars_
 
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_ {}
 
unsigned int nThreads_ {1}
 
std::string outputContents_
 
std::string outputFile_
 
std::shared_ptr< lhef::LHEEventpartonLevel
 
std::unique_ptr< lhef::LHEReaderreader_
 
std::shared_ptr< lhef::LHERunInforunInfo
 
std::shared_ptr< lhef::LHERunInforunInfoLast
 
boost::ptr_deque< LHERunInfoProductrunInfoProducts
 
std::string scriptName_
 
bool storeXML_
 
bool wasMerged
 

Additional Inherited Members

- Public Types inherited from edm::one::EDProducerBase
typedef EDProducerBase ModuleType
 
- Public Types inherited from edm::ProducerBase
using ModuleToResolverIndicies = std::unordered_multimap< std::string, std::tuple< edm::TypeID const *, const char *, edm::ProductResolverIndex >>
 
typedef ProductRegistryHelper::TypeLabelList TypeLabelList
 
- Public Types inherited from edm::EDConsumerBase
typedef ProductLabels Labels
 
- Protected Member Functions inherited from edm::ProducerBase
ProducesCollector producesCollector ()
 
- Protected Member Functions inherited from edm::EDConsumerBase
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > consumes (edm::InputTag const &tag)
 
EDGetToken consumes (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken consumes (TypeToGet const &id, edm::InputTag const &tag)
 
ConsumesCollector consumesCollector ()
 Use a ConsumesCollector to gather consumes information from helper functions. More...
 
template<typename ProductType , BranchType B = InEvent>
void consumesMany ()
 
void consumesMany (const TypeToGet &id)
 
template<BranchType B>
void consumesMany (const TypeToGet &id)
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes ()
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes (ESInputTag const &tag)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > mayConsume (edm::InputTag const &tag)
 
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 

Detailed Description

Description: [one line class summary]

Implementation: [Notes on implementation]

Definition at line 66 of file ExternalLHEProducer.cc.

Constructor & Destructor Documentation

ExternalLHEProducer::ExternalLHEProducer ( const edm::ParameterSet iConfig)
explicit

Definition at line 119 of file ExternalLHEProducer.cc.

References args_, looper::cfg, Exception, edm::ParameterSet::exists(), edm::ParameterSet::getParameterSetVector(), np, npars_, nPartonMapping_, eventshapeDQM_cfi::order, and AlCaHLTBitMon_QueryRunRegistry::string.

120  : scriptName_((iConfig.getParameter<edm::FileInPath>("scriptName")).fullPath()),
121  outputFile_(iConfig.getParameter<std::string>("outputFile")),
122  args_(iConfig.getParameter<std::vector<std::string>>("args")),
123  npars_(iConfig.getParameter<uint32_t>("numberOfParameters")),
124  nEvents_(iConfig.getUntrackedParameter<uint32_t>("nEvents")),
125  storeXML_(iConfig.getUntrackedParameter<bool>("storeXML")) {
126  if (npars_ != args_.size())
127  throw cms::Exception("ExternalLHEProducer")
128  << "Problem with configuration: " << args_.size() << " script arguments given, expected " << npars_;
129 
130  if (iConfig.exists("nPartonMapping")) {
131  auto& processMap(iConfig.getParameterSetVector("nPartonMapping"));
132  for (auto& cfg : processMap) {
133  unsigned processId(cfg.getParameter<unsigned>("idprup"));
134 
135  auto orderStr(cfg.getParameter<std::string>("order"));
136  unsigned order(0);
137  if (orderStr == "LO")
138  order = 0;
139  else if (orderStr == "NLO")
140  order = 1;
141  else
142  throw cms::Exception("ExternalLHEProducer")
143  << "Invalid order specification for process " << processId << ": " << orderStr;
144 
145  unsigned np(cfg.getParameter<unsigned>("np"));
146 
147  nPartonMapping_.emplace(processId, std::make_pair(order, np));
148  }
149  }
150 
151  produces<LHEXMLStringProduct, edm::Transition::BeginRun>("LHEScriptOutput");
152 
153  produces<LHEEventProduct>();
154  produces<LHERunInfoProduct, edm::Transition::BeginRun>();
155  produces<LHERunInfoProduct, edm::Transition::EndRun>();
156 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
VParameterSet const & getParameterSetVector(std::string const &name) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< std::string > args_
int np
Definition: AMPTWrapper.h:43
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
ExternalLHEProducer::~ExternalLHEProducer ( )
override

Definition at line 158 of file ExternalLHEProducer.cc.

158 {}

Member Function Documentation

void ExternalLHEProducer::beginRunProduce ( edm::Run run,
edm::EventSetup const &  es 
)
overrideprivate

Definition at line 249 of file ExternalLHEProducer.cc.

References LHERunInfoProduct::addComment(), LHERunInfoProduct::addHeader(), args_, Exception, executeScript(), edm::Service< T >::isAvailable(), lhe2HepMCConverter_cff::LHERunInfoProduct, LogDebug, eostools::move(), edm::RandomNumberGenerator::mySeed(), nEvents_, nextEvent(), nThreads_, outputFile_, AlCaHLTBitMon_ParallelJobs::p, edm::Run::put(), reader_, runInfo, runInfoLast, runInfoProducts, optionsL1T::skip, storeXML_, and wasMerged.

249  {
250  // pass the number of events as previous to last argument
251 
252  std::ostringstream eventStream;
253  eventStream << nEvents_;
254  // args_.push_back(eventStream.str());
255  args_.insert(args_.begin() + 1, eventStream.str());
256 
257  // pass the random number generator seed as last argument
258 
260 
261  if (!rng.isAvailable()) {
262  throw cms::Exception("Configuration")
263  << "The ExternalLHEProducer module requires the RandomNumberGeneratorService\n"
264  "which is not present in the configuration file. You must add the service\n"
265  "in the configuration file if you want to run ExternalLHEProducer";
266  }
267  std::ostringstream randomStream;
268  randomStream << rng->mySeed();
269  // args_.push_back(randomStream.str());
270  args_.insert(args_.begin() + 2, randomStream.str());
271 
272  // args_.emplace_back(std::to_string(nThreads_));
273  args_.insert(args_.begin() + 3, std::to_string(nThreads_));
274 
275  for (unsigned int iArg = 0; iArg < args_.size(); iArg++) {
276  LogDebug("LHEInputArgs") << "arg [" << iArg << "] = " << args_[iArg];
277  }
278 
279  executeScript();
280 
281  //fill LHEXMLProduct (streaming read directly into compressed buffer to save memory)
282  std::unique_ptr<LHEXMLStringProduct> p(new LHEXMLStringProduct);
283 
284  //store the XML file only if explictly requested
285  if (storeXML_) {
286  std::ifstream instream(outputFile_);
287  if (!instream) {
288  throw cms::Exception("OutputOpenError") << "Unable to open script output file " << outputFile_ << ".";
289  }
290  instream.seekg(0, instream.end);
291  int insize = instream.tellg();
292  instream.seekg(0, instream.beg);
293  p->fillCompressedContent(instream, 0.25 * insize);
294  instream.close();
295  }
296  run.put(std::move(p), "LHEScriptOutput");
297 
298  // LHE C++ classes translation
299  // (read back uncompressed file from disk in streaming mode again to save memory)
300 
301  std::vector<std::string> infiles(1, outputFile_);
302  unsigned int skip = 0;
303  reader_ = std::make_unique<lhef::LHEReader>(infiles, skip);
304 
305  nextEvent();
306  if (runInfoLast) {
308 
309  std::unique_ptr<LHERunInfoProduct> product(new LHERunInfoProduct(*runInfo->getHEPRUP()));
310  std::for_each(runInfo->getHeaders().begin(),
311  runInfo->getHeaders().end(),
312  boost::bind(&LHERunInfoProduct::addHeader, product.get(), _1));
313  std::for_each(runInfo->getComments().begin(),
314  runInfo->getComments().end(),
315  boost::bind(&LHERunInfoProduct::addComment, product.get(), _1));
316 
317  // keep a copy around in case of merging
318  runInfoProducts.push_back(new LHERunInfoProduct(*product));
319  wasMerged = false;
320 
321  run.put(std::move(product));
322 
323  runInfo.reset();
324  }
325 }
#define LogDebug(id)
std::shared_ptr< lhef::LHERunInfo > runInfoLast
void addHeader(const Header &header)
std::vector< std::string > args_
std::unique_ptr< lhef::LHEReader > reader_
bool isAvailable() const
Definition: Service.h:40
virtual std::uint32_t mySeed() const =0
std::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:108
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
def move(src, dest)
Definition: eostools.py:511
int ExternalLHEProducer::closeDescriptors ( int  preserve)
private

Definition at line 351 of file ExternalLHEProducer.cc.

References dqmPostProcessing_online::DIR, DeadROC_duringRun::dir, Calorimetry_cff::dp, and ztee::fd.

Referenced by executeScript().

351  {
352  int maxfd = 1024;
353  int fd;
354 #ifdef __linux__
355  DIR* dir;
356  struct dirent* dp;
357  maxfd = preserve;
358  if ((dir = opendir("/proc/self/fd"))) {
359  errno = 0;
360  while ((dp = readdir(dir)) != nullptr) {
361  if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) {
362  continue;
363  }
364  if (sscanf(dp->d_name, "%d", &fd) != 1) {
365  //throw cms::Exception("closeDescriptors") << "Found unexpected filename in /proc/self/fd: " << dp->d_name;
366  return -1;
367  }
368  if (fd > maxfd) {
369  maxfd = fd;
370  }
371  }
372  if (errno) {
373  //throw cms::Exception("closeDescriptors") << "Unable to determine the number of fd (errno=" << errno << ", " << strerror(errno) << ").";
374  return errno;
375  }
376  closedir(dir);
377  }
378 #endif
379  // TODO: assert for an unreasonable number of fds?
380  for (fd = 3; fd < maxfd + 1; fd++) {
381  if (fd != preserve)
382  close(fd);
383  }
384  return 0;
385 }
fd
Definition: ztee.py:136
void ExternalLHEProducer::endRunProduce ( edm::Run run,
edm::EventSetup const &  es 
)
overrideprivate

Definition at line 328 of file ExternalLHEProducer.cc.

References edm::errors::EventGenerationFailure, Exception, eostools::move(), nextEvent(), outputFile_, partonLevel, edm::Run::put(), reader_, and runInfoProducts.

328  {
329  if (!runInfoProducts.empty()) {
330  std::unique_ptr<LHERunInfoProduct> product(runInfoProducts.pop_front().release());
331  run.put(std::move(product));
332  }
333 
334  nextEvent();
335  if (partonLevel) {
337  << "Error in ExternalLHEProducer::endRunProduce(). "
338  << "Event loop is over, but there are still lhe events to process."
339  << "This could happen if lhe file contains more events than requested. This is never expected to happen.";
340  }
341 
342  reader_.reset();
343 
344  if (unlink(outputFile_.c_str())) {
345  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << outputFile_
346  << " (errno=" << errno << ", " << strerror(errno) << ").";
347  }
348 }
std::shared_ptr< lhef::LHEEvent > partonLevel
std::unique_ptr< lhef::LHEReader > reader_
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:108
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
def move(src, dest)
Definition: eostools.py:511
void ExternalLHEProducer::executeScript ( )
private

Definition at line 388 of file ExternalLHEProducer.cc.

References dir2webdir::argc, args_, cmsBatch::argv, closeDescriptors(), Exception, mps_fire::i, NULL, pipe::pipe(), readEcalDQMStatus::read, scriptName_, mps_update::status, and writeEcalDQMStatus::write.

Referenced by beginRunProduce().

388  {
389  // Fork a script, wait until it finishes.
390 
391  int rc = 0, rc2 = 0;
392  int filedes[2], fd_flags;
393  unsigned int argc;
394 
395  if (pipe(filedes)) {
396  throw cms::Exception("Unable to create a new pipe");
397  }
398  FileCloseSentry sentry1(filedes[0]), sentry2(filedes[1]);
399 
400  if ((fd_flags = fcntl(filedes[1], F_GETFD, NULL)) == -1) {
401  throw cms::Exception("ExternalLHEProducer")
402  << "Failed to get pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
403  }
404  if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
405  throw cms::Exception("ExternalLHEProducer")
406  << "Failed to set pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
407  }
408 
409  argc = 1 + args_.size();
410  // TODO: assert that we have a reasonable number of arguments
411  char** argv = new char*[argc + 1];
412  argv[0] = strdup(scriptName_.c_str());
413  for (unsigned int i = 1; i < argc; i++) {
414  argv[i] = strdup(args_[i - 1].c_str());
415  }
416  argv[argc] = nullptr;
417 
418  pid_t pid = fork();
419  if (pid == 0) {
420  // The child process
421  if (!(rc = closeDescriptors(filedes[1]))) {
422  execvp(argv[0], argv); // If execv returns, we have an error.
423  rc = errno;
424  }
425  while ((write(filedes[1], &rc, sizeof(int)) == -1) && (errno == EINTR)) {
426  }
427  _exit(1);
428  }
429 
430  // Free the arg vector ASAP
431  for (unsigned int i = 0; i < args_.size() + 1; i++) {
432  free(argv[i]);
433  }
434  delete[] argv;
435 
436  if (pid == -1) {
437  throw cms::Exception("ForkException")
438  << "Unable to fork a child (errno=" << errno << ", " << strerror(errno) << ")";
439  }
440 
441  close(filedes[1]);
442  // If the exec succeeds, the read will fail.
443  while (((rc2 = read(filedes[0], &rc, sizeof(int))) == -1) && (errno == EINTR)) {
444  rc2 = 0;
445  }
446  if ((rc2 == sizeof(int)) && rc) {
447  throw cms::Exception("ExternalLHEProducer")
448  << "Failed to execute script (errno=" << rc << ", " << strerror(rc) << ")";
449  }
450  close(filedes[0]);
451 
452  int status = 0;
453  errno = 0;
454  do {
455  if (waitpid(pid, &status, 0) < 0) {
456  if (errno == EINTR) {
457  continue;
458  } else {
459  throw cms::Exception("ExternalLHEProducer")
460  << "Failed to read child status (errno=" << errno << ", " << strerror(errno) << ")";
461  }
462  }
463  if (WIFSIGNALED(status)) {
464  throw cms::Exception("ExternalLHEProducer") << "Child exited due to signal " << WTERMSIG(status) << ".";
465  }
466  if (WIFEXITED(status)) {
467  rc = WEXITSTATUS(status);
468  break;
469  }
470  } while (true);
471  if (rc) {
472  throw cms::Exception("ExternalLHEProducer") << "Child failed with exit code " << rc << ".";
473  }
474 }
#define NULL
Definition: scimark2.h:8
std::vector< std::string > args_
int closeDescriptors(int preserve)
def pipe(cmdline, input=None)
Definition: pipe.py:5
void ExternalLHEProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 507 of file ExternalLHEProducer.cc.

References edm::ParameterSetDescription::add(), edm::ConfigurationDescriptions::addDefault(), edm::ParameterSetDescription::addUntracked(), edm::ParameterSetDescription::addVPSetOptional(), edm::ParameterSetDescription::setComment(), and AlCaHLTBitMon_QueryRunRegistry::string.

507  {
508  //The following says we do not know what parameters are allowed so do no validation
509  // Please change this to state exactly what you do use, even if it is no parameters
511  desc.setComment("Executes an external script and places its output file into an EDM collection");
512 
513  edm::FileInPath thePath;
514  desc.add<edm::FileInPath>("scriptName", thePath);
515  desc.add<std::string>("outputFile", "myoutput");
516  desc.add<std::vector<std::string>>("args");
517  desc.add<uint32_t>("numberOfParameters");
518  desc.addUntracked<uint32_t>("nEvents");
519  desc.addUntracked<bool>("storeXML", false);
520 
521  edm::ParameterSetDescription nPartonMappingDesc;
522  nPartonMappingDesc.add<unsigned>("idprup");
523  nPartonMappingDesc.add<std::string>("order");
524  nPartonMappingDesc.add<unsigned>("np");
525  desc.addVPSetOptional("nPartonMapping", nPartonMappingDesc);
526 
527  descriptions.addDefault(desc);
528 }
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
ParameterDescriptionBase * addVPSetOptional(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
void setComment(std::string const &value)
void addDefault(ParameterSetDescription const &psetDescription)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void ExternalLHEProducer::nextEvent ( )
private

Definition at line 530 of file ExternalLHEProducer.cc.

References DEFINE_FWK_MODULE, partonLevel, reader_, runInfo, and runInfoLast.

Referenced by beginRunProduce(), endRunProduce(), and produce().

530  {
531  if (partonLevel)
532  return;
533 
534  if (not reader_) {
535  return;
536  }
537  partonLevel = reader_->next();
538  if (!partonLevel)
539  return;
540 
541  std::shared_ptr<lhef::LHERunInfo> runInfoThis = partonLevel->getRunInfo();
542  if (runInfoThis != runInfoLast) {
543  runInfo = runInfoThis;
544  runInfoLast = runInfoThis;
545  }
546 }
std::shared_ptr< lhef::LHEEvent > partonLevel
std::shared_ptr< lhef::LHERunInfo > runInfoLast
std::unique_ptr< lhef::LHEReader > reader_
std::shared_ptr< lhef::LHERunInfo > runInfo
void ExternalLHEProducer::preallocThreads ( unsigned int  iThreads)
overrideprivatevirtual

Reimplemented from edm::one::EDProducerBase.

Definition at line 165 of file ExternalLHEProducer.cc.

References nThreads_.

165 { nThreads_ = iThreads; }
void ExternalLHEProducer::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivate

Definition at line 168 of file ExternalLHEProducer.cc.

References LHEEventProduct::addComment(), LHERunInfoProduct::addComment(), LHERunInfoProduct::addHeader(), LHEEventProduct::addWeight(), edm::errors::EventGenerationFailure, Exception, lhe2HepMCConverter_cff::LHEEventProduct, lhe2HepMCConverter_cff::LHERunInfoProduct, eostools::move(), nextEvent(), np, nPartonMapping_, eventshapeDQM_cfi::order, partonLevel, edm::Event::put(), runInfo, runInfoProducts, and wasMerged.

168  {
169  nextEvent();
170  if (!partonLevel) {
172  << "No lhe event found in ExternalLHEProducer::produce(). "
173  << "The likely cause is that the lhe file contains fewer events than were requested, which is possible "
174  << "in case of phase space integration or uneweighting efficiency problems.";
175  }
176 
177  std::unique_ptr<LHEEventProduct> product(
178  new LHEEventProduct(*partonLevel->getHEPEUP(), partonLevel->originalXWGTUP()));
179  if (partonLevel->getPDF()) {
180  product->setPDF(*partonLevel->getPDF());
181  }
182  std::for_each(partonLevel->weights().begin(),
183  partonLevel->weights().end(),
184  boost::bind(&LHEEventProduct::addWeight, product.get(), _1));
185  product->setScales(partonLevel->scales());
186  if (nPartonMapping_.empty()) {
187  product->setNpLO(partonLevel->npLO());
188  product->setNpNLO(partonLevel->npNLO());
189  } else {
190  // overwrite npLO and npNLO values by user-specified mapping
191  unsigned processId(partonLevel->getHEPEUP()->IDPRUP);
192  unsigned order(0);
193  unsigned np(0);
194  try {
195  auto procDef(nPartonMapping_.at(processId));
196  order = procDef.first;
197  np = procDef.second;
198  } catch (std::out_of_range&) {
199  throw cms::Exception("ExternalLHEProducer")
200  << "Unexpected IDPRUP encountered: " << partonLevel->getHEPEUP()->IDPRUP;
201  }
202 
203  switch (order) {
204  case 0:
205  product->setNpLO(np);
206  product->setNpNLO(-1);
207  break;
208  case 1:
209  product->setNpLO(-1);
210  product->setNpNLO(np);
211  break;
212  default:
213  break;
214  }
215  }
216 
217  std::for_each(partonLevel->getComments().begin(),
218  partonLevel->getComments().end(),
219  boost::bind(&LHEEventProduct::addComment, product.get(), _1));
220 
221  iEvent.put(std::move(product));
222 
223  if (runInfo) {
224  std::unique_ptr<LHERunInfoProduct> product(new LHERunInfoProduct(*runInfo->getHEPRUP()));
225  std::for_each(runInfo->getHeaders().begin(),
226  runInfo->getHeaders().end(),
227  boost::bind(&LHERunInfoProduct::addHeader, product.get(), _1));
228  std::for_each(runInfo->getComments().begin(),
229  runInfo->getComments().end(),
230  boost::bind(&LHERunInfoProduct::addComment, product.get(), _1));
231 
232  if (!runInfoProducts.empty()) {
233  runInfoProducts.front().mergeProduct(*product);
234  if (!wasMerged) {
235  runInfoProducts.pop_front();
236  runInfoProducts.push_front(product.release());
237  wasMerged = true;
238  }
239  }
240 
241  runInfo.reset();
242  }
243 
244  partonLevel.reset();
245  return;
246 }
std::shared_ptr< lhef::LHEEvent > partonLevel
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
void addHeader(const Header &header)
void addComment(const std::string &line)
void addWeight(const WGT &wgt)
int np
Definition: AMPTWrapper.h:43
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
std::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
def move(src, dest)
Definition: eostools.py:511
std::unique_ptr< std::string > ExternalLHEProducer::readOutput ( )
private

Definition at line 478 of file ExternalLHEProducer.cc.

References BUFSIZE, Exception, ztee::fd, dqmiodumpmetadata::n, outputFile_, readEcalDQMStatus::read, contentValuesCheck::ss, and AlCaHLTBitMon_QueryRunRegistry::string.

478  {
479  int fd;
480  ssize_t n;
481  char buf[BUFSIZE];
482 
483  if ((fd = open(outputFile_.c_str(), O_RDONLY)) == -1) {
484  throw cms::Exception("OutputOpenError") << "Unable to open script output file " << outputFile_
485  << " (errno=" << errno << ", " << strerror(errno) << ").";
486  }
487 
488  std::stringstream ss;
489  while ((n = read(fd, buf, BUFSIZE)) > 0 || (n == -1 && errno == EINTR)) {
490  if (n > 0)
491  ss.write(buf, n);
492  }
493  if (n == -1) {
494  throw cms::Exception("OutputOpenError") << "Unable to read from script output file " << outputFile_
495  << " (errno=" << errno << ", " << strerror(errno) << ").";
496  }
497 
498  if (unlink(outputFile_.c_str())) {
499  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << outputFile_
500  << " (errno=" << errno << ", " << strerror(errno) << ").";
501  }
502 
503  return std::unique_ptr<std::string>(new std::string(ss.str()));
504 }
#define BUFSIZE
fd
Definition: ztee.py:136

Member Data Documentation

std::vector<std::string> ExternalLHEProducer::args_
private
uint32_t ExternalLHEProducer::nEvents_
private

Definition at line 90 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

uint32_t ExternalLHEProducer::npars_
private

Definition at line 89 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer().

std::map<unsigned, std::pair<unsigned, unsigned> > ExternalLHEProducer::nPartonMapping_ {}
private

Definition at line 96 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer(), and produce().

unsigned int ExternalLHEProducer::nThreads_ {1}
private

Definition at line 92 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and preallocThreads().

std::string ExternalLHEProducer::outputContents_
private

Definition at line 93 of file ExternalLHEProducer.cc.

std::string ExternalLHEProducer::outputFile_
private

Definition at line 87 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), endRunProduce(), and readOutput().

std::shared_ptr<lhef::LHEEvent> ExternalLHEProducer::partonLevel
private

Definition at line 101 of file ExternalLHEProducer.cc.

Referenced by endRunProduce(), nextEvent(), and produce().

std::unique_ptr<lhef::LHEReader> ExternalLHEProducer::reader_
private

Definition at line 98 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), endRunProduce(), and nextEvent().

std::shared_ptr<lhef::LHERunInfo> ExternalLHEProducer::runInfo
private

Definition at line 100 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), nextEvent(), and produce().

std::shared_ptr<lhef::LHERunInfo> ExternalLHEProducer::runInfoLast
private

Definition at line 99 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and nextEvent().

boost::ptr_deque<LHERunInfoProduct> ExternalLHEProducer::runInfoProducts
private

Definition at line 102 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), endRunProduce(), and produce().

std::string ExternalLHEProducer::scriptName_
private

Definition at line 86 of file ExternalLHEProducer.cc.

Referenced by executeScript().

bool ExternalLHEProducer::storeXML_
private

Definition at line 91 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

bool ExternalLHEProducer::wasMerged
private

Definition at line 103 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and produce().