Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4tgbGeometryDumper.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4tgbGeometryDumper.cc 69803 2013-05-15 15:24:50Z gcosmo $
28 // GEANT4 tag $Name: $
29 //
30 //
31 // class G4tgbGeometryDumper
32 
33 // History:
34 // - Created. P.Arce, CIEMAT (November 2007)
35 // -------------------------------------------------------------------------
36 
37 #include "G4tgbGeometryDumper.hh"
38 
39 #include "G4tgrMessenger.hh"
40 
41 #include "G4SystemOfUnits.hh"
42 #include "G4UIcommand.hh"
43 #include "G4Material.hh"
44 #include "G4Element.hh"
45 #include "G4VSolid.hh"
46 #include "G4Box.hh"
47 #include "G4Tubs.hh"
48 #include "G4Cons.hh"
49 #include "G4Trap.hh"
50 #include "G4Sphere.hh"
51 #include "G4Orb.hh"
52 #include "G4Trd.hh"
53 #include "G4Para.hh"
54 #include "G4Torus.hh"
55 #include "G4Hype.hh"
56 #include "G4Polycone.hh"
57 #include "G4Polyhedra.hh"
58 #include "G4EllipticalTube.hh"
59 #include "G4Ellipsoid.hh"
60 #include "G4EllipticalCone.hh"
61 #include "G4Hype.hh"
62 #include "G4Tet.hh"
63 #include "G4TwistedBox.hh"
64 #include "G4TwistedTrap.hh"
65 #include "G4TwistedTrd.hh"
66 #include "G4TwistedTubs.hh"
67 #include "G4PVPlacement.hh"
68 #include "G4PVParameterised.hh"
69 #include "G4PVReplica.hh"
70 #include "G4BooleanSolid.hh"
71 #include "G4ReflectionFactory.hh"
72 #include "G4ReflectedSolid.hh"
73 #include "G4LogicalVolumeStore.hh"
74 #include "G4PhysicalVolumeStore.hh"
75 #include "G4GeometryTolerance.hh"
76 #include "G4VPVParameterisation.hh"
77 #include <iomanip>
78 
79 //------------------------------------------------------------------------
80 G4tgbGeometryDumper* G4tgbGeometryDumper::theInstance = 0;
81 
82 //------------------------------------------------------------------------
83 G4tgbGeometryDumper::G4tgbGeometryDumper()
84  : theFile(0), theRotationNumber(0)
85 {
86 }
87 
88 //------------------------------------------------------------------------
90 {
91  if( theInstance == 0 ){
92  theInstance = new G4tgbGeometryDumper;
93  }
94 
95  return theInstance;
96 
97 }
98 
99 //------------------------------------------------------------------------
101 {
102  theFile = new std::ofstream(fname);
103 
105  DumpPhysVol( pv ); // dump volume and recursively it will dump all hierarchy
106 }
107 
108 //---------------------------------------------------------------------
110 {
112  G4PhysicalVolumeStore::const_iterator ite;
113  G4VPhysicalVolume* pv = *(pvstore->begin());
114  for( ;; )
115  {
116  G4LogicalVolume* lv = pv->GetMotherLogical();
117  if( lv == 0 ) { break; }
118 
119  //----- look for one PV of this LV
120  for( ite = pvstore->begin(); ite != pvstore->end(); ite++ )
121  {
122  pv = (*ite);
123  if( pv->GetLogicalVolume() == lv )
124  {
125  break;
126  }
127  }
128  }
129 
130  return pv;
131 }
132 
133 
134 //------------------------------------------------------------------------
136 {
137 }
138 
139 //------------------------------------------------------------------------
141 {
142 
143  //--- Dump logical volume first
144  G4LogicalVolume* lv = pv->GetLogicalVolume();
145 
147 
148  //--- It is not needed to dump _refl volumes created when parent is reflected
149  // !!WARNING : it must be avoided to reflect a volume hierarchy if children
150  // has also been reflected, as both will have same name
151 
152  if( reffact->IsReflected( lv )
153  && reffact->IsReflected( pv->GetMotherLogical() ) ) { return; }
154 
155 
156  G4bool bVolExists = CheckIfLogVolExists( lv->GetName(), lv );
157 
158  //---- Construct this PV
159  if( pv->GetMotherLogical() != 0 ) // not WORLD volume
160  {
161  if( !pv->IsReplicated() )
162  {
163  G4String lvName = lv->GetName();
164  if( !bVolExists )
165  {
166  lvName = DumpLogVol( lv );
167  }
168  DumpPVPlacement( pv, lvName );
169  }
170  else if( pv->IsParameterised() )
171  {
172  G4PVParameterised* pvparam = (G4PVParameterised*)(pv);
173  DumpPVParameterised( pvparam );
174  }
175  else
176  {
177  G4String lvName = lv->GetName();
178  if( !bVolExists )
179  {
180  lvName = DumpLogVol( lv );
181  }
182  G4PVReplica* pvrepl = (G4PVReplica*)(pv);
183  DumpPVReplica( pvrepl, lvName );
184  }
185 
186  }
187  else
188  {
189  DumpLogVol( lv );
190  }
191 
192  if( !bVolExists )
193  {
194  //---- Construct PV's who has this LV as mother
195  std::vector<G4VPhysicalVolume*> pvChildren = GetPVChildren( lv );
196  std::vector<G4VPhysicalVolume*>::const_iterator ite;
197  for( ite = pvChildren.begin(); ite != pvChildren.end(); ite++ )
198  {
199  DumpPhysVol( *ite );
200  }
201  }
202 }
203 
204 //------------------------------------------------------------------------
205 void
207  const G4String& lvName, G4int copyNo )
208 {
209  G4String pvName = pv->GetName();
210 
211  G4RotationMatrix* rotMat = pv->GetRotation();
212  if( !rotMat ) rotMat = new G4RotationMatrix();
213 
214  //---- Check if it is reflected
216  G4LogicalVolume* lv = pv->GetLogicalVolume();
217  if( reffact->IsReflected( lv ) )
218  {
219 #ifdef G4VERBOSE
221  {
222  G4cout << " G4tgbGeometryDumper::DumpPVPlacement() - Reflected volume: "
223  << pv->GetName() << G4endl;
224  }
225 #endif
226  G4ThreeVector colx = rotMat->colX();
227  G4ThreeVector coly = rotMat->colY();
228  G4ThreeVector colz = rotMat->colZ();
229  // apply a Z reflection (reflection matrix is decomposed in new
230  // reflection-free rotation + z-reflection)
231  colz *= -1.;
232  G4Rep3x3 rottemp(colx.x(),coly.x(),colz.x(),
233  colx.y(),coly.y(),colz.y(),
234  colx.z(),coly.z(),colz.z());
235  // matrix representation (inverted)
236  *rotMat = G4RotationMatrix(rottemp);
237  *rotMat = (*rotMat).inverse();
238  pvName += "_refl";
239  }
240  G4String rotName = DumpRotationMatrix( rotMat );
241  G4ThreeVector pos = pv->GetTranslation();
242 
243  if( copyNo == -999 ) //for parameterisations copy number is provided
244  {
245  copyNo = pv->GetCopyNo();
246  }
247 
248  G4String fullname = pvName
249  +"#"+G4UIcommand::ConvertToString(copyNo)
250  +"/"+pv->GetMotherLogical()->GetName();
251 
252  if( !CheckIfPhysVolExists(fullname, pv ))
253  {
254  (*theFile)
255  << ":PLACE "
256  << SubstituteRefl(AddQuotes(lvName))
257  << " " << copyNo << " "
258  << SubstituteRefl(AddQuotes(pv->GetMotherLogical()->GetName()))
259  << " " << AddQuotes(rotName) << " "
260  << pos.x() << " " << pos.y() << " " << pos.z() << G4endl;
261 
262  thePhysVols[fullname] = pv;
263  }
264 }
265 
266 
267 //------------------------------------------------------------------------
269 {
270  G4String pvName = pv->GetName();
271 
272  EAxis axis;
273  G4int nReplicas;
274  G4double width;
275  G4double offset;
276  G4bool consuming;
277  pv->GetReplicationData(axis, nReplicas, width, offset, consuming);
278 
280 
281  G4LogicalVolume* lv = pv->GetLogicalVolume();
282  G4VSolid* solid1st = param->ComputeSolid(0, pv);
283  G4Material* mate1st = param->ComputeMaterial(0, pv );
284  std::vector<G4double> params1st = GetSolidParams( solid1st );
285  std::vector<G4double> newParams;
286  G4VSolid* newSolid = solid1st;
287  G4String lvName;
288 
289  for( G4int ii = 0; ii < nReplicas; ii++ )
290  {
291  G4Material* newMate = param->ComputeMaterial(ii, pv );
292  if( solid1st->GetEntityType() == "G4Box")
293  {
294  G4Box* box = (G4Box*)(solid1st);
295  param->ComputeDimensions(*box, ii, pv );
296  newParams = GetSolidParams( box );
297  newSolid = (G4VSolid*)box;
298  }
299  else if( solid1st->GetEntityType() == "G4Tubs")
300  {
301  G4Tubs* tubs = (G4Tubs*)(solid1st);
302  param->ComputeDimensions(*tubs, ii, pv );
303  newParams = GetSolidParams( tubs );
304  newSolid = (G4VSolid*)tubs;
305  }
306  else if( solid1st->GetEntityType() == "G4Trd")
307  {
308  G4Trd* trd = (G4Trd*)(solid1st);
309  param->ComputeDimensions(*trd, ii, pv );
310  newParams = GetSolidParams( trd );
311  newSolid = (G4VSolid*)trd;
312  }
313  else if( solid1st->GetEntityType() == "G4Trap")
314  {
315  G4Trap* trap = (G4Trap*)(solid1st);
316  param->ComputeDimensions(*trap, ii, pv );
317  newParams = GetSolidParams( trap );
318  newSolid = (G4VSolid*)trap;
319  }
320  else if( solid1st->GetEntityType() == "G4Cons")
321  {
322  G4Cons* cons = (G4Cons*)(solid1st);
323  param->ComputeDimensions(*cons, ii, pv );
324  newParams = GetSolidParams( cons );
325  newSolid = (G4VSolid*)cons;
326  }
327  else if( solid1st->GetEntityType() == "G4Sphere")
328  {
329  G4Sphere* sphere = (G4Sphere*)(solid1st);
330  param->ComputeDimensions(*sphere, ii, pv );
331  newParams = GetSolidParams( sphere );
332  newSolid = (G4VSolid*)sphere;
333  }
334  else if( solid1st->GetEntityType() == "G4Orb")
335  {
336  G4Orb* orb = (G4Orb*)(solid1st);
337  param->ComputeDimensions(*orb, ii, pv );
338  newParams = GetSolidParams( orb );
339  newSolid = (G4VSolid*)orb;
340  }
341  else if( solid1st->GetEntityType() == "G4Torus")
342  {
343  G4Torus* torus = (G4Torus*)(solid1st);
344  param->ComputeDimensions(*torus, ii, pv );
345  newParams = GetSolidParams( torus );
346  newSolid = (G4VSolid*)torus;
347  }
348  else if( solid1st->GetEntityType() == "G4Para")
349  {
350  G4Para* para = (G4Para*)(solid1st);
351  param->ComputeDimensions(*para, ii, pv );
352  newParams = GetSolidParams( para );
353  newSolid = (G4VSolid*)para;
354  }
355  else if( solid1st->GetEntityType() == "G4Polycone")
356  {
357  G4Polycone* polycone = (G4Polycone*)(solid1st);
358  param->ComputeDimensions(*polycone, ii, pv );
359  newParams = GetSolidParams( polycone );
360  newSolid = (G4VSolid*)polycone;
361  }
362  else if( solid1st->GetEntityType() == "G4Polyhedra")
363  {
364  G4Polyhedra* polyhedra = (G4Polyhedra*)(solid1st);
365  param->ComputeDimensions(*polyhedra, ii, pv );
366  newParams = GetSolidParams( polyhedra );
367  newSolid = (G4VSolid*)polyhedra;
368  }
369  else if( solid1st->GetEntityType() == "G4Hype")
370  {
371  G4Hype* hype = (G4Hype*)(solid1st);
372  param->ComputeDimensions(*hype, ii, pv );
373  newParams = GetSolidParams( hype );
374  newSolid = (G4VSolid*)hype;
375  }
376  if( ii == 0 || mate1st != newMate || params1st[0] != newParams[0] )
377  {
378  G4String extraName = "";
379  if( ii != 0 )
380  {
381  extraName= "#"+G4UIcommand::ConvertToString(ii)
382  +"/"+pv->GetMotherLogical()->GetName();
383  }
384  lvName = DumpLogVol( lv, extraName, newSolid, newMate );
385  }
386 
387  param->ComputeTransformation(ii, pv);
388  DumpPVPlacement( pv, lvName, ii );
389  }
390 }
391 
392 
393 //------------------------------------------------------------------------
395  const G4String& lvName )
396 {
397  EAxis axis;
398  G4int nReplicas;
399  G4double width;
400  G4double offset;
401  G4bool consuming;
402  pv->GetReplicationData(axis, nReplicas, width, offset, consuming);
403  G4String axisName;
404  switch (axis )
405  {
406  case kXAxis:
407  axisName = "X";
408  break;
409  case kYAxis:
410  axisName = "Y";
411  break;
412  case kZAxis:
413  axisName = "Z";
414  break;
415  case kRho:
416  axisName = "R";
417  break;
418  case kPhi:
419  axisName = "PHI";
420  break;
421  case kRadial3D:
422  case kUndefined:
423  G4String ErrMessage = "Unknown axis of replication for volume"
424  + pv->GetName();
425  G4Exception("G4tgbGeometryDumper::DumpPVReplica",
426  "Wrong axis ", FatalException, ErrMessage);
427  break;
428  }
429 
430  G4String fullname = lvName
431  +"/"+pv->GetMotherLogical()->GetName();
432 
433  if( !CheckIfPhysVolExists(fullname, pv ))
434  {
435  (*theFile)
436  << ":REPL "
437  << SubstituteRefl(AddQuotes(lvName))
438  << " " << SubstituteRefl(AddQuotes(pv->GetMotherLogical()->GetName()))
439  << " " << axisName
440  << " " << nReplicas;
441  if( axis != kPhi )
442  {
443  (*theFile)
444  << " " << width
445  << " " << offset << G4endl;
446  }
447  else
448  {
449  (*theFile)
450  << " " << width/deg << "*deg"
451  << " " << offset/deg << "*deg" << G4endl;
452  }
453 
454  thePhysVols[fullname] = pv;
455  }
456 }
457 
458 
459 //------------------------------------------------------------------------
460 G4String
462  G4VSolid* solid, G4Material* mate )
463 {
464  G4String lvName;
465 
466  if( extraName == "" ) //--- take out the '_refl' in the name
467  {
468  lvName = GetObjectName(lv,theLogVols);
469  }
470  else
471  {
472  lvName = lv->GetName()+extraName;
473  }
474 
475  if( theLogVols.find( lvName ) != theLogVols.end() ) // alredy dumped
476  {
477  return lvName;
478  }
479 
480  if( !solid ) { solid = lv->GetSolid(); }
481 
482  //---- Dump solid
483  G4String solidName = DumpSolid( solid, extraName );
484 
485  //---- Dump material
486  if( !mate ) { mate = lv->GetMaterial(); }
487  G4String mateName = DumpMaterial( mate );
488 
489  //---- Dump logical volume (solid + material)
490  (*theFile) << ":VOLU " << SubstituteRefl(AddQuotes(lvName)) << " "
491  << SupressRefl(AddQuotes(solidName))
492  << " " << AddQuotes(mateName) << G4endl;
493 
494  theLogVols[lvName] = lv;
495 
496  return lvName;
497 }
498 
499 
500 //------------------------------------------------------------------------
502 {
503  G4String mateName = GetObjectName(mat,theMaterials);
504  if( theMaterials.find( mateName ) != theMaterials.end() ) // alredy dumped
505  {
506  return mateName;
507  }
508 
509  size_t numElements = mat->GetNumberOfElements();
510  G4double density = mat->GetDensity()/g*cm3;
511 
512 
513  // start tag
514  //
515  if (numElements == 1)
516  {
517  (*theFile) << ":MATE " << AddQuotes(mateName) << " "
518  << mat->GetZ() << " " << mat->GetA()/(g/mole) << " "
519  << density << G4endl;
520  }
521  else
522  {
523  const G4ElementVector* elems = mat->GetElementVector();
524  const G4double* fractions = mat->GetFractionVector();
525  for (size_t ii = 0; ii < numElements; ii++)
526  {
527  DumpElement( (*elems)[ii] );
528  }
529 
530  (*theFile) << ":MIXT "<< AddQuotes(mateName) << " "
531  << density << " " << numElements << G4endl;
532  // close start element tag and get ready to do composit "parts"
533  for (size_t ii = 0; ii < numElements; ii++)
534  {
535  (*theFile) << " "
536  << AddQuotes(GetObjectName((*elems)[ii],theElements)) << " "
537  << fractions[ii] << G4endl;
538  }
539 
540  }
541 
542  (*theFile) << ":MATE_MEE " << AddQuotes(mateName) << " "
544  << "*eV" << G4endl;
545 
546  (*theFile) << ":MATE_TEMPERATURE " << AddQuotes(mateName) << " "
547  << mat->GetTemperature()/kelvin << "*kelvin" << G4endl;
548 
549  (*theFile) << ":MATE_PRESSURE " << AddQuotes(mateName) << " "
550  << mat->GetPressure()/atmosphere << "*atmosphere" << G4endl;
551 
552  G4State state = mat->GetState();
553  G4String stateStr;
554  switch (state) {
555  case kStateUndefined:
556  stateStr = "Undefined";
557  break;
558  case kStateSolid:
559  stateStr = "Solid";
560  break;
561  case kStateLiquid:
562  stateStr = "Liquid";
563  break;
564  case kStateGas:
565  stateStr = "Gas";
566  break;
567  }
568 
569  (*theFile) << ":MATE_STATE " << AddQuotes(mateName) << " "
570  << stateStr << G4endl;
571 
572  theMaterials[mateName] = mat;
573 
574  return mateName;
575 }
576 
577 
578 //------------------------------------------------------------------------
580 {
581  G4String elemName = GetObjectName(ele,theElements);
582 
583  if( theElements.find( elemName ) != theElements.end() ) // alredy dumped
584  {
585  return;
586  }
587 
588  //--- Add symbol name: Material mixtures store the components as elements
589  // (even if the input are materials), but without symbol
590  //
591  G4String symbol = ele->GetSymbol();
592  if( symbol == "" || symbol == " " )
593  {
594  symbol = elemName;
595  }
596 
597  if( ele->GetNumberOfIsotopes() == 0 )
598  {
599  (*theFile) << ":ELEM " << AddQuotes(elemName) << " "
600  << AddQuotes(symbol) << " " << ele->GetZ() << " "
601  << ele->GetA()/(g/mole) << " " << G4endl;
602  }
603  else
604  {
605  const G4IsotopeVector* isots = ele->GetIsotopeVector();
606  for (size_t ii = 0; ii < ele->GetNumberOfIsotopes(); ii++)
607  {
608  DumpIsotope( (*isots)[ii] );
609  }
610 
611  (*theFile) << ":ELEM_FROM_ISOT " << AddQuotes(elemName) << " "
612  << AddQuotes(symbol) << " " << ele->GetNumberOfIsotopes()
613  << G4endl;
614  const G4double* fractions = ele->GetRelativeAbundanceVector();
615  for (size_t ii = 0; ii < ele->GetNumberOfIsotopes(); ii++)
616  {
617  (*theFile) << " "
618  << AddQuotes(GetObjectName((*isots)[ii],theIsotopes)) << " "
619  << fractions[ii] << G4endl;
620  }
621  }
622  theElements[elemName] = ele;
623 }
624 
625 
626 //------------------------------------------------------------------------
628 {
629  G4String isotName = GetObjectName(isot,theIsotopes);
630  if( theIsotopes.find( isotName ) != theIsotopes.end() ) // alredy dumped
631  {
632  return;
633  }
634 
635  (*theFile) << ":ISOT " << AddQuotes(isotName) << " "
636  << isot->GetZ() << " " << isot->GetN() << " "
637  << isot->GetA()/(g/mole) << " " << G4endl;
638 
639  theIsotopes[isotName] = isot;
640 }
641 
642 
643 //------------------------------------------------------------------------
645  const G4String& extraName )
646 {
647  G4String solidName;
648  if( extraName == "" )
649  {
650  solidName = GetObjectName(solid,theSolids);
651  }
652  else
653  {
654  solidName = solid->GetName()+extraName;
655  }
656 
657  if( theSolids.find( solidName ) != theSolids.end() ) // alredy dumped
658  {
659  return solidName;
660  }
661 
662  G4String solidType = solid->GetEntityType();
663  solidType = GetTGSolidType( solidType );
664 
665  if (solidType == "UNIONSOLID")
666  {
667  DumpBooleanVolume( "UNION", solid );
668 
669  } else if (solidType == "SUBTRACTIONSOLID") {
670  DumpBooleanVolume( "SUBTRACTION", solid );
671 
672  } else if (solidType == "INTERSECTIONSOLID") {
673  DumpBooleanVolume( "INTERSECTION", solid );
674 
675  } else if (solidType == "REFLECTEDSOLID") {
676  G4ReflectedSolid* solidrefl = dynamic_cast<G4ReflectedSolid*>(solid);
677  if (!solidrefl)
678  {
679  G4Exception("G4tgbGeometryDumper::DumpSolid()",
680  "InvalidType", FatalException, "Invalid reflected solid!");
681  return solidName;
682  }
683  G4VSolid* solidori = solidrefl->GetConstituentMovedSolid();
684  DumpSolid( solidori );
685  }
686  else
687  {
688  (*theFile) << ":SOLID " << AddQuotes(solidName) << " ";
689  (*theFile) << AddQuotes(solidType) << " ";
690  DumpSolidParams( solid );
691  theSolids[solidName] = solid;
692  }
693 
694  return solidName;
695 }
696 
697 
698 //------------------------------------------------------------------------
700  G4VSolid* so )
701 {
702  G4BooleanSolid * bso = dynamic_cast < G4BooleanSolid * > (so);
703  if (!bso) { return; }
704  G4VSolid* solid0 = bso->GetConstituentSolid( 0 );
705  G4VSolid* solid1 = bso->GetConstituentSolid( 1 );
706  G4DisplacedSolid* solid1Disp = 0;
707  G4bool displaced = dynamic_cast<G4DisplacedSolid*>(solid1);
708  if( displaced )
709  {
710  solid1Disp = dynamic_cast<G4DisplacedSolid*>(solid1);
711  if (solid1Disp) { solid1 = solid1Disp->GetConstituentMovedSolid(); }
712  }
713  DumpSolid( solid0 );
714  DumpSolid( solid1 );
715 
716  G4String rotName;
717  G4ThreeVector pos;
718  if( displaced )
719  {
720  pos = solid1Disp->GetObjectTranslation(); // translation is of mother frame
721  rotName = DumpRotationMatrix( new G4RotationMatrix( (solid1Disp->
722  GetTransform().NetRotation()).inverse() ) );
723  }
724  else // no displacement
725  {
726  rotName = DumpRotationMatrix( new G4RotationMatrix );
727  pos = G4ThreeVector();
728  }
729 
730  G4String bsoName = GetObjectName(so,theSolids);
731  if( theSolids.find( bsoName ) != theSolids.end() ) return; // alredy dumped
732  G4String solid0Name = FindSolidName( solid0 );
733  G4String solid1Name = FindSolidName( solid1 );
734 
735  (*theFile) << ":SOLID "
736  << AddQuotes(bsoName) << " "
737  << AddQuotes(solidType) << " "
738  << AddQuotes(solid0Name) << " "
739  << AddQuotes(solid1Name) << " "
740  << AddQuotes(rotName) << " "
741  << approxTo0(pos.x()) << " "
742  << approxTo0(pos.y()) << " "
743  << approxTo0(pos.z()) << " " << G4endl;
744 
745  theSolids[bsoName] = bso;
746 }
747 
748 
749 //------------------------------------------------------------------------
751 {
752  std::vector<G4double> params = GetSolidParams( so );
753  for( size_t ii = 0 ; ii < params.size(); ii++ )
754  {
755  (*theFile) << params[ii] << " " ;
756  }
757  (*theFile) << G4endl;
758 }
759 
760 
761 //------------------------------------------------------------------------
762 std::vector<G4double> G4tgbGeometryDumper::GetSolidParams( const G4VSolid * so)
763 {
764  std::vector<G4double> params;
765 
766  G4String solidType = so->GetEntityType();
767  solidType = GetTGSolidType( solidType );
768 
769  if (solidType == "BOX") {
770  const G4Box * sb = dynamic_cast < const G4Box*>(so);
771  if (sb) {
772  params.push_back( sb->GetXHalfLength() );
773  params.push_back( sb->GetYHalfLength() );
774  params.push_back( sb->GetZHalfLength() );
775  }
776  } else if (solidType == "TUBS") {
777  const G4Tubs * tu = dynamic_cast < const G4Tubs * > (so);
778  if (tu) {
779  params.push_back( tu->GetInnerRadius() );
780  params.push_back( tu->GetOuterRadius() );
781  params.push_back( tu->GetZHalfLength() );
782  params.push_back( tu->GetStartPhiAngle()/deg );
783  params.push_back( tu->GetDeltaPhiAngle()/deg );
784  }
785  } else if (solidType == "TRAP") {
786  const G4Trap * trp = dynamic_cast < const G4Trap * > (so);
787  if (trp) {
788  G4ThreeVector symAxis(trp->GetSymAxis());
789  G4double theta = symAxis.theta()/deg;
790  G4double phi = symAxis.phi()/deg;
791  params.push_back( trp->GetZHalfLength() );
792  params.push_back( theta );
793  params.push_back( phi);
794  params.push_back( trp->GetYHalfLength1() );
795  params.push_back( trp->GetXHalfLength1() );
796  params.push_back( trp->GetXHalfLength2() );
797  params.push_back( std::atan(trp->GetTanAlpha1())/deg );
798  params.push_back( trp->GetYHalfLength2() );
799  params.push_back( trp->GetXHalfLength3() );
800  params.push_back( trp->GetXHalfLength4() );
801  params.push_back( std::atan(trp->GetTanAlpha2())/deg );
802  }
803  } else if (solidType == "TRD") {
804  const G4Trd * tr = dynamic_cast < const G4Trd * > (so);
805  if (tr) {
806  params.push_back( tr->GetXHalfLength1() );
807  params.push_back( tr->GetXHalfLength2() );
808  params.push_back( tr->GetYHalfLength1() );
809  params.push_back( tr->GetYHalfLength2() );
810  params.push_back( tr->GetZHalfLength());
811  }
812  } else if (solidType == "PARA") {
813  const G4Para * para = dynamic_cast < const G4Para * > (so);
814  if (para) {
815  G4double phi = 0.;
816  if(para->GetSymAxis().z()!=1.0)
817  { phi = std::atan(para->GetSymAxis().y()/para->GetSymAxis().x()); }
818  params.push_back( para->GetXHalfLength());
819  params.push_back( para->GetYHalfLength());
820  params.push_back( para->GetZHalfLength());
821  params.push_back( std::atan(para->GetTanAlpha())/deg);
822  params.push_back( std::acos(para->GetSymAxis().z())/deg);
823  params.push_back( phi/deg);
824  }
825  } else if (solidType == "CONS") {
826  const G4Cons * cn = dynamic_cast < const G4Cons * > (so);
827  if (cn) {
828  params.push_back( cn->GetInnerRadiusMinusZ() );
829  params.push_back( cn->GetOuterRadiusMinusZ() );
830  params.push_back( cn->GetInnerRadiusPlusZ() );
831  params.push_back( cn->GetOuterRadiusPlusZ() );
832  params.push_back( cn->GetZHalfLength() );
833  params.push_back( cn->GetStartPhiAngle()/deg );
834  params.push_back( cn->GetDeltaPhiAngle()/deg );
835  }
836  } else if (solidType == "SPHERE") {
837  const G4Sphere * sphere = dynamic_cast < const G4Sphere * > (so);
838  if (sphere) {
839  params.push_back( sphere->GetInnerRadius());
840  params.push_back( sphere->GetOuterRadius());
841  params.push_back( sphere->GetStartPhiAngle()/deg);
842  params.push_back( sphere->GetDeltaPhiAngle()/deg);
843  params.push_back( sphere->GetStartThetaAngle()/deg);
844  params.push_back( sphere->GetDeltaThetaAngle()/deg);
845  }
846  } else if (solidType == "ORB") {
847  const G4Orb * orb = dynamic_cast < const G4Orb * > (so);
848  if (orb) {
849  params.push_back( orb->GetRadius());
850  }
851  } else if (solidType == "TORUS") {
852  const G4Torus * torus = dynamic_cast < const G4Torus * > (so);
853  if (torus) {
854  params.push_back( torus->GetRmin());
855  params.push_back( torus->GetRmax());
856  params.push_back( torus->GetRtor());
857  params.push_back( torus->GetSPhi()/deg);
858  params.push_back( torus->GetDPhi()/deg);
859  }
860  } else if (solidType == "POLYCONE") {
861  //--- Dump RZ corners, as original parameters will not be present
862  // if it was build from RZ corners
863  const G4Polycone * plc = dynamic_cast < const G4Polycone * > (so);
864  if (plc) {
865  G4double angphi = plc->GetStartPhi()/deg;
866  if( angphi > 180*deg ) { angphi -= 360*deg; }
867  G4int ncor = plc->GetNumRZCorner();
868  params.push_back( angphi );
869  params.push_back( plc->GetOriginalParameters()->Opening_angle/deg );
870  params.push_back( ncor );
871 
872  for( G4int ii = 0; ii < ncor; ii++ )
873  {
874  params.push_back( plc->GetCorner(ii).r );
875  params.push_back( plc->GetCorner(ii).z );
876  }
877  }
878  } else if (solidType == "POLYHEDRA") {
879  //--- Dump RZ corners, as original parameters will not be present
880  // if it was build from RZ corners
881  const G4Polyhedra * ph = (dynamic_cast < const G4Polyhedra * > (so));
882  if (ph) {
883  G4double angphi = ph->GetStartPhi()/deg;
884  if( angphi > 180*deg ) angphi -= 360*deg;
885 
886  G4int ncor = ph->GetNumRZCorner();
887 
888  params.push_back( angphi );
889  params.push_back( ph->GetOriginalParameters()->Opening_angle/deg );
890  params.push_back( ph->GetNumSide() );
891  params.push_back( ncor );
892 
893  for( G4int ii = 0; ii < ncor; ii++ )
894  {
895  params.push_back( ph->GetCorner(ii).r );
896  params.push_back( ph->GetCorner(ii).z );
897  }
898  }
899  } else if (solidType == "ELLIPTICALTUBE") {
900  const G4EllipticalTube * eltu =
901  dynamic_cast < const G4EllipticalTube * > (so);
902  if (eltu) {
903  params.push_back( eltu->GetDx());
904  params.push_back( eltu->GetDy());
905  params.push_back( eltu->GetDz());
906  }
907  } else if (solidType == "ELLIPSOID" ){
908  const G4Ellipsoid* dso = dynamic_cast < const G4Ellipsoid * > (so);
909  if (dso) {
910  params.push_back( dso->GetSemiAxisMax(0) );
911  params.push_back( dso->GetSemiAxisMax(1) );
912  params.push_back( dso->GetSemiAxisMax(2) );
913  params.push_back( dso->GetZBottomCut() );
914  params.push_back( dso->GetZTopCut() );
915  }
916  } else if (solidType == "ELLIPTICAL_CONE") {
917  const G4EllipticalCone * elco =
918  dynamic_cast < const G4EllipticalCone * > (so);
919  if (elco) {
920  params.push_back( elco-> GetSemiAxisX() );
921  params.push_back( elco-> GetSemiAxisY() );
922  params.push_back( elco-> GetZMax() );
923  params.push_back( elco-> GetZTopCut() );
924  }
925  } else if (solidType == "HYPE") {
926  const G4Hype* hype = dynamic_cast < const G4Hype * > (so);
927  if (hype) {
928  params.push_back( hype->GetInnerRadius());
929  params.push_back( hype->GetOuterRadius());
930  params.push_back( hype->GetInnerStereo()/deg);
931  params.push_back( hype->GetOuterStereo()/deg);
932  params.push_back( 2*hype->GetZHalfLength());
933  }
934 // } else if( solidType == "TET" ) {
935 
936  } else if( solidType == "TWISTEDBOX" ) {
937  const G4TwistedBox* tbox = dynamic_cast < const G4TwistedBox * > (so);
938  if (tbox) {
939  params.push_back( tbox->GetPhiTwist()/deg );
940  params.push_back( tbox->GetXHalfLength() );
941  params.push_back( tbox->GetYHalfLength() );
942  params.push_back( tbox->GetZHalfLength() );
943  }
944  } else if( solidType == "TWISTEDTRAP" ) {
945  const G4TwistedTrap * ttrap = dynamic_cast < const G4TwistedTrap * > (so);
946  if (ttrap) {
947  params.push_back( ttrap->GetPhiTwist()/deg );
948  params.push_back( ttrap->GetZHalfLength() );
949  params.push_back( ttrap->GetPolarAngleTheta()/deg );
950  params.push_back( ttrap->GetAzimuthalAnglePhi()/deg );
951  params.push_back( ttrap->GetY1HalfLength() );
952  params.push_back( ttrap->GetX1HalfLength() );
953  params.push_back( ttrap->GetX2HalfLength() );
954  params.push_back( ttrap->GetY2HalfLength() );
955  params.push_back( ttrap->GetX3HalfLength() );
956  params.push_back( ttrap->GetX4HalfLength() );
957  params.push_back( ttrap->GetTiltAngleAlpha()/deg );
958  }
959  } else if( solidType == "TWISTEDTRD" ) {
960  const G4TwistedTrd * ttrd = dynamic_cast < const G4TwistedTrd * > (so);
961  if (ttrd) {
962  params.push_back( ttrd->GetX1HalfLength());
963  params.push_back( ttrd->GetX2HalfLength() );
964  params.push_back( ttrd->GetY1HalfLength() );
965  params.push_back( ttrd->GetY2HalfLength() );
966  params.push_back( ttrd->GetZHalfLength() );
967  params.push_back( ttrd->GetPhiTwist()/deg );
968  }
969  } else if( solidType == "TWISTEDTUBS" ) {
970  const G4TwistedTubs * ttub = dynamic_cast < const G4TwistedTubs * > (so);
971  if (ttub) {
972  params.push_back( ttub->GetInnerRadius() );
973  params.push_back( ttub->GetOuterRadius() );
974  params.push_back( ttub->GetZHalfLength() );
975  params.push_back( ttub->GetDPhi()/deg );
976  params.push_back( ttub->GetPhiTwist()/deg );
977  }
978  }
979  else
980  {
981  G4String ErrMessage = "Solid type not supported, sorry... " + solidType;
982  G4Exception("G4tgbGeometryDumpe::DumpSolidParams()",
983  "NotImplemented", FatalException, ErrMessage);
984  }
985 
986  return params;
987 }
988 
989 
990 //------------------------------------------------------------------------
992 {
993  if (!rotm) { rotm = new G4RotationMatrix(); }
994 
995  G4double de = MatDeterminant(rotm);
996  G4String rotName = LookForExistingRotation( rotm );
997  if( rotName != "" ) { return rotName; }
998 
999  G4ThreeVector v(1.,1.,1.);
1000  if (de < -0.9 ) // a reflection ....
1001  {
1002  (*theFile) << ":ROTM ";
1003  rotName = "RRM";
1004  rotName += G4UIcommand::ConvertToString(theRotationNumber++);
1005 
1006  (*theFile) << AddQuotes(rotName) << std::setprecision(9) << " "
1007  << approxTo0(rotm->xx()) << " "
1008  << approxTo0(rotm->yx()) << " "
1009  << approxTo0(rotm->zx()) << " "
1010  << approxTo0(rotm->xy()) << " "
1011  << approxTo0(rotm->yy()) << " "
1012  << approxTo0(rotm->zy()) << " "
1013  << approxTo0(rotm->xz()) << " "
1014  << approxTo0(rotm->yz()) << " "
1015  << approxTo0(rotm->zz()) << G4endl;
1016  }
1017  else if(de > 0.9 ) // a rotation ....
1018  {
1019  (*theFile) << ":ROTM ";
1020  rotName = "RM";
1021  rotName += G4UIcommand::ConvertToString(theRotationNumber++);
1022 
1023  (*theFile) << AddQuotes(rotName) << " "
1024  << approxTo0(rotm->thetaX()/deg) << " "
1025  << approxTo0(rotm->phiX()/deg) << " "
1026  << approxTo0(rotm->thetaY()/deg) << " "
1027  << approxTo0(rotm->phiY()/deg) << " "
1028  << approxTo0(rotm->thetaZ()/deg) << " "
1029  << approxTo0(rotm->phiZ()/deg) << G4endl;
1030  }
1031 
1032  theRotMats[rotName] = rotm;
1033 
1034  return rotName;
1035 }
1036 
1037 
1038 //------------------------------------------------------------------------
1039 std::vector<G4VPhysicalVolume*>
1040 G4tgbGeometryDumper::GetPVChildren( G4LogicalVolume* lv )
1041 {
1043  G4PhysicalVolumeStore::const_iterator ite;
1044  std::vector<G4VPhysicalVolume*> children;
1045  for( ite = pvstore->begin(); ite != pvstore->end(); ite++ )
1046  {
1047  if( (*ite)->GetMotherLogical() == lv )
1048  {
1049  children.push_back( *ite );
1050 #ifdef G4VERBOSE
1051  if( G4tgrMessenger::GetVerboseLevel() >= 1 )
1052  {
1053  G4cout << " G4tgbGeometryDumper::GetPVChildren() - adding children: "
1054  << (*ite)->GetName() << " of " << lv->GetName() << G4endl;
1055  }
1056 #endif
1057  }
1058  }
1059 
1060  return children;
1061 }
1062 
1063 
1064 //------------------------------------------------------------------------
1065 G4String G4tgbGeometryDumper::GetTGSolidType( const G4String& solidType )
1066 {
1067  G4String newsolidType = solidType.substr(2,solidType.length() );
1068  for( size_t ii = 0; ii < newsolidType.length(); ii++ )
1069  {
1070  newsolidType[ii] = toupper(newsolidType[ii] );
1071  }
1072  return newsolidType;
1073 }
1074 
1075 
1076 //------------------------------------------------------------------------
1077 G4double G4tgbGeometryDumper::MatDeterminant(G4RotationMatrix * ro)
1078 {
1079  G4Rep3x3 r = ro->rep3x3();
1080  return r.xx_*(r.yy_*r.zz_ - r.zy_*r.yz_)
1081  - r.yx_*(r.xy_*r.zz_ - r.zy_*r.xz_)
1082  + r.zx_*(r.xy_*r.yz_ - r.yy_*r.xz_);
1083 }
1084 
1085 
1086 //-----------------------------------------------------------------------
1087 G4double G4tgbGeometryDumper::approxTo0( G4double val )
1088 {
1090  ->GetSurfaceTolerance();
1091 
1092  if( std::fabs(val) < precision ) { val = 0; }
1093  return val;
1094 }
1095 
1096 
1097 //-----------------------------------------------------------------------
1098 G4String G4tgbGeometryDumper::AddQuotes( const G4String& str )
1099 {
1100  //--- look if there is a separating blank
1101 
1102  G4bool bBlank = FALSE;
1103  size_t siz = str.length();
1104  for( size_t ii = 0; ii < siz; ii++ )
1105  {
1106  if( str.substr(ii,1) == " " )
1107  {
1108  bBlank = TRUE;
1109  break;
1110  }
1111  }
1112  G4String str2 = str;
1113  if( bBlank )
1114  {
1115  str2 = G4String("\"") + str2 + G4String("\"");
1116  }
1117  return str2;
1118 }
1119 
1120 
1121 //------------------------------------------------------------------------
1122 G4String G4tgbGeometryDumper::SupressRefl( G4String name )
1123 {
1124  G4int irefl = name.rfind("_refl");
1125  if( irefl != -1 )
1126  {
1127  name = name.substr( 0, irefl );
1128  }
1129  return name;
1130 }
1131 
1132 //------------------------------------------------------------------------
1133 G4String G4tgbGeometryDumper::SubstituteRefl( G4String name )
1134 {
1135  G4int irefl = name.rfind("_refl");
1136  if( irefl != -1 )
1137  {
1138  name = name.substr( 0, irefl ) + "_REFL";
1139  }
1140  return name;
1141 }
1142 
1143 
1144 //------------------------------------------------------------------------
1145 G4String G4tgbGeometryDumper::GetIsotopeName( G4Isotope* isot )
1146 {
1147  G4String isotName = isot->GetName();
1148  // first look if this is isotope is already dumped,
1149  // with original isotope name or new one
1150  //
1151  std::map<G4String,G4Isotope*>::const_iterator ite;
1152  for( ite = theIsotopes.begin(); ite != theIsotopes.end(); ite++ )
1153  {
1154  if( isot == (*ite).second ) { return (*ite).first; }
1155  }
1156 
1157  // Now look if there is another isotope dumped with same name,
1158  // and if found add _N to the name
1159  //
1160  ite = theIsotopes.find( isotName );
1161  if( ite != theIsotopes.end() ) // Isotope found with same name
1162  {
1163  G4Isotope* isotold = (*ite).second;
1164  if( isot != isotold ) // new isotope it is not the really
1165  { // the same one as isotope found
1166  if( !Same2G4Isotopes(isot, isotold))
1167  { // if the two have same data, use the old one
1168  G4int ii = 2; // G4Nist does names isotopes of same element
1169  // with same name
1170  for(;;ii++)
1171  {
1172  G4String newIsotName = isotName + "_"
1174  std::map<G4String,G4Isotope*>::const_iterator ite2 =
1175  theIsotopes.find( newIsotName );
1176  if( ite2 == theIsotopes.end() )
1177  {
1178  isotName = newIsotName;
1179  break;
1180  }
1181  else
1182  {
1183  if( Same2G4Isotopes( isot, (*ite2).second ) )
1184  {
1185  isotName = newIsotName;
1186  break;
1187  }
1188  }
1189  }
1190  }
1191  }
1192  }
1193  return isotName;
1194 }
1195 
1196 
1197 //------------------------------------------------------------------------
1198 template< class TYP > G4String G4tgbGeometryDumper::
1199 GetObjectName( TYP* obj, std::map<G4String,TYP*> objectsDumped )
1200 {
1201  G4String objName = obj->GetName();
1202 
1203  // first look if this is objecy is already dumped,
1204  // with original object name or new one
1205  //
1206  typename std::map<G4String,TYP*>::const_iterator ite;
1207  for( ite = objectsDumped.begin(); ite != objectsDumped.end(); ite++ )
1208  {
1209  if( obj == (*ite).second ) { return (*ite).first; }
1210  }
1211 
1212  // Now look if there is another object dumped with same name,
1213  // and if found add _N to the name
1214  //
1215  ite = objectsDumped.find( objName );
1216 
1217  if( ite != objectsDumped.end() ) // Object found with same name
1218  {
1219  TYP* objold = (*ite).second;
1220  if( obj != objold ) // new object it is not the really
1221  { // the same one as object found
1222  G4int ii = 2;
1223  for(;;ii++)
1224  {
1225  G4String newObjName = objName + "_" + G4UIcommand::ConvertToString(ii);
1226  typename std::map<G4String,TYP*>::const_iterator ite2 =
1227  objectsDumped.find( newObjName );
1228  if( ite2 == objectsDumped.end() )
1229  {
1230  objName = newObjName;
1231  break;
1232  }
1233  }
1234  }
1235  }
1236  return objName;
1237 }
1238 
1239 
1240 //------------------------------------------------------------------------
1241 G4bool G4tgbGeometryDumper::CheckIfLogVolExists( const G4String& name,
1242  G4LogicalVolume* pt )
1243 {
1244  if( theLogVols.find( name ) != theLogVols.end() )
1245  {
1246  G4LogicalVolume* lvnew = (*(theLogVols.find(name))).second;
1247  if( lvnew != pt )
1248  {
1249  /*
1250  //---- Reflected volumes are repeated
1251 
1252  G4ReflectionFactory* reffact = G4ReflectionFactory::Instance();
1253  if( !reffact->IsReflected( pt ) && !reffact->IsReflected( lvnew ) )
1254  {
1255  G4String ErrMessage = "LogVol found but not same as before: " + name;
1256  G4Exception("G4tgbGeometryDumper::CheckIfLogVolExists()",
1257  "InvalidSetup", FatalException, ErrMessage);
1258  }
1259  */
1260  }
1261  return 1;
1262  }
1263  else
1264  {
1265  return 0;
1266  }
1267 }
1268 
1269 
1270 //-----------------------------------------------------------------------
1271 G4bool G4tgbGeometryDumper::CheckIfPhysVolExists( const G4String& name,
1272  G4VPhysicalVolume* pt )
1273 {
1274 #ifdef G4VERBOSE
1275  if( G4tgrMessenger::GetVerboseLevel() >= 1 )
1276  {
1277  G4cout << " G4tgbGeometryDumper::CheckIfPhysVolExists() - "
1278  << name << G4endl;
1279  }
1280 #endif
1281  if( thePhysVols.find( name ) != thePhysVols.end() )
1282  {
1283  if( (*(thePhysVols.find(name))).second != pt )
1284  {
1285  // G4String ErrMessage = "Placement found but not same as before: "
1286  // + name;
1287  // G4Exception("G4tgbGeometryDumper::CheckIfPhysVolExists()",
1288  // "InvalidSetup", FatalException, ErrMessage);
1289  G4cerr << " G4tgbGeometryDumper::CheckIfPhysVolExists () -"
1290  << " Placement found but not same as before : " << name << G4endl;
1291  }
1292  return 1;
1293  }
1294  else
1295  {
1296  return 0;
1297  }
1298 }
1299 
1300 
1301 //-----------------------------------------------------------------------
1302 G4String
1303 G4tgbGeometryDumper::LookForExistingRotation( const G4RotationMatrix* rotm )
1304 {
1305  G4String rmName = "";
1306 
1307  std::map<G4String,G4RotationMatrix*>::const_iterator ite;
1308  for( ite = theRotMats.begin(); ite != theRotMats.end(); ite++ )
1309  {
1310  if( (*ite).second->isNear( *rotm ) )
1311  {
1312  rmName = (*ite).first;
1313  break;
1314  }
1315  }
1316  return rmName;
1317 }
1318 
1319 
1320 //------------------------------------------------------------------------
1321 G4bool
1322 G4tgbGeometryDumper::Same2G4Isotopes( G4Isotope* isot1, G4Isotope* isot2 )
1323 {
1324  if ( (isot1->GetZ() != isot2->GetZ())
1325  || (isot1->GetN() != isot2->GetN())
1326  || (isot1->GetA() != isot2->GetA()) )
1327  {
1328  return 0;
1329  }
1330  else
1331  {
1332  return 1;
1333  }
1334 }
1335 
1336 
1337 //------------------------------------------------------------------------
1338 const G4String& G4tgbGeometryDumper::FindSolidName( G4VSolid* solid )
1339 {
1340  std::map<G4String,G4VSolid*>::const_iterator ite;
1341  for( ite = theSolids.begin(); ite != theSolids.end(); ite++ )
1342  {
1343  if( solid == (*ite).second ) { return (*ite).first; }
1344  }
1345 
1346  if( ite == theSolids.end() )
1347  {
1348  G4Exception("G4tgbGeometryDumper::FindSolidName()", "ReadError",
1349  FatalException, "Programming error.");
1350  }
1351  return (*ite).first;
1352 }