32 #include <boost/shared_ptr.hpp>
33 #include <boost/ptr_container/ptr_deque.hpp>
98 boost::shared_ptr<lhef::LHERunInfo>
runInfo;
129 scriptName_((iConfig.getParameter<edm::
FileInPath>(
"scriptName")).fullPath().c_str()),
130 outputFile_(iConfig.getParameter<std::string>(
"outputFile")),
131 args_(iConfig.getParameter<std::vector<std::string> >(
"args")),
132 npars_(iConfig.getParameter<uint32_t>(
"numberOfParameters")),
133 nEvents_(iConfig.getUntrackedParameter<uint32_t>(
"nEvents"))
136 throw cms::Exception(
"ExternalLHEProducer") <<
"Problem with configuration: " <<
args_.size() <<
" script arguments given, expected " <<
npars_;
137 produces<LHEXMLStringProduct, edm::InRun>(
"LHEScriptOutput");
139 produces<LHEEventProduct>();
140 produces<LHERunInfoProduct, edm::InRun>();
161 std::auto_ptr<LHEEventProduct> product(
184 std::for_each(
runInfo->getHeaders().begin(),
188 std::for_each(
runInfo->getComments().begin(),
222 std::ostringstream eventStream;
224 args_.push_back(eventStream.str());
232 <<
"The ExternalLHEProducer module requires the RandomNumberGeneratorService\n"
233 "which is not present in the configuration file. You must add the service\n"
234 "in the configuration file if you want to run ExternalLHEProducer";
236 std::ostringstream randomStream;
237 randomStream << rng->
mySeed();
238 args_.push_back(randomStream.str());
240 for (
unsigned int iArg = 0; iArg <
args_.size() ; iArg++ ) {
241 LogDebug(
"LHEInputArgs") <<
"arg [" << iArg <<
"] = " <<
args_[iArg];
252 instream.seekg (0, instream.end);
253 int insize = instream.tellg();
254 instream.seekg (0, instream.beg);
255 p->fillCompressedContent(instream, 0.25*insize);
257 run.
put(p,
"LHEScriptOutput");
263 unsigned int skip = 0;
264 std::auto_ptr<lhef::LHEReader> thisRead(
new lhef::LHEReader(infiles, skip ) );
272 std::for_each(
runInfo->getHeaders().begin(),
276 std::for_each(
runInfo->getComments().begin(),
298 std::auto_ptr<LHERunInfoProduct> product(
runInfoProducts.pop_front().release());
305 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
320 if ((dir = opendir(
"/proc/self/fd"))) {
322 while ((dp = readdir (dir)) !=
NULL) {
323 if ((strcmp(dp->d_name,
".") == 0) || (strcmp(dp->d_name,
"..") == 0)) {
326 if (sscanf(dp->d_name,
"%d", &fd) != 1) {
342 for (fd=3; fd<maxfd+1; fd++) {
357 int filedes[2], fd_flags;
365 if ((fd_flags = fcntl(filedes[1], F_GETFD,
NULL)) == -1) {
366 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to get pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
368 if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
369 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to set pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
372 argc = 1 +
args_.size();
374 char **
argv =
new char *[argc+1];
376 for (
unsigned int i=1;
i<
argc;
i++) {
377 argv[
i] = strdup(
args_[
i-1].c_str());
385 execvp(argv[0], argv);
388 while ((
write(filedes[1], &rc,
sizeof(
int)) == -1) && (errno == EINTR)) {}
393 for (
unsigned int i=0;
i<
args_.size()+1;
i++) {
399 throw cms::Exception(
"ForkException") <<
"Unable to fork a child (errno=" << errno <<
", " << strerror(errno) <<
")";
404 while (((rc2 =
read(filedes[0], &rc,
sizeof(
int))) == -1) && (errno == EINTR)) { rc2 = 0; }
405 if ((rc2 ==
sizeof(
int)) && rc) {
406 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to execute script (errno=" << rc <<
", " << strerror(rc) <<
")";
413 if (waitpid(pid, &status, 0) < 0) {
414 if (errno == EINTR) {
417 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to read child status (errno=" << errno <<
", " << strerror(errno) <<
")";
420 if (WIFSIGNALED(status)) {
421 throw cms::Exception(
"ExternalLHEProducer") <<
"Child exited due to signal " << WTERMSIG(status) <<
".";
423 if (WIFEXITED(status)) {
424 rc = WEXITSTATUS(status);
429 throw cms::Exception(
"ExternalLHEProducer") <<
"Child failed with exit code " << rc <<
".";
442 if ((fd = open(
outputFile_.c_str(), O_RDONLY)) == -1) {
443 throw cms::Exception(
"OutputOpenError") <<
"Unable to open script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
446 std::stringstream ss;
447 while ((n =
read(fd, buf,
BUFSIZE)) > 0 || (n == -1 && errno == EINTR)) {
452 throw cms::Exception(
"OutputOpenError") <<
"Unable to read from script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
456 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
459 return std::auto_ptr<std::string>(
new std::string(ss.str()));
480 desc.
setComment(
"Executes an external script and places its output file into an EDM collection");
484 desc.
add<std::string>(
"outputFile",
"myoutput");
485 desc.
add<std::vector<std::string> >(
"args");
486 desc.
add<uint32_t>(
"numberOfParameters");
502 boost::shared_ptr<lhef::LHERunInfo> runInfoThis =
partonLevel->getRunInfo();
boost::shared_ptr< lhef::LHEEvent > partonLevel
std::auto_ptr< std::string > readOutput()
virtual void produce(edm::Event &, const edm::EventSetup &)
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 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)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
virtual void beginRun(edm::Run &run, edm::EventSetup const &es)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::string outputContents_
boost::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
virtual void beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
void put(std::auto_ptr< PROD > product)
Put a new product.
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
virtual uint32_t mySeed() const =0
Exists for backward compatibility.
ExternalLHEProducer(const edm::ParameterSet &iConfig)
virtual void endLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
virtual void endRun(edm::Run &, edm::EventSetup const &)