Geant4 VMC Version 6.6
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#include "TG4GeometryServices.h"
18#include "TG4SDServices.h"
20#include "TG4StateManager.h"
21
22#include <G4LogicalVolume.hh>
23#include <G4LogicalVolumeStore.hh>
24#include <G4SDManager.hh>
25#include <G4Threading.hh>
26
27#include <TGeoManager.h>
28#include <TGeoVolume.h>
29#include <TVirtualMCApplication.h>
30#include <TVirtualMCSensitiveDetector.h>
31
32#include <TVirtualMC.h>
33
34const G4String TG4SDConstruction::fgkDefaultSVLabel = "SV";
35
36//_____________________________________________________________________________
38 : TG4Verbose("SDConstruction"),
39 fMessenger(this),
40 fExclusiveSDScoring(false),
41 fSelectionFromTGeo(false),
42 fSVLabel(fgkDefaultSVLabel),
43 fSelection(),
44 fIsGflash(false)
45{
47}
48
49//_____________________________________________________________________________
54
55//
56// private methods
57//
58
59//_____________________________________________________________________________
61 G4LogicalVolume* lv, TVirtualMCSensitiveDetector* userSD) const
62{
64
65 if (VerboseLevel() > 2) {
66 G4cout << "TG4SDConstruction::CreateSD: lv=" << lv->GetName()
67 << " userSD=" << userSD << G4endl;
68 }
69
71 G4SDManager* pSDManager = G4SDManager::GetSDMpointer();
72
73 G4String sdName;
74 if (userSD) {
75 sdName = userSD->GetName();
76 }
77 else {
78 G4String lvName = lv->GetName();
79 sdName = "/" + lvName;
80 // cut copy number from sdName
81 sdName = geometryServices->UserVolumeName(sdName);
82 }
83
84 // create/retrieve the sensitive detector
86 sd = pSDManager->FindSensitiveDetector(sdName, false);
87 if (!sd) {
88
89 G4int mediumId = TG4GeometryServices::Instance()->GetMediumId(lv);
90
91 TG4SensitiveDetector* newSD = 0;
92 if (fIsGflash) {
93 newSD = new TG4GflashSensitiveDetector(sdName, mediumId);
94 if (VerboseLevel() > 2) {
95 G4cout << "Created TG4GflashSensitiveDetector with sdName=" << sdName
96 << " mediumId=" << mediumId << G4endl;
97 }
98 }
99 else if (userSD) {
100 newSD = new TG4SensitiveDetector(userSD, mediumId, fExclusiveSDScoring);
101 if (VerboseLevel() > 2) {
102 G4cout << "Created TG4SensitiveDetector with userSD=" << userSD
103 << " mediumId=" << mediumId
104 << " exclusiveSoring=" << fExclusiveSDScoring << G4endl;
105 }
106 }
107 else {
108 newSD = new TG4SensitiveDetector(sdName, mediumId);
109 if (VerboseLevel() > 2) {
110 G4cout << "Created TG4SensitiveDetector with sdName=" << sdName
111 << " mediumId=" << mediumId << G4endl;
112 }
113 }
114 pSDManager->AddNewDetector(newSD);
115 if (VerboseLevel() > 1) {
116 G4cout << "Sensitive detector " << sdName << " ID=" << newSD->GetID()
117 << " medium ID=" << newSD->GetMediumID() << " has been created."
118 << G4endl;
119 }
120 else {
121 if (VerboseLevel() > 2) {
122 G4cout << "Sensitive detector " << sdName << ", " << sd
123 << " already exists." << G4endl;
124 }
125 }
126 sd = newSD;
127 }
128 lv->SetSensitiveDetector(sd);
129}
130
131//_____________________________________________________________________________
133{
137
138 if (!gGeoManager) {
140 "TG4SDServices", "FillSDSelectionFromTGeo", "TGeo manager not defined.");
141 }
142
143 TObjArray* volumes = gGeoManager->GetListOfVolumes();
144 TIterator* it = volumes->MakeIterator();
145 TGeoVolume* volume = 0;
146 while ((volume = (static_cast<TGeoVolume*>(it->Next())))) {
147 if (TString(volume->GetOption()) == TString(fSVLabel.data())) {
148 G4cout << "Adding volume " << volume->GetName() << " in SD selection"
149 << G4endl;
150 fSelection.insert(volume->GetName());
151 }
152 }
153
154 if (!fSelection.size()) {
155 TString text = "No volumes with the option = \"";
156 text += TString(fSVLabel.data());
157 text += "\" were found in TGeo geometry.\n";
158 text += " The SD selection will not be applied.";
159 TG4Globals::Warning("TG4SDServices", "FillSDSelectionFromTGeo", text);
160 }
161}
162
163//_____________________________________________________________________________
165{
174
175 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
176 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
177 G4LogicalVolume* lv = (*lvStore)[i];
178
179 if (VerboseLevel() > 1) {
180 G4cout << "Setting volId as instance Id "
181 << lv->GetInstanceID() + TG4SDServices::GetFirstVolumeId()
182 << " to " << lv->GetName() << G4endl;
183 }
184
186 lv, lv->GetInstanceID() + TG4SDServices::GetFirstVolumeId(), false);
187 }
188}
189
190//_____________________________________________________________________________
192{
197
198 // Set volume IDs to volumes which have not SD
200
201 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
202 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
203 G4LogicalVolume* lv = (*lvStore)[i];
204
205 G4int id = 0;
206 if (lv->GetSensitiveDetector()) {
207 id =
208 static_cast<TG4SensitiveDetector*>(lv->GetSensitiveDetector())->GetID();
209 }
210 else {
211 id = counter++;
212 }
213
214 if (VerboseLevel() > 1) {
215 G4cout << "Setting volId as SD id " << id << " to " << lv->GetName()
216 << G4endl;
217 }
218
219 TG4SDServices::Instance()->MapVolume(lv, id, true);
220 }
221}
222
223//
224// public methods
225//
226
227//_____________________________________________________________________________
229{
232
233 if (VerboseLevel() > 1) G4cout << "TG4SDConstruction::Construct" << G4endl;
234
235 G4bool isMaster = !G4Threading::IsWorkerThread();
236
237 // Get volumes selected as SD in TGeo geometry
239
240 // Construct user SDs
242 TVirtualMCApplication::Instance()->ConstructSensitiveDetectors();
244
245 G4bool isUserSD = false;
246 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
247
248 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
249 G4LogicalVolume* lv = (*lvStore)[i];
250
251 // Check if a user SD is defined
252 TVirtualMCSensitiveDetector* userSD =
253 TG4SDServices::Instance()->GetUserSD(lv->GetName(), false);
254
255 // Create SD calling user sensitive detector
256
257 if (userSD) {
258 CreateSD(lv, userSD);
259 // if ( isMaster ) TG4SDServices::Instance()->MapVolume(lv, sdID);
260 isUserSD = true;
261 }
262 else {
263 // Create SD calling MCApplication::Stepping
264 // if exclusive scoring via user sensitive detectors is not activated and
265 // if selection is empty or if selection is defined and the volume name is
266 // in selection
267 if ((!fExclusiveSDScoring) &&
268 (!fSelection.size() ||
269 fSelection.find(lv->GetName()) != fSelection.end())) {
270
271 CreateSD(lv, 0);
272 }
273 }
274 }
275
276 // Define volume Ids if VMC SD is not defined for all volumes
277 // (either due to user defined SDs or user selection of sensitive volumes)
278 if (isMaster) {
279 if (isUserSD) {
281 }
282 else {
284 }
285 }
286
287 // Initialize user geometry
289 TVirtualMCApplication::Instance()->InitGeometry();
291
292 // Initialize user SDs
293 if (TG4SDServices::Instance()->GetUserSDs()) {
294 for (auto& userSD : (*TG4SDServices::Instance()->GetUserSDs())) {
295 userSD->Initialize();
296 }
297 }
298
299 if (VerboseLevel() > 1) {
302 if (fSelection.size()) {
304 }
305 if (TG4SDServices::Instance()->GetUserSDs()) {
307 }
308 }
309
310 if (VerboseLevel() > 1)
311 G4cout << "TG4SDConstruction::Construct done" << G4endl;
312}
313
314//_____________________________________________________________________________
315void TG4SDConstruction::AddSelection(const G4String& selection)
316{
319
320 std::istringstream is(selection);
321 G4String token;
322 while (is >> token) {
323 if (VerboseLevel() > 1) {
324 G4cout << "Adding volume " << token << " in SD selection." << G4endl;
325 }
326 fSelection.insert(token);
327 }
328}
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
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()
Base class for defining the verbose level and a common messenger.
Definition TG4Verbose.h:36
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
@ kConstructSD
in ConstructSensitiveDetectors
@ kNotInApplication
not in VMC application
@ kInitGeometry
in InitGeometry