31 #include <boost/shared_ptr.hpp>
32 #include <boost/ptr_container/ptr_deque.hpp>
65 edm::EndRunProducer> {
94 boost::shared_ptr<lhef::LHERunInfo>
runInfo;
125 scriptName_((iConfig.getParameter<edm::
FileInPath>(
"scriptName")).
fullPath().c_str()),
126 outputFile_(iConfig.getParameter<std::
string>(
"outputFile")),
127 args_(iConfig.getParameter<std::vector<std::
string> >(
"args")),
128 npars_(iConfig.getParameter<uint32_t>(
"numberOfParameters")),
129 nEvents_(iConfig.getUntrackedParameter<uint32_t>(
"nEvents"))
132 throw cms::Exception(
"ExternalLHEProducer") <<
"Problem with configuration: " <<
args_.size() <<
" script arguments given, expected " <<
npars_;
133 produces<LHEXMLStringProduct, edm::InRun>(
"LHEScriptOutput");
135 produces<LHEEventProduct>();
136 produces<LHERunInfoProduct, edm::InRun>();
156 <<
"The likely cause is that the lhe file contains fewer events than were requested, which is possible "
157 <<
"in case of phase space integration or uneweighting efficiency problems.";
160 std::auto_ptr<LHEEventProduct> product(
183 std::for_each(
runInfo->getHeaders().begin(),
187 std::for_each(
runInfo->getComments().begin(),
215 std::ostringstream eventStream;
218 args_.insert(
args_.begin() + 1, eventStream.str());
226 <<
"The ExternalLHEProducer module requires the RandomNumberGeneratorService\n"
227 "which is not present in the configuration file. You must add the service\n"
228 "in the configuration file if you want to run ExternalLHEProducer";
230 std::ostringstream randomStream;
231 randomStream << rng->
mySeed();
233 args_.insert(
args_.begin() + 2, randomStream.str());
235 for (
unsigned int iArg = 0; iArg <
args_.size() ; iArg++ ) {
236 LogDebug(
"LHEInputArgs") <<
"arg [" << iArg <<
"] = " <<
args_[iArg];
247 instream.seekg (0, instream.end);
248 int insize = instream.tellg();
249 instream.seekg (0, instream.beg);
250 p->fillCompressedContent(instream, 0.25*insize);
252 run.
put(p,
"LHEScriptOutput");
258 unsigned int skip = 0;
259 std::auto_ptr<lhef::LHEReader> thisRead(
new lhef::LHEReader(infiles, skip ) );
267 std::for_each(
runInfo->getHeaders().begin(),
271 std::for_each(
runInfo->getComments().begin(),
293 std::auto_ptr<LHERunInfoProduct> product(
runInfoProducts.pop_front().release());
300 <<
"Event loop is over, but there are still lhe events to process."
301 <<
"This could happen if lhe file contains more events than requested. This is never expected to happen.";
307 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
322 if ((dir = opendir(
"/proc/self/fd"))) {
324 while ((dp = readdir (dir)) !=
NULL) {
325 if ((strcmp(dp->d_name,
".") == 0) || (strcmp(dp->d_name,
"..") == 0)) {
328 if (sscanf(dp->d_name,
"%d", &fd) != 1) {
344 for (fd=3; fd<maxfd+1; fd++) {
359 int filedes[2], fd_flags;
367 if ((fd_flags = fcntl(filedes[1], F_GETFD,
NULL)) == -1) {
368 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to get pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
370 if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
371 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to set pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
374 argc = 1 +
args_.size();
376 char **
argv =
new char *[argc+1];
378 for (
unsigned int i=1;
i<
argc;
i++) {
379 argv[
i] = strdup(
args_[
i-1].c_str());
387 execvp(argv[0], argv);
390 while ((
write(filedes[1], &rc,
sizeof(
int)) == -1) && (errno == EINTR)) {}
395 for (
unsigned int i=0;
i<
args_.size()+1;
i++) {
401 throw cms::Exception(
"ForkException") <<
"Unable to fork a child (errno=" << errno <<
", " << strerror(errno) <<
")";
406 while (((rc2 =
read(filedes[0], &rc,
sizeof(
int))) == -1) && (errno == EINTR)) { rc2 = 0; }
407 if ((rc2 ==
sizeof(
int)) && rc) {
408 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to execute script (errno=" << rc <<
", " << strerror(rc) <<
")";
415 if (waitpid(pid, &status, 0) < 0) {
416 if (errno == EINTR) {
419 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to read child status (errno=" << errno <<
", " << strerror(errno) <<
")";
422 if (WIFSIGNALED(status)) {
423 throw cms::Exception(
"ExternalLHEProducer") <<
"Child exited due to signal " << WTERMSIG(status) <<
".";
425 if (WIFEXITED(status)) {
426 rc = WEXITSTATUS(status);
431 throw cms::Exception(
"ExternalLHEProducer") <<
"Child failed with exit code " << rc <<
".";
444 if ((fd = open(
outputFile_.c_str(), O_RDONLY)) == -1) {
445 throw cms::Exception(
"OutputOpenError") <<
"Unable to open script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
448 std::stringstream
ss;
449 while ((n =
read(fd, buf,
BUFSIZE)) > 0 || (n == -1 && errno == EINTR)) {
454 throw cms::Exception(
"OutputOpenError") <<
"Unable to read from script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
458 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
461 return std::auto_ptr<std::string>(
new std::string(ss.str()));
470 desc.
setComment(
"Executes an external script and places its output file into an EDM collection");
475 desc.
add<std::vector<std::string> >(
"args");
476 desc.
add<uint32_t>(
"numberOfParameters");
492 boost::shared_ptr<lhef::LHERunInfo> runInfoThis =
partonLevel->getRunInfo();
boost::shared_ptr< lhef::LHEEvent > partonLevel
std::auto_ptr< std::string > readOutput()
virtual void beginRunProduce(edm::Run &run, edm::EventSetup const &es) override
virtual void endRunProduce(edm::Run &, edm::EventSetup const &) override
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void addHeader(const Header &header)
void addComment(const std::string &line)
#define DEFINE_FWK_MODULE(type)
virtual void produce(edm::Event &, const edm::EventSetup &) override
void addWeight(const WGT &wgt)
std::vector< std::string > args_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::auto_ptr< lhef::LHEReader > reader_
boost::shared_ptr< lhef::LHERunInfo > runInfoLast
void setComment(std::string const &value)
int closeDescriptors(int preserve)
void addDefault(ParameterSetDescription const &psetDescription)
virtual std::uint32_t mySeed() const =0
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::string outputContents_
boost::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
virtual ~ExternalLHEProducer()
void put(std::auto_ptr< PROD > product)
Put a new product.
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
ExternalLHEProducer(const edm::ParameterSet &iConfig)