diff --git a/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.cpp b/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.cpp index 9b3218b3c2f47521c892e854e2732728cb7e7a72..4d6a9306b215de904c996ba134325fa114377e84 100644 --- a/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.cpp +++ b/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.cpp @@ -19,10 +19,14 @@ void GenericBFieldMapBrBz::fieldComponents(const double* pos, double* field) { throw std::runtime_error(error_msg); } + if (m_length_unit<=0||m_bfield_unit<=0) { + std::string error_msg = "[ERROR] GenericBFieldMapBrBz: Not set units or error (<0)! "; + throw std::runtime_error(error_msg); + } // convert pos to r/z - double x = pos[0] / dd4hep::m; // convert to meter - double y = pos[1] / dd4hep::m; - double z = pos[2] / dd4hep::m; + double x = pos[0] / m_length_unit; // convert to length unit from input + double y = pos[1] / m_length_unit; + double z = pos[2] / m_length_unit; double r = sqrt(x*x+y*y); double phi = atan2(y, x); @@ -98,9 +102,9 @@ void GenericBFieldMapBrBz::fieldComponents(const double* pos, double* field) { + rn * zn * Bz_r1z1; // update the global field - field[0] += Br*cos(phi); - field[1] += Br*sin(phi); - field[2] += Bz; + field[0] += Br*cos(phi)*m_bfield_unit; // convert to input unit + field[1] += Br*sin(phi)*m_bfield_unit; + field[2] += Bz*m_bfield_unit; return; } @@ -116,3 +120,9 @@ void GenericBFieldMapBrBz::init_provider(const std::string& provider, const std: throw std::runtime_error(error_msg); } } + +void GenericBFieldMapBrBz::init_unit(double l, double b) { + m_length_unit = l; + m_bfield_unit = b; + std::cout << "Initialize units to l = " << m_length_unit/dd4hep::m << " m" << ", b = " << m_bfield_unit/dd4hep::tesla << " tesla" << std::endl; +} diff --git a/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.h b/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.h index 93abfcfbbb5650d5e5945b774280e6586b1c17c3..1907b0ae154733c6caf6ece78967416d745f8b4c 100644 --- a/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.h +++ b/Detector/MagneticFieldMap/src/GenericBFieldMapBrBz.h @@ -26,9 +26,15 @@ public: // following are interfaces to configure this field map void init_provider(const std::string& provider, const std::string& url); + // set unit + void init_unit(double l, double b); + private: IFieldMapProvider* m_provider; + + double m_length_unit = 0; + double m_bfield_unit = 0; }; #endif diff --git a/Detector/MagneticFieldMap/src/GenericBFieldMapBrBzFactory.cpp b/Detector/MagneticFieldMap/src/GenericBFieldMapBrBzFactory.cpp index d7d9fe302d75e214e290f52ebf2382441df305c0..28d6fe24f04e3ff631722b59ae630a1dfa608808 100644 --- a/Detector/MagneticFieldMap/src/GenericBFieldMapBrBzFactory.cpp +++ b/Detector/MagneticFieldMap/src/GenericBFieldMapBrBzFactory.cpp @@ -70,6 +70,19 @@ static dd4hep::Ref_t create_GenericBFieldMapBrBz(dd4hep::Detector& , ptr->init_provider(provider, url); + // set unit, default values will be m and tesla + double lunit = dd4hep::m; + double bunit = dd4hep::tesla; + bool hasLunit = xmlParameter.hasAttr(_Unicode(lunit)); + if (hasLunit) { + lunit = xmlParameter.attr<double>(_Unicode(lunit)); + } + bool hasBunit = xmlParameter.hasAttr(_Unicode(bunit)); + if (hasBunit) { + bunit = xmlParameter.attr<double>(_Unicode(bunit)); + } + ptr->init_unit(lunit, bunit); + obj.assign(ptr, xmlParameter.nameStr(), xmlParameter.typeStr()); return obj;