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)
 
- Public Member Functions inherited from edm::one::EDProducer< edm::BeginRunProducer, edm::EndRunProducer >
 EDProducer ()=default
 
SerialTaskQueueglobalLuminosityBlocksQueue () final
 
SerialTaskQueueglobalRunsQueue () final
 
bool hasAbilityToProduceInLumis () const final
 
bool hasAbilityToProduceInRuns () 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) const
 
void endRunProduce (edm::Run &, edm::EventSetup const &) override
 
void executeScript (std::vector< std::string > const &args, int id, bool isPost) const
 
std::vector< std::string > makeArgs (uint32_t nEvents, unsigned int nThreads, std::uint32_t seed) const
 
void nextEvent ()
 
void preallocThreads (unsigned int) override
 
void produce (edm::Event &, const edm::EventSetup &) override
 

Private Attributes

const std::vector< std::string > args_
 
bool generateConcurrently_ {false}
 
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_
 
const std::vector< std::string > postGenerationCommand_
 
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::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 73 of file ExternalLHEProducer.cc.

Constructor & Destructor Documentation

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

Definition at line 131 of file ExternalLHEProducer.cc.

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

131  :
132  scriptName_((iConfig.getParameter<edm::FileInPath>("scriptName")).fullPath()),
133  outputFile_(iConfig.getParameter<std::string>("outputFile")),
134  args_(iConfig.getParameter<std::vector<std::string> >("args")),
135  npars_(iConfig.getParameter<uint32_t>("numberOfParameters")),
136  nEvents_(iConfig.getUntrackedParameter<uint32_t>("nEvents")),
137  storeXML_(iConfig.getUntrackedParameter<bool>("storeXML")),
138  generateConcurrently_(iConfig.getUntrackedParameter<bool>("generateConcurrently")),
139  postGenerationCommand_(iConfig.getUntrackedParameter<std::vector<std::string>>("postGenerationCommand"))
140 {
141  if (npars_ != args_.size())
142  throw cms::Exception("ExternalLHEProducer") << "Problem with configuration: " << args_.size() << " script arguments given, expected " << npars_;
143 
144  if (iConfig.exists("nPartonMapping")) {
145  auto& processMap(iConfig.getParameterSetVector("nPartonMapping"));
146  for (auto& cfg : processMap) {
147  unsigned processId(cfg.getParameter<unsigned>("idprup"));
148 
149  auto orderStr(cfg.getParameter<std::string>("order"));
150  unsigned order(0);
151  if (orderStr == "LO")
152  order = 0;
153  else if (orderStr == "NLO")
154  order = 1;
155  else
156  throw cms::Exception("ExternalLHEProducer") << "Invalid order specification for process " << processId << ": " << orderStr;
157 
158  unsigned np(cfg.getParameter<unsigned>("np"));
159 
160  nPartonMapping_.emplace(processId, std::make_pair(order, np));
161  }
162  }
163 
164  produces<LHEXMLStringProduct, edm::Transition::BeginRun>("LHEScriptOutput");
165 
166  produces<LHEEventProduct>();
167  produces<LHERunInfoProduct, edm::Transition::BeginRun>();
168  produces<LHERunInfoProduct, edm::Transition::EndRun>();
169 }
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
int np
Definition: AMPTWrapper.h:33
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
const std::vector< std::string > args_
const std::vector< std::string > postGenerationCommand_

Member Function Documentation

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

Definition at line 275 of file ExternalLHEProducer.cc.

References LHERunInfoProduct::addComment(), LHERunInfoProduct::addHeader(), pyrootRender::destroy(), MillePedeFileConverter_cfg::e, Exception, executeScript(), FrontierConditions_GlobalTag_cff::file, edm::FileInPath::fullPath(), generateConcurrently_, edm::Service< T >::isAvailable(), edm::make_functor_task(), makeArgs(), eostools::move(), edm::RandomNumberGenerator::mySeed(), nEvents, nEvents_, nextEvent(), nThreads_, MillePedeFileConverter_cfg::out, outputFile_, AlCaHLTBitMon_ParallelJobs::p, callgraph::path, postGenerationCommand_, edm::Run::put(), reader_, runInfo_, runInfoLast_, runInfoProducts_, alignCSCRings::s, SurveyInfoScenario_cff::seed, createPayload::skip, storeXML_, AlCaHLTBitMon_QueryRunRegistry::string, protons_cff::t, TrackValidation_cff::task, and wasMerged.

276 {
277 
278  // pass the number of events as previous to last argument
279 
280  // pass the random number generator seed as last argument
281 
283 
284  if ( ! rng.isAvailable()) {
285  throw cms::Exception("Configuration")
286  << "The ExternalLHEProducer module requires the RandomNumberGeneratorService\n"
287  "which is not present in the configuration file. You must add the service\n"
288  "in the configuration file if you want to run ExternalLHEProducer";
289  }
290 
291  std::vector<std::string> infiles;
292  auto const seed = rng->mySeed();
293  if (generateConcurrently_) {
294  infiles.resize(nThreads_);
295  auto const nEventsAve = nEvents_ / nThreads_;
296  unsigned int const overflow = nThreads_ - (nEvents_ % nThreads_);
297  std::exception_ptr except;
298  std::atomic<char> exceptSet{0};
299 
300  tbb::this_task_arena::isolate([this, &except, &infiles, &exceptSet, nEventsAve, overflow, seed]() {
301  tbb::empty_task* waitTask = new (tbb::task::allocate_root()) tbb::empty_task;
302  waitTask->set_ref_count(1 + nThreads_);
303 
304  for (unsigned int t = 0; t < nThreads_; ++t) {
305  uint32_t nEvents = nEventsAve;
306  if (nEvents_ % nThreads_ != 0 and t >= overflow) {
307  nEvents += 1;
308  }
309  auto task = edm::make_functor_task(tbb::task::allocate_root(),
310  [t, this, &infiles, seed, nEvents, &except, &exceptSet, waitTask]() {
311  try {
312  using namespace boost::filesystem;
313  using namespace std::string_literals;
314  auto out = path("thread"s + std::to_string(t)) / path(outputFile_);
315  infiles[t] = out.native();
316  executeScript(makeArgs(nEvents, 1, seed + t), t, false);
317  } catch (...) {
318  char expected = 0;
319  if (exceptSet.compare_exchange_strong(expected, 1)) {
320  except = std::current_exception();
321  exceptSet.store(2);
322  }
323  }
324  waitTask->decrement_ref_count();
325  });
326  tbb::task::spawn(*task);
327  }
328  waitTask->wait_for_all();
329  tbb::task::destroy(*waitTask);
330  });
331  if (exceptSet != 0) {
332  std::rethrow_exception(except);
333  }
334  } else {
335  infiles = std::vector<std::string>(1, outputFile_);
336  executeScript(makeArgs(nEvents_, nThreads_, seed), 0, false);
337  }
338 
339  //run post-generation command if specified
340  if (!postGenerationCommand_.empty()) {
341  std::vector<std::string> postcmd = postGenerationCommand_;
342  try {
343  postcmd[0] = edm::FileInPath(postcmd[0]).fullPath();
344  } catch (const edm::Exception& e) {
345  edm::LogWarning("ExternalLHEProducer") << postcmd[0] << " is not a relative path. Run it as a shell command.";
346  }
347  executeScript(postcmd, 0, true);
348  }
349 
350  //fill LHEXMLProduct (streaming read directly into compressed buffer to save memory)
351  std::unique_ptr<LHEXMLStringProduct> p(new LHEXMLStringProduct);
352 
353  //store the XML file only if explictly requested
354  if (storeXML_) {
356  if (generateConcurrently_) {
357  using namespace boost::filesystem;
358  file = (path("thread0") / path(outputFile_)).native();
359  } else {
360  file = outputFile_;
361  }
362  std::ifstream instream(file);
363  if (!instream) {
364  throw cms::Exception("OutputOpenError") << "Unable to open script output file " << outputFile_ << ".";
365  }
366  instream.seekg (0, instream.end);
367  int insize = instream.tellg();
368  instream.seekg (0, instream.beg);
369  p->fillCompressedContent(instream, 0.25*insize);
370  instream.close();
371  }
372  run.put(std::move(p), "LHEScriptOutput");
373 
374  // LHE C++ classes translation
375  // (read back uncompressed file from disk in streaming mode again to save memory)
376 
377  unsigned int skip = 0;
378  reader_ = std::make_unique<lhef::LHEReader>(infiles, skip);
379 
380  nextEvent();
381  if (runInfoLast_) {
383 
384  std::unique_ptr<LHERunInfoProduct> product(new LHERunInfoProduct(*runInfo_->getHEPRUP()));
385  std::for_each(runInfo_->getHeaders().begin(),
386  runInfo_->getHeaders().end(),
387  boost::bind(&LHERunInfoProduct::addHeader,
388  product.get(), _1));
389  std::for_each(runInfo_->getComments().begin(),
390  runInfo_->getComments().end(),
391  boost::bind(&LHERunInfoProduct::addComment,
392  product.get(), _1));
393 
394  // keep a copy around in case of merging
395  runInfoProducts_.push_back(new LHERunInfoProduct(*product));
396  wasMerged = false;
397 
398  run.put(std::move(product));
399 
400  runInfo_.reset();
401  }
402 
403 }
void addHeader(const Header &header)
def destroy(e)
Definition: pyrootRender.py:15
FunctorTask< F > * make_functor_task(ALLOC &&iAlloc, F f)
Definition: FunctorTask.h:47
void executeScript(std::vector< std::string > const &args, int id, bool isPost) const
boost::ptr_deque< LHERunInfoProduct > runInfoProducts_
std::shared_ptr< lhef::LHERunInfo > runInfoLast_
std::shared_ptr< lhef::LHERunInfo > runInfo_
std::unique_ptr< lhef::LHEReader > reader_
bool isAvailable() const
Definition: Service.h:40
std::vector< std::string > makeArgs(uint32_t nEvents, unsigned int nThreads, std::uint32_t seed) const
virtual std::uint32_t mySeed() const =0
const std::vector< std::string > postGenerationCommand_
void addComment(const std::string &line)
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:108
std::string fullPath() const
Definition: FileInPath.cc:163
UInt_t nEvents
Definition: hcalCalib.cc:41
def move(src, dest)
Definition: eostools.py:511
int ExternalLHEProducer::closeDescriptors ( int  preserve) const
private

Definition at line 471 of file ExternalLHEProducer.cc.

References dir.

Referenced by executeScript().

472 {
473  int maxfd = 1024;
474  int fd;
475 #ifdef __linux__
476  DIR * dir;
477  struct dirent *dp;
478  maxfd = preserve;
479  if ((dir = opendir("/proc/self/fd"))) {
480  errno = 0;
481  while ((dp = readdir (dir)) != nullptr) {
482  if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) {
483  continue;
484  }
485  if (sscanf(dp->d_name, "%d", &fd) != 1) {
486  //throw cms::Exception("closeDescriptors") << "Found unexpected filename in /proc/self/fd: " << dp->d_name;
487  return -1;
488  }
489  if (fd > maxfd) {
490  maxfd = fd;
491  }
492  }
493  if (errno) {
494  //throw cms::Exception("closeDescriptors") << "Unable to determine the number of fd (errno=" << errno << ", " << strerror(errno) << ").";
495  return errno;
496  }
497  closedir(dir);
498  }
499 #endif
500  // TODO: assert for an unreasonable number of fds?
501  for (fd=3; fd<maxfd+1; fd++) {
502  if (fd != preserve)
503  close(fd);
504  }
505  return 0;
506 }
dbl *** dir
Definition: mlp_gen.cc:35
void ExternalLHEProducer::endRunProduce ( edm::Run run,
edm::EventSetup const &  es 
)
overrideprivate

Definition at line 407 of file ExternalLHEProducer.cc.

References edm::errors::EventGenerationFailure, Exception, generateConcurrently_, eostools::move(), nextEvent(), nThreads_, MillePedeFileConverter_cfg::out, outputFile_, partonLevel_, callgraph::path, edm::Run::put(), reader_, runInfoProducts_, alignCSCRings::s, and protons_cff::t.

408 {
409 
410  if (!runInfoProducts_.empty()) {
411  std::unique_ptr<LHERunInfoProduct> product(runInfoProducts_.pop_front().release());
412  run.put(std::move(product));
413  }
414 
415  nextEvent();
416  if (partonLevel_) {
417  // VALIDATION_RUN env variable allows to finish event processing early without errors by sending SIGINT
418  if (std::getenv("VALIDATION_RUN") != nullptr) {
419  edm::LogWarning("ExternalLHEProducer")
420  << "Event loop is over, but there are still lhe events to process, ignoring...";
421  } else {
423  << "Error in ExternalLHEProducer::endRunProduce(). "
424  << "Event loop is over, but there are still lhe events to process."
425  << "This could happen if lhe file contains more events than requested. This is never expected to happen.";
426  }
427  }
428 
429  reader_.reset();
430  if (generateConcurrently_) {
431  for (unsigned int t = 0; t < nThreads_; ++t) {
432  using namespace boost::filesystem;
433  using namespace std::string_literals;
434  auto out = path("thread"s + std::to_string(t)) / path(outputFile_);
435  if (unlink(out.c_str())) {
436  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << out
437  << " (errno=" << errno << ", " << strerror(errno) << ").";
438  }
439  }
440  } else {
441  if (unlink(outputFile_.c_str())) {
442  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << outputFile_
443  << " (errno=" << errno << ", " << strerror(errno) << ").";
444  }
445  }
446 }
boost::ptr_deque< LHERunInfoProduct > runInfoProducts_
std::unique_ptr< lhef::LHEReader > reader_
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:108
def move(src, dest)
Definition: eostools.py:511
std::shared_ptr< lhef::LHEEvent > partonLevel_
void ExternalLHEProducer::executeScript ( std::vector< std::string > const &  args,
int  id,
bool  isPost 
) const
private

Definition at line 510 of file ExternalLHEProducer.cc.

References dir2webdir::argc, cmsBatch::argv, closeDescriptors(), RecoEcal_EventContent_cff::ec, Exception, generateConcurrently_, mps_fire::i, NULL, callgraph::path, sysUtil::pid, pipe::pipe(), alignCSCRings::s, scriptName_, mps_update::status, and TriggerAnalyzer::write().

Referenced by beginRunProduce().

511 {
512 
513  // Fork a script, wait until it finishes.
514 
515  int rc = 0, rc2 = 0;
516  int filedes[2], fd_flags;
517 
518  if (pipe(filedes)) {
519  throw cms::Exception("Unable to create a new pipe");
520  }
521  FileCloseSentry sentry1(filedes[0]), sentry2(filedes[1]);
522 
523  if ((fd_flags = fcntl(filedes[1], F_GETFD, NULL)) == -1) {
524  throw cms::Exception("ExternalLHEProducer") << "Failed to get pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
525  }
526  if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
527  throw cms::Exception("ExternalLHEProducer") << "Failed to set pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
528  }
529 
530  unsigned int argc_pre = 0;
531  // For generation command the first argument gives to the scriptName
532  if (!isPost) {
533  argc_pre = 1;
534  }
535  unsigned int argc = argc_pre + args.size();
536  // TODO: assert that we have a reasonable number of arguments
537  char **argv = new char *[argc+1];
538  if (!isPost) {
539  argv[0] = strdup(scriptName_.c_str());
540  }
541  for (unsigned int i = 0; i < args.size(); i++) {
542  argv[argc_pre + i] = strdup(args[i].c_str());
543  }
544  argv[argc] = nullptr;
545 
546  pid_t pid = fork();
547  if (pid == 0) {
548  // The child process
549  if (!(rc = closeDescriptors(filedes[1]))) {
550  if (!isPost && generateConcurrently_) {
551  using namespace boost::filesystem;
552  using namespace std::string_literals;
553  boost::system::error_code ec;
554  auto newDir = path("thread"s + std::to_string(id));
555  create_directory(newDir, ec);
556  current_path(newDir, ec);
557  }
558  execvp(argv[0], argv); // If execv returns, we have an error.
559  rc = errno;
560  }
561  while ((write(filedes[1], &rc, sizeof(int)) == -1) && (errno == EINTR)) {}
562  _exit(1);
563  }
564 
565  // Free the arg vector ASAP
566  for (unsigned int i=0; i<args.size()+1; i++) {
567  free(argv[i]);
568  }
569  delete [] argv;
570 
571  if (pid == -1) {
572  throw cms::Exception("ForkException") << "Unable to fork a child (errno=" << errno << ", " << strerror(errno) << ")";
573  }
574 
575  close(filedes[1]);
576  // If the exec succeeds, the read will fail.
577  while (((rc2 = read(filedes[0], &rc, sizeof(int))) == -1) && (errno == EINTR)) { rc2 = 0; }
578  if ((rc2 == sizeof(int)) && rc) {
579  throw cms::Exception("ExternalLHEProducer") << "Failed to execute script (errno=" << rc << ", " << strerror(rc) << ")";
580  }
581  close(filedes[0]);
582 
583  int status = 0;
584  errno = 0;
585  do {
586  if (waitpid(pid, &status, 0) < 0) {
587  if (errno == EINTR) {
588  continue;
589  } else {
590  throw cms::Exception("ExternalLHEProducer") << "Failed to read child status (errno=" << errno << ", " << strerror(errno) << ")";
591  }
592  }
593  if (WIFSIGNALED(status)) {
594  throw cms::Exception("ExternalLHEProducer") << "Child exited due to signal " << WTERMSIG(status) << ".";
595  }
596  if (WIFEXITED(status)) {
597  rc = WEXITSTATUS(status);
598  break;
599  }
600  } while (true);
601  if (rc) {
602  throw cms::Exception("ExternalLHEProducer") << "Child failed with exit code " << rc << ".";
603  }
604 
605 }
#define NULL
Definition: scimark2.h:8
int closeDescriptors(int preserve) const
def pipe(cmdline, input=None)
Definition: pipe.py:5
def write(self, setup)
void ExternalLHEProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 609 of file ExternalLHEProducer.cc.

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

609  {
610  //The following says we do not know what parameters are allowed so do no validation
611  // Please change this to state exactly what you do use, even if it is no parameters
613  desc.setComment("Executes an external script and places its output file into an EDM collection");
614 
615  edm::FileInPath thePath;
616  desc.add<edm::FileInPath>("scriptName", thePath);
617  desc.add<std::string>("outputFile", "myoutput");
618  desc.add<std::vector<std::string> >("args");
619  desc.add<uint32_t>("numberOfParameters");
620  desc.addUntracked<uint32_t>("nEvents");
621  desc.addUntracked<bool>("storeXML", false);
622  desc.addUntracked<bool>("generateConcurrently", false)
623  ->setComment("If true, run the script concurrently in separate processes.");
624  desc.addUntracked<std::vector<std::string>>("postGenerationCommand", std::vector<std::string>())
625  ->setComment(
626  "Command to run after the generation script has completed. The first argument can be a relative path.");
627 
628  edm::ParameterSetDescription nPartonMappingDesc;
629  nPartonMappingDesc.add<unsigned>("idprup");
630  nPartonMappingDesc.add<std::string>("order");
631  nPartonMappingDesc.add<unsigned>("np");
632  desc.addVPSetOptional("nPartonMapping", nPartonMappingDesc);
633 
634  descriptions.addDefault(desc);
635 }
void setComment(std::string const &value)
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)
std::vector< std::string > ExternalLHEProducer::makeArgs ( uint32_t  nEvents,
unsigned int  nThreads,
std::uint32_t  seed 
) const
private

Definition at line 448 of file ExternalLHEProducer.cc.

References writedatasetfile::args, args_, popcon2dropbox::copy(), and LogDebug.

Referenced by beginRunProduce().

450  {
451  std::vector<std::string> args;
452  args.reserve(3 + args_.size());
453 
454  args.push_back(args_.front());
455  args.push_back(std::to_string(nEvents));
456 
457  args.push_back(std::to_string(seed));
458 
459  args.push_back(std::to_string(nThreads));
460  std::copy(args_.begin() + 1, args_.end(), std::back_inserter(args));
461 
462  for (unsigned int iArg = 0; iArg < args.size(); iArg++) {
463  LogDebug("LHEInputArgs") << "arg [" << iArg << "] = " << args[iArg];
464  }
465 
466  return args;
467 }
#define LogDebug(id)
def copy(args, dbName)
const std::vector< std::string > args_
UInt_t nEvents
Definition: hcalCalib.cc:41
void ExternalLHEProducer::nextEvent ( )
private

Definition at line 637 of file ExternalLHEProducer.cc.

References DEFINE_FWK_MODULE, partonLevel_, reader_, runInfo_, and runInfoLast_.

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

638 {
639 
640  if (partonLevel_)
641  return;
642 
643  if(not reader_) { return;}
644 
645  partonLevel_ = reader_->next();
646  if (!partonLevel_) {
647  //see if we have another file to read;
648  bool newFileOpened;
649  do {
650  newFileOpened = false;
651  partonLevel_ = reader_->next(&newFileOpened);
652  } while (newFileOpened && !partonLevel_);
653  }
654  if (!partonLevel_)
655  return;
656 
657  std::shared_ptr<lhef::LHERunInfo> runInfoThis = partonLevel_->getRunInfo();
658  if (runInfoThis != runInfoLast_) {
659  runInfo_ = runInfoThis;
660  runInfoLast_ = runInfoThis;
661  }
662 }
std::shared_ptr< lhef::LHERunInfo > runInfoLast_
std::shared_ptr< lhef::LHERunInfo > runInfo_
std::unique_ptr< lhef::LHEReader > reader_
std::shared_ptr< lhef::LHEEvent > partonLevel_
void ExternalLHEProducer::preallocThreads ( unsigned int  iThreads)
overrideprivatevirtual

Reimplemented from edm::one::EDProducerBase.

Definition at line 178 of file ExternalLHEProducer.cc.

References nThreads_.

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

Definition at line 185 of file ExternalLHEProducer.cc.

References LHEEventProduct::addComment(), LHERunInfoProduct::addComment(), LHERunInfoProduct::addHeader(), LHEEventProduct::addWeight(), edm::errors::EventGenerationFailure, Exception, eostools::move(), nextEvent(), np, nPartonMapping_, partonLevel_, edm::Event::put(), runInfo_, runInfoProducts_, and wasMerged.

186 {
187  nextEvent();
188  if (!partonLevel_) {
189  throw edm::Exception(edm::errors::EventGenerationFailure) << "No lhe event found in ExternalLHEProducer::produce(). "
190  << "The likely cause is that the lhe file contains fewer events than were requested, which is possible "
191  << "in case of phase space integration or unweighting efficiency problems.";
192  }
193 
194  std::unique_ptr<LHEEventProduct> product(
195  new LHEEventProduct(*partonLevel_->getHEPEUP(),
196  partonLevel_->originalXWGTUP())
197  );
198  if (partonLevel_->getPDF()) {
199  product->setPDF(*partonLevel_->getPDF());
200  }
201  std::for_each(partonLevel_->weights().begin(),
202  partonLevel_->weights().end(),
203  boost::bind(&LHEEventProduct::addWeight,
204  product.get(), _1));
205  product->setScales(partonLevel_->scales());
206  product->setEvtNum(partonLevel_->evtnum());
207  if (nPartonMapping_.empty()) {
208  product->setNpLO(partonLevel_->npLO());
209  product->setNpNLO(partonLevel_->npNLO());
210  }
211  else {
212  // overwrite npLO and npNLO values by user-specified mapping
213  unsigned processId(partonLevel_->getHEPEUP()->IDPRUP);
214  unsigned order(0);
215  unsigned np(0);
216  try {
217  auto procDef(nPartonMapping_.at(processId));
218  order = procDef.first;
219  np = procDef.second;
220  }
221  catch (std::out_of_range&) {
222  throw cms::Exception("ExternalLHEProducer") << "Unexpected IDPRUP encountered: " << partonLevel_->getHEPEUP()->IDPRUP;
223  }
224 
225  switch (order) {
226  case 0:
227  product->setNpLO(np);
228  product->setNpNLO(-1);
229  break;
230  case 1:
231  product->setNpLO(-1);
232  product->setNpNLO(np);
233  break;
234  default:
235  break;
236  }
237  }
238 
239  std::for_each(partonLevel_->getComments().begin(),
240  partonLevel_->getComments().end(),
241  boost::bind(&LHEEventProduct::addComment,
242  product.get(), _1));
243 
244  iEvent.put(std::move(product));
245 
246  if (runInfo_) {
247  std::unique_ptr<LHERunInfoProduct> product(new LHERunInfoProduct(*runInfo_->getHEPRUP()));
248  std::for_each(runInfo_->getHeaders().begin(),
249  runInfo_->getHeaders().end(),
250  boost::bind(&LHERunInfoProduct::addHeader,
251  product.get(), _1));
252  std::for_each(runInfo_->getComments().begin(),
253  runInfo_->getComments().end(),
254  boost::bind(&LHERunInfoProduct::addComment,
255  product.get(), _1));
256 
257  if (!runInfoProducts_.empty()) {
258  runInfoProducts_.front().mergeProduct(*product);
259  if (!wasMerged) {
260  runInfoProducts_.pop_front();
261  runInfoProducts_.push_front(product.release());
262  wasMerged = true;
263  }
264  }
265 
266  runInfo_.reset();
267  }
268 
269  partonLevel_.reset();
270  return;
271 }
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
void addHeader(const Header &header)
void addComment(const std::string &line)
void addWeight(const WGT &wgt)
boost::ptr_deque< LHERunInfoProduct > runInfoProducts_
int np
Definition: AMPTWrapper.h:33
std::shared_ptr< lhef::LHERunInfo > runInfo_
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
void addComment(const std::string &line)
def move(src, dest)
Definition: eostools.py:511
std::shared_ptr< lhef::LHEEvent > partonLevel_

Member Data Documentation

const std::vector<std::string> ExternalLHEProducer::args_
private
bool ExternalLHEProducer::generateConcurrently_ {false}
private

Definition at line 102 of file ExternalLHEProducer.cc.

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

uint32_t ExternalLHEProducer::nEvents_
private

Definition at line 98 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

uint32_t ExternalLHEProducer::npars_
private

Definition at line 97 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer().

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

Definition at line 106 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer(), and produce().

unsigned int ExternalLHEProducer::nThreads_ {1}
private

Definition at line 100 of file ExternalLHEProducer.cc.

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

std::string ExternalLHEProducer::outputContents_
private

Definition at line 101 of file ExternalLHEProducer.cc.

std::string ExternalLHEProducer::outputFile_
private

Definition at line 95 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and endRunProduce().

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

Definition at line 111 of file ExternalLHEProducer.cc.

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

const std::vector<std::string> ExternalLHEProducer::postGenerationCommand_
private

Definition at line 103 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

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

Definition at line 108 of file ExternalLHEProducer.cc.

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

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

Definition at line 110 of file ExternalLHEProducer.cc.

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

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

Definition at line 109 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and nextEvent().

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

Definition at line 112 of file ExternalLHEProducer.cc.

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

std::string ExternalLHEProducer::scriptName_
private

Definition at line 94 of file ExternalLHEProducer.cc.

Referenced by executeScript().

bool ExternalLHEProducer::storeXML_
private

Definition at line 99 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

bool ExternalLHEProducer::wasMerged
private

Definition at line 113 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and produce().