EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairGeoOldAsciiIo.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairGeoOldAsciiIo.cxx
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 10/11/2003
3 
5 // FairGeoOldAsciiIo
6 //
7 // Class for geometry I/O from ASCII file written in old FAIR input format
8 // This class is only used for conversion to the new format!
9 //
11 
12 #include "FairGeoOldAsciiIo.h"
13 
14 #include "FairGeoSet.h"
15 #include "FairGeoNode.h"
16 #include "FairGeoMedium.h"
17 #include "FairGeoMedia.h"
18 #include "FairGeoShapes.h"
19 
20 using std::cout;
21 using std::cerr;
22 using std::endl;
23 using std::ios;
24 
26 
28  : FairGeoIo(),
29  filename(""),
30  filedir(""),
31  writable(kFALSE),
32  file(NULL)
33 {
34  // Constructor
35 }
36 
38 {
39  // Destructor
40  close();
41  if (file) {
42  delete file;
43  file=0;
44  }
45 }
46 
47 Bool_t FairGeoOldAsciiIo::open(const char* fname,const Text_t* status)
48 {
49  // Opens the file fname
50  close();
51  if (!file) { file=new std::fstream(); }
52  else { (file->clear()); }
53  if (!filedir.IsNull()) { filename=filedir+"/"+fname; }
54  else { filename=fname; }
55  filename=filename.Strip();
56  if (strcmp(status,"in")==0) {
57  file->open(filename,ios::in);
58  writable=kFALSE;
59  } else {
60  if (strcmp(status,"out")==0) {
61  file->open(filename,ios::in);
62  if (!isOpen()) {
63  file->close();
64  file->clear();
65  file->open(filename,ios::out);
66  writable=kTRUE;
67  } else {
68  file->close();
69  Error("open","Output file %s exists already and will not be recreated.",
70  filename.Data());
71  return kFALSE;
72  }
73  } else { Error("open","Invalid file option!"); }
74  }
75  if (file->rdbuf()->is_open()==0) {
76  Error("open","Failed to open file %s",filename.Data());
77  return kFALSE;
78  }
79  return kTRUE;
80 }
81 
83 {
84  // Returns kTRUE, if the file is open
85  if (file && file->rdbuf()->is_open()==1) { return kTRUE; }
86  return kFALSE;
87 }
88 
90 {
91  // Returns kTRUE, if the file is open and writable
92  if (isOpen() && writable) { return kTRUE; }
93  return kFALSE;
94 }
95 
97 {
98  // Closes the file
99  if (isOpen()) {
100  file->close();
101  filename="";
102  }
103 }
104 
106 {
107  // Prints file information
108  if (isOpen()) {
109  if (writable) { cout<<"Open output file: "<<filename<<endl; }
110  else { cout<<"Open input file: "<<filename<<endl; }
111  } else { cout<<"No file open."<<endl; }
112 }
113 
115 {
116  // Reads the geometry from file and converts it to the new format
117  if (!isOpen() || writable || set==0) { return kFALSE; }
118  std::fstream& fin=*file;
119  fin.clear();
120  fin.seekg(0,ios::beg);
121  FairGeoNode* volu=0;
122  Int_t sensitivity=0, na=0;
123  Double_t par[20];
124  TList* volumes=set->getListOfVolumes();
125  FairGeoShapes* pShapes=set->getShapes();
126  while(!fin.eof()) {
127  // Read volumeName
128  TString volumeName = "";
129  fin >> volumeName;
130  if (fin.eof()) { break; }
131  volu=new FairGeoNode;
132  volu->SetName(volumeName);
133  // Read sensitivity
134  fin >> sensitivity;
135 // Why this additional integer only in trd file ????????????
136  if (sensitivity>0) { fin >> na; }
137  // Read motherName
138  TString motherName = "";
139  fin >> motherName;
140  FairGeoNode* mother=0;
141  if (motherName=="world") { mother=set->getMasterNode("cave"); }
142  else { mother=set->getVolume(motherName.Data()); }
143  volu->setMother(mother);
144  // Read position and rotation matrix
145  Double_t r[9], t[3];
146  fin>>t[0]>>t[1]>>t[2];
147  for(Int_t kk=0; kk<3; kk++) { t[kk]*=10.; }
148  for(Int_t i=0; i<9; i++) { fin>>r[i]; }
149  FairGeoTransform& tf=volu->getTransform();
150  tf.setRotMatrix(r);
151  tf.setTransVector(t);
152  // Read material
153  TString materialName = "";
154  fin >> materialName;
155  FairGeoMedium* medium=media->getMedium(materialName);
156  if (!medium) {
157  medium=new FairGeoMedium(materialName);
158  media->addMedium(medium);
159  }
160  volu->setMedium(medium);
161  // Read shape
162  TString type = "";
163  fin >> type;
164  FairGeoBasicShape* sh=pShapes->selectShape(type);
165  if (sh) { volu->setShape(sh); }
166  else {
167  cerr << "Shape "<<type<<" not supported."<<endl;
168  return kFALSE;
169  }
170  Int_t npar = sh->getNumParam();
171  for (Int_t ik=0; ik<npar; ik++) {
172  fin >> par[ik];
173  }
174  Bool_t rc=calculateShapePoints(par,volu);
175  if (!rc) {
176  cerr << "Conversion for shape "<<type<<" not implemented."<<endl;
177  return kFALSE;
178  }
179  // Check of volume end
180  TString control = "";
181  fin >> control;
182  if ( control != "#fi" && !fin.eof()) {
183  cerr << "End of File section is '" << control << "' instead of '#fi'." << endl;
184  return kFALSE;
185  }
186  volu->print();
187  volumes->Add(volu);
188  }
189  return kTRUE;
190 }
191 
193 {
195  TString shName=sh->GetName();
196  Int_t n=sh->getNumPoints();
197  volu->createPoints(n);
198  Bool_t rc=kTRUE;
199  if (shName == "BOX ") {
200  Double_t x=par[0]*10.;
201  Double_t y=par[1]*10.;
202  Double_t z=par[2]*10.;
203  volu->setPoint(0, x,-y,-z);
204  volu->setPoint(1, x, y,-z);
205  volu->setPoint(2,-x, y,-z);
206  volu->setPoint(3,-x,-y,-z);
207  volu->setPoint(4, x,-y, z);
208  volu->setPoint(5, x, y, z);
209  volu->setPoint(6,-x, y, z);
210  volu->setPoint(7,-x,-y, z);
211  } else if (shName == "TUBE") {
212  Double_t z=par[2]*10.;
213  volu->setPoint(0,0.,0.,-z);
214  volu->setPoint(1,par[0]*10.,par[1]*10.,0.);
215  volu->setPoint(2,0.,0.,z);
216  } else if (shName == "TUBS") {
217  Double_t z=par[2]*10.;
218  Double_t a=par[3]/6.28318548*360;
219  volu->setPoint(0,0.,0.,-z);
220  volu->setPoint(1,par[0]*10.,par[1]*10.,0.);
221  volu->setPoint(2,0.,0.,z);
222  volu->setPoint(3,a,par[4]/6.28318548*360+a,0.);
223  } else if (shName == "CONE") {
224  Double_t z=par[4]*10.;
225  volu->setPoint(0,0.,0.,-z);
226  volu->setPoint(1,par[0]*10.,par[1]*10.,0.);
227  volu->setPoint(2,0.,0.,z);
228  volu->setPoint(3,par[2]*10.,par[3]*10.,0.);
229  } else if (shName == "CONS") {
230  Double_t z=par[4]*10.;
231  Double_t a=par[5]/6.28318548*360;
232  volu->setPoint(0,0.,0.,-z);
233  volu->setPoint(1,par[0]*10.,par[1]*10.,0.);
234  volu->setPoint(2,0.,0.,z);
235  volu->setPoint(3,par[2]*10.,par[3]*10.,0.);
236  volu->setPoint(4,a,par[6]/6.28318548*360+a,0.);
237  } else if (shName == "SPHE") {
238  Double_t a=par[4]/6.28318548*360;
239  volu->setPoint(0,par[0]*10.,par[1]*10.,0.);
240  volu->setPoint(1,par[1],par[2],0.);
241  volu->setPoint(2,a,par[5]/6.28318548*360+a,0.);
242  } else if (shName == "ELTU") {
243  Double_t z=par[2]*10.;
244  volu->setPoint(0,0.,0.,-z);
245  volu->setPoint(1,par[0]*10.,par[1]*10.,0.);
246  volu->setPoint(2,0.,0.,z);
247  } else { rc=kFALSE; }
248  return rc;
249 }