Skip to content

Commit a08f636

Browse files
committed
Workaround for HepMC3 bug
1 parent ab73e40 commit a08f636

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

Generators/src/GeneratorHepMC.cxx

Lines changed: 35 additions & 7 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,34 @@ 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 >> spectNeutrons >> spectProtons // deprecated v0 fields
439+
>> hi->N_Nwounded_collisions >> hi->Nwounded_N_collisions >> hi->Nwounded_Nwounded_collisions >> hi->impact_parameter >> hi->event_plane_angle >> eccentricity // deprecated v0 field
440+
>> hi->sigma_inel_NN >> hi->centrality >> userCentEst // GenHeavyIon::to_string always writes this, but GenHeavyIon::from_string skips it for v0 (HepMC3 bug to fix)
441+
>> hi->Nspec_proj_n >> hi->Nspec_targ_n >> hi->Nspec_proj_p >> hi->Nspec_targ_p;
442+
if (!is.fail()) {
443+
LOG(debug) << "GenHeavyIon: using manual v0 parser (workaround for HepMC3 from_string bug)";
444+
hiInfo = hi;
445+
} else {
446+
LOG(warn) << "GenHeavyIon: manual v0 parser also failed on: [" << attStr << "]";
447+
}
448+
}
449+
}
450+
}
451+
423452
// Set default cross-section
424453
if (xSection) {
425454
eventHeader->putInfo<float>(Key::xSection, xSection->xsec());
@@ -457,8 +486,9 @@ void GeneratorHepMC::updateHeader(o2::dataformats::MCEventHeader* eventHeader)
457486

458487
// Set heavy-ion information
459488
if (hiInfo) {
460-
eventHeader->putInfo<int>(Key::impactParameter,
461-
hiInfo->impact_parameter);
489+
eventHeader->SetB(hiInfo->impact_parameter); // sets the impact parameter to the FairMCEventHeader field for quick access in the AO2D
490+
eventHeader->putInfo<float>(Key::impactParameter,
491+
hiInfo->impact_parameter);
462492
eventHeader->putInfo<int>(Key::nPart,
463493
hiInfo->Npart_proj + hiInfo->Npart_targ);
464494
eventHeader->putInfo<int>(Key::nPartProjectile, hiInfo->Npart_proj);
@@ -471,11 +501,9 @@ void GeneratorHepMC::updateHeader(o2::dataformats::MCEventHeader* eventHeader)
471501
hiInfo->Nwounded_N_collisions);
472502
eventHeader->putInfo<int>(Key::nCollNWoundedNwounded,
473503
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);
504+
eventHeader->putInfo<float>(Key::planeAngle, hiInfo->event_plane_angle);
505+
eventHeader->putInfo<float>(Key::sigmaInelNN, hiInfo->sigma_inel_NN);
506+
eventHeader->putInfo<float>(Key::centrality, hiInfo->centrality);
479507
eventHeader->putInfo<int>(Key::nSpecProjectileProton, hiInfo->Nspec_proj_p);
480508
eventHeader->putInfo<int>(Key::nSpecProjectileNeutron, hiInfo->Nspec_proj_n);
481509
eventHeader->putInfo<int>(Key::nSpecTargetProton, hiInfo->Nspec_targ_p);

0 commit comments

Comments
 (0)