FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairGeoOldAsciiIo.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 //*-- AUTHOR : Ilse Koenig
9 //*-- Created : 10/11/2003
10 
12 // FairGeoOldAsciiIo
13 //
14 // Class for geometry I/O from ASCII file written in old FAIR input format
15 // This class is only used for conversion to the new format!
16 //
18 #include "FairGeoOldAsciiIo.h"
19 
20 #include "FairGeoBasicShape.h" // for FairGeoBasicShape
21 #include "FairGeoMedia.h" // for FairGeoMedia
22 #include "FairGeoMedium.h" // for FairGeoMedium
23 #include "FairGeoNode.h" // for FairGeoNode
24 #include "FairGeoSet.h" // for FairGeoSet
25 #include "FairGeoShapes.h" // for FairGeoShapes
26 #include "FairGeoTransform.h" // for FairGeoTransform
27 
28 #include <TList.h> // for TList
29 #include <fstream> // for fstream
30 #include <iostream> // for cerr, cout
31 #include <string.h> // for strcmp
32 
33 using std::cerr;
34 using std::cout;
35 using std::endl;
36 using std::ios;
37 
39 
41  : FairGeoIo()
42  , filename("")
43  , filedir("")
44  , writable(kFALSE)
45  , file(nullptr)
46 {
47  // Constructor
48 }
49 
51 {
52  // Destructor
53  close();
54  if (file) {
55  delete file;
56  file = 0;
57  }
58 }
59 
60 Bool_t FairGeoOldAsciiIo::open(const char* fname, const Text_t* status)
61 {
62  // Opens the file fname
63  close();
64  if (!file) {
65  file = new std::fstream();
66  } else {
67  (file->clear());
68  }
69  if (!filedir.IsNull()) {
70  filename = filedir + "/" + fname;
71  } else {
72  filename = fname;
73  }
74  filename = filename.Strip();
75  if (strcmp(status, "in") == 0) {
76  file->open(filename, ios::in);
77  writable = kFALSE;
78  } else {
79  if (strcmp(status, "out") == 0) {
80  file->open(filename, ios::in);
81  if (!isOpen()) {
82  file->close();
83  file->clear();
84  file->open(filename, ios::out);
85  writable = kTRUE;
86  } else {
87  file->close();
88  Error("open", "Output file %s exists already and will not be recreated.", filename.Data());
89  return kFALSE;
90  }
91  } else {
92  Error("open", "Invalid file option!");
93  }
94  }
95  if (file->rdbuf()->is_open() == 0) {
96  Error("open", "Failed to open file %s", filename.Data());
97  return kFALSE;
98  }
99  return kTRUE;
100 }
101 
103 {
104  // Returns kTRUE, if the file is open
105  if (file && file->rdbuf()->is_open() == 1) {
106  return kTRUE;
107  }
108  return kFALSE;
109 }
110 
112 {
113  // Returns kTRUE, if the file is open and writable
114  if (isOpen() && writable) {
115  return kTRUE;
116  }
117  return kFALSE;
118 }
119 
121 {
122  // Closes the file
123  if (isOpen()) {
124  file->close();
125  filename = "";
126  }
127 }
128 
130 {
131  // Prints file information
132  if (isOpen()) {
133  if (writable) {
134  cout << "Open output file: " << filename << endl;
135  } else {
136  cout << "Open input file: " << filename << endl;
137  }
138  } else {
139  cout << "No file open." << endl;
140  }
141 }
142 
144 {
145  // Reads the geometry from file and converts it to the new format
146  if (!isOpen() || writable || set == 0) {
147  return kFALSE;
148  }
149  std::fstream& fin = *file;
150  fin.clear();
151  fin.seekg(0, ios::beg);
152  FairGeoNode* volu = 0;
153  Int_t sensitivity = 0, na = 0;
154  TList* volumes = set->getListOfVolumes();
155  FairGeoShapes* pShapes = set->getShapes();
156  while (!fin.eof()) {
157  // Read volumeName
158  TString volumeName = "";
159  fin >> volumeName;
160  if (fin.eof()) {
161  break;
162  }
163  volu = new FairGeoNode;
164  volu->SetName(volumeName);
165  // Read sensitivity
166  fin >> sensitivity;
167  // Why this additional integer only in trd file ????????????
168  if (sensitivity > 0) {
169  fin >> na;
170  }
171  // Read motherName
172  TString motherName = "";
173  fin >> motherName;
174  FairGeoNode* mother = 0;
175  if (motherName == "world") {
176  mother = set->getMasterNode("cave");
177  } else {
178  mother = set->getVolume(motherName.Data());
179  }
180  volu->setMother(mother);
181  // Read position and rotation matrix
182  Double_t r[9], t[3];
183  fin >> t[0] >> t[1] >> t[2];
184  for (Int_t kk = 0; kk < 3; kk++) {
185  t[kk] *= 10.;
186  }
187  for (Int_t i = 0; i < 9; i++) {
188  fin >> r[i];
189  }
190  FairGeoTransform& tf = volu->getTransform();
191  tf.setRotMatrix(r);
192  tf.setTransVector(t);
193  // Read material
194  TString materialName = "";
195  fin >> materialName;
196  FairGeoMedium* medium = media->getMedium(materialName);
197  if (!medium) {
198  medium = new FairGeoMedium(materialName);
199  media->addMedium(medium);
200  }
201  volu->setMedium(medium);
202  // Read shape
203  TString type = "";
204  fin >> type;
205  FairGeoBasicShape* sh = pShapes->selectShape(type);
206  if (sh) {
207  volu->setShape(sh);
208  } else {
209  cerr << "Shape " << type << " not supported." << endl;
210  return kFALSE;
211  }
212  Int_t npar = sh->getNumParam();
213  Double_t* par = new Double_t[npar](); //() after array default-initialize an array
214  for (Int_t ik = 0; ik < npar; ik++) {
215  fin >> par[ik];
216  }
217  Bool_t rc = calculateShapePoints(par, volu);
218  delete[] par;
219  if (!rc) {
220  cerr << "Conversion for shape " << type << " not implemented." << endl;
221  return kFALSE;
222  }
223  // Check of volume end
224  TString control = "";
225  fin >> control;
226  if (control != "#fi" && !fin.eof()) {
227  cerr << "End of File section is '" << control << "' instead of '#fi'." << endl;
228  return kFALSE;
229  }
230  volu->print();
231  volumes->Add(volu);
232  }
233  return kTRUE;
234 }
235 
236 Bool_t FairGeoOldAsciiIo::calculateShapePoints(Double_t* par, FairGeoNode* volu)
237 {
238  FairGeoBasicShape* sh = volu->getShapePointer();
239  TString shName = sh->GetName();
240  Int_t n = sh->getNumPoints();
241  volu->createPoints(n);
242  Bool_t rc = kTRUE;
243  if (shName == "BOX ") {
244  Double_t x = par[0] * 10.;
245  Double_t y = par[1] * 10.;
246  Double_t z = par[2] * 10.;
247  volu->setPoint(0, x, -y, -z);
248  volu->setPoint(1, x, y, -z);
249  volu->setPoint(2, -x, y, -z);
250  volu->setPoint(3, -x, -y, -z);
251  volu->setPoint(4, x, -y, z);
252  volu->setPoint(5, x, y, z);
253  volu->setPoint(6, -x, y, z);
254  volu->setPoint(7, -x, -y, z);
255  } else if (shName == "TUBE") {
256  Double_t z = par[2] * 10.;
257  volu->setPoint(0, 0., 0., -z);
258  volu->setPoint(1, par[0] * 10., par[1] * 10., 0.);
259  volu->setPoint(2, 0., 0., z);
260  } else if (shName == "TUBS") {
261  Double_t z = par[2] * 10.;
262  Double_t a = par[3] / 6.28318548 * 360;
263  volu->setPoint(0, 0., 0., -z);
264  volu->setPoint(1, par[0] * 10., par[1] * 10., 0.);
265  volu->setPoint(2, 0., 0., z);
266  volu->setPoint(3, a, par[4] / 6.28318548 * 360 + a, 0.);
267  } else if (shName == "CONE") {
268  Double_t z = par[4] * 10.;
269  volu->setPoint(0, 0., 0., -z);
270  volu->setPoint(1, par[0] * 10., par[1] * 10., 0.);
271  volu->setPoint(2, 0., 0., z);
272  volu->setPoint(3, par[2] * 10., par[3] * 10., 0.);
273  } else if (shName == "CONS") {
274  Double_t z = par[4] * 10.;
275  Double_t a = par[5] / 6.28318548 * 360;
276  volu->setPoint(0, 0., 0., -z);
277  volu->setPoint(1, par[0] * 10., par[1] * 10., 0.);
278  volu->setPoint(2, 0., 0., z);
279  volu->setPoint(3, par[2] * 10., par[3] * 10., 0.);
280  volu->setPoint(4, a, par[6] / 6.28318548 * 360 + a, 0.);
281  } else if (shName == "SPHE") {
282  Double_t a = par[4] / 6.28318548 * 360;
283  volu->setPoint(0, par[0] * 10., par[1] * 10., 0.);
284  volu->setPoint(1, par[1], par[2], 0.);
285  volu->setPoint(2, a, par[5] / 6.28318548 * 360 + a, 0.);
286  } else if (shName == "ELTU") {
287  Double_t z = par[2] * 10.;
288  volu->setPoint(0, 0., 0., -z);
289  volu->setPoint(1, par[0] * 10., par[1] * 10., 0.);
290  volu->setPoint(2, 0., 0., z);
291  } else {
292  rc = kFALSE;
293  }
294  return rc;
295 }
void addMedium(FairGeoMedium *m)
void setTransVector(const FairGeoVector &t)
void setPoint(const Int_t, const Double_t, const Double_t, const Double_t)
FairGeoMedium * getMedium(const char *)
FairGeoShapes * getShapes()
Definition: FairGeoSet.h:86
Bool_t read(FairGeoMedia *)
ClassImp(FairEventBuilder)
void setMother(FairGeoNode *s)
Definition: FairGeoNode.h:136
void setShape(FairGeoBasicShape *s)
Definition: FairGeoNode.h:127
FairGeoNode * getVolume(const char *name)
Definition: FairGeoSet.h:83
Bool_t open(const char *, const Text_t *status="in")
void createPoints(const Int_t)
FairGeoBasicShape * selectShape(FairGeoVolume *)
TList * getListOfVolumes()
Definition: FairGeoSet.h:85
FairMQExParamsParOne * par
void setRotMatrix(const FairGeoRotation &r)
void setMedium(FairGeoMedium *med)
Definition: FairGeoNode.h:91
FairGeoTransform & getTransform()
Definition: FairGeoVolume.h:48
FairGeoNode * getMasterNode(const char *name)
Definition: FairGeoSet.h:84
FairGeoBasicShape * getShapePointer()
Definition: FairGeoNode.h:72