EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EicMagneticFieldFromGradients.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EicMagneticFieldFromGradients.cxx
1 //
2 // RMP (rpetti@bnl.gov), 2016/02/22
3 //
4 // EIC magnetic field map handler;
5 // imports from gradients
6 //
7 
8 #include <stdlib.h>
9 #include <assert.h>
10 #include <dirent.h>
11 #include <unistd.h>
12 
13 #include <TFile.h>
14 
15 #include <FairRunSim.h>
16 
17 #include <EicFieldGradDetector.h>
18 #include <EicLibrary.h>
20 
21 
22 // =======================================================================================
23 
25 {
26  // If file name is given, import it as a whole; in principle may want to add more
27  // fields to existing structure; should work I guess;
28  if (fileName) {
29  TFile fin(ExpandedFileName(fileName));
30 
31  if (!fin.IsOpen())
32  fLogger->Fatal(MESSAGE_ORIGIN, "\033[5m\033[31m Failed to open '%s' field! \033[0m",
33  fileName);
34 
36  fin.GetObject(_EIC_MAGNETIC_FIELD_FROM_GRADIENTS_, fptr);
37  fin.Close();
38 
39  if (!fptr)
40  fLogger->Fatal(MESSAGE_ORIGIN, "\033[5m\033[31m Failed to import '%s' field! \033[0m",
41  fileName);
42 
43  *this = *fptr;
44  } //if
45 
46  // Is it really needed?; just set whatever index >2;
47  fType = 3;
48 
49  mInitialized = false;
50 } // EicMagneticFieldFromGradients::EicMagneticFieldFromGradients()
51 
52 // ---------------------------------------------------------------------------------------
53 
54 int EicMagneticFieldFromGradients::AddBeamLineElementGrads(const char *directory, float fieldScaler, int color)
55 {
56  TString dirName = ExpandedFileName("input/", directory);
57  std::cout << "dirName = " << dirName << std::endl;
58 
59  DIR *curr_dir = opendir(dirName.Data());
60  if (!curr_dir)
61  fLogger->Fatal(MESSAGE_ORIGIN, "\033[5m\033[31m Directory '%s' does not exist! \033[0m",
62  dirName.Data());
63 
64  // Loop through all ".csv" file in this directory and add them one by one;
65  {
66  struct dirent *curr_file;
67  int extention_len = strlen(".dat");
68 
69  while((curr_file = readdir(curr_dir))) {
70  int len = strlen(curr_file->d_name);
71 
72  if (len >= extention_len &&
73  !memcmp(curr_file->d_name + len - extention_len,
74  ".dat", extention_len))
75  {
76  //TString fileName = TString(directory) + "/" + curr_file->d_name;
77  TString fileName = dirName + "/" + curr_file->d_name;
78 
79  // Figure out the actual file name;
80  //TString fileName = ExpandedFileName("input/", GetFileName());
81 
82  std::cout << "Getting ready to read in the text file with the field information" << std::endl;
83 
84  //FILE *fin = fopen(fileName.Data(), "r");
85  std::ifstream fin;
86  fin.open(fileName.Data());
87  if (!fin) {
88  printf("-E- EicBeamLineElementGrad::Initialize() -> fail to open '%s' file!\n", fileName.Data());
89  return -1;
90  } //if
91 
92  std::cout << "its open..." << std::endl;
93 
94  char buffer[1024];
95  char mName[256];
96  double mCenterX, mCenterY, mCenterZ, mRadius, mLength, mAngle, mB, mGradient;
97 
98  // FIXME: this crap should be fixed one day;
99  //fgets(buffer, 1024-1, fin);
100  // read in the header comments and discard it
101  for(int i=0; i<6; i++)
102  {
103  fin.getline(buffer, 1024);
104  }
105 
106  // format for files
107  //-------------------------------
108  // element name (header to be ingnored)
109  // magnet_name center_x[m] center_y[m] center_z[m] aperture_radius[m] length[m] angle[mrad] B[T] gradient[T/m]
110 
111  std::cout << "extract the information in the file" << std::endl;
112  while(1)
113  {
114  if(fin >> mName >> mCenterZ >> mCenterX >> mCenterY >> mRadius >> mLength >> mAngle >> mB >> mGradient)
115  {
116 
117  printf("Adding beam line element gradient '%s'\n", mName);
118  printf("%15.10f %15.10f %15.10f %15.10f %15.10f %15.10f %15.10f %15.10f\n",
119  mCenterX, mCenterY, mCenterZ, mRadius, mLength, mAngle, mB, mGradient);
120 
121  // No transformation and no shape (will be taken from the source file);
122  EicBeamLineElementGrad *map = new EicBeamLineElementGrad(mName, mCenterX, mCenterY, mCenterZ, mRadius, mLength, mAngle, mB, mGradient);
123  map->SetYokeColor(color);
124  map->SetFieldScale(fieldScaler);
125  AddFieldGradient(map);//new EicBeamLineElementMap(fileName.Data(), 0, 0, color));
126  } // if
127  else{
128  break;
129  }
130  } // while(1)
131  fin.close();
132  } /*if*/
133  } //while
134  }
135 
136  return 0;
137 } // EicMagneticFieldFromGradients::AddBeamLineElementMaps()
138 
139 // ---------------------------------------------------------------------------------------
140 
142 {
144 
145  // Loop through all the maps and call detector construction routines if available; at present only
146  // beam line elements can do (and perhaps even there this functionality is rather artificial);
147  for(unsigned mm=0; mm<mMaps.size(); mm++) {
148  EicMagneticFieldGrad *fmap = mMaps[mm];
149 
150  if (fmap->CapableToBuildYoke()) {
151  //printf("here: %s\n", fmap->GetDetectorName().Data()); exit(0);
152 
153  fRun->AddModule(new EicFieldGradDetector(fmap, Active));
154  } //if
155  } //for mm
156 
157  return 0;
158 } // EicMagneticFieldFromGradients::CreateYokeVolumes()
159 
160 // ---------------------------------------------------------------------------------------
161 
163 {
164  for(unsigned mm=0; mm<mMaps.size(); mm++) {
165  EicMagneticFieldGrad *fmap = mMaps[mm];
166 
167  // Map was initialized already -> skip here;
168  if (fmap->Initialized()) continue;
169 
170  // Well, this call is used in FairRoot::Init() implementation which has no return code;
171  // so if anything goes wrong here, just exit with a clear red message;
172  if (fmap->Initialize())
173  fLogger->Fatal(MESSAGE_ORIGIN, "\033[5m\033[31m Failed to initialize '%s' field map! \033[0m",
174  fmap->GetFileName().IsNull() ? "NO NAME" : fmap->GetFileName().Data());
175  } //for mm
176 
177  mInitialized = true;
178 
179  // FIXME: arrange a queue or a separate task for such purposes;
180  /*
181  {
182  FairRun *fRun = FairRun::Instance();
183 
184  // I hope there is no need to save/restore current directory here?;
185  fRun->GetOutputFile()->cd();
186 
187  Write(_EIC_MAGNETIC_FIELD_);
188  }
189  */
190  return 0;
191 } // EicMagneticFieldFromGradients::InitializeFieldGradients()
192 
193 // ---------------------------------------------------------------------------------------
194 
195 int EicMagneticFieldFromGradients::GetFieldSumValue(const double xx[], double B[])
196 {
197  // Reset field to 0.0 for clarity;
198  for(unsigned iq=0; iq<3; iq++)
199  B[iq] = 0.0;
200 
201  if (!mInitialized && InitializeFieldGradients()) return -1;
202 
203  // FIXME: need an error printout here I guess;
204  if (!xx || !B || !mMaps.size()) return -1;
205 
206  // Failure per default;
207  int ret = -1;
208 
209 
210  // For now a trivial implementation: just a sum of all maps which can provide
211  // field at this location xx[];
212  for(unsigned mm=0; mm<mMaps.size(); mm++) {
213  EicMagneticFieldGrad *fmap = mMaps[mm];
214 
215  double BMap[3];
216 
217  if (!fmap->GetFieldValue(xx, BMap)) {
218  for(unsigned iq=0; iq<3; iq++)
219  B[iq] += BMap[iq];
220 
221  ret = 0;
222  } //if
223  } //for mm
224 
225  return ret;
226 } // EicMagneticFieldFromGradients::GetFieldSumValue()
227 
228 // -----------------------------------------------------------------------------------------------
229 
230 /*
231 int EicMagneticFieldFromGradients::Export(const char *fileName) const
232 {
233  //if (!mInitialied) return -1;
234 
235  // Yes, export always happens precisely to the path given via 'fileName' (no VMCWORKDIR
236  // expansion like in importTpcDigiParameters());
237  TFile fout(fileName, "RECREATE");
238 
239  if (!fout.IsOpen())
240  {
241  printf("-E- EicMagneticFieldFromGradients::Export() -> failed to open '%s' for writing!\n", fileName);
242  return -1;
243  } //if
244 
245  fout.WriteObject(this, _EIC_MAGNETIC_FIELD_);
246  fout.Close();
247 
248  return 0;
249 } // EicMagneticFieldFromGradients::Export()
250 */
251 
252 // =======================================================================================
253 
254  //ClassImp(EicPndFieldMap)