Skip to content

Commit 7f5e3eb

Browse files
committed
refactor seed setting for simulation
now also works in serial app
1 parent f012a0e commit 7f5e3eb

File tree

5 files changed

+76
-26
lines changed

5 files changed

+76
-26
lines changed

Common/Utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Set(HEADERS
1616
include/${MODULE_NAME}/RootChain.h
1717
include/${MODULE_NAME}/BoostSerializer.h
1818
include/${MODULE_NAME}/ShmManager.h
19+
include/${MODULE_NAME}/RngHelper.h
1920
)
2021

2122
Set(LINKDEF src/CommonUtilsLinkDef.h)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
/*
12+
* RngHelper.h
13+
*
14+
* Created on: Jun 18, 2018
15+
* Author: swenzel
16+
*/
17+
18+
#ifndef COMMON_UTILS_INCLUDE_COMMONUTILS_RNGHELPER_H_
19+
#define COMMON_UTILS_INCLUDE_COMMONUTILS_RNGHELPER_H_
20+
21+
#include <TRandom.h>
22+
#include <fcntl.h>
23+
24+
namespace o2
25+
{
26+
namespace utils
27+
{
28+
29+
// helper functions for random number (management)
30+
class RngHelper
31+
{
32+
public:
33+
// sets the state of the currently active ROOT gRandom Instance
34+
// if -1 (or negative) is given ... we will init with a random seed
35+
// returns seed set to TRandom
36+
static unsigned int setGRandomSeed(int seed = -1)
37+
{
38+
unsigned int s = seed < 0 ? readURandom<unsigned int>() : seed;
39+
gRandom->SetSeed(s);
40+
return s;
41+
}
42+
43+
// static function to get a true random number from /dev/urandom
44+
template <typename T>
45+
static T readURandom()
46+
{
47+
int randomDataHandle = open("/dev/urandom", O_RDONLY);
48+
if (randomDataHandle < 0) {
49+
// something went wrong
50+
} else {
51+
T seed;
52+
auto result = read(randomDataHandle, &seed, sizeof(T));
53+
if (result < 0) {
54+
// something went wrong
55+
}
56+
close(randomDataHandle);
57+
return seed;
58+
}
59+
return T(0);
60+
}
61+
};
62+
63+
} // namespace utils
64+
} // namespace o2
65+
66+
#endif /* COMMON_UTILS_INCLUDE_COMMONUTILS_RNGHELPER_H_ */

Common/Utils/src/CommonUtilsLinkDef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
#pragma link C++ class o2::utils::TreeStream+;
1919
#pragma link C++ class o2::utils::TreeStreamRedirector+;
2020
#pragma link C++ class o2::utils::RootChain + ;
21+
#pragma link C++ class o2::utils::RngHelper;
2122

2223
#endif

macro/o2sim.C

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <Generators/PDG.h>
1616
#include <SimConfig/SimConfig.h>
1717
#include <SimConfig/ConfigurableParam.h>
18+
#include <CommonUtils/RngHelper.h>
1819
#include <TStopwatch.h>
1920
#include <memory>
2021
#include "DataFormatsParameters/GRPObject.h"
@@ -40,8 +41,11 @@ FairRunSim* o2sim_init(bool asservice)
4041
// we can update the binary CCDB entry something like this ( + timestamp key )
4142
// o2::conf::ConfigurableParam::toCCDB("params_ccdb.root");
4243

43-
auto genconfig = confref.getGenerator();
44+
// set seed
45+
auto seed = o2::utils::RngHelper::setGRandomSeed(confref.getStartSeed());
46+
LOG(INFO) << "RNG INITIAL SEED " << seed;
4447

48+
auto genconfig = confref.getGenerator();
4549
FairRunSim* run;
4650
if (asservice) {
4751
run = new o2::steer::O2RunSim();

run/O2PrimaryServerDevice.h

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#include <SimulationDataFormat/PrimaryChunk.h>
2525
#include <Generators/GeneratorFromFile.h>
2626
#include <SimConfig/SimConfig.h>
27+
#include <CommonUtils/RngHelper.h>
2728
#include <typeinfo>
2829
#include <thread>
2930
#include <TROOT.h>
30-
#include <fcntl.h>
3131

3232
namespace o2
3333
{
@@ -76,12 +76,8 @@ class O2PrimaryServerDevice : public FairMQDevice
7676

7777
// initial initial seed --> we should store this somewhere
7878
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);
79+
mInitialSeed = o2::utils::RngHelper::setGRandomSeed(mInitialSeed);
80+
LOG(INFO) << "RNG INITIAL SEED " << mInitialSeed;
8581

8682
mMaxEvents = conf.getNEvents();
8783

@@ -211,24 +207,6 @@ class O2PrimaryServerDevice : public FairMQDevice
211207
}
212208

213209
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-
232210
std::string mOutChannelName = "";
233211
FairPrimaryGenerator mPrimGen;
234212
FairMCEventHeader mEventHeader;

0 commit comments

Comments
 (0)