00001
00002
00003
00004
00005
00006
00007
00008 #include "PrimaryGeneratorAction.hh"
00009
00010 #include "PrimaryGeneratorMessenger.hh"
00011
00012 #include "G4RunManager.hh"
00013 #include "G4Event.hh"
00014 #include "G4ParticleGun.hh"
00015 #include "G4ParticleTable.hh"
00016 #include "G4ParticleDefinition.hh"
00017 #include "G4RotationMatrix.hh"
00018 #include "G4ThreeVector.hh"
00019 #include "G4ParticleMomentum.hh"
00020 #include "G4TransportationManager.hh"
00021 #include "Randomize.hh"
00022
00023
00024 #include "MyDetectorManager.hh"
00025 #include "MyVGeometrySvc.hh"
00026 #include "MyVGeometryParameter.hh"
00027 #include "SimpleGeometryParameter.hh"
00028
00029 #include "EventHeaderSvc.hh"
00030
00031 #include "TFile.h"
00032 #include "TH1D.h"
00033 #include "TCanvas.h"
00034 #include "TChain.h"
00035 #include "TFitResult.h"
00036
00037 #include "CLHEP/Vector/EulerAngles.h"
00038
00039 #include <stdlib.h>
00040 #include <iostream>
00041 #include <fstream>
00042
00043 PrimaryGeneratorAction* PrimaryGeneratorAction::fPrimaryGeneratorAction = 0;
00044
00045 PrimaryGeneratorAction::PrimaryGeneratorAction()
00046 : MyConfigure(),
00047 root_index(0),
00048 m_TChain(0),
00049 particleGun(0),
00050 gunMessenger(0),
00051 fParticle(0),
00052 fp(0)
00053 {
00054 if (fPrimaryGeneratorAction){
00055 G4Exception("PrimaryGeneratorAction::PrimaryGeneratorAction()","Run0031",
00056 FatalException, "PrimaryGeneratorAction constructed twice.");
00057 }
00058 fPrimaryGeneratorAction = this;
00059
00060 gunMessenger = new PrimaryGeneratorMessenger(this);
00061
00062
00063 G4String file_name = getenv("GENFILEROOT");
00064 ReadCard(file_name);
00065 Initialize();
00066
00067
00068 fXPositionFinalFocusFit = NULL;
00069 fYPositionFinalFocusFit = NULL;
00070
00071 fXPositionFinalFocus_Lower = -1.5;
00072 fXPositionFinalFocus_Upper = 1.5;
00073
00074 fYPositionFinalFocus_Lower = -1.5;
00075 fYPositionFinalFocus_Upper = 1.5;
00076
00077 fMuPCBeamDistHist = NULL;
00078
00079
00080 fCollimatedInputHist_XYPz = NULL;
00081 fCollimatedInputHist_XPxPz = NULL;
00082 fCollimatedInputHist_YPyPz = NULL;
00083
00084
00085
00086 }
00087
00088 PrimaryGeneratorAction::~PrimaryGeneratorAction()
00089 {
00090
00091
00092
00093
00094
00095 delete particleGun;
00096 delete gunMessenger;
00097 if(EM_hist) delete EM_hist;
00098 if(DM_hist) delete DM_hist;
00099 if(PM_hist) delete PM_hist;
00100 if(m_TChain) delete m_TChain;
00101 }
00102
00103 PrimaryGeneratorAction* PrimaryGeneratorAction::GetPrimaryGeneratorAction(){
00104 if ( !fPrimaryGeneratorAction ){
00105 new PrimaryGeneratorAction();
00106 }
00107 return fPrimaryGeneratorAction;
00108 }
00109
00110 void* PrimaryGeneratorAction::get_extra(G4String name){
00111 if (name=="weight") {if(!flag_weight) return &root_double[9];}
00112 else if (name=="ox") {if(!flag_ox) return &root_double[10];}
00113 else if (name=="oy") {if(!flag_oy) return &root_double[11];}
00114 else if (name=="oz") {if(!flag_oz) return &root_double[12];}
00115 else if (name=="opx") {if(!flag_opx) return &root_double[13];}
00116 else if (name=="opy") {if(!flag_opy) return &root_double[14];}
00117 else if (name=="opz") {if(!flag_opz) return &root_double[15];}
00118 else if (name=="ot") {if(!flag_ot) return &root_double[16];}
00119 else if (name=="ppid") {if(!flag_ppid) return &root_int[1];}
00120 else if (name=="ptid") {if(!flag_ptid) return &root_int[2];}
00121 else if (name=="process") {if(!flag_process) return root_str[0];}
00122 else if (name=="volume") {if(!flag_volume) return root_str[1];}
00123
00124
00125 else if (name=="R0") {if(!flag_R0) return &root_int[3];}
00126 else if (name=="R1") {if(!flag_R1) return &root_int[4];}
00127 return NULL;
00128 }
00129
00130 void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
00131 {
00132
00133
00134 if (UseRoot){
00135 root_get_para();
00136 }
00137 if (RandMode=="root"){
00138 long seeds[3];
00139
00140
00141 seeds[0] = root_int[3];
00142 seeds[1] = root_int[4];
00143 seeds[2] = 0;
00144
00145 CLHEP::HepRandom::setTheSeeds(seeds);
00146 }
00147
00148
00149
00150
00151
00152 if (fType=="ion"){
00153 if (!fParticle){
00154 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
00155 fParticle = particleTable->GetIon(Z,A,E*keV);
00156 if (!fParticle){
00157 std::cout<<"ERROR: In PrimaryGeneratorAction::PrimaryGeneratorAction() Cannot find particle "
00158 <<"Z = "<<Z
00159 <<", A = "<<A
00160 <<", E = "<<E<<" keV"
00161 <<"!!!"<<std::endl;
00162 G4Exception("PrimaryGeneratorAction::PrimaryGeneratorAction()","Run0031",
00163 FatalException, "Cannot find particle.");
00164 }
00165 }
00166 if (fType == "stable"){
00167 fParticle->SetPDGStable(true);
00168 }
00169 particleGun->SetParticleDefinition(fParticle);
00170 mass = particleGun->GetParticleDefinition()->GetPDGMass();
00171 particleGun->SetParticleCharge(C*eplus);
00172 }
00173
00174 if ( pidMode == "root"){
00175 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
00176 G4ParticleDefinition* particle = particleTable->FindParticle(root_int[0]);
00177 if (!particle){
00178 std::cout<<"ERROR: In PrimaryGeneratorAction::PrimaryGeneratorAction() Cannot find particle "<<root_int[0]<<"!!! Will use geantino"<<std::endl;
00179
00180
00181 particle = particleTable->FindParticle("geantino");
00182 }
00183 if (fType == "stable"){
00184 particle->SetPDGStable(true);
00185 }
00186 particleGun->SetParticleDefinition(particle);
00187 mass = particleGun->GetParticleDefinition()->GetPDGMass();
00188 }
00189
00190 if ( EnergyMode == "histo"){
00191 G4double ekin = EM_hist->GetRandom() / 1e3 * MeV;
00192
00193 particleGun->SetParticleEnergy(ekin);
00194 }
00195 else if ( EnergyMode == "root" ){
00196
00197
00198
00199
00200
00201
00202 G4double ekin = sqrt(root_double[3]*root_double[3]*MeV*MeV+root_double[4]*root_double[4]*MeV*MeV+root_double[5]*root_double[5]*MeV*MeV+mass*mass)-mass;
00203 particleGun->SetParticleMomentumDirection(G4ThreeVector(root_double[3] * MeV, root_double[4] * MeV, root_double[5] * MeV).unit());
00204 particleGun->SetParticleEnergy(ekin);
00205 }
00206 else if ( EnergyMode == "gRand" || EnergyMode == "uRand" ){
00207 SetRandomEnergy();
00208 }
00209 else if ( EnergyMode == "collimated") {
00210 if (!fCollimatedInputHist_XYPz && !fCollimatedInputHist_XPxPz && !fCollimatedInputHist_YPyPz) {
00211 TDirectory* prev_dir = gDirectory;
00212
00213 std::string dir_name = getenv("CONFIGUREDATAROOT");
00214 dir_name += "CollimatedInput.root";
00215 TFile* collimated_file = new TFile(dir_name.c_str(), "READ");
00216
00217 fCollimatedInputHist_XYPz = (TH3F*) (collimated_file->Get("hXYPz"))->Clone();
00218 fCollimatedInputHist_XYPz->SetDirectory(0);
00219
00220 fCollimatedInputHist_XPxPz = (TH3F*) (collimated_file->Get("hXPxPz"))->Clone();
00221 fCollimatedInputHist_XPxPz->SetDirectory(0);
00222
00223 fCollimatedInputHist_YPyPz = (TH3F*) (collimated_file->Get("hYPyPz"))->Clone();
00224 fCollimatedInputHist_YPyPz->SetDirectory(0);
00225
00226 collimated_file->Close();
00227 gDirectory = prev_dir;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 double px, py, pz;
00237 double x, y, z;
00238 z = -7.06*cm;
00239
00240 TH2F* hXPx = NULL;
00241 TH1D* hPx = NULL;
00242 TH2F* hYPy = NULL;
00243 TH1D* hPy = NULL;
00244
00245 double monitor_location = 2*cm;
00246 double hole_ellipse_half_x = 1.6*cm;
00247 double hole_ellipse_half_y = 2.5*cm;
00248
00249 bool found = false;
00250 while (!found) {
00251 fCollimatedInputHist_XYPz->GetRandom3(x, y, pz);
00252 x *= cm; y *= cm; pz *= MeV;
00253
00254
00255
00256 int bin = fCollimatedInputHist_XPxPz->GetZaxis()->FindBin(pz);
00257 fCollimatedInputHist_XPxPz->GetZaxis()->SetRange(bin, bin);
00258 hXPx = (TH2F*) fCollimatedInputHist_XPxPz->Project3D("xy");
00259
00260 if (hXPx->GetEntries() == 0) {
00261 continue;
00262 }
00263
00264
00265 bin = hXPx->GetYaxis()->FindBin(x);
00266 hXPx->GetYaxis()->SetRange(bin, bin);
00267 hPx = hXPx->ProjectionX();
00268
00269 if (hPx->GetEntries() == 0) {
00270 continue;
00271 }
00272
00273
00274 px = hPx->GetRandom()*MeV;
00275
00276
00277 bin = fCollimatedInputHist_YPyPz->GetZaxis()->FindBin(pz);
00278 fCollimatedInputHist_YPyPz->GetZaxis()->SetRange(bin, bin);
00279 hYPy = (TH2F*) fCollimatedInputHist_YPyPz->Project3D("xy");
00280
00281 if (hYPy->GetEntries() == 0) {
00282 continue;
00283 }
00284
00285
00286
00287 bin = hYPy->GetYaxis()->FindBin(y);
00288 hYPy->GetYaxis()->SetRange(bin, bin);
00289 hPy = hYPy->ProjectionX();
00290
00291 if (hPy->GetEntries() == 0) {
00292 continue;
00293 }
00294
00295
00296 py = hPy->GetRandom()*MeV;
00297
00298
00299
00300 double p_total = std::sqrt(px*px + py*py + pz*pz);
00301
00302 double unit_px = px / p_total;
00303 double trackback_x = x - unit_px*monitor_location;
00304 double unit_py = py / p_total;
00305 double trackback_y = y - unit_py*monitor_location;
00306
00307 double ellipse = (trackback_x*trackback_x)/(hole_ellipse_half_x*hole_ellipse_half_x) + (trackback_y*trackback_y)/(hole_ellipse_half_y*hole_ellipse_half_y);
00308 if (ellipse <= 1) {
00309
00310 found = true;
00311 }
00312 }
00313 G4ParticleMomentum particleMomentum(px, py, pz);
00314
00315 G4double mom = particleMomentum.mag();
00316 G4double mass = particleGun->GetParticleDefinition()->GetPDGMass();
00317 G4double ekin = sqrt(mom*mom+mass*mass)-mass;
00318 particleGun->SetParticleEnergy(ekin);
00319 particleGun->SetParticleMomentumDirection(particleMomentum.unit());
00320
00321 G4ThreeVector start_pos(x, y, z);
00322 particleGun->SetParticlePosition(start_pos);
00323
00324
00325 }
00326 else if ( EnergyMode != "none" ){
00327 std::cout<<"ERROR: unknown EnergyMode: "<<EnergyMode<<"!!!"<<std::endl;
00328 G4Exception("PrimaryGeneratorAction::GeneratePrimaries()",
00329 "InvalidSetup", FatalException,
00330 "unknown EnergyMode");
00331 }
00332
00333 if ( DirectionMode == "uniform" ){
00334 SetUniformDirection();
00335 }
00336 else if ( DirectionMode == "histo" ){
00337 G4double theta = DM_hist->GetRandom() * rad;
00338 G4double phi = G4UniformRand() * 360. *deg;
00339 G4ThreeVector dir_3Vec(1, 1, 1);
00340 dir_3Vec.setTheta(theta);
00341 dir_3Vec.setPhi(phi);
00342 particleGun->SetParticleMomentumDirection(dir_3Vec);
00343 }
00344 else if ( DirectionMode == "turtle" || PositionMode == "turtle") {
00345
00346
00347
00348
00349
00350
00351
00352 if (!fXPositionFinalFocusFit && !fYPositionFinalFocusFit && !fMuPCBeamDistHist) {
00353 TDirectory* prev_dir = gDirectory;
00354
00355 std::string dir_name = getenv("CONFIGUREDATAROOT");
00356 dir_name += "TURTLE_fits.root";
00357 TFile* turtle_file = new TFile(dir_name.c_str(), "READ");
00358
00359
00360 fXPositionFinalFocusFit = (TF1*) turtle_file->Get("final_focus_horizontal");
00361 fYPositionFinalFocusFit = (TF1*) turtle_file->Get("final_focus_vertical");
00362
00363
00364 fMuPCBeamDistHist = (TH2F*) turtle_file->Get("hmuPC_XYWires")->Clone();
00365 fMuPCBeamDistHist->SetDirectory(0);
00366 turtle_file->Close();
00367 gDirectory = prev_dir;
00368 }
00369
00370 double x_muPC = 0;
00371 double y_muPC = 0;
00372 fMuPCBeamDistHist->GetRandom2(x_muPC, y_muPC);
00373
00374 double z_muPC = 52.5;
00375
00376 double x_ff = fXPositionFinalFocusFit->GetRandom()*10;
00377 double y_ff = fYPositionFinalFocusFit->GetRandom()*10;
00378
00379 double z_ff = 120;
00380
00381
00382 double z_pos_beam_pipe = -285.58 - 60;
00383 double translation = z_pos_beam_pipe;
00384
00385 z_muPC += translation;
00386 z_ff += translation;
00387
00388 G4ThreeVector muPCPos(x_muPC*mm, y_muPC*mm, z_muPC*mm);
00389 G4ThreeVector ffPos(x_ff*mm, y_ff*mm, z_ff*mm);
00390
00391
00392
00393 G4ThreeVector direction = (ffPos - muPCPos).unit();
00394
00395
00396
00397 double n_steps = (z_pos_beam_pipe - muPCPos.z()/mm) / (direction.z()/mm);
00398
00399 G4ThreeVector start_pos = muPCPos + n_steps*direction;
00400
00401
00402 particleGun->SetParticlePosition(start_pos);
00403 particleGun->SetParticleMomentumDirection(direction);
00404 }
00405 else if ( DirectionMode == "muPC" || PositionMode == "muPC" || PositionMode == "collimator" || DirectionMode == "collimator") {
00406 if (!fMuPCBeamDistHist) {
00407 TDirectory* prev_dir = gDirectory;
00408
00409 std::string dir_name = getenv("CONFIGUREDATAROOT");
00410 dir_name += "mupc_profile_run3600_Al50.root";
00411 TFile* mupc_profile_file = new TFile(dir_name.c_str(), "READ");
00412
00413
00414 fMuPCBeamDistHist = (TH2F*) mupc_profile_file->Get("muPC/hmuPC_XYWires")->Clone();
00415 fMuPCBeamDistHist->SetDirectory(0);
00416 mupc_profile_file->Close();
00417 gDirectory = prev_dir;
00418 }
00419
00420 bool found = false;
00421
00422 while (!found) {
00423
00424 if (DirectionMode == "muPC" || PositionMode == "muPC") {
00425 found = true;
00426 }
00427
00428 double x, y, z;
00429 z = -(285.58+7.5)*mm;
00430 fMuPCBeamDistHist->GetRandom2(x, y);
00431 x *= mm; y *= mm;
00432 G4ThreeVector muPCPos(x, y, z);
00433
00434
00435
00436 double dMom=G4RandGauss::shoot(0,MomSpread);
00437 double pz = Pa + dMom;
00438
00439 double px = G4RandGauss::shoot(0, 0.033*pz);
00440 double py = G4RandGauss::shoot(0, 0.11*pz);
00441 double p_tot = std::sqrt(px*px + py*py + pz*pz);
00442
00443
00444
00445
00446 double scale_factor = pz / p_tot;
00447 px *= scale_factor;
00448 py *= scale_factor;
00449 pz *= scale_factor;
00450 p_tot = std::sqrt(px*px + py*py + pz*pz);
00451
00452
00453 G4ThreeVector particleMomentum(px, py, pz);
00454 G4ThreeVector direction = particleMomentum.unit();
00455
00456
00457 G4double mom = particleMomentum.mag();
00458 G4double scale = pz / particleMomentum.mag();
00459
00460 particleMomentum *= scale;
00461
00462 G4double mass = particleGun->GetParticleDefinition()->GetPDGMass();
00463 G4double ekin = sqrt(mom*mom+mass*mass)-mass;
00464 particleGun->SetParticleEnergy(ekin);
00465 particleGun->SetParticleMomentumDirection(direction);
00466
00467 if (DirectionMode == "collimator" || PositionMode == "collimator") {
00468 double z_pos_collimator = -90.5;
00469 double n_steps = (z_pos_collimator - muPCPos.z()/mm) / (direction.z()/mm);
00470 G4ThreeVector start_pos = muPCPos + n_steps*direction;
00471
00472 double hole_ellipse_half_x = 16*mm;
00473 double hole_ellipse_half_y = 25*mm;
00474 double ellipse = (start_pos.x()*start_pos.x())/(hole_ellipse_half_x*hole_ellipse_half_x) + (start_pos.y()*start_pos.y())/(hole_ellipse_half_y*hole_ellipse_half_y);
00475 if ( ellipse < 1) {
00476 particleGun->SetParticlePosition(start_pos);
00477
00478 if (!fEnergyLoss) {
00479 fEnergyLoss = new TF1("energy_loss", "[0]*TMath::Landau(x, [1], [2]) + [3]*TMath::Exp([4]*x^[5] + [6]) + [7]*TMath::Gaus(x, [8], [9])", 0, 0.03);
00480
00481 fEnergyLoss->SetParameter(0, 14051.2);
00482 fEnergyLoss->SetParameter(1, 0.0141187);
00483 fEnergyLoss->SetParameter(2, 0.000738656);
00484 fEnergyLoss->SetParameter(3, -0.0127883);
00485 fEnergyLoss->SetParameter(4, 0.0599257);
00486 fEnergyLoss->SetParameter(5, 11.5222);
00487 fEnergyLoss->SetParameter(6, -1.30946);
00488 fEnergyLoss->SetParameter(7, 2510.06);
00489 fEnergyLoss->SetParameter(8, 0.0155356);
00490 fEnergyLoss->SetParameter(9, 0.00170543);
00491 }
00492
00493 double e_loss = fEnergyLoss->GetRandom()*GeV;
00494 G4double new_mom = mom - e_loss;
00495 mass = particleGun->GetParticleDefinition()->GetPDGMass();
00496 ekin = sqrt(new_mom*new_mom+mass*mass)-mass;
00497 particleGun->SetParticleEnergy(ekin);
00498
00499 found = true;
00500 }
00501 }
00502 if (DirectionMode == "muPC" || PositionMode == "muPC") {
00503
00504 double z_pos_beam_pipe = -285.58 - 60;
00505 double n_steps = (z_pos_beam_pipe - muPCPos.z()/mm) / (direction.z()/mm);
00506
00507 G4ThreeVector start_pos = muPCPos + n_steps*direction;
00508
00509 G4ThreeVector move(xSpread, ySpread, zSpread);
00510 start_pos += move;
00511 particleGun->SetParticlePosition(start_pos);
00512 }
00513 }
00514 }
00515 else if ( DirectionMode != "none" ){
00516 std::cout<<"ERROR: unknown DirectionMode: "<<DirectionMode<<"!!!"<<std::endl;
00517 G4Exception("PrimaryGeneratorAction::GeneratePrimaries()",
00518 "InvalidSetup", FatalException,
00519 "unknown DirectionMode");
00520 }
00521
00522
00523 if ( PhiMode== "gRand" || PhiMode=="uRand" || ThetaMode== "gRand" || ThetaMode=="uRand" ){
00524 SetRandomDirection();
00525 }
00526
00527 if ( PositionMode == "uniform" ){
00528 SetUniformPosition();
00529 }
00530 else if ( PositionMode == "root" ){
00531
00532
00533
00534
00535
00536
00537 particleGun->SetParticlePosition(G4ThreeVector(root_double[0] * mm, root_double[1] * mm, (root_double[2])*mm));
00538 }
00539 else if ( PositionMode == "gRand" || PositionMode == "sRand" || PositionMode == "bRand" ){
00540 SetRandomPosition();
00541 }
00542 else if ( PositionMode == "target") {
00543 SetRandomPosition();
00544 }
00545 else if ( PositionMode == "source") {
00546 SetRandomPosition();
00547 }
00548 else if ( PositionMode == "histo") {
00549 if (PM_hist) {
00550 double x=0,y=0,z=0;
00551 PM_hist->GetRandom3(x,y,z);
00552 G4ThreeVector pos_3Vec(x, y, z);
00553 particleGun->SetParticlePosition(pos_3Vec);
00554 }
00555 else if (PM_hist_sparse) {
00556 double random_pos[3];
00557 PM_hist_sparse->GetRandom(random_pos);
00558 G4ThreeVector pos_3Vec(random_pos[0], random_pos[1], random_pos[2]);
00559 particleGun->SetParticlePosition(pos_3Vec);
00560 }
00561 }
00562 else if ( PositionMode == "turtle" || PositionMode == "muPC" || PositionMode == "collimator") {
00563
00564 }
00565 else if ( PositionMode != "none" ){
00566 std::cout<<"ERROR: unknown PositionMode: "<<PositionMode<<"!!!"<<std::endl;
00567 G4Exception("PrimaryGeneratorAction::GeneratePrimaries()",
00568 "InvalidSetup", FatalException,
00569 "unknown PositionMode");
00570 }
00571
00572 if ( TimeMode == "root" ){
00573
00574
00575
00576
00577 particleGun->SetParticleTime(root_double[6]*ns);
00578 }
00579 else if ( TimeMode != "none" ){
00580 std::cout<<"ERROR: unknown TimeMode: "<<TimeMode<<"!!!"<<std::endl;
00581 G4Exception("PrimaryGeneratorAction::GeneratePrimaries()",
00582 "InvalidSetup", FatalException,
00583 "unknown TimeMode");
00584 }
00585
00586
00587 particleGun->GeneratePrimaryVertex(anEvent);
00588
00589
00590
00591
00592
00593
00594
00596 InformEventHeaderHeader();
00597 if (!UseRoot) root_index++;
00598 }
00599
00600 void PrimaryGeneratorAction::InformEventHeaderHeader(){
00601
00602 EventHeaderSvc::GetEventHeaderSvc()->SetSeedsValue();
00603
00604
00605 G4ParticleMomentum mom=particleGun->GetParticleMomentumDirection();
00606 double E_kinetic=particleGun->GetParticleEnergy();
00607 double M=particleGun->GetParticleDefinition()->GetPDGMass();
00608 double P=sqrt(E_kinetic*E_kinetic + 2*M*E_kinetic);
00609 mom=P*mom;
00610 EventHeaderSvc::GetEventHeaderSvc()->SetInitialMomentum(mom.x(),mom.y(),mom.z());
00611
00612
00613 G4ThreeVector pos=particleGun->GetParticlePosition();
00614 EventHeaderSvc::GetEventHeaderSvc()->SetInitialPosition(pos.x(),pos.y(),pos.z());
00615
00616
00617 EventHeaderSvc::GetEventHeaderSvc()->SetInitialParticle(particleGun->GetParticleDefinition()->GetParticleName());
00618 }
00619
00620 void PrimaryGeneratorAction::SetUniformDirection(){
00621 G4double dir_x = 0., dir_y = 0., dir_z = 0.;
00622 G4bool dir_OK = false;
00623 while( !dir_OK ){
00624 dir_x=G4UniformRand()-0.5;
00625 dir_y=G4UniformRand()-0.5;
00626 dir_z=G4UniformRand()-0.5;
00627 if ( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z <= 0.025 && dir_x*dir_x + dir_y*dir_y + dir_z*dir_z != 0) dir_OK = true;
00628 }
00629 G4ThreeVector dir_3Vec(dir_x, dir_y, dir_z);
00630 particleGun->SetParticleMomentumDirection(dir_3Vec);
00631 }
00632
00633 void PrimaryGeneratorAction::SetRandomEnergy(){
00634 if (EnergyType == 1){
00635 G4double dE=0;
00636 if(EnergyMode=="gRand"){
00637 dE=G4RandGauss::shoot(0,EkinSpread);
00638 }
00639 else if(EnergyMode=="uRand"){
00640 dE=(G4UniformRand()-0.5)*EkinSpread;
00641 }
00642 particleGun->SetParticleEnergy(Ekin+dE);
00643 }
00644 else if (EnergyType == 0 ){
00645 G4double dMom=0;
00646 if(EnergyMode=="gRand"){
00647 dMom=G4RandGauss::shoot(0,MomSpread);
00648 }
00649 else if(EnergyMode=="uRand"){
00650 dMom=(G4UniformRand()-0.5)*MomSpread;
00651 }
00652 G4double ekin = sqrt((Pa+dMom)*(Pa+dMom)+mass*mass)-mass;
00653 particleGun->SetParticleEnergy(ekin);
00654 }
00655 }
00656
00657 void PrimaryGeneratorAction::SetRandomDirection(){
00658 G4double dPhi=0;
00659 G4double dTheta=0;
00660 if(PhiMode=="gRand"){
00661 if (PhiSpread) dPhi=G4RandGauss::shoot(Phi,PhiSpread);
00662 }
00663 else if (PhiMode=="uRand"){
00664 if (PhiSpread) {dPhi=(G4UniformRand()-0.5)*PhiSpread;}
00665 }
00666 if(ThetaMode=="gRand"){
00667 if (ThetaSpread) dTheta=G4RandGauss::shoot(Theta,ThetaSpread);
00668 }
00669 else if (ThetaMode=="uRand"){
00670 if (ThetaSpread) {dTheta=(G4UniformRand()-0.5)*ThetaSpread;}
00671 }
00672 G4ThreeVector dir(1,1,1);
00673 dir.setTheta(Theta+dTheta);
00674 dir.setPhi(Phi+dPhi);
00675 G4RotationMatrix rot(Ephi,Etheta,Epsi);
00676 dir = rot*dir;
00677 particleGun->SetParticleMomentumDirection(dir.unit());
00678 }
00679
00680 void PrimaryGeneratorAction::SetRandomPosition(){
00681 G4double dx=0;
00682 G4double dy=0;
00683 G4double dz=0;
00684 G4double dx2=0;
00685 G4double dy2=0;
00686 G4double dz2=0;
00687 bool gotit=false;
00688 if(PositionMode=="gRand"){
00689 do {
00690 dx=G4RandGauss::shoot(0,xSpread);
00691 dy=G4RandGauss::shoot(0,ySpread);
00692 dz=G4RandGauss::shoot(0,zSpread);
00693 if (dx2+dy2+dz2<=PosLimit2) gotit = true;
00694 } while (!gotit);
00695 }
00696 else if (PositionMode=="sRand"){
00697 do {
00698 if (xSpread) {dx=2.*(G4UniformRand()-0.5);dx2 = dx*dx;dx*=xSpread;}
00699 if (ySpread) {dy=2.*(G4UniformRand()-0.5);dy2 = dy*dy;dy*=ySpread;}
00700 if (zSpread) {dz=2.*(G4UniformRand()-0.5);dz2 = dz*dz;dz*=zSpread;}
00701 if (dx2+dy2+dz2<=1.) gotit = true;
00702 } while (!gotit);
00703 }
00704 else if (PositionMode=="bRand"){
00705 if (xSpread) dx=2.*(G4UniformRand()-0.5)*xSpread;
00706 if (ySpread) dy=2.*(G4UniformRand()-0.5)*ySpread;
00707 if (zSpread) dz=2.*(G4UniformRand()-0.5)*zSpread;
00708 }
00709 else if (PositionMode=="source" || PositionMode=="target"){
00710
00711 G4Navigator* theNavigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
00712
00713
00714 do {
00715
00716
00717
00718 dx=G4RandFlat::shoot(-xSpread,xSpread);
00719 dy=G4RandFlat::shoot(-ySpread,ySpread);
00720 dz=G4RandFlat::shoot(-xSpread,zSpread);
00721
00722 G4ThreeVector position(x+dx, y+dy, z+dz);
00723 CLHEP::HepEulerAngles rotation(45*deg, 90*deg, 0*deg);
00724 G4ThreeVector new_position = position.rotate(rotation);
00725 G4VPhysicalVolume* phys_volume = theNavigator->LocateGlobalPointAndSetup(new_position);
00726
00727 if (!phys_volume) {
00728 continue;
00729 }
00730
00731 if ( (PositionMode=="source" && phys_volume->GetName() == "Source") ||
00732 (PositionMode=="target" && phys_volume->GetName() == "Target") ) {
00733 gotit = true;
00734 }
00735 } while (!gotit);
00736 }
00737
00738 G4ThreeVector position(x+dx, y+dy, z+dz);
00739 if (PositionMode == "source" || PositionMode == "target") {
00740 CLHEP::HepEulerAngles rotation(45*deg, 90*deg, 0*deg);
00741 position = position.rotate(rotation);
00742 }
00743 particleGun->SetParticlePosition(position);
00744 }
00745
00746 void PrimaryGeneratorAction::SetUniformPosition(){
00747 MyVGeometryParameter* pMyVGeometryParameter = MyDetectorManager::GetMyDetectorManager()->GetSvc(UP_SubDet)->get_GeometryParameter();
00748 if (!pMyVGeometryParameter){
00749 std::cout<<"ERROR: in PrimaryGeneratorAction::SetUniformPosition cannot find : "<<UP_SubDet<<"!!!"<<std::endl;
00750 G4Exception("PrimaryGeneratorAction::SetUniformPosition()",
00751 "InvalidInput", FatalException,
00752 "cannot ");
00753 }
00754 if ( UP_Type == "Simple" ){
00755 SimpleGeometryParameter* pSimpleGeometryParameter = dynamic_cast<SimpleGeometryParameter*> (pMyVGeometryParameter);
00756 if (!pSimpleGeometryParameter){
00757 std::cout<<"ERROR: in PrimaryGeneratorAction::SetUniformPosition cannot convert : "<<UP_SubDet<<" from MyVGeometryParameter to SimpleGeometryParameter!!!"<<std::endl;
00758 G4Exception("PrimaryGeneratorAction::SetUniformPosition()",
00759 "InvalidInput", FatalException,
00760 "cannot convert from MyVGeometryParameter to SimpleGeometryParameter");
00761 }
00762 int index = pSimpleGeometryParameter->get_VolIndex(UP_Volume);
00763 if ( index == -1 ){
00764 std::cout<<"ERROR: in PrimaryGeneratorAction::SetUniformPosition cannot find : "<<UP_Volume<<"!!!"<<std::endl;
00765 G4Exception("PrimaryGeneratorAction::SetUniformPosition()",
00766 "InvalidInput", FatalException,
00767 "cannot convert volume");
00768 }
00769 G4String sol_type = pSimpleGeometryParameter->get_SolidType(index);
00770 G4ThreeVector pos(1,0,0);
00771 G4int n = pSimpleGeometryParameter->get_RepNo(index);
00772 G4int ivol = G4UniformRand()*n;
00773 if ( sol_type == "Tubs" ){
00774 G4double RMax,RMin,length,StartPhi,SpanPhi;
00775 G4int iTubs = pSimpleGeometryParameter->get_SolidIndex(index);
00776 RMax = pSimpleGeometryParameter->get_Tubs_RMax(iTubs,ivol);
00777 RMin = pSimpleGeometryParameter->get_Tubs_RMin(iTubs,ivol);
00778 length = pSimpleGeometryParameter->get_Tubs_Length(iTubs,ivol);
00779 StartPhi = pSimpleGeometryParameter->get_Tubs_StartAng(iTubs,ivol);
00780 SpanPhi = pSimpleGeometryParameter->get_Tubs_SpanAng(iTubs,ivol);
00781
00782 G4double iz, iphi, ir;
00783 iz = G4UniformRand()*length -length/2;
00784 iphi = G4UniformRand()*SpanPhi + StartPhi;
00785 ir = G4UniformRand()*(RMax*RMax-RMin*RMin)+RMin*RMin;
00786 ir = sqrt(ir);
00787 pos.setPhi(iphi);
00788 pos.setRho(ir);
00789 pos.setZ(iz);
00790 }
00791 else{
00792 std::cout<<"ERROR: in PrimaryGeneratorAction::SetUniformPosition unsupported solid type: "<<sol_type<<"!!!"<<std::endl;
00793 G4Exception("PrimaryGeneratorAction::SetUniformPosition()",
00794 "InvalidInput", FatalException,
00795 "unsupported solid type");
00796 }
00797 G4double xp,yp,zp;
00798 xp = pSimpleGeometryParameter->get_PosX(index,ivol);
00799 yp = pSimpleGeometryParameter->get_PosY(index,ivol);
00800 zp = pSimpleGeometryParameter->get_PosZ(index,ivol);
00801 pos += G4ThreeVector(xp,yp,zp);
00802 G4String mot_volume = pSimpleGeometryParameter->get_MotherName(index);
00803 SimpleGeometryParameter * pmotSimpleGeometryParameter = 0;
00804 int temp_index = index;
00805 int mot_index = -1;
00806 while (mot_volume!="None"){
00807 pmotSimpleGeometryParameter = MyDetectorManager::GetMyDetectorManager()->GetParaFromVolume(mot_volume);
00808 int mot_index = pmotSimpleGeometryParameter->get_VolIndex(mot_volume);
00809 G4double mot_xp = pmotSimpleGeometryParameter->get_PosX(mot_index);
00810 G4double mot_yp = pmotSimpleGeometryParameter->get_PosY(mot_index);
00811 G4double mot_zp = pmotSimpleGeometryParameter->get_PosZ(mot_index);
00812 pos += G4ThreeVector(mot_xp,mot_yp,mot_zp);
00813 temp_index = mot_index;
00814 mot_volume = pmotSimpleGeometryParameter->get_MotherName(temp_index);
00815 }
00816 particleGun->SetParticlePosition(pos);
00817 }
00818 else{
00819 std::cout<<"ERROR: in PrimaryGeneratorAction::SetUniformPosition unsopported parameter class type: "<<UP_Type<<"!!!"<<std::endl;
00820 G4Exception("PrimaryGeneratorAction::SetUniformPosition()",
00821 "InvalidInput", FatalException,
00822 "unsopported parameter class type");
00823 }
00824 }
00825
00826 void PrimaryGeneratorAction::BuildHistoFromFile(){
00827 G4String dir_name = getenv("CONFIGUREDATAROOT");
00828 if (dir_name[dir_name.size()-1] != '/') dir_name.append("/");
00829
00830
00831 if (EnergyMode=="histo"){
00832 G4String full_infile_name = dir_name + EM_hist_filename;
00833
00834 fp = new TFile(full_infile_name.c_str());
00835 if (fp==NULL) {
00836 std::cout<<"ERROR: Can not find file: "<<full_infile_name<<"!!!"<<std::endl;
00837 G4Exception("PrimaryGeneratorAction::BuildHistoFromFile()",
00838 "InvalidInput", FatalException,
00839 "Can not find file");
00840 }
00841 TH1F* h = (TH1F*)fp->Get(EM_hist_histname.c_str());
00842 if(h==NULL){
00843 std::cout<<"ERROR: Can not find file: "<<full_infile_name<<"!!!"<<std::endl;
00844 G4Exception("PrimaryGeneratorAction::BuildHistoFromFile()",
00845 "InvalidInput", FatalException,
00846 "Can not find file");
00847 }
00848 EM_hist = h;
00849 }
00850
00851
00852 if (DirectionMode=="histo"){
00853 G4String full_infile_name = dir_name + DM_hist_filename;
00854 if (fp) delete fp;
00855 fp = new TFile(full_infile_name.c_str());
00856 if (fp==NULL) {
00857 std::cout<<"ERROR: Can not find file: "<<full_infile_name<<"!!!"<<std::endl;
00858 G4Exception("PrimaryGeneratorAction::BuildHistoFromFile()",
00859 "InvalidInput", FatalException,
00860 "Can not find file");
00861 }
00862 TH1F* h = (TH1F*)fp->Get(DM_hist_histname.c_str());
00863 if(h==NULL){
00864 std::cout<<"ERROR: Can not find file: "<<full_infile_name<<"!!!"<<std::endl;
00865 G4Exception("PrimaryGeneratorAction::BuildHistoFromFile()",
00866 "InvalidInput", FatalException,
00867 "Can not find file");
00868 }
00869 DM_hist = h;
00870 }
00871
00872 if (PositionMode =="histo"){
00873 G4String full_infile_name = dir_name + PM_hist_filename;
00874
00875 fp = new TFile(full_infile_name.c_str());
00876 if (fp==NULL) {
00877 std::cout<<"ERROR: Can not find file: "<<full_infile_name<<"!!!"<<std::endl;
00878 G4Exception("PrimaryGeneratorAction::BuildHistoFromFile()",
00879 "InvalidInput", FatalException,
00880 "Can not find file");
00881 }
00882 TObject* obj=fp->Get(PM_hist_histname.c_str());
00883 if(obj==NULL){
00884 std::cout<<"ERROR: Can not find file: "<<full_infile_name<<"!!!"<<std::endl;
00885 G4Exception("PrimaryGeneratorAction::BuildHistoFromFile()",
00886 "InvalidInput", FatalException,
00887 "Can not find file");
00888 }
00889 if(obj->InheritsFrom("TH3")){
00890 PM_hist = (TH3*) obj;
00891
00892 }
00893 else if (obj->InheritsFrom("THnSparse")) {
00894 PM_hist_sparse = (THnSparseF*) obj;
00895
00896 }
00897 else {
00898 std::cout<<"ERROR: Cannot generate positions from non-3D histogram: "<<PM_hist_histname<<"!!!"<<std::endl;
00899 G4Exception("PrimaryGeneratorAction::BuildHistoFromFile()",
00900 "InvalidInput", FatalException,
00901 "Wrong hist type");
00902 }
00903 }
00904
00905 }
00906
00907 void PrimaryGeneratorAction::root_get_para(){
00908 int iEvent = (int) fmod(root_index,root_num);
00909 m_TChain->GetEntry(iEvent);
00910 root_index++;
00911 }
00912
00913 void PrimaryGeneratorAction::root_build(){
00914 G4String dir_name = getenv("CONFIGUREDATAROOT");
00915 if (dir_name[dir_name.size()-1] != '/') dir_name.append("/");
00916 std::string m_TFile_name = dir_name + root_filename;
00917 if (root_filename[0] == '/')
00918 m_TFile_name = root_filename;
00919
00920 m_TChain = new TChain("t");
00921 m_TChain->Add(m_TFile_name.c_str());
00922 root_num = m_TChain->GetEntries();
00923 root_set_extra();
00924 if ( EnergyMode == "root" ){
00925 UseRoot = true;
00926 root_set_Energy();
00927 }
00928 if ( PositionMode == "root" ){
00929 UseRoot = true;
00930 root_set_Position();
00931 }
00932 if ( TimeMode == "root" ){
00933 UseRoot = true;
00934 root_set_Time();
00935 }
00936 if ( pidMode == "root" ){
00937 UseRoot = true;
00938 root_set_pid();
00939 }
00940 if ( RandMode == "root" ){
00941 root_set_Rand();
00942 }
00943 }
00944
00945 void PrimaryGeneratorAction::root_set_Position(){
00946 m_TChain->SetBranchAddress("x", &root_double[0]);
00947 m_TChain->SetBranchAddress("y", &root_double[1]);
00948 m_TChain->SetBranchAddress("z", &root_double[2]);
00949 }
00950 void PrimaryGeneratorAction::root_set_Energy(){
00951 m_TChain->SetBranchAddress("px", &root_double[3]);
00952 m_TChain->SetBranchAddress("py", &root_double[4]);
00953 m_TChain->SetBranchAddress("pz", &root_double[5]);
00954 }
00955 void PrimaryGeneratorAction::root_set_Time(){
00956 m_TChain->SetBranchAddress("t", &root_double[6]);
00957 }
00958 void PrimaryGeneratorAction::root_set_pid(){
00959 m_TChain->SetBranchAddress("pid", &root_int[0]);
00960 }
00961 void PrimaryGeneratorAction::root_set_Rand(){
00962
00963
00964 flag_R0 = m_TChain->SetBranchAddress("R0", &root_int[3]);
00965 flag_R1 = m_TChain->SetBranchAddress("R1", &root_int[4]);
00966 }
00967 void PrimaryGeneratorAction::root_set_extra(){
00968 flag_weight=m_TChain->SetBranchAddress("weight", &root_double[9]);
00969 flag_ox=m_TChain->SetBranchAddress("ox", &root_double[10]);
00970 flag_oy=m_TChain->SetBranchAddress("oy", &root_double[11]);
00971 flag_oz=m_TChain->SetBranchAddress("oz", &root_double[12]);
00972 flag_opx=m_TChain->SetBranchAddress("opx", &root_double[13]);
00973 flag_opy=m_TChain->SetBranchAddress("opy", &root_double[14]);
00974 flag_opz=m_TChain->SetBranchAddress("opz", &root_double[15]);
00975 flag_ot=m_TChain->SetBranchAddress("ot", &root_double[16]);
00976 flag_ipx=m_TChain->SetBranchAddress("ipx", &root_double[17]);
00977 flag_ipy=m_TChain->SetBranchAddress("ipy", &root_double[18]);
00978 flag_ipz=m_TChain->SetBranchAddress("ipz", &root_double[19]);
00979 flag_process=m_TChain->SetBranchAddress("process",&root_str[0]);
00980 flag_volume=m_TChain->SetBranchAddress("volume",&root_str[1]);
00981 flag_ppid=m_TChain->SetBranchAddress("ppid",&root_int[1]);
00982 flag_ptid=m_TChain->SetBranchAddress("ptid",&root_int[2]);
00983 }
00984
00985 void PrimaryGeneratorAction::ResetGen(G4String file_name){
00986 ReadCard(file_name);
00987 Initialize();
00988 }
00989
00990 void PrimaryGeneratorAction::ReadCard(G4String file_name){
00991 std::cout<<" -- PrimaryGeneratorAction: Reading from \""<<file_name<<"\""<<std::endl;
00992 Reset();
00993 if(file_name[0] != '/'){
00994 G4String dir_name = getenv("CONFIGUREROOT");
00995 if (dir_name[dir_name.size()-1] != '/') dir_name.append("/");
00996 file_name = dir_name + file_name;
00997 }
00998 std::ifstream fin_card(file_name);
00999 if ( !fin_card ){
01000 std::cout<<"generator file \""<<file_name<<"\" is not available!!!"<<std::endl;
01001 G4Exception("PrimaryGeneratorAction::PrimaryGeneratorAction()","Run0031",
01002 FatalException, "generator file is not available.");
01003 }
01004 std::stringstream buf_card;
01005 std::string s_card;
01006 while(getline(fin_card,s_card)){
01007 buf_card.str("");
01008 buf_card.clear();
01009 buf_card<<s_card;
01010 const char* c = s_card.c_str();
01011 int length = strlen(c);
01012 int offset = 0;
01013 for ( ; offset < length; offset++ ){
01014 if ( c[offset] != ' ' ) break;
01015 }
01016 if ( c[offset] == '#' ) continue;
01017 else if ( c[offset] == '/' && c[offset+1] == '/' ) continue;
01018 else if ( length - offset == 0 ) continue;
01019 std::string keyword;
01020 std::string temp;
01021 buf_card>>keyword;
01022 if ( keyword == "Type:" ){
01023 buf_card>>fType;
01024 }
01025 else if ( keyword == "Particle:" ){
01026 buf_card>>ParticleName;
01027 }
01028 else if ( keyword == "Z:" ){
01029 buf_card>>Z;
01030 }
01031 else if ( keyword == "A:" ){
01032 buf_card>>A;
01033 }
01034 else if ( keyword == "C:" ){
01035 buf_card>>C;
01036 }
01037 else if ( keyword == "E:" ){
01038 buf_card>>E;
01039 }
01040 else if ( keyword == "Direction:" ){
01041 buf_card>>temp;
01042 Theta = CalFormula(ReplaceMacro(temp))*deg;
01043 buf_card>>temp;
01044 Phi = CalFormula(ReplaceMacro(temp))*deg;
01045 }
01046 else if ( keyword == "PhiSpread:" ){
01047 buf_card>>temp;
01048 PhiSpread = CalFormula(ReplaceMacro(temp))*deg;
01049 }
01050 else if ( keyword == "Ephi:" ){
01051 buf_card>>temp;
01052 Ephi = CalFormula(ReplaceMacro(temp))*deg;
01053 }
01054 else if ( keyword == "Etheta:" ){
01055 buf_card>>temp;
01056 Etheta = CalFormula(ReplaceMacro(temp))*deg;
01057 }
01058 else if ( keyword == "Epsi:" ){
01059 buf_card>>temp;
01060 Epsi = CalFormula(ReplaceMacro(temp))*deg;
01061 }
01062 else if ( keyword == "ThetaSpread:" ){
01063 buf_card>>temp;
01064 ThetaSpread = CalFormula(ReplaceMacro(temp))*deg;
01065 }
01066 else if ( keyword == "MomAmp:" ){
01067 buf_card>>temp;
01068 Pa = CalFormula(ReplaceMacro(temp))*MeV;
01069 EnergyType = 0;
01070 }
01071 else if ( keyword == "MomSpread:" ){
01072 buf_card>>temp;
01073 MomSpread = CalFormula(ReplaceMacro(temp))*MeV;
01074 }
01075 else if ( keyword == "Ekin:" ){
01076 buf_card>>temp;
01077 Ekin = CalFormula(ReplaceMacro(temp))*MeV;
01078 EnergyType = 1;
01079 }
01080 else if ( keyword == "EkinSpread:" ){
01081 buf_card>>temp;
01082 EkinSpread = CalFormula(ReplaceMacro(temp))*MeV;
01083 }
01084 else if ( keyword == "Position:" ){
01085 buf_card>>temp;
01086 x = CalFormula(ReplaceMacro(temp))*mm;
01087 buf_card>>temp;
01088 y = CalFormula(ReplaceMacro(temp))*mm;
01089 buf_card>>temp;
01090 z = CalFormula(ReplaceMacro(temp))*mm;
01091 }
01092 else if ( keyword == "PosSpread:" ){
01093 buf_card>>temp;
01094 xSpread = CalFormula(ReplaceMacro(temp))*mm;
01095 buf_card>>temp;
01096 ySpread = CalFormula(ReplaceMacro(temp))*mm;
01097 buf_card>>temp;
01098 zSpread = CalFormula(ReplaceMacro(temp))*mm;
01099 }
01100 else if ( keyword == "PosLimit:" ){
01101 buf_card>>temp;
01102 PosLimit2 = CalFormula(ReplaceMacro(temp))*mm;
01103 PosLimit2 *= PosLimit2;
01104 }
01105 else if ( keyword == "Time:" ){
01106 buf_card>>temp;
01107 t = CalFormula(ReplaceMacro(temp))*ns;
01108 }
01109 else if ( keyword == "RandMode:" ){
01110 buf_card>>RandMode;
01111 }
01112 else if ( keyword == "EnergyMode:" ){
01113 buf_card>>EnergyMode;
01114 }
01115 else if ( keyword == "PositionMode:" ){
01116 buf_card>>PositionMode;
01117 }
01118 else if ( keyword == "TimeMode:" ){
01119 buf_card>>TimeMode;
01120 }
01121 else if ( keyword == "pidMode:" ){
01122 buf_card>>pidMode;
01123 }
01124 else if ( keyword == "DirectionMode:" ){
01125 buf_card>>DirectionMode;
01126 }
01127 else if ( keyword == "ThetaMode:" ){
01128 buf_card>>ThetaMode;
01129 }
01130 else if ( keyword == "PhiMode:" ){
01131 buf_card>>PhiMode;
01132 }
01133 else if ( keyword == "EMHFN:" ){
01134 buf_card>>EM_hist_filename;
01135 }
01136 else if ( keyword == "EMHHN:" ){
01137 buf_card>>EM_hist_histname;
01138 }
01139 else if ( keyword == "DMHFN:" ){
01140 buf_card>>DM_hist_filename;
01141 }
01142 else if ( keyword == "DMHHN:" ){
01143 buf_card>>DM_hist_histname;
01144 }
01145 else if ( keyword == "PMHFN:" ){
01146 buf_card>>PM_hist_filename;
01147 }
01148 else if ( keyword == "PMHHN:" ){
01149 buf_card>>PM_hist_histname;
01150 }
01151 else if ( keyword == "RFN:" ){
01152 buf_card>>root_filename;
01153 }
01154 else if ( keyword == "RTN:" ){
01155 buf_card>>root_treename;
01156 }
01157 else if ( keyword == "UIV:" ){
01158 buf_card>>UP_Type>>UP_SubDet>>UP_Volume;
01159 }
01160 else if ( keyword == "DEFINE:" ){
01161 G4String MacroName;
01162 G4String MacroValue;
01163 buf_card>>MacroName>>MacroValue;
01164
01165 MacroValue = ReplaceMacro(MacroValue);
01166 bool foundName = false;
01167 for (int i = 0; i < knownValueNames.size(); i++){
01168 if (knownValueNames[i]==MacroName){
01169 foundName = true;
01170 knownValues[i]=MacroValue;
01171 break;
01172 }
01173 }
01174 if (!foundName){
01175 knownValueNames.push_back(MacroName);
01176 knownValues.push_back(MacroValue);
01177 }
01178 }
01179 else{
01180 std::cout<<"In PrimaryGeneratorAction::ReadCard, unknown name: '"<<keyword<<"' in file "<<file_name<<std::endl;
01181 std::cout<<"Will ignore this line!"<<std::endl;
01182 }
01183 }
01184 fin_card.close();
01185 buf_card.str("");
01186 buf_card.clear();
01187 Dump();
01188 }
01189
01190 void PrimaryGeneratorAction::Reset(){
01191 fType = "simple";
01192 RandMode = "none";
01193 EnergyMode = "none";
01194 PositionMode = "none";
01195 TimeMode = "none";
01196 pidMode = "none";
01197 DirectionMode = "none";
01198 PhiMode = "none";
01199 ThetaMode = "none";
01200 ParticleName = "chargedgeantino";
01201 EM_hist_filename = "";
01202 EM_hist_histname = "";
01203 DM_hist_filename = "";
01204 DM_hist_histname = "";
01205 PM_hist_filename = "";
01206 PM_hist_histname = "";
01207 root_filename = "";
01208 root_treename = "";
01209 UP_SubDet = "";
01210 UP_Volume = "";
01211 UP_Type = "";
01212 EnergyType = 1;
01213 Z = 1;
01214 A = 1;
01215 C = 0;
01216 E = 0;
01217 Pa = 0;
01218 Ekin = 0;
01219 mass = 0;
01220 Px = 0;
01221 Py = 0;
01222 Pz = 0;
01223 Theta = 0;
01224 Phi = 0;
01225 x = 0;
01226 y = 0;
01227 z = 0;
01228 t = 0;
01229 xSpread = 0;
01230 ySpread = 0;
01231 zSpread = 0;
01232 PosLimit2 = 0;
01233 MomSpread = 0;
01234 EkinSpread = 0;
01235 ThetaSpread = 0;
01236 PhiSpread = 0;
01237 Ephi = 0;
01238 Etheta = 0;
01239 Epsi = 0;
01240 EM_hist = 0;
01241 DM_hist = 0;
01242 PM_hist = 0;
01243 root_num = 0;
01244 root_index = 0;
01245 UseRoot = false;
01246 flag_weight = false;
01247 flag_ox = false;
01248 flag_oy = false;
01249 flag_oz = false;
01250 flag_opx = false;
01251 flag_opy = false;
01252 flag_opz = false;
01253 flag_ipx = false;
01254 flag_ipy = false;
01255 flag_ipz = false;
01256 flag_ot = false;
01257 flag_process = false;
01258 flag_volume = false;
01259 flag_R0 = false;
01260 flag_R1 = false;
01261 flag_ppid = false;
01262 flag_ptid = false;
01263 }
01264
01265 void PrimaryGeneratorAction::Initialize(){
01266 if (fType == "simple" || fType == "stable" || fType == "ion" ){
01267 G4int n_particle = 1;
01268 if (particleGun) delete particleGun;
01269 particleGun = new G4ParticleGun(n_particle);
01270 }
01271 else {
01272 G4Exception("PrimaryGeneratorAction::PrimaryGeneratorAction()","Run0031",
01273 FatalException, "unknown generator type.");
01274 }
01275
01276 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
01277 G4ParticleDefinition* particle = particleTable->FindParticle(ParticleName);
01278 if (!particle){
01279 std::cout<<"ERROR: In PrimaryGeneratorAction::PrimaryGeneratorAction() Cannot find particle "<<ParticleName<<"!!!"<<std::endl;
01280 G4Exception("PrimaryGeneratorAction::PrimaryGeneratorAction()","Run0031",
01281 FatalException, "Cannot find particle.");
01282 }
01283 if (fType == "stable"){
01284 particle->SetPDGStable(true);
01285 }
01286 particleGun->SetParticleDefinition(particle);
01287 mass = particleGun->GetParticleDefinition()->GetPDGMass();
01288 G4ThreeVector dir(1,0,0);
01289 dir.setTheta(Theta);
01290 dir.setPhi(Phi);
01291 particleGun->SetParticleMomentumDirection(dir.unit());
01292 if (EnergyType==0){
01293 Ekin = sqrt(Pa*Pa+mass*mass)-mass;
01294 }
01295 particleGun->SetParticleEnergy(Ekin);
01296 particleGun->SetParticlePosition(G4ThreeVector(x,y,z));
01297 particleGun->SetParticleTime(t);
01298
01299 if (EnergyMode == "histo" || DirectionMode == "histo" || PositionMode == "histo" ){
01300 BuildHistoFromFile();
01301 }
01302 UseRoot = false;
01303 if ( EnergyMode == "root" || PositionMode == "root" || TimeMode == "root" || pidMode == "root" || RandMode == "root"){
01304 UseRoot = true;
01305 root_build();
01306 }
01307 root_double[9]=1;
01308 root_double[10]=-1;
01309 root_double[11]=-1;
01310 root_double[12]=-1;
01311 root_double[13]=-1;
01312 root_double[14]=-1;
01313 root_double[15]=-1;
01314 root_double[16]=-1;
01315 root_str[0]=0;
01316 root_str[1]=0;
01317 root_int[1]=-1;
01318 root_int[2]=-1;
01319 }
01320
01321 void PrimaryGeneratorAction::Dump(){
01322 std::cout<<"---------------PrimaryGeneratorAction---------------------"<<std::endl;
01323 std::cout<<"Type: "<<fType<<std::endl;
01324 if (fType=="ion"){
01325 std::cout<<"Z: "<<Z<<std::endl;
01326 std::cout<<"A: "<<A<<std::endl;
01327 std::cout<<"C: "<<C<<std::endl;
01328 std::cout<<"E: "<<E<<std::endl;
01329 }
01330 else{
01331 std::cout<<"Particle: "<<ParticleName<<std::endl;
01332 }
01333 std::cout<<"Default Momentum Direction: theta = "<<Theta/deg<<"deg, phi = "<<Phi/deg<<"deg"<<std::endl;
01334 std::cout<<"Default Phi Spread: "<<PhiSpread/deg<<"deg"<<std::endl;
01335 std::cout<<"Default Ephi Spread: "<<Ephi/deg<<"deg"<<std::endl;
01336 std::cout<<"Default Etheta Spread: "<<Etheta/deg<<"deg"<<std::endl;
01337 std::cout<<"Default Epsi Spread: "<<Epsi/deg<<"deg"<<std::endl;
01338 std::cout<<"Default Theta Spread: "<<ThetaSpread/deg<<"deg"<<std::endl;
01339 if (EnergyType==0){
01340 std::cout<<"Default Momentum Amplitude: "<<Pa/MeV<<"MeV"<<std::endl;
01341 std::cout<<"Default Momentum Spread(MeV/c): ("<<MomSpread/MeV<<std::endl;
01342 }
01343 else if (EnergyType==1){
01344 std::cout<<"Default Kinetic Energy: "<<Ekin/MeV<<"MeV"<<std::endl;
01345 std::cout<<"Default Energy Spread(MeV): ("<<EkinSpread/MeV<<std::endl;
01346 }
01347 std::cout<<"Default Position(mm): ("<<x/mm<<", "<<y/mm<<", "<<z/mm<<")"<<std::endl;
01348 std::cout<<"Default Position Spread(mm): ("<<xSpread/mm<<", "<<ySpread/mm<<", "<<zSpread/mm<<")"<<std::endl;
01349 std::cout<<"Default Position Limit(mm): ("<<sqrt(PosLimit2)/mm<<std::endl;
01350 std::cout<<"Default Time(ns): ("<<t/ns<<std::endl;
01351 std::cout<<"EnergyMode: "<<EnergyMode<<std::endl;
01352 std::cout<<"PositionMode: "<<PositionMode<<std::endl;
01353 std::cout<<"TimeMode: "<<TimeMode<<std::endl;
01354 std::cout<<"pidMode: "<<pidMode<<std::endl;
01355 std::cout<<"Uniform In sub-detector: "<<UP_SubDet<<std::endl;
01356 std::cout<<" Volume: "<<UP_Volume<<std::endl;
01357 std::cout<<" Type: "<<UP_Type<<std::endl;
01358 std::cout<<"DirectionMode: "<<DirectionMode<<std::endl;
01359 std::cout<<"PhiMode: "<<PhiMode<<std::endl;
01360 std::cout<<"ThetaMode: "<<ThetaMode<<std::endl;
01361 std::cout<<"File Name for EnergyMode = histo: "<<EM_hist_filename<<std::endl;
01362 std::cout<<"Histogram Name for EnergyMode = histo: "<<EM_hist_histname<<std::endl;
01363 std::cout<<"File Name for DirectionMode = histo: "<<DM_hist_filename<<std::endl;
01364 std::cout<<"Histogram Name for DirectionMode = histo: "<<DM_hist_histname<<std::endl;
01365 std::cout<<"File Name for PositionMode = histo: "<<PM_hist_filename<<std::endl;
01366 std::cout<<"Histogram Name for PositionMode = histo: "<<PM_hist_histname<<std::endl;
01367 std::cout<<"File Name for Energy/Position/TimeMode = root: "<<root_filename<<std::endl;
01368 std::cout<<"Tree Name for Energy/Position/TimeMode = root: "<<root_treename<<std::endl;
01369 std::cout<<"---------------------------------------------------------"<<std::endl;
01370 }