Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sherpa 2.2.4 backport with OpenMPI for 7_1_X #21751

Merged
merged 1 commit into from Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion GeneratorInterface/SherpaInterface/BuildFile.xml
Expand Up @@ -5,7 +5,6 @@
<use name="GeneratorInterface/Core"/>
<use name="GeneratorInterface/ExternalDecays"/>
<use name="GeneratorInterface/Pythia6Interface"/>
<use name="boost"/>
<use name="clhep"/>
<use name="sherpa"/>
<use name="FWCore/Services"/>
Expand Down
Expand Up @@ -5,7 +5,7 @@
#include <sstream>
#include <string>
#include <memory>
#include <stdint.h>
#include <cstdint>
#include <fstream>

#include "FWCore/Framework/interface/Frameworkfwd.h"
Expand Down
Expand Up @@ -8,11 +8,11 @@
* version 1.0, 1st August 2012
*/

#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <string.h>
#include <assert.h>
#include <cstring>
#include <cassert>
#include <zlib.h>
/* This is for mkdir(); this may need to be changed for some platforms. */
#include <sys/stat.h> /* For mkdir() */
Expand Down
99 changes: 50 additions & 49 deletions GeneratorInterface/SherpaInterface/src/SherpaHadronizer.cc
Expand Up @@ -3,7 +3,7 @@
#include <sstream>
#include <string>
#include <memory>
#include <stdint.h>
#include <cstdint>
#include <vector>


Expand All @@ -12,6 +12,10 @@

#include "ATOOLS/Org/Run_Parameter.H"
#include "ATOOLS/Org/MyStrStream.H"
#include "ATOOLS/Org/CXXFLAGS.H"
#include "ATOOLS/Org/CXXFLAGS_PACKAGES.H"
#include "ATOOLS/Org/My_MPI.H"


#include "GeneratorInterface/Core/interface/ParameterCollector.h"
#include "GeneratorInterface/Core/interface/BaseHadronizer.h"
Expand Down Expand Up @@ -46,14 +50,16 @@ class SherpaHadronizer : public gen::BaseHadronizer {
void statistics();
bool generatePartonsAndHadronize();
bool decay();
bool rearrangeWeights;
bool residualDecay();
void finalizeEvent();
GenLumiInfoHeader *getGenLumiInfoHeader() const override;
const char *classname() const { return "SherpaHadronizer"; }


private:

virtual void doSetRandomEngine(CLHEP::HepRandomEngine* v) override;
void doSetRandomEngine(CLHEP::HepRandomEngine* v) override;

std::string SherpaProcess;
std::string SherpaChecksum;
Expand All @@ -64,10 +70,10 @@ class SherpaHadronizer : public gen::BaseHadronizer {
edm::ParameterSet SherpaParameterSet;
unsigned int maxEventsToPrint;
std::vector<std::string> arguments;
SHERPA::Sherpa Generator;
SHERPA::Sherpa *Generator = new SHERPA::Sherpa();
bool isInitialized;
bool isRNGinitialized;
bool rearrangeWeights;
// bool rearrangeWeights;
std::vector<std::string> weightlist;
std::vector<std::string> variationweightlist;
};
Expand Down Expand Up @@ -185,17 +191,23 @@ SherpaHadronizer::SherpaHadronizer(const edm::ParameterSet &params) :
std::string shRng = "EXTERNAL_RNG=CMS_SHERPA_RNG";

//create the command line
arguments.push_back(shRun.c_str());
arguments.push_back(shPath.c_str());
arguments.push_back(shPathPiece.c_str());
arguments.push_back(shRes.c_str());
arguments.push_back(shRng.c_str());
arguments.push_back(shRun);
arguments.push_back(shPath);
arguments.push_back(shPathPiece);
arguments.push_back(shRes);
arguments.push_back(shRng);

isInitialized=false;
//initialization of Sherpa moved to initializeForInternalPartons
}

SherpaHadronizer::~SherpaHadronizer()
{
Generator->~Sherpa();
#ifdef USING__MPI
MPI::Finalize();
#endif

}

bool SherpaHadronizer::initializeForInternalPartons()
Expand All @@ -205,58 +217,34 @@ bool SherpaHadronizer::initializeForInternalPartons()
int argc=arguments.size();
char* argv[argc];
for (int l=0; l<argc; l++) argv[l]=(char*)arguments[l].c_str();
Generator.InitializeTheRun(argc,argv);
Generator.InitializeTheEventHandler();
#ifdef USING__MPI
MPI::Init();
#endif
Generator->InitializeTheRun(argc,argv);
Generator->InitializeTheEventHandler();
isInitialized=true;
}
return true;
}

#if 0
// naive Sherpa HepMC status fixup //FIXME
static int getStatus(const HepMC::GenParticle *p)
{
return status;
}
#endif

//FIXME
bool SherpaHadronizer::declareStableParticles(const std::vector<int> &pdgIds)
{
#if 0
for(std::vector<int>::const_iterator iter = pdgIds.begin();
iter != pdgIds.end(); ++iter)
if (!markStable(*iter))
return false;

return true;
#else
return false;
#endif
}


void SherpaHadronizer::statistics()
{
//calculate statistics
Generator.SummarizeRun();
Generator->SummarizeRun();

//get the xsec & err
double xsec_val = Generator.TotalXS();
double xsec_err = Generator.TotalErr();
double xsec_val = Generator->TotalXS();
double xsec_err = Generator->TotalErr();

//set the internal cross section in pb in GenRunInfoProduct
runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec_val,xsec_err));

if(rearrangeWeights){
edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;
for(auto &i: weightlist){
edm::LogVerbatim("SherpaHadronizer") << i;
}
for(auto &i: variationweightlist) {
edm::LogVerbatim("SherpaHadronizer") << i;
}
}

}

Expand All @@ -269,18 +257,18 @@ bool SherpaHadronizer::generatePartonsAndHadronize()
bool gen_event = true;
while((itry < 3) && gen_event){
try{
rc = Generator.GenerateOneEvent();
rc = Generator->GenerateOneEvent();
gen_event = false;
} catch(...){
++itry;
std::cerr << "Exception from Generator.GenerateOneEvent() catch. Call # "
std::cerr << "Exception from Generator->GenerateOneEvent() catch. Call # "
<< itry << " for this event\n";
}
}
if (rc) {
//convert it to HepMC2
HepMC::GenEvent* evt = new HepMC::GenEvent();
Generator.FillHepMCEvent(*evt);
Generator->FillHepMCEvent(*evt);

// in case of unweighted events sherpa puts the max weight as event weight.
// this is not optimal, we want 1 for unweighted events, so we check
Expand Down Expand Up @@ -355,11 +343,6 @@ bool SherpaHadronizer::residualDecay()

void SherpaHadronizer::finalizeEvent()
{
#if 0
for(HepMC::GenEvent::particle_iterator iter = event->particles_begin();
iter != event->particles_end(); iter++)
(*iter)->set_status(getStatus(*iter));
#endif
//******** Verbosity *******
if (maxEventsToPrint > 0) {
maxEventsToPrint--;
Expand Down Expand Up @@ -389,6 +372,24 @@ double CMS_SHERPA_RNG::Get() {

}

GenLumiInfoHeader *SherpaHadronizer::getGenLumiInfoHeader() const {
GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();

if(rearrangeWeights){
edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;
for(auto &i: weightlist){
genLumiInfoHeader->weightNames().push_back(i);
edm::LogVerbatim("SherpaHadronizer") << i;
}
for(auto &i: variationweightlist) {
genLumiInfoHeader->weightNames().push_back(i);
edm::LogVerbatim("SherpaHadronizer") << i;
}
}
return genLumiInfoHeader;
}


#include "GeneratorInterface/ExternalDecays/interface/ExternalDecayDriver.h"

typedef edm::GeneratorFilter<SherpaHadronizer, gen::ExternalDecayDriver> SherpaGeneratorFilter;
Expand Down
29 changes: 20 additions & 9 deletions GeneratorInterface/SherpaInterface/src/SherpackUtilities.cc
@@ -1,5 +1,6 @@
#include "GeneratorInterface/SherpaInterface/interface/SherpackUtilities.h"
#include <unistd.h>
#include <cstdlib>
namespace spu {

// functions for inflating (and deflating)
Expand Down Expand Up @@ -152,7 +153,17 @@ void zerr(int ret)

/* compress or decompress from stdin to stdout */
int Unzip(std::string infile, std::string outfile)
{
{ /////////////////////////////////////////////
/////////////// BUG FIX FOR MPI /////////////
/////////////////////////////////////////////
const char *tmpdir = getenv("TMPDIR");
if (tmpdir && (strlen(tmpdir) > 50)) {
setenv("TMPDIR", "/tmp", true);
}
/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////

int ret;
FILE *in = fopen(infile.c_str(),"r");
if (!in) return -1;
Expand Down Expand Up @@ -215,7 +226,7 @@ void create_dir(char *pathname, int mode) {
if (r != 0) {
/* On failure, try creating parent directory. */
p = strrchr(pathname, '/');
if (p != NULL) {
if (p != nullptr) {
*p = '\0';
create_dir(pathname, 0755);
*p = '/';
Expand All @@ -230,10 +241,10 @@ void create_dir(char *pathname, int mode) {
FILE* create_file(char *pathname, int mode) {
FILE *f;
f = fopen(pathname, "w+");
if (f == NULL) {
if (f == nullptr) {
/* Try creating parent dir and then creating file. */
char *p = strrchr(pathname, '/');
if (p != NULL) {
if (p != nullptr) {
*p = '\0';
create_dir(pathname, 0755);
*p = '/';
Expand Down Expand Up @@ -264,7 +275,7 @@ void Untar(FILE *a, const char *path) {
char newlongpathname[512];
char newlonglinkname[512];
char buff[512];
FILE *f = NULL;
FILE *f = nullptr;
size_t bytes_read;
int filesize;

Expand Down Expand Up @@ -456,20 +467,20 @@ void Untar(FILE *a, const char *path) {
}
if (filesize < 512)
bytes_read = filesize;
if (f != NULL) {
if (f != nullptr) {
if (fwrite(buff, 1, bytes_read, f)
!= bytes_read)
{
fprintf(stderr, "Failed write\n");
fclose(f);
f = NULL;
f = nullptr;
}
}
filesize -= bytes_read;
}
if (f != NULL) {
if (f != nullptr) {
fclose(f);
f = NULL;
f = nullptr;
}
}
}
Expand Down