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 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
 
std::vector< bool > const & recordProvenanceList () 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)
 
TypeLabelList const & typeLabelList () const
 used by the fwk to register the list of products of this module More...
 
 ~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 &&)=default
 
 EDConsumerBase (EDConsumerBase const &)=delete
 
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
 
EDConsumerBaseoperator= (EDConsumerBase &&)=default
 
EDConsumerBase const & operator= (EDConsumerBase const &)=delete
 
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) 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_
 
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
template<class ProductType >
BranchAliasSetterT< ProductType > produces ()
 declare what type of product will make and with which optional label More...
 
template<typename ProductType , BranchType B>
BranchAliasSetterT< ProductType > produces ()
 
template<typename ProductType , Transition B>
BranchAliasSetterT< ProductType > produces ()
 
BranchAliasSetter produces (const TypeID &id, std::string instanceName=std::string(), bool recordProvenance=true)
 
template<BranchType B>
BranchAliasSetter produces (const TypeID &id, std::string instanceName=std::string(), bool recordProvenance=true)
 
template<Transition B>
BranchAliasSetter produces (const TypeID &id, std::string instanceName=std::string(), bool recordProvenance=true)
 
template<typename ProductType , Transition B>
BranchAliasSetterT< ProductType > produces (std::string instanceName)
 
template<class ProductType >
BranchAliasSetterT< ProductType > produces (std::string instanceName)
 
template<typename ProductType , BranchType B>
BranchAliasSetterT< ProductType > produces (std::string instanceName)
 
ProducesCollector producesCollector ()
 
- Protected Member Functions inherited from edm::EDConsumerBase
EDGetToken consumes (const TypeToGet &id, edm::InputTag const &tag)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > consumes (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)
 
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > mayConsume (edm::InputTag const &tag)
 

Detailed Description

Description: [one line class summary]

Implementation: [Notes on implementation]

Definition at line 70 of file ExternalLHEProducer.cc.

Constructor & Destructor Documentation

◆ ExternalLHEProducer()

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

Definition at line 123 of file ExternalLHEProducer.cc.

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

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

Member Function Documentation

◆ beginRunProduce()

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

Definition at line 252 of file ExternalLHEProducer.cc.

252  {
253  // pass the number of events as previous to last argument
254 
255  // pass the random number generator seed as last argument
256 
258 
259  if (!rng.isAvailable()) {
260  throw cms::Exception("Configuration")
261  << "The ExternalLHEProducer module requires the RandomNumberGeneratorService\n"
262  "which is not present in the configuration file. You must add the service\n"
263  "in the configuration file if you want to run ExternalLHEProducer";
264  }
265 
266  std::vector<std::string> infiles;
267  auto const seed = rng->mySeed();
268  if (generateConcurrently_) {
269  infiles.resize(nThreads_);
270  auto const nEventsAve = nEvents_ / nThreads_;
271  unsigned int const overflow = nThreads_ - (nEvents_ % nThreads_);
272  std::exception_ptr except;
273  std::atomic<char> exceptSet{0};
274 
275  tbb::this_task_arena::isolate([this, &except, &infiles, &exceptSet, nEventsAve, overflow, seed]() {
276  tbb::empty_task* waitTask = new (tbb::task::allocate_root()) tbb::empty_task;
277  waitTask->set_ref_count(1 + nThreads_);
278 
279  for (unsigned int t = 0; t < nThreads_; ++t) {
280  uint32_t nEvents = nEventsAve;
281  if (nEvents_ % nThreads_ != 0 and t >= overflow) {
282  nEvents += 1;
283  }
284  auto task = edm::make_functor_task(tbb::task::allocate_root(),
285  [t, this, &infiles, seed, nEvents, &except, &exceptSet, waitTask]() {
286  CMS_SA_ALLOW try {
287  using namespace boost::filesystem;
288  using namespace std::string_literals;
289  auto out = path("thread"s + std::to_string(t)) / path(outputFile_);
290  infiles[t] = out.native();
292  } catch (...) {
293  char expected = 0;
294  if (exceptSet.compare_exchange_strong(expected, 1)) {
295  except = std::current_exception();
296  exceptSet.store(2);
297  }
298  }
299  waitTask->decrement_ref_count();
300  });
301  tbb::task::spawn(*task);
302  }
303  waitTask->wait_for_all();
304  tbb::task::destroy(*waitTask);
305  });
306  if (exceptSet != 0) {
307  std::rethrow_exception(except);
308  }
309  } else {
310  infiles = std::vector<std::string>(1, outputFile_);
312  }
313 
314  //fill LHEXMLProduct (streaming read directly into compressed buffer to save memory)
315  std::unique_ptr<LHEXMLStringProduct> p(new LHEXMLStringProduct);
316 
317  //store the XML file only if explictly requested
318  if (storeXML_) {
320  if (generateConcurrently_) {
321  using namespace boost::filesystem;
322  file = (path("thread0") / path(outputFile_)).native();
323  } else {
324  file = outputFile_;
325  }
326  std::ifstream instream(file);
327  if (!instream) {
328  throw cms::Exception("OutputOpenError") << "Unable to open script output file " << outputFile_ << ".";
329  }
330  instream.seekg(0, instream.end);
331  int insize = instream.tellg();
332  instream.seekg(0, instream.beg);
333  p->fillCompressedContent(instream, 0.25 * insize);
334  instream.close();
335  }
336  run.put(std::move(p), "LHEScriptOutput");
337 
338  // LHE C++ classes translation
339  // (read back uncompressed file from disk in streaming mode again to save memory)
340 
341  unsigned int skip = 0;
342  reader_ = std::make_unique<lhef::LHEReader>(infiles, skip);
343 
344  nextEvent();
345  if (runInfoLast_) {
347 
348  std::unique_ptr<LHERunInfoProduct> product(new LHERunInfoProduct(*runInfo_->getHEPRUP()));
349  std::for_each(runInfo_->getHeaders().begin(),
350  runInfo_->getHeaders().end(),
351  boost::bind(&LHERunInfoProduct::addHeader, product.get(), _1));
352  std::for_each(runInfo_->getComments().begin(),
353  runInfo_->getComments().end(),
354  boost::bind(&LHERunInfoProduct::addComment, product.get(), _1));
355 
356  // keep a copy around in case of merging
357  runInfoProducts_.push_back(new LHERunInfoProduct(*product));
358  wasMerged = false;
359 
360  run.put(std::move(product));
361 
362  runInfo_.reset();
363  }
364 }

References LHERunInfoProduct::addComment(), LHERunInfoProduct::addHeader(), CMS_SA_ALLOW, Exception, executeScript(), FrontierConditions_GlobalTag_cff::file, generateConcurrently_, edm::Service< T >::isAvailable(), lhe2HepMCConverter_cff::LHERunInfoProduct, edm::make_functor_task(), makeArgs(), eostools::move(), edm::RandomNumberGenerator::mySeed(), nEvents, nEvents_, nextEvent(), nThreads_, MillePedeFileConverter_cfg::out, outputFile_, AlCaHLTBitMon_ParallelJobs::p, castor_dqm_sourceclient_file_cfg::path, reader_, writedatasetfile::run, runInfo_, runInfoLast_, runInfoProducts_, alignCSCRings::s, SurveyInfoScenario_cff::seed, optionsL1T::skip, storeXML_, AlCaHLTBitMon_QueryRunRegistry::string, OrderedSet::t, TrackValidation_cff::task, and wasMerged.

◆ closeDescriptors()

int ExternalLHEProducer::closeDescriptors ( int  preserve) const
private

Definition at line 422 of file ExternalLHEProducer.cc.

422  {
423  int maxfd = 1024;
424  int fd;
425 #ifdef __linux__
426  DIR* dir;
427  struct dirent* dp;
428  maxfd = preserve;
429  if ((dir = opendir("/proc/self/fd"))) {
430  errno = 0;
431  while ((dp = readdir(dir)) != nullptr) {
432  if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) {
433  continue;
434  }
435  if (sscanf(dp->d_name, "%d", &fd) != 1) {
436  //throw cms::Exception("closeDescriptors") << "Found unexpected filename in /proc/self/fd: " << dp->d_name;
437  return -1;
438  }
439  if (fd > maxfd) {
440  maxfd = fd;
441  }
442  }
443  if (errno) {
444  //throw cms::Exception("closeDescriptors") << "Unable to determine the number of fd (errno=" << errno << ", " << strerror(errno) << ").";
445  return errno;
446  }
447  closedir(dir);
448  }
449 #endif
450  // TODO: assert for an unreasonable number of fds?
451  for (fd = 3; fd < maxfd + 1; fd++) {
452  if (fd != preserve)
453  close(fd);
454  }
455  return 0;
456 }

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

Referenced by executeScript().

◆ endRunProduce()

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

Definition at line 367 of file ExternalLHEProducer.cc.

367  {
368  if (!runInfoProducts_.empty()) {
369  std::unique_ptr<LHERunInfoProduct> product(runInfoProducts_.pop_front().release());
370  run.put(std::move(product));
371  }
372 
373  nextEvent();
374  if (partonLevel_) {
376  << "Error in ExternalLHEProducer::endRunProduce(). "
377  << "Event loop is over, but there are still lhe events to process."
378  << "This could happen if lhe file contains more events than requested. This is never expected to happen.";
379  }
380 
381  reader_.reset();
382  if (generateConcurrently_) {
383  for (unsigned int t = 0; t < nThreads_; ++t) {
384  using namespace boost::filesystem;
385  using namespace std::string_literals;
386  auto out = path("thread"s + std::to_string(t)) / path(outputFile_);
387  if (unlink(out.c_str())) {
388  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << out
389  << " (errno=" << errno << ", " << strerror(errno) << ").";
390  }
391  }
392  } else {
393  if (unlink(outputFile_.c_str())) {
394  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << outputFile_
395  << " (errno=" << errno << ", " << strerror(errno) << ").";
396  }
397  }
398 }

References edm::errors::EventGenerationFailure, Exception, generateConcurrently_, eostools::move(), nextEvent(), nThreads_, MillePedeFileConverter_cfg::out, outputFile_, partonLevel_, castor_dqm_sourceclient_file_cfg::path, reader_, writedatasetfile::run, runInfoProducts_, alignCSCRings::s, and OrderedSet::t.

◆ executeScript()

void ExternalLHEProducer::executeScript ( std::vector< std::string > const &  args,
int  id 
) const
private

Definition at line 459 of file ExternalLHEProducer.cc.

459  {
460  // Fork a script, wait until it finishes.
461 
462  int rc = 0, rc2 = 0;
463  int filedes[2], fd_flags;
464 
465  if (pipe(filedes)) {
466  throw cms::Exception("Unable to create a new pipe");
467  }
468  FileCloseSentry sentry1(filedes[0]), sentry2(filedes[1]);
469 
470  if ((fd_flags = fcntl(filedes[1], F_GETFD, NULL)) == -1) {
471  throw cms::Exception("ExternalLHEProducer")
472  << "Failed to get pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
473  }
474  if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
475  throw cms::Exception("ExternalLHEProducer")
476  << "Failed to set pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
477  }
478 
479  unsigned int argc = 1 + args.size();
480  // TODO: assert that we have a reasonable number of arguments
481  char** argv = new char*[argc + 1];
482  argv[0] = strdup(scriptName_.c_str());
483  for (unsigned int i = 1; i < argc; i++) {
484  argv[i] = strdup(args[i - 1].c_str());
485  }
486  argv[argc] = nullptr;
487 
488  pid_t pid = fork();
489  if (pid == 0) {
490  // The child process
491  if (!(rc = closeDescriptors(filedes[1]))) {
492  if (generateConcurrently_) {
493  using namespace boost::filesystem;
494  using namespace std::string_literals;
495  boost::system::error_code ec;
496  auto newDir = path("thread"s + std::to_string(id));
497  create_directory(newDir, ec);
498  current_path(newDir, ec);
499  }
500  execvp(argv[0], argv); // If execv returns, we have an error.
501  rc = errno;
502  }
503  while ((write(filedes[1], &rc, sizeof(int)) == -1) && (errno == EINTR)) {
504  }
505  _exit(1);
506  }
507 
508  // Free the arg vector ASAP
509  for (unsigned int i = 0; i < args.size() + 1; i++) {
510  free(argv[i]);
511  }
512  delete[] argv;
513 
514  if (pid == -1) {
515  throw cms::Exception("ForkException")
516  << "Unable to fork a child (errno=" << errno << ", " << strerror(errno) << ")";
517  }
518 
519  close(filedes[1]);
520  // If the exec succeeds, the read will fail.
521  while (((rc2 = read(filedes[0], &rc, sizeof(int))) == -1) && (errno == EINTR)) {
522  rc2 = 0;
523  }
524  if ((rc2 == sizeof(int)) && rc) {
525  throw cms::Exception("ExternalLHEProducer")
526  << "Failed to execute script (errno=" << rc << ", " << strerror(rc) << ")";
527  }
528  close(filedes[0]);
529 
530  int status = 0;
531  errno = 0;
532  do {
533  if (waitpid(pid, &status, 0) < 0) {
534  if (errno == EINTR) {
535  continue;
536  } else {
537  throw cms::Exception("ExternalLHEProducer")
538  << "Failed to read child status (errno=" << errno << ", " << strerror(errno) << ")";
539  }
540  }
541  if (WIFSIGNALED(status)) {
542  throw cms::Exception("ExternalLHEProducer") << "Child exited due to signal " << WTERMSIG(status) << ".";
543  }
544  if (WIFEXITED(status)) {
545  rc = WEXITSTATUS(status);
546  break;
547  }
548  } while (true);
549  if (rc) {
550  throw cms::Exception("ExternalLHEProducer") << "Child failed with exit code " << rc << ".";
551  }
552 }

References dir2webdir::argc, writedatasetfile::args, cmsBatch::argv, closeDescriptors(), Exception, generateConcurrently_, mps_fire::i, NULL, castor_dqm_sourceclient_file_cfg::path, pipe::pipe(), readEcalDQMStatus::read, alignCSCRings::s, scriptName_, mps_update::status, and writeEcalDQMStatus::write.

Referenced by beginRunProduce().

◆ fillDescriptions()

void ExternalLHEProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 555 of file ExternalLHEProducer.cc.

555  {
556  //The following says we do not know what parameters are allowed so do no validation
557  // Please change this to state exactly what you do use, even if it is no parameters
559  desc.setComment("Executes an external script and places its output file into an EDM collection");
560 
561  edm::FileInPath thePath;
562  desc.add<edm::FileInPath>("scriptName", thePath);
563  desc.add<std::string>("outputFile", "myoutput");
564  desc.add<std::vector<std::string>>("args");
565  desc.add<uint32_t>("numberOfParameters");
566  desc.addUntracked<uint32_t>("nEvents");
567  desc.addUntracked<bool>("storeXML", false);
568  desc.addUntracked<bool>("generateConcurrently", false)
569  ->setComment("If true, run the script concurrently in separate processes.");
570 
571  edm::ParameterSetDescription nPartonMappingDesc;
572  nPartonMappingDesc.add<unsigned>("idprup");
573  nPartonMappingDesc.add<std::string>("order");
574  nPartonMappingDesc.add<unsigned>("np");
575  desc.addVPSetOptional("nPartonMapping", nPartonMappingDesc);
576 
577  descriptions.addDefault(desc);
578 }

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

◆ makeArgs()

std::vector< std::string > ExternalLHEProducer::makeArgs ( uint32_t  nEvents,
unsigned int  nThreads,
std::uint32_t  seed 
) const
private

Definition at line 400 of file ExternalLHEProducer.cc.

402  {
403  std::vector<std::string> args;
404  args.reserve(3 + args_.size());
405 
406  args.push_back(args_.front());
407  args.push_back(std::to_string(nEvents));
408 
409  args.push_back(std::to_string(seed));
410 
411  args.push_back(std::to_string(nThreads));
412  std::copy(args_.begin() + 1, args_.end(), std::back_inserter(args));
413 
414  for (unsigned int iArg = 0; iArg < args.size(); iArg++) {
415  LogDebug("LHEInputArgs") << "arg [" << iArg << "] = " << args[iArg];
416  }
417 
418  return args;
419 }

References writedatasetfile::args, args_, filterCSVwithJSON::copy, LogDebug, nEvents, runTheMatrix::nThreads, and SurveyInfoScenario_cff::seed.

Referenced by beginRunProduce().

◆ nextEvent()

void ExternalLHEProducer::nextEvent ( )
private

Definition at line 580 of file ExternalLHEProducer.cc.

580  {
581  if (partonLevel_)
582  return;
583 
584  if (not reader_) {
585  return;
586  }
587 
588  partonLevel_ = reader_->next();
589  if (!partonLevel_) {
590  //see if we have another file to read;
591  bool newFileOpened;
592  do {
593  newFileOpened = false;
594  partonLevel_ = reader_->next(&newFileOpened);
595  } while (newFileOpened && !partonLevel_);
596  }
597  if (!partonLevel_)
598  return;
599 
600  std::shared_ptr<lhef::LHERunInfo> runInfoThis = partonLevel_->getRunInfo();
601  if (runInfoThis != runInfoLast_) {
602  runInfo_ = runInfoThis;
603  runInfoLast_ = runInfoThis;
604  }
605 }

References partonLevel_, reader_, runInfo_, and runInfoLast_.

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

◆ preallocThreads()

void ExternalLHEProducer::preallocThreads ( unsigned int  iThreads)
overrideprivatevirtual

Reimplemented from edm::one::EDProducerBase.

Definition at line 168 of file ExternalLHEProducer.cc.

168 { nThreads_ = iThreads; }

References nThreads_.

◆ produce()

void ExternalLHEProducer::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivatevirtual

Implements edm::one::EDProducerBase.

Definition at line 171 of file ExternalLHEProducer.cc.

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

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

Member Data Documentation

◆ args_

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

◆ generateConcurrently_

bool ExternalLHEProducer::generateConcurrently_ {false}
private

Definition at line 97 of file ExternalLHEProducer.cc.

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

◆ nEvents_

uint32_t ExternalLHEProducer::nEvents_
private

Definition at line 93 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

◆ npars_

uint32_t ExternalLHEProducer::npars_
private

Definition at line 92 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer().

◆ nPartonMapping_

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

Definition at line 100 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer(), and produce().

◆ nThreads_

unsigned int ExternalLHEProducer::nThreads_ {1}
private

Definition at line 95 of file ExternalLHEProducer.cc.

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

◆ outputContents_

std::string ExternalLHEProducer::outputContents_
private

Definition at line 96 of file ExternalLHEProducer.cc.

◆ outputFile_

std::string ExternalLHEProducer::outputFile_
private

Definition at line 90 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and endRunProduce().

◆ partonLevel_

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

Definition at line 105 of file ExternalLHEProducer.cc.

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

◆ reader_

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

Definition at line 102 of file ExternalLHEProducer.cc.

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

◆ runInfo_

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

Definition at line 104 of file ExternalLHEProducer.cc.

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

◆ runInfoLast_

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

Definition at line 103 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and nextEvent().

◆ runInfoProducts_

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

Definition at line 106 of file ExternalLHEProducer.cc.

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

◆ scriptName_

std::string ExternalLHEProducer::scriptName_
private

Definition at line 89 of file ExternalLHEProducer.cc.

Referenced by executeScript().

◆ storeXML_

bool ExternalLHEProducer::storeXML_
private

Definition at line 94 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

◆ wasMerged

bool ExternalLHEProducer::wasMerged
private

Definition at line 107 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and produce().

writedatasetfile.args
args
Definition: writedatasetfile.py:18
dqmPostProcessing_online.DIR
DIR
Definition: dqmPostProcessing_online.py:10
cmsBatch.argv
argv
Definition: cmsBatch.py:279
mps_fire.i
i
Definition: mps_fire.py:355
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
ExternalLHEProducer::nEvents_
uint32_t nEvents_
Definition: ExternalLHEProducer.cc:93
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
dir2webdir.argc
argc
Definition: dir2webdir.py:39
mps_update.status
status
Definition: mps_update.py:69
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
np
int np
Definition: AMPTWrapper.h:43
ExternalLHEProducer::runInfo_
std::shared_ptr< lhef::LHERunInfo > runInfo_
Definition: ExternalLHEProducer.cc:104
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
edm::make_functor_task
FunctorTask< F > * make_functor_task(ALLOC &&iAlloc, F f)
Definition: FunctorTask.h:47
edm::errors::EventGenerationFailure
Definition: EDMException.h:70
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::RandomNumberGenerator::mySeed
virtual std::uint32_t mySeed() const =0
ExternalLHEProducer::closeDescriptors
int closeDescriptors(int preserve) const
Definition: ExternalLHEProducer.cc:422
lhe2HepMCConverter_cff.LHERunInfoProduct
LHERunInfoProduct
Definition: lhe2HepMCConverter_cff.py:5
LHERunInfoProduct::addHeader
void addHeader(const Header &header)
Definition: LHERunInfoProduct.h:52
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
lhe2HepMCConverter_cff.LHEEventProduct
LHEEventProduct
Definition: lhe2HepMCConverter_cff.py:4
ExternalLHEProducer::runInfoProducts_
boost::ptr_deque< LHERunInfoProduct > runInfoProducts_
Definition: ExternalLHEProducer.cc:106
edm::FileInPath
Definition: FileInPath.h:64
optionsL1T.skip
skip
Definition: optionsL1T.py:30
ztee.fd
fd
Definition: ztee.py:136
alignCSCRings.s
s
Definition: alignCSCRings.py:92
ExternalLHEProducer::makeArgs
std::vector< std::string > makeArgs(uint32_t nEvents, unsigned int nThreads, std::uint32_t seed) const
Definition: ExternalLHEProducer.cc:400
Calorimetry_cff.dp
dp
Definition: Calorimetry_cff.py:157
LHERunInfoProduct::addComment
void addComment(const std::string &line)
Definition: LHERunInfoProduct.h:53
TrackValidation_cff.task
task
Definition: TrackValidation_cff.py:252
ExternalLHEProducer::executeScript
void executeScript(std::vector< std::string > const &args, int id) const
Definition: ExternalLHEProducer.cc:459
runTheMatrix.nThreads
nThreads
Definition: runTheMatrix.py:344
OrderedSet.t
t
Definition: OrderedSet.py:90
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ExternalLHEProducer::args_
const std::vector< std::string > args_
Definition: ExternalLHEProducer.cc:91
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:674
edm::ParameterSetDescription::addUntracked
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:100
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:670
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
ExternalLHEProducer::scriptName_
std::string scriptName_
Definition: ExternalLHEProducer.cc:89
eventshapeDQM_cfi.order
order
Definition: eventshapeDQM_cfi.py:8
ExternalLHEProducer::outputFile_
std::string outputFile_
Definition: ExternalLHEProducer.cc:90
edm::Service< edm::RandomNumberGenerator >
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
iEvent
int iEvent
Definition: GenABIO.cc:224
ExternalLHEProducer::wasMerged
bool wasMerged
Definition: ExternalLHEProducer.cc:107
ExternalLHEProducer::nThreads_
unsigned int nThreads_
Definition: ExternalLHEProducer.cc:95
NULL
#define NULL
Definition: scimark2.h:8
ExternalLHEProducer::nPartonMapping_
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
Definition: ExternalLHEProducer.cc:100
looper.cfg
cfg
Definition: looper.py:297
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
eostools.move
def move(src, dest)
Definition: eostools.py:511
writedatasetfile.run
run
Definition: writedatasetfile.py:27
ExternalLHEProducer::generateConcurrently_
bool generateConcurrently_
Definition: ExternalLHEProducer.cc:97
Exception
Definition: hltDiff.cc:246
ExternalLHEProducer::reader_
std::unique_ptr< lhef::LHEReader > reader_
Definition: ExternalLHEProducer.cc:102
ExternalLHEProducer::npars_
uint32_t npars_
Definition: ExternalLHEProducer.cc:92
edm::ParameterSet::getParameterSetVector
VParameterSet const & getParameterSetVector(std::string const &name) const
Definition: ParameterSet.cc:2153
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
cms::Exception
Definition: Exception.h:70
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
ExternalLHEProducer::nextEvent
void nextEvent()
Definition: ExternalLHEProducer.cc:580
pipe.pipe
def pipe(cmdline, input=None)
Definition: pipe.py:5
LHEXMLStringProduct
Definition: LHEXMLStringProduct.h:12
nEvents
UInt_t nEvents
Definition: hcalCalib.cc:41
ExternalLHEProducer::storeXML_
bool storeXML_
Definition: ExternalLHEProducer.cc:94
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
ExternalLHEProducer::partonLevel_
std::shared_ptr< lhef::LHEEvent > partonLevel_
Definition: ExternalLHEProducer.cc:105
edm::ParameterSetDescription::addVPSetOptional
ParameterDescriptionBase * addVPSetOptional(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
Definition: ParameterSetDescription.h:163
ExternalLHEProducer::runInfoLast_
std::shared_ptr< lhef::LHERunInfo > runInfoLast_
Definition: ExternalLHEProducer.cc:103
SurveyInfoScenario_cff.seed
seed
Definition: SurveyInfoScenario_cff.py:295
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23
LHEEventProduct::addComment
void addComment(const std::string &line)
Definition: LHEEventProduct.h:32
LHEEventProduct::addWeight
void addWeight(const WGT &wgt)
Definition: LHEEventProduct.h:31