Geant4 Cross Reference |
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 // Gorad (Geant4 Open-source Radiation Analysis and Design) 27 // 28 // Author : Makoto Asai (SLAC National Accelerator Laboratory) 29 // 30 // Development of Gorad is funded by NASA Johnson Space Center (JSC) 31 // under the contract NNJ15HK11B. 32 // 33 // ******************************************************************** 34 // 35 // GRPrimGenActionMessenger.hh 36 // Header file of a messenger that handles primary generator action. 37 // Input radiation spectrum file should be in ASCII format and each 38 // row should have low-end kinetic energy and differential flux 39 // separated by a space. 40 // 41 // History 42 // September 8th, 2020 : first implementation 43 // 44 // ******************************************************************** 45 46 #include "GRPrimGenActionMessenger.hh" 47 48 #include "G4UIdirectory.hh" 49 #include "G4UIcommand.hh" 50 #include "G4UIparameter.hh" 51 52 GRPrimGenActionMessenger::GRPrimGenActionMessenger(G4int verboseLvl) 53 : verboseLevel(verboseLvl) 54 { 55 G4UIparameter* param = nullptr; 56 57 srcDir = new G4UIdirectory("/gorad/source/"); 58 srcDir->SetGuidance("Define primary particle spectrum."); 59 60 defCmd = new G4UIcommand("/gorad/source/define",this); 61 defCmd->SetGuidance("Define primary particle spectrum."); 62 defCmd->SetGuidance("[usage] /gorad/source/define pName fName srcType radius unit (x0 y0 z0)"); 63 defCmd->SetGuidance(" pName : Particle name."); 64 defCmd->SetGuidance(" fName : File name of the spectrum. Directory may be preceded to the file name."); 65 defCmd->SetGuidance(" srcType : defines how the primaries are generated."); 66 defCmd->SetGuidance(" Arb : Generate primaries along with the defined spectrum."); 67 defCmd->SetGuidance(" LW : Generate primaries in flat ditribution with track weight representing the spectrum."); 68 defCmd->SetGuidance(" radius : Radius of the source sphere. If it is set to -1 (default), radius is set to 98% of the world volume."); 69 defCmd->SetGuidance(" unit : Unit of radius value."); 70 defCmd->SetGuidance(" x0,y0,z0 : (Optional) location of the center of the sphere (same unit is applied as radius)."); 71 defCmd->SetGuidance(" By default the sphere is located at the center of the world volume."); 72 defCmd->SetGuidance("[note] This command is not mandatory but dwialternatively granular /gps/ commands can be used for defining primary particles."); 73 particlePara = new G4UIparameter("pName",'s',false); 74 defCmd->SetParameter(particlePara); 75 param = new G4UIparameter("fName",'s',false); 76 defCmd->SetParameter(param); 77 param = new G4UIparameter("srcType",'s',false); 78 param->SetParameterCandidates("Arb LW"); 79 defCmd->SetParameter(param); 80 param = new G4UIparameter("radius",'d',true); 81 param->SetParameterRange("radius == -1. || radius>0."); 82 param->SetDefaultValue(-1.); 83 defCmd->SetParameter(param); 84 param = new G4UIparameter("unit",'s',true); 85 param->SetDefaultUnit("m"); 86 defCmd->SetParameter(param); 87 param = new G4UIparameter("x0",'d',true); 88 param->SetDefaultValue(0.); 89 defCmd->SetParameter(param); 90 param = new G4UIparameter("y0",'d',true); 91 param->SetDefaultValue(0.); 92 defCmd->SetParameter(param); 93 param = new G4UIparameter("z0",'d',true); 94 param->SetDefaultValue(0.); 95 defCmd->SetParameter(param); 96 defCmd->AvailableForStates(G4State_Idle); 97 defCmd->SetToBeBroadcasted(false); 98 } 99 100 GRPrimGenActionMessenger::~GRPrimGenActionMessenger() 101 { 102 delete defCmd; 103 delete srcDir; 104 } 105 106 #include "G4UImanager.hh" 107 #include "G4Threading.hh" 108 #include "G4Tokenizer.hh" 109 #include "G4UnitsTable.hh" 110 #include <fstream> 111 #include "GRDetectorConstruction.hh" 112 113 void GRPrimGenActionMessenger::SetNewValue(G4UIcommand * command,G4String newVal) 114 { 115 auto UI = G4UImanager::GetUIpointer(); 116 if(verboseLevel>0) G4cout << newVal << G4endl; 117 118 G4Tokenizer next(newVal); 119 G4String pName = next(); 120 G4String fName = next(); 121 std::ifstream specFile; 122 specFile.open(fName,std::ios::in); 123 if(specFile.fail()) 124 { 125 G4ExceptionDescription ed; 126 ed << "ERROR : File <" << fName << "> is not found."; 127 command->CommandFailed(ed); 128 return; 129 } 130 131 G4String srcType = next(); 132 G4String radius = next(); 133 G4String unit = next(); 134 G4double r = StoD(radius); 135 if(r<0.) 136 { 137 r = GRDetectorConstruction::GetWorldSize() * 0.98; 138 radius = DtoS(r); 139 unit = "mm"; 140 G4cout<<"################ Radius of the GPS sphere is set to "<<r<<" (mm)"<<G4endl; 141 } 142 G4String pos = next() + " " + next() + " " + next(); 143 144 G4String cmd; 145 G4int ec = 0; 146 ec = std::max(UI->ApplyCommand("/gps/pos/shape Sphere"),ec); 147 cmd = "/gps/pos/centre " + pos + " " + unit; 148 ec = std::max(UI->ApplyCommand(cmd),ec); 149 cmd = "/gps/pos/radius " + radius + " " + unit; 150 ec = std::max(UI->ApplyCommand(cmd),ec); 151 ec = std::max(UI->ApplyCommand("/gps/pos/type Surface"),ec); 152 ec = std::max(UI->ApplyCommand("/gps/number 1"),ec); 153 cmd = "/gps/particle " + pName; 154 ec = std::max(UI->ApplyCommand(cmd),ec); 155 ec = std::max(UI->ApplyCommand("/gps/ang/type cos"),ec); 156 ec = std::max(UI->ApplyCommand("/gps/ang/maxtheta 90.0 deg"),ec); 157 ec = std::max(UI->ApplyCommand("/gps/ang/mintheta 0.0 deg"),ec); 158 cmd = "/gps/ene/type " + srcType; 159 ec = std::max(UI->ApplyCommand(cmd),ec); 160 ec = std::max(UI->ApplyCommand("/gps/hist/type arb"),ec); 161 162 enum { bufsize = 128 }; 163 static char* line = new char[bufsize]; 164 while(specFile.good()) 165 { 166 specFile.getline(line,bufsize); 167 if(line[(size_t)0]=='#') continue; 168 G4String valStr = line; 169 G4StrUtil::strip(valStr); 170 G4StrUtil::rstrip(valStr, 0x0d); 171 if(valStr.size()==0) continue; 172 cmd = "/gps/hist/point " + valStr; 173 ec = std::max(UI->ApplyCommand(cmd),ec); 174 if(specFile.eof()) break; 175 } 176 ec = std::max(UI->ApplyCommand("/gps/hist/inter Lin"),ec); 177 178 if(ec>0) 179 { 180 G4ExceptionDescription ed; 181 ed << "ERROR : Internal error while processing /gorad/source/define command."; 182 command->CommandFailed(ec,ed); 183 } 184 } 185 186 G4String GRPrimGenActionMessenger::GetCurrentValue(G4UIcommand* /*command*/) 187 { 188 G4String val; 189 return val; 190 } 191 192 #include "G4ParticleTable.hh" 193 void GRPrimGenActionMessenger::UpdateParticleList() 194 { 195 auto particleTable = G4ParticleTable::GetParticleTable(); 196 G4String candList; 197 for(G4int i=0;i<particleTable->entries();i++) 198 { candList += particleTable->GetParticleName(i) + " "; } 199 candList += "ion"; 200 particlePara->SetParameterCandidates(candList); 201 } 202 203