File indexing completed on 2025-08-05 08:18:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #ifndef genfit_FieldManager_h
0025 #define genfit_FieldManager_h
0026
0027 #include "AbsBField.h"
0028 #include "IO.h"
0029
0030 #include <stdexcept>
0031 #include <string>
0032
0033 #define CACHE
0034
0035 namespace genfit {
0036
0037 #ifdef CACHE
0038
0039
0040
0041 struct fieldCache {
0042 double posX; double posY; double posZ;
0043 double Bx; double By; double Bz;
0044 };
0045 #endif
0046
0047
0048
0049
0050
0051
0052
0053 class FieldManager {
0054
0055 public:
0056
0057 AbsBField* getField(){
0058 checkInitialized();
0059 return field_;
0060 }
0061
0062
0063 TVector3 getFieldVal(const TVector3& position){
0064 checkInitialized();
0065 return field_->get(position);
0066 }
0067
0068 #ifdef CACHE
0069 void getFieldVal(const double& posX, const double& posY, const double& posZ, double& Bx, double& By, double& Bz);
0070 #else
0071 inline void getFieldVal(const double& posX, const double& posY, const double& posZ, double& Bx, double& By, double& Bz) {
0072 checkInitialized();
0073 return field_->get(posX, posY, posZ, Bx, By, Bz);
0074 }
0075 #endif
0076
0077
0078 void init(AbsBField* b) {
0079 field_=b;
0080 }
0081
0082 void destruct() {
0083 if (instance_ != nullptr) {
0084 delete instance_;
0085 instance_ = nullptr;
0086 }
0087 }
0088
0089 bool isInitialized() { return field_ != nullptr; }
0090
0091 void checkInitialized() {
0092 if(! isInitialized()){
0093 errorOut << "FieldManager hasn't been initialized with a correct AbsBField pointer!" << std::endl;
0094 std::string msg("FieldManager hasn't been initialized with a correct AbsBField pointer!");
0095 std::runtime_error err(msg);
0096 throw err;
0097 }
0098 }
0099
0100 static void checkInstanciated() {
0101 if(instance_==nullptr){
0102 errorOut << "FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!" << std::endl;
0103 std::string msg("FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!");
0104 std::runtime_error err(msg);
0105 throw err;
0106 }
0107 }
0108
0109 #ifdef CACHE
0110
0111 void useCache(bool opt = true, unsigned int nBuckets = 8);
0112 #else
0113 void useCache(bool opt = true, unsigned int nBuckets = 8) {
0114 std::cerr << "genfit::FieldManager::useCache() - FieldManager is compiled w/o CACHE, no caching will be done!" << std::endl;
0115 }
0116 #endif
0117
0118
0119 static FieldManager* getInstance(){
0120 if(instance_ == nullptr) {
0121 instance_ = new FieldManager();
0122 }
0123 return instance_;
0124 }
0125
0126
0127 private:
0128
0129 FieldManager() {}
0130 #ifdef CACHE
0131 ~FieldManager() { delete cache_; }
0132 #else
0133 ~FieldManager() { }
0134 #endif
0135 static FieldManager* instance_;
0136 static AbsBField* field_;
0137
0138 #ifdef CACHE
0139 static bool useCache_;
0140 static unsigned int n_buckets_;
0141 static fieldCache* cache_;
0142 #endif
0143
0144 };
0145
0146 }
0147
0148
0149 #endif