er  dev
ERFieldMap.cxx
1 // -------------------------------------------------------------------------
2 // ----- ERFieldMap source file -----
3 // ----- Created 12/05/17 by V.Schetinin -----
4 // -------------------------------------------------------------------------
5 
6 #include "ERFieldMap.h"
7 
8 #include "ERFieldPar.h"
9 // Includes from ROOT
10 #include "TArrayF.h"
11 #include "TFile.h"
12 #include "TMath.h"
13 #include "TGeoManager.h"
14 // Includes from C
15 #include <iomanip>
16 #include <iostream>
17 #include <fstream>
18 using std::cout;
19 using std::cerr;
20 using std::endl;
21 using std::right;
22 using std::flush;
23 using std::setw;
24 using std::showpoint;
25 // ------------- Default constructor ----------------------------------
27  : FairField(),
28  fFileName(""),
29  fScale(1.),
30  fPosX(0.),
31  fPosY(0.),
32  fPosZ(0.),
33  fXmin(0.),
34  fXmax(0.),
35  fXstep(0.),
36  fYmin(0.),
37  fYmax(0.),
38  fYstep(0.),
39  fZmin(0.),
40  fZmax(0.),
41  fZstep(0.),
42  fNx(0),
43  fNy(0),
44  fNz(0),
45  fBx(NULL),
46  fBy(NULL),
47  fBz(NULL),
48  fBxOrigin(0.),
49  fByOrigin(0.),
50  fBzOrigin(0.),
51  fLocalMagFieldNode(NULL),
52  fLocalMagFieldVolName("")
53 {
54  // Initilization of arrays is to my knowledge not
55  // possible in member initalization lists
56  for (Int_t i=0; i < 2 ; i++) {
57  fHc[i]=0;
58  for (Int_t j=0; j < 2 ; j++) {
59  fHb[i][j]=0;
60  for (Int_t k=0; k < 2 ; k++) {
61  fHa[i][j][k]=0;
62  }
63  }
64  }
65  // Assign values to data members of base classes
66  // There is no appropriate constructor of the base
67  // class.
68  fName = "";
69  fType = 1;
70 }
71 // ------------------------------------------------------------------------
72 // ------------- Standard constructor ---------------------------------
73 ERFieldMap::ERFieldMap(const char* mapName, const char* fileType)
74  : FairField(),
75  fFileName(""),
76  fScale(1.),
77  fPosX(0.),
78  fPosY(0.),
79  fPosZ(0.),
80  fXmin(0.),
81  fXmax(0.),
82  fXstep(0.),
83  fYmin(0.),
84  fYmax(0.),
85  fYstep(0.),
86  fZmin(0.),
87  fZmax(0.),
88  fZstep(0.),
89  fNx(0),
90  fNy(0),
91  fNz(0),
92  fBx(NULL),
93  fBy(NULL),
94  fBz(NULL),
95  fBxOrigin(0.),
96  fByOrigin(0.),
97  fBzOrigin(0.),
98  fLocalMagFieldNode(NULL),
99  fLocalMagFieldVolName("")
100 {
101  // Initilization of arrays is to my knowledge not
102  // possible in member initalization lists
103  for (Int_t i=0; i < 2 ; i++) {
104  fHc[i]=0;
105  for (Int_t j=0; j < 2 ; j++) {
106  fHb[i][j]=0;
107  for (Int_t k=0; k < 2 ; k++) {
108  fHa[i][j][k]=0;
109  }
110  }
111  }
112  // Assign values to data members of base classes
113  // There is no appropriate constructor of the base
114  // class.
115  fName = mapName;
116  fTitle = "ERFieldMap";
117  TString dir = getenv("VMCWORKDIR");
118  fFileName = dir + "/input/" + mapName;
119  if ( fileType[0] == 'R' ) {
120  fFileName += ".root";
121  } else {
122  fFileName += ".dat";
123  }
124  fType = 1;
125 }
126 // ------------------------------------------------------------------------
127 // ------------ Constructor from ERFieldPar --------------------------
129  : FairField(),
130  fFileName(""),
131  fScale(1.),
132  fPosX(0.),
133  fPosY(0.),
134  fPosZ(0.),
135  fXmin(0.),
136  fXmax(0.),
137  fXstep(0.),
138  fYmin(0.),
139  fYmax(0.),
140  fYstep(0.),
141  fZmin(0.),
142  fZmax(0.),
143  fZstep(0.),
144  fNx(0),
145  fNy(0),
146  fNz(0),
147  fBx(NULL),
148  fBy(NULL),
149  fBz(NULL),
150  fBxOrigin(0.),
151  fByOrigin(0.),
152  fBzOrigin(0.),
153  fLocalMagFieldNode(NULL),
154  fLocalMagFieldVolName("")
155 {
156  // Initilization of arrays is to my knowledge not
157  // possible in member initalization lists
158  for (Int_t i=0; i < 2 ; i++) {
159  fHc[i]=0;
160  for (Int_t j=0; j < 2 ; j++) {
161  fHb[i][j]=0;
162  for (Int_t k=0; k < 2 ; k++) {
163  fHa[i][j][k]=0;
164  }
165  }
166  }
167  // Assign values to data members of base classes
168  // There is no appropriate constructor of the base
169  // class.
170  fName = "";
171  fType = 1;
172  if ( ! fieldPar ) {
173  cerr << "-W- ERFieldConst::ERFieldMap: empty parameter container!"
174  << endl;
175  }
176  else {
177  fieldPar->MapName(fName);
178  fPosX = fieldPar->GetPositionX();
179  fPosY = fieldPar->GetPositionY();
180  fPosZ = fieldPar->GetPositionZ();
181  fScale = fieldPar->GetScale();
182  TString dir = getenv("VMCWORKDIR");
183  fFileName = dir + "/input/" + fName + ".root";
184  fType = fieldPar->GetType();
185  }
186 }
187 // ------------------------------------------------------------------------
188 // ------------ Destructor --------------------------------------------
190  if ( fBx ) delete fBx;
191  if ( fBy ) delete fBy;
192  if ( fBz ) delete fBz;
193 }
194 // ------------------------------------------------------------------------
195 // ----------- Intialisation ------------------------------------------
197  if (fLocalMagFieldVolName.Length() != 0) {
198  if ( ! gGeoManager ) {
199  std::cerr << "ERFieldMap: cannot initialise without TGeoManager!"<< std::endl;
200  }
201  gGeoManager->CdTop();
202  TGeoNode* cave = gGeoManager->GetCurrentNode();
203  TGeoNode* localMagField = NULL;
204  for (Int_t iNode = 0; iNode < cave->GetNdaughters(); iNode++) {
205  TString name = cave->GetDaughter(iNode)->GetName();
206  if ( name.Contains(fLocalMagFieldVolName, TString::kIgnoreCase) ) {
207  localMagField = cave->GetDaughter(iNode);
208  break;
209  }
210  }
211  fLocalMagFieldNode = localMagField;
212  }
213  if (fFileName.EndsWith(".root")) ReadRootFile(fFileName, fName);
214  else if (fFileName.EndsWith(".dat")) ReadAsciiFile(fFileName);
215  else {
216  cerr << "-E- ERFieldMap::Init: No proper file name defined! ("
217  << fFileName << ")" << endl;
218  Fatal("Init", "No proper file name");
219  }
220  // Fill values needed in the Print() function. This is needed to allow
221  // a constant Print() function.
222  Double_t originX = 0;
223  Double_t originY = 0;
224  Double_t originZ = 0;
225  if (fLocalMagFieldNode != NULL) {
226  originX = fLocalMagFieldNode->GetMatrix()->GetTranslation()[0];
227  originY = fLocalMagFieldNode->GetMatrix()->GetTranslation()[1];
228  originZ = fLocalMagFieldNode->GetMatrix()->GetTranslation()[2];
229  fPosX = originX;
230  fPosY = originY;
231  fPosZ = originZ;
232  }
233  fBxOrigin = GetBx(originX, originY, originZ);
234  fByOrigin = GetBy(originX, originY, originZ);
235  fBzOrigin = GetBz(originX, originY, originZ);
236  Print();
237 }
238 // ------------------------------------------------------------------------
239 // ----------- Get x component of the field ---------------------------
240 Double_t ERFieldMap::GetBx(Double_t x, Double_t y, Double_t z) {
241  Int_t ix = 0;
242  Int_t iy = 0;
243  Int_t iz = 0;
244  Double_t dx = 0.;
245  Double_t dy = 0.;
246  Double_t dz = 0.;
247  Double_t masterCoord[3];
248  Double_t masterVecB[3];
249  Double_t localCoord[3];
250 
251  localCoord[0] = masterCoord[0] = x;
252  localCoord[1] = masterCoord[1] = y;
253  localCoord[2] = masterCoord[2] = z;
254  if (fLocalMagFieldNode != NULL) {
255  fLocalMagFieldNode->MasterToLocal(masterCoord, localCoord);
256  }
257  if ( IsInside(localCoord[0], localCoord[1], localCoord[2], ix, iy, iz, dx, dy, dz) ) {
258  // Get Bx field values at grid cell corners
259  fHa[0][0][0] = fBx->At(ix *fNy*fNz + iy *fNz + iz);
260  fHa[1][0][0] = fBx->At((ix+1)*fNy*fNz + iy *fNz + iz);
261  fHa[0][1][0] = fBx->At(ix *fNy*fNz + (iy+1)*fNz + iz);
262  fHa[1][1][0] = fBx->At((ix+1)*fNy*fNz + (iy+1)*fNz + iz);
263  fHa[0][0][1] = fBx->At(ix *fNy*fNz + iy *fNz + (iz+1));
264  fHa[1][0][1] = fBx->At((ix+1)*fNy*fNz + iy *fNz + (iz+1));
265  fHa[0][1][1] = fBx->At(ix *fNy*fNz + (iy+1)*fNz + (iz+1));
266  fHa[1][1][1] = fBx->At((ix+1)*fNy*fNz + (iy+1)*fNz + (iz+1));
267  // Return interpolated field value
268  Double_t interpolatedCoord[3];
269  interpolatedCoord[0] = masterVecB[0] = Interpolate(dx, dy, dz);
270  interpolatedCoord[1] = 0;
271  interpolatedCoord[2] = 0;
272  if (fLocalMagFieldNode != NULL) {
273  fLocalMagFieldNode->LocalToMasterVect(interpolatedCoord, masterVecB);
274  }
275  return masterVecB[0];
276  }
277  return 0.;
278 }
279 // ------------------------------------------------------------------------
280 // ----------- Get y component of the field ---------------------------
281 Double_t ERFieldMap::GetBy(Double_t x, Double_t y, Double_t z) {
282  Int_t ix = 0;
283  Int_t iy = 0;
284  Int_t iz = 0;
285  Double_t dx = 0.;
286  Double_t dy = 0.;
287  Double_t dz = 0.;
288  Double_t masterCoord[3];
289  Double_t masterVecB[3];
290  Double_t localCoord[3];
291 
292  localCoord[0] = masterCoord[0] = x;
293  localCoord[1] = masterCoord[1] = y;
294  localCoord[2] = masterCoord[2] = z;
295  if (fLocalMagFieldNode != NULL) {
296  fLocalMagFieldNode->MasterToLocal(masterCoord, localCoord);
297  }
298  if ( IsInside(localCoord[0], localCoord[1], localCoord[2], ix, iy, iz, dx, dy, dz) ) {
299  // Get Bx field values at grid cell corners
300  fHa[0][0][0] = fBy->At(ix *fNy*fNz + iy *fNz + iz);
301  fHa[1][0][0] = fBy->At((ix+1)*fNy*fNz + iy *fNz + iz);
302  fHa[0][1][0] = fBy->At(ix *fNy*fNz + (iy+1)*fNz + iz);
303  fHa[1][1][0] = fBy->At((ix+1)*fNy*fNz + (iy+1)*fNz + iz);
304  fHa[0][0][1] = fBy->At(ix *fNy*fNz + iy *fNz + (iz+1));
305  fHa[1][0][1] = fBy->At((ix+1)*fNy*fNz + iy *fNz + (iz+1));
306  fHa[0][1][1] = fBy->At(ix *fNy*fNz + (iy+1)*fNz + (iz+1));
307  fHa[1][1][1] = fBy->At((ix+1)*fNy*fNz + (iy+1)*fNz + (iz+1));
308  // Return interpolated field value
309  Double_t interpolatedCoord[3];// = {0, 0, 0};
310  interpolatedCoord[0] = 0;
311  interpolatedCoord[1] = masterVecB[1] = Interpolate(dx, dy, dz);
312  interpolatedCoord[2] = 0;
313  if (fLocalMagFieldNode != NULL) {
314  fLocalMagFieldNode->LocalToMasterVect(interpolatedCoord, masterVecB);
315  }
316  return masterVecB[1];
317  }
318  return 0.;
319 }
320 // ------------------------------------------------------------------------
321 // ----------- Get z component of the field ---------------------------
322 Double_t ERFieldMap::GetBz(Double_t x, Double_t y, Double_t z) {
323  Int_t ix = 0;
324  Int_t iy = 0;
325  Int_t iz = 0;
326  Double_t dx = 0.;
327  Double_t dy = 0.;
328  Double_t dz = 0.;
329  Double_t masterCoord[3];
330  Double_t masterVecB[3];
331  Double_t localCoord[3];
332 
333  localCoord[0] = masterCoord[0] = x;
334  localCoord[1] = masterCoord[1] = y;
335  localCoord[2] = masterCoord[2] = z;
336  if (fLocalMagFieldNode != NULL) {
337  fLocalMagFieldNode->MasterToLocal(masterCoord, localCoord);
338  }
339  if ( IsInside(localCoord[0], localCoord[1], localCoord[2], ix, iy, iz, dx, dy, dz) ) {
340  // Get Bx field values at grid cell corners
341  fHa[0][0][0] = fBz->At(ix *fNy*fNz + iy *fNz + iz);
342  fHa[1][0][0] = fBz->At((ix+1)*fNy*fNz + iy *fNz + iz);
343  fHa[0][1][0] = fBz->At(ix *fNy*fNz + (iy+1)*fNz + iz);
344  fHa[1][1][0] = fBz->At((ix+1)*fNy*fNz + (iy+1)*fNz + iz);
345  fHa[0][0][1] = fBz->At(ix *fNy*fNz + iy *fNz + (iz+1));
346  fHa[1][0][1] = fBz->At((ix+1)*fNy*fNz + iy *fNz + (iz+1));
347  fHa[0][1][1] = fBz->At(ix *fNy*fNz + (iy+1)*fNz + (iz+1));
348  fHa[1][1][1] = fBz->At((ix+1)*fNy*fNz + (iy+1)*fNz + (iz+1));
349  // Return interpolated field value
350  Double_t interpolatedCoord[3];
351  interpolatedCoord[0] = 0;
352  interpolatedCoord[1] = 0;
353  interpolatedCoord[2] = masterVecB[2] = Interpolate(dx, dy, dz);
354  if (fLocalMagFieldNode != NULL) {
355  fLocalMagFieldNode->LocalToMasterVect(interpolatedCoord, masterVecB);
356  }
357  return masterVecB[2];
358  }
359  return 0.;
360 }
361 // ------------------------------------------------------------------------
362 // ----------- Check whether a point is inside the map ----------------
363 Bool_t ERFieldMap::IsInside(Double_t x, Double_t y, Double_t z,
364  Int_t& ix, Int_t& iy, Int_t& iz,
365  Double_t& dx, Double_t& dy, Double_t& dz) {
366  // --- Transform into local coordinate system
367  Double_t xl = x; //- fPosX;
368  Double_t yl = y; //- fPosY;
369  Double_t zl = z; //- fPosZ;
370  // --- Check for being outside the map range
371  if ( ! ( xl >= fXmin && xl < fXmax && yl >= fYmin && yl < fYmax &&
372  zl >= fZmin && zl < fZmax ) ) {
373  ix = iy = iz = 0;
374  dx = dy = dz = 0.;
375  return kFALSE;
376  }
377 
378  // --- Determine grid cell
379  ix = Int_t( (xl-fXmin) / fXstep );
380  iy = Int_t( (yl-fYmin) / fYstep );
381  iz = Int_t( (zl-fZmin) / fZstep );
382  // Relative distance from grid point (in units of cell size)
383  dx = (xl-fXmin) / fXstep - Double_t(ix);
384  dy = (yl-fYmin) / fYstep - Double_t(iy);
385  dz = (zl-fZmin) / fZstep - Double_t(iz);
386  return kTRUE;
387 }
388 // ------------------------------------------------------------------------
389 // ---------- Write the map to an ASCII file --------------------------
390 void ERFieldMap::WriteAsciiFile(const char* fileName) {
391  // Open file
392  cout << "-I- ERFieldMap: Writing field map to ASCII file "
393  << fileName << endl;
394  std::ofstream mapFile(fileName);
395  if ( ! mapFile.is_open() ) {
396  cerr << "-E- ERFieldMap:ReadAsciiFile: Could not open file! " << endl;
397  return;
398  }
399  // Write field map grid parameters
400  mapFile.precision(6);
401  mapFile << showpoint;
402  if ( fType == 1 ) mapFile << "nosym" << endl;
403  if ( fType == 2 ) mapFile << "sym2" << endl;
404  if ( fType == 3 ) mapFile << "sym3" << endl;
405  mapFile << fXmin << " " << fXmax << " " << fNx << endl;
406  mapFile << fYmin << " " << fYmax << " " << fNy << endl;
407  mapFile << fZmin << " " << fZmax << " " << fNz << endl;
408  // Write field values
409  Double_t factor = 10. * fScale; // Takes out scaling and converts kG->T
410  cout << right;
411  Int_t nTot = fNx * fNy * fNz;
412  cout << "-I- ERFieldMap: " << fNx*fNy*fNz << " entries to write... "
413  << setw(3) << 0 << " % ";
414  Int_t index=0;
415  div_t modul;
416  Int_t iDiv = TMath::Nint(nTot/100.);
417  for(Int_t ix=0; ix<fNx; ix++) {
418  for(Int_t iy=0; iy<fNy; iy++) {
419  for(Int_t iz=0; iz<fNz; iz++) {
420  index =ix*fNy*fNz + iy*fNz + iz;
421  modul = div(index,iDiv);
422  if ( modul.rem == 0 ) {
423  Double_t perc = TMath::Nint(100.*index/nTot);
424  cout << "\b\b\b\b\b\b" << setw(3) << perc << " % " << flush;
425  }
426  mapFile << fBx->At(index)/factor << " " << fBy->At(index)/factor
427  << " " << fBz->At(index)/factor << endl;
428  } // z-Loop
429  } // y-Loop
430  } // x-Loop
431  cout << " " << index+1 << " written" << endl;
432  mapFile.close();
433 }
434 // ------------------------------------------------------------------------
435 // ------- Write field map to a ROOT file -----------------------------
436 void ERFieldMap::WriteRootFile(const char* fileName,
437  const char* mapName) {
438  /*
439  ERFieldMapData* data = new ERFieldMapData(mapName, *this);
440  TFile* oldFile = gFile;
441  TFile* file = new TFile(fileName, "RECREATE");
442  data->Write();
443  file->Close();
444  if(oldFile) oldFile->cd();
445  */
446 }
447 // ------------------------------------------------------------------------
448 // ----- Set the position of the field centre in global coordinates -----
449 void ERFieldMap::SetPosition(Double_t x, Double_t y, Double_t z) {
450  fPosX = x;
451  fPosY = y;
452  fPosZ = z;
453 }
454 // ------------------------------------------------------------------------
455 // --------- Screen output --------------------------------------------
456 void ERFieldMap::Print(Option_t*) const {
457  TString type = "Map";
458  if ( fType == 2 ) type = "Map sym2";
459  if ( fType == 3 ) type = "Map sym3";
460  cout << "======================================================" << endl;
461  cout.precision(4);
462  cout << showpoint;
463  cout << "---- " << fTitle << " : " << fName << endl;
464  cout << "----" << endl;
465  cout << "---- Field type : " << type << endl;
466  cout << "----" << endl;
467  cout << "---- Field map grid : " << endl;
468  cout << "---- x = " << setw(4) << fXmin << " to " << setw(4) << fXmax
469  << " cm, " << fNx << " grid points, dx = " << fXstep << " cm" << endl;
470  cout << "---- y = " << setw(4) << fYmin << " to " << setw(4) << fYmax
471  << " cm, " << fNy << " grid points, dy = " << fYstep << " cm" << endl;
472  cout << "---- z = " << setw(4) << fZmin << " to " << setw(4) << fZmax
473  << " cm, " << fNz << " grid points, dz = " << fZstep << " cm" << endl;
474  cout << endl;
475  cout << "---- Field centre position: ( " << setw(6) << fPosX << ", "
476  << setw(6) << fPosY << ", " << setw(6) << fPosZ << ") cm" << endl;
477  cout << "---- Field scaling factor: " << fScale << endl;
478  cout << "----" << endl;
479  cout << "---- Field at origin is ( " << setw(6) << fBxOrigin << ", " << setw(6)
480  << fByOrigin << ", " << setw(6) << fBzOrigin << ") kG" << endl;
481  cout << "======================================================" << endl;
482 }
483 // ------------------------------------------------------------------------
484 // --------- Reset parameters and data (private) ----------------------
486  fPosX = fPosY = fPosZ = 0.;
487  fXmin = fYmin = fZmin = 0.;
488  fXmax = fYmax = fZmax = 0.;
489  fXstep = fYstep = fZstep = 0.;
490  fNx = fNy = fNz = 0;
491  fScale = 1.;
492  if ( fBx ) { delete fBx; fBx = NULL; }
493  if ( fBy ) { delete fBy; fBy = NULL; }
494  if ( fBz ) { delete fBz; fBz = NULL; }
495 }
496 // ------------------------------------------------------------------------
497 // ----- Read field map from ASCII file (private) ---------------------
498 void ERFieldMap::ReadAsciiFile(const char* fileName) {
499  Double_t bx=0., by=0., bz=0.;
500  Double_t localVecB[3]; // (Bx, By, Bz) in local frame;
501  Double_t masterVecB[3]; // (Bx, By, Bz) in master frame;
502  Double_t localCoord[3];
503  Double_t masterCoord[3];
504  // Open file
505  LOG(INFO) << "ERFieldMap: Reading field map from ASCII file "
506  << fileName << FairLogger::endl;
507  std::ifstream mapFile(fileName);
508  if ( ! mapFile.is_open() ) {
509  LOG(ERROR) << "ERFieldMap:ReadAsciiFile: Could not open file!" << FairLogger::endl;
510  LOG(FATAL) << "ERFieldMap:ReadAsciiFile: Could not open file!" << FairLogger::endl;
511  }
512  // Read map type
513  TString type;
514  mapFile >> type;
515  Int_t iType = 0;
516  if ( type == "nosym" ) iType = 1;
517  if ( type == "sym2" ) iType = 2;
518  if ( type == "sym3" ) iType = 3;
519  if ( fType != iType ) {
520  cout << "-E- ERFieldMap::ReadAsciiFile: Incompatible map types!"
521  << endl;
522  cout << " Field map is of type " << fType
523  << " but map on file is of type " << iType << endl;
524  Fatal("ReadAsciiFile","Incompatible map types");
525  }
526  // Read grid parameters
527  mapFile >> fXmin >> fXmax >> fNx;
528  mapFile >> fYmin >> fYmax >> fNy;
529  mapFile >> fZmin >> fZmax >> fNz;
530 
531  fXstep = ( fXmax - fXmin ) / Double_t( fNx - 1 );
532  fYstep = ( fYmax - fYmin ) / Double_t( fNy - 1 );
533  fZstep = ( fZmax - fZmin ) / Double_t( fNz - 1 );
534 
535  // Create field arrays
536  fBx = new TArrayF(fNx * fNy * fNz);
537  fBy = new TArrayF(fNx * fNy * fNz);
538  fBz = new TArrayF(fNx * fNy * fNz);
539  // Read the field values
540  Double_t factor = fScale * 10.; // Factor 10 for T -> kG
541  cout << right;
542  Int_t nTot = fNx * fNy * fNz;
543  cout << "-I- ERFieldMap: " << nTot << " entries to read... "
544  << setw(3) << 0 << " % ";
545  Int_t index = 0;
546  div_t modul;
547  Int_t iDiv = TMath::Nint(nTot/100.);
548 
549 
550  for (Int_t ix=0; ix<fNx; ix++) {
551  for (Int_t iy = 0; iy<fNy; iy++) {
552  for (Int_t iz = 0; iz<fNz; iz++) {
553  if (! mapFile.good()) cerr << "-E- ERFieldMap::ReadAsciiFile: "
554  << "I/O Error at " << ix << " "
555  << iy << " " << iz << endl;
556  index = ix*fNy*fNz + iy*fNz + iz;
557  /*
558  modul = div(index,iDiv);
559  if ( modul.rem == 0 ) {
560  Double_t perc = TMath::Nint(100.*index/nTot);
561  cout << "\b\b\b\b\b\b" << setw(3) << perc << " % " << flush;
562  }
563  */
564  mapFile >> bx >> by >> bz;
565 
566  fBx->AddAt(factor*bx, index);
567  fBy->AddAt(factor*by, index);
568  fBz->AddAt(factor*bz, index);
569  if ( mapFile.eof() ) {
570  cerr << endl << "-E- ERFieldMap::ReadAsciiFile: EOF"
571  << " reached at " << ix << " " << iy << " " << iz << endl;
572  mapFile.close();
573  break;
574  }
575  } // z-Loop
576  } // y-Loop0)
577  } // x-Loop
578  cout << " " << index+1 << " read" << endl;
579  mapFile.close();
580 }
581 // ------------------------------------------------------------------------
582 // ------------- Read field map from ROOT file (private) ---------------
583 void ERFieldMap::ReadRootFile(const char* fileName,
584  const char* mapName) {
585  /*
586  // Store gFile pointer
587  TFile* oldFile = gFile;
588  // Open root file
589  LOG(INFO) << "ERFieldMap: Reading field map from ROOT file "
590  << fileName << FairLogger::endl;
591  TFile* file = new TFile(fileName, "READ");
592  if (!(file->IsOpen())) {
593  LOG(ERROR) << "ERFieldMap:ReadRootFile: Could not open file!" << FairLogger::endl;
594  LOG(FATAL) << "ERFieldMap:ReadRootFile: Could not open file!" << FairLogger::endl;
595  }
596  // Get the field data object
597  ERFieldMapData* data = NULL;
598  file->GetObject(mapName, data);
599  if ( ! data ) {
600  cout << "-E- ERFieldMap::ReadRootFile: data object " << fileName
601  << " not found in file! " << endl;
602  exit(-1);
603  }
604  // Get the field parameters
605  SetField(data);
606  // Close the root file and delete the data object
607  file->Close();
608  delete data;
609  if ( oldFile ) oldFile->cd();
610  */
611 }
612 // ------------------------------------------------------------------------
613 // ------------ Interpolation in a grid cell (private) -----------------
614 Double_t ERFieldMap::Interpolate(Double_t dx, Double_t dy, Double_t dz) {
615  // Interpolate in x coordinate
616  fHb[0][0] = fHa[0][0][0] + ( fHa[1][0][0]-fHa[0][0][0] ) * dx;
617  fHb[1][0] = fHa[0][1][0] + ( fHa[1][1][0]-fHa[0][1][0] ) * dx;
618  fHb[0][1] = fHa[0][0][1] + ( fHa[1][0][1]-fHa[0][0][1] ) * dx;
619  fHb[1][1] = fHa[0][1][1] + ( fHa[1][1][1]-fHa[0][1][1] ) * dx;
620  // Interpolate in y coordinate
621  fHc[0] = fHb[0][0] + ( fHb[1][0] - fHb[0][0] ) * dy;
622  fHc[1] = fHb[0][1] + ( fHb[1][1] - fHb[0][1] ) * dy;
623  // Interpolate in z coordinate
624  return fHc[0] + ( fHc[1] - fHc[0] ) * dz;
625 }
626 // ------------------------------------------------------------------------
627 ClassImp(ERFieldMap)
Double_t fHa[2][2][2]
Definition: ERFieldMap.h:125
virtual Bool_t IsInside(Double_t x, Double_t y, Double_t z, Int_t &ix, Int_t &iy, Int_t &iz, Double_t &dx, Double_t &dy, Double_t &dz)
Definition: ERFieldMap.cxx:363
virtual void SetPosition(Double_t x, Double_t y, Double_t z)
Definition: ERFieldMap.cxx:449
TArrayF * GetBx() const
Definition: ERFieldMap.h:85
TArrayF * fBx
Definition: ERFieldMap.h:120
void ReadAsciiFile(const char *fileName)
Definition: ERFieldMap.cxx:498
virtual void Init()
Definition: ERFieldMap.cxx:196
TGeoNode * fLocalMagFieldNode
z-component of the field at the origin
Definition: ERFieldMap.h:132
Double_t fHb[2][2]
Field at corners of a grid cell.
Definition: ERFieldMap.h:126
Int_t GetType() const
Definition: ERFieldPar.h:36
Double_t fBzOrigin
y-component of the field at the origin
Definition: ERFieldMap.h:130
Double_t fHc[2]
Interpolated field (2-dim)
Definition: ERFieldMap.h:127
Double_t fBxOrigin
Interpolated field (1-dim)
Definition: ERFieldMap.h:128
Double_t fPosX
Definition: ERFieldMap.h:112
void WriteRootFile(const char *fileName, const char *mapName)
Definition: ERFieldMap.cxx:436
void ReadRootFile(const char *fileName, const char *mapName)
Definition: ERFieldMap.cxx:583
Double_t fXmin
Definition: ERFieldMap.h:114
void Reset()
Definition: ERFieldMap.cxx:485
void WriteAsciiFile(const char *fileName)
Definition: ERFieldMap.cxx:390
Double_t Interpolate(Double_t dx, Double_t dy, Double_t dz)
Definition: ERFieldMap.cxx:614
Int_t fNx
Definition: ERFieldMap.h:118
TString fFileName
Definition: ERFieldMap.h:108
Double_t fScale
Definition: ERFieldMap.h:110
virtual ~ERFieldMap()
Definition: ERFieldMap.cxx:189
Double_t fByOrigin
x-component of the field at the origin
Definition: ERFieldMap.h:129
virtual void Print(Option_t *="") const
Definition: ERFieldMap.cxx:456