31 #include <sys/resource.h> 34 #include "boost/bind.hpp" 35 #include "boost/shared_ptr.hpp" 36 #include "boost/ptr_container/ptr_deque.hpp" 70 edm::EndRunProducer> {
133 scriptName_((iConfig.getParameter<
edm::FileInPath>(
"scriptName")).fullPath()),
136 npars_(iConfig.getParameter<uint32_t>(
"numberOfParameters")),
137 nEvents_(iConfig.getUntrackedParameter<uint32_t>(
"nEvents")),
141 throw cms::Exception(
"ExternalLHEProducer") <<
"Problem with configuration: " <<
args_.size() <<
" script arguments given, expected " <<
npars_;
142 produces<LHEXMLStringProduct, edm::Transition::BeginRun>(
"LHEScriptOutput");
144 produces<LHEEventProduct>();
145 produces<LHERunInfoProduct, edm::Transition::BeginRun>();
146 produces<LHERunInfoProduct, edm::Transition::EndRun>();
172 throw cms::Exception(
"ExternalLHEProducer") <<
"No lhe event found in ExternalLHEProducer::produce(). " 173 <<
"The likely cause is that the lhe file contains fewer events than were requested, which is possible " 174 <<
"in case of phase space integration or uneweighting efficiency problems.";
177 std::unique_ptr<LHEEventProduct> product(
200 std::for_each(
runInfo->getHeaders().begin(),
204 std::for_each(
runInfo->getComments().begin(),
232 std::ostringstream eventStream;
235 args_.insert(
args_.begin() + 1, eventStream.str());
243 <<
"The ExternalLHEProducer module requires the RandomNumberGeneratorService\n" 244 "which is not present in the configuration file. You must add the service\n" 245 "in the configuration file if you want to run ExternalLHEProducer";
247 std::ostringstream randomStream;
248 randomStream << rng->
mySeed();
250 args_.insert(
args_.begin() + 2, randomStream.str());
255 for (
unsigned int iArg = 0; iArg <
args_.size() ; iArg++ ) {
256 LogDebug(
"LHEInputArgs") <<
"arg [" << iArg <<
"] = " <<
args_[iArg];
270 instream.seekg (0, instream.end);
271 int insize = instream.tellg();
272 instream.seekg (0, instream.beg);
273 p->fillCompressedContent(instream, 0.25*insize);
282 unsigned int skip = 0;
283 reader_ = std::make_unique<lhef::LHEReader>(infiles,
skip);
290 std::for_each(
runInfo->getHeaders().begin(),
294 std::for_each(
runInfo->getComments().begin(),
316 std::unique_ptr<LHERunInfoProduct> product(
runInfoProducts.pop_front().release());
322 throw cms::Exception(
"ExternalLHEProducer") <<
"Error in ExternalLHEProducer::endRunProduce(). " 323 <<
"Event loop is over, but there are still lhe events to process." 324 <<
"This could happen if lhe file contains more events than requested. This is never expected to happen.";
330 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
345 if ((dir = opendir(
"/proc/self/fd"))) {
347 while ((dp = readdir (dir)) !=
nullptr) {
348 if ((strcmp(dp->d_name,
".") == 0) || (strcmp(dp->d_name,
"..") == 0)) {
351 if (sscanf(dp->d_name,
"%d", &fd) != 1) {
367 for (fd=3; fd<maxfd+1; fd++) {
382 int filedes[2], fd_flags;
390 if ((fd_flags = fcntl(filedes[1], F_GETFD,
NULL)) == -1) {
391 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to get pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
393 if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
394 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to set pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
397 argc = 1 +
args_.size();
399 char **
argv =
new char *[argc+1];
401 for (
unsigned int i=1;
i<
argc;
i++) {
402 argv[
i] = strdup(
args_[
i-1].c_str());
404 argv[
argc] =
nullptr;
410 execvp(argv[0], argv);
413 while ((
write(filedes[1], &rc,
sizeof(
int)) == -1) && (errno == EINTR)) {}
418 for (
unsigned int i=0;
i<
args_.size()+1;
i++) {
424 throw cms::Exception(
"ForkException") <<
"Unable to fork a child (errno=" << errno <<
", " << strerror(errno) <<
")";
429 while (((rc2 = read(filedes[0], &rc,
sizeof(
int))) == -1) && (errno == EINTR)) { rc2 = 0; }
430 if ((rc2 ==
sizeof(
int)) && rc) {
431 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to execute script (errno=" << rc <<
", " << strerror(rc) <<
")";
438 if (waitpid(pid, &status, 0) < 0) {
439 if (errno == EINTR) {
442 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to read child status (errno=" << errno <<
", " << strerror(errno) <<
")";
445 if (WIFSIGNALED(status)) {
446 throw cms::Exception(
"ExternalLHEProducer") <<
"Child exited due to signal " << WTERMSIG(status) <<
".";
448 if (WIFEXITED(status)) {
449 rc = WEXITSTATUS(status);
456 getrusage(RUSAGE_CHILDREN,&ru);
457 double time =
static_cast<double>(ru.ru_stime.tv_sec) + (static_cast<double>(ru.ru_stime.tv_usec) * 1E-6);
461 throw cms::Exception(
"ExternalLHEProducer") <<
"Child failed with exit code " << rc <<
".";
474 if ((fd = open(
outputFile_.c_str(), O_RDONLY)) == -1) {
475 throw cms::Exception(
"OutputOpenError") <<
"Unable to open script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
478 std::stringstream ss;
479 while ((n = read(fd, buf,
BUFSIZE)) > 0 || (n == -1 && errno == EINTR)) {
484 throw cms::Exception(
"OutputOpenError") <<
"Unable to read from script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
488 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
491 return std::unique_ptr<std::string>(
new std::string(ss.str()));
500 desc.
setComment(
"Executes an external script and places its output file into an EDM collection");
505 desc.
add<std::vector<std::string> >(
"args");
506 desc.
add<uint32_t>(
"numberOfParameters");
524 boost::shared_ptr<lhef::LHERunInfo> runInfoThis =
partonLevel->getRunInfo();
boost::shared_ptr< lhef::LHEEvent > partonLevel
void beginRunProduce(edm::Run &run, edm::EventSetup const &es) override
void endRunProduce(edm::Run &, edm::EventSetup const &) override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
std::unique_ptr< std::string > readOutput()
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void addHeader(const Header &header)
void addComment(const std::string &line)
#define DEFINE_FWK_MODULE(type)
void produce(edm::Event &, const edm::EventSetup &) override
void addWeight(const WGT &wgt)
std::vector< std::string > args_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual void addToCPUTime(double iTime)=0
boost::shared_ptr< lhef::LHERunInfo > runInfoLast
void setComment(std::string const &value)
int closeDescriptors(int preserve)
void addDefault(ParameterSetDescription const &psetDescription)
std::unique_ptr< lhef::LHEReader > reader_
def pipe(cmdline, input=None)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual std::uint32_t mySeed() const =0
std::string outputContents_
boost::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
~ExternalLHEProducer() override
void preallocThreads(unsigned int) override
void put(std::unique_ptr< PROD > product)
Put a new product.
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
ExternalLHEProducer(const edm::ParameterSet &iConfig)