PandaRoot
FieldManager.h
Go to the documentation of this file.
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 */
23 #ifndef genfit_FieldManager_h
24 #define genfit_FieldManager_h
25 
26 #include "AbsBField.h"
27 
28 #include <iostream>
29 #include <stdexcept>
30 #include <string>
31 
32 #define CACHE
33 
34 namespace genfit {
35 
36 #ifdef CACHE
37 
40 struct fieldCache {
41  double posX;
42  double posY;
43  double posZ;
44  double Bx;
45  double By;
46  double Bz;
47 };
48 #endif
49 
55 class FieldManager {
56 
57  public:
59  {
60  checkInitialized();
61  return field_;
62  }
63 
65  TVector3 getFieldVal(const TVector3 &position)
66  {
67  checkInitialized();
68  return field_->get(position);
69  }
70 
71 #ifdef CACHE
72  void getFieldVal(const double &posX, const double &posY, const double &posZ, double &Bx, double &By, double &Bz);
73 #else
74  inline void getFieldVal(const double &posX, const double &posY, const double &posZ, double &Bx, double &By, double &Bz)
75  {
76  checkInitialized();
77  return field_->get(posX, posY, posZ, Bx, By, Bz);
78  }
79 #endif
80 
82  void init(AbsBField *b) { field_ = b; }
83 
84  bool isInitialized() { return field_ != nullptr; }
85 
87  {
88  if (!isInitialized()) {
89  std::cerr << "FieldManager hasn't been initialized with a correct AbsBField pointer!" << std::endl;
90  std::string msg("FieldManager hasn't been initialized with a correct AbsBField pointer!");
91  std::runtime_error err(msg);
92  throw err;
93  }
94  }
95 
96  static void checkInstanciated()
97  {
98  if (instance_ == nullptr) {
99  std::cerr << "FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!" << std::endl;
100  std::string msg("FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!");
101  std::runtime_error err(msg);
102  throw err;
103  }
104  }
105 
106 #ifdef CACHE
107  void useCache(bool opt = true, unsigned int nBuckets = 8);
109 #else
110  void useCache(bool opt = true, unsigned int nBuckets = 8)
111  {
112  std::cerr << "genfit::FieldManager::useCache() - FieldManager is compiled w/o CACHE, no caching will be done!" << std::endl;
113  }
114 #endif
115 
118  {
119  if (instance_ == nullptr) {
120  instance_ = new FieldManager();
121  }
122  return instance_;
123  }
124 
125  private:
126  FieldManager() {}
127 #ifdef CACHE
128  ~FieldManager() { delete cache_; }
129 #else
130  ~FieldManager() {}
131 #endif
132  static FieldManager *instance_;
133  static AbsBField *field_;
134 
135 #ifdef CACHE
136  static bool useCache_;
137  static unsigned int n_buckets_;
138  static fieldCache *cache_;
139 #endif
140 };
141 
142 } /* End of namespace genfit */
145 #endif // genfit_FieldManager_h
static void checkInstanciated()
Definition: FieldManager.h:96
Abstract Interface to magnetic fields in GENFIT.
Definition: AbsBField.h:35
static FieldManager * getInstance()
Get singleton instance.
Definition: FieldManager.h:117
AbsBField * getField()
Definition: FieldManager.h:58
void init(AbsBField *b)
set the magnetic field here. Magnetic field classes must be derived from AbsBField.
Definition: FieldManager.h:82
Cache B field at a position. Used by FieldManager.
Definition: FieldManager.h:40
TVector3 getFieldVal(const TVector3 &position)
This does NOT use the cache!
Definition: FieldManager.h:65
Singleton which provides access to magnetic field maps.
Definition: FieldManager.h:55
Matrix inversion tools.
Definition: AbsBField.h:28