Updated class description: added class title, author;
[u/mrichter/AliRoot.git] / AliGeant4 / AliSDConstruction.cxx
CommitLineData
c926eee0 1// $Id$
2// Category: digits+hits
3//
4// See the class description in the header file.
5
6#include "AliSDConstruction.h"
7#include "AliSensitiveDetector.h"
8#include "AliLegoSensitiveDetector.h"
9#include "AliGlobals.h"
10#include "AliRun.h"
11#include "AliModule.h"
12
13#include "TG4GeometryServices.h"
14
15#include <G4SDManager.hh>
16#include <G4LogicalVolume.hh>
17#include <G4LogicalVolumeStore.hh>
18
19#include <TObjArray.h>
20
21AliSDConstruction::AliSDConstruction()
22 : TG4VSDConstruction() {
23//
24}
25
26AliSDConstruction::~AliSDConstruction() {
27//
28}
29
30// private methods
31
32void AliSDConstruction::InitializeModules()
33{
34// Initializes AliModules.
35// ---
36
37 TObjArray* modules = gAlice->Modules();
38 TIter next(modules);
39 AliModule* module;
40 while((module = (AliModule*)next()))
41 module->Init();
42}
43
44AliModule* AliSDConstruction::FindAliModule(G4LogicalVolume* lv) const
45{
46// Finds the module containing specified logical volume.
47// ---
48
49 // geometry manager
50 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
51
52 // get g3 volume name
53 G4String g3Name = lv->GetName();
54 geometryServices->G4ToG3VolumeName(g3Name);
55
56 // get module name from the map
57 G4String moduleName = geometryServices->GetMapSecond(g3Name);
58
59 // find module from gAlice
60 AliModule* module = gAlice->GetModule(moduleName);
61 if (!module) {
62 G4String text = "AliSDConstruction::FindAliModule:\n";
63 text = text + " AliModule " + moduleName;
64 text = text + " (mapped from logical volume " + lv->GetName() + ")\n";
65 text = text + " has not been found in gAlice.";
66 AliGlobals::Exception(text);
67 }
68
69 return module;
70}
71
72void AliSDConstruction::CreateSD(G4LogicalVolume* lv, AliModule* module) const
73{
74// Creates/retrieves a sensitive detector for the logical volume.
75// ---
76
77 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
78 G4SDManager* pSDManager = G4SDManager::GetSDMpointer();
79
80 G4String lvName = lv->GetName();
81
82 G4String moduleName = module->GetName();
83 G4String sdName = "/Alice/" + moduleName + "/" + lvName;
84
85 // cut copy number from sdName
86 geometryServices->G4ToG3VolumeName(sdName);
87
88 // create/retrieve the sensitive detector
89 G4VSensitiveDetector* sd = 0;
90 sd = pSDManager->FindSensitiveDetector(sdName);
91 if (!sd) {
92 AliSensitiveDetector* asd
93 = new AliSensitiveDetector(sdName, module);
94 pSDManager->AddNewDetector(asd);
95 // add verbose
96 G4cout << "Sensitive detector " << sdName << "("
97 << asd->GetID() << ") has been created." << G4endl;
98 sd = asd;
99 }
100 lv->SetSensitiveDetector(sd);
101}
102
103void AliSDConstruction::CreateLegoSD(G4LogicalVolume* lv, AliLego* lego) const
104{
105// Replaces the existing sensitive detector of the logical volume
106// with the lego sensitive detector.
107// ---
108
109 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
110 G4SDManager* pSDManager = G4SDManager::GetSDMpointer();
111
112 G4String lvName = lv->GetName();
113 G4String sdName = "/Alice/lego/" + lvName;
114
115 // cut copy number from sdName
116 geometryServices->G4ToG3VolumeName(sdName);
117
118 // retrieve the standard sensitive detector
119 G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
120
121 // create/retrieve the lego sensitive detector
122 G4VSensitiveDetector* legoVSD = 0;
123 legoVSD = pSDManager->FindSensitiveDetector(sdName);
124
125 if (!legoVSD) {
126 AliLegoSensitiveDetector* legoSD
127 = new AliLegoSensitiveDetector(sdName, lego, sd);
128 pSDManager->AddNewDetector(legoSD);
129 // add verbose
130 G4cout << "Lego sensitive detector " << sdName
131 << ") has been created." << G4endl;
132 legoVSD = legoSD;
133 }
134 lv->SetSensitiveDetector(legoVSD);
135}
136
137void AliSDConstruction::UnsetLegoSD(G4LogicalVolume* lv) const
138{
139// Replace the lego sensitive detector of the logical volume
140// with the standard sensitive detector.
141// ---
142
143 G4SDManager* pSDManager = G4SDManager::GetSDMpointer();
144
145 // get the lego sensitive detector
146 G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
147 AliLegoSensitiveDetector* legoSD = 0;
148 if (sd) {
149 // check type
150 legoSD = dynamic_cast<AliLegoSensitiveDetector*>(sd);
151 if (!legoSD) {
152 G4String text = "AliSDConstruction::UnsetLego: \n";
153 text = text + " Wrong sensitive detector type.";
154 AliGlobals::Exception(text);
155 }
156 }
157
158 // get the standard sensitive detector
159 G4VSensitiveDetector* standardSD = legoSD->GetStandardSD();
160
161 // set the standard sensitive detector
162 lv->SetSensitiveDetector(standardSD);
163}
164
165
166// public methods
167
168void AliSDConstruction::Construct()
169{
170// Creates sensitive detectors and initialize AliModules.
171// Sensitive detectors are set to all logical volumes
172// ---
173
174 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
175 G4int nofLV = lvStore->size();
176
177 for (G4int i=0; i<nofLV; i++) {
178 G4LogicalVolume* lv = (*lvStore)[i];
179 AliModule* module = FindAliModule(lv);
180 CreateSD(lv, module);
181 }
182
183 InitializeModules();
184}
185
186/*
187//#include "TG4GeometryManager.h"
188//#include <G3SensVolVector.hh>
189
190void AliSDConstruction::CreateSensitiveDetectors2()
191{
192// Creates sensitive detectors.
193// Sensitive detectors are set only to logical volumes
194// in G3SensVolVector.
195// ---
196
197 G3SensVolVector svVector
198 = TG4GeometryManager::Instance()->GetG3SensVolVector();
199
200 G4int nofSV = svVector.entries();
201 if (nofSV>0)
202 for (G4int isv=0; isv<nofSV; isv++) {
203 G4LogicalVolume* lv = svVector[isv];
204 AliModule* module = FindAliModule(lv);
205 CreateSD(lv, module);
206 }
207}
208*/
209
210void AliSDConstruction::SetLego(AliLego* lego) const
211{
212// Replaces the existing sensitive detectors
213// with lego sensitive detectors.
214// ---
215
216 if (!lego) {
217 G4String text = "AliSDConstruction::SetLego: \n";
218 text = text + " No AliLego is defined.";
219 AliGlobals::Warning(text);
220 return;
221 }
222
223 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
224 G4int nofLV = lvStore->size();
225
226 for (G4int i=0; i<nofLV; i++) {
227 G4LogicalVolume* lv = (*lvStore)[i];
228 CreateLegoSD(lv, lego);
229 }
230}
231
232void AliSDConstruction::UnsetLego() const
233{
234// Replace the lego sensitive detectors
235// back with standard sensitive detectors.
236// ---
237
238 G4SDManager* pSDManager = G4SDManager::GetSDMpointer();
239 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
240 G4int nofLV = lvStore->size();
241
242 // set back standard sensitive detectors
243 for (G4int i=0; i<nofLV; i++) {
244 G4LogicalVolume* lv = (*lvStore)[i];
245 UnsetLegoSD(lv);
246 }
247
248 // The lego sensitive detectors are not deleted here
249 // as there is no way how to unregister them
250 // from G4SDManager
251}