@@ -84,61 +84,68 @@ int PropagatorImpl<value_T>::initFieldFromGRP(const std::string grpFileName, boo
8484 if (verbose) {
8585 grp->print ();
8686 }
87-
8887 return initFieldFromGRP (grp);
8988}
9089
9190// ____________________________________________________________
9291template <typename value_T>
9392int PropagatorImpl<value_T>::initFieldFromGRP(const o2::parameters::GRPObject* grp, bool verbose)
9493{
95- // / init mag field from GRP data and attach it to TGeoGlobalMagField
96-
97- if (TGeoGlobalMagField::Instance ()->IsLocked ()) {
98- if (TGeoGlobalMagField::Instance ()->GetField ()->TestBit (o2::field::MagneticField::kOverrideGRP )) {
99- LOG (warning) << " ExpertMode!!! GRP information will be ignored" ;
100- LOG (warning) << " ExpertMode!!! Running with the externally locked B field" ;
101- return 0 ;
102- } else {
103- LOG (info) << " Destroying existing B field instance" ;
104- delete TGeoGlobalMagField::Instance ();
105- }
106- }
107- auto fld = o2::field::MagneticField::createFieldMap (grp->getL3Current (), grp->getDipoleCurrent (), o2::field::MagneticField::kConvLHC , grp->getFieldUniformity ());
108- TGeoGlobalMagField::Instance ()->SetField (fld);
109- TGeoGlobalMagField::Instance ()->Lock ();
110- if (verbose) {
111- LOG (info) << " Running with the B field constructed out of GRP" ;
112- LOG (info) << " Access field via TGeoGlobalMagField::Instance()->Field(xyz,bxyz) or via" ;
113- LOG (info) << " auto o2field = static_cast<o2::field::MagneticField*>( TGeoGlobalMagField::Instance()->GetField() )" ;
114- }
115- return 0 ;
94+ return initFieldFromGRP (grp->getL3Current (), grp->getDipoleCurrent (), grp->getFieldUniformity (), verbose);
11695}
11796
11897// ____________________________________________________________
11998template <typename value_T>
12099int PropagatorImpl<value_T>::initFieldFromGRP(const o2::parameters::GRPMagField* grp, bool verbose)
121100{
122101 // / init mag field from GRP data and attach it to TGeoGlobalMagField
102+ return initFieldFromGRP (grp->getL3Current (), grp->getDipoleCurrent (), grp->getFieldUniformity (), verbose);
103+ }
123104
124- if (TGeoGlobalMagField::Instance ()->IsLocked ()) {
125- if (TGeoGlobalMagField::Instance ()->GetField ()->TestBit (o2::field::MagneticField::kOverrideGRP )) {
126- LOG (warning) << " ExpertMode!!! GRP information will be ignored" ;
127- LOG (warning) << " ExpertMode!!! Running with the externally locked B field" ;
128- return 0 ;
105+ // ____________________________________________________________
106+ template <typename value_T>
107+ int PropagatorImpl<value_T>::initFieldFromGRP(float currL3, float currDip, bool uniform, bool verbose)
108+ {
109+ // / init mag field from GRP data and attach it to TGeoGlobalMagField or rescale already updated field
110+ auto fldGlo = static_cast <o2::field::MagneticField*>(TGeoGlobalMagField::Instance ()->GetField ());
111+ if (fldGlo) { // global field object was already initialized, reuse it if it is locked (as it normally should be)
112+ float _currL3 (currL3), _currDip (currDip);
113+ auto newFieldType = fldGlo->getFieldMapScale (_currL3, _currDip, uniform);
114+ bool sameFieldType = newFieldType == fldGlo->getMapType ();
115+ if (!sameFieldType) {
116+ LOGP (warn, " Existing B-field type {} cannot be rescaled to type {} requested by the GRP" , int (fldGlo->getMapType ()), int (newFieldType));
117+ }
118+ if (TGeoGlobalMagField::Instance ()->IsLocked () && sameFieldType) {
119+ if (Instance ()->mField && Instance ()->mField != fldGlo) { // just make sure that cached field is the same as the global one
120+ std::string name{" PropagatorF" };
121+ if constexpr (std::is_same_v<value_T, double >) {
122+ std::string name{" PropagatorD" };
123+ }
124+ LOGP (fatal, " Magnetic field pointer cached in the {} instance differs from the gloabal field pointer" , name);
125+ }
126+ if (verbose) {
127+ LOGP (info, " Rescaling magnetic field to currents L3: {}, Dipole: {}, UniformityFlag: {}" , currL3, currDip, uniform);
128+ }
129+ fldGlo->rescaleField (currL3, currDip, uniform);
129130 } else {
130- LOG (info) << " Destroying existing B field instance" ;
131+ LOGP (warn, " Destroying existing B field instance. This may invalidate field pointer cached in other objects " ) ;
131132 delete TGeoGlobalMagField::Instance ();
133+ Instance ()->mField = nullptr ;
134+ Instance ()->mFieldFast = nullptr ;
135+ fldGlo = nullptr ;
132136 }
133137 }
134- auto fld = o2::field::MagneticField::createFieldMap (grp->getL3Current (), grp->getDipoleCurrent (), o2::field::MagneticField::kConvLHC , grp->getFieldUniformity ());
135- TGeoGlobalMagField::Instance ()->SetField (fld);
136- TGeoGlobalMagField::Instance ()->Lock ();
137- if (verbose) {
138- LOG (info) << " Running with the B field constructed out of GRP" ;
139- LOG (info) << " Access field via TGeoGlobalMagField::Instance()->Field(xyz,bxyz) or via" ;
140- LOG (info) << " auto o2field = static_cast<o2::field::MagneticField*>( TGeoGlobalMagField::Instance()->GetField() )" ;
138+ if (!fldGlo) {
139+ fldGlo = o2::field::MagneticField::createFieldMap (currL3, currDip, o2::field::MagneticField::kConvLHC , uniform);
140+ TGeoGlobalMagField::Instance ()->SetField (fldGlo);
141+ TGeoGlobalMagField::Instance ()->Lock ();
142+ if (verbose) {
143+ LOG (info) << " Running with the B field constructed out of GRP" ;
144+ LOG (info) << " Access field via TGeoGlobalMagField::Instance()->Field(xyz,bxyz) or via" ;
145+ LOG (info) << " auto o2field = static_cast<o2::field::MagneticField*>( TGeoGlobalMagField::Instance()->GetField() )" ;
146+ }
141147 }
148+ Instance ()->updateField ();
142149 return 0 ;
143150}
144151
0 commit comments