FairRoot/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 */
24 #ifndef genfit_FieldManager_h
25 #define genfit_FieldManager_h
26 
27 #include "AbsBField.h"
28 
29 #include <iostream>
30 #include <stdexcept>
31 #include <string>
32 
33 #define CACHE
34 
35 namespace genfit {
36 
37 #ifdef CACHE
38 
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 
59  return field_;
60  }
61 
63  TVector3 getFieldVal(const TVector3& position){
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) {
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  bool isInitialized() { return field_ != NULL; }
83 
85  if(! isInitialized()){
86  std::cerr << "FieldManager hasn't been initialized with a correct AbsBField pointer!" << std::endl;
87  std::string msg("FieldManager hasn't been initialized with a correct AbsBField pointer!");
88  std::runtime_error err(msg);
89  throw err;
90  }
91  }
92 
93  static void checkInstanciated() {
94  if(instance_==NULL){
95  std::cerr << "FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!" << std::endl;
96  std::string msg("FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!");
97  std::runtime_error err(msg);
98  throw err;
99  }
100  }
101 
102 #ifdef CACHE
103  void useCache(bool opt = true, unsigned int nBuckets = 8);
105 #else
106  void useCache(bool opt = true, unsigned int nBuckets = 8) {
107  std::cerr << "genfit::FieldManager::useCache() - FieldManager is compiled w/o CACHE, no caching will be done!" << std::endl;
108  }
109 #endif
110 
113  if(instance_ == NULL) {
114  instance_ = new FieldManager();
115  }
116  return instance_;
117  }
118 
119 
120  private:
121 
123 #ifdef CACHE
124  ~FieldManager() { delete cache_; }
125 #else
126  ~FieldManager() { }
127 #endif
129  static AbsBField* field_;
130 
131 #ifdef CACHE
132  static bool useCache_;
133  static unsigned int n_buckets_;
135 #endif
136 
137 };
138 
139 } /* End of namespace genfit */
142 #endif // genfit_FieldManager_h
static void checkInstanciated()
Definition: FieldManager.h:93
Abstract Interface to magnetic fields in GENFIT.
Definition: AbsBField.h:36
static FieldManager * getInstance()
Get singleton instance.
Definition: FieldManager.h:112
TTree * b
AbsBField * getField()
Definition: FieldManager.h:57
static bool useCache_
Definition: FieldManager.h:132
static AbsBField * field_
Definition: FieldManager.h:129
void init(AbsBField *b)
set the magnetic field here. Magnetic field classes must be derived from AbsBField.
Definition: FieldManager.h:78
Cache B field at a position. Used by FieldManager.
Definition: FieldManager.h:41
static fieldCache * cache_
Definition: FieldManager.h:134
TVector3 getFieldVal(const TVector3 &position)
This does NOT use the cache!
Definition: FieldManager.h:63
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...
Singleton which provides access to magnetic field maps.
Definition: FieldManager.h:53
static FieldManager * instance_
Definition: FieldManager.h:128
virtual TVector3 get(const TVector3 &position) const =0
Get the magneticField [kGauss] at position.
static unsigned int n_buckets_
Definition: FieldManager.h:133