EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairGeoAsciiIo.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairGeoAsciiIo.cxx
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 10/11/2003
3 
5 // FairGeoAsciiIo
6 //
7 // Class for geometry I/O from ASCII file
8 //
10 
11 #include "FairGeoAsciiIo.h"
12 
13 #include "FairGeoSet.h"
14 #include "FairGeoMedia.h"
15 #include "FairGeoIo.h"
16 #include "FairGeoInterface.h"
17 
18 using std::cout;
19 using std::endl;
20 using std::ios;
21 
23 
25  : FairGeoIo(),
26  filename(""),
27  filedir(""),
28  writable(kFALSE),
29  file(NULL)
30 {
31  // Constructor
32 }
33 
35 {
36  // Destructor
37  close();
38  if (file) {
39  delete file;
40  file=0;
41  }
42 }
43 
44 Bool_t FairGeoAsciiIo::open(const char* fname,const Text_t* status)
45 {
46  // Opens the file fname
47  close();
48  if (!file) { file=new std::fstream(); }
49  else { (file->clear()); }
50  if (!filedir.IsNull()) { filename=filedir+"/"+fname; }
51  else { filename=fname; }
52  filename=filename.Strip();
53  if (strcmp(status,"in")==0) {
54  file->open(filename,ios::in);
55  writable=kFALSE;
56  } else {
57  if (strcmp(status,"out")==0) {
58  file->open(filename,ios::in);
59  if (!isOpen()) {
60  file->close();
61  file->clear();
62  file->open(filename,ios::out);
63  writable=kTRUE;
64  } else {
65  file->close();
66  Error("open","Output file %s exists already and will not be recreated.",
67  filename.Data());
68  return kFALSE;
69  }
70  } else { Error("open","Invalid file option!"); }
71  }
72  if (file->rdbuf()->is_open()==0) {
73  Fatal("open","Failed to open file %s",filename.Data());
74  return kFALSE;
75  }
76  return kTRUE;
77 }
78 
80 {
81  // Returns kTRUE, if the file is open
82  if (file && file->rdbuf()->is_open()==1) { return kTRUE; }
83  return kFALSE;
84 }
85 
87 {
88  // Returns kTRUE, if the file is open and writable
89  if (isOpen() && writable) { return kTRUE; }
90  return kFALSE;
91 }
92 
94 {
95  // Closes the file
96  if (isOpen()) {
97  file->close();
98  filename="";
99  }
100 }
101 
103 {
104  // Prints file information
105  if (isOpen()) {
106  if (writable) { cout<<"Open output file: "<<filename<<endl; }
107  else { cout<<"Open input file: "<<filename<<endl; }
108  } else { cout<<"No file open."<<endl; }
109 }
110 
112 {
113  // Reads the media from file
114  if (!isOpen() || writable || media==0) { return kFALSE; }
115  media->read(*file);
116  return kTRUE;
117 }
118 
120 {
121  // Reads the geometry set from file
122  if (!isOpen() || writable || set==0) { return kFALSE; }
123  set->read(*file,media);
124  return kTRUE;
125 }
126 
128 {
129  // Writes the media to file
130  if (!isOpen() || !writable || media==0) { return kFALSE; }
131  media->write(*file);
132  return kTRUE;
133 }
134 
136 {
137  // Writes the geometry set to file
138  if (!isOpen() || !writable || set==0) { return kFALSE; }
139  set->write(*file);
140  return kTRUE;
141 }
142 
144 {
145  // Reads the GEANT configuration file
146  if (!isOpen() || writable || interface==0) { return kFALSE; }
147  TString buf(256);
148  TString simRefRun,historyDate;
149  Int_t k=-1;
150  while(!(*file).eof()) {
151  buf.ReadLine(*file);
152  buf=buf.Strip(buf.kBoth);
153  if (!buf.IsNull() && buf(0,2)!="//" && buf(0,1)!="*") {
154  if (buf.Contains(".geo")||buf.Contains("_gdb")) {
155  interface->addInputFile(buf.Data());
156  } else {
157  if (buf.Contains(".setup")) {
158  interface->addSetupFile(buf.Data());
159  } else {
160  if (buf.Contains("SimulRefRunDb:")) {
161  k=buf.Last(' ')+1;
162  if (k) { simRefRun=buf(k,buf.Length()-k); }
163  } else {
164  if (buf.Contains("HistoryDateDb:")) {
165  k=buf.First(' ')+1;
166  if (buf.Length()>k) {
167  historyDate=buf(k,buf.Length()-k);
168  historyDate=historyDate.Strip(historyDate.kBoth);
169  }
170  }
171  }
172  }
173  }
174  }
175  }
176  Bool_t rc=kTRUE;
177  FairGeoIo* oraIo=interface->getOraInput();
178  if (oraIo) {
179  if (historyDate.Length()>0) { rc=oraIo->setHistoryDate(historyDate.Data()); }
180  if (rc&&simRefRun.Length()>0) { rc=oraIo->setSimulRefRun(simRefRun.Data()); }
181  }
182  return rc;
183 }
184 
186 {
187  // Reads the detector setups, needed for create only subsets
188  if (!isOpen() || writable || interface==0) { return kFALSE; }
189  const Int_t maxbuf=256;
190  char buf[maxbuf];
191  TString s, det;
192  FairGeoSet* set=0;
193  Int_t maxModules=0, secNo=-1;
194  Int_t* mod=0;
195  const char d[]=" ";
196  while(!(*file).eof()) {
197  (*file).getline(buf,maxbuf);
198  if (strlen(buf)>=3 && buf[1]!='/') {
199  if (buf[0]=='[') {
200  set=0;
201  if (mod) {
202  delete [] mod;
203  mod=0;
204  }
205  s=buf;
206  Ssiz_t n=s.First(']');
207  det=s(1,n-1);
208  det.ToLower();
209  set=interface->findSet(det);
210  if (!set) {
211  Error("readDetectorSetup","Detector %s not found",det.Data());
212  if (mod) { delete [] mod; }
213  return kFALSE;
214  }
215  maxModules=set->getMaxModules();
216  mod=new Int_t[maxModules];
217  } else {
218  if (set&&mod) {
219  char* ss=strtok(buf,d);
220  if (ss&&strlen(ss)>3) {
221  secNo=(Int_t)(ss[3]-'0')-1;
222  for(Int_t i=0; i<maxModules&&mod; i++) {
223  ss=strtok(NULL,d);
224  if (ss) { sscanf(ss,"%i",&mod[i]); }
225  }
226  set->setModules(secNo,mod);
227  }
228  } else {
229  if (mod) { delete [] mod; }
230  return kFALSE;
231  }
232  }
233  }
234  }
235  if (mod) { delete [] mod; }
236  return kTRUE;
237 }