Skip to content

Commit 6e76d93

Browse files
committed
Workaround for HepMC3 bug
1 parent ab73e40 commit 6e76d93

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

Generators/src/GeneratorHepMC.cxx

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <fairlogger/Logger.h>
2828
#include "FairPrimaryGenerator.h"
2929
#include <cmath>
30+
#include <sstream>
3031

3132
namespace o2
3233
{
@@ -420,6 +421,40 @@ void GeneratorHepMC::updateHeader(o2::dataformats::MCEventHeader* eventHeader)
420421
auto pdfInfo = mEvent->pdf_info();
421422
auto hiInfo = mEvent->heavy_ion();
422423

424+
// Workaround for a bug in HepMC3 (3.3.1 on 23/02/2026): GenHeavyIon::from_string() for the "v0"
425+
// format skips reading user_cent_estimate, but to_string() always writes it.
426+
// This shifts all subsequent fields by one, causing a istringstream failure and and heavy_ion()
427+
// to return null even when the attribute is present and well-formed.
428+
// For now we use this manual parser in case the infos are available
429+
if (!hiInfo) {
430+
auto attStr = mEvent->attribute_as_string("GenHeavyIon");
431+
if (!attStr.empty() && attStr[0] == 'v') {
432+
std::istringstream is(attStr);
433+
std::string version;
434+
is >> version;
435+
if (version == "v0") {
436+
auto hi = std::make_shared<HepMC3::GenHeavyIon>();
437+
double spectNeutrons, spectProtons, eccentricity, userCentEst;
438+
is >> hi->Ncoll_hard >> hi->Npart_proj >> hi->Npart_targ >> hi->Ncoll
439+
>> spectNeutrons >> spectProtons // deprecated v0 fields
440+
>> hi->N_Nwounded_collisions >> hi->Nwounded_N_collisions
441+
>> hi->Nwounded_Nwounded_collisions
442+
>> hi->impact_parameter >> hi->event_plane_angle
443+
>> eccentricity // deprecated v0 field
444+
>> hi->sigma_inel_NN >> hi->centrality
445+
>> userCentEst // GenHeavyIon::to_string always writes this, but GenHeavyIon::from_string skips it for v0 (HepMC3 bug to fix)
446+
>> hi->Nspec_proj_n >> hi->Nspec_targ_n
447+
>> hi->Nspec_proj_p >> hi->Nspec_targ_p;
448+
if (!is.fail()) {
449+
LOG(debug) << "GenHeavyIon: using manual v0 parser (workaround for HepMC3 from_string bug)";
450+
hiInfo = hi;
451+
} else {
452+
LOG(warn) << "GenHeavyIon: manual v0 parser also failed on: [" << attStr << "]";
453+
}
454+
}
455+
}
456+
}
457+
423458
// Set default cross-section
424459
if (xSection) {
425460
eventHeader->putInfo<float>(Key::xSection, xSection->xsec());
@@ -457,7 +492,8 @@ void GeneratorHepMC::updateHeader(o2::dataformats::MCEventHeader* eventHeader)
457492

458493
// Set heavy-ion information
459494
if (hiInfo) {
460-
eventHeader->putInfo<int>(Key::impactParameter,
495+
eventHeader->SetB(hiInfo->impact_parameter); // sets the impact parameter to the FairMCEventHeader field for quick access in the AO2D
496+
eventHeader->putInfo<float>(Key::impactParameter,
461497
hiInfo->impact_parameter);
462498
eventHeader->putInfo<int>(Key::nPart,
463499
hiInfo->Npart_proj + hiInfo->Npart_targ);
@@ -471,11 +507,9 @@ void GeneratorHepMC::updateHeader(o2::dataformats::MCEventHeader* eventHeader)
471507
hiInfo->Nwounded_N_collisions);
472508
eventHeader->putInfo<int>(Key::nCollNWoundedNwounded,
473509
hiInfo->Nwounded_Nwounded_collisions);
474-
eventHeader->putInfo<int>(Key::planeAngle,
475-
hiInfo->event_plane_angle);
476-
eventHeader->putInfo<int>(Key::sigmaInelNN,
477-
hiInfo->sigma_inel_NN);
478-
eventHeader->putInfo<int>(Key::centrality, hiInfo->centrality);
510+
eventHeader->putInfo<float>(Key::planeAngle, hiInfo->event_plane_angle);
511+
eventHeader->putInfo<float>(Key::sigmaInelNN, hiInfo->sigma_inel_NN);
512+
eventHeader->putInfo<float>(Key::centrality, hiInfo->centrality);
479513
eventHeader->putInfo<int>(Key::nSpecProjectileProton, hiInfo->Nspec_proj_p);
480514
eventHeader->putInfo<int>(Key::nSpecProjectileNeutron, hiInfo->Nspec_proj_n);
481515
eventHeader->putInfo<int>(Key::nSpecTargetProton, hiInfo->Nspec_targ_p);

0 commit comments

Comments
 (0)