Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // >> 23 // $Id: G4LossTableBuilder.cc,v 1.10 2004/02/27 09:41:09 vnivanch Exp $ >> 24 // GEANT4 tag $Name: geant4-06-01 $ >> 25 // 26 // ------------------------------------------- 26 // ------------------------------------------------------------------- 27 // 27 // 28 // GEANT4 Class file 28 // GEANT4 Class file 29 // 29 // 30 // 30 // 31 // File name: G4LossTableBuilder 31 // File name: G4LossTableBuilder 32 // 32 // 33 // Author: Vladimir Ivanchenko 33 // Author: Vladimir Ivanchenko 34 // 34 // 35 // Creation date: 03.01.2002 35 // Creation date: 03.01.2002 36 // 36 // 37 // Modifications: 37 // Modifications: 38 // 38 // 39 // 23-01-03 V.Ivanchenko Cut per region << 39 // 23.01.2003 V.Ivanchenko Cut per region 40 // 21-07-04 V.Ivanchenko Fix problem of range << 41 // 08-11-04 Migration to new interface of Stor << 42 // 07-12-04 Fix of BuildDEDX table (V.Ivanchen << 43 // 27-03-06 Add bool options isIonisation (V.I << 44 // 16-01-07 Fill new (not old) DEDX table (V.I << 45 // 12-02-07 Use G4LPhysicsFreeVector for the i << 46 // 24-06-09 Removed hidden bin in G4PhysicsVec << 47 // 40 // 48 // Class Description: << 41 // Class Description: 49 // 42 // 50 // ------------------------------------------- 43 // ------------------------------------------------------------------- 51 // 44 // 52 //....oooOO0OOooo........oooOO0OOooo........oo 45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 53 //....oooOO0OOooo........oooOO0OOooo........oo 46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 54 47 55 #include "G4LossTableBuilder.hh" 48 #include "G4LossTableBuilder.hh" 56 #include "G4SystemOfUnits.hh" << 57 #include "G4PhysicsTable.hh" 49 #include "G4PhysicsTable.hh" 58 #include "G4PhysicsLogVector.hh" 50 #include "G4PhysicsLogVector.hh" 59 #include "G4PhysicsTableHelper.hh" << 60 #include "G4PhysicsFreeVector.hh" << 61 #include "G4ProductionCutsTable.hh" << 62 #include "G4MaterialCutsCouple.hh" << 63 #include "G4Material.hh" << 64 #include "G4VEmModel.hh" << 65 #include "G4ParticleDefinition.hh" << 66 #include "G4LossTableManager.hh" << 67 #include "G4EmParameters.hh" << 68 << 69 G4bool G4LossTableBuilder::baseMatFlag = false << 70 std::vector<G4double>* G4LossTableBuilder::the << 71 std::vector<G4int>* G4LossTableBuilder::the << 72 std::vector<G4bool>* G4LossTableBuilder::the << 73 51 74 //....oooOO0OOooo........oooOO0OOooo........oo 52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 75 53 76 G4LossTableBuilder::G4LossTableBuilder(G4bool << 54 G4PhysicsTable* G4LossTableBuilder::BuildDEDXTable( 77 : isInitializer(master) << 55 const std::vector<G4PhysicsTable*>& list) 78 { << 56 { 79 theParameters = G4EmParameters::Instance(); << 57 size_t n_processes = list.size(); 80 if (nullptr == theFlag) { << 81 theDensityFactor = new std::vector<G4doubl << 82 theDensityIdx = new std::vector<G4int>; << 83 theFlag = new std::vector<G4bool>; << 84 } << 85 } << 86 58 87 //....oooOO0OOooo........oooOO0OOooo........oo << 59 if (0 == n_processes) return 0; 88 << 60 else if (1 == n_processes) return list[0]; 89 G4LossTableBuilder::~G4LossTableBuilder() << 90 { << 91 if (isInitializer) { << 92 delete theDensityFactor; << 93 delete theDensityIdx; << 94 delete theFlag; << 95 theDensityFactor = nullptr; << 96 theDensityIdx = nullptr; << 97 theFlag = nullptr; << 98 } << 99 } << 100 61 101 //....oooOO0OOooo........oooOO0OOooo........oo << 62 size_t n_vectors = list[0]->length(); 102 63 103 const std::vector<G4int>* G4LossTableBuilder:: << 64 if(!n_vectors) return 0; 104 { << 105 return theDensityIdx; << 106 } << 107 65 108 //....oooOO0OOooo........oooOO0OOooo........oo << 66 G4PhysicsTable* theTable = new G4PhysicsTable(); >> 67 G4bool b; 109 68 110 const std::vector<G4double>* G4LossTableBuilde << 69 for (size_t i=0; i<n_vectors; i++) { 111 { << 112 return theDensityFactor; << 113 } << 114 70 115 //....oooOO0OOooo........oooOO0OOooo........oo << 71 G4PhysicsVector* pv = (*(list[0]))[i]; >> 72 size_t nbins = pv->GetVectorLength(); 116 73 117 G4bool G4LossTableBuilder::GetFlag(std::size_t << 74 G4double elow = pv->GetLowEdgeEnergy(0); 118 { << 75 G4double ehigh = pv->GetLowEdgeEnergy(nbins); 119 return (idx < theFlag->size()) ? (*theFlag)[ << 76 G4PhysicsLogVector* v = new G4PhysicsLogVector(elow, ehigh, nbins); 120 } << 121 77 122 //....oooOO0OOooo........oooOO0OOooo........oo << 78 for (size_t j=0; j<nbins; j++) { >> 79 G4double dedx = 0.0; >> 80 G4double energy = pv->GetLowEdgeEnergy(j); 123 81 124 G4bool G4LossTableBuilder::GetBaseMaterialFlag << 82 for (size_t k=0; k<n_processes; k++) { 125 { << 126 return baseMatFlag; << 127 } << 128 83 129 //....oooOO0OOooo........oooOO0OOooo........oo << 84 dedx += ((*(list[k]))[i])->GetValue(energy, b); 130 85 131 void << 132 G4LossTableBuilder::BuildDEDXTable(G4PhysicsTa << 133 const std:: << 134 { << 135 InitialiseBaseMaterials(dedxTable); << 136 std::size_t n_processes = list.size(); << 137 if(1 >= n_processes) { return; } << 138 << 139 std::size_t nCouples = dedxTable->size(); << 140 //G4cout << "Nproc= " << n_processes << " nC << 141 // << dedxTable->size() << G4endl; << 142 if(0 >= nCouples) { return; } << 143 << 144 for (std::size_t i=0; i<nCouples; ++i) { << 145 auto pv0 = static_cast<G4PhysicsLogVector* << 146 //if (0 == i) G4cout << i << ". pv0=" << p << 147 if(pv0 == nullptr) { continue; } << 148 std::size_t npoints = pv0->GetVectorLength << 149 auto pv = new G4PhysicsLogVector(*pv0); << 150 for (std::size_t j=0; j<npoints; ++j) { << 151 G4double dedx = 0.0; << 152 for (std::size_t k=0; k<n_processes; ++k << 153 const G4PhysicsVector* pv1 = (*(list[k]))[i] << 154 //if (0 == i) G4cout << " " << k << ". p << 155 dedx += (*pv1)[j]; << 156 } 86 } 157 pv->PutValue(j, dedx); << 87 v->PutValue(j, dedx); 158 } 88 } 159 if(splineFlag) { pv->FillSecondDerivatives << 89 theTable->insert(v); 160 G4PhysicsTableHelper::SetPhysicsVector(ded << 161 } 90 } 162 //G4cout << "### G4LossTableBuilder::BuildDE << 91 return theTable; 163 //G4cout << *dedxTable << G4endl; << 164 } 92 } 165 93 166 //....oooOO0OOooo........oooOO0OOooo........oo 94 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 167 95 168 void G4LossTableBuilder::BuildRangeTable(const << 96 G4PhysicsTable* G4LossTableBuilder::BuildRangeTable( 169 G4Phy << 97 const G4PhysicsTable* dedxTable) 170 // Build range table from the energy loss tabl 98 // Build range table from the energy loss table 171 { 99 { 172 //G4cout << "### G4LossTableBuilder::BuildRa << 100 size_t n_vectors = dedxTable->length(); 173 //G4cout << *const_cast<G4PhysicsTable*>(ded << 101 if(!n_vectors) return 0; 174 const std::size_t nCouples = dedxTable->size << 175 if(0 >= nCouples) { return; } << 176 << 177 const std::size_t n = 100; << 178 const G4double del = 1.0/(G4double)n; << 179 << 180 for (std::size_t i=0; i<nCouples; ++i) { << 181 auto pv = static_cast<G4PhysicsLogVector*> << 182 if((pv == nullptr) || (isBaseMatActive && << 183 std::size_t npoints = pv->GetVectorLength( << 184 std::size_t bin0 = 0; << 185 G4double elow = pv->Energy(0); << 186 G4double ehigh = pv->Energy(npoints-1); << 187 G4double dedx1 = (*pv)[0]; << 188 << 189 // protection for specific cases dedx=0 << 190 if(dedx1 == 0.0) { << 191 for (std::size_t k=1; k<npoints; ++k) { << 192 ++bin0; << 193 elow = pv->Energy(k); << 194 dedx1 = (*pv)[k]; << 195 if(dedx1 > 0.0) { break; } << 196 } << 197 npoints -= bin0; << 198 } << 199 << 200 // initialisation of a new vector << 201 if(npoints < 3) { npoints = 3; } << 202 102 203 delete (*rangeTable)[i]; << 103 G4bool b; 204 G4PhysicsLogVector* v; << 104 size_t n = 100; 205 if(0 == bin0) { v = new G4PhysicsLogVector << 105 G4double del = 1.0/(G4double)n; 206 else { v = new G4PhysicsLogVector(elow, eh << 106 G4PhysicsTable* theTable = new G4PhysicsTable(); 207 << 107 208 // assumed dedx proportional to beta << 108 for (size_t i=0; i<n_vectors; i++) { 209 G4double energy1 = v->Energy(0); << 109 210 G4double range = 2.*energy1/dedx1; << 110 G4PhysicsVector* pv = (*dedxTable)[i]; 211 /* << 111 size_t nbins = pv->GetVectorLength(); 212 G4cout << "New Range vector Npoints=" << v << 112 G4double elow = pv->GetLowEdgeEnergy(0); 213 << " coupleIdx=" << i << " spline=" << v- << 113 G4double ehigh = pv->GetLowEdgeEnergy(nbins); 214 << " Elow=" << v->GetMinEnergy() <<" Ehig << 114 G4PhysicsLogVector* v = new G4PhysicsLogVector(elow, ehigh, nbins); 215 << " DEDX(Elow)=" << dedx1 << " R(Elow)=" << 115 216 */ << 116 G4double dedx1 = pv->GetValue(elow, b); >> 117 G4double range = 2.*elow/dedx1; >> 118 //G4double range = elow/dedx1; 217 v->PutValue(0,range); 119 v->PutValue(0,range); >> 120 G4double energy1 = elow; 218 121 219 for (std::size_t j=1; j<npoints; ++j) { << 122 for (size_t j=1; j<nbins; j++) { 220 123 221 G4double energy2 = v->Energy(j); << 124 G4double energy2 = pv->GetLowEdgeEnergy(j); 222 G4double de = (energy2 - energy1) * << 125 G4double dedx2 = pv->GetValue(energy2, b); 223 G4double energy = energy2 + de*0.5; << 126 G4double de = (energy2 - energy1) * del; 224 G4double sum = 0.0; << 127 G4double energy = energy1 - de*0.5; 225 std::size_t idx = j - 1; << 128 G4double fac = log(dedx2/dedx1)/log(energy2/energy1); 226 for (std::size_t k=0; k<n; ++k) { << 129 227 energy -= de; << 130 for (size_t k=0; k<n; k++) { 228 dedx1 = pv->Value(energy, idx); << 131 energy += de; 229 if(dedx1 > 0.0) { sum += de/dedx1; } << 132 G4double f = dedx1*exp(fac*log(energy/energy1)); >> 133 range += de/f; 230 } 134 } 231 range += sum; << 232 /* << 233 if(energy < 10.) << 234 G4cout << "j= " << j << " e1= " << energy1 < << 235 << " n= " << n << " range=" << range< << 236 */ << 237 v->PutValue(j,range); 135 v->PutValue(j,range); 238 energy1 = energy2; 136 energy1 = energy2; >> 137 dedx1 = dedx2; 239 } 138 } 240 if(splineFlag) { v->FillSecondDerivatives( << 139 theTable->insert(v); 241 G4PhysicsTableHelper::SetPhysicsVector(ran << 242 } 140 } 243 //G4cout << "### Range table" << G4endl; << 244 //G4cout << *rangeTable << G4endl; << 245 } << 246 << 247 //....oooOO0OOooo........oooOO0OOooo........oo << 248 << 249 void << 250 G4LossTableBuilder::BuildInverseRangeTable(con << 251 G4P << 252 // Build inverse range table from the energy l << 253 { << 254 std::size_t nCouples = rangeTable->size(); << 255 if(0 >= nCouples) { return; } << 256 << 257 for (std::size_t i=0; i<nCouples; ++i) { << 258 G4PhysicsVector* pv = (*rangeTable)[i]; << 259 if((pv == nullptr) || (isBaseMatActive && << 260 std::size_t npoints = pv->GetVectorLength( << 261 << 262 delete (*invRangeTable)[i]; << 263 auto v = new G4PhysicsFreeVector(npoints, << 264 << 265 for (std::size_t j=0; j<npoints; ++j) { << 266 G4double e = pv->Energy(j); << 267 G4double r = (*pv)[j]; << 268 v->PutValues(j,r,e); << 269 } << 270 if (splineFlag) { v->FillSecondDerivatives << 271 v->EnableLogBinSearch(theParameters->Numbe << 272 141 273 G4PhysicsTableHelper::SetPhysicsVector(inv << 142 return theTable; 274 } << 275 //G4cout << "### Inverse range table" << G4e << 276 //G4cout << *invRangeTable << G4endl; << 277 } 143 } 278 144 279 //....oooOO0OOooo........oooOO0OOooo........oo 145 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 280 146 281 void G4LossTableBuilder::InitialiseBaseMateria << 147 G4PhysicsTable* G4LossTableBuilder::BuildInverseRangeTable( >> 148 const G4PhysicsTable*, >> 149 const G4PhysicsTable* rangeTable) >> 150 // Build inverse range table from the energy loss table 282 { 151 { 283 if(!isInitializer) { return; } << 152 size_t n_vectors = rangeTable->length(); 284 const G4ProductionCutsTable* theCoupleTable= << 153 if(!n_vectors) return 0; 285 G4ProductionCutsTable::GetProductionCutsTa << 286 std::size_t nCouples = theCoupleTable->GetTa << 287 std::size_t nFlags = theFlag->size(); << 288 /* << 289 G4cout << "### InitialiseBaseMaterials: nCou << 290 << " nFlags=" << nFlags << " isInit:" << is << 291 << " baseMat:" << baseMatFlag << G4endl; << 292 */ << 293 // define base material flag << 294 if(isBaseMatActive && !baseMatFlag) { << 295 for(G4int i=0; i<(G4int)nCouples; ++i) { << 296 if(nullptr != theCoupleTable->GetMateria << 297 baseMatFlag = true; << 298 isInitialized = false; << 299 break; << 300 } << 301 } << 302 } << 303 154 304 if(nFlags != nCouples) { isInitialized = fal << 155 G4bool b; 305 if(isInitialized) { return; } << 156 G4PhysicsTable* theTable = new G4PhysicsTable(); 306 157 307 // reserve memory << 158 for (size_t i=0; i<n_vectors; i++) { 308 theFlag->resize(nCouples, true); << 309 theDensityFactor->resize(nCouples,1.0); << 310 theDensityIdx->resize(nCouples, 0); << 311 << 312 // define default flag and index of used mat << 313 for (G4int i=0; i<(G4int)nCouples; ++i) { << 314 (*theFlag)[i] = (nullptr == table) ? true << 315 (*theDensityIdx)[i] = i; << 316 } << 317 isInitialized = true; << 318 if (!baseMatFlag) { return; } << 319 159 320 // use base materials << 160 G4PhysicsVector* pv = (*rangeTable)[i]; 321 for (G4int i=0; i<(G4int)nCouples; ++i) { << 161 size_t nbins = pv->GetVectorLength(); 322 // base material is needed only for a coup << 162 G4double elow = pv->GetLowEdgeEnergy(0); 323 // initialised and for which tables will b << 163 G4double ehigh = pv->GetLowEdgeEnergy(nbins-1); 324 auto couple = theCoupleTable->GetMaterialC << 164 G4double rlow = pv->GetValue(elow, b); 325 auto pcuts = couple->GetProductionCuts(); << 165 G4double rhigh = pv->GetValue(ehigh, b); 326 auto mat = couple->GetMaterial(); << 166 327 auto bmat = mat->GetBaseMaterial(); << 167 rhigh *= exp(log(rhigh/rlow)/((G4double)(nbins-1))); 328 << 168 329 // base material exists - find it and chec << 169 G4PhysicsLogVector* v = new G4PhysicsLogVector(rlow, rhigh, nbins); 330 if(nullptr != bmat) { << 170 331 for(G4int j=0; j<(G4int)nCouples; ++j) { << 171 v->PutValue(0,elow); 332 if(j == i) { continue; } << 172 G4double energy1 = elow; 333 auto bcouple = theCoupleTable->GetMaterialCu << 173 G4double range1 = rlow; 334 << 174 G4double energy2 = elow; 335 if(bcouple->GetMaterial() == bmat && << 175 G4double range2 = rlow; 336 bcouple->GetProductionCuts() == pcuts) { << 176 size_t ilow = 0; 337 << 177 size_t ihigh; 338 // based couple exist in the same region << 178 339 (*theDensityFactor)[i] = mat->GetDensity() << 179 for (size_t j=1; j<nbins; j++) { 340 (*theDensityIdx)[i] = j; << 180 341 (*theFlag)[i] = false; << 181 G4double range = v->GetLowEdgeEnergy(j); 342 << 182 343 // ensure that there will no double initia << 183 for (ihigh=ilow+1; ihigh<nbins; ihigh++) { 344 (*theDensityFactor)[j] = 1.0; << 184 energy2 = pv->GetLowEdgeEnergy(ihigh); 345 (*theDensityIdx)[j] = j; << 185 range2 = pv->GetValue(energy2, b); 346 (*theFlag)[j] = true; << 186 if(range2 >= range || ihigh == nbins-1) { 347 break; << 187 ilow = ihigh - 1; >> 188 energy1 = pv->GetLowEdgeEnergy(ilow); >> 189 range1 = pv->GetValue(energy1, b); >> 190 break; 348 } 191 } 349 } 192 } 350 } << 351 } << 352 } << 353 << 354 //....oooOO0OOooo........oooOO0OOooo........oo << 355 193 356 G4PhysicsTable* << 194 G4double e = log(energy1) + log(energy2/energy1)*log(range/range1)/log(range2/range1); 357 G4LossTableBuilder::BuildTableForModel(G4Physi << 358 G4VEmMo << 359 const G << 360 G4doubl << 361 G4bool << 362 { << 363 // check input << 364 G4PhysicsTable* table = G4PhysicsTableHelper << 365 if (nullptr == table) { return table; } << 366 if (aTable != nullptr && aTable != table) { << 367 aTable->clearAndDestroy(); << 368 delete aTable; << 369 } << 370 195 371 InitialiseBaseMaterials(table); << 196 v->PutValue(j,exp(e)); 372 G4int nbins = theParameters->NumberOfBinsPer << 373 << 374 // Access to materials << 375 const G4ProductionCutsTable* theCoupleTable= << 376 G4ProductionCutsTable::GetProductionCu << 377 std::size_t numOfCouples = theCoupleTable->G << 378 /* << 379 G4cout << " G4LossTableBuilder::BuildTable << 380 << " isMaster=" << isInitializer << " model << 381 << " " << part->GetParticleName() << G4end << 382 */ << 383 G4PhysicsLogVector* aVector = nullptr; << 384 << 385 for(G4int i=0; i<(G4int)numOfCouples; ++i) { << 386 //G4cout << i << ". " << (*theFlag)[i] << << 387 if (table->GetFlag(i)) { << 388 << 389 // create physics vector and fill it << 390 auto couple = theCoupleTable->GetMateria << 391 delete (*table)[i]; << 392 << 393 // if start from zero then change the sc << 394 const G4Material* mat = couple->GetMater << 395 << 396 G4double tmin = std::max(emin, model->Mi << 397 if(0.0 >= tmin) { tmin = CLHEP::eV; } << 398 G4int n = nbins; << 399 << 400 if(tmin >= emax) { << 401 aVector = nullptr; << 402 } else { << 403 n *= G4lrint(std::log10(emax/tmin)); << 404 n = std::max(n, 3); << 405 aVector = new G4PhysicsLogVector(tmin, << 406 } << 407 << 408 if(nullptr != aVector) { << 409 //G4cout << part->GetParticleName() << << 410 // << " emin= " << tmin << " emax=" << e << 411 for(G4int j=0; j<=n; ++j) { << 412 G4double e = aVector->Energy(j); << 413 G4double y = model->Value(couple, part, e) << 414 //G4cout << " " << j << ") E=" << e < << 415 aVector->PutValue(j, y); << 416 } << 417 if(spline) { aVector->FillSecondDeriva << 418 } << 419 G4PhysicsTableHelper::SetPhysicsVector(t << 420 } 197 } >> 198 theTable->insert(v); 421 } 199 } 422 /* << 200 423 G4cout << "G4LossTableBuilder::BuildTableFor << 201 return theTable; 424 << part->GetParticleName() << " and " << 425 << " " << table << G4endl; << 426 */ << 427 //G4cout << *table << G4endl; << 428 return table; << 429 } 202 } 430 203 431 //....oooOO0OOooo........oooOO0OOooo........oo 204 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 432 205