Skip to content

Commit a4cf23f

Browse files
committed
Merge branch 'cholmcc_generators_more' into cholmcc_aod_to_hepmc
2 parents a21d519 + a8ded64 commit a4cf23f

File tree

12 files changed

+247
-160
lines changed

12 files changed

+247
-160
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +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 R+Preghenella - August 2017
12+
/// @author Christian Holm Christensen <cholm@nbi.dk>
1313

1414
#include "SimulationDataFormat/MCUtils.h"
1515
#include "Generators/GeneratorFileOrCmd.h"
@@ -31,8 +31,9 @@ namespace
3131
std::string ltrim(const std::string& s, const std::string& what = "\" ' ")
3232
{
3333
auto start = s.find_first_not_of(what);
34-
if (start == std::string::npos)
34+
if (start == std::string::npos) {
3535
return "";
36+
}
3637

3738
return s.substr(start);
3839
}
@@ -73,21 +74,25 @@ void GeneratorFileOrCmd::setFileNames(const std::string& filenames)
7374
{
7475
std::stringstream s(filenames);
7576
std::string f;
76-
while (std::getline(s, f, ','))
77+
while (std::getline(s, f, ',')) {
7778
mFileNames.push_back(f);
79+
}
7880
}
7981
// -----------------------------------------------------------------
8082
std::string GeneratorFileOrCmd::makeCmdLine() const
8183
{
8284
std::string fileName = mFileNames.front();
8385
std::stringstream s;
8486
s << mCmd << " ";
85-
if (not mSeedSwitch.empty() and mSeedSwitch != "none")
87+
if (not mSeedSwitch.empty() and mSeedSwitch != "none") {
8688
s << mSeedSwitch << " " << mSeed << " ";
87-
if (not mNEventsSwitch.empty() and mNEventsSwitch != "none")
89+
}
90+
if (not mNEventsSwitch.empty() and mNEventsSwitch != "none") {
8891
s << mNEventsSwitch << " " << mNEvents << " ";
89-
if (not mBmaxSwitch.empty() and mBmax >= 0 and mBmaxSwitch != "none")
92+
}
93+
if (not mBmaxSwitch.empty() and mBmax >= 0 and mBmaxSwitch != "none") {
9094
s << mBmaxSwitch.c_str() << " " << mBmax << " ";
95+
}
9196

9297
s << mOutputSwitch << " " << fileName << " "
9398
<< mBackgroundSwitch;
@@ -190,13 +195,15 @@ void GeneratorFileOrCmd::waitForData(const std::string& filename) const
190195
LOG(debug) << "Waiting for data on " << p;
191196

192197
// Wait until child process creates the file
193-
while (not std::filesystem::exists(p))
198+
while (not std::filesystem::exists(p)) {
194199
std::this_thread::sleep_for(mWait * 1ms);
200+
}
195201

196202
// Wait until we have more data in the file than just the file
197203
// header
198-
while (std::filesystem::file_size(p) <= 256)
204+
while (std::filesystem::file_size(p) <= 256) {
199205
std::this_thread::sleep_for(mWait * 1ms);
206+
}
200207

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

Generators/src/GeneratorHepMC.cxx

Lines changed: 82 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,71 @@ 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)) {
195+
return;
196+
}
197+
if (putAttributeInfoImpl<LongAttribute, int>(eventHeader, name, a)) {
198+
return;
199+
}
200+
if (putAttributeInfoImpl<FloatAttribute, float>(eventHeader, name, a)) {
201+
return;
202+
}
203+
if (putAttributeInfoImpl<DoubleAttribute, float>(eventHeader, name, a)) {
204+
return;
205+
}
206+
if (putAttributeInfoImpl<StringAttribute, std::string>(eventHeader, name, a)) {
207+
return;
208+
}
209+
if (putAttributeInfoImpl<CharAttribute, char>(eventHeader, name, a)) {
210+
return;
211+
}
212+
if (putAttributeInfoImpl<LongLongAttribute, int>(eventHeader, name, a)) {
213+
return;
214+
}
215+
if (putAttributeInfoImpl<LongDoubleAttribute, float>(eventHeader, name, a)) {
216+
return;
217+
}
218+
if (putAttributeInfoImpl<UIntAttribute, int>(eventHeader, name, a)) {
219+
return;
220+
}
221+
if (putAttributeInfoImpl<ULongAttribute, int>(eventHeader, name, a)) {
222+
return;
223+
}
224+
if (putAttributeInfoImpl<ULongLongAttribute, int>(eventHeader, name, a)) {
225+
return;
226+
}
227+
if (putAttributeInfoImpl<BoolAttribute, bool>(eventHeader, name, a)) {
228+
return;
229+
}
184230
}
185231
} // namespace
186232

@@ -264,8 +310,9 @@ void GeneratorHepMC::updateHeader(o2::dataformats::MCEventHeader* eventHeader)
264310
std::string name = na.first;
265311
if (name == "GenPdfInfo" ||
266312
name == "GenCrossSection" ||
267-
name == "GenHeavyIon")
313+
name == "GenHeavyIon") {
268314
continue;
315+
}
269316

270317
for (auto ia : na.second) {
271318
int no = ia.first;
@@ -354,12 +401,14 @@ Bool_t GeneratorHepMC::Init()
354401
// around the actual EG program.
355402
if (not mCmd.empty()) {
356403
// Set filename to be a temporary name
357-
if (not makeTemp())
404+
if (not makeTemp()) {
358405
return false;
406+
}
359407

360408
// Make a fifo
361-
if (not makeFifo())
409+
if (not makeFifo()) {
362410
return false;
411+
}
363412

364413
// Build command line, rediret stdout to our fifo and put
365414
std::string cmd = makeCmdLine();
@@ -390,8 +439,9 @@ Bool_t GeneratorHepMC::Init()
390439
//
391440
// However, here we will assume system local files. If _any_ of
392441
// the listed files do not exist, then we fail.
393-
if (not ensureFiles())
442+
if (not ensureFiles()) {
394443
return false;
444+
}
395445
}
396446

397447
// 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
@@ -279,7 +279,7 @@ o2_add_test_command(NAME o2sim_hepmc
279279
-g
280280
hepmc
281281
--configKeyValues
282-
"HepMC.fileName=${CMAKE_SOURCE_DIR}/Generators/share/data/pythia.hepmc;HepMC.version=2;align-geom.mDetectors=none"
282+
"FileOrCmd.fileNames=${CMAKE_SOURCE_DIR}/Generators/share/data/pythia.hepmc;HepMC.version=2;align-geom.mDetectors=none"
283283
-o
284284
o2simhepmc
285285
LABELS long sim hepmc3

0 commit comments

Comments
 (0)