Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 //
27 // $Id: G4VUserDetectorConstruction.cc 101679 2016-11-21 09:30:00Z gcosmo $
28 //
29 
31 #include "G4VPhysicalVolume.hh"
32 #include "G4VUserParallelWorld.hh"
33 #include "G4LogicalVolume.hh"
34 #include "G4LogicalVolumeStore.hh"
35 #include "G4VSensitiveDetector.hh"
36 #include "G4FieldManager.hh"
37 #include "G4SDManager.hh"
39 #include <assert.h>
40 #include <sstream>
41 
43 {;}
44 
46 {;}
47 
49 {
50  std::vector<G4VUserParallelWorld*>::iterator pwItr;
51  for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++)
52  {
53  if((*pwItr)->GetName()==aPW->GetName())
54  {
55  G4String eM = "A parallel world <";
56  eM += aPW->GetName();
57  eM += "> is already registered to the user detector construction.";
58  G4Exception("G4VUserDetectorConstruction::RegisterParallelWorld",
59  "Run0051",FatalErrorInArgument,eM);
60  }
61  }
62  parallelWorld.push_back(aPW);
63 }
64 
66 {
67  G4int nP = 0;
68  std::vector<G4VUserParallelWorld*>::iterator pwItr;
69  for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++)
70  {
71  (*pwItr)->Construct();
72  nP++;
73  }
74  return nP;
75 }
76 
78 {
79  std::vector<G4VUserParallelWorld*>::iterator pwItr;
80  for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++)
81  { (*pwItr)->ConstructSD(); }
82 }
83 
85 { return parallelWorld.size(); }
86 
88 {
89  if(i<0||i>=GetNumberOfParallelWorld()) return 0;
90  return parallelWorld[i];
91 }
92 
93 #include "G4RunManager.hh"
94 
96 {
97 // G4RunManager::RMType rmtype = G4RunManager::GetRunManager()->GetRunManagerType();
98 // if(rmtype != G4RunManager::sequentialRM)
99 // {
100 // G4cout
101 // << "User-derived detector construction class does not implement \n"
102 // << "ConstructSDandFiled method: i.e. workers will not have SD and fields!\n"
103 // << "The user can safely ignore this message if (s)he has no sensitive\n"
104 // << "detector or field in her/his application." << G4endl;
105 // }
106 }
107 
108 #include <map>
110 {
111  typedef std::map<G4FieldManager*,G4FieldManager*> FMtoFMmap;
112  typedef std::pair<G4FieldManager*,G4FieldManager*> FMpair;
113  FMtoFMmap masterToWorker;
115  assert( logVolStore != NULL );
116  for ( G4LogicalVolumeStore::const_iterator it = logVolStore->begin() ; it != logVolStore->end() ; ++it )
117  {
118  G4LogicalVolume *g4LogicalVolume = *it;
119  //Use shadow of master to get instance of FM
120  G4FieldManager* masterFM = 0;//g4LogicalVolume->fFieldManager;
121  G4FieldManager* clonedFM = 0;
122  if ( masterFM )
123  {
124  FMtoFMmap::iterator fmFound = masterToWorker.find(masterFM);
125  if ( fmFound == masterToWorker.end() )
126  {
127  //First time we see this SD, let's clone and remember...
128  try {
129  std::pair<FMtoFMmap::iterator,bool> insertedEl = masterToWorker.insert( FMpair(masterFM, masterFM->Clone()) );
130  clonedFM = (insertedEl.first)->second;
131  }
132  catch (...)
133  {
135  msg << "Cloning of G4FieldManager failed."
136  << " But derived class does not implement cloning. Cannot continue.";
137  G4Exception("G4VUserDetectorConstruction::CloneSD", "Run0053", FatalException,msg);
138 
139  }
140  }
141  else
142  {
143  // We have already seen this SD attached to a fifferent LogicalVolume, let's re-use previous clone
144  clonedFM = (*fmFound).second;
145  }
146  }// masterFM != 0
147  //Note that we do not push FM to doughters (false argument), however, since we area looping on all
148  //logical volumes and we implemented the "trick" of the map master<->cloned the final
149  //effect is the same as using here the correct boolean flag: log-volumes that originally were sharing
150  //the same FM they will have cloned ones
151  g4LogicalVolume->SetFieldManager(clonedFM, false);
152  }
153 }
154 
156 {
157  //Loop on ALL logial volumes to search for attached SD
159  assert( logVolStore != NULL );
160 
161  typedef std::map<G4VSensitiveDetector*,G4VSensitiveDetector*> SDtoSDmap;
162  typedef std::pair<G4VSensitiveDetector*,G4VSensitiveDetector*> SDpair;
163  SDtoSDmap masterToWorker;
164 
165  for ( G4LogicalVolumeStore::const_iterator it = logVolStore->begin() ; it != logVolStore->end() ; ++it )
166  {
167  G4LogicalVolume *g4LogicalVolume = *it;
168  //Use shadow of master to get the instance of SD
169  G4VSensitiveDetector* masterSD = 0;//g4LogicalVolume->fSensitiveDetector;
170  G4VSensitiveDetector* clonedSD = 0;
171  if ( masterSD )
172  {
173  SDtoSDmap::iterator sdFound = masterToWorker.find(masterSD);
174  if ( sdFound == masterToWorker.end() )
175  {
176  //First time we see this SD, let's clone and remember...
177  try {
178  std::pair<SDtoSDmap::iterator,bool> insertedEl = masterToWorker.insert( SDpair(masterSD,masterSD->Clone()) );
179  clonedSD = (insertedEl.first)->second;
180  }
181  catch (...)
182  {
184  msg << "Cloning of G4VSensitiveDetector requested for:" << masterSD->GetName() << "\n"
185 #ifndef WIN32
186  << " (full path name: " << masterSD->GetFullPathName() << ").\n"
187 #endif
188  << " But derived class does not implement cloning. Cannot continue.";
189  G4Exception("G4VUserDetectorConstruction::CloneSD", "Run0053", FatalException,msg);
190  }
191 
192  }
193  else
194  {
195  // We have already seen this SD attached to a fifferent LogicalVolume, let's re-use previous clone
196  clonedSD = (*sdFound).second;
197 
198  }
199  }// masterSD!=0
200  g4LogicalVolume->SetSensitiveDetector(clonedSD);
201 
202  }
203 }
204 
206 (const G4String& logVolName, G4VSensitiveDetector* aSD, G4bool multi)
207 {
208  G4bool found = false;
210  for(G4LogicalVolumeStore::iterator pos=store->begin(); pos!=store->end(); pos++)
211  {
212  if((*pos)->GetName()==logVolName)
213  {
214  if(found && !multi)
215  {
216  G4String eM = "More than one logical volumes of the name <";
217  eM += (*pos)->GetName();
218  eM += "> are found and thus the sensitive detector <";
219  eM += aSD->GetName();
220  eM += "> cannot be uniquely assigned.";
221  G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector",
222  "Run0052",FatalErrorInArgument,eM);
223  }
224  found = true;
225  SetSensitiveDetector(*pos,aSD);
226  }
227  }
228  if(!found)
229  {
230  G4String eM2 = "No logical volume of the name <";
231  eM2 += logVolName;
232  eM2 += "> is found. The specified sensitive detector <";
233  eM2 += aSD->GetName();
234  eM2 += "> couldn't be assigned to any volume.";
235  G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector",
236  "Run0053",FatalErrorInArgument,eM2);
237  }
238 }
239 
242 {
243  assert(logVol!=nullptr&&aSD!=nullptr);
244 
245  //The aSD has already been added by user to the manager if needed
246  //G4SDManager::GetSDMpointer()->AddNewDetector(aSD);
247 
248  //New Logic: allow for "multiple" SDs being attached to a single LV.
249  //To do that we use a special proxy SD called G4MultiSensitiveDetector
250 
251  //Get existing SD if already set and check if it is of the special type
252  G4VSensitiveDetector* originalSD = logVol->GetSensitiveDetector();
253  if ( originalSD == nullptr ) {
254  logVol->SetSensitiveDetector(aSD);
255  } else {
256  G4MultiSensitiveDetector* msd = dynamic_cast<G4MultiSensitiveDetector*>(originalSD);
257  if ( msd != nullptr ) {
258  msd->AddSD(aSD);
259  } else {
260  std::ostringstream mn;
261  mn<<"/MultiSD_"<<logVol->GetName()<<"_"<<logVol;
262  const G4String msdname = mn.str();
263  msd = new G4MultiSensitiveDetector(msdname);
264  //We need to register the proxy to have correct handling of IDs
266  msd->AddSD(originalSD);
267  msd->AddSD(aSD);
268  logVol->SetSensitiveDetector(msd);
269  }
270  }
271 }
virtual G4FieldManager * Clone() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
static constexpr double second
Definition: G4SIunits.hh:157
void AddSD(G4VSensitiveDetector *sd)
int G4int
Definition: G4Types.hh:78
void SetFieldManager(G4FieldManager *pFieldMgr, G4bool forceToAllDaughters)
void RegisterParallelWorld(G4VUserParallelWorld *)
virtual G4VSensitiveDetector * Clone() const
bool G4bool
Definition: G4Types.hh:79
static G4LogicalVolumeStore * GetInstance()
G4VUserParallelWorld * GetParallelWorld(G4int i) const
G4String GetFullPathName() const
void SetSensitiveDetector(const G4String &logVolName, G4VSensitiveDetector *aSD, G4bool multi=false)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void AddNewDetector(G4VSensitiveDetector *aSD)
Definition: G4SDManager.cc:71
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
const G4String & GetName() const
G4VSensitiveDetector * GetSensitiveDetector() const
void SetSensitiveDetector(G4VSensitiveDetector *pSDetector)
static const G4double pos