CMS 3D CMS Logo

BaseHadronizer.cc
Go to the documentation of this file.
1 
4 
5 #include <random>
6 #include <sys/wait.h>
7 
8 namespace gen {
9 
10  const std::vector<std::string> BaseHadronizer::theSharedResources;
11 
12  BaseHadronizer::BaseHadronizer(edm::ParameterSet const& ps) : randomIndex_(-1), gridpackPaths_(1) {
13  if (ps.exists("RandomizedParameters")) {
14  std::vector<edm::ParameterSet> randomizedParameters =
15  ps.getParameter<std::vector<edm::ParameterSet> >("RandomizedParameters");
16  randomInitWeights_.resize(randomizedParameters.size());
17  randomInitConfigDescriptions_.resize(randomizedParameters.size());
18  gridpackPaths_.resize(randomizedParameters.size());
19  for (unsigned int irand = 0; irand < randomizedParameters.size(); ++irand) {
20  randomInitWeights_[irand] = randomizedParameters[irand].getParameter<double>("ConfigWeight");
21  if (randomizedParameters[irand].exists("ConfigDescription")) {
23  randomizedParameters[irand].getParameter<std::string>("ConfigDescription");
24  }
25  if (randomizedParameters[irand].exists("GridpackPath")) {
26  gridpackPaths_[irand] = randomizedParameters[irand].getParameter<std::string>("GridpackPath");
27  }
28  }
29  } else {
30  if (ps.exists("GridpackPath")) {
31  gridpackPaths_[0] = ps.getParameter<std::string>("GridpackPath");
32  }
33  }
34 
35  runInfo().setFilterEfficiency(ps.getUntrackedParameter<double>("filterEfficiency", -1.));
36  runInfo().setExternalXSecLO(GenRunInfoProduct::XSec(ps.getUntrackedParameter<double>("crossSection", -1.)));
37  runInfo().setExternalXSecNLO(GenRunInfoProduct::XSec(ps.getUntrackedParameter<double>("crossSectionNLO", -1.)));
38  }
39 
40  std::unique_ptr<GenLumiInfoHeader> BaseHadronizer::getGenLumiInfoHeader() const {
41  auto genLumiInfoHeader = std::make_unique<GenLumiInfoHeader>();
42 
43  //fill information on randomized configs for parameter scans
44  genLumiInfoHeader->setRandomConfigIndex(randomIndex_);
45  if (randomIndex_ >= 0) {
46  genLumiInfoHeader->setConfigDescription(randomInitConfigDescription());
47  }
48 
49  return genLumiInfoHeader;
50  }
51 
52  void BaseHadronizer::randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine) {
53  if (!randomInitWeights_.empty()) {
54  //randomly select from a list of provided configuration sets (for parameter scans)
55 
56  //seeds std 32-bit mersene twister with HepRandomEngine state plus run and lumi section numbers
57  //(random engine state will be the same for every lumi section in a job)
58  std::vector<long unsigned int> seeds = rengine->put();
59  seeds.push_back(lumi.id().run());
60  seeds.push_back(lumi.id().luminosityBlock());
61  std::seed_seq seedseq(seeds.begin(), seeds.end());
62  std::mt19937 randgen(seedseq);
63  std::discrete_distribution<int> randdist(randomInitWeights_.begin(), randomInitWeights_.end());
64 
65  randomIndex_ = randdist(randgen);
66  }
67  }
68 
70  CLHEP::HepRandomEngine* rengine,
71  unsigned int ncpu) {
72  if (gridpackPath().empty()) {
73  return;
74  }
75 
76  //get random seed from HepRandomEngine state plus run and lumi section numbers
77  //(random engine state will be the same for every lumi section in a job)
78  std::vector<long unsigned int> seeds = rengine->put();
79  seeds.push_back(lumi.id().run());
80  seeds.push_back(lumi.id().luminosityBlock());
81  std::seed_seq seedseq(seeds.begin(), seeds.end());
82  std::array<unsigned int, 1> lheseed;
83  seedseq.generate(lheseed.begin(), lheseed.end());
84 
85  constexpr unsigned int maxseed = 30081 * 30081; //madgraph cannot handle seeds larger than this
86  unsigned int seedval = lheseed[0] % (maxseed + 1);
87 
88  unsigned int nevents = edm::pset::Registry::instance()
89  ->getMapped(lumi.processHistory().rbegin()->parameterSetID())
90  ->getParameter<edm::ParameterSet>("@main_input")
91  .getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock");
92 
93  std::ostringstream nevStream;
94  nevStream << nevents;
95 
96  std::ostringstream randomStream;
97  randomStream << seedval;
98 
99  edm::FileInPath script("GeneratorInterface/LHEInterface/data/run_generic_tarball_cvmfs.sh");
100  const char* outfilename = "cmsgrid_final.lhe";
101 
102  std::array<std::string, 5> argStrs;
103  argStrs[0] = script.fullPath();
104  argStrs[1] = gridpackPath();
105  argStrs[2] = nevStream.str();
106  argStrs[3] = randomStream.str();
107  argStrs[4] = std::to_string(ncpu);
108  std::array<char*, 6> args{
109  {&argStrs[0][0], &argStrs[1][0], &argStrs[2][0], &argStrs[3][0], &argStrs[4][0], nullptr}};
110 
111  pid_t pid = fork();
112 
113  if (pid == -1) {
114  // error, failed to fork()
115  throw cms::Exception("BaseHadronizer::generateLHE") << "Unable to fork a child";
116  } else if (pid == 0) {
117  //child
118  execvp(args[0], std::begin(args));
119  _exit(1); // exec never returns
120  } else {
121  //parent
122  int status;
123  waitpid(pid, &status, 0);
124  if (status) {
125  throw cms::Exception("BaseHadronizer::generateLHE") << "Failed to execute script";
126  }
127  }
128  FILE* lhef = std::fopen(outfilename, "r");
129  if (!lhef) {
130  throw cms::Exception("BaseHadronizer::generateLHE") << "Output file " << outfilename << " not found.";
131  }
132  std::fclose(lhef);
133 
135  }
136 
138  if (lheFile_.empty()) {
139  return;
140  }
141 
142  std::remove(lheFile_.c_str());
143  }
144 
145 } // namespace gen
cmsHarvester.nevents
nevents
Definition: cmsHarvester.py:3177
edm::pset::Registry::instance
static Registry * instance()
Definition: Registry.cc:12
writedatasetfile.args
args
Definition: writedatasetfile.py:18
gen::BaseHadronizer::randomInitConfigDescriptions_
std::vector< std::string > randomInitConfigDescriptions_
Definition: BaseHadronizer.h:111
genWeightsTable_cfi.genLumiInfoHeader
genLumiInfoHeader
Definition: genWeightsTable_cfi.py:5
gen::BaseHadronizer::getGenLumiInfoHeader
virtual std::unique_ptr< GenLumiInfoHeader > getGenLumiInfoHeader() const
Definition: BaseHadronizer.cc:40
edm::LuminosityBlock
Definition: LuminosityBlock.h:50
mps_update.status
status
Definition: mps_update.py:69
BaseHadronizer.h
gen::BaseHadronizer::gridpackPath
const std::string & gridpackPath() const
Definition: BaseHadronizer.h:78
GenRunInfoProduct::setFilterEfficiency
void setFilterEfficiency(double effic)
Definition: GenRunInfoProduct.h:29
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
submitPVResolutionJobs.script
string script
Definition: submitPVResolutionJobs.py:193
edm::FileInPath
Definition: FileInPath.h:64
gen::BaseHadronizer::randomizeIndex
void randomizeIndex(edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine)
Definition: BaseHadronizer.cc:52
SiStripCommissioningSource_FromRAW_cfg.outfilename
outfilename
Definition: SiStripCommissioningSource_FromRAW_cfg.py:122
gen::BaseHadronizer::lheFile_
std::string lheFile_
Definition: BaseHadronizer.h:92
gen::BaseHadronizer::randomInitWeights_
std::vector< double > randomInitWeights_
Definition: BaseHadronizer.h:110
gen
Definition: PythiaDecays.h:13
gen::BaseHadronizer::randomIndex_
int randomIndex_
Definition: BaseHadronizer.h:91
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
gen::BaseHadronizer::theSharedResources
static const std::vector< std::string > theSharedResources
Definition: BaseHadronizer.h:108
InitialStep_cff.seeds
seeds
Definition: InitialStep_cff.py:230
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
edm::ParameterSet
Definition: ParameterSet.h:47
GenRunInfoProduct::XSec
Definition: GenRunInfoProduct.h:32
lhef
Definition: ExhumeHadronizer.h:12
gen::BaseHadronizer::cleanLHE
void cleanLHE()
Definition: BaseHadronizer.cc:137
Registry.h
gen::BaseHadronizer::generateLHE
void generateLHE(edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine, unsigned int ncpu)
Definition: BaseHadronizer.cc:69
gen::BaseHadronizer::BaseHadronizer
BaseHadronizer(edm::ParameterSet const &ps)
Definition: BaseHadronizer.cc:12
relativeConstraints.empty
bool empty
Definition: relativeConstraints.py:46
Exception
Definition: hltDiff.cc:246
gen::BaseHadronizer::gridpackPaths_
std::vector< std::string > gridpackPaths_
Definition: BaseHadronizer.h:112
MatrixUtil.remove
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:219
gen::BaseHadronizer::randomInitConfigDescription
const std::string & randomInitConfigDescription() const
Definition: BaseHadronizer.h:77
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
GenRunInfoProduct::setExternalXSecLO
void setExternalXSecLO(const XSec &xsec)
Definition: GenRunInfoProduct.h:27
lumi
Definition: LumiSectionData.h:20
gen::BaseHadronizer::runInfo
GenRunInfoProduct & runInfo()
Definition: BaseHadronizer.h:85
edm::pset::Registry::getMapped
bool getMapped(key_type const &k, value_type &result) const
Definition: Registry.cc:17
GenRunInfoProduct::setExternalXSecNLO
void setExternalXSecNLO(const XSec &xsec)
Definition: GenRunInfoProduct.h:28