Message: distribution of energy spectrum Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

Question distribution of energy spectrum 

Forum: Particles
Date: 23 May, 2014
From: shayma nader <shayma nader>

Hi Please i want to make the energy spectrum of F18 in a geant4 Primary generator action but my code is wrong please i need your help.

i take the fit equation from the plot of the energy spectrum of F18 was made before by a Geant4 and tried to wrote this code to obtain the same spectrum.

This is my code

...........................................

#include "PSPrimaryGeneratorAction.hh"

#include "PSDetectorConstruction.hh"
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "G4ChargedGeantino.hh"
#include "Randomize.hh"
#include <iostream>
#include <fstream>
using namespace std;

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

PSPrimaryGeneratorAction::PSPrimaryGeneratorAction(PSDetectorConstruction* DC) :Detector(DC) {

  G4int n_particle = 1;
  particleGun  =  new G4ParticleGun(n_particle);

 //G4cout<<"-----------------------------------------------------------"<<G4endl;
 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
  G4String particleName;
  G4ParticleDefinition* particle
                    = particleTable->FindParticle(particleName="e+");
  particleGun->SetParticleDefinition(particle);

}

PSPrimaryGeneratorAction::~PSPrimaryGeneratorAction()
{
  delete particleGun;
}

void PSPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
  //this function is called at the begining of event
  ofstream output("psp3.txt",ios::app);

  int divided_by=100;
  double Emax=0.635;
  double E= Emax/divided_by;
  double Ee= E;

  double Vec_E[101];
  double N_E[101];

  int Number=0;
  for (G4int i=1;i<divided_by+1; i++) 
  {
      Vec_E[i]=i*Emax/divided_by;
      Number=Number+5715*Vec_E[i]*Vec_E[i]*Vec_E[i] - 8030*Vec_E[i]*Vec_E[i] + 2837*Vec_E[i] - 30.43;
      N_E[i]=Number;
  }

  double test=G4UniformRand()*Number;
  double Energy=-1;
  for (G4int i=1;i<divided_by; i++) 
  {
     if(test<N_E[1]){Energy=Vec_E[1];i=divided_by+1;}
     if(N_E[i]<test<N_E[i+1]){Energy=Vec_E[i+1];i=i=divided_by+1;}     
  }

  particleGun->SetParticleEnergy(Energy);

  cout<<Energy<<"     "<<endl;
  output<<Energy<<endl;

  G4double Pi = 3.14159;
  G4double cosTheta = 2*G4UniformRand() - 1., phi = twopi*G4UniformRand();
  G4double sinTheta = std::sqrt(1. - cosTheta*cosTheta);
  G4double Px = sinTheta*std::cos(phi),
           Py = sinTheta*std::sin(phi),
           Pz = cosTheta;

particleGun->SetParticleMomentumDirection(G4ThreeVector(Px,Py,Pz)); particleGun->SetParticlePosition(G4ThreeVector(0.0*mm,0.0*mm,0.0*mm)); particleGun->GeneratePrimaryVertex(anEvent);

}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

   Attachment:
      http://hypernews.slac.stanford.edu/HyperNews/geant4/get/AUX/2014/05/23/11.02-38177-PrimaryGeneratorAction.cc

 Add Message Add Message
to: "distribution of energy spectrum"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]