G4Root Version 6.6
Loading...
Searching...
No Matches
TG4RootNavMgr.cxx
Go to the documentation of this file.
1// @(#)root/g4root:$Id$
2// Author: Andrei Gheata 07/08/06
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
16
17#include "TG4RootNavMgr.h"
19#include "TG4RootNavigator.h"
20
21#include "G4PropagatorInField.hh"
22#include "G4RunManager.hh"
23#include "G4TransportationManager.hh"
24
25#include "TGeoManager.h"
26
28// ClassImp(TG4RootNavMgr)
30
33
34//______________________________________________________________________________
36 : TObject(),
37 fGeometry(0),
38 fNavigator(0),
39 fDetConstruction(0),
40 fConnected(kFALSE)
41{
43}
44
45//______________________________________________________________________________
47 TGeoManager* geom, TG4RootDetectorConstruction* detConstruction)
48 : TObject(),
49 fGeometry(geom),
50 fNavigator(0),
51 fDetConstruction(detConstruction),
52 fConnected(kFALSE)
53{
55 if (!detConstruction) {
58 }
59 else {
60 SetNavigator(new TG4RootNavigator(detConstruction));
61 }
62}
63
64//______________________________________________________________________________
66{
68 // if (fNavigator) delete fNavigator;
70 fRootNavMgr = 0;
71
72 G4bool isMaster = !G4Threading::IsWorkerThread();
73 if (isMaster) {
75 }
76}
77
78//______________________________________________________________________________
80{
82 if (fRootNavMgr) return fRootNavMgr;
83 // Check if we have to create one.
84 if (!geom) return NULL;
85 fRootNavMgr = new TG4RootNavMgr(geom);
86 G4bool isMaster = !G4Threading::IsWorkerThread();
87 Printf("isMaster=%d", isMaster);
88 if (isMaster) {
90 }
91 return fRootNavMgr;
92}
93
94//______________________________________________________________________________
96{
98 if (fRootNavMgr) return fRootNavMgr;
99 // Check if we have to create one.
101 G4bool isMaster = !G4Threading::IsWorkerThread();
102 if (isMaster) {
104 }
105 return fRootNavMgr;
106}
107
108//_____________________________________________________________________________
114
115//______________________________________________________________________________
117{
119 if (fConnected) {
120 Info("ConnectToG4", "Already connected");
121 return kTRUE;
122 }
123 if (!fDetConstruction) {
124 Error("ConnectToG4", "No detector construction set !");
125 return kFALSE;
126 }
127
128 if (!fNavigator) {
129 Error(
130 "ConnectToG4", "Navigator has to be created befor connecting to G4 !!!");
131 return kFALSE;
132 }
133
134 G4RunManager* runManager = G4RunManager::GetRunManager();
135 if (!runManager) {
136 Error("ConnectToG4", "Unable to connect: G4RunManager not instantiated");
137 return kFALSE;
138 }
139 G4bool isMaster = !G4Threading::IsWorkerThread();
140 if (isMaster) runManager->SetUserInitialization(fDetConstruction);
141 Info("ConnectToG4",
142 "ROOT detector construction class connected to G4RunManager");
143 fConnected = kTRUE;
144 return kTRUE;
145}
146
147//______________________________________________________________________________
149{
151 if (fConnected) {
152 Error("SetNavigator",
153 "Navigator set after instantiation of G4RunManager. Won't set!!!");
154 return;
155 }
156 G4TransportationManager* trMgr =
157 G4TransportationManager::GetTransportationManager();
158 // G4Navigator *oldNav = trMgr->GetNavigatorForTracking();
159 trMgr->SetNavigatorForTracking(nav);
160 G4FieldManager* fieldMgr =
161 trMgr->GetPropagatorInField()->GetCurrentFieldManager();
162 delete trMgr->GetPropagatorInField();
163 trMgr->SetPropagatorInField(new G4PropagatorInField(nav, fieldMgr));
164 trMgr->ActivateNavigator(nav);
165 G4EventManager* evtMgr = G4EventManager::GetEventManager();
166 if (evtMgr)
167 evtMgr->GetTrackingManager()->GetSteppingManager()->SetNavigator(nav);
168 fNavigator = nav;
169 // trMgr->DeRegisterNavigator(oldNav);
170 Info("SetNavigator",
171 "TG4RootNavigator created and registered to G4TransportationManager");
172}
173
174//______________________________________________________________________________
176 TVirtualUserPostDetConstruction* sdinit, Int_t nthreads)
177{
179 Info("Initialize", "Creating G4 hierarchy ...");
181 if (nthreads > 1) gGeoManager->SetMaxThreads(nthreads);
182}
183
184//______________________________________________________________________________
185void TG4RootNavMgr::LocateGlobalPointAndSetup(Double_t* pt, Double_t* dir)
186{
188 G4ThreeVector point(pt[0], pt[1], pt[2]);
189 if (dir) {
190 G4ThreeVector direction(dir[0], dir[1], dir[2]);
191 fNavigator->LocateGlobalPointAndSetup(point, &direction);
192 }
193 else {
195 }
196 fNavigator->PrintState();
197}
198
199//______________________________________________________________________________
201{
203 fNavigator->SetVerboseLevel(level);
204}
205
206//______________________________________________________________________________
208{
210 G4NavigationHistory* history = fNavigator->GetHistory();
211 G4cout << *history << G4endl;
212}
213
214//______________________________________________________________________________
215void TG4RootNavMgr::SetG4TrackingManager(G4TrackingManager* trackingManager)
216{
217 fNavigator->SetG4TrackingManager(trackingManager);
218}
219
220//______________________________________________________________________________
222 std::function<Bool_t(Int_t)> restoreGeoStateFunction)
223{
224 fNavigator->SetGeometryRestoreFunction(restoreGeoStateFunction);
225}
Definition of the TG4RootDetectorConstruction and TVirtualUserPostDetConstruction classes.
Definition of the TG4RootNavMgr class.
Definition of the TG4RootNavigator class.
Builder creating a pseudo G4 geometry starting from a TGeo geometry.
void Initialize(TVirtualUserPostDetConstruction *sdinit=0)
Manager class creating a G4Navigator based on a ROOT geometry.
static G4ThreadLocal TG4RootNavMgr * fRootNavMgr
void SetVerboseLevel(Int_t level)
TGeoManager * fGeometry
Pointer to TGeo geometry.
void SetNavigator(TG4RootNavigator *nav)
static TG4RootNavMgr * fgMasterInstance
void SetG4TrackingManager(G4TrackingManager *trackingManager)
Forward current G4TrackingManager.
Bool_t fConnected
Flags connection to G4.
static TG4RootNavMgr * GetMasterInstance()
TG4RootNavigator * fNavigator
G4 navigator working with TGeo.
void PrintG4State() const
void LocateGlobalPointAndSetup(Double_t *pt, Double_t *dir=0)
static TG4RootNavMgr * GetInstance(TGeoManager *geom=0)
void Initialize(TVirtualUserPostDetConstruction *sdinit=0, Int_t nthreads=1)
virtual ~TG4RootNavMgr()
TG4RootDetectorConstruction * fDetConstruction
void SetGeometryRestoreFunction(std::function< Bool_t(Int_t)> restoreGeoStateFunction)
Forward the given function to the TG4RootNavigator.
GEANT4 navigator using directly a TGeo geometry.
void SetG4TrackingManager(G4TrackingManager *trackingManager)
Set current G4TrackingManager.
G4NavigationHistory * GetHistory()
Return the navigation history.
void SetGeometryRestoreFunction(std::function< Bool_t(Int_t)> restoreGeoStateFunction)
virtual G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=0, const G4bool pRelativeSearch=true, const G4bool ignoreDirection=true)
Abstract class for defining links to G4 geometry.