-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathSimConfig.cxx
More file actions
268 lines (246 loc) · 13.1 KB
/
SimConfig.cxx
File metadata and controls
268 lines (246 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include <SimConfig/SimConfig.h>
#include <DetectorsCommonDataFormats/DetID.h>
#include <boost/program_options.hpp>
#include <iostream>
#include <FairLogger.h>
#include <thread>
#include <cmath>
#include <chrono>
#include <regex>
using namespace o2::conf;
namespace bpo = boost::program_options;
void SimConfig::initOptions(boost::program_options::options_description& options)
{
int nsimworkersdefault = std::max(1u, std::thread::hardware_concurrency() / 2);
options.add_options()(
"mcEngine,e", bpo::value<std::string>()->default_value("TGeant3"), "VMC backend to be used.")(
"generator,g", bpo::value<std::string>()->default_value("boxgen"), "Event generator to be used.")(
"trigger,t", bpo::value<std::string>()->default_value(""), "Event generator trigger to be used.")(
"modules,m", bpo::value<std::vector<std::string>>()->multitoken()->default_value(std::vector<std::string>({"all"}), "all modules"), "list of modules included in geometry")(
"skipModules", bpo::value<std::vector<std::string>>()->multitoken()->default_value(std::vector<std::string>({""}), ""), "list of modules excluded in geometry (precendence over -m")(
"readoutDetectors", bpo::value<std::vector<std::string>>()->multitoken()->default_value(std::vector<std::string>(), ""), "list of detectors creating hits, all if not given; added to to active modules")(
"skipReadoutDetectors", bpo::value<std::vector<std::string>>()->multitoken()->default_value(std::vector<std::string>(), ""), "list of detectors to skip hit creation (precendence over --readoutDetectors")(
"nEvents,n", bpo::value<unsigned int>()->default_value(1), "number of events")(
"startEvent", bpo::value<unsigned int>()->default_value(0), "index of first event to be used (when applicable)")(
"extKinFile", bpo::value<std::string>()->default_value("Kinematics.root"),
"name of kinematics file for event generator from file (when applicable)")(
"embedIntoFile", bpo::value<std::string>()->default_value(""),
"filename containing the reference events to be used for the embedding")(
"bMax,b", bpo::value<float>()->default_value(0.), "maximum value for impact parameter sampling (when applicable)")(
"isMT", bpo::value<bool>()->default_value(false), "multi-threaded mode (Geant4 only")(
"outPrefix,o", bpo::value<std::string>()->default_value("o2sim"), "prefix of output files")(
"logseverity", bpo::value<std::string>()->default_value("INFO"), "severity level for FairLogger")(
"logverbosity", bpo::value<std::string>()->default_value("medium"), "level of verbosity for FairLogger (low, medium, high, veryhigh)")(
"configKeyValues", bpo::value<std::string>()->default_value(""), "semicolon separated key=value strings (e.g.: 'TPC.gasDensity=1;...")(
"configFile", bpo::value<std::string>()->default_value(""), "Path to an INI or JSON configuration file")(
"chunkSize", bpo::value<unsigned int>()->default_value(500), "max size of primary chunk (subevent) distributed by server")(
"chunkSizeI", bpo::value<int>()->default_value(-1), "internalChunkSize")(
"seed", bpo::value<int>()->default_value(-1), "initial seed (default: -1 random)")(
"field", bpo::value<std::string>()->default_value("-5"), "L3 field rounded to kGauss, allowed values +-2,+-5 and 0; +-<intKGaus>U for uniform field; \"ccdb\" for taking it from CCDB ")(
"nworkers,j", bpo::value<int>()->default_value(nsimworkersdefault), "number of parallel simulation workers (only for parallel mode)")(
"noemptyevents", "only writes events with at least one hit")(
"CCDBUrl", bpo::value<std::string>()->default_value("http://alice-ccdb.cern.ch"), "URL for CCDB to be used.")(
"timestamp", bpo::value<uint64_t>(), "global timestamp value in ms (for anchoring) - default is now")(
"asservice", bpo::value<bool>()->default_value(false), "run in service/server mode")(
"noGeant", bpo::bool_switch(), "prohibits any Geant transport/physics (by using tight cuts)");
}
bool SimConfig::resetFromParsedMap(boost::program_options::variables_map const& vm)
{
using o2::detectors::DetID;
mConfigData.mMCEngine = vm["mcEngine"].as<std::string>();
mConfigData.mActiveModules = vm["modules"].as<std::vector<std::string>>();
auto& activeModules = mConfigData.mActiveModules;
if (activeModules.size() == 1 && activeModules[0] == "all") {
activeModules.clear();
for (int d = DetID::First; d <= DetID::Last; ++d) {
#ifdef ENABLE_UPGRADES
if (d != DetID::IT3 && d != DetID::TRK && d != DetID::FT3) {
activeModules.emplace_back(DetID::getName(d));
}
#else
activeModules.emplace_back(DetID::getName(d));
#endif
}
// add passive components manually (make a PassiveDetID for them!)
activeModules.emplace_back("HALL");
activeModules.emplace_back("MAG");
activeModules.emplace_back("DIPO");
activeModules.emplace_back("COMP");
activeModules.emplace_back("PIPE");
activeModules.emplace_back("ABSO");
activeModules.emplace_back("SHIL");
}
// now we take out detectors listed as skipped
auto& skipped = vm["skipModules"].as<std::vector<std::string>>();
for (auto& s : skipped) {
auto iter = std::find(activeModules.begin(), activeModules.end(), s);
if (iter != activeModules.end()) {
// take it out
activeModules.erase(iter);
}
}
// find all detectors that should be readout
auto enableReadout = vm["readoutDetectors"].as<std::vector<std::string>>();
auto& disableReadout = vm["skipReadoutDetectors"].as<std::vector<std::string>>();
auto& readoutDetectors = mConfigData.mReadoutDetectors;
readoutDetectors.clear();
auto isDet = [](std::string const& s) {
return DetID::nameToID(s.c_str()) >= DetID::First;
};
if (enableReadout.empty()) {
// if no readout explicitly given, use all detectors from active modules
for (auto& am : activeModules) {
if (!isDet(am)) {
// either we found a passive module or one with disabled readout ==> skip
continue;
}
readoutDetectors.emplace_back(am);
}
} else {
for (auto& er : enableReadout) {
if (!isDet(er)) {
// either we found a passive module or one with disabled readout ==> skip
LOG(fatal) << "Enabled readout for " << er << " which is not a detector.";
}
if (std::find(activeModules.begin(), activeModules.end(), er) == activeModules.end()) {
// add to active modules if not yet there
LOG(fatal) << "Module " << er << " not constructed and cannot be used for readout (make sure it is contained in -m option).";
}
readoutDetectors.emplace_back(er);
}
}
for (auto& dr : disableReadout) {
if (!isDet(dr)) {
// either we found a passive module or one with disabled readout ==> skip
LOG(fatal) << "Disabled readout for " << dr << " which is not a detector.";
}
if (std::find(activeModules.begin(), activeModules.end(), dr) == activeModules.end()) {
// add to active modules if not yet there
LOG(fatal) << "Module " << dr << " not constructed, makes no sense to disable its readout (make sure it is contained in -m option).";
}
auto iter = std::find(readoutDetectors.begin(), readoutDetectors.end(), dr);
if (iter != readoutDetectors.end()) {
readoutDetectors.erase(iter);
}
}
mConfigData.mGenerator = vm["generator"].as<std::string>();
mConfigData.mTrigger = vm["trigger"].as<std::string>();
mConfigData.mNEvents = vm["nEvents"].as<unsigned int>();
mConfigData.mExtKinFileName = vm["extKinFile"].as<std::string>();
mConfigData.mEmbedIntoFileName = vm["embedIntoFile"].as<std::string>();
mConfigData.mStartEvent = vm["startEvent"].as<unsigned int>();
mConfigData.mBMax = vm["bMax"].as<float>();
mConfigData.mIsMT = vm["isMT"].as<bool>();
mConfigData.mOutputPrefix = vm["outPrefix"].as<std::string>();
mConfigData.mLogSeverity = vm["logseverity"].as<std::string>();
mConfigData.mLogVerbosity = vm["logverbosity"].as<std::string>();
mConfigData.mKeyValueTokens = vm["configKeyValues"].as<std::string>();
mConfigData.mConfigFile = vm["configFile"].as<std::string>();
mConfigData.mPrimaryChunkSize = vm["chunkSize"].as<unsigned int>();
mConfigData.mInternalChunkSize = vm["chunkSizeI"].as<int>();
mConfigData.mStartSeed = vm["seed"].as<int>();
mConfigData.mSimWorkers = vm["nworkers"].as<int>();
if (vm.count("timestamp")) {
mConfigData.mTimestamp = vm["timestamp"].as<uint64_t>();
} else {
mConfigData.mTimestamp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
}
mConfigData.mCCDBUrl = vm["CCDBUrl"].as<std::string>();
mConfigData.mAsService = vm["asservice"].as<bool>();
mConfigData.mNoGeant = vm["noGeant"].as<bool>();
if (vm.count("noemptyevents")) {
mConfigData.mFilterNoHitEvents = true;
}
// analyse field options
// either: "ccdb" or +-2[U],+-5[U] and 0[U]; +-<intKGaus>U
auto& fieldstring = vm["field"].as<std::string>();
std::regex re("(ccdb)|([+-]?[250]U?)");
if (!std::regex_match(fieldstring, re)) {
LOG(error) << "Invalid field option";
return false;
}
if (fieldstring == "ccdb") {
mConfigData.mFieldMode = SimFieldMode::kCCDB;
} else if (fieldstring.find("U") != std::string::npos) {
mConfigData.mFieldMode = SimFieldMode::kUniform;
}
if (fieldstring != "ccdb") {
mConfigData.mField = std::stoi((vm["field"].as<std::string>()).substr(0, (vm["field"].as<std::string>()).rfind("U")));
}
return true;
}
bool SimConfig::resetFromArguments(int argc, char* argv[])
{
namespace bpo = boost::program_options;
// Arguments parsing
bpo::variables_map vm;
bpo::options_description desc("Allowed options");
desc.add_options()("help,h", "Produce help message.");
initOptions(desc);
try {
bpo::store(parse_command_line(argc, argv, desc), vm);
// help
if (vm.count("help")) {
std::cout << desc << std::endl;
return false;
}
bpo::notify(vm);
} catch (const bpo::error& e) {
std::cerr << e.what() << "\n\n";
std::cerr << "Error parsing command line arguments; Available options:\n";
std::cerr << desc << std::endl;
return false;
}
return resetFromParsedMap(vm);
}
namespace o2::conf
{
// returns a reconfig struct given a configuration string (boost program options format)
bool parseSimReconfigFromString(std::string const& argumentstring, SimReconfigData& data)
{
namespace bpo = boost::program_options;
bpo::options_description options("Allowed options");
options.add_options()(
"nEvents,n", bpo::value<unsigned int>(&data.nEvents)->default_value(1), "number of events")(
"generator,g", bpo::value<std::string>(&data.generator)->default_value("boxgen"), "Event generator to be used.")(
"trigger,t", bpo::value<std::string>(&data.trigger)->default_value(""), "Event generator trigger to be used.")(
"startEvent", bpo::value<unsigned int>(&data.startEvent)->default_value(0), "index of first event to be used (when applicable)")(
"extKinFile", bpo::value<std::string>(&data.extKinfileName)->default_value("Kinematics.root"),
"name of kinematics file for event generator from file (when applicable)")(
"embedIntoFile", bpo::value<std::string>(&data.embedIntoFileName)->default_value(""),
"filename containing the reference events to be used for the embedding")(
"bMax,b", bpo::value<float>(&data.mBMax)->default_value(0.), "maximum value for impact parameter sampling (when applicable)")(
"outPrefix,o", bpo::value<std::string>(&data.outputPrefix)->default_value("o2sim"), "prefix of output files")(
"outDir,d", bpo::value<std::string>(&data.outputDir), "directory where to put simulation output (created when non-existant")(
"configKeyValues", bpo::value<std::string>(&data.keyValueTokens)->default_value(""), "semicolon separated key=value strings (e.g.: 'TPC.gasDensity=1;...")(
"configFile", bpo::value<std::string>(&data.configFile)->default_value(""), "Path to an INI or JSON configuration file")(
"chunkSize", bpo::value<unsigned int>(&data.primaryChunkSize)->default_value(500), "max size of primary chunk (subevent) distributed by server")(
"seed", bpo::value<int>(&data.startSeed)->default_value(-1), "initial seed (default: -1 random)")(
"stop", bpo::value<bool>(&data.stop)->default_value(false), "control command to shut down daemon");
bpo::variables_map vm;
try {
bpo::store(bpo::command_line_parser(bpo::split_unix(argumentstring))
.options(options)
.run(),
vm);
bpo::notify(vm);
} catch (const bpo::error& e) {
std::cerr << e.what() << "\n\n";
std::cerr << "Error parsing ReConfig data; Available options:\n";
std::cerr << options << std::endl;
return false;
}
return true;
}
} // namespace o2::conf
ClassImp(o2::conf::SimConfig);