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