19 #include "FieldManager.h"
26 FieldManager* FieldManager::instance_ =
nullptr;
27 AbsBField* FieldManager::field_ =
nullptr;
30 bool FieldManager::useCache_ =
false;
31 unsigned int FieldManager::n_buckets_ = 8;
32 fieldCache* FieldManager::cache_ =
nullptr;
38 void FieldManager::getFieldVal(
const double& posX,
const double& posY,
const double& posZ,
double& Bx,
double& By,
double& Bz){
44 static int last_read_i = 0;
45 static int last_written_i = 0;
48 static const double epsilon = 0.001;
52 static int notUsed = 0;
56 if (fabs(cache_[i].posX - posX) < epsilon &&
57 fabs(cache_[i].posY - posY) < epsilon &&
58 fabs(cache_[i].posZ - posZ) < epsilon) {
64 debugOut<<
"used the cache! " << double(used)/(used + notUsed) <<
"\n";
68 i = (i + 1) % n_buckets_;
69 }
while (i != last_read_i);
71 last_read_i = last_written_i = (last_written_i + 1) % n_buckets_;
73 cache_[last_written_i].posX = posX;
74 cache_[last_written_i].posY = posY;
75 cache_[last_written_i].posZ = posZ;
77 field_->
get(posX, posY, posZ, cache_[last_written_i].Bx, cache_[last_written_i].By, cache_[last_written_i].Bz);
79 Bx = cache_[last_written_i].Bx;
80 By = cache_[last_written_i].By;
81 Bz = cache_[last_written_i].Bz;
84 debugOut<<
"did NOT use the cache! \n";
90 return field_->
get(posX, posY, posZ, Bx, By, Bz);
97 n_buckets_ = nBuckets;
100 cache_ =
new fieldCache[n_buckets_];
101 for (
size_t i = 0; i < n_buckets_; ++i) {
103 cache_[i].posX = cache_[i].posY = cache_[i].posZ = 2.4e24 /
sqrt(3);
104 cache_[i].Bx = cache_[i].By = cache_[i].Bz = 1e30;
virtual TVector3 get(const TVector3 &position) const =0
Get the magneticField [kGauss] at position.
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!
double sqrt(double a)
sqrt for double
Defines for I/O streams used for error and debug printing.
std::ostream debugOut
Default stream for debug output.