Geant4 VMC Version 6.8
Loading...
Searching...
No Matches
TG4SDConstruction.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2007 - 2015 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
14
15#include "TG4SDConstruction.h"
16
17#include "TG4GeometryServices.h"
19#include "TG4SDServices.h"
21#include "TG4StateManager.h"
22
23#include <G4LogicalVolume.hh>
24#include <G4LogicalVolumeStore.hh>
25#include <G4SDManager.hh>
26#include <G4Threading.hh>
27
28#include <TGeoManager.h>
29#include <TGeoVolume.h>
30#include <TVirtualMCApplication.h>
31#include <TVirtualMCSensitiveDetector.h>
32
33#include <TVirtualMC.h>
34
35const G4String TG4SDConstruction::fgkDefaultSVLabel = "SV";
36
37//_____________________________________________________________________________
39 : TG4Verbose("SDConstruction"),
40 fMessenger(this),
42 fSelectionFromTGeo(false),
44 fSelection(),
45 fIsGflash(false)
46{
48}
49
50//_____________________________________________________________________________
55
56//
57// private methods
58//
59
60//_____________________________________________________________________________
62 G4LogicalVolume* lv, TVirtualMCSensitiveDetector* userSD) const
63{
65
66 if (VerboseLevel() > 2) {
67 G4cout << "TG4SDConstruction::CreateSD: lv=" << lv->GetName()
68 << " userSD=" << userSD << G4endl;
69 }
70
72 G4SDManager* pSDManager = G4SDManager::GetSDMpointer();
73
74 G4String sdName;
75 if (userSD) {
76 sdName = userSD->GetName();
77 }
78 else {
79 sdName = "/" + geometryServices->UserVolumeName(lv);
80 }
81
82 // create/retrieve the sensitive detector
84 sd = pSDManager->FindSensitiveDetector(sdName, false);
85 if (!sd) {
86
87 G4int mediumId = TG4GeometryServices::Instance()->GetMediumId(lv);
88
89 TG4SensitiveDetector* newSD = 0;
90 if (fIsGflash) {
91 newSD = new TG4GflashSensitiveDetector(sdName, mediumId);
92 if (VerboseLevel() > 2) {
93 G4cout << "Created TG4GflashSensitiveDetector with sdName=" << sdName
94 << " mediumId=" << mediumId << G4endl;
95 }
96 }
97 else if (userSD) {
98 newSD = new TG4SensitiveDetector(userSD, mediumId, fExclusiveSDScoring);
99 if (VerboseLevel() > 2) {
100 G4cout << "Created TG4SensitiveDetector with userSD=" << userSD
101 << " mediumId=" << mediumId
102 << " exclusiveSoring=" << fExclusiveSDScoring << G4endl;
103 }
104 }
105 else {
106 newSD = new TG4SensitiveDetector(sdName, mediumId);
107 if (VerboseLevel() > 2) {
108 G4cout << "Created TG4SensitiveDetector with sdName=" << sdName
109 << " mediumId=" << mediumId << G4endl;
110 }
111 }
112 pSDManager->AddNewDetector(newSD);
113 if (VerboseLevel() > 1) {
114 G4cout << "Sensitive detector " << sdName << " ID=" << newSD->GetID()
115 << " medium ID=" << newSD->GetMediumID() << " has been created."
116 << G4endl;
117 }
118 else {
119 if (VerboseLevel() > 2) {
120 G4cout << "Sensitive detector " << sdName << ", " << sd
121 << " already exists." << G4endl;
122 }
123 }
124 sd = newSD;
125 }
126 lv->SetSensitiveDetector(sd);
127}
128
129//_____________________________________________________________________________
131{
135
136 if (!gGeoManager) {
138 "TG4SDServices", "FillSDSelectionFromTGeo", "TGeo manager not defined.");
139 }
140
141 TObjArray* volumes = gGeoManager->GetListOfVolumes();
142 TIterator* it = volumes->MakeIterator();
143 TGeoVolume* volume = 0;
144 while ((volume = (static_cast<TGeoVolume*>(it->Next())))) {
145 if (TString(volume->GetOption()) == TString(fSVLabel.data())) {
146 G4cout << "Adding volume " << volume->GetName() << " in SD selection"
147 << G4endl;
148 fSelection.insert(volume->GetName());
149 }
150 }
151
152 if (!fSelection.size()) {
153 TString text = "No volumes with the option = \"";
154 text += TString(fSVLabel.data());
155 text += "\" were found in TGeo geometry.\n";
156 text += " The SD selection will not be applied.";
157 TG4Globals::Warning("TG4SDServices", "FillSDSelectionFromTGeo", text);
158 }
159}
160
161//_____________________________________________________________________________
163{
164 // Use the first instance ID as the candidate for each source volume.
165 // MapVolume reuses that ID for all other representations of the source.
166
167 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
168 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
169 G4LogicalVolume* lv = (*lvStore)[i];
170
171 if (VerboseLevel() > 1) {
172 G4cout << "Candidate volId from instance Id "
173 << lv->GetInstanceID() + TG4SDServices::GetFirstVolumeId()
174 << " to " << lv->GetName() << G4endl;
175 }
176
178 lv, lv->GetInstanceID() + TG4SDServices::GetFirstVolumeId(), true);
179 }
180}
181
182//_____________________________________________________________________________
184{
188
189 // Set volume IDs to volumes which have not SD
191
192 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
193 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
194 G4LogicalVolume* lv = (*lvStore)[i];
195
196 G4int id = 0;
197 if (lv->GetSensitiveDetector()) {
198 id =
199 static_cast<TG4SensitiveDetector*>(lv->GetSensitiveDetector())->GetID();
200 }
201 else {
202 id = counter++;
203 }
204
205 if (VerboseLevel() > 1) {
206 G4cout << "Candidate volId from SD id " << id << " to " << lv->GetName()
207 << G4endl;
208 }
209
210 TG4SDServices::Instance()->MapVolume(lv, id, true);
211 }
212}
213
214//
215// public methods
216//
217
218//_____________________________________________________________________________
220{
223
224 if (VerboseLevel() > 1) G4cout << "TG4SDConstruction::Construct" << G4endl;
225
226 G4bool isMaster = !G4Threading::IsWorkerThread();
227
228 // Get volumes selected as SD in TGeo geometry
230
231 // Construct user SDs
233 TVirtualMCApplication::Instance()->ConstructSensitiveDetectors();
235
237
238 G4bool isUserSD = false;
239 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
240
241 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
242 G4LogicalVolume* lv = (*lvStore)[i];
243
244 const G4String selectionName =
246
247 // Check if a user SD is defined
248 TVirtualMCSensitiveDetector* userSD =
249 TG4SDServices::Instance()->GetUserSD(selectionName, false);
250
251 // Create SD calling user sensitive detector
252
253 if (userSD) {
254 CreateSD(lv, userSD);
255 // if ( isMaster ) TG4SDServices::Instance()->MapVolume(lv, sdID);
256 isUserSD = true;
257 }
258 else {
259 // Create SD calling MCApplication::Stepping
260 // if exclusive scoring via user sensitive detectors is not activated and
261 // if selection is empty or if selection is defined and the volume name is
262 // in selection
263 if ((!fExclusiveSDScoring) &&
264 (!fSelection.size() ||
265 fSelection.find(selectionName) != fSelection.end())) {
266
267 CreateSD(lv, 0);
268 }
269 }
270 }
271
272 // Define volume Ids if VMC SD is not defined for all volumes
273 // (either due to user defined SDs or user selection of sensitive volumes)
274 if (isMaster) {
276 if (isUserSD) {
278 }
279 else {
281 }
282 }
283
284 // Initialize user geometry
286 TVirtualMCApplication::Instance()->InitGeometry();
288
289 // Initialize user SDs
290 if (TG4SDServices::Instance()->GetUserSDs()) {
291 for (auto& userSD : (*TG4SDServices::Instance()->GetUserSDs())) {
292 userSD->Initialize();
293 }
294 }
295
296 if (VerboseLevel() > 1) {
299 if (fSelection.size()) {
301 }
302 if (TG4SDServices::Instance()->GetUserSDs()) {
304 }
305 }
306
307 if (VerboseLevel() > 1)
308 G4cout << "TG4SDConstruction::Construct done" << G4endl;
309}
310
311//_____________________________________________________________________________
312void TG4SDConstruction::AddSelection(const G4String& selection)
313{
316
317 std::istringstream is(selection);
318 G4String token;
319 while (is >> token) {
320 if (VerboseLevel() > 1) {
321 G4cout << "Adding volume " << token << " in SD selection." << G4endl;
322 }
323 fSelection.insert(token);
324 }
325}
Definition of the TG4GeometryServices class.
Definition of the TG4GflashSensitiveDetector class.
Definition of the TG4SDConstruction class.
Definition of the TG4SDServices class.
Definition of the TG4SensitiveDetector class.
Definition of the TG4StateManager class.
Services for accessing to Geant4 geometry.
G4int GetMediumId(G4LogicalVolume *lv) const
const G4String & UserVolumeName(const G4String &name) const
static TG4GeometryServices * Instance()
Sensitive detector with Gflash.
static void Warning(const TString &className, const TString &methodName, const TString &text)
static void Exception(const TString &className, const TString &methodName, const TString &text)
void CreateSD(G4LogicalVolume *lv, TVirtualMCSensitiveDetector *userSD) const
G4String fSVLabel
the label used to flag sensitive volumes in TGeo
std::set< G4String > fSelection
the set of volumes names which are selected as sensitive
void AddSelection(const G4String &selection)
G4bool fIsGflash
the flag to acivate creating Gflash sensitive detectors
static const G4String fgkDefaultSVLabel
default value of the sensitive volumes label
G4bool fExclusiveSDScoring
the flag to activate new scoring via user defined sensitive detectors
TG4SDMessenger fMessenger
messenger
G4bool fSelectionFromTGeo
the flag to activate retrieving sensitive volumes from TGeo
TVirtualMCSensitiveDetector * GetUserSD(G4String volumeName, G4bool warn=true) const
void PrintUserSensitiveDetectors() const
void PrintSensitiveVolumes() const
void PrintVolNameToIdMap() const
void MapVolume(G4LogicalVolume *lv, G4int id, G4bool fillLVToVolIdMap)
void PrintVolIdToLVMap() const
static TG4SDServices * Instance()
static G4int GetFirstVolumeId()
Sensitive detector class for calling a user defined stepping function.
static G4int GetTotalNofSensitiveDetectors()
void SetNewState(TG4ApplicationState state)
static TG4StateManager * Instance()
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
TG4Verbose(const G4String &cmdName)
@ kConstructSD
in ConstructSensitiveDetectors
@ kNotInApplication
not in VMC application
@ kInitGeometry
in InitGeometry