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>
16 TG4SDServices* TG4SDServices::fgInstance = 0;
18 //_____________________________________________________________________________
19 TG4SDServices::TG4SDServices(){
22 TG4Globals::Exception(
23 "TG4SDServices: attempt to create two instances of singleton.");
29 //_____________________________________________________________________________
30 TG4SDServices::TG4SDServices(const TG4SDServices& right) {
32 TG4Globals::Exception(
33 "Attempt to copy TG4SDServices singleton.");
37 //_____________________________________________________________________________
38 TG4SDServices::~TG4SDServices(){
44 //_____________________________________________________________________________
45 TG4SDServices& TG4SDServices::operator=(const TG4SDServices& right)
47 // check assignement to self
48 if (this == &right) return *this;
50 TG4Globals::Exception(
51 "Attempt to assign TG4SDServices singleton.");
59 //_____________________________________________________________________________
60 void TG4SDServices::PrintStatistics(G4bool open, G4bool close) const
62 // Print G4 SD statistics
65 if (open) TG4Globals::PrintStars(true);
67 G4cout << " " << NofSensitiveDetectors()
68 << " sensitive detectors" << G4endl;
70 if (close) TG4Globals::PrintStars(false);
73 //_____________________________________________________________________________
74 G4int TG4SDServices::GetVolumeID(const G4String& volName) const
76 // Returns the sensitive detector identifier.
77 // !! Gives exception in case logical volume is not associated with
78 // a sensitive detector.
82 = TG4GeometryServices::Instance()->CutName(volName);
84 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
86 for (G4int i=0; i<pLVStore->size(); i++) {
87 G4LogicalVolume* lv = (*pLVStore)[i];
88 G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
90 if (sd && sd->GetName() == g4VolName)
91 return GetSensitiveDetector(sd)->GetID();
94 G4String text = "TG4SDServices::GetVolumeID: Sensitive detector ";
95 text = text + g4VolName;
96 text = text + " is not defined.\n";
97 TG4Globals::Warning(text);
102 //_____________________________________________________________________________
103 G4int TG4SDServices::GetVolumeID(G4LogicalVolume* logicalVolume) const
105 // Returns the sensitive detector ID of the specified
109 G4VSensitiveDetector* sd
110 = logicalVolume->GetSensitiveDetector();
112 if (sd) return GetSensitiveDetector(sd)->GetID();
114 G4String text = "TG4SDServices::GetVolumeID: \n";
115 text = text + " Volume " + logicalVolume->GetName();
116 text = text + " has not a sensitive detector.";
117 //TG4Globals::Exception(text);
118 TG4Globals::Warning(text);
123 //_____________________________________________________________________________
124 G4String TG4SDServices::GetVolumeName(G4int volumeId) const
126 // Returns the name of the sensitive detector with the given identifier.
127 // Gives a warning in case logical volume is not associated with
128 // a sensitive detector.
131 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
133 for (G4int i=0; i<pLVStore->size(); i++) {
134 G4LogicalVolume* lv = (*pLVStore)[i];
135 G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
137 if (sd && GetSensitiveDetector(sd)->GetID() == volumeId)
138 return sd->GetName();
141 G4String text = "TG4SDServices::VolName:\n";
142 text = text + " Sensitive detector with given id is not defined. \n";
143 TG4Globals::Warning(text);
148 //_____________________________________________________________________________
149 G4LogicalVolume* TG4SDServices::GetLogicalVolume(G4int volumeId) const
151 // Finds the first logical volume with specified volumeId
152 // (sensitive detector ID) in G4LogicalVolumeStore.
155 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
157 for (G4int i=0; i<pLVStore->size(); i++) {
158 G4LogicalVolume* lv = (*pLVStore)[i];
159 if (GetVolumeID(lv) == volumeId) return lv;
162 G4String text = "TG4SDServices::GetLogicalVolume: \n";
163 text = text + " Logical volume with given ID does not exist.";
168 //_____________________________________________________________________________
169 Int_t TG4SDServices::GetMediumId(G4int volumeId) const
171 // Return the material number for a given volume id
174 return TG4GeometryServices::Instance()
175 ->GetMediumId(GetLogicalVolume(volumeId));
179 //_____________________________________________________________________________
180 Int_t TG4SDServices::NofSensitiveDetectors() const
182 // Returns the total number of sensitive detectors.
185 return TG4VSensitiveDetector::GetTotalNofSensitiveDetectors();
188 //_____________________________________________________________________________
189 TG4VSensitiveDetector* TG4SDServices::GetSensitiveDetector(
190 G4VSensitiveDetector* sd) const
192 // Checks and converts type of the sensitive detector.
197 TG4VSensitiveDetector* tsd = dynamic_cast<TG4VSensitiveDetector*>(sd);
200 TG4Globals::Exception(
201 "TG4SDServices::GetSensitiveDetector: Wrong sensitive detector type.");