forked from sPHENIX-Collaboration/acts
/
BFieldUtils.cpp
201 lines (179 loc) · 6.58 KB
/
BFieldUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// This file is part of the Acts project.
//
// Copyright (C) 2017 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "ActsExamples/Plugins/BField/BFieldUtils.hpp"
#include "Acts/MagneticField/BFieldMapUtils.hpp"
#include "Acts/Utilities/detail/Axis.hpp"
#include "Acts/Utilities/detail/Grid.hpp"
#include <fstream>
#include "TFile.h"
#include "TROOT.h"
#include "TTree.h"
Acts::InterpolatedBFieldMapper<
Acts::detail::Grid<Acts::Vector2D, Acts::detail::EquidistantAxis,
Acts::detail::EquidistantAxis>>
ActsExamples::BField::txt::fieldMapperRZ(
std::function<size_t(std::array<size_t, 2> binsRZ,
std::array<size_t, 2> nBinsRZ)>
localToGlobalBin,
std::string fieldMapFile, double lengthUnit, double BFieldUnit,
size_t nPoints, bool firstQuadrant) {
/// [1] Read in field map file
// Grid position points in r and z
std::vector<double> rPos;
std::vector<double> zPos;
// components of magnetic field on grid points
std::vector<Acts::Vector2D> bField;
// reserve estimated size
rPos.reserve(nPoints);
zPos.reserve(nPoints);
bField.reserve(nPoints);
// [1] Read in file and fill values
std::ifstream map_file(fieldMapFile.c_str(), std::ios::in);
std::string line;
double r = 0., z = 0.;
double br = 0., bz = 0.;
while (std::getline(map_file, line)) {
if (line.empty() || line[0] == '%' || line[0] == '#' ||
line.find_first_not_of(' ') == std::string::npos)
continue;
std::istringstream tmp(line);
tmp >> r >> z >> br >> bz;
rPos.push_back(r);
zPos.push_back(z);
bField.push_back(Acts::Vector2D(br, bz));
}
map_file.close();
/// [2] use helper function in core
return Acts::fieldMapperRZ(localToGlobalBin, rPos, zPos, bField, lengthUnit,
BFieldUnit, firstQuadrant);
}
Acts::InterpolatedBFieldMapper<Acts::detail::Grid<
Acts::Vector3D, Acts::detail::EquidistantAxis,
Acts::detail::EquidistantAxis, Acts::detail::EquidistantAxis>>
ActsExamples::BField::txt::fieldMapperXYZ(
std::function<size_t(std::array<size_t, 3> binsXYZ,
std::array<size_t, 3> nBinsXYZ)>
localToGlobalBin,
std::string fieldMapFile, double lengthUnit, double BFieldUnit,
size_t nPoints, bool firstOctant) {
/// [1] Read in field map file
// Grid position points in x, y and z
std::vector<double> xPos;
std::vector<double> yPos;
std::vector<double> zPos;
// components of magnetic field on grid points
std::vector<Acts::Vector3D> bField;
// reserve estimated size
xPos.reserve(nPoints);
yPos.reserve(nPoints);
zPos.reserve(nPoints);
bField.reserve(nPoints);
// [1] Read in file and fill values
std::ifstream map_file(fieldMapFile.c_str(), std::ios::in);
std::string line;
double x = 0., y = 0., z = 0.;
double bx = 0., by = 0., bz = 0.;
while (std::getline(map_file, line)) {
if (line.empty() || line[0] == '%' || line[0] == '#' ||
line.find_first_not_of(' ') == std::string::npos)
continue;
std::istringstream tmp(line);
tmp >> x >> y >> z >> bx >> by >> bz;
xPos.push_back(x);
yPos.push_back(y);
zPos.push_back(z);
bField.push_back(Acts::Vector3D(bx, by, bz));
}
map_file.close();
return Acts::fieldMapperXYZ(localToGlobalBin, xPos, yPos, zPos, bField,
lengthUnit, BFieldUnit, firstOctant);
}
Acts::InterpolatedBFieldMapper<
Acts::detail::Grid<Acts::Vector2D, Acts::detail::EquidistantAxis,
Acts::detail::EquidistantAxis>>
ActsExamples::BField::root::fieldMapperRZ(
std::function<size_t(std::array<size_t, 2> binsRZ,
std::array<size_t, 2> nBinsRZ)>
localToGlobalBin,
std::string fieldMapFile, std::string treeName, double lengthUnit,
double BFieldUnit, bool firstQuadrant) {
/// [1] Read in field map file
// Grid position points in r and z
std::vector<double> rPos;
std::vector<double> zPos;
// components of magnetic field on grid points
std::vector<Acts::Vector2D> bField;
// [1] Read in file and fill values
TFile* inputFile = TFile::Open(fieldMapFile.c_str());
TTree* tree = (TTree*)inputFile->Get(treeName.c_str());
Int_t entries = tree->GetEntries();
float r, z;
float Br, Bz;
tree->SetBranchAddress("r", &r);
tree->SetBranchAddress("z", &z);
tree->SetBranchAddress("br", &Br);
tree->SetBranchAddress("bz", &Bz);
// reserve size
rPos.reserve(entries);
zPos.reserve(entries);
bField.reserve(entries);
for (int i = 0; i < entries; i++) {
tree->GetEvent(i);
rPos.push_back(r);
zPos.push_back(z);
bField.push_back(Acts::Vector2D(Br, Bz));
}
inputFile->Close();
/// [2] use helper function in core
return Acts::fieldMapperRZ(localToGlobalBin, rPos, zPos, bField, lengthUnit,
BFieldUnit, firstQuadrant);
}
Acts::InterpolatedBFieldMapper<Acts::detail::Grid<
Acts::Vector3D, Acts::detail::EquidistantAxis,
Acts::detail::EquidistantAxis, Acts::detail::EquidistantAxis>>
ActsExamples::BField::root::fieldMapperXYZ(
std::function<size_t(std::array<size_t, 3> binsXYZ,
std::array<size_t, 3> nBinsXYZ)>
localToGlobalBin,
std::string fieldMapFile, std::string treeName, double lengthUnit,
double BFieldUnit, bool firstOctant) {
/// [1] Read in field map file
// Grid position points in x, y and z
std::vector<double> xPos;
std::vector<double> yPos;
std::vector<double> zPos;
// components of magnetic field on grid points
std::vector<Acts::Vector3D> bField;
// [1] Read in file and fill values
TFile* inputFile = TFile::Open(fieldMapFile.c_str());
TTree* tree = (TTree*)inputFile->Get(treeName.c_str());
Int_t entries = tree->GetEntries();
float x, y, z;
float Bx, By, Bz;
tree->SetBranchAddress("x", &x);
tree->SetBranchAddress("y", &y);
tree->SetBranchAddress("z", &z);
tree->SetBranchAddress("bx", &Bx);
tree->SetBranchAddress("by", &By);
tree->SetBranchAddress("bz", &Bz);
// reserve size
xPos.reserve(entries);
yPos.reserve(entries);
zPos.reserve(entries);
bField.reserve(entries);
for (int i = 0; i < entries; i++) {
tree->GetEvent(i);
xPos.push_back(x);
yPos.push_back(y);
zPos.push_back(z);
bField.push_back(Acts::Vector3D(Bx, By, Bz));
}
inputFile->Close();
return Acts::fieldMapperXYZ(localToGlobalBin, xPos, yPos, zPos, bField,
lengthUnit, BFieldUnit, firstOctant);
}