FairRoot/PandaRoot
interpolate_fieldmaps.cxx
Go to the documentation of this file.
1 // Panda FullSim macro
2 
3 #include<TString.h>
4 #include<TStopwatch.h>
5 #include<TROOT.h>
6 #include<FairModule.h>
7 #include<PndCave.h>
8 #include<PndMagnet.h>
9 #include<PndPipe.h>
10 #include<PndMvdDetector.h>
11 #include<FairRunSim.h>
12 #include<PndLmdDetector.h>
13 #include<FairPrimaryGenerator.h>
14 #include<FairBoxGenerator.h>
15 #include<PndMultiField.h>
16 #include<PndTransMap.h>
17 #include<PndDipoleMap.h>
18 #include<PndSolenoidMap.h>
19 #include<FairParRootFileIo.h>
20 #include<PndMultiFieldPar.h>
21 #include<map>
22 #include<vector>
23 #include<fstream>
24 #include<iostream>
25 #include<iomanip>
26 #include<sstream>
27 #include<TH1.h>
28 #include<TTree.h>
29 #include<TFile.h>
30 #include<TCanvas.h>
31 
32 using namespace std;
33 
34 // for testing purposes the default
35 // field map name is modified
36 // Moreover in case of setting a string of
37 // length > 0
38 // files are stored in the $VMCWORKDIR/input/ folder
39 const string name_modification = "_v1";
40 
41 vector<string> fieldmapnames;
42 
43 TTree* gtree_values(NULL);
44 
45 double gBx, gBy, gBz, gDBx, gDBy, gDBz;
46 
48  PndFieldMap* map_to_interpol, double weight, string fileName) {
49  // Open file
50  //LOG(INFO) << "PndFieldMap: Writing field map to ASCII file " << fileName.c_Str() ;
51  ofstream mapFile((fileName+".dat").c_str());
52  if (!mapFile.is_open()) {
53  cout << "WriteAsciiFile: Could not open file! " << endl;
54  return;
55  }
56 
57  // Write field map grid parameters
58  mapFile.precision(6);
59  mapFile << showpoint;
60  if (map_to_corr->GetType() == 1)
61  mapFile << "nosym" << endl;
62  if (map_to_corr->GetType() == 2)
63  mapFile << "Solenoid" << endl;
64  if (map_to_corr->GetType() == 3)
65  mapFile << "Dipole" << endl;
66  if (map_to_corr->GetType() == 4)
67  mapFile << "Trans" << endl;
68  if (map_to_corr->GetUnit() == 10.0)
69  mapFile << "T" << endl;
70  if (map_to_corr->GetUnit() == 0.001)
71  mapFile << "G" << endl;
72  if (map_to_corr->GetUnit() == 1.0)
73  mapFile << "kG" << endl;
74 
75  mapFile << map_to_corr->GetXmin() << " " << map_to_corr->GetXmax() << " "
76  << map_to_corr->GetNx() << endl;
77  mapFile << map_to_corr->GetYmin() << " " << map_to_corr->GetYmax() << " "
78  << map_to_corr->GetNy() << endl;
79  mapFile << map_to_corr->GetZmin() << " " << map_to_corr->GetZmax() << " "
80  << map_to_corr->GetNz() << endl;
81 
82  // Write field values
83  Double_t factor = map_to_corr->GetUnit() * map_to_corr->GetScale(); // Takes out scaling
84  cout << right;
85  Int_t nTot = map_to_corr->GetNx() * map_to_corr->GetNy() * map_to_corr->GetNz();
86  cout << "-I- PndFieldMap: " << map_to_corr->GetNx() * map_to_corr->GetNy() * map_to_corr->GetNz() << " entries to write... "
87  << setw(3) << 0 << " % ";
88  Int_t index = 0;
89  div_t modul;
90  Int_t iDiv = TMath::Nint(nTot / 100.);
91 
92  // using linear interpolation between two values from two energies E1 and E2
93  // x_E = [0,1]; f(0) = val(E1); f(1) = val(E2)
94  // f(x_E) = (f(1)-f(0)) * x_E + f(0) =
95  for (int x = 0; x < map_to_corr->GetNx(); x++) {
96  for (int y = 0; y < map_to_corr->GetNy(); y++) {
97  for (int z = 0; z < map_to_corr->GetNz(); z++) {
98  index = x * map_to_corr->GetNy() * map_to_corr->GetNz() + y
99  * map_to_corr->GetNz() + z;
100  double Bx1 = (*(map_to_corr->GetBx()))[index];
101  double Bx2 = (*(map_to_interpol->GetBx()))[index];
102  double By1 = (*(map_to_corr->GetBy()))[index];
103  double By2 = (*(map_to_interpol->GetBy()))[index];
104  double Bz1 = (*(map_to_corr->GetBz()))[index];
105  double Bz2 = (*(map_to_interpol->GetBz()))[index];
106  double new_Bx = (Bx2 - Bx1) * weight + Bx1;
107  double new_By = (By2 - By1) * weight + By1;
108  double new_Bz = (Bz2 - Bz1) * weight + Bz1;
109  //cout << " changing \t" << Bx1 << '\t' << By1 << '\t' << Bz1 << '\n' ;
110  //cout << " to \t" << new_Bx << '\t' << new_By << '\t' << new_Bz << '\n' << endl;
111  modul = div(index, iDiv);
112  if (modul.rem == 0) {
113  Double_t perc = TMath::Nint(100. * index / nTot);
114  cout << "\b\b\b\b\b\b" << setw(3) << perc << " % " << flush;
115  }
116  mapFile << new_Bx / factor << " " << new_By
117  / factor << " " << new_Bz / factor << endl;
118  }
119  }
120  }
121 
122 // for (Int_t ix = 0; ix < fNx; ix++) {
123 // for (Int_t iy = 0; iy < fNy; iy++) {
124 // for (Int_t iz = 0; iz < fNz; iz++) {
125 // index = ix * fNy * fNz + iy * fNz + iz;
126 // } // z-Loop
127 // } // y-Loop
128 // } // x-Loop
129  cout << " " << index + 1 << " written" << endl;
130  mapFile.close();
131 
132  // test
133  //map_to_corr->WriteAsciiFile((fileName+".dat").c_str());
134 
135  // It is indeed annoying that I did not find any accessors to the field values to
136  // modify. So I have to write an ascii file, read it and save it as a root file.
137 
138  cout << " moving ascii file " << fileName << " to the PANDAROOT input folder " << endl;
139  // move it to the input directory of PANDAROOT
140  string dir = getenv("VMCWORKDIR");
141  string moveto = dir + "/input/" + fileName + ".dat";
142  system(("mv "+fileName+".dat"+" "+moveto).c_str());
143  cout << " converting ASCII files to ROOT files " << endl;
144  PndFieldMap* fieldmap_converted = NULL;
145  //gfield = 0;
146  if (map_to_corr->GetType() == 3) {
147  fieldmap_converted = new PndDipoleMap(fileName.c_str(), "A");
148  //gfield = 3;
149  }
150  if (map_to_corr->GetType() == 4) {
151  fieldmap_converted = new PndTransMap (fileName.c_str(), "A");
152  //gfield = 4;
153  }
154  if (fieldmap_converted){
155  fieldmap_converted->Init();
156  stringstream _filename;
157  _filename << fileName;// << "_" <<
158  fieldmap_converted->WriteRootFile((_filename.str()+".root").c_str(),_filename.str().c_str());
159  cout << " converted " << map_to_corr->GetNx() << " x " << map_to_corr->GetNy() << " x " << map_to_corr->GetNz() << endl;
160  cout << " to " << fieldmap_converted->GetNx() << " x " << fieldmap_converted->GetNy() << " x " << fieldmap_converted->GetNz() << endl;
161  cout << " values " << endl;
162 
163  cout << " creating comparison plots " << endl;
164  TH1F hist_relative_difference_Bx("relative_difference_Bx", ("relative difference in Bx for map "+_filename.str()).c_str(), 1000, -1, 1);
165  TH1F hist_relative_difference_By("relative_difference_By", ("relative difference in By for map "+_filename.str()).c_str(), 1000, -1, 1);
166  TH1F hist_relative_difference_Bz("relative_difference_Bz", ("relative difference in Bz for map "+_filename.str()).c_str(), 1000, -1, 1);
167  for (int x = 0; x < map_to_corr->GetNx(); x++) {
168  for (int y = 0; y < map_to_corr->GetNy(); y++) {
169  for (int z = 0; z < map_to_corr->GetNz(); z++) {
170  index = x * map_to_corr->GetNy() * map_to_corr->GetNz() + y
171  * map_to_corr->GetNz() + z;
172  double Bx1 = (*(map_to_corr->GetBx()))[index];
173  double Bx2 = (*(fieldmap_converted->GetBx()))[index];
174  double By1 = (*(map_to_corr->GetBy()))[index];
175  double By2 = (*(fieldmap_converted->GetBy()))[index];
176  double Bz1 = (*(map_to_corr->GetBz()))[index];
177  double Bz2 = (*(fieldmap_converted->GetBz()))[index];
178  gBx = Bx1;
179  gBy = By1;
180  gBz = Bz1;
181  gDBx = (Bx2-Bx1);
182  gDBy = (By2-By1);
183  gDBz = (Bz2-Bz1);
184  gtree_values->Fill();
185  hist_relative_difference_Bx.Fill(gDBx/Bx1*100.);
186  hist_relative_difference_By.Fill(gDBy/By1*100.);
187  hist_relative_difference_Bz.Fill(gDBz/Bz1*100.);
188  modul = div(index, iDiv);
189  if (modul.rem == 0) {
190  Double_t perc = TMath::Nint(100. * index / nTot);
191  cout << "\b\b\b\b\b\b" << setw(3) << perc << " % " << flush;
192  }
193  }
194  }
195  }
196  hist_relative_difference_Bx.Draw();
197  hist_relative_difference_Bx.SetXTitle("#Delta Bx/Bx [%]");
198  gPad->Print("relative_fieldmap_differences.ps(");
199  hist_relative_difference_By.Draw();
200  hist_relative_difference_By.SetXTitle("#Delta By/By [%]");
201  gPad->Print("relative_fieldmap_differences.ps(");
202  hist_relative_difference_Bz.Draw();
203  hist_relative_difference_Bz.SetXTitle("#Delta Bz/Bz [%]");
204  gPad->Print("relative_fieldmap_differences.ps(");
205  delete fieldmap_converted;
206  if (name_modification.size() > 0){
207  cout << " moving root file " << _filename.str()+".root" << " to the PANDAROOT input folder " << endl;
208  string _moveto = dir + "/input/" + _filename.str()+".root";
209  system(("mv "+_filename.str()+".root"+" "+_moveto).c_str());
210  fieldmapnames.push_back(_filename.str());
211  }
212  }
213 }
214 
216  //Load basic libraries
217  //gROOT->Macro("$VMCWORKDIR/gconfig/rootlogon.C");
218 
219  //PndFieldMap* fieldmap = new PndFieldMap("DipoleMap1.0150", "R");
220  // pairs of values for interpolation are provided
221  // the key is always the value to be corrected by interpolation
222  map < string, string > momenta;
223  // The weight is the position for the interpolation between 0 and 1
224  map<string, double> weight;
225  momenta["0150"] = "0406";
226  weight["0150"] = -0.001; // extrapolate
227  momenta["0406"] = "0150";
228  weight["0406"] = 0.00235; // interpolate
229  momenta["0890"] = "0406";
230  weight["0890"] = 0.0032;
231  momenta["1191"] = "0890";
232  weight["1191"] = 0.0067;
233  momenta["1500"] = "1191";
234  weight["1500"] = 0.0095;
235 
236  map<string, PndDipoleMap*> dipolefieldmaps1;
237  map<string, PndDipoleMap*> dipolefieldmaps2;
238  map<string, PndTransMap*> transfieldmaps;
239 
240  for (map<string, string>::iterator it = momenta.begin(); it
241  != momenta.end(); it++) {
242  string momentum = it->first;
243  dipolefieldmaps1[momentum] = new PndDipoleMap(
244  ("DipoleMap1." + (momentum)).c_str(), "R");
245  dipolefieldmaps1[momentum]->Init();
246  dipolefieldmaps2[momentum] = new PndDipoleMap(
247  ("DipoleMap2." + (momentum)).c_str(), "R");
248  dipolefieldmaps2[momentum]->Init();
249  transfieldmaps[momentum] = new PndTransMap(
250  ("TransMap." + (momentum)).c_str(), "R");
251  transfieldmaps[momentum]->Init();
252  //std::cout << "interpolating " << transfieldmaps[*it]->GetNx();
253  //std::cout << " x " << transfieldmaps[*it]->GetNy();
254  //std::cout << " x " << transfieldmaps[*it]->GetNz() << " values " << std::endl;
255  //dipolefieldmaps1[momentum]->WriteAsciiFile(
256  // ("Promme_test_file" + momentum + ".dat").c_str());
257  }
258 
259  TCanvas canvas("canvas", "comparison plots", 600, 600);
260  canvas.cd();
261  TFile filetree("values.root", "RECREATE");
262  int field;
263  int energy;
264  gtree_values = new TTree("tree", "tree");
265  gtree_values->Branch("Bx", &gBx);
266  gtree_values->Branch("By", &gBy);
267  gtree_values->Branch("Bz", &gBz);
268  gtree_values->Branch("DBx", &gDBx);
269  gtree_values->Branch("DBy", &gDBy);
270  gtree_values->Branch("DBz", &gDBz);
271  gtree_values->Branch("energy", &energy);
272  gtree_values->Branch("field", &field);
273  for (map<string, string>::iterator it = momenta.begin(); it
274  != momenta.end(); it++) {
275  string momentum = it->first;
276  string interpol_mom = it->second;
277  cout << " interpolating maps for " << momentum << endl;
278  //PndFieldMap* new_dipolefieldmap1 = new PndDipoleMap(("DipoleMap1_new."+(momentum)).c_str(), "R");
279  //new_dipolefiledmap1
280  energy = atoi((it->first).c_str());
281  field = 31;
282  interpolate_fieldmap(dipolefieldmaps1[momentum],
283  dipolefieldmaps1[interpol_mom], weight[momentum], "DipoleMap1"+name_modification+"."+momentum);
284  field = 32;
285  interpolate_fieldmap(dipolefieldmaps2[momentum],
286  dipolefieldmaps2[interpol_mom], weight[momentum], "DipoleMap2"+name_modification+"."+momentum);
287  field = 40;
288  interpolate_fieldmap(transfieldmaps[momentum],
289  transfieldmaps[interpol_mom], weight[momentum], "TransMap"+name_modification+"."+momentum);
290  }
291  // close the opened ps file
292  canvas.Clear();
293  canvas.Print("relative_fieldmap_differences.ps)");
294  filetree.Write();
295  // draw some more histograms for checks
296  canvas.Divide(2,2);
297  map<string, string> drawpairs;
298  drawpairs["Bx"]="DBx";
299  drawpairs["By"]="DBy";
300  drawpairs["Bz"]="DBz";
301  for (map<string, string>::iterator it = momenta.begin(); it
302  != momenta.end(); it++) {
303  string momentum = it->first;
304  energy = atoi((it->first).c_str());
305  for (map<string, string>::iterator itB = drawpairs.begin(); itB != drawpairs.end(); itB++){
306  canvas.cd(1);
307  gPad->Clear();
308  gtree_values->Draw((itB->first+":"+itB->second).c_str(),(momentum+"==energy&&field==31").c_str());
309  canvas.cd(2);
310  gPad->Clear();
311  gtree_values->Draw((itB->first+":"+itB->second).c_str(),(momentum+"==energy&&field==32").c_str());
312  canvas.cd(3);
313  gPad->Clear();
314  gtree_values->Draw((itB->first+":"+itB->second).c_str(),(momentum+"==energy&&field==40").c_str());
315  canvas.Print("correlation_histograms.ps(");
316  }
317  }
318  canvas.Clear();
319  canvas.Print("correlation_histograms.ps)");
320  filetree.Close();
321 
322  cout << "\n list of created field map names " << endl;
323  cout << " ********************************" << endl;
324  for (vector<string>::iterator it = fieldmapnames.begin(); it != fieldmapnames.end(); it++){
325  cout << " " << *it << endl;
326  }
327  cout << " ********************************" << endl;
328 }
329 
330 #include<TApplication.h>
331 
332 int main(int argc, char **argv) {
334  return 0;
335 }
336 
Double_t GetZmin() const
Definition: PndFieldMap.h:90
int main(int argc, char **argv)
virtual Double_t GetBy(Double_t x, Double_t y, Double_t z)
virtual void Init()
double gBx
Double_t GetYmax() const
Definition: PndFieldMap.h:92
Double_t GetScale() const
Definition: PndFieldMap.h:110
void WriteRootFile(const char *fileName, const char *mapName)
Int_t GetNy() const
Definition: PndFieldMap.h:98
double gBy
const string name_modification
Double_t
double gBz
TTree * gtree_values(NULL)
Double_t GetYmin() const
Definition: PndFieldMap.h:89
Double_t GetXmin() const
Definition: PndFieldMap.h:88
Int_t GetNx() const
Definition: PndFieldMap.h:97
Double_t z
double gDBz
void interpolate_fieldmaps()
Double_t x
double gDBx
void interpolate_fieldmap(PndFieldMap *map_to_corr, PndFieldMap *map_to_interpol, double weight, string fileName)
Double_t GetUnit() const
Definition: PndFieldMap.h:101
double gDBy
Double_t GetXmax() const
Definition: PndFieldMap.h:91
Double_t y
Int_t GetNz() const
Definition: PndFieldMap.h:99
virtual Double_t GetBx(Double_t x, Double_t y, Double_t z)
virtual Double_t GetBz(Double_t x, Double_t y, Double_t z)
int Nint(float x)
Definition: PndCAMath.h:117
Double_t GetZmax() const
Definition: PndFieldMap.h:93
vector< string > fieldmapnames
Double_t energy
Definition: plot_dirc.C:15