Skip to content

feat: make FieldMapB rotation and translation optional #895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions compact/far_backward/lumi/lumi_magnets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ Construct the sweeper and analyzer dipole magnets for the luminosity subsystem.
<Y step="2.0*cm" min="-34*cm" max="34*cm" />
<Z step="2.0*cm" min="-80*cm" max="80*cm" />
<translationCoord x="LumiSweepMag_X" y="LumiSweepMag_Y" z="LumiSweepMag_Z" />
<rotationField x="0" y="0" z="0" />
</dimensions>
</field>

Expand All @@ -121,7 +120,6 @@ Construct the sweeper and analyzer dipole magnets for the luminosity subsystem.
<Y step="2.0*cm" min="-34*cm" max="34*cm" />
<Z step="2.0*cm" min="-80*cm" max="80*cm" />
<translationCoord x="LumiAnalyzerMag_X" y="LumiAnalyzerMag_Y" z="LumiAnalyzerMag_Z" />
<rotationField x="0" y="0" z="0" />
</dimensions>
</field>

Expand Down
2 changes: 0 additions & 2 deletions compact/fields/marco.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<dimensions>
<R step="2.0*cm" min="0*cm" max="998*cm" />
<Z step="2.0*cm" min="-800*cm" max="798*cm" />
<translationCoord x="0.0*cm" y="0.0*cm" z="0.0*cm" />
<rotationField x="0" y="0" z="0" />
</dimensions>
</field>
</fields>
Expand Down
41 changes: 24 additions & 17 deletions src/FieldMapB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -57,8 +58,10 @@ class FieldMapB : public dd4hep::CartesianField::Object {

private:
FieldCoord fieldCoord; // field coordinate type
Transform3D coordTranslate, coordTranslate_inv; // coord translation
Transform3D fieldRot, fieldRot_inv; // field rotation
std::optional<Transform3D> coordTranslate{std::nullopt}; // coord translation
std::optional<Transform3D> coordTranslate_inv{std::nullopt};
std::optional<Transform3D> fieldRot{std::nullopt}; // field rotation
std::optional<Transform3D> fieldRot_inv{std::nullopt};
std::vector<float> steps, mins, maxs; // B map cell info
int ir, ix, iy, iz; // lookup indices
float dr, dx, dy, dz; // deltas for interpolation
Expand Down Expand Up @@ -199,7 +202,9 @@ void FieldMapB::LoadMap(const std::string& map_file, float scale) {
} else { // scale and rotate B field vector
auto B = ROOT::Math::XYZPoint(Bcomp[0], Bcomp[1], Bcomp[2]);
B *= scale * float(tesla);
B = fieldRot * B;
if (fieldRot.has_value()) {
B = fieldRot.value() * B;
}
Bvals_XYZ[ix][iy][iz] = {float(B.x()), float(B.y()), float(B.z())};
}
}
Expand All @@ -209,13 +214,14 @@ void FieldMapB::LoadMap(const std::string& map_file, float scale) {
// get field components
void FieldMapB::fieldComponents(const double* pos, double* field) {
// coordinate conversion
auto p = coordTranslate_inv * ROOT::Math::XYZPoint(pos[0], pos[1], pos[2]);
auto p = coordTranslate_inv.has_value()
? coordTranslate_inv.value() * ROOT::Math::XYZPoint(pos[0], pos[1], pos[2])
: ROOT::Math::XYZPoint(pos[0], pos[1], pos[2]);

if (fieldCoord == FieldCoord::BrBz) {
// coordinates conversion
const float r = sqrt(p.x() * p.x() + p.y() * p.y());
const float z = p.z();
const float phi = atan2(p.y(), p.x());
const float r = sqrt(p.x() * p.x() + p.y() * p.y());
const float z = p.z();

if (!GetIndices(r, z, &ir, &iz, &dr, &dz)) {
// out of range
Expand All @@ -238,7 +244,10 @@ void FieldMapB::fieldComponents(const double* pos, double* field) {
p3[1] * dr * dz;

// convert Br Bz to Bx By Bz and rotate field
auto B = fieldRot * ROOT::Math::XYZPoint(Br * cos(phi), Br * sin(phi), Bz);
const float phi = atan2(p.y(), p.x());
auto B = fieldRot.has_value()
? fieldRot.value() * ROOT::Math::XYZPoint(Br * cos(phi), Br * sin(phi), Bz)
: ROOT::Math::XYZPoint(Br * cos(phi), Br * sin(phi), Bz);
field[0] += B.x();
field[1] += B.y();
field[2] += B.z();
Expand Down Expand Up @@ -324,21 +333,19 @@ static Ref_t create_field_map_b(Detector& /*lcdd*/, xml::Handle_t handle) {
auto map = new FieldMapB(field_type, coord_type);
map->Configure(dimensions);

// translation, rotation
static float deg2r = ROOT::Math::Pi() / 180.;
RotationZYX rot(0., 0., 0.);
// translation
if (x_dim.hasChild(_Unicode(rotationField))) {
static float deg2r = ROOT::Math::Pi() / 180.;
xml_comp_t rot_dim = x_dim.child(_Unicode(rotationField));
rot = RotationZYX(rot_dim.z() * deg2r, rot_dim.y() * deg2r, rot_dim.x() * deg2r);
RotationZYX rot(rot_dim.z() * deg2r, rot_dim.y() * deg2r, rot_dim.x() * deg2r);
map->SetFieldRotation(Transform3D(rot));
}

Translation3D trans(0., 0., 0.);
// rotation
if (x_dim.hasChild(_Unicode(translationCoord))) {
xml_comp_t trans_dim = x_dim.child(_Unicode(translationCoord));
trans = Translation3D(trans_dim.x(), trans_dim.y(), trans_dim.z());
Translation3D trans(trans_dim.x(), trans_dim.y(), trans_dim.z());
map->SetCoordTranslation(Transform3D(trans));
}
map->SetCoordTranslation(Transform3D(trans));
map->SetFieldRotation(Transform3D(rot));

map->LoadMap(field_map_file, field_map_scale);
field.assign(map, x_par.nameStr(), "FieldMapB");
Expand Down
Loading