Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:18:22

0001 /* Copyright 2008-2010, Technische Universitaet Muenchen,
0002    Authors: Christian Hoeppner & Sebastian Neubert & Johannes Rauch
0003 
0004    This file is part of GENFIT.
0005 
0006    GENFIT is free software: you can redistribute it and/or modify
0007    it under the terms of the GNU Lesser General Public License as published
0008    by the Free Software Foundation, either version 3 of the License, or
0009    (at your option) any later version.
0010 
0011    GENFIT is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014    GNU Lesser General Public License for more details.
0015 
0016    You should have received a copy of the GNU Lesser General Public License
0017    along with GENFIT.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 #include "FieldManager.h"
0020 #include "IO.h"
0021 
0022 #include <math.h>
0023 
0024 namespace genfit {
0025 
0026 FieldManager* FieldManager::instance_ = nullptr;
0027 AbsBField* FieldManager::field_ = nullptr;
0028 
0029 #ifdef CACHE
0030 bool FieldManager::useCache_ = false;
0031 unsigned int FieldManager::n_buckets_ = 8;
0032 fieldCache* FieldManager::cache_ = nullptr;
0033 #endif
0034 
0035 //#define DEBUG
0036 
0037 #ifdef CACHE
0038 void FieldManager::getFieldVal(const double& posX, const double& posY, const double& posZ, double& Bx, double& By, double& Bz){
0039   checkInitialized();
0040 
0041   if (useCache_) {
0042 
0043     // cache code copied from http://en.wikibooks.org/wiki/Optimizing_C%2B%2B/General_optimization_techniques/Memoization
0044     static int last_read_i = 0;
0045     static int last_written_i = 0;
0046     int i = last_read_i;
0047 
0048     static const double epsilon = 0.001;
0049 
0050     #ifdef DEBUG
0051     static int used = 0;
0052     static int notUsed = 0;
0053     #endif
0054 
0055     do {
0056       if (fabs(cache_[i].posX - posX) < epsilon &&
0057           fabs(cache_[i].posY - posY) < epsilon &&
0058           fabs(cache_[i].posZ - posZ) < epsilon) {
0059         Bx = cache_[i].Bx;
0060         By = cache_[i].By;
0061         Bz = cache_[i].Bz;
0062         #ifdef DEBUG
0063         ++used;
0064         debugOut<<"used the cache! " << double(used)/(used + notUsed) << "\n";
0065         #endif
0066         return;
0067       }
0068       i = (i + 1) % n_buckets_;
0069     } while (i != last_read_i);
0070 
0071     last_read_i = last_written_i = (last_written_i + 1) % n_buckets_;
0072 
0073     cache_[last_written_i].posX = posX;
0074     cache_[last_written_i].posY = posY;
0075     cache_[last_written_i].posZ = posZ;
0076 
0077     field_->get(posX, posY, posZ, cache_[last_written_i].Bx, cache_[last_written_i].By, cache_[last_written_i].Bz);
0078 
0079     Bx = cache_[last_written_i].Bx;
0080     By = cache_[last_written_i].By;
0081     Bz = cache_[last_written_i].Bz;
0082     #ifdef DEBUG
0083     ++notUsed;
0084     debugOut<<"did NOT use the cache! \n";
0085     #endif
0086     return;
0087 
0088   }
0089   else
0090     return field_->get(posX, posY, posZ, Bx, By, Bz);
0091 
0092 }
0093 
0094 
0095 void FieldManager::useCache(bool opt, unsigned int nBuckets) {
0096   useCache_ = opt;
0097   n_buckets_ = nBuckets;
0098 
0099   if (useCache_) {
0100     cache_ = new fieldCache[n_buckets_];
0101     for (size_t i = 0; i < n_buckets_; ++i) {
0102       // Should be safe to initialize with values in Andromeda
0103       cache_[i].posX = cache_[i].posY = cache_[i].posZ = 2.4e24 / sqrt(3);
0104       cache_[i].Bx = cache_[i].By = cache_[i].Bz = 1e30;      
0105     }      
0106   }
0107 }
0108 #endif
0109 
0110 } /* End of namespace genfit */