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[ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /run/src/G4VUserDetectorConstruction.cc (Version 11.3.0) and /run/src/G4VUserDetectorConstruction.cc (Version 11.2.2)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 // G4VUserDetectorConstruction implementation      26 // G4VUserDetectorConstruction implementation
 27 //                                                 27 //
 28 // Original author: M.Asai, 1999                   28 // Original author: M.Asai, 1999
 29 // -------------------------------------------     29 // --------------------------------------------------------------------
 30                                                    30 
 31 #include "G4VUserDetectorConstruction.hh"          31 #include "G4VUserDetectorConstruction.hh"
 32                                                    32 
 33 #include "G4FieldManager.hh"                       33 #include "G4FieldManager.hh"
 34 #include "G4LogicalVolume.hh"                      34 #include "G4LogicalVolume.hh"
 35 #include "G4LogicalVolumeStore.hh"                 35 #include "G4LogicalVolumeStore.hh"
 36 #include "G4MultiSensitiveDetector.hh"             36 #include "G4MultiSensitiveDetector.hh"
 37 #include "G4RunManager.hh"                         37 #include "G4RunManager.hh"
 38 #include "G4SDManager.hh"                          38 #include "G4SDManager.hh"
 39 #include "G4VPhysicalVolume.hh"                    39 #include "G4VPhysicalVolume.hh"
 40 #include "G4VSensitiveDetector.hh"                 40 #include "G4VSensitiveDetector.hh"
 41 #include "G4VUserParallelWorld.hh"                 41 #include "G4VUserParallelWorld.hh"
 42                                                    42 
 43 #include <cassert>                                 43 #include <cassert>
 44 #include <map>                                     44 #include <map>
 45 #include <sstream>                                 45 #include <sstream>
 46                                                    46 
 47 // -------------------------------------------     47 // --------------------------------------------------------------------
 48 void G4VUserDetectorConstruction::RegisterPara     48 void G4VUserDetectorConstruction::RegisterParallelWorld(G4VUserParallelWorld* aPW)
 49 {                                                  49 {
 50   auto pwItr = std::find(parallelWorld.cbegin(     50   auto pwItr = std::find(parallelWorld.cbegin(), parallelWorld.cend(), aPW);
 51   if (pwItr != parallelWorld.cend()) {             51   if (pwItr != parallelWorld.cend()) {
 52     G4String eM = "A parallel world <";            52     G4String eM = "A parallel world <";
 53     eM += aPW->GetName();                          53     eM += aPW->GetName();
 54     eM += "> is already registered to the user     54     eM += "> is already registered to the user detector construction.";
 55     G4Exception("G4VUserDetectorConstruction::     55     G4Exception("G4VUserDetectorConstruction::RegisterParallelWorld", "Run0051",
 56                 FatalErrorInArgument, eM);         56                 FatalErrorInArgument, eM);
 57   }                                                57   }
 58   parallelWorld.push_back(aPW);                    58   parallelWorld.push_back(aPW);
 59 }                                                  59 }
 60                                                    60 
 61 // -------------------------------------------     61 // --------------------------------------------------------------------
 62 G4int G4VUserDetectorConstruction::ConstructPa     62 G4int G4VUserDetectorConstruction::ConstructParallelGeometries()
 63 {                                                  63 {
 64   G4int nP = 0;                                    64   G4int nP = 0;
 65   for (const auto& pwItr : parallelWorld) {        65   for (const auto& pwItr : parallelWorld) {
 66     pwItr->Construct();                            66     pwItr->Construct();
 67     ++nP;                                          67     ++nP;
 68   }                                                68   }
 69   return nP;                                       69   return nP;
 70 }                                                  70 }
 71                                                    71 
 72 // -------------------------------------------     72 // --------------------------------------------------------------------
 73 void G4VUserDetectorConstruction::ConstructPar     73 void G4VUserDetectorConstruction::ConstructParallelSD()
 74 {                                                  74 {
 75   for (const auto& pwItr : parallelWorld) {        75   for (const auto& pwItr : parallelWorld) {
 76     pwItr->ConstructSD();                          76     pwItr->ConstructSD();
 77   }                                                77   }
 78 }                                                  78 }
 79                                                    79 
 80 // -------------------------------------------     80 // --------------------------------------------------------------------
 81 G4int G4VUserDetectorConstruction::GetNumberOf     81 G4int G4VUserDetectorConstruction::GetNumberOfParallelWorld() const
 82 {                                                  82 {
 83   return (G4int)parallelWorld.size();              83   return (G4int)parallelWorld.size();
 84 }                                                  84 }
 85                                                    85 
 86 // -------------------------------------------     86 // --------------------------------------------------------------------
 87 G4VUserParallelWorld* G4VUserDetectorConstruct     87 G4VUserParallelWorld* G4VUserDetectorConstruction::GetParallelWorld(G4int i) const
 88 {                                                  88 {
 89   if (i < 0 || i >= GetNumberOfParallelWorld()     89   if (i < 0 || i >= GetNumberOfParallelWorld()) return nullptr;
 90   return parallelWorld[i];                         90   return parallelWorld[i];
 91 }                                                  91 }
 92                                                    92 
 93 // -------------------------------------------     93 // --------------------------------------------------------------------
 94 void G4VUserDetectorConstruction::ConstructSDa     94 void G4VUserDetectorConstruction::ConstructSDandField() {}
 95                                                    95 
 96 // -------------------------------------------     96 // --------------------------------------------------------------------
 97 void G4VUserDetectorConstruction::CloneF()         97 void G4VUserDetectorConstruction::CloneF()
 98 {                                                  98 {
 99   using FMtoFMmap = std::map<G4FieldManager*,      99   using FMtoFMmap = std::map<G4FieldManager*, G4FieldManager*>;
100   using FMpair = std::pair<G4FieldManager*, G4    100   using FMpair = std::pair<G4FieldManager*, G4FieldManager*>;
101                                                   101 
102   FMtoFMmap masterToWorker;                       102   FMtoFMmap masterToWorker;
103   G4LogicalVolumeStore* const logVolStore = G4    103   G4LogicalVolumeStore* const logVolStore = G4LogicalVolumeStore::GetInstance();
104   for (const auto& g4LogicalVolume : *logVolSt    104   for (const auto& g4LogicalVolume : *logVolStore) {
105     // Use shadow of master to get instance of    105     // Use shadow of master to get instance of FM
106     G4FieldManager* masterFM = nullptr;  // g4    106     G4FieldManager* masterFM = nullptr;  // g4LogicalVolume->fFieldManager;
107     G4FieldManager* clonedFM = nullptr;           107     G4FieldManager* clonedFM = nullptr;
108     if (masterFM != nullptr) {                    108     if (masterFM != nullptr) {
109       auto fmFound = masterToWorker.find(maste    109       auto fmFound = masterToWorker.find(masterFM);
110       if (fmFound == masterToWorker.cend()) {     110       if (fmFound == masterToWorker.cend()) {
111         // First time we see this SD, let's cl    111         // First time we see this SD, let's clone and remember...
112         try {                                     112         try {
113           auto insertedEl = masterToWorker.ins    113           auto insertedEl = masterToWorker.insert(FMpair(masterFM, masterFM->Clone()));
114           clonedFM = (insertedEl.first)->secon    114           clonedFM = (insertedEl.first)->second;
115         }                                         115         }
116         catch (...) {                             116         catch (...) {
117           G4ExceptionDescription msg;             117           G4ExceptionDescription msg;
118           msg << "Cloning of G4FieldManager fa    118           msg << "Cloning of G4FieldManager failed."
119               << " But derived class does not     119               << " But derived class does not implement cloning. Cannot "
120                  "continue.";                     120                  "continue.";
121           G4Exception("G4VUserDetectorConstruc    121           G4Exception("G4VUserDetectorConstruction::CloneSD()", "Run0053", FatalException, msg);
122         }                                         122         }
123       }                                           123       }
124       else {                                      124       else {
125         // We have already seen this SD attach    125         // We have already seen this SD attached to a different LogicalVolume,
126         // let's re-use previous clone            126         // let's re-use previous clone
127         clonedFM = (*fmFound).second;             127         clonedFM = (*fmFound).second;
128       }                                           128       }
129     }  // masterFM != 0                           129     }  // masterFM != 0
130     // Note that we do not push FM to daughter    130     // Note that we do not push FM to daughters (false argument), however, since
131     // we area looping on all logical volumes     131     // we area looping on all logical volumes and we implemented the "trick" of
132     // the map master<->cloned the final effec    132     // the map master<->cloned the final effect is the same as using here the
133     // correct Boolean flag: log-volumes that     133     // correct Boolean flag: log-volumes that originally were sharing the same
134     // FM they will have cloned ones              134     // FM they will have cloned ones
135     g4LogicalVolume->SetFieldManager(clonedFM,    135     g4LogicalVolume->SetFieldManager(clonedFM, false);
136   }                                               136   }
137 }                                                 137 }
138                                                   138 
139 // -------------------------------------------    139 // --------------------------------------------------------------------
140 void G4VUserDetectorConstruction::CloneSD()       140 void G4VUserDetectorConstruction::CloneSD()
141 {                                                 141 {
142   // Loop on ALL logial volumes to search for     142   // Loop on ALL logial volumes to search for attached SD
143   G4LogicalVolumeStore* const logVolStore = G4    143   G4LogicalVolumeStore* const logVolStore = G4LogicalVolumeStore::GetInstance();
144                                                   144 
145   using SDtoSDmap = std::map<G4VSensitiveDetec    145   using SDtoSDmap = std::map<G4VSensitiveDetector*, G4VSensitiveDetector*>;
146   using SDpair = std::pair<G4VSensitiveDetecto    146   using SDpair = std::pair<G4VSensitiveDetector*, G4VSensitiveDetector*>;
147   SDtoSDmap masterToWorker;                       147   SDtoSDmap masterToWorker;
148                                                   148 
149   for (const auto& g4LogicalVolume : *logVolSt    149   for (const auto& g4LogicalVolume : *logVolStore) {
150     // Use shadow of master to get the instanc    150     // Use shadow of master to get the instance of SD
151     G4VSensitiveDetector* masterSD = nullptr;     151     G4VSensitiveDetector* masterSD = nullptr;
152     G4VSensitiveDetector* clonedSD = nullptr;     152     G4VSensitiveDetector* clonedSD = nullptr;
153     if (masterSD != nullptr) {                    153     if (masterSD != nullptr) {
154       auto sdFound = masterToWorker.find(maste    154       auto sdFound = masterToWorker.find(masterSD);
155       if (sdFound == masterToWorker.cend()) {     155       if (sdFound == masterToWorker.cend()) {
156         // First time we see this SD, let's cl    156         // First time we see this SD, let's clone and remember...
157         try {                                     157         try {
158           auto insertedEl = masterToWorker.ins    158           auto insertedEl = masterToWorker.insert(SDpair(masterSD, masterSD->Clone()));
159           clonedSD = (insertedEl.first)->secon    159           clonedSD = (insertedEl.first)->second;
160         }                                         160         }
161         catch (...) {                             161         catch (...) {
162           G4ExceptionDescription msg;             162           G4ExceptionDescription msg;
163           msg << "Cloning of G4VSensitiveDetec    163           msg << "Cloning of G4VSensitiveDetector requested for:" << masterSD->GetName() << "\n"
164 #ifndef WIN32                                     164 #ifndef WIN32
165               << " (full path name: " << maste    165               << " (full path name: " << masterSD->GetFullPathName() << ").\n"
166 #endif                                            166 #endif
167               << " But derived class does not     167               << " But derived class does not implement cloning. Cannot "
168                  "continue.";                     168                  "continue.";
169           G4Exception("G4VUserDetectorConstruc    169           G4Exception("G4VUserDetectorConstruction::CloneSD()", "Run0053", FatalException, msg);
170         }                                         170         }
171       }                                           171       }
172       else {                                      172       else {
173         // We have already seen this SD attach    173         // We have already seen this SD attached to a different LogicalVolume,
174         // let's re-use previous clone            174         // let's re-use previous clone
175         clonedSD = (*sdFound).second;             175         clonedSD = (*sdFound).second;
176       }                                           176       }
177     }  // masterSD!=0                             177     }  // masterSD!=0
178     g4LogicalVolume->SetSensitiveDetector(clon    178     g4LogicalVolume->SetSensitiveDetector(clonedSD);
179   }                                               179   }
180 }                                                 180 }
181                                                   181 
182 // -------------------------------------------    182 // --------------------------------------------------------------------
183 void G4VUserDetectorConstruction::SetSensitive    183 void G4VUserDetectorConstruction::SetSensitiveDetector(const G4String& logVolName,
184                                                   184                                                        G4VSensitiveDetector* aSD, G4bool multi)
185 {                                                 185 {
186   G4bool found = false;                           186   G4bool found = false;
187   G4LogicalVolumeStore* store = G4LogicalVolum    187   G4LogicalVolumeStore* store = G4LogicalVolumeStore::GetInstance();
188   auto volmap = store->GetMap();                  188   auto volmap = store->GetMap();
189   auto pos = volmap.find(logVolName);             189   auto pos = volmap.find(logVolName);
190   if (pos != volmap.cend()) {                     190   if (pos != volmap.cend()) {
191     if ((pos->second.size() > 1) && !multi) {     191     if ((pos->second.size() > 1) && !multi) {
192       G4String eM = "More than one logical vol    192       G4String eM = "More than one logical volumes of name <";
193       eM += pos->first;                           193       eM += pos->first;
194       eM += "> are found and thus the sensitiv    194       eM += "> are found and thus the sensitive detector <";
195       eM += aSD->GetName();                       195       eM += aSD->GetName();
196       eM += "> cannot be uniquely assigned.";     196       eM += "> cannot be uniquely assigned.";
197       G4Exception("G4VUserDetectorConstruction    197       G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector()", "Run0052",
198                   FatalErrorInArgument, eM);      198                   FatalErrorInArgument, eM);
199     }                                             199     }
200     found = true;                                 200     found = true;
201     for (auto& i : pos->second) {                 201     for (auto& i : pos->second) {
202       SetSensitiveDetector(i, aSD);               202       SetSensitiveDetector(i, aSD);
203     }                                             203     }
204   }                                               204   }
205   if (!found) {                                   205   if (!found) {
206     G4String eM2 = "No logical volume of name     206     G4String eM2 = "No logical volume of name <";
207     eM2 += logVolName;                            207     eM2 += logVolName;
208     eM2 += "> is found. The specified sensitiv    208     eM2 += "> is found. The specified sensitive detector <";
209     eM2 += aSD->GetName();                        209     eM2 += aSD->GetName();
210     eM2 += "> couldn't be assigned to any volu    210     eM2 += "> couldn't be assigned to any volume.";
211     G4Exception("G4VUserDetectorConstruction::    211     G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector()", "Run0053",
212                 FatalErrorInArgument, eM2);       212                 FatalErrorInArgument, eM2);
213   }                                               213   }
214 }                                                 214 }
215                                                   215 
216 // -------------------------------------------    216 // --------------------------------------------------------------------
217 void G4VUserDetectorConstruction::SetSensitive    217 void G4VUserDetectorConstruction::SetSensitiveDetector(G4LogicalVolume* logVol,
218                                                   218                                                        G4VSensitiveDetector* aSD)
219 {                                                 219 {
220   assert(logVol != nullptr && aSD != nullptr);    220   assert(logVol != nullptr && aSD != nullptr);
221                                                   221 
222   // The aSD has already been added by user to    222   // The aSD has already been added by user to the manager if needed
223   // G4SDManager::GetSDMpointer()->AddNewDetec    223   // G4SDManager::GetSDMpointer()->AddNewDetector(aSD);
224                                                   224 
225   // New Logic: allow for "multiple" SDs being    225   // New Logic: allow for "multiple" SDs being attached to a single LV.
226   // To do that we use a special proxy SD call    226   // To do that we use a special proxy SD called G4MultiSensitiveDetector
227                                                   227 
228   // Get existing SD if already set and check     228   // Get existing SD if already set and check if it is of the special type
229   G4VSensitiveDetector* originalSD = logVol->G    229   G4VSensitiveDetector* originalSD = logVol->GetSensitiveDetector();
230   if (originalSD == aSD) {                        230   if (originalSD == aSD) {
231     G4ExceptionDescription msg;                   231     G4ExceptionDescription msg;
232     msg << "Attempting to add multiple times t    232     msg << "Attempting to add multiple times the same sensitive detector (\"";
233     msg << originalSD->GetName() << "\") is no    233     msg << originalSD->GetName() << "\") is not allowed, skipping.";
234     G4Exception("G4VUserDetectorConstruction::    234     G4Exception("G4VUserDetectorConstruction::SetSensitiveDetector", "Run0054", JustWarning, msg);
235     return;                                       235     return;
236   }                                               236   }
237   if (originalSD == nullptr) {                    237   if (originalSD == nullptr) {
238     logVol->SetSensitiveDetector(aSD);            238     logVol->SetSensitiveDetector(aSD);
239   }                                               239   }
240   else {                                          240   else {
241     auto msd = dynamic_cast<G4MultiSensitiveDe    241     auto msd = dynamic_cast<G4MultiSensitiveDetector*>(originalSD);
242     if (msd != nullptr) {                         242     if (msd != nullptr) {
243       msd->AddSD(aSD);                            243       msd->AddSD(aSD);
244     }                                             244     }
245     else {                                        245     else {
246       std::ostringstream mn;                      246       std::ostringstream mn;
247       mn << "/MultiSD_" << logVol->GetName() <    247       mn << "/MultiSD_" << logVol->GetName() << "_" << logVol;
248       const G4String msdname = mn.str();          248       const G4String msdname = mn.str();
249       msd = new G4MultiSensitiveDetector(msdna    249       msd = new G4MultiSensitiveDetector(msdname);
250       // We need to register the proxy to have    250       // We need to register the proxy to have correct handling of IDs
251       G4SDManager::GetSDMpointer()->AddNewDete    251       G4SDManager::GetSDMpointer()->AddNewDetector(msd);
252       msd->AddSD(originalSD);                     252       msd->AddSD(originalSD);
253       msd->AddSD(aSD);                            253       msd->AddSD(aSD);
254       logVol->SetSensitiveDetector(msd);          254       logVol->SetSensitiveDetector(msd);
255     }                                             255     }
256   }                                               256   }
257 }                                                 257 }
258                                                   258