CbmRoot
CbmFieldMapCreator.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- CbmFieldMapCreator source file -----
3 // ----- Created 15/01/08 by V. Friese -----
4 // -------------------------------------------------------------------------
5 #include "CbmFieldMapCreator.h"
6 
7 #include "CbmFieldMap.h" // for CbmFieldMap
8 
9 #include <FairField.h> // for FairField
10 
11 #include <TArrayF.h> // for TArrayF
12 #include <TCollection.h> // for TIter
13 #include <TList.h> // for TList
14 
15 #include <cstring> // for strcmp
16 #include <iostream> // for operator<<, basic_ostream, endl, ostream, cout
17 
18 using namespace std;
19 
20 // ------------- Default constructor -----------------------------------
22  : fMapName("")
23  , fNx(0)
24  , fNy(0)
25  , fNz(0)
26  , fXmin(0.)
27  , fXmax(0.)
28  , fYmin(0.)
29  , fYmax(0.)
30  , fZmin(0.)
31  , fZmax(0.)
32  , fBx(nullptr)
33  , fBy(nullptr)
34  , fBz(nullptr)
35  , fFieldList()
36  , fInit(kFALSE) {}
37 // ------------------------------------------------------------------------
38 
39 
40 // ------------- Standard constructor ----------------------------------
42  : fMapName(mapName)
43  , fNx(0)
44  , fNy(0)
45  , fNz(0)
46  , fXmin(0.)
47  , fXmax(0.)
48  , fYmin(0.)
49  , fYmax(0.)
50  , fZmin(0.)
51  , fZmax(0.)
52  , fBx(nullptr)
53  , fBy(nullptr)
54  , fBz(nullptr)
55  , fFieldList()
56  , fInit(kFALSE)
57 
58 {}
59 // ------------------------------------------------------------------------
60 
61 
62 // ------------ Destructor --------------------------------------------
64  if (fBx) delete fBx;
65  if (fBy) delete fBy;
66  if (fBz) delete fBz;
67  fFieldList.Clear();
68 }
69 // ------------------------------------------------------------------------
70 
71 
72 // ------------- Public method SetGridParameters ----------------------
74  Double_t xmin,
75  Double_t xmax,
76  Int_t ny,
77  Double_t ymin,
78  Double_t ymax,
79  Int_t nz,
80  Double_t zmin,
81  Double_t zmax) {
82  fNx = nx;
83  fNy = ny;
84  fNz = nz;
85  fXmin = xmin;
86  fYmin = ymin;
87  fZmin = zmin;
88  fXmax = xmax;
89  fYmax = ymax;
90  fZmax = zmax;
91  fInit = kTRUE;
92 }
93 // ------------------------------------------------------------------------
94 
95 
96 // ----------- Public method CreateMap --------------------------------
97 Bool_t CbmFieldMapCreator::CreateMap(const char* fileName) {
98 
99  // Define output file name
100  TString outFileName = fileName;
101 
102  if (strcmp(fileName, "") == 0) {
103  // if (fileName == "") {
104  outFileName = fMapName + ".root";
105  }
106 
107  // Check for proper intialisation
108  if (!fInit) {
109  cout << "-E- CbmFieldMapCreator::CreateMap: "
110  << "Grid parameters are not specified!" << endl;
111  return kFALSE;
112  }
113  if (fFieldList.IsEmpty()) {
114  cout << "-E- CbmFieldMapCreator::CreateMap: "
115  << "No input field(s) specified!" << endl;
116  return kFALSE;
117  }
118 
119  // Create field arrays
120  fBx = new TArrayF(fNx * fNy * fNz);
121  fBy = new TArrayF(fNx * fNy * fNz);
122  fBz = new TArrayF(fNx * fNy * fNz);
123 
124  // Calculate grid step sizes
125  Double_t xStep = (fXmax - fXmin) / Double_t(fNx - 1);
126  Double_t yStep = (fYmax - fYmin) / Double_t(fNy - 1);
127  Double_t zStep = (fZmax - fZmin) / Double_t(fNz - 1);
128 
129  // Control output
130  cout.precision(2);
131  cout << "-I- CbmFieldMapCreator: Grid step sizes are " << xStep << ", "
132  << yStep << ", " << zStep << " cm" << endl;
133  cout << " Using " << fFieldList.GetSize()
134  << " input fields." << endl;
135  cout << " Total number of grid points is "
136  << fNx * fNy * fNz << endl;
137 
138  // Triple loop over grid points
139  Double_t x = 0.;
140  Double_t y = 0.;
141  Double_t z = 0.;
142  for (Int_t ix = 0; ix < fNx; ix++) {
143  x = fXmin + Double_t(ix) * xStep;
144  for (Int_t iy = 0; iy < fNy; iy++) {
145  y = fYmin + Double_t(iy) * yStep;
146  for (Int_t iz = 0; iz < fNz; iz++) {
147  z = fZmin + Double_t(iz) * zStep;
148 
149  // Get and add all field values at this grid point
150  Double_t bx = 0.;
151  Double_t by = 0.;
152  Double_t bz = 0.;
153  TIter next(&fFieldList);
154  while (FairField* field = ((FairField*) next())) {
155  bx += field->GetBx(x, y, z);
156  by += field->GetBy(x, y, z);
157  bz += field->GetBz(x, y, z);
158  }
159 
160  // Store field values in arrays
161  Int_t index = ix * fNy * fNz + iy * fNz + iz;
162  fBx->AddAt(Float_t(bx), index);
163  fBy->AddAt(Float_t(by), index);
164  fBz->AddAt(Float_t(bz), index);
165  }
166  }
167  }
168 
169  // Create new field map
170  CbmFieldMap* fieldMap = new CbmFieldMap(this);
171 
172  // Write the new field map to the ROOT file
173  fieldMap->WriteRootFile(outFileName.Data(), fMapName.Data());
174 
175  // Delete new field map
176  delete fieldMap;
177 
178  cout << "-I- CbmFieldMapCreator: Field map " << fMapName << " was stored in "
179  << outFileName << endl;
180  return kTRUE;
181 }
182 // ------------------------------------------------------------------------
CbmFieldMapCreator::fZmax
Double_t fZmax
Definition: CbmFieldMapCreator.h:98
CbmFieldMapCreator::fMapName
TString fMapName
Definition: CbmFieldMapCreator.h:94
CbmFieldMapCreator::fNx
Int_t fNx
Definition: CbmFieldMapCreator.h:95
CbmFieldMapCreator::CbmFieldMapCreator
CbmFieldMapCreator()
Definition: CbmFieldMapCreator.cxx:21
CbmFieldMapCreator::fXmin
Double_t fXmin
Definition: CbmFieldMapCreator.h:96
CbmFieldMapCreator::fNy
Int_t fNy
Definition: CbmFieldMapCreator.h:95
CbmFieldMap
Definition: CbmFieldMap.h:34
CbmFieldMapCreator.h
CbmFieldMapCreator::fBx
TArrayF * fBx
Definition: CbmFieldMapCreator.h:99
CbmFieldMapCreator::fYmin
Double_t fYmin
Definition: CbmFieldMapCreator.h:97
CbmFieldMapCreator::fFieldList
TList fFieldList
Definition: CbmFieldMapCreator.h:102
CbmFieldMap::WriteRootFile
void WriteRootFile(const char *fileName, const char *mapName)
Definition: CbmFieldMap.cxx:536
CbmFieldMapCreator::fInit
Bool_t fInit
Definition: CbmFieldMapCreator.h:103
CbmFieldMapCreator::fNz
Int_t fNz
Definition: CbmFieldMapCreator.h:95
x
Double_t x
Definition: CbmMvdSensorDigiToHitTask.cxx:68
y
Double_t y
Definition: CbmMvdSensorDigiToHitTask.cxx:68
CbmFieldMapCreator::fZmin
Double_t fZmin
Definition: CbmFieldMapCreator.h:98
CbmFieldMapCreator::SetGridParameters
void SetGridParameters(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax, Int_t nz, Double_t zmin, Double_t zmax)
Definition: CbmFieldMapCreator.cxx:73
CbmFieldMapCreator::~CbmFieldMapCreator
virtual ~CbmFieldMapCreator()
Definition: CbmFieldMapCreator.cxx:63
CbmFieldMapCreator::fBz
TArrayF * fBz
Definition: CbmFieldMapCreator.h:101
CbmFieldMap.h
CbmFieldMapCreator::fXmax
Double_t fXmax
Definition: CbmFieldMapCreator.h:96
CbmFieldMapCreator::CreateMap
Bool_t CreateMap(const char *fileName="")
Definition: CbmFieldMapCreator.cxx:97
CbmFieldMapCreator::fBy
TArrayF * fBy
Definition: CbmFieldMapCreator.h:100
CbmFieldMapCreator::fYmax
Double_t fYmax
Definition: CbmFieldMapCreator.h:97