12 #include<FairRunSim.h>
14 #include<FairPrimaryGenerator.h>
15 #include<FairBoxGenerator.h>
20 #include<FairParRootFileIo.h>
33 #include "TPolyLine3D.h"
37 #include<TApplication.h>
38 #include "TGLViewer.h"
39 #include <TGeoManager.h>
40 #include "TGeoMaterial.h"
41 #include "TGeoMedium.h"
42 #include "TGeoVolume.h"
45 #include "FairGeoLoader.h"
46 #include "FairGeoInterface.h"
47 #include "FairGeoMedia.h"
48 #include "FairGeoBuilder.h"
73 cout <<
" reading file " << filename_in << endl;
74 ifstream jostmapFile(filename_in.c_str());
75 if (!jostmapFile.is_open()) {
76 cout <<
"ReadAsciiFile: Could not open file! " << endl;
95 map <double, int> x_counts;
96 map <double, int> y_counts;
97 map <double, int> z_counts;
98 map <double, int>::iterator it_counts, it_counts_prev;
99 map <string, TVector3> field_in;
100 map <string, TVector3>::iterator it_field_in;
105 getline (jostmapFile , line);
106 if (!jostmapFile.good())
break;
108 bool isvalidline =
true;
112 unsigned int ivalchar = 0;
114 unsigned int ival = 0;
116 for (
unsigned int ichar = 0; ichar < line.size(); ichar++){
117 if (line[ichar] ==
'#'){
125 if (line[ichar] ==
' ' || line[ichar] ==
'\n' || line[ichar] ==
'\0' || ichar == line.size()-1){
126 if (value[0] !=
'\0' && ivalchar != 0){
128 cout <<
" hmm, more than 6 values per line? -> Fatal " << endl;
131 value[ivalchar] =
'\n';
132 values[ival] = atof(value);
137 if (line[ichar] ==
'\n' || line[ichar] ==
'\0'){
142 value[ivalchar] = line[ichar];
145 if (ival != 6 && ival > 0 && isvalidline) {
146 cout <<
" not enough values in this line!!! -> Fatal " << ival << endl;
147 cout << line << endl;
152 if (x_min == -1.e9 || values[0] < x_min) x_min = values[0];
153 if (x_max == -1.e9 || values[0] > x_max) x_max = values[0];
154 if (y_min == -1.e9 || values[1] < y_min) y_min = values[1];
155 if (y_max == -1.e9 || values[1] > y_max) y_max = values[1];
156 if (z_min == -1.e9 || values[2] < z_min) z_min = values[2];
157 if (z_max == -1.e9 || values[2] > z_max) z_max = values[2];
158 x_counts[values[0]]++;
159 y_counts[values[1]]++;
160 z_counts[values[2]]++;
162 key << ((int)floor(values[0])) <<
"_" << ((
int)floor(values[1])) <<
"_" << ((
int)floor(values[2]));
163 TVector3 fieldval(values[3], values[4], values[5]);
164 field_in[key.str()] = fieldval;
170 cout <<
" finished reading " << endl;
172 n_x = x_counts.size();
173 n_y = y_counts.size();
174 n_z = z_counts.size();
175 d_x = (int)floor((x_max - x_min)/(n_x-1));
176 d_y = (int)floor((y_max - y_min)/(n_y-1));
177 d_z = (int)floor((z_max - z_min)/(n_z-1));
179 cout <<
"grid size is " << n_x <<
" X " << n_y <<
" X " << n_z << endl;
180 cout <<
"grid spacing is [mm] " << d_x <<
" " << d_y <<
" " << d_z << endl;
181 cout << x_min <<
" < x [mm] < " << x_max << endl;
182 cout << y_min <<
" < y [mm] < " << y_max << endl;
183 cout << z_min <<
" < z [mm] < " << z_max << endl;
185 cout <<
"checking consistency " << endl;
188 for (it_counts = x_counts.begin(); it_counts != x_counts.end(); it_counts++){
189 if (it_counts == x_counts.begin()) {
190 it_counts_prev = it_counts;
193 double spacing = it_counts->first - it_counts_prev->first;
194 if (
fabs(spacing - d_x) > 1e-5)
195 cout <<
" warning: grid spacing does not match for dx: " << spacing <<
" vs " << d_x << endl;
196 if (it_counts->second != it_counts_prev->second)
197 cout <<
" warning: grid is not fully determined in x! " << endl;
198 it_counts_prev = it_counts;
200 for (it_counts = y_counts.begin(); it_counts != y_counts.end(); it_counts++){
201 if (it_counts == y_counts.begin()) {
202 it_counts_prev = it_counts;
205 double spacing = it_counts->first - it_counts_prev->first;
206 if (
fabs(spacing - d_y) > 1e-5)
207 cout <<
" warning: grid spacing does not match for dy: " << spacing <<
" vs " << d_y << endl;
208 if (it_counts->second != it_counts_prev->second)
209 cout <<
" warning: grid is not fully determined in y! " << endl;
210 it_counts_prev = it_counts;
212 for (it_counts = z_counts.begin(); it_counts != z_counts.end(); it_counts++){
213 if (it_counts == z_counts.begin()) {
214 it_counts_prev = it_counts;
217 double spacing = it_counts->first - it_counts_prev->first;
218 if (
fabs(spacing - d_z) > 1e-5)
219 cout <<
" warning: grid spacing does not match for dz: " << spacing <<
" vs " << d_z << endl;
220 if (it_counts->second != it_counts_prev->second)
221 cout <<
" warning: grid is not fully determined in z! " << endl;
222 it_counts_prev = it_counts;
224 cout <<
" done " << endl;
229 cout <<
"writing " << filename_out <<
" files" << endl;
230 ofstream mapFile((filename_out+
".dat").c_str());
231 if (!mapFile.is_open()) {
232 cout <<
"WriteAsciiFile: Could not open file! " << endl;
237 mapFile.precision(6);
238 mapFile << showpoint;
240 mapFile <<
"nosym" << endl;
242 mapFile <<
"Solenoid" << endl;
244 mapFile <<
"Dipole" << endl;
246 mapFile <<
"Trans" << endl;
251 mapFile <<
"G" << endl;
255 mapFile << x_min/10. <<
" " << x_max/10. <<
" "
257 mapFile << y_min/10. <<
" " << y_max/10. <<
" "
259 mapFile << z_min/10. <<
" " << z_max/10. <<
" "
266 Int_t nTot = n_x * n_y * n_z;
267 cout <<
"-I- PndFieldMap: " << n_x * n_y * n_z <<
" entries to write... "
268 << setw(3) << 0 <<
" % ";
273 for (
int x = 0;
x < n_x;
x++) {
274 for (
int y = 0;
y < n_y;
y++) {
275 for (
int z = 0;
z < n_z;
z++) {
276 index =
x * n_y * n_z +
y
285 key << (int)floor(x_min +
x * d_x) <<
"_";
286 key << (int)floor(y_min +
y * d_y) <<
"_";
287 key << (int)floor(z_min + z * d_z);
288 it_field_in = field_in.find(key.str());
289 if (it_field_in != field_in.end()){
290 double new_Bx = it_field_in->second.X();
291 double new_By = it_field_in->second.Y();
292 double new_Bz = it_field_in->second.Z();
296 modul = div(index, iDiv);
297 if (modul.rem == 0) {
299 cout <<
"\b\b\b\b\b\b" << setw(3) << perc <<
" % " << flush;
303 mapFile << new_Bx <<
" " << new_By <<
" " << new_Bz << endl;
305 cout <<
" fatal: no value to key " << key <<
" found!" << endl;
318 cout <<
" " << index + 1 <<
" written" << endl;
327 cout <<
" moving ascii file " << filename_out <<
" to the PANDAROOT input folder " << endl;
329 string dir = getenv(
"VMCWORKDIR");
330 string moveto = dir +
"/input/" + filename_out +
".dat";
331 system((
"mv "+filename_out+
".dat"+
" "+moveto).c_str());
332 cout <<
" converting ASCII files to ROOT files " << endl;
335 if (fieldtype == 2) {
336 fieldmap_converted =
new PndSolenoidMap(filename_out.c_str(),
"A");
339 if (fieldtype == 3) {
340 fieldmap_converted =
new PndDipoleMap(filename_out.c_str(),
"A");
343 if (fieldtype == 4) {
344 fieldmap_converted =
new PndTransMap (filename_out.c_str(),
"A");
347 if (fieldmap_converted){
348 fieldmap_converted->
Init();
349 stringstream _filename;
350 _filename << filename_out;
351 fieldmap_converted->
WriteRootFile((_filename.str()+
".root").c_str(),_filename.str().c_str());
352 cout <<
" converted " << n_x <<
" x " << n_y <<
" x " << n_z << endl;
353 cout <<
" to " << fieldmap_converted->
GetNx() <<
" x " << fieldmap_converted->
GetNy() <<
" x " << fieldmap_converted->
GetNz() << endl;
354 cout <<
" values " << endl;
357 static TApplication* myapp = NULL;
358 static TCanvas *canvas_fieldlines = NULL;
360 myapp =
new TApplication(
"myapp",0,0);
378 gROOT->Macro(
"$VMCWORKDIR/gconfig/rootlogon.C");
379 TString vmcWorkdir = gSystem->Getenv(
"VMCWORKDIR");
381 FairGeoLoader*
geoLoad =
new FairGeoLoader(
"TGeo",
"FairGeoLoader");
382 FairGeoInterface*
geoFace = geoLoad->getGeoInterface();
383 geoFace->setMediaFile(vmcWorkdir +
"/geometry/media_pnd.geo");
384 geoFace->readMedia();
386 FairGeoMedia*
geoMedia = geoFace->getMedia();
388 FairGeoBuilder*
geoBuild = geoLoad->getGeoBuilder();
389 Int_t
nmed=geoBuild->createMedium(FairMediumAir);
391 top_vol->SetVisibility(0);
392 TFile* solenoid_file =
new TFile(
"$VMCWORKDIR/geometry/FullSolenoid.root" ,
"OPEN");
393 TGeoVolume * solenoid = (TGeoVolume*) solenoid_file->Get(
"topNode");
395 cout <<
" 'Error: no solenoid file found to display " << endl;
397 solenoid->SetLineColor(12);
398 solenoid->SetTransparency(50);
399 top_vol->AddNode(solenoid, 1);
401 TFile* beampipe_file =
new TFile(
"$VMCWORKDIR/geometry/beampipe_201309.root" ,
"OPEN");
402 TGeoVolume * beampipe = (TGeoVolume*) beampipe_file->Get(
"pipeassembly");
404 cout <<
" 'Error: no beam pipe file found to display " << endl;
406 beampipe->SetTransparency(50);
407 top_vol->AddNode(beampipe, 1);
410 top_vol->Draw(
"ogl");
423 static vector<double>xstart_prev;
424 static vector<double>ystart_prev;
425 static vector<double>zstart_prev;
426 static vector<int> line_color;
428 if (xstart_prev.size()==0){
431 for (
double ix = -2.; ix < 2.; ix += .2){
432 for (
double iy = -2.; iy < 2.; iy += .2){
433 double r2 = (ix*ix + iy*iy);
434 if (r2 > 4)
continue;
435 xstart_prev.push_back(ix);
436 ystart_prev.push_back(iy);
437 zstart_prev.push_back(-50.);
438 int color = 12 + floor(7./4.*r2);
439 line_color.push_back(color);
444 for (
unsigned int istartpoint = 0; istartpoint < xstart_prev.size(); istartpoint++){
446 double stepsize = d_x/10.;
447 if (d_y/10. < stepsize) stepsize = d_y/10.;
448 if (d_z/10. < stepsize) stepsize = d_z/10.;
452 xs.push_back(xstart_prev[istartpoint]);
453 ys.push_back(ystart_prev[istartpoint]);
454 zs.push_back(zstart_prev[istartpoint]);
456 for (
int ipoint = 1; ipoint <
npoints; ipoint++){
458 double pval[3] = {xs[ipoint-1],ys[ipoint-1],zs[ipoint-1]};
463 if (!(x_min/10. < pval[0] && pval[0] < x_max/10.)||
464 !(y_min/10. < pval[1] && pval[1] < y_max/10.)||
465 !(z_min/10. < pval[2] && pval[2] < z_max/10.)){
468 xstart_prev[istartpoint] = pval[0];
469 ystart_prev[istartpoint] = pval[1];
470 zstart_prev[istartpoint] = pval[2];
476 fieldmap_converted->GetBxyz(pval, Bval);
478 double fieldstrength =
sqrt(Bval[0]*Bval[0]+Bval[1]*Bval[1]+Bval[2]*Bval[2]);
479 pval[0] = pval[0] + stepsize/fieldstrength*Bval[0];
480 pval[1] = pval[1] + stepsize/fieldstrength*Bval[1];
481 pval[2] = pval[2] + stepsize/fieldstrength*Bval[2];
483 xs.push_back(pval[0]);
484 ys.push_back(pval[1]);
485 zs.push_back(pval[2]);
490 TPolyLine3D* fieldline =
new TPolyLine3D(xs.size(), &xs[0], &ys[0], &zs[0]);
491 fieldline->SetLineColor(line_color[istartpoint]);
492 fieldline->Draw(
"same");
494 for (
unsigned int ipoint = 0; ipoint < xs.size(); ipoint++){
495 xs[ipoint] = -xs[ipoint];
497 fieldline =
new TPolyLine3D(xs.size(), &xs[0], &ys[0], &zs[0]);
498 fieldline->SetLineColor(line_color[istartpoint]);
499 fieldline->Draw(
"same");
500 for (
unsigned int ipoint = 0; ipoint < xs.size(); ipoint++){
501 ys[ipoint] = -ys[ipoint];
503 fieldline =
new TPolyLine3D(xs.size(), &xs[0], &ys[0], &zs[0]);
504 fieldline->SetLineColor(line_color[istartpoint]);
505 fieldline->Draw(
"same");
506 for (
unsigned int ipoint = 0; ipoint < xs.size(); ipoint++){
507 xs[ipoint] = -xs[ipoint];
509 fieldline =
new TPolyLine3D(xs.size(), &xs[0], &ys[0], &zs[0]);
510 fieldline->SetLineColor(line_color[istartpoint]);
511 fieldline->Draw(
"same");
537 int main(
int argc,
char **argv) {
int main(int argc, char **argv)
friend F32vec4 sqrt(const F32vec4 &a)
void WriteRootFile(const char *fileName, const char *mapName)
TGeoManager * gGeoManager
FairGeoMedium * FairMediumAir
const string name_modification
TTree * gtree_values(NULL)
friend F32vec4 fabs(const F32vec4 &a)
void read_jost_fieldmaps()
FairGeoInterface * geoFace
vector< string > fieldmapnames
FairGeoBuilder * geoBuild
void transform_jost_fieldmap(string filename_in, string filename_out, int fieldtype)