Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4SDManager.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27//
28
29#include "G4SDManager.hh"
30#include "G4SDmessenger.hh"
31#include "G4HCofThisEvent.hh"
32#include "G4VHitsCollection.hh"
34#include "G4ios.hh"
35
36G4ThreadLocal G4SDManager* G4SDManager::fSDManager = nullptr;
37
39{
40 if(!fSDManager)
41 {
42 fSDManager = new G4SDManager;
43 }
44 return fSDManager;
45}
46
48
50 : verboseLevel(0)
51{
52 G4String topName = "/";
53 treeTop = new G4SDStructure(topName);
54 theMessenger = new G4SDmessenger(this);
55 HCtable = new G4HCtable;
56}
57
59{
60 delete theMessenger;
61 delete HCtable;
62 delete treeTop;
63 DestroyFilters();
64 theMessenger = nullptr;
65 HCtable = nullptr;
66 treeTop = nullptr;
67 fSDManager = nullptr;
68}
69
71{
72 G4int numberOfCollections = aSD->GetNumberOfCollections();
73 G4String pathName = aSD->GetPathName();
74 if(pathName[0] != '/')
75 pathName.insert(0, "/");
76 if(pathName.back() != '/')
77 pathName += "/";
78 treeTop->AddNewDetector(aSD, pathName);
79 if(numberOfCollections < 1)
80 return;
81 for(G4int i = 0; i < numberOfCollections; i++)
82 {
83 G4String SDname = aSD->GetName();
84 G4String DCname = aSD->GetCollectionName(i);
85 AddNewCollection(SDname, DCname);
86 }
87 if(verboseLevel > 0)
88 {
89 G4cout << "New sensitive detector <" << aSD->GetName()
90 << "> is registered at " << pathName << G4endl;
91 }
92}
93
95{
96 G4int i = HCtable->Registor(SDname, DCname);
97 if(verboseLevel > 0)
98 {
99 if(i < 0)
100 {
101 if(verboseLevel > 1)
102 G4cout << "G4SDManager::AddNewCollection : the collection <" << SDname
103 << "/" << DCname << "> has already been reginstered." << G4endl;
104 }
105 else
106 {
107 G4cout << "G4SDManager::AddNewCollection : the collection <" << SDname
108 << "/" << DCname << "> is registered at " << i << G4endl;
109 }
110 }
111}
112
114{
115 G4HCofThisEvent* HCE = new G4HCofThisEvent(HCtable->entries());
116 treeTop->Initialize(HCE);
117 return HCE;
118}
119
121{
122 treeTop->Terminate(HCE);
123}
124
125void G4SDManager::Activate(G4String dName, G4bool activeFlag)
126{
127 G4String pathName = dName;
128 if(pathName[0] != '/')
129 pathName.insert(0, "/");
130 treeTop->Activate(pathName, activeFlag);
131}
132
134 G4bool warning)
135{
136 G4String pathName = dName;
137 if(pathName[0] != '/')
138 pathName.insert(0, "/");
139 return treeTop->FindSensitiveDetector(pathName, warning);
140}
141
143{
144 G4int id = HCtable->GetCollectionID(colName);
145 if(id == -1)
146 {
147 G4cout << "<" << colName << "> is not found." << G4endl;
148 }
149 else if(id == -2)
150 {
151 G4cout << "<" << colName << "> is ambiguous." << G4endl;
152 }
153 return id;
154}
155
157{
158 G4String HCname = aHC->GetSDname();
159 HCname += "/";
160 HCname += aHC->GetName();
161 return GetCollectionID(HCname);
162}
163
165{
166 FilterList.push_back(filter);
167}
168
170{
171 for(auto f = FilterList.begin(); f != FilterList.end(); f++)
172 {
173 if(*f == filter)
174 {
175 FilterList.erase(f);
176 break;
177 }
178 }
179}
180
181void G4SDManager::DestroyFilters()
182{
183 auto f = FilterList.begin();
184 while(f != FilterList.end())
185 {
186 if(verboseLevel > 0)
187 G4cout << "### deleting " << (*f)->GetName() << " " << (*f) << G4endl;
188 delete *f;
189 f = FilterList.begin();
190 }
191 FilterList.clear();
192}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
G4int Registor(G4String SDname, G4String HCname)
Definition: G4HCtable.cc:36
G4int GetCollectionID(G4String HCname) const
Definition: G4HCtable.cc:48
G4int entries() const
Definition: G4HCtable.hh:65
void RegisterSDFilter(G4VSDFilter *filter)
Definition: G4SDManager.cc:164
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:38
void TerminateCurrentEvent(G4HCofThisEvent *HCE)
Definition: G4SDManager.cc:120
G4HCofThisEvent * PrepareNewEvent()
Definition: G4SDManager.cc:113
void Activate(G4String dName, G4bool activeFlag)
Definition: G4SDManager.cc:125
void AddNewDetector(G4VSensitiveDetector *aSD)
Definition: G4SDManager.cc:70
void DeRegisterSDFilter(G4VSDFilter *filter)
Definition: G4SDManager.cc:169
G4int GetCollectionID(G4String colName)
Definition: G4SDManager.cc:142
G4VSensitiveDetector * FindSensitiveDetector(G4String dName, G4bool warning=true)
Definition: G4SDManager.cc:133
static G4SDManager * GetSDMpointerIfExist()
Definition: G4SDManager.cc:47
void AddNewCollection(G4String SDname, G4String DCname)
Definition: G4SDManager.cc:94
void Initialize(G4HCofThisEvent *HCE)
void Activate(const G4String &aName, G4bool sensitiveFlag)
void Terminate(G4HCofThisEvent *HCE)
G4VSensitiveDetector * FindSensitiveDetector(const G4String &aName, G4bool warning=true)
void AddNewDetector(G4VSensitiveDetector *aSD, const G4String &treeStructure)
const G4String & GetSDname() const
const G4String & GetName() const
G4int GetNumberOfCollections() const
G4String GetCollectionName(G4int id) const
G4String GetPathName() const
#define G4ThreadLocal
Definition: tls.hh:77