Skip to content

Commit 7eded60

Browse files
committed
Possibility to set seed for simulation
Allow to set the seed for simulation. By default a random seed will be chosen at each start. If reproducible tests are needed ... set the seed with the --seed option. For the moment, change restricted to o2sim_parallel.
1 parent b1bb9cf commit 7eded60

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Common/SimConfig/include/SimConfig/SimConfig.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ struct SimConfigData {
3939
// (can be used to **loosly** change any configuration parameter from
4040
// command-line)
4141
int mPrimaryChunkSize; // defining max granularity for input primaries of a sim job
42-
int mInternalChunkSize;
43-
ClassDefNV(SimConfigData, 1);
42+
int mInternalChunkSize; //
43+
int mStartSeed; // base for random number seeds
44+
45+
ClassDefNV(SimConfigData, 2);
4446
};
4547

4648
// A singleton class which can be used
@@ -97,6 +99,7 @@ class SimConfig
9799
std::string getKeyValueString() const { return mConfigData.mKeyValueTokens; }
98100
int getPrimChunkSize() const { return mConfigData.mPrimaryChunkSize; }
99101
int getInternalChunkSize() const { return mConfigData.mInternalChunkSize; }
102+
int getStartSeed() const { return mConfigData.mStartSeed; }
100103

101104
private:
102105
SimConfigData mConfigData; //!

Common/SimConfig/src/SimConfig.cxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ void SimConfig::initOptions(boost::program_options::options_description& options
3636
"outPrefix,o", bpo::value<std::string>()->default_value("o2sim"), "prefix of output files")(
3737
"logseverity", bpo::value<std::string>()->default_value("INFO"), "severity level for FairLogger")(
3838
"logverbosity", bpo::value<std::string>()->default_value("low"), "level of verbosity for FairLogger (low, medium, high, veryhigh)")(
39-
"configKeyValues", bpo::value<std::string>()->default_value(""), "comma separated key=value strings (e.g.: 'TPC.gasDensity=1,...")("chunkSize", bpo::value<unsigned int>()->default_value(10000), "max size of primary chunk (subevent) distributed by server")("chunkSizeI", bpo::value<int>()->default_value(-1), "internalChunkSize");
39+
"configKeyValues", bpo::value<std::string>()->default_value(""), "comma separated key=value strings (e.g.: 'TPC.gasDensity=1,...")("chunkSize", bpo::value<unsigned int>()->default_value(10000), "max size of primary chunk (subevent) distributed by server")(
40+
"chunkSizeI", bpo::value<int>()->default_value(-1), "internalChunkSize")(
41+
"seed", bpo::value<int>()->default_value(-1), "initial seed (default: -1 random)");
4042
}
4143

4244
bool SimConfig::resetFromParsedMap(boost::program_options::variables_map const& vm)
@@ -82,6 +84,7 @@ bool SimConfig::resetFromParsedMap(boost::program_options::variables_map const&
8284
mConfigData.mKeyValueTokens = vm["configKeyValues"].as<std::string>();
8385
mConfigData.mPrimaryChunkSize = vm["chunkSize"].as<unsigned int>();
8486
mConfigData.mInternalChunkSize = vm["chunkSizeI"].as<int>();
87+
mConfigData.mStartSeed = vm["seed"].as<int>();
8588
return true;
8689
}
8790

run/O2PrimaryServerDevice.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <typeinfo>
2828
#include <thread>
2929
#include <TROOT.h>
30+
#include <fcntl.h>
3031

3132
namespace o2
3233
{
@@ -73,6 +74,15 @@ class O2PrimaryServerDevice : public FairMQDevice
7374
mChunkGranularity = vm["chunkSize"].as<unsigned int>();
7475
LOG(INFO) << "CHUNK SIZE SET TO " << mChunkGranularity;
7576

77+
// initial initial seed --> we should store this somewhere
78+
mInitialSeed = vm["seed"].as<int>();
79+
if (mInitialSeed == -1) {
80+
mInitialSeed = getRandomSeed();
81+
}
82+
LOG(INFO) << "INITIAL SEED " << mInitialSeed;
83+
// set seed here ... in order to influence already event generation
84+
gRandom->SetSeed(mInitialSeed);
85+
7686
mMaxEvents = conf.getNEvents();
7787

7888
// need to make ROOT thread-safe since we use ROOT services in all places
@@ -151,7 +161,7 @@ class O2PrimaryServerDevice : public FairMQDevice
151161
i.maxEvents = mMaxEvents;
152162
i.part = mPartCounter + 1;
153163
i.nparts = numberofparts;
154-
i.seed = counter + 10;
164+
i.seed = counter + mInitialSeed;
155165
i.index = m.mParticles.size();
156166
m.mEventIDs.emplace_back(i);
157167

@@ -201,6 +211,24 @@ class O2PrimaryServerDevice : public FairMQDevice
201211
}
202212

203213
private:
214+
// helper function to get truly random seed
215+
int getRandomSeed() const
216+
{
217+
int randomDataHandle = open("/dev/urandom", O_RDONLY);
218+
if (randomDataHandle < 0) {
219+
// something went wrong
220+
} else {
221+
int seed;
222+
auto result = read(randomDataHandle, &seed, sizeof(seed));
223+
if (result < 0) {
224+
// something went wrong
225+
}
226+
close(randomDataHandle);
227+
return seed;
228+
}
229+
return 0;
230+
}
231+
204232
std::string mOutChannelName = "";
205233
FairPrimaryGenerator mPrimGen;
206234
FairMCEventHeader mEventHeader;
@@ -210,6 +238,7 @@ class O2PrimaryServerDevice : public FairMQDevice
210238
int mPartCounter = 0;
211239
bool mNeedNewEvent = true;
212240
int mMaxEvents = 2;
241+
int mInitialSeed = -1;
213242

214243
std::thread mGeneratorInitThread; //! a thread used to concurrently init the particle generator
215244
};

0 commit comments

Comments
 (0)