File: G4GeoManager.cpp

package info (click to toggle)
vecgeom 1.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,928 kB
  • sloc: cpp: 88,717; ansic: 6,894; python: 1,035; sh: 582; sql: 538; makefile: 29
file content (77 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "VecGeom/management/G4GeoManager.h"

#if defined(VECGEOM_GEANT4)

#include "G4GDMLParser.hh"
#include "G4GeometryManager.hh"
#include "G4VPhysicalVolume.hh"

#if defined(VECGEOM_ROOT)
// from VGM
#include "Geant4GM/volumes/Factory.h"
#include "RootGM/volumes/Factory.h"
// from ROOT
#include "TGeoManager.h"
#endif

namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

G4GeoManager::G4GeoManager() : fNavigator(nullptr)
{
}

void G4GeoManager::LoadG4Geometry(std::string gdmlfile, bool validate)
{
  G4GDMLParser parser;
  parser.Read(gdmlfile, validate);
  LoadG4Geometry(const_cast<G4VPhysicalVolume *>(parser.GetWorldVolume()));
}

// converts to a G4 geometry from ROOT using VGM
// expects a valid gGeoManager object for the import
// returns world pointer of in-memory representation
G4VPhysicalVolume *G4GeoManager::GetG4GeometryFromROOT()
{
  G4VPhysicalVolume *world(nullptr);
#ifdef VECGEOM_ROOT
  try {
    RootGM::Factory rtFactory;
    // rtFactory.SetDebug(1);
    rtFactory.SetIgnore(true);
    rtFactory.Import(gGeoManager->GetTopNode());

    Geant4GM::Factory g4Factory;

    // enable this to see debug output
    // g4Factory.SetDebug(1);
    // set to avoid crashes due to wrong material, etc. (which should
    // not affect the geometry test
    g4Factory.SetIgnore(true);
    rtFactory.Export(&g4Factory);
    world = g4Factory.World();
  } catch (...) {
    std::cerr << "caught exception in VGM; so return nullptr\n";
    world = nullptr;
  }
#else
// throw or put warning
#endif
  return world;
}

// sets a G4 geometry from existing G4PhysicalVolume
void G4GeoManager::LoadG4Geometry(G4VPhysicalVolume *world)
{
  // if there is an existing geometry
  if (fNavigator != nullptr) delete fNavigator;
  fNavigator = new G4Navigator();
  fNavigator->SetWorldVolume(world);

  // voxelize
  G4GeometryManager::GetInstance()->CloseGeometry(fNavigator->GetWorldVolume());
}
}
} // end namespaces

#endif