Geant4 10.7.0
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
36
37G4ThreadLocal G4SDManager* G4SDManager::fSDManager = nullptr;
38
40{
41 if(!fSDManager)
42 {
43 fSDManager = new G4SDManager;
44 }
45 return fSDManager;
46}
47
49{ return fSDManager; }
50
51G4SDManager::G4SDManager():verboseLevel(0)
52{
53 G4String topName = "/";
54 treeTop = new G4SDStructure(topName);
55 theMessenger = new G4SDmessenger(this);
56 HCtable = new G4HCtable;
57}
58
60{
61 delete theMessenger;
62 delete HCtable;
63 delete treeTop;
64 DestroyFilters();
65 theMessenger = nullptr;
66 HCtable = nullptr;
67 treeTop = nullptr;
68 fSDManager = nullptr;
69}
70
72{
73 G4int numberOfCollections = aSD->GetNumberOfCollections();
74 G4String pathName = aSD->GetPathName();
75 if( pathName(0) != '/' ) pathName.prepend("/");
76 if( pathName(pathName.length()-1) != '/' ) pathName += "/";
77 treeTop->AddNewDetector(aSD,pathName);
78 if(numberOfCollections<1) return;
79 for(G4int i=0;i<numberOfCollections;i++)
80 {
81 G4String SDname = aSD->GetName();
82 G4String DCname = aSD->GetCollectionName(i);
83 AddNewCollection(SDname,DCname);
84 }
85 if( verboseLevel > 0 )
86 {
87 G4cout << "New sensitive detector <" << aSD->GetName()
88 << "> is registered at " << pathName << G4endl;
89 }
90}
91
93{
94 G4int i = HCtable->Registor(SDname,DCname);
95 if(verboseLevel>0)
96 {
97 if(i<0) {
98 if(verboseLevel>1) G4cout << "G4SDManager::AddNewCollection : the collection <"
99 << SDname << "/" << DCname << "> has already been reginstered." << G4endl;
100 }
101 else
102 {
103 G4cout << "G4SDManager::AddNewCollection : the collection <"
104 << SDname << "/" << DCname << "> is registered at " << i << G4endl;
105 }
106 }
107}
108
110{
111 G4HCofThisEvent* HCE = new G4HCofThisEvent(HCtable->entries());
112 treeTop->Initialize(HCE);
113 return HCE;
114}
115
117{
118 treeTop->Terminate(HCE);
119}
120
121void G4SDManager::Activate(G4String dName, G4bool activeFlag)
122{
123 G4String pathName = dName;
124 if( pathName(0) != '/' ) pathName.prepend("/");
125 treeTop->Activate(pathName,activeFlag);
126}
127
129{
130 G4String pathName = dName;
131 if( pathName(0) != '/' ) pathName.prepend("/");
132 return treeTop->FindSensitiveDetector(pathName, warning);
133}
134
136{
137 G4int id = HCtable->GetCollectionID(colName);
138 if(id==-1)
139 { G4cout << "<" << colName << "> is not found." << G4endl; }
140 else if(id==-2)
141 { G4cout << "<" << colName << "> is ambiguous." << G4endl; }
142 return id;
143}
144
146{
147 G4String HCname = aHC->GetSDname();
148 HCname += "/";
149 HCname += aHC->GetName();
150 return GetCollectionID(HCname);
151}
152
154{
155 FilterList.push_back(filter);
156}
157
159{
160 for(auto f = FilterList.begin(); f != FilterList.end(); f++)
161 {
162 if(*f == filter)
163 {
164 FilterList.erase(f);
165 break;
166 }
167 }
168}
169
170void G4SDManager::DestroyFilters()
171{
172 auto f = FilterList.begin();
173 while( f != FilterList.end() )
174 {
175 if(verboseLevel>0) G4cout << "### deleting " << (*f)->GetName() << " " << (*f) << G4endl;
176 delete *f;
177 f = FilterList.begin();
178 }
179 FilterList.clear();
180}
181
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:45
G4int entries() const
Definition: G4HCtable.hh:65
void RegisterSDFilter(G4VSDFilter *filter)
Definition: G4SDManager.cc:153
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:39
void TerminateCurrentEvent(G4HCofThisEvent *HCE)
Definition: G4SDManager.cc:116
G4HCofThisEvent * PrepareNewEvent()
Definition: G4SDManager.cc:109
void Activate(G4String dName, G4bool activeFlag)
Definition: G4SDManager.cc:121
void AddNewDetector(G4VSensitiveDetector *aSD)
Definition: G4SDManager.cc:71
void DeRegisterSDFilter(G4VSDFilter *filter)
Definition: G4SDManager.cc:158
G4int GetCollectionID(G4String colName)
Definition: G4SDManager.cc:135
G4VSensitiveDetector * FindSensitiveDetector(G4String dName, G4bool warning=true)
Definition: G4SDManager.cc:128
static G4SDManager * GetSDMpointerIfExist()
Definition: G4SDManager.cc:48
void AddNewCollection(G4String SDname, G4String DCname)
Definition: G4SDManager.cc:92
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)
G4String & prepend(const char *)
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