FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairGeoAsciiIo.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 // FairGeoAsciiIo
13 //
14 // Class for geometry I/O from ASCII file
15 //
17 #include "FairGeoAsciiIo.h"
18 
19 #include "FairGeoInterface.h" // for FairGeoInterface
20 #include "FairGeoIo.h" // for FairGeoIo
21 #include "FairGeoMedia.h" // for FairGeoMedia
22 #include "FairGeoSet.h" // for FairGeoSet
23 
24 #include <fstream> // for fstream
25 #include <iostream> // for cout
26 #include <stdio.h> // for sscanf
27 #include <string.h> // for strcmp, strlen, strtok, etc
28 
29 using std::cout;
30 using std::endl;
31 using std::ios;
32 
34 
36  : FairGeoIo()
37  , filename("")
38  , filedir("")
39  , writable(kFALSE)
40  , file(nullptr)
41 {
42  // Constructor
43 }
44 
46 {
47  // Destructor
48  close();
49  delete file;
50  file = 0;
51 }
52 
53 Bool_t FairGeoAsciiIo::open(const char* fname, const Text_t* status)
54 {
55  // Opens the file fname
56  close();
57  if (!file) {
58  file = new std::fstream();
59  } else {
60  (file->clear());
61  }
62  if (!filedir.IsNull()) {
63  filename = filedir + "/" + fname;
64  } else {
65  filename = fname;
66  }
67  filename = filename.Strip();
68  if (strcmp(status, "in") == 0) {
69  file->open(filename, ios::in);
70  writable = kFALSE;
71  } else {
72  if (strcmp(status, "out") == 0) {
73  file->open(filename, ios::in);
74  if (!isOpen()) {
75  file->close();
76  file->clear();
77  file->open(filename, ios::out);
78  writable = kTRUE;
79  } else {
80  file->close();
81  Error("open", "Output file %s exists already and will not be recreated.", filename.Data());
82  return kFALSE;
83  }
84  } else {
85  Error("open", "Invalid file option!");
86  }
87  }
88  if (file->rdbuf()->is_open() == 0) {
89  Fatal("open", "Failed to open file %s", filename.Data());
90  return kFALSE;
91  }
92  return kTRUE;
93 }
94 
96 {
97  // Returns kTRUE, if the file is open
98  if (file && file->rdbuf()->is_open() == 1) {
99  return kTRUE;
100  }
101  return kFALSE;
102 }
103 
105 {
106  // Returns kTRUE, if the file is open and writable
107  if (isOpen() && writable) {
108  return kTRUE;
109  }
110  return kFALSE;
111 }
112 
114 {
115  // Closes the file
116  if (isOpen()) {
117  file->close();
118  filename = "";
119  }
120 }
121 
123 {
124  // Prints file information
125  if (isOpen()) {
126  if (writable) {
127  cout << "Open output file: " << filename << endl;
128  } else {
129  cout << "Open input file: " << filename << endl;
130  }
131  } else {
132  cout << "No file open." << endl;
133  }
134 }
135 
137 {
138  // Reads the media from file
139  if (!isOpen() || writable || media == 0) {
140  return kFALSE;
141  }
142  media->read(*file);
143  return kTRUE;
144 }
145 
147 {
148  // Reads the geometry set from file
149  if (!isOpen() || writable || set == 0) {
150  return kFALSE;
151  }
152  set->read(*file, media);
153  return kTRUE;
154 }
155 
157 {
158  // Writes the media to file
159  if (!isOpen() || !writable || media == 0) {
160  return kFALSE;
161  }
162  media->write(*file);
163  return kTRUE;
164 }
165 
167 {
168  // Writes the geometry set to file
169  if (!isOpen() || !writable || set == 0) {
170  return kFALSE;
171  }
172  set->write(*file);
173  return kTRUE;
174 }
175 
177 {
178  // Reads the GEANT configuration file
179  if (!isOpen() || writable || interface == 0) {
180  return kFALSE;
181  }
182  TString buf(256);
183  TString simRefRun, historyDate;
184  Int_t k = -1;
185  while (!(*file).eof()) {
186  buf.ReadLine(*file);
187  buf = buf.Strip(buf.kBoth);
188  if (!buf.IsNull() && buf(0, 2) != "//" && buf(0, 1) != "*") {
189  if (buf.Contains(".geo") || buf.Contains("_gdb")) {
190  interface->addInputFile(buf.Data());
191  } else {
192  if (buf.Contains(".setup")) {
193  interface->addSetupFile(buf.Data());
194  } else {
195  if (buf.Contains("SimulRefRunDb:")) {
196  k = buf.Last(' ') + 1;
197  if (k) {
198  simRefRun = buf(k, buf.Length() - k);
199  }
200  } else {
201  if (buf.Contains("HistoryDateDb:")) {
202  k = buf.First(' ') + 1;
203  if (buf.Length() > k) {
204  historyDate = buf(k, buf.Length() - k);
205  historyDate = historyDate.Strip(historyDate.kBoth);
206  }
207  }
208  }
209  }
210  }
211  }
212  }
213  Bool_t rc = kTRUE;
214  FairGeoIo* oraIo = interface->getOraInput();
215  if (oraIo) {
216  if (historyDate.Length() > 0) {
217  rc = oraIo->setHistoryDate(historyDate.Data());
218  }
219  if (rc && simRefRun.Length() > 0) {
220  rc = oraIo->setSimulRefRun(simRefRun.Data());
221  }
222  }
223  return rc;
224 }
225 
227 {
228  // Reads the detector setups, needed for create only subsets
229  if (!isOpen() || writable || interface == 0) {
230  return kFALSE;
231  }
232  const Int_t maxbuf = 256;
233  char buf[maxbuf];
234  TString s, det;
235  FairGeoSet* set = 0;
236  Int_t maxModules = 0, secNo = -1;
237  Int_t* mod = 0;
238  const char d[] = " ";
239  while (!(*file).eof()) {
240  (*file).getline(buf, maxbuf);
241  if (strlen(buf) >= 3 && buf[1] != '/') {
242  if (buf[0] == '[') {
243  set = 0;
244  delete[] mod;
245  mod = 0;
246  s = buf;
247  Ssiz_t n = s.First(']');
248  det = s(1, n - 1);
249  det.ToLower();
250  set = interface->findSet(det);
251  if (!set) {
252  Error("readDetectorSetup", "Detector %s not found", det.Data());
253  delete[] mod;
254  return kFALSE;
255  }
256  maxModules = set->getMaxModules();
257  mod = new Int_t[maxModules];
258  } else {
259  if (set && mod) {
260  char* ss = strtok(buf, d);
261  if (ss && strlen(ss) > 3) {
262  secNo = static_cast<Int_t>(ss[3] - '0') - 1;
263  for (Int_t i = 0; i < maxModules && mod; i++) {
264  ss = strtok(nullptr, d);
265  if (ss) {
266  sscanf(ss, "%i", &mod[i]);
267  }
268  }
269  set->setModules(secNo, mod);
270  }
271  } else {
272  delete[] mod;
273  return kFALSE;
274  }
275  }
276  }
277  }
278  delete[] mod;
279  return kTRUE;
280 }
Int_t getMaxModules(void)
Definition: FairGeoSet.h:78
FairGeoIo * getOraInput()
Bool_t readDetectorSetup(FairGeoInterface *)
virtual Bool_t setHistoryDate(const char *)=0
Bool_t read(FairGeoMedia *)
Bool_t write(FairGeoMedia *)
void setModules(Int_t, Int_t *)
Definition: FairGeoSet.cxx:67
virtual Bool_t read(std::fstream &, FairGeoMedia *)
Definition: FairGeoSet.cxx:116
ClassImp(FairEventBuilder)
virtual void write(std::fstream &)
Definition: FairGeoSet.cxx:432
void read(std::fstream &)
virtual ~FairGeoAsciiIo()
virtual Bool_t setSimulRefRun(const char *)=0
FairGeoSet * findSet(const char *)
void addInputFile(const char *)
Bool_t readGeomConfig(FairGeoInterface *)
void write(std::fstream &)
void addSetupFile(const char *f)
Bool_t open(const char *, const Text_t *status="in")