Skip to content
Snippets Groups Projects
Commit 3bfebb62 authored by Christopher Rob Jones's avatar Christopher Rob Jones
Browse files

Merge branch 'MagneticFieldExtension-cleanup' into 'master'

MagneticFieldExtension - Prefer return values to INOUT arguments

See merge request !311
parents fd2863e8 841dc0bb
No related branches found
No related tags found
1 merge request!311MagneticFieldExtension - Prefer return values to INOUT arguments
Pipeline #4679702 passed
......@@ -16,7 +16,6 @@
#include "DD4hep/DetFactoryHelper.h"
#include "DD4hep/Detector.h"
#include "DDCond/ConditionsSlice.h"
#include <map>
#include <memory>
#include <string>
#include <utility>
......@@ -32,26 +31,39 @@ namespace LHCb::Magnet {
* @param description The dd4hep::Detector instance for the geometry description
* @param field_map_path The directory containing the field map files
*/
void setup_magnetic_field_extension( dd4hep::Detector& description, std::string field_map_path,
double nominal_current = NominalCurrent );
void setup_magnetic_field_extension( const dd4hep::Detector& description, std::string field_map_path,
const double nominal_current = NominalCurrent );
class MagneticFieldExtension {
public:
MagneticFieldExtension( dd4hep::Detector& description, std::string magFieldDir, double nominal_current )
struct FieldData {
bool useRealMap{true};
bool isDown{true};
double signedRelativeCurrent{1.0};
std::shared_ptr<MagneticFieldGrid> grid;
};
public:
MagneticFieldExtension( const dd4hep::Detector& description, //
std::string magFieldDir, //
const double nominal_current )
: m_description{description}
, m_magneticFieldFilesLocation{std::move( magFieldDir )}
, m_nominalCurrent{nominal_current} {}
// Load the magnetic field map from a specific condition
std::shared_ptr<LHCb::Magnet::MagneticFieldGrid>
load_magnetic_field_grid( dd4hep::cond::ConditionUpdateContext& context, bool& useRealMap, bool& isDown,
double& signedRelativeCurrent );
FieldData load_magnetic_field_grid( const dd4hep::cond::ConditionUpdateContext& context );
const auto& fieldMapLocation() const noexcept { return m_magneticFieldFilesLocation; }
auto nominalCurrent() const noexcept { return m_nominalCurrent; }
private:
// Extension state
dd4hep::Detector& m_description; ///< Description
std::string m_magneticFieldFilesLocation; ///< Loaded from the conditions
double m_nominalCurrent{NominalCurrent}; ///< Nominal current in amps
const dd4hep::Detector& m_description; ///< Description
std::string m_magneticFieldFilesLocation; ///< Loaded from the conditions
double m_nominalCurrent{NominalCurrent}; ///< Nominal current in amps
};
} // End namespace LHCb::Magnet
......@@ -21,13 +21,18 @@
#include <nlohmann/json.hpp>
#include <cassert>
#include <memory>
#include <utility>
void LHCb::Magnet::setup_magnetic_field_extension( dd4hep::Detector& description, std::string field_map_path,
double nominal_current ) {
using namespace LHCb::Magnet;
using LHCb::Detector::ConditionKey;
using nlohmann::json;
void LHCb::Magnet::setup_magnetic_field_extension( const dd4hep::Detector& description, std::string field_map_path,
const double nominal_current ) {
auto magnetdet = description.detector( "Magnet" );
using Ext_t = LHCb::Magnet::MagneticFieldExtension;
using Ext_t = MagneticFieldExtension;
auto mfhelper = std::make_unique<Ext_t>( description, std::move( field_map_path ), nominal_current );
auto dExt = std::make_unique<dd4hep::detail::DeleteExtension<Ext_t, Ext_t>>( mfhelper.release() );
magnetdet.addExtension( dExt.release() );
......@@ -36,47 +41,44 @@ void LHCb::Magnet::setup_magnetic_field_extension( dd4hep::Detector& description
/*
* Method to load the magnetic field file based on the conditions
*/
std::shared_ptr<LHCb::Magnet::MagneticFieldGrid> //
LHCb::Magnet::MagneticFieldExtension::load_magnetic_field_grid( dd4hep::cond::ConditionUpdateContext& context,
bool& useRealMap, bool& isDown,
double& signedRelativeCurrent ) {
using LHCb::Detector::ConditionKey;
using nlohmann::json;
MagneticFieldExtension::FieldData
MagneticFieldExtension::load_magnetic_field_grid( const dd4hep::cond::ConditionUpdateContext& context ) {
using LHCb::Detector::ConditionKey;
MagneticFieldGridReader reader{m_magneticFieldFilesLocation};
LHCb::Magnet::MagneticFieldGridReader reader{m_magneticFieldFilesLocation};
FieldData data;
auto grid = std::make_shared<LHCb::Magnet::MagneticFieldGrid>();
data.grid = std::make_shared<LHCb::Magnet::MagneticFieldGrid>();
// Getting the Magnet DetectorElement
auto magnetdet = m_description.detector( "Magnet" );
// Loading the current value
auto& magnet_cond = context.condition( ConditionKey( magnetdet, "Magnet" ) ).get<json>();
const auto current = magnet_cond["Current"].get<double>();
const auto polarity = magnet_cond["Polarity"].get<int>();
const auto& magnet_cond = context.condition( ConditionKey( magnetdet, "Magnet" ) ).get<json>();
const auto current = magnet_cond["Current"].get<double>();
const auto polarity = magnet_cond["Polarity"].get<int>();
dd4hep::printout( dd4hep::DEBUG, "MagneticFieldExtension", "Current value: %f", current );
dd4hep::printout( dd4hep::DEBUG, "MagneticFieldExtension", "Current polarity: %d", polarity );
// Loading the appropriate map file list depending on the polarity
const std::string mapCondName = polarity > 0 ? "FieldMapFilesUp" : "FieldMapFilesDown";
auto& magnet_filescond = context.condition( ConditionKey( magnetdet, mapCondName ) ).get<json>();
const auto& magnet_filescond = context.condition( ConditionKey( magnetdet, mapCondName ) ).get<json>();
const auto filenames = magnet_filescond["Files"].get<std::vector<std::string>>();
assert( !filenames.empty() );
// Loading the ScaleUp/ScaleDown attributes
std::string scaleCondName = polarity > 0 ? "ScaleUp" : "ScaleDown";
auto& magnet_scalecond = context.condition( ConditionKey( magnetdet, scaleCondName ) ).get<json>();
const auto coeffs = magnet_scalecond["Coeffs"].get<std::vector<double>>();
const std::string scaleCondName = polarity > 0 ? "ScaleUp" : "ScaleDown";
const auto& magnet_scalecond = context.condition( ConditionKey( magnetdet, scaleCondName ) ).get<json>();
const auto coeffs = magnet_scalecond["Coeffs"].get<std::vector<double>>();
// Computing the scale factor for the magnetic field
const auto scale_factor = coeffs[0] + ( coeffs[1] * ( current / m_nominalCurrent ) );
const auto scale_factor = coeffs[0] + ( coeffs[1] * ( current / nominalCurrent() ) );
dd4hep::printout( dd4hep::INFO, "MagneticFieldExtension", "Scale factor: %f", scale_factor );
grid->setScaleFactor( scale_factor );
data.grid->setScaleFactor( scale_factor );
// loading the files into the grid
const auto sc = ( filenames.size() == 1 ? reader.readDC06File( filenames.front(), *grid )
: reader.readFiles( filenames, *grid ) );
const auto sc = ( filenames.size() == 1 ? reader.readDC06File( filenames.front(), *data.grid )
: reader.readFiles( filenames, *data.grid ) );
if ( !sc ) {
dd4hep::printout( dd4hep::ERROR, "MagneticFieldExtension", "Error loading magnetic field map" );
dd4hep::except( "load_magnetic_field_map", "Error loading magnetic field map" );
......@@ -84,12 +86,9 @@ LHCb::Magnet::MagneticFieldExtension::load_magnetic_field_grid( dd4hep::cond::Co
// Imported from the MagneticFieldSvc
// Setting constants
useRealMap = ( filenames.size() == 4 );
isDown = grid->fieldVectorClosestPoint( {0, 0, 5200} ).y() < 0;
// current
const int sign = ( isDown ? -1 : +1 );
signedRelativeCurrent = std::abs( scale_factor ) * sign;
data.useRealMap = ( filenames.size() == 4 );
data.isDown = data.grid->fieldVectorClosestPoint( {0, 0, 5200} ).y() < 0;
data.signedRelativeCurrent = std::abs( scale_factor ) * ( data.isDown ? -1 : +1 );
return grid;
return data;
}
......@@ -20,6 +20,8 @@
#include <yaml-cpp/yaml.h>
#include <memory>
// Callback to setup the DeMagnet object
namespace LHCb::Detector {
......@@ -36,20 +38,16 @@ namespace LHCb::Detector {
auto magnetdet = dd4hep::Detector::getInstance().detector( "Magnet" );
// Loading the Magnetic field
LHCb::Magnet::MagneticFieldExtension* mfhelper = magnetdet.extension<LHCb::Magnet::MagneticFieldExtension>();
auto mfhelper = magnetdet.extension<LHCb::Magnet::MagneticFieldExtension>();
dd4hep::printout( dd4hep::INFO, "DeMagnetConditionCall", "Loading mag field from %s",
mfhelper->m_magneticFieldFilesLocation.c_str() );
mfhelper->fieldMapLocation().c_str() );
// Creating the Detector element with the desired field
bool useRealMap;
bool isDown;
double signedRelativeCurrent;
auto magnetic_field_grid =
mfhelper->load_magnetic_field_grid( context, useRealMap, isDown, signedRelativeCurrent );
auto demagnetobj = new detail::DeMagnetObject( magnetdet, context, magnetic_field_grid, useRealMap, isDown,
signedRelativeCurrent );
const auto data = mfhelper->load_magnetic_field_grid( context );
auto demagnetobj = std::make_unique<detail::DeMagnetObject>( magnetdet, context, data.grid, data.useRealMap,
data.isDown, data.signedRelativeCurrent );
return DeIOV( demagnetobj );
return DeIOV( demagnetobj.release() );
}
throw std::logic_error( "Key unknown to DeMagnetConditionCall" );
}
......@@ -66,4 +64,5 @@ static long create_conditions_recipes( dd4hep::Detector& description, xml_h e )
return 1;
}
DECLARE_XML_DOC_READER( LHCb_Magnet_cond, create_conditions_recipes )
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment