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