Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FieldMapRootIo.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FieldMapRootIo.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2021 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 
12 
13 #include <stdexcept>
14 #include <vector>
15 
16 #include <RtypesCore.h>
17 #include <TFile.h>
18 #include <TTree.h>
19 
22  const std::function<size_t(std::array<size_t, 2> binsRZ,
23  std::array<size_t, 2> nBinsRZ)>&
24  localToGlobalBin,
25  const std::string& fieldMapFile, const std::string& treeName,
27  bool firstQuadrant) {
29  // Grid position points in r and z
30  std::vector<double> rPos;
31  std::vector<double> zPos;
32  // components of magnetic field on grid points
33  std::vector<Acts::Vector2> bField;
34  // [1] Read in file and fill values
35  TFile* inputFile = TFile::Open(fieldMapFile.c_str());
36  if (inputFile == nullptr) {
37  throw std::runtime_error("file does not exist");
38  }
39  TTree* tree = inputFile->Get<TTree>(treeName.c_str());
40  if (tree == nullptr) {
41  throw std::runtime_error("object not found in file");
42  }
43  Int_t entries = tree->GetEntries();
44 
45  double r = 0, z = 0;
46  double Br = 0, Bz = 0;
47 
48  tree->SetBranchAddress("r", &r);
49  tree->SetBranchAddress("z", &z);
50 
51  tree->SetBranchAddress("Br", &Br);
52  tree->SetBranchAddress("Bz", &Bz);
53 
54  // reserve size
55  rPos.reserve(entries);
56  zPos.reserve(entries);
57  bField.reserve(entries);
58 
59  for (int i = 0; i < entries; i++) {
60  tree->GetEvent(i);
61  rPos.push_back(r);
62  zPos.push_back(z);
63  bField.push_back(Acts::Vector2(Br, Bz));
64  }
65  delete inputFile;
67  return Acts::fieldMapRZ(localToGlobalBin, rPos, zPos, bField, lengthUnit,
68  BFieldUnit, firstQuadrant);
69 }
70 
73  const std::function<size_t(std::array<size_t, 3> binsXYZ,
74  std::array<size_t, 3> nBinsXYZ)>&
75  localToGlobalBin,
76  const std::string& fieldMapFile, const std::string& treeName,
78  bool firstOctant) {
80  // Grid position points in x, y and z
81  std::vector<double> xPos;
82  std::vector<double> yPos;
83  std::vector<double> zPos;
84  // components of magnetic field on grid points
85  std::vector<Acts::Vector3> bField;
86  // [1] Read in file and fill values
87  TFile* inputFile = TFile::Open(fieldMapFile.c_str());
88  if (inputFile == nullptr) {
89  throw std::runtime_error("file does not exist");
90  }
91  TTree* tree = inputFile->Get<TTree>(treeName.c_str());
92  if (tree == nullptr) {
93  throw std::runtime_error("object not found in file");
94  }
95  Int_t entries = tree->GetEntries();
96 
97  float x = 0, y = 0, z = 0;
98  float Bx = 0, By = 0, Bz = 0;
99 
100  tree->SetBranchAddress("x", &x);
101  tree->SetBranchAddress("y", &y);
102  tree->SetBranchAddress("z", &z);
103 
104  tree->SetBranchAddress("bx", &Bx);
105  tree->SetBranchAddress("by", &By);
106  tree->SetBranchAddress("bz", &Bz);
107 
108  // reserve size
109  xPos.reserve(entries);
110  yPos.reserve(entries);
111  zPos.reserve(entries);
112  bField.reserve(entries);
113 
114  for (int i = 0; i < entries; i++) {
115  tree->GetEvent(i);
116  xPos.push_back(x);
117  yPos.push_back(y);
118  zPos.push_back(z);
119  bField.push_back(Acts::Vector3(Bx, By, Bz));
120  }
121  delete inputFile;
122 
123  return Acts::fieldMapXYZ(localToGlobalBin, xPos, yPos, zPos, bField,
124  lengthUnit, BFieldUnit, firstOctant);
125 }