2 // Category: digits+hits
4 // See the class description in the header file.
6 #include "TG4SDServices.h"
7 #include "TG4VSensitiveDetector.h"
8 #include "TG4GeometryServices.h"
9 #include "TG4Globals.h"
11 #include <G4VSensitiveDetector.hh>
12 #include <G4LogicalVolumeStore.hh>
13 #include <G4LogicalVolume.hh>
14 #include <G4Material.hh>
15 #include <g4std/iomanip>
17 TG4SDServices* TG4SDServices::fgInstance = 0;
19 //_____________________________________________________________________________
20 TG4SDServices::TG4SDServices(){
23 TG4Globals::Exception(
24 "TG4SDServices: attempt to create two instances of singleton.");
30 //_____________________________________________________________________________
31 TG4SDServices::TG4SDServices(const TG4SDServices& right) {
33 TG4Globals::Exception(
34 "Attempt to copy TG4SDServices singleton.");
38 //_____________________________________________________________________________
39 TG4SDServices::~TG4SDServices(){
45 //_____________________________________________________________________________
46 TG4SDServices& TG4SDServices::operator=(const TG4SDServices& right)
48 // check assignement to self
49 if (this == &right) return *this;
51 TG4Globals::Exception(
52 "Attempt to assign TG4SDServices singleton.");
60 //_____________________________________________________________________________
61 void TG4SDServices::PrintStatistics(G4bool open, G4bool close) const
63 // Print G4 SD statistics
66 if (open) TG4Globals::PrintStars(true);
68 G4cout << " " << G4std::setw(5) << NofSensitiveDetectors()
69 << " sensitive detectors" << G4endl;
71 if (close) TG4Globals::PrintStars(false);
74 //_____________________________________________________________________________
75 G4int TG4SDServices::GetVolumeID(const G4String& volName) const
77 // Returns the sensitive detector identifier.
78 // !! Gives exception in case logical volume is not associated with
79 // a sensitive detector.
83 = TG4GeometryServices::Instance()->CutName(volName);
85 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
87 for (G4int i=0; i<pLVStore->size(); i++) {
88 G4LogicalVolume* lv = (*pLVStore)[i];
89 G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
91 if (sd && sd->GetName() == g4VolName)
92 return GetSensitiveDetector(sd)->GetID();
95 G4String text = "TG4SDServices::GetVolumeID: Sensitive detector ";
96 text = text + g4VolName;
97 text = text + " is not defined.\n";
98 TG4Globals::Warning(text);
103 //_____________________________________________________________________________
104 G4int TG4SDServices::GetVolumeID(G4LogicalVolume* logicalVolume) const
106 // Returns the sensitive detector ID of the specified
110 G4VSensitiveDetector* sd
111 = logicalVolume->GetSensitiveDetector();
113 if (sd) return GetSensitiveDetector(sd)->GetID();
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);
124 //_____________________________________________________________________________
125 G4String TG4SDServices::GetVolumeName(G4int volumeId) const
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.
132 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
134 for (G4int i=0; i<pLVStore->size(); i++) {
135 G4LogicalVolume* lv = (*pLVStore)[i];
136 G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
138 if (sd && GetSensitiveDetector(sd)->GetID() == volumeId)
139 return sd->GetName();
142 G4String text = "TG4SDServices::VolName:\n";
143 text = text + " Sensitive detector with given id is not defined. \n";
144 TG4Globals::Warning(text);
149 //_____________________________________________________________________________
150 G4LogicalVolume* TG4SDServices::GetLogicalVolume(G4int volumeId) const
152 // Finds the first logical volume with specified volumeId
153 // (sensitive detector ID) in G4LogicalVolumeStore.
156 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
158 for (G4int i=0; i<pLVStore->size(); i++) {
159 G4LogicalVolume* lv = (*pLVStore)[i];
160 if (GetVolumeID(lv) == volumeId) return lv;
163 G4String text = "TG4SDServices::GetLogicalVolume: \n";
164 text = text + " Logical volume with given ID does not exist.";
169 //_____________________________________________________________________________
170 Int_t TG4SDServices::GetMediumId(G4int volumeId) const
172 // Return the material number for a given volume id
175 return TG4GeometryServices::Instance()
176 ->GetMediumId(GetLogicalVolume(volumeId));
180 //_____________________________________________________________________________
181 Int_t TG4SDServices::NofSensitiveDetectors() const
183 // Returns the total number of sensitive detectors.
186 return TG4VSensitiveDetector::GetTotalNofSensitiveDetectors();
189 //_____________________________________________________________________________
190 TG4VSensitiveDetector* TG4SDServices::GetSensitiveDetector(
191 G4VSensitiveDetector* sd) const
193 // Checks and converts type of the sensitive detector.
198 TG4VSensitiveDetector* tsd = dynamic_cast<TG4VSensitiveDetector*>(sd);
201 TG4Globals::Exception(
202 "TG4SDServices::GetSensitiveDetector: Wrong sensitive detector type.");