Skip to content

Commit 0b7791e

Browse files
authored
Refactor field function and improve readability
Refactored field function to use static constexpr for constants and improved readability. Updated condition checks to use std::abs.
1 parent 595090e commit 0b7791e

1 file changed

Lines changed: 68 additions & 22 deletions

File tree

Detectors/Upgrades/ALICE3/macros/ALICE3Field.C

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,41 @@
1111
//
1212
// Author: J. E. Munoz Mendez jesus.munoz@cern.ch
1313

14+
#include <functional>
15+
#include <cmath>
16+
#include <TCanvas.h>
17+
#include <TH2F.h>
18+
#include <TStyle.h>
19+
1420
std::function<void(const double*, double*)> field()
1521
{
1622
return [](const double* x, double* b) {
17-
double Rc;
18-
double R1;
19-
double R2;
20-
double B1;
21-
double B2;
22-
double beamStart = 500.; //[cm]
23-
double tokGauss = 1. / 0.1; // conversion from Tesla to kGauss
24-
25-
bool isMagAbs = true;
23+
// RADIUS
24+
static constexpr double Rc = 170.; // [cm] — R_out_coil per Ian DetectorConstruction.cc; confirmed by A. Ortiz definition
25+
static constexpr double R1 = 220.; // [cm]
26+
static constexpr double R2 = 290.; // [cm]
2627

27-
// ***********************
28-
// LAYOUT 1
29-
// ***********************
28+
// FIELD
29+
static constexpr double B1 = 2.; // [T]
30+
static constexpr double B2 = -B1 * Rc * Rc / (R2 * R2 - R1 * R1); // [T] — B1 in numerator, confirmed by A. Ortiz Aug 2026
31+
static constexpr double beamStart = 500.; // [cm]
32+
static constexpr double tokGauss = 1. / 0.1; // conversion from Tesla to kGauss
3033

31-
// RADIUS
32-
Rc = 170.; //[cm] — R_out_coil per Ian DetectorConstruction.cc; confirmed by A. Ortiz definition
33-
R1 = 220.; //[cm]
34-
R2 = 290.; //[cm]
34+
static constexpr bool isMagAbs = true;
3535

3636
// To set the B2
37-
B1 = 2.; //[T]
38-
B2 = -B1 * Rc * Rc / (R2 * R2 - R1 * R1); //[T] — B1 in numerator, confirmed by A. Ortiz Aug 2026
3937

40-
if ((abs(x[2]) <= beamStart) && (sqrt(x[0] * x[0] + x[1] * x[1]) < Rc)) {
38+
if ((std::abs(x[2]) <= beamStart) && (sqrt(x[0] * x[0] + x[1] * x[1]) < Rc)) {
4139
b[0] = 0.;
4240
b[1] = 0.;
4341
b[2] = B1 * tokGauss;
44-
} else if ((abs(x[2]) <= beamStart) &&
42+
} else if ((std::abs(x[2]) <= beamStart) &&
4543
(sqrt(x[0] * x[0] + x[1] * x[1]) >= Rc &&
4644
sqrt(x[0] * x[0] + x[1] * x[1]) < R1)) {
4745
b[0] = 0.;
4846
b[1] = 0.;
4947
b[2] = 0.;
50-
} else if ((abs(x[2]) <= beamStart) &&
48+
} else if ((std::abs(x[2]) <= beamStart) &&
5149
(sqrt(x[0] * x[0] + x[1] * x[1]) >= R1 &&
5250
sqrt(x[0] * x[0] + x[1] * x[1]) < R2)) {
5351
b[0] = 0.;
@@ -63,4 +61,52 @@ std::function<void(const double*, double*)> field()
6361
b[2] = 0.;
6462
}
6563
};
66-
}
64+
}
65+
66+
void ALICE3Field()
67+
{
68+
gStyle->SetPalette(kRainBow);
69+
gStyle->SetNumberContours(255);
70+
71+
auto fieldFunc = field();
72+
// RZ plane visualization
73+
TCanvas* cRZ = new TCanvas("cRZ", "Field in RZ plane", 800, 800);
74+
gPad->SetRightMargin(0.15);
75+
TH2F* hRZ = new TH2F("hRZ", "Magnetic Field B_z in RZ plane;Z [m];R [m];B_{z} [kGauss]", 100, -10, 10, 100, -5, 5);
76+
hRZ->SetBit(TH1::kNoStats); // disable stats box
77+
for (int i = 1; i <= hRZ->GetNbinsX(); i++) {
78+
const double Z = hRZ->GetXaxis()->GetBinCenter(i);
79+
for (int j = 1; j <= hRZ->GetNbinsY(); j++) {
80+
const double R = hRZ->GetYaxis()->GetBinCenter(j);
81+
const double pos[3] = {R * 100, 0, Z * 100}; // convert to cm
82+
double b[3] = {0, 0, 0};
83+
fieldFunc(pos, b);
84+
hRZ->SetBinContent(i, j, b[2]);
85+
}
86+
}
87+
88+
hRZ->GetZaxis()->SetRangeUser(-30, 30);
89+
hRZ->Draw("COLZ");
90+
cRZ->Update();
91+
92+
// XY plane visualization
93+
TCanvas* cXY = new TCanvas("cXY", "Field in XY plane", 800, 800);
94+
gPad->SetRightMargin(0.15);
95+
TH2F* hXY = new TH2F("hXY", "Magnetic Field B_z in XY plane;X [m];Y [m];B_{z} [kGauss]", 100, -5, 5, 100, -5, 5);
96+
hXY->SetBit(TH1::kNoStats); // disable stats box
97+
98+
for (int i = 1; i <= hXY->GetNbinsX(); i++) {
99+
const double X = hXY->GetXaxis()->GetBinCenter(i);
100+
for (int j = 1; j <= hXY->GetNbinsY(); j++) {
101+
const double Y = hXY->GetYaxis()->GetBinCenter(j);
102+
const double pos[3] = {X * 100, Y * 100, 0}; // convert to cm
103+
double b[3] = {0, 0, 0};
104+
fieldFunc(pos, b);
105+
hXY->SetBinContent(i, j, b[2]);
106+
}
107+
}
108+
109+
hXY->GetZaxis()->SetRangeUser(-30, 30);
110+
hXY->Draw("COLZ");
111+
cXY->Update();
112+
}

0 commit comments

Comments
 (0)