PandaRoot
FieldManager.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 /* Copyright 2008-2010, Technische Universitaet Muenchen,
14  Authors: Christian Hoeppner & Sebastian Neubert & Johannes Rauch
15 
16  This file is part of GENFIT.
17 
18  GENFIT is free software: you can redistribute it and/or modify
19  it under the terms of the GNU Lesser General Public License as published
20  by the Free Software Foundation, either version 3 of the License, or
21  (at your option) any later version.
22 
23  GENFIT is distributed in the hope that it will be useful,
24  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  GNU Lesser General Public License for more details.
27 
28  You should have received a copy of the GNU Lesser General Public License
29  along with GENFIT. If not, see <http://www.gnu.org/licenses/>.
30 */
35 #ifndef genfit_FieldManager_h
36 #define genfit_FieldManager_h
37 
38 #include "AbsBField.h"
39 
40 #include <iostream>
41 #include <stdexcept>
42 #include <string>
43 
44 #define CACHE
45 
46 namespace genfit {
47 
48 #ifdef CACHE
49 
52 struct fieldCache {
53  double posX;
54  double posY;
55  double posZ;
56  double Bx;
57  double By;
58  double Bz;
59 };
60 #endif
61 
67 class FieldManager {
68 
69  public:
71  {
72  checkInitialized();
73  return field_;
74  }
75 
77  TVector3 getFieldVal(const TVector3 &position)
78  {
79  checkInitialized();
80  return field_->get(position);
81  }
82 
83 #ifdef CACHE
84  void getFieldVal(const double &posX, const double &posY, const double &posZ, double &Bx, double &By, double &Bz);
85 #else
86  inline void getFieldVal(const double &posX, const double &posY, const double &posZ, double &Bx, double &By, double &Bz)
87  {
88  checkInitialized();
89  return field_->get(posX, posY, posZ, Bx, By, Bz);
90  }
91 #endif
92 
94  void init(AbsBField *b) { field_ = b; }
95 
96  bool isInitialized() { return field_ != nullptr; }
97 
99  {
100  if (!isInitialized()) {
101  std::cerr << "FieldManager hasn't been initialized with a correct AbsBField pointer!" << std::endl;
102  std::string msg("FieldManager hasn't been initialized with a correct AbsBField pointer!");
103  std::runtime_error err(msg);
104  throw err;
105  }
106  }
107 
108  static void checkInstanciated()
109  {
110  if (instance_ == nullptr) {
111  std::cerr << "FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!" << std::endl;
112  std::string msg("FieldManager hasn't been instantiated yet, call getInstance() and init() before getFieldVal()!");
113  std::runtime_error err(msg);
114  throw err;
115  }
116  }
117 
118 #ifdef CACHE
119  void useCache(bool opt = true, unsigned int nBuckets = 8);
121 #else
122  void useCache(bool opt = true, unsigned int nBuckets = 8)
123  {
124  std::cerr << "genfit::FieldManager::useCache() - FieldManager is compiled w/o CACHE, no caching will be done!" << std::endl;
125  }
126 #endif
127 
130  {
131  if (instance_ == nullptr) {
132  instance_ = new FieldManager();
133  }
134  return instance_;
135  }
136 
137  private:
138  FieldManager() {}
139 #ifdef CACHE
140  ~FieldManager() { delete cache_; }
141 #else
142  ~FieldManager() {}
143 #endif
144  static FieldManager *instance_;
145  static AbsBField *field_;
146 
147 #ifdef CACHE
148  static bool useCache_;
149  static unsigned int n_buckets_;
150  static fieldCache *cache_;
151 #endif
152 };
153 
154 } /* End of namespace genfit */
157 #endif // genfit_FieldManager_h
static void checkInstanciated()
Definition: FieldManager.h:108
Abstract Interface to magnetic fields in GENFIT.
Definition: AbsBField.h:47
static FieldManager * getInstance()
Get singleton instance.
Definition: FieldManager.h:129
AbsBField * getField()
Definition: FieldManager.h:70
void init(AbsBField *b)
set the magnetic field here. Magnetic field classes must be derived from AbsBField.
Definition: FieldManager.h:94
Cache B field at a position. Used by FieldManager.
Definition: FieldManager.h:52
TVector3 getFieldVal(const TVector3 &position)
This does NOT use the cache!
Definition: FieldManager.h:77
Singleton which provides access to magnetic field maps.
Definition: FieldManager.h:67
Matrix inversion tools.
Definition: AbsBField.h:40