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 274 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.

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

References dir.

Referenced by executeScript().

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

Definition at line 406 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.

407 {
408 
409  if (!runInfoProducts_.empty()) {
410  std::unique_ptr<LHERunInfoProduct> product(runInfoProducts_.pop_front().release());
411  run.put(std::move(product));
412  }
413 
414  nextEvent();
415  if (partonLevel_) {
416  // VALIDATION_RUN env variable allows to finish event processing early without errors by sending SIGINT
417  if (std::getenv("VALIDATION_RUN") != nullptr) {
418  edm::LogWarning("ExternalLHEProducer")
419  << "Event loop is over, but there are still lhe events to process, ignoring...";
420  } else {
422  << "Error in ExternalLHEProducer::endRunProduce(). "
423  << "Event loop is over, but there are still lhe events to process."
424  << "This could happen if lhe file contains more events than requested. This is never expected to happen.";
425  }
426  }
427 
428  reader_.reset();
429  if (generateConcurrently_) {
430  for (unsigned int t = 0; t < nThreads_; ++t) {
431  using namespace boost::filesystem;
432  using namespace std::string_literals;
433  auto out = path("thread"s + std::to_string(t)) / path(outputFile_);
434  if (unlink(out.c_str())) {
435  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << out
436  << " (errno=" << errno << ", " << strerror(errno) << ").";
437  }
438  }
439  } else {
440  if (unlink(outputFile_.c_str())) {
441  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << outputFile_
442  << " (errno=" << errno << ", " << strerror(errno) << ").";
443  }
444  }
445 }
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 509 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().

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

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

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

Referenced by beginRunProduce().

449  {
450  std::vector<std::string> args;
451  args.reserve(3 + args_.size());
452 
453  args.push_back(args_.front());
454  args.push_back(std::to_string(nEvents));
455 
456  args.push_back(std::to_string(seed));
457 
458  args.push_back(std::to_string(nThreads));
459  std::copy(args_.begin() + 1, args_.end(), std::back_inserter(args));
460 
461  for (unsigned int iArg = 0; iArg < args.size(); iArg++) {
462  LogDebug("LHEInputArgs") << "arg [" << iArg << "] = " << args[iArg];
463  }
464 
465  return args;
466 }
#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 636 of file ExternalLHEProducer.cc.

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

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

637 {
638 
639  if (partonLevel_)
640  return;
641 
642  if(not reader_) { return;}
643 
644  partonLevel_ = reader_->next();
645  if (!partonLevel_) {
646  //see if we have another file to read;
647  bool newFileOpened;
648  do {
649  newFileOpened = false;
650  partonLevel_ = reader_->next(&newFileOpened);
651  } while (newFileOpened && !partonLevel_);
652  }
653  if (!partonLevel_)
654  return;
655 
656  std::shared_ptr<lhef::LHERunInfo> runInfoThis = partonLevel_->getRunInfo();
657  if (runInfoThis != runInfoLast_) {
658  runInfo_ = runInfoThis;
659  runInfoLast_ = runInfoThis;
660  }
661 }
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 uneweighting 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  if (nPartonMapping_.empty()) {
207  product->setNpLO(partonLevel_->npLO());
208  product->setNpNLO(partonLevel_->npNLO());
209  }
210  else {
211  // overwrite npLO and npNLO values by user-specified mapping
212  unsigned processId(partonLevel_->getHEPEUP()->IDPRUP);
213  unsigned order(0);
214  unsigned np(0);
215  try {
216  auto procDef(nPartonMapping_.at(processId));
217  order = procDef.first;
218  np = procDef.second;
219  }
220  catch (std::out_of_range&) {
221  throw cms::Exception("ExternalLHEProducer") << "Unexpected IDPRUP encountered: " << partonLevel_->getHEPEUP()->IDPRUP;
222  }
223 
224  switch (order) {
225  case 0:
226  product->setNpLO(np);
227  product->setNpNLO(-1);
228  break;
229  case 1:
230  product->setNpLO(-1);
231  product->setNpNLO(np);
232  break;
233  default:
234  break;
235  }
236  }
237 
238  std::for_each(partonLevel_->getComments().begin(),
239  partonLevel_->getComments().end(),
240  boost::bind(&LHEEventProduct::addComment,
241  product.get(), _1));
242 
243  iEvent.put(std::move(product));
244 
245  if (runInfo_) {
246  std::unique_ptr<LHERunInfoProduct> product(new LHERunInfoProduct(*runInfo_->getHEPRUP()));
247  std::for_each(runInfo_->getHeaders().begin(),
248  runInfo_->getHeaders().end(),
249  boost::bind(&LHERunInfoProduct::addHeader,
250  product.get(), _1));
251  std::for_each(runInfo_->getComments().begin(),
252  runInfo_->getComments().end(),
253  boost::bind(&LHERunInfoProduct::addComment,
254  product.get(), _1));
255 
256  if (!runInfoProducts_.empty()) {
257  runInfoProducts_.front().mergeProduct(*product);
258  if (!wasMerged) {
259  runInfoProducts_.pop_front();
260  runInfoProducts_.push_front(product.release());
261  wasMerged = true;
262  }
263  }
264 
265  runInfo_.reset();
266  }
267 
268  partonLevel_.reset();
269  return;
270 }
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().