]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TGeant4/TG4SDServices.cxx
More exact rounding function, but also much slower.
[u/mrichter/AliRoot.git] / TGeant4 / TG4SDServices.cxx
1 // $Id$
2 // Category: digits+hits
3 //
4 // See the class description in the header file.
5
6 #include "TG4SDServices.h"
7 #include "TG4VSensitiveDetector.h"
8 #include "TG4GeometryServices.h"
9 #include "TG4Globals.h"
10
11 #include <G4VSensitiveDetector.hh>
12 #include <G4LogicalVolumeStore.hh>
13 #include <G4LogicalVolume.hh>
14 #include <G4Material.hh>
15 #include <g4std/iomanip>
16
17 TG4SDServices* TG4SDServices::fgInstance = 0;
18
19 //_____________________________________________________________________________
20 TG4SDServices::TG4SDServices(){
21 //
22   if (fgInstance) {
23     TG4Globals::Exception(
24       "TG4SDServices: attempt to create two instances of singleton.");
25       
26   fgInstance = this;
27   }
28 }
29
30 //_____________________________________________________________________________
31 TG4SDServices::TG4SDServices(const TG4SDServices& right) {
32 // 
33   TG4Globals::Exception(
34     "Attempt to copy TG4SDServices singleton.");
35 }
36
37
38 //_____________________________________________________________________________
39 TG4SDServices::~TG4SDServices(){
40 //
41 }
42
43 // operators
44
45 //_____________________________________________________________________________
46 TG4SDServices& TG4SDServices::operator=(const TG4SDServices& right)
47 {
48   // check assignement to self
49   if (this == &right) return *this;
50
51   TG4Globals::Exception(
52     "Attempt to assign TG4SDServices singleton.");
53     
54   return *this;  
55 }    
56           
57
58 // public methods 
59
60 //_____________________________________________________________________________
61 void TG4SDServices::PrintStatistics(G4bool open, G4bool close) const
62 {
63 // Print G4 SD statistics
64 // ---
65   
66   if (open)  TG4Globals::PrintStars(true);
67      
68    G4cout << "          " << G4std::setw(5) << NofSensitiveDetectors()  
69                           << " sensitive detectors" << G4endl;
70
71   if (close) TG4Globals::PrintStars(false);
72 }
73
74 //_____________________________________________________________________________
75 G4int TG4SDServices::GetVolumeID(const G4String& volName) const
76
77 // Returns the sensitive detector identifier.
78 // !! Gives exception in case logical volume is not associated with 
79 // a sensitive detector.
80 // ---
81
82   G4String g4VolName 
83     = TG4GeometryServices::Instance()->CutName(volName);
84
85   G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
86   
87   for (G4int i=0; i<pLVStore->size(); i++) {
88     G4LogicalVolume* lv = (*pLVStore)[i];
89     G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
90   
91     if (sd && sd->GetName() == g4VolName) 
92         return GetSensitiveDetector(sd)->GetID();
93   }
94
95   G4String text = "TG4SDServices::GetVolumeID: Sensitive detector ";
96   text = text + g4VolName;
97   text = text + " is not defined.\n"; 
98   TG4Globals::Warning(text);
99   return 0;
100 }
101
102
103 //_____________________________________________________________________________
104 G4int TG4SDServices::GetVolumeID(G4LogicalVolume* logicalVolume) const 
105 {
106 // Returns the sensitive detector ID of the specified
107 // logical volume.
108 // ---
109  
110   G4VSensitiveDetector* sd
111     = logicalVolume->GetSensitiveDetector();
112
113   if (sd) return GetSensitiveDetector(sd)->GetID();
114
115   G4String text = "TG4SDServices::GetVolumeID: \n";
116   text = text + "    Volume " + logicalVolume->GetName();
117   text = text + " has not a sensitive detector.";
118   //TG4Globals::Exception(text);
119   TG4Globals::Warning(text);
120   return 0;
121
122
123
124 //_____________________________________________________________________________
125 G4String TG4SDServices::GetVolumeName(G4int volumeId) const
126 {
127 // Returns the name of the sensitive detector with the given identifier.
128 // Gives a warning in case logical volume is not associated with 
129 // a sensitive detector.
130 // ---
131
132   G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
133   
134   for (G4int i=0; i<pLVStore->size(); i++) {
135     G4LogicalVolume* lv = (*pLVStore)[i];
136     G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
137     
138     if (sd && GetSensitiveDetector(sd)->GetID() == volumeId) 
139         return sd->GetName();
140   }
141
142   G4String text = "TG4SDServices::VolName:\n";
143   text = text + "    Sensitive detector with given id is not defined. \n";
144   TG4Globals::Warning(text);
145   return "";                     
146 }
147
148
149 //_____________________________________________________________________________
150 G4LogicalVolume* TG4SDServices::GetLogicalVolume(G4int volumeId) const 
151 {
152 // Finds the first logical volume with specified volumeId 
153 // (sensitive detector ID) in G4LogicalVolumeStore.
154 // ---
155
156   G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
157   
158   for (G4int i=0; i<pLVStore->size(); i++) {
159     G4LogicalVolume* lv = (*pLVStore)[i];
160     if (GetVolumeID(lv) == volumeId) return lv;
161   }
162   
163   G4String text = "TG4SDServices::GetLogicalVolume: \n";
164   text = text + "    Logical volume with given ID does not exist.";
165   return 0;                      
166 }  
167
168
169 //_____________________________________________________________________________
170 Int_t TG4SDServices::GetMediumId(G4int volumeId)  const
171 {
172 // Return the material number for a given volume id
173 // ---
174
175   return TG4GeometryServices::Instance()
176             ->GetMediumId(GetLogicalVolume(volumeId));                   
177 }
178  
179
180 //_____________________________________________________________________________
181 Int_t TG4SDServices::NofSensitiveDetectors() const
182 {
183 // Returns the total number of sensitive detectors.
184 // ---
185
186   return TG4VSensitiveDetector::GetTotalNofSensitiveDetectors();
187 }
188
189 //_____________________________________________________________________________
190 TG4VSensitiveDetector* TG4SDServices::GetSensitiveDetector(
191                                          G4VSensitiveDetector* sd) const
192 {
193 // Checks and converts type of the sensitive detector.
194 // ---
195
196   if (!sd) return 0;
197   
198   TG4VSensitiveDetector* tsd = dynamic_cast<TG4VSensitiveDetector*>(sd);
199   
200   if (!tsd) {
201     TG4Globals::Exception(
202       "TG4SDServices::GetSensitiveDetector: Wrong sensitive detector type.");
203     return 0;
204   }    
205   else 
206     return tsd;  
207
208
209