EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairGeoInterface.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairGeoInterface.cxx
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 10/11/03 by Ilse Koenig
3 
5 // FairGeoInterface
6 //
7 // Manager class for geometry used by simulation
8 
9 // *--15.12.04 M. Al-Turany
10 // 1-addInputFile(const char* name) is not used anymore
11 // 2-addGeoModule(FairGeoSet*pSet) is added
12 // 3-setMediaFile(const char *file) is added
13 // 3-findSet(const char* name) has been changed, no hard coding of name
14 // 4-nSets is now set by a method SetNoOfSets(Int_t) (see header file)
15 
17 
18 #include "FairGeoInterface.h"
19 
20 #include "FairGeoIo.h"
21 #include "FairGeoAsciiIo.h"
22 #include "FairGeoMedia.h"
23 #include "FairGeoShapes.h"
24 #include "FairGeoSet.h"
25 #include "FairGeoBuilder.h"
26 //#include "FairGeoNode.h"
27 
28 #include "TClass.h"
29 #include "TObjArray.h"
30 
31 #include <iostream>
32 //#include <iomanip>
33 //#include <ctime>
34 
35 using std::cout;
36 using std::endl;
37 
39 
41  :TObject(),
42  fileInput(NULL),
43  oraInput(NULL),
44  output(NULL),
45  nSets(0),
46  nActualSets(0),
47  sets(new TObjArray()),
48  media(new FairGeoMedia()),
49  shapes(new FairGeoShapes()),
50  masterNodes(new TList()),
51  setupFile(""),
52  geoBuilder(NULL)
53 {
54  // Constructor
55 }
56 
58 {
59  // Destructor
60  if (fileInput) {
61  delete fileInput;
62  fileInput=0;
63  }
64  if (oraInput) {
65  delete oraInput;
66  oraInput=0;
67  }
68  if (output) {
69  delete output;
70  output=0;
71  }
72 // if (geoBuilder) {
73 // delete geoBuilder;
74 // geoBuilder=0;
75 // }
76  if (masterNodes) {
77  masterNodes->Delete();
78  delete masterNodes;
79  masterNodes=0;
80  }
81  if (sets) {
82  sets->Delete();
83  delete sets;
84  sets=0;
85  }
86  if (media) {
87  delete media;
88  media=0;
89  }
90  if (shapes) {
91  delete shapes;
92  shapes=0;
93  }
94 }
95 
97 {
98  sets->AddLast(pSet);
99  pSet->setShapes(shapes);
101  nActualSets++;
102 
103 }
105 {
106  media->setInputFile(file);
107 }
108 
110 {
111 
112  Error("This Methode is not used any more ","Use addGeoModule to add detectors: %s",file);
113  /*
114  // Adds the input file and creates the corresponding detector object
115  FairGeoSet* pSet=0;
116  TString setName(file);
117  setName.ToLower();
118  Int_t l=setName.Last('/')+1;
119  setName=setName(l,setName.Length()-l);
120  if (setName.Contains("media")) {
121  media->setInputFile(file);
122  } else {
123  if (setName.Contains("cave")) {
124  pSet=(FairGeoSet*)sets->At(kFairGeoCave);
125  if (!pSet) {
126  pSet=new FairGeoCave;
127  sets->AddAt(pSet,kFairGeoCave);
128  }
129  } else if (setName.Contains("pipe")) {
130  pSet=(FairGeoSet*)sets->At(kFairGeoPipe);
131  if (!pSet) {
132  pSet=new FairGeoPipe;
133  sets->AddAt(pSet,kFairGeoPipe);
134  }
135  } else if (setName.Contains("target")) {
136  pSet=(FairGeoSet*)sets->At(kFairGeoTarget);
137  if (!pSet) {
138  pSet=new FairGeoTarget;
139  sets->AddAt(pSet,kFairGeoTarget);
140  }
141  } else if (setName.Contains("magnet")) {
142  pSet=(FairGeoSet*)sets->At(kFairGeoMagnet);
143  if (!pSet) {
144  pSet=new FairGeoMagnet;
145  sets->AddAt(pSet,kFairGeoMagnet);
146  }
147  } else if (setName.Contains("rich")) {
148  pSet=(FairGeoSet*)sets->At(kFairGeoRich);
149  if (!pSet) {
150  pSet=new FairGeoRich;
151  sets->AddAt(pSet,kFairGeoRich);
152  }
153  } else if (setName.Contains("sts")) {
154  pSet=(FairGeoSet*)sets->At(kFairGeoSts);
155  if (!pSet) {
156  pSet=new FairGeoSts;
157  sets->AddAt(pSet,kFairGeoSts);
158  }
159  } else if (setName.Contains("trd")) {
160  pSet=(FairGeoSet*)sets->At(kFairGeoTrd);
161  if (!pSet) {
162  pSet=new FairGeoTrd;
163  sets->AddAt(pSet,kFairGeoTrd);
164  }
165  } else if (setName.Contains("tof")) {
166  pSet=(FairGeoSet*)sets->At(kFairGeoTof);
167  if (!pSet) {
168  pSet=new FairGeoTof;
169  sets->AddAt(pSet,kFairGeoTof);
170  }
171  } else Error("addInputFile","Unknown detector set: %s",file);
172  if (pSet) {
173  pSet->setShapes(shapes);
174  pSet->setMasterNodes(masterNodes);
175  pSet->setGeomFile(file);
176  nActualSets++;
177  }
178  }
179  */
180 }
181 
183 {
184  // Find the detector set in the actual setup
185  FairGeoSet* pSet=0;
186  TString setName(name);
187  TString PSetName;
188  setName.ToLower();
189  Int_t l=setName.Last('/')+1;
190  setName=setName(l,setName.Length()-l);
191  /* if (setName.Contains("cave")) pSet=(FairGeoSet*)sets->At(kFairGeoCave);
192  else if (setName.Contains("pipe")) pSet=(FairGeoSet*)sets->At(kFairGeoPipe);
193  else if (setName.Contains("target")) pSet=(FairGeoSet*)sets->At(kFairGeoTarget);
194  else if (setName.Contains("magnet")) pSet=(FairGeoSet*)sets->At(kFairGeoMagnet);
195  else if (setName.Contains("rich")) pSet=(FairGeoSet*)sets->At(kFairGeoRich);
196  else if (setName.Contains("sts")) pSet=(FairGeoSet*)sets->At(kFairGeoSts);
197  else if (setName.Contains("trd")) pSet=(FairGeoSet*)sets->At(kFairGeoTrd);
198  else if (setName.Contains("tof")) pSet=(FairGeoSet*)sets->At(kFairGeoTof);
199  else if (setName.Contains("ecal")) pSet=(FairGeoSet*)sets->At(kFairGeoEcal);
200  return pSet;
201  */
202  for(Int_t i=0; i<nSets; i++) {
203  PSetName=sets->At(i)->GetName();
204  if (PSetName.Contains(setName.Data())) {
205  pSet=(FairGeoSet*)sets->At(i);
206  cout << "FairGeoInterface::findSet" <<pSet->GetName() <<endl;
207  break;
208  }
209 
210  }
211  return pSet;
212 
213 }
214 
216 {
217  // Reads the geometry for the detector part
218  Bool_t rc=kFALSE;
219  FairGeoIo* input=0;
220  if (pSet) {
221  input=connectInput(pSet->getGeomFile());
222  if (input) { rc=input->read(pSet,media); }
223  }
224  return rc;
225 }
226 
228 {
229  // Writes the geometry for the detector part to output
230  if (pSet&&connectOutput(pSet->GetName())) { return output->write(pSet); }
231  return kFALSE;
232 }
233 
235 {
236  // Writes the geometry for the detector part to file
237  if (pSet&&output) {
238  output->open(filename,"out");
239  if (output->isOpen()) { return output->write(pSet); }
240  }
241  return kFALSE;
242 }
243 
245 {
246  // Deletes the set
247  if (pSet) {
248  sets->Remove(pSet);
249  delete pSet;
250  pSet=0;
251  }
252 }
253 
255 {
256  // Reads the media
257  Bool_t rc=kFALSE;
259  if (input) {
260  rc=input->read(media);
261  }
262  return rc;
263 }
264 
266 {
267  // Writes the media to output
268  if (connectOutput("media")) { return output->write(media); }
269  return kFALSE;
270 }
271 
273 {
274  // Writes the media to file
275  if (output) {
276  output->open(filename,"out");
277  if (output->isOpen()) { return output->write(media); }
278  }
279  return kFALSE;
280 }
281 
283 {
284  // Reads the media and the whole geometry
285  Bool_t rc=kTRUE;
286  if (nActualSets==0&&oraInput) {
287  rc=oraInput->readGeomConfig(this);
288 // if (rc) addInputFile("media_gdb");
289  if (rc) { addInputFile("media_gdb"); }
290  }
291  if(rc) { rc=readSetupFile(); }
292  if(rc) { rc=readMedia(); }
293  FairGeoSet* pSet=0;
294  for(Int_t i=0; i<nSets&&rc; i++) {
295  pSet=(FairGeoSet*)sets->At(i);
296  if (pSet) {
297  cout<<"Read "<<pSet->GetName()<<endl;
298  rc=readSet(pSet);
299  }
300  }
301  return rc;
302 }
303 
305 {
306  // Reads the media and the whole geometry to output
307  Bool_t rc=kTRUE;
308  FairGeoSet* pSet=0;
309  rc=writeMedia();
310  for(Int_t i=0; i<nSets&&rc; i++) {
311  pSet=(FairGeoSet*)sets->At(i);
312  if (pSet) { rc=writeSet(pSet); }
313  }
314  return rc;
315 }
316 
317 Bool_t FairGeoInterface::createAll(Bool_t withCleanup)
318 {
319  // Creates the whole geometry
320  // (if withCleanup==kTRUE, the geometry input is deleted afterwards)
321  Bool_t rc=kTRUE;
322  Int_t n=0;
323  if (geoBuilder) {
324  FairGeoSet* pSet=0;
325  for(Int_t i=0; i<nSets&&rc; i++) {
326  pSet=(FairGeoSet*)sets->At(i);
327  if (pSet) {
328  cout<<"Create "<<pSet->GetName()<<endl;
329  rc=pSet->create(geoBuilder);
330  n++;
331  if (withCleanup) {
332  sets->RemoveAt(i);
333  delete pSet;
334  }
335  }
336  }
337  if (rc&&n>0) { geoBuilder->finalize(); }
338  } else { Error("createAll()","No Builder"); }
339  return rc;
340 }
341 
342 Bool_t FairGeoInterface::createGeometry(Bool_t withCleanup)
343 {
344  // Reads and creates the whole geometry
345  // (if withCleanup==kTRUE, the geometry input is deleted after creation)
346  Bool_t rc=kTRUE;
347  Int_t n=0;
348  if (geoBuilder) {
349  if (nActualSets==0&&oraInput) {
350  rc=oraInput->readGeomConfig(this);
351  if (rc) { addInputFile("media_gdb"); }
352  }
353  if (rc) { rc=readSetupFile(); }
354  if (rc) { rc=readMedia(); }
355  FairGeoSet* pSet=0;
356  for(Int_t i=0; i<nSets&&rc; i++) {
357  pSet=(FairGeoSet*)sets->At(i);
358  if (pSet) {
359  cout<<"Read and create "<<pSet->GetName()<<endl;
360  rc=readSet(pSet);
361  if (rc) {
362  rc=pSet->create(geoBuilder);
363  n++;
364  }
365  if (withCleanup) {
366  sets->RemoveAt(i);
367  delete pSet;
368  }
369  }
370  }
371  if (rc&&n>0) { geoBuilder->finalize(); }
372  } else { Error("createAll()","No Builder"); }
373  return rc;
374 }
375 
377 {
378  // Connects the input (ASCII or Oracle)
379  FairGeoIo* currentIo=0;
380  if (filename) {
381  TString s(filename);
382  s=s.Strip();
383  if (s.EndsWith(".dat")||s.EndsWith(".geo")||s.EndsWith(".setup")) {
384  if (!fileInput) { fileInput=new FairGeoAsciiIo(); }
385  fileInput->open(filename);
386  currentIo=fileInput;
387  } else if (s.EndsWith("_gdb")) { currentIo=oraInput; }
388  }
389  if (currentIo && currentIo->isOpen()) { return currentIo; }
390  return 0;
391 }
392 
394 {
395  // Connects the output (ASCII or Oracle)
396  if (output) {
397  if (strcmp(output->IsA()->GetName(),"FairGeoAsciiIo")==0) {
398  TString fName(name);
399  char buf[80];
400  struct tm* newtime;
401  time_t t;
402  time(&t);
403  newtime=localtime(&t);
404  if (newtime->tm_mday<10) { sprintf(buf,"_0%i",newtime->tm_mday); }
405  else { sprintf(buf,"_%i",newtime->tm_mday); }
406  fName=fName+buf;
407  if (newtime->tm_mon<9) { sprintf(buf,"0%i",newtime->tm_mon+1); }
408  else { sprintf(buf,"%i",newtime->tm_mon+1); }
409  fName=fName+buf;
410  Int_t y=newtime->tm_year-100;
411  if (y<10) { sprintf(buf,"0%i",y); }
412  else { sprintf(buf,"%i",y); }
413  fName=fName+buf;
414  if (newtime->tm_hour<10) { sprintf(buf,"0%i",newtime->tm_hour); }
415  else { sprintf(buf,"%i",newtime->tm_hour); }
416  fName=fName+buf;
417  if (newtime->tm_min<10) { sprintf(buf,"0%i",newtime->tm_min); }
418  else { sprintf(buf,"%i",newtime->tm_min); }
419  fName=fName+buf;
420  if (newtime->tm_sec<10) { sprintf(buf,"0%i",newtime->tm_sec); }
421  else { sprintf(buf,"%i",newtime->tm_sec); }
422  fName=fName+buf+".geo";
423  output->open(fName,"out");
424  cout<<"Output file for "<<name<<": "
425  <<((FairGeoAsciiIo*)output)->getFilename()<<endl;
426  }
427  if (output->isOpen()&&output->isWritable()) { return kTRUE; }
428  } else { Error("connectOutput","No output open"); }
429  return kFALSE;
430 }
431 
433 {
434  // Creates the geometry of detector part
435  if (!geoBuilder) { return kFALSE; }
436  return pSet->create(geoBuilder);
437 }
438 
440 {
441  // Prints the media and the list of detector parts
442  media->list();
443  cout<<"********************************************************************\n";
444  cout<<"List of detector parts:\n";
445  FairGeoSet* pSet=0;
446  for(Int_t i=0; i<nSets; i++) {
447  pSet=(FairGeoSet*)sets->At(i);
448  if (pSet) { cout<<" "<<pSet->GetName()<<":\t"<<pSet->getGeomFile()<<'\n'; }
449  }
450  cout<<"********************************************************************\n";
451 }
452 
453 Bool_t FairGeoInterface::readGeomConfig(const char* configFile)
454 {
455  // Reads the GEANT configuration file
456  Bool_t rc=kFALSE;
457  FairGeoIo* input=0;
458  if (configFile) {
459  input=connectInput(configFile);
460  if (input) { rc=input->readGeomConfig(this); }
461  }
462  return rc;
463 }
464 
466 {
467  // Reads the detector setups, needed for create only subsets
468  Bool_t rc=kTRUE;
469  if (setupFile.Length()>0) {
471  if (input) { rc=input->readDetectorSetup(this); }
472  }
473  return rc;
474 }
475 
476