Belle II Software  release-08-01-10
FieldManager.h
1 /* Copyright 2008-2010, Technische Universitaet Muenchen,
2  Authors: Christian Hoeppner & Sebastian Neubert & Johannes Rauch
3 
4  This file is part of GENFIT.
5 
6  GENFIT is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  GENFIT is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with GENFIT. If not, see <http://www.gnu.org/licenses/>.
18 */
24 #ifndef genfit_FieldManager_h
25 #define genfit_FieldManager_h
26 
27 #include "AbsBField.h"
28 #include "IO.h"
29 
30 #include <stdexcept>
31 #include <string>
32 
33 #define CACHE
34 
35 namespace genfit {
36 
37 #ifdef CACHE
41 struct fieldCache {
42  double posX; double posY; double posZ;
43  double Bx; double By; double Bz;
44 };
45 #endif
46 
47 
53 class FieldManager {
54 
55  public:
56 
57  AbsBField* getField(){
58  checkInitialized();
59  return field_;
60  }
61 
63  TVector3 getFieldVal(const TVector3& position){
64  checkInitialized();
65  return field_->get(position);
66  }
67 
68 #ifdef CACHE
69  void getFieldVal(const double& posX, const double& posY, const double& posZ, double& Bx, double& By, double& Bz);
70 #else
71  inline void getFieldVal(const double& posX, const double& posY, const double& posZ, double& Bx, double& By, double& Bz) {
72  checkInitialized();
73  return field_->get(posX, posY, posZ, Bx, By, Bz);
74  }
75 #endif
76 
78  void init(AbsBField* b) {
79  field_=b;
80  }
81 
82  void destruct() {
83  if (instance_ != nullptr) {
84  delete instance_;
85  instance_ = nullptr;
86  }
87  }
88 
89  bool isInitialized() { return field_ != nullptr; }
90 
91  void checkInitialized() {
92  if(! isInitialized()){
93  errorOut << "FieldManager hasn't been initialized with a correct AbsBField pointer!" << std::endl;
94  std::string msg("FieldManager hasn't been initialized with a correct AbsBField pointer!");
95  std::runtime_error err(msg);
96  throw err;
97  }
98  }
99 
100  static void checkInstanciated() {
101  if(instance_==nullptr){
102  errorOut << "FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!" << std::endl;
103  std::string msg("FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!");
104  std::runtime_error err(msg);
105  throw err;
106  }
107  }
108 
109 #ifdef CACHE
111  void useCache(bool opt = true, unsigned int nBuckets = 8);
112 #else
113  void useCache(bool opt = true, unsigned int nBuckets = 8) {
114  std::cerr << "genfit::FieldManager::useCache() - FieldManager is compiled w/o CACHE, no caching will be done!" << std::endl;
115  }
116 #endif
117 
120  if(instance_ == nullptr) {
121  instance_ = new FieldManager();
122  }
123  return instance_;
124  }
125 
126 
127  private:
128 
129  FieldManager() {}
130 #ifdef CACHE
131  ~FieldManager() { delete cache_; }
132 #else
133  ~FieldManager() { }
134 #endif
135  static FieldManager* instance_;
136  static AbsBField* field_;
137 
138 #ifdef CACHE
139  static bool useCache_;
140  static unsigned int n_buckets_;
141  static fieldCache* cache_;
142 #endif
143 
144 };
145 
146 } /* End of namespace genfit */
149 #endif // genfit_FieldManager_h
Abstract Interface to magnetic fields in GENFIT.
Definition: AbsBField.h:36
virtual TVector3 get(const TVector3 &position) const =0
Get the magneticField [kGauss] at position.
Singleton which provides access to magnetic field maps.
Definition: FieldManager.h:53
void useCache(bool opt=true, unsigned int nBuckets=8)
Cache last lookup positions, and use stored field values if a lookup at (almost) the same position is...
TVector3 getFieldVal(const TVector3 &position)
This does NOT use the cache!
Definition: FieldManager.h:63
static FieldManager * getInstance()
Get singleton instance.
Definition: FieldManager.h:119
void init(AbsBField *b)
set the magnetic field here. Magnetic field classes must be derived from AbsBField.
Definition: FieldManager.h:78
Defines for I/O streams used for error and debug printing.
std::ostream errorOut
Default stream for error output.
Cache B field at a position.
Definition: FieldManager.h:41