FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairGeoSet.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 // FairGeoSet
13 //
14 // Base class for geometry of detector parts
15 //
17 #include "FairGeoSet.h"
18 
19 #include "FairGeoBasicShape.h" // for FairGeoBasicShape
20 #include "FairGeoBuilder.h" // for FairGeoBuilder
21 #include "FairGeoMedia.h" // for FairGeoMedia
22 #include "FairGeoNode.h" // for FairGeoNode, etc
23 #include "FairGeoShapes.h" // for FairGeoShapes
24 #include "FairGeoTransform.h" // for FairGeoTransform
25 
26 #include <TArrayI.h> // for TArrayI
27 #include <TString.h> // for TString, operator<<
28 #include <ctype.h> // for isalpha
29 #include <fstream> // for fstream
30 #include <iostream> // for cout
31 #include <string.h> // for strcmp
32 
33 class FairGeoMedium;
34 
35 using std::cout;
36 using std::endl;
37 using std::ios;
38 
40 
42  : TNamed()
43  , hadesGeo(0)
44  , volumes(new TList())
45  , masterNodes(nullptr)
46  , maxSectors(0)
47  , maxKeepinVolumes(0)
48  , maxModules(0)
49  , modules(nullptr)
50  , pShapes(nullptr)
51  , geoFile("")
52  , author("")
53  , description("")
54 {
55  // Constructor
56 }
57 
59 {
60  // Destructor
61  delete volumes;
62  volumes = 0;
63  delete modules;
64  modules = 0;
65 }
66 
67 void FairGeoSet::setModules(Int_t s, Int_t* m)
68 {
69  // Stores the modules given in 'm' as active modules in sector 's'
70  // May be called with s=-1 for detectors not belonging to a sector
71  if (!modules) {
72  if (maxSectors == 0 && maxModules > 0) {
73  modules = new TArrayI(maxModules);
74  }
75  if (maxSectors > 0 && maxModules > 0) {
76  modules = new TArrayI(maxSectors * maxModules);
77  }
78  }
79  Int_t sec = 0;
80  if (s > 0) {
81  sec = s;
82  }
83  for (Int_t i = 0; i < maxModules; i++) {
84  if (modules) {
85  modules->AddAt(m[i], (sec * maxModules + i));
86  }
87  }
88 }
89 
91 {
92  // Returns a linear array of all modules
93  if (modules) {
94  return modules->GetArray();
95  }
96  return 0;
97 }
98 
99 Int_t FairGeoSet::getModule(Int_t s, Int_t m)
100 {
101  // Returns 1, if the module is not explicitly set inactive
102  // (in this case it returns 0)
103  // May be called with s=-1 for detectors not belonging to a sector
104  if (!modules) {
105  return 1;
106  }
107  if (m < maxModules && s < 0) {
108  return (*modules)[m];
109  }
110  if (m < maxModules && s < maxSectors) {
111  return (*modules)[(s * maxModules + m)];
112  }
113  return 0;
114 }
115 
116 Bool_t FairGeoSet::read(std::fstream& fin, FairGeoMedia* media)
117 {
118  // Reads the geometry from file
119  Int_t s1 = -1, s2 = 0;
120  if (maxSectors > 0) {
121  s1 = 0;
122  s2 = maxSectors;
123  }
124  Bool_t rc = kTRUE;
125  for (Int_t s = s1; s < s2 && rc; s++) {
126  for (Int_t k = 0; k < maxKeepinVolumes && rc; k++) {
127  TString keepinName = getKeepinName(s, k);
128  rc = readKeepIn(fin, media, keepinName);
129  }
130  }
131  for (Int_t m = 0; m < maxModules && rc; m++) {
132  Bool_t containsActiveModule = kFALSE;
133  for (Int_t s = s1; s < s2; s++) {
134  if (getModule(s, m)) {
135  containsActiveModule = kTRUE;
136  }
137  }
138  TString modName = getModuleName(m);
139  TString eleName = getEleName(m);
140  rc = readModule(fin, media, modName, eleName, containsActiveModule);
141  }
142  return rc;
143 }
144 
145 void FairGeoSet::readInout(std::fstream& fin)
146 {
147  // Reads the inout flag (in old files)
148  char c = ' ';
149  do {
150  fin.get(c);
151  } while (c == ' ' || c == '\n');
152  if (c != '0' && c != '1') {
153  fin.putback(c);
154  } else
155  do {
156  fin.get(c);
157  } while (c != '\n');
158  return;
159 }
160 
161 void FairGeoSet::readTransform(std::fstream& fin, FairGeoTransform& tf)
162 {
163  // Reads the transformation from file
164  Double_t r[9], t[3];
165  fin >> t[0] >> t[1] >> t[2];
166  for (Int_t i = 0; i < 9; i++) {
167  fin >> r[i];
168  }
169  tf.setRotMatrix(r);
170  tf.setTransVector(t);
171 }
172 
173 Bool_t FairGeoSet::readVolumeParams(std::fstream& fin, FairGeoMedia* media, FairGeoNode* volu, TList* refVolumes)
174 {
175  // Reads the volume definition from file
176  // Int_t hadgeo = 1;
177  if (volu == 0 || media == 0 || refVolumes == 0) {
178  return kFALSE;
179  }
180  const Int_t maxbuf = 256;
181  char buf[maxbuf];
182  TString name(volu->GetName());
183  Int_t nameLength = name.Length();
184  Bool_t isCopy = kFALSE;
185  fin >> buf;
186  FairGeoNode* mother = 0;
187  FairGeoCopyNode* refNode = 0;
188  TString refName;
189 
190  if (volu->isKeepin()) {
191  mother = getMasterNode(buf);
192  } else if (volu->isModule()) {
193  mother = getMasterNode(buf);
194  if (!mother) {
195  mother = getVolume(buf);
196  }
197  if (volu->isActive() && mother) {
198  mother->setActive();
199  }
200  } else {
201  mother = getVolume(buf);
202  if (!mother) {
203  mother = getMasterNode(buf);
204  }
205  }
206 
207  volu->setMother(mother);
208  if (!mother) {
209  Warning("readVolumeParams", "Mother volume %s not found!", buf);
210  }
211 
212  if (hadesGeo == 1) {
213  // cout << " read copies in Hades format " << endl;
214 
215  if (nameLength > 4) {
216  char c = ' ';
217  do {
218  fin.get(c);
219  } while (c == ' ' || c == '\n');
220  Int_t i = static_cast<Int_t>(c);
221  fin.putback(c);
222 
223  if (!isalpha(i)) {
224  isCopy = kTRUE;
225  }
226  refName = name(0, 4);
227  refNode = static_cast<FairGeoCopyNode*>(refVolumes->FindObject(refName));
228  if (!refNode) {
229  if (isCopy) {
230  return kFALSE;
231  }
232  refVolumes->Add(new FairGeoCopyNode(refName.Data(), volu));
233  } else {
234  FairGeoNode* cn = refNode->pNode;
235  volu->setCopyNode(cn);
236  volu->setMedium(cn->getMedium());
237  volu->setShape(cn->getShapePointer());
238  Int_t n = cn->getNumPoints();
239  volu->createPoints(n);
240  for (Int_t k = 0; k < n; k++) {
241  volu->setPoint(k, *(cn->getPoint(k)));
242  }
243  }
244  }
245 
246  } else {
247  // cbm format
248  Ssiz_t l = name.Last('#');
249  if (l > 0) {
250  char c;
251  do {
252  fin.get(c);
253  } while (c == ' ' || c == '\n');
254  Int_t i = static_cast<Int_t>(c);
255  fin.putback(c);
256  if (!isalpha(i)) {
257  isCopy = kTRUE;
258  }
259  refName = name(0, l);
260  refNode = static_cast<FairGeoCopyNode*>(refVolumes->FindObject(refName));
261  if (!refNode) {
262  if (isCopy) {
263  return kFALSE;
264  }
265  refVolumes->Add(new FairGeoCopyNode(refName.Data(), volu));
266  } else {
267  FairGeoNode* cn = refNode->pNode;
268  volu->setCopyNode(cn);
269  volu->setMedium(cn->getMedium());
270  volu->setShape(cn->getShapePointer());
271  Int_t n = cn->getNumPoints();
272  volu->createPoints(n);
273  for (Int_t k = 0; k < n; k++) {
274  volu->setPoint(k, *(cn->getPoint(k)));
275  }
276  }
277  }
278  }
279 
280  if (!isCopy) {
281  fin >> buf;
283  if (sh) {
284  volu->setShape(sh);
285  } else {
286  return kFALSE;
287  }
288  fin >> buf;
289  FairGeoMedium* medium = media->getMedium(buf);
290  if (!medium) {
291  Error("readVolumeParams", "Medium %s not found in list of media", buf);
292  return kFALSE;
293  }
294  volu->setMedium(medium);
295  Int_t n = 0;
296  fin.getline(buf, maxbuf); // to read the end of line
297  if (sh) {
298  n = sh->readPoints(&fin, volu);
299  }
300  if (n <= 0) {
301  return kFALSE;
302  }
303  }
304  readTransform(fin, volu->getTransform());
305  return kTRUE;
306 }
307 
308 Bool_t FairGeoSet::readKeepIn(std::fstream& fin, FairGeoMedia* media, TString& name)
309 {
310  // Reads the keepin volume from file
311  fin.clear();
312  fin.seekg(0, ios::beg);
313  FairGeoNode* volu = 0;
314  Bool_t rc = kTRUE;
315  TList refVolumes;
316  const Int_t maxbuf = 256;
317  char buf[maxbuf];
318  while (rc && !volu && !fin.eof()) {
319  fin >> buf;
320  if (buf[0] == '/') {
321  fin.getline(buf, maxbuf);
322  } else if (!fin.eof()) {
323  if (strcmp(buf, name) == 0) {
324  volu = new FairGeoNode;
325  volu->SetName(buf);
327  readInout(fin);
328  rc = readVolumeParams(fin, media, volu, &refVolumes);
329  } else {
330  do {
331  fin.getline(buf, maxbuf);
332  } while (!fin.eof() && buf[0] != '/');
333  }
334  }
335  }
336  if (rc) {
337  volumes->Add(volu);
338  } else {
339  delete volu;
340  volu = 0;
341  }
342  refVolumes.Delete();
343  return rc;
344 }
345 
346 Bool_t FairGeoSet::readModule(std::fstream& fin,
347  FairGeoMedia* media,
348  TString& modName,
349  TString& eleName,
350  Bool_t containsActiveMod)
351 {
352  // Reads the whole geometry of a module from file
353  const Int_t maxbuf = 256;
354  Text_t buf[maxbuf];
355  fin.clear();
356  fin.seekg(0, ios::beg);
357  FairGeoNode* volu = 0;
358  Bool_t rc = kTRUE;
359  TList refVolumes;
360  TString name;
361  while (rc && !fin.eof()) {
362  fin >> buf;
363  if (buf[0] == '/') {
364  fin.getline(buf, maxbuf);
365  } else if (!fin.eof()) {
366  // TString name(buf);
367  name = buf;
368  if (name.BeginsWith(modName)) {
369  volu = new FairGeoNode;
370  volu->SetName(buf);
372  Int_t a = getModule(getSecNumInMod(buf), getModNumInMod(buf));
373  if (a > 0) {
374  volu->setActive(kTRUE);
375  } else {
376  volu->setActive(kFALSE);
377  }
378  readInout(fin);
379  rc = readVolumeParams(fin, media, volu, &refVolumes);
380  if (rc) {
381  volumes->Add(volu);
382  } else {
383  Error("readModule", "Failed for module %s\n", volu->GetName());
384  delete volu;
385  volu = 0;
386  }
387  } else if (!eleName.IsNull() && name.BeginsWith(eleName)) {
388  volu = new FairGeoNode;
389  volu->SetName(buf);
391  volu->setActive(containsActiveMod);
392  rc = readVolumeParams(fin, media, volu, &refVolumes);
393  if (rc) {
394  volumes->Add(volu);
395  } else {
396  Error("readModule", "Failed for %s\n", volu->GetName());
397  delete volu;
398  volu = 0;
399  }
400  } else {
401  do {
402  fin.getline(buf, maxbuf);
403  } while (!fin.eof() && buf[0] != '/');
404  }
405  }
406  }
407  refVolumes.Delete();
408  return rc;
409 }
410 
412 {
413  // Prints the geometry
414  if (!author.IsNull()) {
415  cout << "//Author: " << author << '\n';
416  }
417  if (!description.IsNull()) {
418  cout << "//Description: " << description << '\n';
419  }
420  if (!description.IsNull()) {
421  cout << "//----------------------------------------------------------\n";
422  }
423  std::ios_base::fmtflags tmp = cout.setf(ios::fixed, ios::floatfield);
424  TListIter iter(volumes);
425  FairGeoNode* volu;
426  while ((volu = static_cast<FairGeoNode*>(iter.Next()))) {
427  volu->print();
428  }
429  cout.setf(tmp);
430 }
431 
432 void FairGeoSet::write(std::fstream& fout)
433 {
434  // Writes the volumes to file
435  if (!author.IsNull()) {
436  fout << "//Author: " << author << '\n';
437  }
438  if (!description.IsNull()) {
439  fout << "//Description: " << description << '\n';
440  }
441  fout << "//----------------------------------------------------------\n";
442  std::ios_base::fmtflags tmp = fout.setf(ios::fixed, ios::floatfield);
443  TListIter iter(volumes);
444  FairGeoNode* volu;
445  Bool_t rc = kTRUE;
446  while ((volu = static_cast<FairGeoNode*>(iter.Next())) && rc) {
447  rc = volu->write(fout);
448  }
449  fout.setf(tmp);
450 }
451 
453 {
454  // Creates the geometry
455  if (!builder) {
456  return kFALSE;
457  }
458  TListIter iter(volumes);
459  FairGeoNode* volu;
460  Bool_t rc = kTRUE;
461  while ((volu = static_cast<FairGeoNode*>(iter.Next())) && rc) {
462  if (volu->isActive()) {
463 
464  if (hadesGeo == 1) {
465  rc = builder->createNode(volu, hadesGeo);
466  } else {
467  rc = builder->createNode(volu);
468  }
469  if (rc) {
470  if (volu->isTopNode() || volu->isRefNode()) {
471  FairGeoNode* n = getMasterNode(volu->GetName());
472  if (n) {
474  n->setRootVolume(volu->getRootVolume());
475  }
476  }
477  } else {
478  Error("create", "Creation of %s failed!", volu->GetName());
479  }
480  }
481  }
482  return rc;
483 }
484 
486 {
487  // Compares the geometry parameters and print a diagnose
488  if (fName.CompareTo(rset.GetName()) != 0) {
489  Error("compare", "Sets have different type");
490  return;
491  }
492  TListIter iter(volumes);
493  FairGeoNode* volu;
494  Int_t n = 0, nnf = 0, nDiff = 0;
495  cout << "name\t mother medium shape points pos rot\n";
496  cout << "------------------------------------------------\n";
497  while ((volu = static_cast<FairGeoNode*>(iter.Next()))) {
498  n++;
499  FairGeoNode* rNode = rset.getVolume(volu->GetName());
500  if (!rNode) {
501  nnf++;
502  } else if (volu->compare(*rNode) > 0) {
503  nDiff++;
504  }
505  }
506  cout << "Number of volumes in first list: " << n << '\n';
507  cout << "Number of different volumes: " << nDiff << endl;
508  cout << "Number of volumes not found in second list: " << nnf << '\n';
509  TList* rlist = rset.getListOfVolumes();
510  if (rlist->GetSize() != (n - nnf)) {
511  nnf = 0;
512  TListIter riter(rlist);
513  while ((volu = static_cast<FairGeoNode*>(riter.Next()))) {
514  FairGeoNode* rNode = getVolume(volu->GetName());
515  if (!rNode) {
516  nnf++;
517  }
518  }
519  } else {
520  nnf = 0;
521  }
522  cout << "Number of additional volumes in second list: " << nnf << '\n';
523 }
virtual const char * getEleName(Int_t)
Definition: FairGeoSet.h:93
void readTransform(std::fstream &, FairGeoTransform &)
Definition: FairGeoSet.cxx:161
FairGeoShapes * pShapes
Definition: FairGeoSet.h:59
virtual ~FairGeoSet()
Definition: FairGeoSet.cxx:58
void setTransVector(const FairGeoVector &t)
void setCopyNode(FairGeoNode *p)
Definition: FairGeoNode.h:94
void setActive(Bool_t a=kTRUE)
Definition: FairGeoNode.h:92
void setPoint(const Int_t, const Double_t, const Double_t, const Double_t)
FairGeoMedium * getMedium(const char *)
void compare(FairGeoSet &)
Definition: FairGeoSet.cxx:485
Bool_t isActive()
Definition: FairGeoNode.h:75
virtual Bool_t createNode(FairGeoNode *, Int_t hadFormat=0)=0
FairGeoTransform & getCenterPosition()
Definition: FairGeoNode.h:76
TGeoVolume * getRootVolume()
Definition: FairGeoNode.h:81
void setModules(Int_t, Int_t *)
Definition: FairGeoSet.cxx:67
TString description
Definition: FairGeoSet.h:62
virtual Bool_t create(FairGeoBuilder *)
Definition: FairGeoSet.cxx:452
virtual Bool_t read(std::fstream &, FairGeoMedia *)
Definition: FairGeoSet.cxx:116
ClassImp(FairEventBuilder)
Int_t maxSectors
Definition: FairGeoSet.h:55
virtual void write(std::fstream &)
Definition: FairGeoSet.cxx:432
virtual Int_t getSecNumInMod(const TString &)
Definition: FairGeoSet.h:94
virtual const char * getModuleName(Int_t)
Definition: FairGeoSet.h:92
void setRootVolume(TGeoVolume *p)
Definition: FairGeoNode.h:95
Bool_t isKeepin()
Definition: FairGeoNode.h:69
void setMother(FairGeoNode *s)
Definition: FairGeoNode.h:136
TString author
Definition: FairGeoSet.h:61
void setShape(FairGeoBasicShape *s)
Definition: FairGeoNode.h:127
FairGeoNode * getVolume(const char *name)
Definition: FairGeoSet.h:83
Int_t * getModules(void)
Definition: FairGeoSet.cxx:90
virtual const char * getKeepinName(Int_t, Int_t)
Definition: FairGeoSet.h:91
Bool_t isTopNode()
Definition: FairGeoNode.h:67
void createPoints(const Int_t)
FairGeoBasicShape * selectShape(FairGeoVolume *)
Bool_t readVolumeParams(std::fstream &, FairGeoMedia *, FairGeoNode *, TList *l=0)
Definition: FairGeoSet.cxx:173
Int_t maxModules
Definition: FairGeoSet.h:57
TList * getListOfVolumes()
Definition: FairGeoSet.h:85
Bool_t write(std::fstream &)
void setRotMatrix(const FairGeoRotation &r)
FairGeoVector * getPoint(const Int_t n)
Definition: FairGeoVolume.h:85
Bool_t isRefNode()
Definition: FairGeoNode.h:68
Bool_t readModule(std::fstream &, FairGeoMedia *, TString &, TString &, Bool_t a=kFALSE)
Definition: FairGeoSet.cxx:346
Int_t getNumPoints()
Definition: FairGeoVolume.h:51
void setVolumeType(EFairGeoNodeType t)
Definition: FairGeoNode.h:87
void setMedium(FairGeoMedium *med)
Definition: FairGeoNode.h:91
FairGeoMedium * getMedium()
Definition: FairGeoNode.h:74
virtual Int_t readPoints(std::fstream *, FairGeoVolume *)
Bool_t isModule()
Definition: FairGeoNode.h:70
FairGeoTransform & getTransform()
Definition: FairGeoVolume.h:48
FairGeoNode * getMasterNode(const char *name)
Definition: FairGeoSet.h:84
void readInout(std::fstream &)
Definition: FairGeoSet.cxx:145
virtual Int_t getModNumInMod(const TString &)
Definition: FairGeoSet.h:95
Int_t compare(FairGeoNode &)
void setCenterPosition(const FairGeoTransform &t)
Definition: FairGeoNode.h:93
Int_t maxKeepinVolumes
Definition: FairGeoSet.h:56
FairGeoBasicShape * getShapePointer()
Definition: FairGeoNode.h:72
TList * volumes
Definition: FairGeoSet.h:53
Bool_t readKeepIn(std::fstream &, FairGeoMedia *, TString &)
Definition: FairGeoSet.cxx:308
Int_t hadesGeo
Definition: FairGeoSet.h:33
Int_t getModule(Int_t, Int_t)
Definition: FairGeoSet.cxx:99
virtual void print()
Definition: FairGeoSet.cxx:411
TArrayI * modules
Definition: FairGeoSet.h:58