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