CbmRoot
Loading...
Searching...
No Matches
CbmFieldMap.h
Go to the documentation of this file.
1/* Copyright (C) 2004-2020 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Mohammad Al-Turany, Denis Bertini [committer], Volker Friese */
4
5// -------------------------------------------------------------------------
6// ----- CbmFieldMap header file -----
7// ----- Created 12/01/04 by M. Al/Turany (CbmField.h) -----
8// ----- Redesign 13/02/06 by V. Friese -----
9// -------------------------------------------------------------------------
10
11
21
22
23#ifndef CBMFIELDMAP_H
24#define CBMFIELDMAP_H 1
25
26
27#include <FairField.h> // for FairField
28
29#include <Rtypes.h> // for THashConsistencyHolder, ClassDef
30#include <RtypesCore.h> // for Double_t, Int_t, Bool_t, Option_t
31#include <TArrayF.h> // for TArrayF
32#include <TString.h> // for TString
33
35class CbmFieldMapData;
36class CbmFieldPar;
37class TArrayF;
38
39class CbmFieldMap : public FairField {
40
41
42 public:
45
46
51 CbmFieldMap(const char* mapName, const char* fileType = "R");
52
53
55 CbmFieldMap(CbmFieldPar* fieldPar);
56
57
60
61
63 virtual ~CbmFieldMap();
64
65
67 virtual void Init() override;
68
69
81 virtual void Init(Int_t nX, Double_t xMin, Double_t xMax, Int_t nY, Double_t yMin, Double_t yMax, Int_t nZ,
82 Double_t zMin, Double_t zMax, TArrayF* bx, TArrayF* by, TArrayF* bz);
83
84
89 virtual Double_t GetBx(Double_t x, Double_t y, Double_t z) override;
90 virtual Double_t GetBy(Double_t x, Double_t y, Double_t z) override;
91 virtual Double_t GetBz(Double_t x, Double_t y, Double_t z) override;
92
97 virtual void GetFieldValue(const Double_t point[3], Double_t* bField) override;
98
100 void WriteAsciiFile(const char* fileName);
101
102
104 void WriteRootFile(const char* fileName, const char* mapName);
105
106
108 virtual void SetPosition(Double_t x, Double_t y, Double_t z);
109
110
112 virtual void SetScale(Double_t factor) { fScale = factor; }
113
114
116 Double_t GetXmin() const { return fXmin; }
117 Double_t GetYmin() const { return fYmin; }
118 Double_t GetZmin() const { return fZmin; }
119 Double_t GetXmax() const { return fXmax; }
120 Double_t GetYmax() const { return fYmax; }
121 Double_t GetZmax() const { return fZmax; }
122 Double_t GetXstep() const { return fXstep; }
123 Double_t GetYstep() const { return fYstep; }
124 Double_t GetZstep() const { return fZstep; }
125 Int_t GetNx() const { return fNx; }
126 Int_t GetNy() const { return fNy; }
127 Int_t GetNz() const { return fNz; }
128
129
131 Double_t GetPositionX() const { return fPosX; }
132 Double_t GetPositionY() const { return fPosY; }
133 Double_t GetPositionZ() const { return fPosZ; }
134
135
137 Double_t GetScale() const { return fScale; }
138
139
141 TArrayF* GetBx() const { return fBx; }
142 TArrayF* GetBy() const { return fBy; }
143 TArrayF* GetBz() const { return fBz; }
144
145
147 const char* GetFileName() { return fFileName.Data(); }
148
149
151 virtual void Print(Option_t* = "") const override;
152
153
154 protected:
159 Double_t x{0.}; // Local x coordinate [cm]
160 Double_t y{0.}; // Local y coordinate [cm]
161 Double_t z{0.}; // Local z coordinate [cm]
162 Double_t hemiX{1.}; // Hemisphere in x direction (1 or -1)
163 Double_t hemiY{1.}; // Hemisphere in y direction (1 or -1)
164 Double_t hemiZ{1.}; // Hemisphere in z direction (1 or -1)
165 };
166
168 [[gnu::always_inline]] LocalCoordinates Global2Local(Double_t x, Double_t y, Double_t z) const
169 {
170 return {x - fPosX, y - fPosY, z - fPosZ, (x < fPosX ? -1. : 1.), (y < fPosY ? -1. : 1.), (z < fPosZ ? -1. : 1.)};
171 }
172
175 Bool_t isValid{kFALSE}; // kTRUE if point is inside the grid
176 Int_t ix{0}; // Grid cell index in x
177 Int_t iy{0}; // Grid cell index in y
178 Int_t iz{0}; // Grid cell index in z
179 Double_t dx{0.}; // Relative distance from grid point in x [units of cell size]
180 Double_t dy{0.}; // Relative distance from grid point in y [units of cell size]
181 Double_t dz{0.}; // Relative distance from grid point in z [units of cell size]
182 };
183
185 GridCoordinates Local2Grid(const LocalCoordinates& local) const;
186
187
192 [[gnu::always_inline]] Double_t Interpolate(const TArrayF* B, const GridCoordinates& point) const;
193
194
196 void Reset();
197
198
200 void ReadAsciiFile(const char* fileName);
201
203 void ReadAsciiFile2018(const char* fileName);
204
206 void ReadRootFile(const char* fileName, const char* mapName);
207
208
210 void SetField(const CbmFieldMapData* data);
211
213 TString fFileName;
214
215
217 Double_t fScale;
218
219
221 Double_t fPosX, fPosY, fPosZ;
222
223
225 Double_t fXmin, fXmax, fXstep;
226 Double_t fYmin, fYmax, fYstep;
227 Double_t fZmin, fZmax, fZstep;
228
229
232
233
235 TArrayF* fBx;
236 TArrayF* fBy;
237 TArrayF* fBz;
238
239
241
242 Double_t fBxOrigin;
243 Double_t fByOrigin;
244 Double_t fBzOrigin;
245
246 private:
249
253 [[gnu::always_inline]] GridCoordinates Global2Grid(Double_t x, Double_t y, Double_t z) const
254 {
255 return Local2Grid(Global2Local(x, y, z));
256 }
257
258 ClassDefOverride(CbmFieldMap, 2)
259};
260
261
262// ----------- Check whether a point is inside the map ----------------
263[[gnu::always_inline]] inline CbmFieldMap::GridCoordinates
265{
266 // --- Determine grid cell
267 Double_t x = (loc.x - fXmin) / fXstep;
268 Double_t y = (loc.y - fYmin) / fYstep;
269 Double_t z = (loc.z - fZmin) / fZstep;
270
271 Int_t ix = (Int_t) std::floor(x);
272 Int_t iy = (Int_t) std::floor(y);
273 Int_t iz = (Int_t) std::floor(z);
274
275 bool isValid = (ix >= 0 && ix < fNx - 1 && iy >= 0 && iy < fNy - 1 && iz >= 0 && iz < fNz - 1);
276
277 // Relative distance from grid point (in units of cell size)
278 Double_t dx = x - (Double_t) ix;
279 Double_t dy = y - (Double_t) iy;
280 Double_t dz = z - (Double_t) iz;
281
282 return {isValid, ix, iy, iz, dx, dy, dz};
283}
284// ------------------------------------------------------------------------
285
286
287// ------------ Interpolation in a grid cell (private) -----------------
288[[gnu::always_inline]] inline Double_t
289CbmFieldMap::Interpolate(const TArrayF* B, // Array with field values - fBx, fBy, or fBz
290 const CbmFieldMap::GridCoordinates& p) const
291{
292 if (!p.isValid) {
293 return 0.;
294 }
295
296 int Nyz = fNy * fNz; // Number of grid points in y-z plane
297 int ind000 = p.ix * Nyz + p.iy * fNz + p.iz;
298 int ind111 = ind000 + Nyz + fNz + 1;
299
300 if (ind000 < 0 || ind111 >= B->GetSize()) {
301 LOG(error) << "CbmFieldMap::Interpolate: Index out of bounds!";
302 return 0.;
303 }
304
305 const auto* arr = B->GetArray();
306
307 // Get field values at grid cell corners
308
309 Double_t H000 = arr[ind000]; // Bx, By, or Bz at (ix, iy, iz)
310 Double_t H001 = arr[ind000 + 1]; // Bx, By, or Bz at (ix, iy, iz+1)
311 Double_t H010 = arr[ind000 + fNz]; // Bx, By, or Bz at (ix, iy+1, iz)
312 Double_t H011 = arr[ind000 + fNz + 1]; // Bx, By, or Bz at (ix, iy+1, iz+1)
313 Double_t H100 = arr[ind000 + Nyz]; // Bx, By, or Bz at (ix+1, iy, iz)
314 Double_t H101 = arr[ind000 + Nyz + 1]; // Bx, By, or Bz at (ix+1, iy, iz+1)
315 Double_t H110 = arr[ind000 + Nyz + fNz]; // Bx, By, or Bz at (ix+1, iy+1, iz)
316 Double_t H111 = arr[ind111]; // Bx, By, or Bz at (ix+1, iy+1, iz+1)
317
318 // Interpolate in x coordinate. Interpolated field in yz (2-dim)
319
320 Double_t Hyz00 = H000 + (H100 - H000) * p.dx;
321 Double_t Hyz10 = H010 + (H110 - H010) * p.dx;
322 Double_t Hyz01 = H001 + (H101 - H001) * p.dx;
323 Double_t Hyz11 = H011 + (H111 - H011) * p.dx;
324
325 // Interpolate in y coordinate. Interpolated field in z (1-dim)
326 Double_t Hz0 = Hyz00 + (Hyz10 - Hyz00) * p.dy;
327 Double_t Hz1 = Hyz01 + (Hyz11 - Hyz01) * p.dy;
328
329 // Interpolate in z coordinate
330 return Hz0 + (Hz1 - Hz0) * p.dz;
331}
332// ------------------------------------------------------------------------
333
334
335#endif
int Int_t
bool Bool_t
Some class B.
Double_t fPosZ
Double_t fBxOrigin
TArrayF * fBy
Int_t GetNy() const
Double_t GetZmax() const
Double_t fPosY
TArrayF * GetBz() const
virtual void SetPosition(Double_t x, Double_t y, Double_t z)
virtual void GetFieldValue(const Double_t point[3], Double_t *bField) override
Double_t fYmax
Double_t GetXstep() const
GridCoordinates Local2Grid(const LocalCoordinates &local) const
Transform local coordinates into grid cell coordinates.
virtual void SetScale(Double_t factor)
Double_t fPosX
Double_t fZmin
Double_t fZmax
Double_t fYmin
void WriteRootFile(const char *fileName, const char *mapName)
Double_t GetYmin() const
virtual ~CbmFieldMap()
void ReadAsciiFile(const char *fileName)
TArrayF * GetBx() const
TArrayF * fBx
TArrayF * fBz
Double_t GetPositionY() const
void SetField(const CbmFieldMapData *data)
Double_t fXmin
Double_t GetYmax() const
void ReadAsciiFile2018(const char *fileName)
virtual void Print(Option_t *="") const override
TString fFileName
Double_t GetYstep() const
LocalCoordinates Global2Local(Double_t x, Double_t y, Double_t z) const
Transform global coordinates into local coordinates.
Double_t GetPositionZ() const
Double_t fYstep
CbmFieldMap(const CbmFieldMap &)
z-component of the field at the origin
Double_t fZstep
Double_t GetXmin() const
Int_t GetNz() const
Double_t GetPositionX() const
Int_t GetNx() const
GridCoordinates Global2Grid(Double_t x, Double_t y, Double_t z) const
Get grid cell coordinates for a point in global coordinates.
void ReadRootFile(const char *fileName, const char *mapName)
const char * GetFileName()
void WriteAsciiFile(const char *fileName)
Double_t fXstep
CbmFieldMap & operator=(const CbmFieldMap &)
Double_t fBzOrigin
y-component of the field at the origin
Double_t GetXmax() const
Double_t GetZmin() const
Double_t GetZstep() const
Double_t fScale
Double_t Interpolate(const TArrayF *B, const GridCoordinates &point) const
Get field values by interpolation of the grid.
virtual void Init() override
Double_t fByOrigin
x-component of the field at the origin
Double_t GetScale() const
TArrayF * GetBy() const
Double_t fXmax
Structure to hold grid cell coordinates and relative distances.
Structure to hold local coordinates in the field map. Local coordinates are defined as the distance f...