Back to home page

sPhenix code displayed by LXR

 
 

    


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

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 /** @addtogroup genfit
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  * @brief Cache B field at a position. Used by FieldManager.
0040  */
0041 struct fieldCache {
0042   double posX; double posY; double posZ;
0043   double Bx; double By; double Bz;
0044 };
0045 #endif
0046 
0047 
0048 /** @brief Singleton which provides access to magnetic field maps.
0049  *
0050  *  @author Christian H&ouml;ppner (Technische Universit&auml;t M&uuml;nchen, original author)
0051  *  @author Sebastian Neubert  (Technische Universit&auml;t M&uuml;nchen, original author)
0052  */
0053 class FieldManager {
0054 
0055  public:
0056 
0057   AbsBField* getField(){
0058     checkInitialized();
0059     return field_;
0060   }
0061 
0062   //! This does NOT use the cache!
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   //! set the magnetic field here. Magnetic field classes must be derived from AbsBField.
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   //! Cache last lookup positions, and use stored field values if a lookup at (almost) the same position is done.
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   //! Get singleton instance.
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 } /* End of namespace genfit */
0147 /** @} */
0148 
0149 #endif // genfit_FieldManager_h