Geant4-11
G4VUserDetectorConstruction.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// G4VUserDetectorConstruction implementation
27//
28// Original author: M.Asai, 1999
29// --------------------------------------------------------------------
30
31#include <sstream>
32#include <map>
33#include <assert.h>
34
36#include "G4FieldManager.hh"
37#include "G4LogicalVolume.hh"
40#include "G4SDManager.hh"
41#include "G4VPhysicalVolume.hh"
44#include "G4RunManager.hh"
45
46// --------------------------------------------------------------------
48{
49}
50
51// --------------------------------------------------------------------
53{
54}
55
56// --------------------------------------------------------------------
59{
60 auto pwItr = std::find(parallelWorld.cbegin(), parallelWorld.cend(), aPW);
61 if (pwItr != parallelWorld.cend())
62 {
63 G4String eM = "A parallel world <";
64 eM += aPW->GetName();
65 eM += "> is already registered to the user detector construction.";
66 G4Exception("G4VUserDetectorConstruction::RegisterParallelWorld",
67 "Run0051", FatalErrorInArgument, eM);
68 }
69 parallelWorld.push_back(aPW);
70}
71
72// --------------------------------------------------------------------
74{
75 G4int nP = 0;
76 for(auto pwItr = parallelWorld.cbegin();
77 pwItr != parallelWorld.cend(); ++pwItr)
78 {
79 (*pwItr)->Construct();
80 ++nP;
81 }
82 return nP;
83}
84
85// --------------------------------------------------------------------
87{
88 for(auto pwItr = parallelWorld.cbegin();
89 pwItr != parallelWorld.cend(); ++pwItr)
90 {
91 (*pwItr)->ConstructSD();
92 }
93}
94
95// --------------------------------------------------------------------
97{
98 return parallelWorld.size();
99}
100
101// --------------------------------------------------------------------
104{
105 if(i < 0 || i >= GetNumberOfParallelWorld())
106 return nullptr;
107 return parallelWorld[i];
108}
109
110// --------------------------------------------------------------------
112{
113 // G4RunManager::RMType rmtype =
114 // G4RunManager::GetRunManager()->GetRunManagerType(); if(rmtype !=
115 // G4RunManager::sequentialRM)
116 // {
117 // G4cout
118 // << "User-derived detector construction class does not implement \n"
119 // << "ConstructSDandFiled method: workers will not have SD and fields!\n"
120 // << "The user can safely ignore this message if (s)he has no sensitive\n"
121 // << "detector or field in her/his application." << G4endl;
122 // }
123}
124
125// --------------------------------------------------------------------
127{
128 using FMtoFMmap = std::map<G4FieldManager*, G4FieldManager*>;
129 using FMpair = std::pair<G4FieldManager*, G4FieldManager*>;
130
131 FMtoFMmap masterToWorker;
133 for(auto it = logVolStore->cbegin(); it != logVolStore->cend(); ++it)
134 {
135 G4LogicalVolume* g4LogicalVolume = *it;
136 // Use shadow of master to get instance of FM
137 G4FieldManager* masterFM = nullptr; // g4LogicalVolume->fFieldManager;
138 G4FieldManager* clonedFM = nullptr;
139 if(masterFM != nullptr)
140 {
141 auto fmFound = masterToWorker.find(masterFM);
142 if(fmFound == masterToWorker.cend())
143 {
144 // First time we see this SD, let's clone and remember...
145 try
146 {
147 auto insertedEl =
148 masterToWorker.insert(FMpair(masterFM, masterFM->Clone()));
149 clonedFM = (insertedEl.first)->second;
150 } catch(...)
151 {
153 msg << "Cloning of G4FieldManager failed."
154 << " But derived class does not implement cloning. Cannot "
155 "continue.";
156 G4Exception("G4VUserDetectorConstruction::CloneSD()", "Run0053",
157 FatalException, msg);
158 }
159 }
160 else
161 {
162 // We have already seen this SD attached to a different LogicalVolume,
163 // let's re-use previous clone
164 clonedFM = (*fmFound).second;
165 }
166 } // masterFM != 0
167 // Note that we do not push FM to daughters (false argument), however, since
168 // we area looping on all logical volumes and we implemented the "trick" of
169 // the map master<->cloned the final effect is the same as using here the
170 // correct Boolean flag: log-volumes that originally were sharing the same
171 // FM they will have cloned ones
172 g4LogicalVolume->SetFieldManager(clonedFM, false);
173 }
174}
175
176// --------------------------------------------------------------------
178{
179 // Loop on ALL logial volumes to search for attached SD
181
182 using SDtoSDmap = std::map<G4VSensitiveDetector*, G4VSensitiveDetector*>;
183 using SDpair = std::pair<G4VSensitiveDetector*, G4VSensitiveDetector*>;
184 SDtoSDmap masterToWorker;
185
186 for(auto it = logVolStore->cbegin(); it != logVolStore->cend(); ++it)
187 {
188 G4LogicalVolume* g4LogicalVolume = *it;
189 // Use shadow of master to get the instance of SD
190 G4VSensitiveDetector* masterSD = nullptr;
191 G4VSensitiveDetector* clonedSD = nullptr;
192 if(masterSD != nullptr)
193 {
194 auto sdFound = masterToWorker.find(masterSD);
195 if(sdFound == masterToWorker.cend())
196 {
197 // First time we see this SD, let's clone and remember...
198 try
199 {
200 auto insertedEl =
201 masterToWorker.insert(SDpair(masterSD, masterSD->Clone()));
202 clonedSD = (insertedEl.first)->second;
203 } catch(...)
204 {
206 msg << "Cloning of G4VSensitiveDetector requested for:"
207 << masterSD->GetName() << "\n"
208#ifndef WIN32
209 << " (full path name: " << masterSD->GetFullPathName() << ").\n"
210#endif
211 << " But derived class does not implement cloning. Cannot "
212 "continue.";
213 G4Exception("G4VUserDetectorConstruction::CloneSD()", "Run0053",
214 FatalException, msg);
215 }
216 }
217 else
218 {
219 // We have already seen this SD attached to a different LogicalVolume,
220 // let's re-use previous clone
221 clonedSD = (*sdFound).second;
222 }
223 } // masterSD!=0
224 g4LogicalVolume->SetSensitiveDetector(clonedSD);
225 }
226}
227
228// --------------------------------------------------------------------
230SetSensitiveDetector(const G4String& logVolName,
231 G4VSensitiveDetector* aSD, G4bool multi)
232{
233 G4bool found = false;
235 auto volmap = store->GetMap();
236 auto pos = volmap.find(logVolName);
237 if(pos != volmap.cend())
238 {
239 if ((pos->second.size()>1) && !multi)
240 {
241 G4String eM = "More than one logical volumes of name <";
242 eM += pos->first;
243 eM += "> are found and thus the sensitive detector <";
244 eM += aSD->GetName();
245 eM += "> cannot be uniquely assigned.";
246 G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector()",
247 "Run0052", FatalErrorInArgument, eM);
248 }
249 found = true;
250 for (std::size_t i = 0; i < pos->second.size(); ++i)
251 {
252 SetSensitiveDetector(pos->second[i], aSD);
253 }
254 }
255 if(!found)
256 {
257 G4String eM2 = "No logical volume of name <";
258 eM2 += logVolName;
259 eM2 += "> is found. The specified sensitive detector <";
260 eM2 += aSD->GetName();
261 eM2 += "> couldn't be assigned to any volume.";
262 G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector()",
263 "Run0053", FatalErrorInArgument, eM2);
264 }
265}
266
267// --------------------------------------------------------------------
270{
271 assert(logVol != nullptr && aSD != nullptr);
272
273 // The aSD has already been added by user to the manager if needed
274 // G4SDManager::GetSDMpointer()->AddNewDetector(aSD);
275
276 // New Logic: allow for "multiple" SDs being attached to a single LV.
277 // To do that we use a special proxy SD called G4MultiSensitiveDetector
278
279 // Get existing SD if already set and check if it is of the special type
280 G4VSensitiveDetector* originalSD = logVol->GetSensitiveDetector();
281 if(originalSD == aSD)
282 {
284 msg << "Attempting to add multiple times the same sensitive detector (\"";
285 msg << originalSD->GetName() << "\") is not allowed, skipping.";
286 G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector", "Run0054",
287 JustWarning, msg);
288 return;
289 }
290 if(originalSD == nullptr)
291 {
292 logVol->SetSensitiveDetector(aSD);
293 }
294 else
295 {
297 dynamic_cast<G4MultiSensitiveDetector*>(originalSD);
298 if(msd != nullptr)
299 {
300 msd->AddSD(aSD);
301 }
302 else
303 {
304 std::ostringstream mn;
305 mn << "/MultiSD_" << logVol->GetName() << "_" << logVol;
306 const G4String msdname = mn.str();
307 msd = new G4MultiSensitiveDetector(msdname);
308 // We need to register the proxy to have correct handling of IDs
310 msd->AddSD(originalSD);
311 msd->AddSD(aSD);
312 logVol->SetSensitiveDetector(msd);
313 }
314 }
315}
static const G4double pos
@ JustWarning
@ FatalException
@ FatalErrorInArgument
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
static constexpr double second
Definition: G4SIunits.hh:137
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
virtual G4FieldManager * Clone() const
const std::map< G4String, std::vector< G4LogicalVolume * > > & GetMap() const
static G4LogicalVolumeStore * GetInstance()
void SetFieldManager(G4FieldManager *pFieldMgr, G4bool forceToAllDaughters)
G4VSensitiveDetector * GetSensitiveDetector() const
const G4String & GetName() const
void SetSensitiveDetector(G4VSensitiveDetector *pSDetector)
void AddSD(G4VSensitiveDetector *sd)
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:38
void AddNewDetector(G4VSensitiveDetector *aSD)
Definition: G4SDManager.cc:70
virtual G4VSensitiveDetector * Clone() const
G4String GetFullPathName() const
G4VUserParallelWorld * GetParallelWorld(G4int i) const
void RegisterParallelWorld(G4VUserParallelWorld *)
void SetSensitiveDetector(const G4String &logVolName, G4VSensitiveDetector *aSD, G4bool multi=false)
std::vector< G4VUserParallelWorld * > parallelWorld
const G4String & GetName()