Skip to content

Commit 634f5f9

Browse files
committed
Fix test case and other similar fixes
- The test case `o2sim-hepmc` fixed to use proper config key - `GeneratorHepMC` will give error if old config key used - `GeneratorHepMC` will give warning if version key is set - the code deduces the version on its own now. - Added superflous `{...}` around single statement `if`, `while`, ... - boy those checks are silly and counter productive.
1 parent fba7f8c commit 634f5f9

File tree

5 files changed

+92
-49
lines changed

5 files changed

+92
-49
lines changed

Generators/include/Generators/GeneratorHepMCParam.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ namespace eventgen
3030
**/
3131

3232
struct GeneratorHepMCParam : public o2::conf::ConfigurableParamHelper<GeneratorHepMCParam> {
33-
int version = 3;
33+
int version = 0;
3434
uint64_t eventsToSkip = 0;
35+
std::string fileName = "";
3536
O2ParamDef(GeneratorHepMCParam, "HepMC");
3637
};
3738

Generators/src/GeneratorFileOrCmd.cxx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
1111

12+
/// @author Christian Holm Christensen <cholm@nbi.dk>
1213

1314
#include "SimulationDataFormat/MCUtils.h"
1415
#include "Generators/GeneratorFileOrCmd.h"
@@ -30,8 +31,9 @@ namespace
3031
std::string ltrim(const std::string& s, const std::string& what = "\" ' ")
3132
{
3233
auto start = s.find_first_not_of(what);
33-
if (start == std::string::npos)
34+
if (start == std::string::npos) {
3435
return "";
36+
}
3537

3638
return s.substr(start);
3739
}
@@ -72,21 +74,25 @@ void GeneratorFileOrCmd::setFileNames(const std::string& filenames)
7274
{
7375
std::stringstream s(filenames);
7476
std::string f;
75-
while (std::getline(s, f, ','))
77+
while (std::getline(s, f, ',')) {
7678
mFileNames.push_back(f);
79+
}
7780
}
7881
// -----------------------------------------------------------------
7982
std::string GeneratorFileOrCmd::makeCmdLine() const
8083
{
8184
std::string fileName = mFileNames.front();
8285
std::stringstream s;
8386
s << mCmd << " ";
84-
if (not mSeedSwitch.empty() and mSeedSwitch != "none")
87+
if (not mSeedSwitch.empty() and mSeedSwitch != "none") {
8588
s << mSeedSwitch << " " << mSeed << " ";
86-
if (not mNEventsSwitch.empty() and mNEventsSwitch != "none")
89+
}
90+
if (not mNEventsSwitch.empty() and mNEventsSwitch != "none") {
8791
s << mNEventsSwitch << " " << mNEvents << " ";
88-
if (not mBmaxSwitch.empty() and mBmax >= 0 and mBmaxSwitch != "none")
92+
}
93+
if (not mBmaxSwitch.empty() and mBmax >= 0 and mBmaxSwitch != "none") {
8994
s << mBmaxSwitch.c_str() << " " << mBmax << " ";
95+
}
9096

9197
s << mOutputSwitch << " " << fileName << " "
9298
<< mBackgroundSwitch;
@@ -189,13 +195,15 @@ void GeneratorFileOrCmd::waitForData(const std::string& filename) const
189195
LOG(debug) << "Waiting for data on " << p;
190196

191197
// Wait until child process creates the file
192-
while (not std::filesystem::exists(p))
198+
while (not std::filesystem::exists(p)) {
193199
std::this_thread::sleep_for(mWait * 1ms);
200+
}
194201

195202
// Wait until we have more data in the file than just the file
196203
// header
197-
while (std::filesystem::file_size(p) <= 256)
204+
while (std::filesystem::file_size(p) <= 256) {
198205
std::this_thread::sleep_for(mWait * 1ms);
206+
}
199207

200208
// Give the child process 1 second to post the data to the file
201209
LOG(debug) << "Got data in " << p << ", sleeping for a while";

Generators/src/GeneratorHepMC.cxx

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ GeneratorHepMC::~GeneratorHepMC()
5959
{
6060
/** default destructor **/
6161
LOG(info) << "Destructing GeneratorHepMC";
62-
if (mReader)
62+
if (mReader) {
6363
mReader->close();
64-
if (mEvent)
64+
}
65+
if (mEvent) {
6566
delete mEvent;
67+
}
6668
removeTemp();
6769
}
6870

@@ -71,21 +73,28 @@ void GeneratorHepMC::setup(const GeneratorFileOrCmdParam& param0,
7173
const GeneratorHepMCParam& param,
7274
const conf::SimConfig& config)
7375
{
76+
if (not param.fileName.empty()) {
77+
LOG(fatal) << "The use of the key \"HepMC.fileName\" is "
78+
<< "no longer supported, use \"FileOrCmd.fileNames\" instead";
79+
}
80+
if (param.version != 0)
81+
LOG(warn) << "The key \"HepMC.version\" is no longer used. The "
82+
<< "version of the input files are automatically deduced.";
7483
GeneratorFileOrCmd::setup(param0, config);
7584
setEventsToSkip(param.eventsToSkip);
7685
}
7786

7887
/*****************************************************************/
79-
8088
Bool_t GeneratorHepMC::generateEvent()
8189
{
8290
LOG(debug) << "Generating an event";
8391
/** generate event **/
8492
int tries = 0;
8593
do {
8694
LOG(debug) << " try # " << ++tries;
87-
if (not mReader and not makeReader())
95+
if (not mReader and not makeReader()) {
8896
return false;
97+
}
8998

9099
/** clear and read event **/
91100
mEvent->clear();
@@ -153,34 +162,47 @@ Bool_t GeneratorHepMC::importParticles()
153162

154163
namespace
155164
{
165+
template <typename AttributeType, typename TargetType>
166+
bool putAttributeInfoImpl(o2::dataformats::MCEventHeader* eventHeader,
167+
const std::string& name,
168+
const std::shared_ptr<HepMC3::Attribute>& a)
169+
{
170+
if (auto* p = dynamic_cast<AttributeType*>(a.get())) {
171+
eventHeader->putInfo<TargetType>(name, p->value());
172+
return true;
173+
}
174+
return false;
175+
}
176+
156177
void putAttributeInfo(o2::dataformats::MCEventHeader* eventHeader,
157178
const std::string& name,
158179
const std::shared_ptr<HepMC3::Attribute>& a)
159180
{
160-
if (auto* p = dynamic_cast<HepMC3::IntAttribute*>(a.get()))
161-
eventHeader->putInfo<int>(name, p->value());
162-
if (auto* p = dynamic_cast<HepMC3::LongAttribute*>(a.get()))
163-
eventHeader->putInfo<int>(name, p->value());
164-
if (auto* p = dynamic_cast<HepMC3::FloatAttribute*>(a.get()))
165-
eventHeader->putInfo<float>(name, p->value());
166-
if (auto* p = dynamic_cast<HepMC3::DoubleAttribute*>(a.get()))
167-
eventHeader->putInfo<float>(name, p->value());
168-
if (auto* p = dynamic_cast<HepMC3::StringAttribute*>(a.get()))
169-
eventHeader->putInfo<std::string>(name, p->value());
170-
if (auto* p = dynamic_cast<HepMC3::CharAttribute*>(a.get()))
171-
eventHeader->putInfo<char>(name, p->value());
172-
if (auto* p = dynamic_cast<HepMC3::LongLongAttribute*>(a.get()))
173-
eventHeader->putInfo<int>(name, p->value());
174-
if (auto* p = dynamic_cast<HepMC3::LongDoubleAttribute*>(a.get()))
175-
eventHeader->putInfo<float>(name, p->value());
176-
if (auto* p = dynamic_cast<HepMC3::UIntAttribute*>(a.get()))
177-
eventHeader->putInfo<int>(name, p->value());
178-
if (auto* p = dynamic_cast<HepMC3::ULongAttribute*>(a.get()))
179-
eventHeader->putInfo<int>(name, p->value());
180-
if (auto* p = dynamic_cast<HepMC3::ULongLongAttribute*>(a.get()))
181-
eventHeader->putInfo<int>(name, p->value());
182-
if (auto* p = dynamic_cast<HepMC3::BoolAttribute*>(a.get()))
183-
eventHeader->putInfo<bool>(name, p->value());
181+
using IntAttribute=HepMC3::IntAttribute;
182+
using LongAttribute=HepMC3::LongAttribute;
183+
using FloatAttribute=HepMC3::FloatAttribute;
184+
using DoubleAttribute=HepMC3::DoubleAttribute;
185+
using StringAttribute=HepMC3::StringAttribute;
186+
using CharAttribute=HepMC3::CharAttribute;
187+
using LongLongAttribute=HepMC3::LongLongAttribute;
188+
using LongDoubleAttribute=HepMC3::LongDoubleAttribute;
189+
using UIntAttribute=HepMC3::UIntAttribute;
190+
using ULongAttribute=HepMC3::ULongAttribute;
191+
using ULongLongAttribute=HepMC3::ULongLongAttribute;
192+
using BoolAttribute=HepMC3::BoolAttribute;
193+
194+
if (putAttributeInfoImpl<IntAttribute, int>(eventHeader, name, a)) return;
195+
if (putAttributeInfoImpl<LongAttribute, int>(eventHeader, name, a)) return;
196+
if (putAttributeInfoImpl<FloatAttribute, float>(eventHeader, name, a)) return;
197+
if (putAttributeInfoImpl<DoubleAttribute, float>(eventHeader, name, a)) return;
198+
if (putAttributeInfoImpl<StringAttribute, std::string>(eventHeader, name, a)) return;
199+
if (putAttributeInfoImpl<CharAttribute, char>(eventHeader, name, a)) return;
200+
if (putAttributeInfoImpl<LongLongAttribute, int>(eventHeader, name, a)) return;
201+
if (putAttributeInfoImpl<LongDoubleAttribute, float>(eventHeader, name, a)) return;
202+
if (putAttributeInfoImpl<UIntAttribute, int>(eventHeader, name, a)) return;
203+
if (putAttributeInfoImpl<ULongAttribute, int>(eventHeader, name, a)) return;
204+
if (putAttributeInfoImpl<ULongLongAttribute, int>(eventHeader, name, a)) return;
205+
if (putAttributeInfoImpl<BoolAttribute, bool>(eventHeader, name, a)) return;
184206
}
185207
} // namespace
186208

@@ -264,8 +286,9 @@ void GeneratorHepMC::updateHeader(o2::dataformats::MCEventHeader* eventHeader)
264286
std::string name = na.first;
265287
if (name == "GenPdfInfo" ||
266288
name == "GenCrossSection" ||
267-
name == "GenHeavyIon")
289+
name == "GenHeavyIon") {
268290
continue;
291+
}
269292

270293
for (auto ia : na.second) {
271294
int no = ia.first;
@@ -354,12 +377,14 @@ Bool_t GeneratorHepMC::Init()
354377
// around the actual EG program.
355378
if (not mCmd.empty()) {
356379
// Set filename to be a temporary name
357-
if (not makeTemp())
380+
if (not makeTemp()) {
358381
return false;
382+
}
359383

360384
// Make a fifo
361-
if (not makeFifo())
385+
if (not makeFifo()) {
362386
return false;
387+
}
363388

364389
// Build command line, rediret stdout to our fifo and put
365390
std::string cmd = makeCmdLine();
@@ -390,8 +415,9 @@ Bool_t GeneratorHepMC::Init()
390415
//
391416
// However, here we will assume system local files. If _any_ of
392417
// the listed files do not exist, then we fail.
393-
if (not ensureFiles())
418+
if (not ensureFiles()) {
394419
return false;
420+
}
395421
}
396422

397423
// Create reader for current (first) file

Generators/src/GeneratorTParticle.cxx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ GeneratorTParticle::~GeneratorTParticle()
3535
{
3636
if (mChain) {
3737
TFile* file = mChain->GetCurrentFile();
38-
if (file)
38+
if (file) {
3939
mChain->RecursiveRemove(file);
40+
}
4041
delete mChain;
4142
}
42-
if (mCmd.empty())
43+
if (mCmd.empty()) {
4344
return;
45+
}
4446

4547
removeTemp();
4648
}
@@ -53,8 +55,9 @@ Bool_t GeneratorTParticle::Init()
5355

5456
if (not mCmd.empty()) {
5557
// Set filename to be a temporary name
56-
if (not makeTemp())
58+
if (not makeTemp()) {
5759
return false;
60+
}
5861

5962
// Build command line, Assumes command line parameter
6063
std::string cmd = makeCmdLine();
@@ -66,8 +69,9 @@ Bool_t GeneratorTParticle::Init()
6669
return false;
6770
}
6871
}
69-
for (auto filename : mFileNames)
72+
for (auto filename : mFileNames) {
7073
mChain->AddFile(filename.c_str());
74+
}
7175

7276
// Clear the array of file names
7377
mFileNames.clear();
@@ -90,20 +94,23 @@ Bool_t GeneratorTParticle::generateEvent()
9094
{
9195
// If this is the first entry, and we're executing a command, then
9296
// wait until the input file exists and actually contain some data.
93-
if (mEntry == 0 and not mCmd.empty())
97+
if (mEntry == 0 and not mCmd.empty()) {
9498
waitForData(mTemporary);
99+
}
95100

96101
// Read in the next entry in the chain
97102
int read = mChain->GetEntry(mEntry);
98103
mEntry++;
99104

100105
// If we got an error while reading, then give error message
101-
if (read < 0)
106+
if (read < 0) {
102107
LOG(error) << "Failed to read entry " << mEntry << " of chain";
108+
}
103109

104110
// If we had an error or nothing was read back, then return false
105-
if (read <= 0)
111+
if (read <= 0) {
106112
return false;
113+
}
107114

108115
return true;
109116
}
@@ -113,9 +120,10 @@ Bool_t GeneratorTParticle::importParticles()
113120
for (auto* object : *mTParticles) {
114121
TParticle* particle = static_cast<TParticle*>(object);
115122
auto statusCode = particle->GetStatusCode();
116-
if (!mcgenstatus::isEncoded(statusCode))
123+
if (!mcgenstatus::isEncoded(statusCode)) {
117124
statusCode = mcgenstatus::MCGenStatusEncoding(statusCode, 0)
118125
.fullEncoding;
126+
}
119127

120128
mParticles.emplace_back(particle->GetPdgCode(),
121129
statusCode,

run/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ o2_add_test_command(NAME o2sim_hepmc
271271
-g
272272
hepmc
273273
--configKeyValues
274-
"HepMC.fileName=${CMAKE_SOURCE_DIR}/Generators/share/data/pythia.hepmc;HepMC.version=2;align-geom.mDetectors=none"
274+
"FileOrCmd.fileNames=${CMAKE_SOURCE_DIR}/Generators/share/data/pythia.hepmc;HepMC.version=2;align-geom.mDetectors=none"
275275
-o
276276
o2simhepmc
277277
LABELS long sim hepmc3

0 commit comments

Comments
 (0)