CbmRoot
Loading...
Searching...
No Matches
CbmFieldMapCreator.cxx
Go to the documentation of this file.
1/* Copyright (C) 2008-2020 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Volker Friese [committer], Florian Uhlig */
4
5// -------------------------------------------------------------------------
6// ----- CbmFieldMapCreator source file -----
7// ----- Created 15/01/08 by V. Friese -----
8// -------------------------------------------------------------------------
10
11#include "CbmFieldMap.h" // for CbmFieldMap
12
13#include <FairField.h> // for FairField
14
15#include <TArrayF.h> // for TArrayF
16#include <TCollection.h> // for TIter
17#include <TList.h> // for TList
18
19#include <cstring> // for strcmp
20#include <iostream> // for operator<<, basic_ostream, endl, ostream, cout
21
22using namespace std;
23
24// ------------- Default constructor -----------------------------------
26 : fMapName("")
27 , fNx(0)
28 , fNy(0)
29 , fNz(0)
30 , fXmin(0.)
31 , fXmax(0.)
32 , fYmin(0.)
33 , fYmax(0.)
34 , fZmin(0.)
35 , fZmax(0.)
36 , fBx(nullptr)
37 , fBy(nullptr)
38 , fBz(nullptr)
39 , fFieldList()
40 , fInit(kFALSE)
41{
42}
43// ------------------------------------------------------------------------
44
45
46// ------------- Standard constructor ----------------------------------
48 : fMapName(mapName)
49 , fNx(0)
50 , fNy(0)
51 , fNz(0)
52 , fXmin(0.)
53 , fXmax(0.)
54 , fYmin(0.)
55 , fYmax(0.)
56 , fZmin(0.)
57 , fZmax(0.)
58 , fBx(nullptr)
59 , fBy(nullptr)
60 , fBz(nullptr)
61 , fFieldList()
62 , fInit(kFALSE)
63
64{
65}
66// ------------------------------------------------------------------------
67
68
69// ------------ Destructor --------------------------------------------
71{
72 if (fBx) delete fBx;
73 if (fBy) delete fBy;
74 if (fBz) delete fBz;
75 fFieldList.Clear();
76}
77// ------------------------------------------------------------------------
78
79
80// ------------- Public method SetGridParameters ----------------------
81void CbmFieldMapCreator::SetGridParameters(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin,
82 Double_t ymax, Int_t nz, Double_t zmin, Double_t zmax)
83{
84 fNx = nx;
85 fNy = ny;
86 fNz = nz;
87 fXmin = xmin;
88 fYmin = ymin;
89 fZmin = zmin;
90 fXmax = xmax;
91 fYmax = ymax;
92 fZmax = zmax;
93 fInit = kTRUE;
94}
95// ------------------------------------------------------------------------
96
97
98// ----------- Public method CreateMap --------------------------------
99Bool_t CbmFieldMapCreator::CreateMap(const char* fileName)
100{
101
102 // Define output file name
103 TString outFileName = fileName;
104
105 if (strcmp(fileName, "") == 0) {
106 // if (fileName == "") {
107 outFileName = fMapName + ".root";
108 }
109
110 // Check for proper intialisation
111 if (!fInit) {
112 cout << "-E- CbmFieldMapCreator::CreateMap: "
113 << "Grid parameters are not specified!" << endl;
114 return kFALSE;
115 }
116 if (fFieldList.IsEmpty()) {
117 cout << "-E- CbmFieldMapCreator::CreateMap: "
118 << "No input field(s) specified!" << endl;
119 return kFALSE;
120 }
121
122 // Create field arrays
123 fBx = new TArrayF(fNx * fNy * fNz);
124 fBy = new TArrayF(fNx * fNy * fNz);
125 fBz = new TArrayF(fNx * fNy * fNz);
126
127 // Calculate grid step sizes
128 Double_t xStep = (fXmax - fXmin) / Double_t(fNx - 1);
129 Double_t yStep = (fYmax - fYmin) / Double_t(fNy - 1);
130 Double_t zStep = (fZmax - fZmin) / Double_t(fNz - 1);
131
132 // Control output
133 cout.precision(2);
134 cout << "-I- CbmFieldMapCreator: Grid step sizes are " << xStep << ", " << yStep << ", " << zStep << " cm" << endl;
135 cout << " Using " << fFieldList.GetSize() << " input fields." << endl;
136 cout << " Total number of grid points is " << 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 " << outFileName << endl;
179 return kTRUE;
180}
181// ------------------------------------------------------------------------
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)
Bool_t CreateMap(const char *fileName="")
void WriteRootFile(const char *fileName, const char *mapName)
Hash for CbmL1LinkKey.