|
12 | 12 | #include "Generators/GeneratorHybrid.h" |
13 | 13 | #include <fairlogger/Logger.h> |
14 | 14 | #include <algorithm> |
15 | | -#include <FairPrimaryGenerator.h> |
16 | 15 |
|
17 | 16 | namespace o2 |
18 | 17 | { |
19 | 18 | namespace eventgen |
20 | 19 | { |
21 | | - GeneratorHybrid::GeneratorHybrid(const std::vector<std::string>& inputgens) |
| 20 | + GeneratorHybrid::GeneratorHybrid(const std::string& inputgens) |
22 | 21 | { |
23 | | - auto configs = GeneratorHybridParam::Instance().configs; |
24 | | - mRandomize = GeneratorHybridParam::Instance().randomize; |
25 | | - std::stringstream ss(configs); |
26 | | - std::string conf; |
27 | | - while (std::getline(ss, conf, ':')) { |
28 | | - mConfigs.push_back(conf); |
| 22 | + if (!parseJSON(inputgens)) { |
| 23 | + LOG(fatal) << "Failed to parse JSON configuration from input generators"; |
| 24 | + exit(1); |
29 | 25 | } |
30 | | - if(mConfigs.size() != inputgens.size()){ |
| 26 | + mRandomize = GeneratorHybridParam::Instance().randomize; |
| 27 | + if (mConfigs.size() != mInputGens.size()) { |
31 | 28 | LOG(fatal) << "Number of configurations does not match the number of generators"; |
| 29 | + exit(1); |
32 | 30 | } |
33 | | - if(mConfigs.size() == 0){ |
34 | | - for(auto gen : inputgens){ |
| 31 | + if (mConfigs.size() == 0){ |
| 32 | + for(auto gen : mInputGens){ |
35 | 33 | mConfigs.push_back(""); |
36 | 34 | } |
37 | 35 | } |
38 | 36 | int index = 0; |
39 | 37 | if (!mRandomize) { |
40 | | - std::string fractions = GeneratorHybridParam::Instance().fractions; |
41 | | - if(fractions.compare("") == 0){ |
42 | | - for (auto gen : inputgens) { |
43 | | - mFractions.push_back(1); |
44 | | - } |
| 38 | + if (mFractions.size() != mInputGens.size()) { |
| 39 | + LOG(fatal) << "Number of fractions does not match the number of generators"; |
| 40 | + return; |
| 41 | + } |
| 42 | + // Check if all elements of mFractions are 0 |
| 43 | + if (std::all_of(mFractions.begin(), mFractions.end(), [](int i){ return i == 0; })) { |
| 44 | + LOG(fatal) << "All fractions provided are 0, no simulation will be performed"; |
| 45 | + return; |
45 | 46 | } |
46 | | - else{ |
47 | | - std::stringstream streamfrac(fractions); |
48 | | - std::string frac; |
49 | | - while (std::getline(streamfrac, frac, ',')) { |
50 | | - if(frac.compare("") == 0) |
51 | | - mFractions.push_back(1); |
52 | | - else |
53 | | - mFractions.push_back(std::stoi(frac)); |
54 | | - } |
55 | | - if(mFractions.size() != inputgens.size()){ |
56 | | - LOG(fatal) << "Number of fractions does not match the number of generators"; |
57 | | - return; |
58 | | - } |
59 | | - // Check if all elements of mFractions are 0 |
60 | | - if (std::all_of(mFractions.begin(), mFractions.end(), [](int i){ return i == 0; })) { |
61 | | - LOG(fatal) << "All fractions provided are 0, no simulation will be performed"; |
62 | | - return; |
63 | | - } |
64 | | - |
65 | | - } |
66 | 47 | } |
67 | | - for(auto gen : inputgens) |
68 | | - { |
| 48 | + for (auto gen : mInputGens) { |
69 | 49 | // Search if the generator name is inside generatorNames (which is a vector of strings) |
70 | 50 | LOG(info) << "Checking if generator " << gen << " is in the list of available generators \n"; |
71 | 51 | if (std::find(generatorNames.begin(), generatorNames.end(), gen) != generatorNames.end()) |
@@ -133,7 +113,7 @@ namespace o2 |
133 | 113 | } |
134 | 114 | } |
135 | 115 | index++; |
136 | | - } |
| 116 | + } |
137 | 117 | } |
138 | 118 |
|
139 | 119 | Bool_t GeneratorHybrid::Init() |
@@ -208,7 +188,68 @@ namespace o2 |
208 | 188 | } |
209 | 189 |
|
210 | 190 | return true; |
211 | | - } |
| 191 | + } |
| 192 | + |
| 193 | + Bool_t GeneratorHybrid::parseJSON(const std::string& path) |
| 194 | + { |
| 195 | + // Parse JSON file to build map |
| 196 | + std::ifstream fileStream(path, std::ios::in); |
| 197 | + if (!fileStream.is_open()) { |
| 198 | + LOG(error) << "Cannot open " << path; |
| 199 | + return false; |
| 200 | + } |
| 201 | + rapidjson::IStreamWrapper isw(fileStream); |
| 202 | + rapidjson::Document doc; |
| 203 | + doc.ParseStream(isw); |
| 204 | + if (doc.HasParseError()) { |
| 205 | + LOG(error) << "Error parsing provided json file " << path; |
| 206 | + LOG(error) << " - Error -> " << rapidjson::GetParseError_En(doc.GetParseError()); |
| 207 | + LOG(error) << " - Offset -> " << doc.GetErrorOffset(); |
| 208 | + return false; |
| 209 | + } |
| 210 | + |
| 211 | + // Put the generator names in mInputGens |
| 212 | + if (doc.HasMember("generators")) { |
| 213 | + const auto& gens = doc["generators"]; |
| 214 | + for (const auto& gen : gens.GetArray()) { |
| 215 | + // push in mInputGens the "name" of the generator |
| 216 | + mInputGens.push_back(gen["name"].GetString()); |
| 217 | + if (gen.HasMember("config")) { |
| 218 | + //Check if config is an array |
| 219 | + if (gen["config"].IsArray()) { |
| 220 | + std::string config = ""; |
| 221 | + for (const auto& conf : gen["config"].GetArray()) { |
| 222 | + config += conf.GetString(); |
| 223 | + config += ","; |
| 224 | + } |
| 225 | + mConfigs.push_back(config); |
| 226 | + } else { |
| 227 | + mConfigs.push_back(gen["config"].GetString()); |
| 228 | + } |
| 229 | + } |
| 230 | + else { |
| 231 | + mConfigs.push_back(""); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + // Get fractions and put them in mFractions |
| 237 | + if (doc.HasMember("fractions")) { |
| 238 | + const auto& fractions = doc["fractions"]; |
| 239 | + for (const auto& frac : fractions.GetArray()) { |
| 240 | + mFractions.push_back(frac.GetInt()); |
| 241 | + } |
| 242 | + } |
| 243 | + else { |
| 244 | + // Set fractions to unity for all generators in case they are not provided |
| 245 | + const auto& gens = doc["generators"]; |
| 246 | + for (const auto& gen : gens.GetArray()) { |
| 247 | + mFractions.push_back(1); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return true; |
| 252 | + } |
212 | 253 |
|
213 | 254 | } // namespace eventgen |
214 | 255 | } // namespace o2 |
|
0 commit comments