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