Geant4 Cross Reference |
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 // G4AdjointPrimaryGeneratorAction implementat << 26 // $Id: G4AdjointPrimaryGeneratorAction.cc,v 1.3 2010-07-21 14:21:19 gcosmo Exp $ >> 27 // GEANT4 tag $Name: not supported by cvs2svn $ 27 // 28 // 28 // ------------------------------------------- << 29 ///////////////////////////////////////////////////////////////////////////// 29 // Class Name: G4AdjointPrimaryGeneratorAc << 30 // Class Name: G4AdjointPrimaryGeneratorAction 30 // Author: L. Desorgher, 2007-2009 << 31 // Author: L. Desorgher 31 // Organisation: SpaceIT GmbH << 32 // Organisation: SpaceIT GmbH 32 // Contract: ESA contract 21435/08/NL/AT << 33 // Contract: ESA contract 21435/08/NL/AT 33 // Customer: ESA/ESTEC << 34 // Customer: ESA/ESTEC 34 // ------------------------------------------- << 35 ///////////////////////////////////////////////////////////////////////////// 35 36 36 #include "G4AdjointPrimaryGeneratorAction.hh" 37 #include "G4AdjointPrimaryGeneratorAction.hh" 37 << 38 #include "G4AdjointPrimaryGenerator.hh" << 39 #include "G4AdjointSimManager.hh" << 40 #include "G4Event.hh" 38 #include "G4Event.hh" 41 #include "G4Gamma.hh" << 42 #include "G4ParticleDefinition.hh" << 43 #include "G4ParticleTable.hh" 39 #include "G4ParticleTable.hh" 44 #include "G4PhysicalConstants.hh" << 40 #include "G4ParticleDefinition.hh" 45 << 41 #include "G4ParticleTable.hh" 46 // ------------------------------------------- << 42 #include "G4AdjointSimManager.hh" >> 43 #include "G4AdjointPrimaryGenerator.hh" >> 44 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 47 // 45 // 48 G4AdjointPrimaryGeneratorAction::G4AdjointPrim 46 G4AdjointPrimaryGeneratorAction::G4AdjointPrimaryGeneratorAction() 49 { << 47 : Emin(0.), Emax(0.), EminIon(0.), EmaxIon(0.), NbOfAdjointPrimaryTypes(0), 50 theAdjointPrimaryGenerator = new G4AdjointPr << 48 index_particle(100000), last_generated_part_was_adjoint(false), 51 << 49 radius_spherical_source(0.), fwd_ion(0), adj_ion(0), 52 PrimariesConsideredInAdjointSim[G4String("e- << 50 ion_name("not_defined") 53 PrimariesConsideredInAdjointSim[G4String("ga << 51 { 54 PrimariesConsideredInAdjointSim[G4String("pr << 52 theAdjointPrimaryGenerator= new G4AdjointPrimaryGenerator(); 55 PrimariesConsideredInAdjointSim[G4String("io << 53 >> 54 PrimariesConsideredInAdjointSim[G4String("e-")]=false; >> 55 PrimariesConsideredInAdjointSim[G4String("gamma")]=false; >> 56 PrimariesConsideredInAdjointSim[G4String("proton")]=false; >> 57 PrimariesConsideredInAdjointSim[G4String("ion")]=false; 56 58 57 ListOfPrimaryFwdParticles.clear(); 59 ListOfPrimaryFwdParticles.clear(); 58 ListOfPrimaryAdjParticles.clear(); 60 ListOfPrimaryAdjParticles.clear(); 59 } 61 } 60 << 62 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 61 // ------------------------------------------- << 62 // 63 // 63 G4AdjointPrimaryGeneratorAction::~G4AdjointPri 64 G4AdjointPrimaryGeneratorAction::~G4AdjointPrimaryGeneratorAction() 64 { 65 { 65 delete theAdjointPrimaryGenerator; 66 delete theAdjointPrimaryGenerator; 66 } 67 } 67 << 68 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 68 // ------------------------------------------- << 69 // 69 // 70 void G4AdjointPrimaryGeneratorAction::Generate 70 void G4AdjointPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) 71 { 71 { 72 G4int evt_id = anEvent->GetEventID(); << 72 if ( !last_generated_part_was_adjoint ) { 73 std::size_t n = ListOfPrimaryAdjParticles.si << 73 74 index_particle = std::size_t(evt_id) - n * ( << 74 index_particle++; 75 << 75 if (index_particle >= ListOfPrimaryAdjParticles.size()) index_particle =0; 76 G4double E1 = Emin; << 76 77 G4double E2 = Emax; << 77 78 if (ListOfPrimaryAdjParticles[index_particle << 78 G4double E1=Emin; 79 UpdateListOfPrimaryParticles(); // ion ha << 79 G4double E2=Emax; 80 << 80 if (!ListOfPrimaryAdjParticles[index_particle]) UpdateListOfPrimaryParticles();//ion has not been created yet 81 if (ListOfPrimaryAdjParticles[index_particle << 81 82 E1 = EminIon; << 82 if (ListOfPrimaryAdjParticles[index_particle]->GetParticleName() == "adj_proton") { 83 E2 = EmaxIon; << 83 E1=EminIon; 84 } << 84 E2=EmaxIon; 85 if (ListOfPrimaryAdjParticles[index_particle << 85 } 86 G4int A = ListOfPrimaryAdjParticles[index_ << 86 if (ListOfPrimaryAdjParticles[index_particle]->GetParticleType() == "adjoint_nucleus") { 87 E1 = EminIon * A; << 87 G4int A= ListOfPrimaryAdjParticles[index_particle]->GetAtomicMass(); 88 E2 = EmaxIon * A; << 88 E1=EminIon*A; 89 } << 89 E2=EmaxIon*A; 90 // Generate first the forwrad primaries << 90 } 91 theAdjointPrimaryGenerator->GenerateFwdPrima << 91 theAdjointPrimaryGenerator->GenerateAdjointPrimaryVertex(anEvent, 92 anEvent, ListOfPrimaryFwdParticles[index_p << 92 ListOfPrimaryAdjParticles[index_particle], 93 G4PrimaryVertex* fwdPrimVertex = anEvent->Ge << 93 E1,E2); 94 << 94 G4PrimaryVertex* aPrimVertex = anEvent->GetPrimaryVertex(); 95 p = fwdPrimVertex->GetPrimary()->GetMomentum << 95 96 pos = fwdPrimVertex->GetPosition(); << 96 97 G4double pmag = p.mag(); << 97 p=aPrimVertex->GetPrimary()->GetMomentum(); 98 G4double m0 = ListOfPrimaryFwdParticles[inde << 98 pos=aPrimVertex->GetPosition(); 99 G4double ekin = std::sqrt(m0 * m0 + pmag * p << 99 G4double pmag=p.mag(); 100 << 100 101 G4double weight_correction = 1.; << 101 G4double m0=ListOfPrimaryAdjParticles[index_particle]->GetPDGMass(); 102 // For gamma generate the particle along the << 102 G4double ekin=std::sqrt( m0*m0 + pmag*pmag) -m0; 103 G4ThreeVector dir = -p / p.mag(); << 103 104 << 104 105 weight_correction = 1.; << 105 //The factor pi is to normalise the weight to the directional flux 106 << 106 G4double adjoint_source_area = G4AdjointSimManager::GetInstance()->GetAdjointSourceArea(); 107 if (ListOfPrimaryFwdParticles[index_particle << 107 G4double adjoint_weight = ComputeEnergyDistWeight(ekin,E1,E2)*adjoint_source_area*pi; 108 { << 108 109 G4double weight = (1. / nb_fwd_gammas_per_ << 109 aPrimVertex->SetWeight(adjoint_weight); 110 fwdPrimVertex->SetWeight(weight); << 110 111 for (G4int i = 0; i < nb_fwd_gammas_per_ev << 111 last_generated_part_was_adjoint =true; 112 auto newFwdPrimVertex = new G4PrimaryVer << 112 G4AdjointSimManager::GetInstance()->SetAdjointTrackingMode(true); 113 newFwdPrimVertex->SetPosition(pos.x(), p << 113 G4AdjointSimManager::GetInstance()->RegisterAdjointPrimaryWeight(adjoint_weight); 114 newFwdPrimVertex->SetT0(0.); << 114 } 115 auto aPrimParticle = << 115 else { 116 new G4PrimaryParticle(ListOfPrimaryFwd << 116 //fwd particle equivalent to the last generated adjoint particle ios generated 117 newFwdPrimVertex->SetPrimary(aPrimPartic << 117 G4PrimaryVertex* aPrimVertex = new G4PrimaryVertex(); 118 newFwdPrimVertex->SetWeight(weight); << 118 aPrimVertex->SetPosition(pos.x(),pos.y(),pos.z()); 119 anEvent->AddPrimaryVertex(newFwdPrimVert << 119 aPrimVertex->SetT0(0.); 120 } << 120 G4PrimaryParticle* aPrimParticle = new G4PrimaryParticle(ListOfPrimaryFwdParticles[index_particle], 121 } << 121 -p.x(),-p.y(),-p.z()); 122 << 122 123 // Now generate the adjoint primaries << 123 aPrimVertex->SetPrimary(aPrimParticle); 124 auto adjPrimVertex = new G4PrimaryVertex(); << 124 anEvent->AddPrimaryVertex(aPrimVertex); 125 adjPrimVertex->SetPosition(pos.x(), pos.y(), << 125 last_generated_part_was_adjoint =false; 126 adjPrimVertex->SetT0(0.); << 126 G4AdjointSimManager::GetInstance()->SetAdjointTrackingMode(false); 127 auto aPrimParticle = << 127 } 128 new G4PrimaryParticle(ListOfPrimaryAdjPart << 129 << 130 adjPrimVertex->SetPrimary(aPrimParticle); << 131 anEvent->AddPrimaryVertex(adjPrimVertex); << 132 << 133 // The factor pi is to normalise the weight << 134 G4double adjoint_source_area = G4AdjointSimM << 135 G4double adjoint_weight = << 136 weight_correction * ComputeEnergyDistWeigh << 137 if (ListOfPrimaryAdjParticles[index_particle << 138 // The weight will be corrected at the end << 139 // are used << 140 adjoint_weight = adjoint_weight / nb_adj_p << 141 for (G4int i = 0; i < nb_adj_primary_gamma << 142 auto newAdjPrimVertex = new G4PrimaryVer << 143 newAdjPrimVertex->SetPosition(pos.x(), p << 144 newAdjPrimVertex->SetT0(0.); << 145 aPrimParticle = << 146 new G4PrimaryParticle(ListOfPrimaryAdj << 147 newAdjPrimVertex->SetPrimary(aPrimPartic << 148 newAdjPrimVertex->SetWeight(adjoint_weig << 149 anEvent->AddPrimaryVertex(newAdjPrimVert << 150 } << 151 } << 152 else if (ListOfPrimaryAdjParticles[index_par << 153 // The weight will be corrected at the end << 154 // are used << 155 adjoint_weight = adjoint_weight / nb_adj_p << 156 for (G4int i = 0; i < nb_adj_primary_elect << 157 auto newAdjPrimVertex = new G4PrimaryVer << 158 newAdjPrimVertex->SetPosition(pos.x(), p << 159 newAdjPrimVertex->SetT0(0.); << 160 aPrimParticle = << 161 new G4PrimaryParticle(ListOfPrimaryAdj << 162 newAdjPrimVertex->SetPrimary(aPrimPartic << 163 newAdjPrimVertex->SetWeight(adjoint_weig << 164 anEvent->AddPrimaryVertex(newAdjPrimVert << 165 } << 166 } << 167 adjPrimVertex->SetWeight(adjoint_weight); << 168 << 169 // Call some methods of G4AdjointSimManager << 170 G4AdjointSimManager::GetInstance()->SetAdjoi << 171 G4AdjointSimManager::GetInstance()->ClearEnd << 172 G4AdjointSimManager::GetInstance()->ResetDid << 173 } 128 } 174 << 129 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 175 // ------------------------------------------- << 176 // 130 // 177 void G4AdjointPrimaryGeneratorAction::SetEmin( 131 void G4AdjointPrimaryGeneratorAction::SetEmin(G4double val) 178 { 132 { 179 Emin = val; << 133 Emin=val; 180 EminIon = val; << 134 EminIon=val; 181 } 135 } 182 << 136 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 183 // ------------------------------------------- << 184 // 137 // 185 void G4AdjointPrimaryGeneratorAction::SetEmax( 138 void G4AdjointPrimaryGeneratorAction::SetEmax(G4double val) 186 { 139 { 187 Emax = val; << 140 Emax=val; 188 EmaxIon = val; << 141 EmaxIon=val; 189 } 142 } 190 << 143 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 191 // ------------------------------------------- << 192 // 144 // 193 void G4AdjointPrimaryGeneratorAction::SetEminI 145 void G4AdjointPrimaryGeneratorAction::SetEminIon(G4double val) 194 { 146 { 195 EminIon = val; << 147 EminIon=val; 196 } 148 } 197 << 149 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 198 // ------------------------------------------- << 199 // 150 // 200 void G4AdjointPrimaryGeneratorAction::SetEmaxI 151 void G4AdjointPrimaryGeneratorAction::SetEmaxIon(G4double val) 201 { 152 { 202 EmaxIon = val; << 153 EmaxIon=val; 203 } 154 } 204 << 155 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 205 // ------------------------------------------- << 206 // 156 // 207 G4double G4AdjointPrimaryGeneratorAction::Comp << 157 G4double G4AdjointPrimaryGeneratorAction::ComputeEnergyDistWeight(G4double E ,G4double E1, G4double E2) 208 << 209 { 158 { 210 // We generate N numbers of primaries with << 159 // We generate N numbers of primaries with a 1/E energy law distribution. 211 // We have therefore an energy distribution << 160 // We have therefore an energy distribution function 212 // f(E)=C/E (1) << 161 // f(E)=C/E (1) 213 // with C a constant that is such that << 162 // with C a constant that is such that 214 // N=Integral(f(E),E1,E2)=C.std::log(E2/E1 << 163 // N=Integral(f(E),E1,E2)=C.std::log(E2/E1) (2) 215 // Therefore from (2) we get << 164 // Therefore from (2) we get 216 // C=N/ std::log(E2/E1) (3) << 165 // C=N/ std::log(E2/E1) (3) 217 // and << 166 // and 218 // f(E)=N/ std::log(E2/E1)/E (4) << 167 // f(E)=N/ std::log(E2/E1)/E (4) 219 // For the adjoint simulation we need a ener << 168 //For the adjoint simulation we need a energy distribution f'(E)=1.. 220 // To get that we need therefore to apply a << 169 //To get that we need therefore to apply a weight to the primary 221 // W=1/f(E)=E*std::log(E2/E1)/N << 170 // W=1/f(E)=E*std::log(E2/E1)/N 222 // << 171 // 223 return std::log(E2 / E1) * E / G4AdjointSimM << 172 return std::log(E2/E1)*E/G4AdjointSimManager::GetInstance()->GetNbEvtOfLastRun(); >> 173 224 } 174 } 225 << 175 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 226 // ------------------------------------------- << 227 // 176 // 228 void G4AdjointPrimaryGeneratorAction::SetSpher << 177 void G4AdjointPrimaryGeneratorAction::SetSphericalAdjointPrimarySource(G4double radius, G4ThreeVector center_pos) 229 << 178 { 230 { << 231 radius_spherical_source = radius; 179 radius_spherical_source = radius; 232 center_spherical_source = center_pos; 180 center_spherical_source = center_pos; 233 type_of_adjoint_source = "Spherical"; << 181 type_of_adjoint_source ="Spherical"; 234 theAdjointPrimaryGenerator->SetSphericalAdjo << 182 theAdjointPrimaryGenerator->SetSphericalAdjointPrimarySource(radius,center_pos); 235 } 183 } 236 << 184 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 237 // ------------------------------------------- << 238 // 185 // 239 void G4AdjointPrimaryGeneratorAction::SetAdjoi << 186 void G4AdjointPrimaryGeneratorAction::SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(const G4String& volume_name) 240 const G4String& volume_name) << 241 { 187 { 242 type_of_adjoint_source = "ExternalSurfaceOfA << 188 type_of_adjoint_source ="ExternalSurfaceOfAVolume"; 243 theAdjointPrimaryGenerator->SetAdjointPrimar 189 theAdjointPrimaryGenerator->SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(volume_name); 244 } 190 } 245 << 191 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 246 // ------------------------------------------- << 247 // 192 // 248 void G4AdjointPrimaryGeneratorAction::Consider 193 void G4AdjointPrimaryGeneratorAction::ConsiderParticleAsPrimary(const G4String& particle_name) 249 { 194 { 250 if (PrimariesConsideredInAdjointSim.find(par << 195 if (PrimariesConsideredInAdjointSim.find(particle_name) != PrimariesConsideredInAdjointSim.end()){ 251 { << 196 PrimariesConsideredInAdjointSim[particle_name]=true; 252 PrimariesConsideredInAdjointSim[particle_n << 253 } 197 } 254 UpdateListOfPrimaryParticles(); 198 UpdateListOfPrimaryParticles(); 255 } 199 } 256 << 200 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 257 // ------------------------------------------- << 258 // 201 // 259 void G4AdjointPrimaryGeneratorAction::NeglectP 202 void G4AdjointPrimaryGeneratorAction::NeglectParticleAsPrimary(const G4String& particle_name) 260 { 203 { 261 if (PrimariesConsideredInAdjointSim.find(par << 204 if (PrimariesConsideredInAdjointSim.find(particle_name) != PrimariesConsideredInAdjointSim.end()){ 262 { << 205 PrimariesConsideredInAdjointSim[particle_name]= false; 263 PrimariesConsideredInAdjointSim[particle_n << 264 } 206 } 265 UpdateListOfPrimaryParticles(); 207 UpdateListOfPrimaryParticles(); 266 } 208 } 267 << 209 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 268 // ------------------------------------------- << 269 // 210 // 270 void G4AdjointPrimaryGeneratorAction::UpdateLi 211 void G4AdjointPrimaryGeneratorAction::UpdateListOfPrimaryParticles() 271 { 212 { 272 G4ParticleTable* theParticleTable = G4Partic << 213 G4ParticleTable* theParticleTable = G4ParticleTable::GetParticleTable(); 273 ListOfPrimaryFwdParticles.clear(); << 214 ListOfPrimaryFwdParticles.clear(); 274 ListOfPrimaryAdjParticles.clear(); << 215 ListOfPrimaryAdjParticles.clear(); 275 for (const auto& iter : PrimariesConsideredI << 216 std::map<G4String, G4bool>::iterator iter; 276 if (iter.second) { << 217 for( iter = PrimariesConsideredInAdjointSim.begin(); iter != PrimariesConsideredInAdjointSim.end(); ++iter ) { 277 G4String fwd_particle_name = iter.first; << 218 if(iter->second) { 278 if (fwd_particle_name != "ion") { << 219 G4String fwd_particle_name = iter->first; 279 G4String adj_particle_name = G4String( << 220 if ( fwd_particle_name != "ion") { 280 ListOfPrimaryFwdParticles.push_back(th << 221 G4String adj_particle_name = G4String("adj_") + fwd_particle_name; 281 ListOfPrimaryAdjParticles.push_back(th << 222 ListOfPrimaryFwdParticles.push_back(theParticleTable->FindParticle(fwd_particle_name)); 282 } << 223 ListOfPrimaryAdjParticles.push_back(theParticleTable->FindParticle(adj_particle_name)); 283 else { << 224 } 284 if (fwd_ion != nullptr) { << 225 else { 285 ion_name = fwd_ion->GetParticleName( << 226 if (fwd_ion ){ 286 G4String adj_ion_name = G4String("ad << 227 ion_name=fwd_ion->GetParticleName(); 287 ListOfPrimaryFwdParticles.push_back( << 228 G4String adj_ion_name=G4String("adj_") +ion_name; 288 ListOfPrimaryAdjParticles.push_back( << 229 ListOfPrimaryFwdParticles.push_back(fwd_ion); 289 } << 230 ListOfPrimaryAdjParticles.push_back(adj_ion); 290 else { << 231 } 291 ListOfPrimaryFwdParticles.push_back( << 232 else { 292 ListOfPrimaryAdjParticles.push_back( << 233 ListOfPrimaryFwdParticles.push_back(0); 293 } << 234 ListOfPrimaryAdjParticles.push_back(0); 294 } << 235 295 } << 236 } 296 } << 237 } >> 238 } >> 239 } 297 } 240 } 298 << 241 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 299 // ------------------------------------------- << 300 // 242 // 301 void G4AdjointPrimaryGeneratorAction::SetPrima << 243 void G4AdjointPrimaryGeneratorAction::SetPrimaryIon(G4ParticleDefinition* adjointIon, G4ParticleDefinition* fwdIon) 302 << 303 { 244 { 304 fwd_ion = fwdIon; 245 fwd_ion = fwdIon; 305 adj_ion = adjointIon; 246 adj_ion = adjointIon; 306 UpdateListOfPrimaryParticles(); 247 UpdateListOfPrimaryParticles(); 307 } 248 } >> 249 308 250