Geant4_10
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 77002 2013-11-20 11:22:35Z 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"
38 #include <assert.h>
39 
41 {;}
42 
44 {;}
45 
47 {
48  std::vector<G4VUserParallelWorld*>::iterator pwItr;
49  for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++)
50  {
51  if((*pwItr)->GetName()==aPW->GetName())
52  {
53  G4String eM = "A parallel world <";
54  eM += aPW->GetName();
55  eM += "> is already registered to the user detector construction.";
56  G4Exception("G4VUserDetectorConstruction::RegisterParallelWorld",
57  "Run0051",FatalErrorInArgument,eM);
58  }
59  }
60  parallelWorld.push_back(aPW);
61 }
62 
64 {
65  G4int nP = 0;
66  std::vector<G4VUserParallelWorld*>::iterator pwItr;
67  for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++)
68  {
69  (*pwItr)->Construct();
70  nP++;
71  }
72  return nP;
73 }
74 
76 {
77  std::vector<G4VUserParallelWorld*>::iterator pwItr;
78  for(pwItr=parallelWorld.begin();pwItr!=parallelWorld.end();pwItr++)
79  { (*pwItr)->ConstructSD(); }
80 }
81 
83 { return parallelWorld.size(); }
84 
86 {
87  if(i<0||i>=GetNumberOfParallelWorld()) return 0;
88  return parallelWorld[i];
89 }
90 
91 #include "G4RunManager.hh"
92 
94 {
95 // G4RunManager::RMType rmtype = G4RunManager::GetRunManager()->GetRunManagerType();
96 // if(rmtype != G4RunManager::sequentialRM)
97 // {
98 // G4cout
99 // << "User-derived detector construction class does not implement \n"
100 // << "ConstructSDandFiled method: i.e. workers will not have SD and fields!\n"
101 // << "The user can safely ignore this message if (s)he has no sensitive\n"
102 // << "detector or field in her/his application." << G4endl;
103 // }
104 }
105 
106 #include <map>
108 {
109  typedef std::map<G4FieldManager*,G4FieldManager*> FMtoFMmap;
110  typedef std::pair<G4FieldManager*,G4FieldManager*> FMpair;
111  FMtoFMmap masterToWorker;
113  assert( logVolStore != NULL );
114  for ( G4LogicalVolumeStore::const_iterator it = logVolStore->begin() ; it != logVolStore->end() ; ++it )
115  {
116  G4LogicalVolume *g4LogicalVolume = *it;
117  //Use shadow of master to get instance of FM
118  G4FieldManager* masterFM = 0;//g4LogicalVolume->fFieldManager;
119  G4FieldManager* clonedFM = 0;
120  if ( masterFM )
121  {
122  FMtoFMmap::iterator fmFound = masterToWorker.find(masterFM);
123  if ( fmFound == masterToWorker.end() )
124  {
125  //First time we see this SD, let's clone and remember...
126  try {
127  std::pair<FMtoFMmap::iterator,bool> insertedEl = masterToWorker.insert( FMpair(masterFM, masterFM->Clone()) );
128  clonedFM = (insertedEl.first)->second;
129  }
130  catch (...)
131  {
133  msg << "Cloning of G4FieldManager failed."
134  << " But derived class does not implement cloning. Cannot continue.";
135  G4Exception("G4VUserDetectorConstruction::CloneSD", "Run0053", FatalException,msg);
136 
137  }
138  }
139  else
140  {
141  // We have already seen this SD attached to a fifferent LogicalVolume, let's re-use previous clone
142  clonedFM = (*fmFound).second;
143  }
144  }// masterFM != 0
145  //Note that we do not push FM to doughters (false argument), however, since we area looping on all
146  //logical volumes and we implemented the "trick" of the map master<->cloned the final
147  //effect is the same as using here the correct boolean flag: log-volumes that originally were sharing
148  //the same FM they will have cloned ones
149  g4LogicalVolume->SetFieldManager(clonedFM, false);
150  }
151 }
152 
154 {
155  //Loop on ALL logial volumes to search for attached SD
157  assert( logVolStore != NULL );
158 
159  typedef std::map<G4VSensitiveDetector*,G4VSensitiveDetector*> SDtoSDmap;
160  typedef std::pair<G4VSensitiveDetector*,G4VSensitiveDetector*> SDpair;
161  SDtoSDmap masterToWorker;
162 
163  for ( G4LogicalVolumeStore::const_iterator it = logVolStore->begin() ; it != logVolStore->end() ; ++it )
164  {
165  G4LogicalVolume *g4LogicalVolume = *it;
166  //Use shadow of master to get the instance of SD
167  G4VSensitiveDetector* masterSD = 0;//g4LogicalVolume->fSensitiveDetector;
168  G4VSensitiveDetector* clonedSD = 0;
169  if ( masterSD )
170  {
171  SDtoSDmap::iterator sdFound = masterToWorker.find(masterSD);
172  if ( sdFound == masterToWorker.end() )
173  {
174  //First time we see this SD, let's clone and remember...
175  try {
176  std::pair<SDtoSDmap::iterator,bool> insertedEl = masterToWorker.insert( SDpair(masterSD,masterSD->Clone()) );
177  clonedSD = (insertedEl.first)->second;
178  }
179  catch (...)
180  {
182  msg << "Cloning of G4VSensitiveDetector requested for:" << masterSD->GetName() << "\n"
183 #ifndef WIN32
184  << " (full path name: " << masterSD->GetFullPathName() << ").\n"
185 #endif
186  << " But derived class does not implement cloning. Cannot continue.";
187  G4Exception("G4VUserDetectorConstruction::CloneSD", "Run0053", FatalException,msg);
188  }
189 
190  }
191  else
192  {
193  // We have already seen this SD attached to a fifferent LogicalVolume, let's re-use previous clone
194  clonedSD = (*sdFound).second;
195 
196  }
197  }// masterSD!=0
198  g4LogicalVolume->SetSensitiveDetector(clonedSD);
199 
200  }
201 }
202 
204 (const G4String& logVolName, G4VSensitiveDetector* aSD, G4bool multi)
205 {
206  G4bool found = false;
208  for(G4LogicalVolumeStore::iterator pos=store->begin(); pos!=store->end(); pos++)
209  {
210  if((*pos)->GetName()==logVolName)
211  {
212  if(found && !multi)
213  {
214  G4String eM = "More than one logical volumes of the name <";
215  eM += (*pos)->GetName();
216  eM += "> are found and thus the sensitive detector <";
217  eM += aSD->GetName();
218  eM += "> cannot be uniquely assigned.";
219  G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector",
220  "Run0052",FatalErrorInArgument,eM);
221  }
222  found = true;
223  SetSensitiveDetector(*pos,aSD);
224  }
225  }
226  if(!found)
227  {
228  G4String eM2 = "No logical volume of the name <";
229  eM2 += logVolName;
230  eM2 += "> is found. The specified sensitive detector <";
231  eM2 += aSD->GetName();
232  eM2 += "> couldn't be assigned to any volume.";
233  G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector",
234  "Run0053",FatalErrorInArgument,eM2);
235  }
236 }
237 
240 {
242  logVol->SetSensitiveDetector(aSD);
243 }
virtual G4FieldManager * Clone() const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
#define assert(x)
Definition: mymalloc.cc:1309
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:67
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
void SetSensitiveDetector(G4VSensitiveDetector *pSDetector)