Skip to content

Commit 7a8e2ec

Browse files
committed
Corrected indexing and compiler issue
1 parent 2772a6c commit 7a8e2ec

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/geant4/app/src/DagSolidMagneticField.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,35 @@ void MagneticField::ProcessField() {
7575
int MagneticField::FindRadialIndex(const double r) const {
7676
int radialIndex = 0;
7777
// given r find the owning radial index
78-
const std::vector<double>::const_iterator it_r = std::lower_bound(r_point.begin(),
78+
const std::vector<double>::const_iterator it_r = std::upper_bound(r_point.begin(),
7979
r_point.end(), r);
80-
radialIndex = it_r - r_point.begin();
80+
81+
// if we are pointing outside return the last element
82+
if (it_r - r_point.begin() > r_point.size() - 1)
83+
radialIndex = r_point.size() - 1;
84+
else if (it_r - r_point.begin() <= 0 )
85+
radialIndex = 0;
86+
else
87+
radialIndex = it_r - r_point.begin();
8188

8289
return radialIndex;
8390
}
8491

8592
// given the vertical position return its index
8693
int MagneticField::FindVerticalIndex(const double z) const {
8794
int verticalIndex = 0;
95+
8896
// given z find the owning vertical index
8997
std::vector<double>::const_iterator it_z = std::lower_bound(z_point.begin(),
9098
z_point.end(), z);
91-
verticalIndex = it_z - z_point.begin();
99+
100+
// if we are pointing outside return the last element
101+
if (it_z - z_point.begin() > z_point.size() - 1)
102+
verticalIndex = z_point.size() - 1;
103+
else if (it_z - z_point.begin() <= 0 )
104+
verticalIndex = 0;
105+
else
106+
verticalIndex = it_z - z_point.begin();
92107

93108
return verticalIndex;
94109
}
@@ -178,7 +193,7 @@ std::array<std::array<double,4>, 3> MagneticField::GetFourFieldValuesByIndex(con
178193

179194
// order of values needs to be: p[0,0], p[0,1], p[1,0], p[1,1];
180195
for ( int dir = 0 ; dir < 3 ; dir++ ) {
181-
int index = (r_index-1)*n_z + z_index-1; // q11
196+
int index = r_index*n_z + z_index; // q11
182197
values[dir][0] = b_field[index][dir];
183198
index = (r_index-1)*n_z + z_index; // q12
184199
values[dir][1] = b_field[index][dir];

src/geant4/app/test/dagsolid_magfield_unit_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,14 @@ TEST_F(DagSolidMagneticFieldTest, sample_test) {
9595
EXPECT_EQ(0.0, field[1]);
9696
EXPECT_EQ(0.0, field[2]);
9797

98+
point[0] = 1100.0;
99+
point[1] = 0.0;
100+
point[2] = 0.0;
101+
102+
magneticField->GetFieldValue(point,field);
103+
// expect field x to be 0.0
104+
EXPECT_EQ(0.0, field[0]);
105+
EXPECT_EQ(0.0, field[1]);
106+
EXPECT_EQ(0.0, field[2]);
98107
return;
99108
}

0 commit comments

Comments
 (0)