Skip to content

Commit 70803ce

Browse files
committed
Attach the magnetic field to the VMC engine before media creation
Fixing a long standing error message about "No magnetic field found" when initializing tracking media. Fixed by reordering of initializations.
1 parent 5240041 commit 70803ce

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Steer/src/O2MCApplication.cxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include <filesystem>
3636
#include <CommonUtils/FileSystemUtils.h>
3737
#include "SimConfig/GlobalProcessCutSimParam.h"
38+
#include <FairRunSim.h>
39+
#include <FairField.h> // full type: FairField derives from TVirtualMagField
40+
#include <TVirtualMC.h>
3841
#include "DetectorsBase/GeometryManagerParam.h"
3942
#include <TGeoParallelWorld.h>
4043
#include <TGeoVolume.h>
@@ -124,6 +127,23 @@ void O2MCApplicationBase::PreTrack()
124127

125128
void O2MCApplicationBase::ConstructGeometry()
126129
{
130+
// The transport engine constructs the geometry from inside its own
131+
// constructor, long before FairMCApplication::InitMC() attaches the magnetic
132+
// field to it. The media built below read the field through
133+
// Detector::initFieldTrackingParams(), so without this they all silently fall
134+
// back to hardcoded defaults. The run has known the field since
135+
// build_geometry.C, which runs before Init() -- hand it over now.
136+
if (auto* vmc = TVirtualMC::GetMC(); vmc != nullptr && vmc->GetMagField() == nullptr) {
137+
auto* run = FairRunSim::Instance();
138+
if (run != nullptr && run->GetField() != nullptr) {
139+
vmc->SetMagField(run->GetField());
140+
LOG(info) << "Magnetic field attached to the engine before media creation";
141+
} else {
142+
LOG(warn) << "No magnetic field available at geometry construction; media "
143+
"will be initialised with default tracking parameters";
144+
}
145+
}
146+
127147
// fill the mapping
128148
mModIdToName.clear();
129149
o2::detectors::DetID::mask_t dmask{};

0 commit comments

Comments
 (0)