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 hasAbilityToProduceInBeginProcessBlocks () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndProcessBlocks () const final
 
bool hasAbilityToProduceInEndRuns () const final
 
bool wantsGlobalLuminosityBlocks () const final
 
bool wantsGlobalRuns () const final
 
bool wantsInputProcessBlocks () const final
 
bool wantsProcessBlocks () 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
 
std::vector< ESProxyIndex > const & esGetTokenIndicesVector (edm::Transition iTrans) const
 
std::vector< ESRecordIndex > const & esGetTokenRecordIndicesVector (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_
 
std::deque< std::unique_ptr< LHERunInfoProduct > > runInfoProducts_
 
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 = InEvent>
EDConsumerBaseAdaptor< B > consumes (edm::InputTag tag) noexcept
 
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<Transition Tr = Transition::Event>
constexpr auto esConsumes () noexcept
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes (ESInputTag const &tag)
 
template<Transition Tr = Transition::Event>
auto esConsumes (ESInputTag tag) noexcept
 
template<Transition Tr = Transition::Event>
ESGetTokenGeneric esConsumes (eventsetup::EventSetupRecordKey const &iRecord, eventsetup::DataKey const &iKey)
 Used with EventSetupRecord::doGet. More...
 
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 69 of file ExternalLHEProducer.cc.

Constructor & Destructor Documentation

◆ ExternalLHEProducer()

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

Definition at line 126 of file ExternalLHEProducer.cc.

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

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 255 of file ExternalLHEProducer.cc.

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

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, fileCollector::seed, optionsL1T::skip, storeXML_, AlCaHLTBitMon_QueryRunRegistry::string, submitPVValidationJobs::t, TrackValidation_cff::task, and wasMerged.

◆ closeDescriptors()

int ExternalLHEProducer::closeDescriptors ( int  preserve) const
private

Definition at line 426 of file ExternalLHEProducer.cc.

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

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 370 of file ExternalLHEProducer.cc.

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

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 submitPVValidationJobs::t.

◆ executeScript()

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

Definition at line 463 of file ExternalLHEProducer.cc.

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

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 559 of file ExternalLHEProducer.cc.

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

References edm::ParameterSetDescription::add(), edm::ConfigurationDescriptions::addDefault(), submitPVResolutionJobs::desc, 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 404 of file ExternalLHEProducer.cc.

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

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

Referenced by beginRunProduce().

◆ nextEvent()

void ExternalLHEProducer::nextEvent ( )
private

Definition at line 584 of file ExternalLHEProducer.cc.

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

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 171 of file ExternalLHEProducer.cc.

171 { nThreads_ = iThreads; }

References nThreads_.

◆ produce()

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

Implements edm::one::EDProducerBase.

Definition at line 174 of file ExternalLHEProducer.cc.

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

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 96 of file ExternalLHEProducer.cc.

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

◆ nEvents_

uint32_t ExternalLHEProducer::nEvents_
private

Definition at line 92 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

◆ npars_

uint32_t ExternalLHEProducer::npars_
private

Definition at line 91 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer().

◆ nPartonMapping_

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

Definition at line 99 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer(), and produce().

◆ nThreads_

unsigned int ExternalLHEProducer::nThreads_ {1}
private

Definition at line 94 of file ExternalLHEProducer.cc.

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

◆ outputContents_

std::string ExternalLHEProducer::outputContents_
private

Definition at line 95 of file ExternalLHEProducer.cc.

◆ outputFile_

std::string ExternalLHEProducer::outputFile_
private

Definition at line 89 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and endRunProduce().

◆ partonLevel_

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

Definition at line 104 of file ExternalLHEProducer.cc.

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

◆ reader_

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

Definition at line 101 of file ExternalLHEProducer.cc.

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

◆ runInfo_

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

Definition at line 103 of file ExternalLHEProducer.cc.

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

◆ runInfoLast_

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

Definition at line 102 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and nextEvent().

◆ runInfoProducts_

std::deque<std::unique_ptr<LHERunInfoProduct> > ExternalLHEProducer::runInfoProducts_
private

Definition at line 105 of file ExternalLHEProducer.cc.

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

◆ scriptName_

std::string ExternalLHEProducer::scriptName_
private

Definition at line 88 of file ExternalLHEProducer.cc.

Referenced by executeScript().

◆ storeXML_

bool ExternalLHEProducer::storeXML_
private

Definition at line 93 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

◆ wasMerged

bool ExternalLHEProducer::wasMerged
private

Definition at line 106 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:428
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
ExternalLHEProducer::nEvents_
uint32_t nEvents_
Definition: ExternalLHEProducer.cc:92
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:103
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:426
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
fileCollector.seed
seed
Definition: fileCollector.py:127
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:404
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:463
runTheMatrix.nThreads
nThreads
Definition: runTheMatrix.py:355
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ExternalLHEProducer::args_
const std::vector< std::string > args_
Definition: ExternalLHEProducer.cc:90
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:223
ExternalLHEProducer::runInfoProducts_
std::deque< std::unique_ptr< LHERunInfoProduct > > runInfoProducts_
Definition: ExternalLHEProducer.cc:105
ExternalLHEProducer::scriptName_
std::string scriptName_
Definition: ExternalLHEProducer.cc:88
eventshapeDQM_cfi.order
order
Definition: eventshapeDQM_cfi.py:8
ExternalLHEProducer::outputFile_
std::string outputFile_
Definition: ExternalLHEProducer.cc:89
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:106
ExternalLHEProducer::nThreads_
unsigned int nThreads_
Definition: ExternalLHEProducer.cc:94
NULL
#define NULL
Definition: scimark2.h:8
ExternalLHEProducer::nPartonMapping_
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
Definition: ExternalLHEProducer.cc:99
looper.cfg
cfg
Definition: looper.py:297
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
writedatasetfile.run
run
Definition: writedatasetfile.py:27
ExternalLHEProducer::generateConcurrently_
bool generateConcurrently_
Definition: ExternalLHEProducer.cc:96
Exception
Definition: hltDiff.cc:246
ExternalLHEProducer::reader_
std::unique_ptr< lhef::LHEReader > reader_
Definition: ExternalLHEProducer.cc:101
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ExternalLHEProducer::npars_
uint32_t npars_
Definition: ExternalLHEProducer.cc:91
edm::ParameterSet::getParameterSetVector
VParameterSet const & getParameterSetVector(std::string const &name) const
Definition: ParameterSet.cc:2160
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:584
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:93
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
ExternalLHEProducer::partonLevel_
std::shared_ptr< lhef::LHEEvent > partonLevel_
Definition: ExternalLHEProducer.cc:104
ExternalLHEProducer::runInfoLast_
std::shared_ptr< lhef::LHERunInfo > runInfoLast_
Definition: ExternalLHEProducer.cc:102
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