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