Trajectories doesnt show on decay of geantino particle

I have a little problem with the accumulate trajectories visualization when i change from a “proton” particle on my generator.cc to a “geantino” particle. (see both images - note that both of them have 100 event) It desnt show any trajectories at all when i made this changes. At the end i will share the generator.cc, generator.hh and physics.cc and physics.hh files



//////////////////////////////////////////////////////////////
PHYSICS.CC
#include “Physics.hh”

MyPhysicsList::MyPhysicsList()
{
RegisterPhysics (new G4EmStandardPhysics());
RegisterPhysics (new G4OpticalPhysics());
RegisterPhysics (new G4DecayPhysics());
RegisterPhysics (new G4RadioactiveDecayPhysics());
}

MyPhysicsList::~MyPhysicsList()
{}
//////////////////////////////////////////////////////////////////////////
PHYSICS.HH
#ifndef PHYSICS_HH
#define PHYSICS_HH

#include “G4VModularPhysicsList.hh”
#include “G4EmStandardPhysics.hh”
#include “G4OpticalPhysics.hh”
#include “G4DecayPhysics.hh”
#include “G4RadioactiveDecayPhysics.hh”

class MyPhysicsList : public G4VModularPhysicsList
{
public:
MyPhysicsList();
~MyPhysicsList() override;
};

#endif
///////////////////////////////////////////////////////////////////////////////
GENERATOR.CC
#include “generator.hh”

MyPrimaryGenerator::MyPrimaryGenerator()
{
fParticleGun = new G4ParticleGun(1);

G4ParticleTable *particleTable = G4ParticleTable::GetParticleTable();
G4ParticleDefinition *particle = particleTable->FindParticle("geantino");

G4ThreeVector pos(0.,0.,0.);
G4ThreeVector mom(0.,0.,1.);

fParticleGun->SetParticlePosition(pos);
fParticleGun->SetParticleMomentumDirection(mom);
fParticleGun->SetParticleMomentum(0.*keV);
fParticleGun->SetParticleEnergy(0.*keV);
fParticleGun->SetParticleDefinition(particle);

}

MyPrimaryGenerator::~MyPrimaryGenerator()
{
delete fParticleGun;
}

void MyPrimaryGenerator::GeneratePrimaries(G4Event *anEvent)
{
G4ParticleDefinition *particle = fParticleGun->GetParticleDefinition();

if(particle == G4Geantino::Geantino())
{
    G4int Z = 27;
    G4int A = 60;

    G4double charge = 0.*eplus;
    G4double energy = 0.*keV;

    G4ParticleDefinition *ion =G4IonTable::GetIonTable()->GetIon(Z, A, energy);

    fParticleGun->SetParticleDefinition(ion);
    fParticleGun->SetParticleCharge(charge);
}

fParticleGun->GeneratePrimaryVertex(anEvent);

}

//////////////////////////////////////////////////////////////////////////////////////////////7
GENERATOR.HH
#ifndef GENERATOR_HH
#define GENERATOR_HH

#include “G4VUserPrimaryGeneratorAction.hh”
#include “G4ParticleGun.hh”
#include “G4SystemOfUnits.hh”
#include “G4ParticleTable.hh”
#include “G4Geantino.hh”
#include “G4IonTable.hh”
#include “G4ChargedGeantino.hh”

class MyPrimaryGenerator : public G4VUserPrimaryGeneratorAction
{
public:
MyPrimaryGenerator();
~MyPrimaryGenerator() override;

virtual void GeneratePrimaries(G4Event*);

private:
G4ParticleGun *fParticleGun;
};

#endif

///////////////////////////////////////////////////////
VIS.MAC
/run/initialize
/vis/open OGL
/vis/viewer/set/viewpointVector 1 1 1
/vis/drawVolume
/vis/viewer/set/autoRefresh true
/vis/scene/add/trajectories smooth
/vis/scene/add/scale 10 cm
/vis/scene/add/axes
/vis/scene/add/eventID
/vis/scene/endOfEventAction accumulate

///////////////////////////////////////////////////////////////////////
SIM.CC

#include

#include “G4RunManagerFactory.hh”
#include “G4UImanager.hh”
//#include “G4VisManager.hh”
#include “G4VisExecutive.hh”
#include “G4UIExecutive.hh”
#include “G4MTRunManager.hh”
//#include “G4UIcontrolMessenger.hh”

#include “construction.hh”
#include “Physics.hh”
#include “action.hh”

int main(int argc, char** argv)
{
#ifndef G4MULTITHREADED
G4MTRunManager *runManager= new G4MTRunManager(); //MULTITHREADE MODE
#else
G4RunManager *runManager= new G4RunManager(); //SINGLETHREADE MODE

#endif

runManager->SetUserInitialization(new MyDetectorConstruction);
runManager->SetUserInitialization(new MyPhysicsList);
runManager->SetUserInitialization(new MyActionInitialization);

G4UIExecutive *ui = 0;

if(argc == 1)
{
 	ui = new G4UIExecutive(argc, argv);
 }

G4VisManager *visManager= new G4VisExecutive();
visManager->Initialize();

G4UImanager *UImanager= G4UImanager::GetUIpointer();

if(ui)
{

	UImanager->ApplyCommand("/control/execute vis.mac");
	ui->SessionStart();
}
else
{
	G4String command = "/control/execute";
	G4String filename = argv[1];
	UImanager->ApplyCommand(command+filename);
}
return 0;

};

//////////////////////////////////////////////////////////////////
_Geant4 Version:_geant4-v11.2.1
_Operating System:_Linux 23.10
Compiler/Version:
_CMake Version:_CMake Version 3.27.4


Quick response…

Check the value of the pointer “particle” in your constructor.

Otherwise, looks OK. But the particle gun comes with some messengers, so no need to set it up in your constructor - use, e.g., “/gun/particle geantino” in your “.mac” file.

I’m actually travelling right now. Get back if you need and I’ll look deeper this evening.

John

Hi John,

Thanks for your reply. I’m new with Geant4 and don’t know what exactly what value I should check on the constructor of the particle, so it seems I’m going to need your help and experience :slight_smile:
Best Regards

Hi Jorsh

When I said, “Check the value of the pointer “particle” in your constructor,” that was really a programming issue, i.e., not about Geant4. You should always (well, mostly, unless you’re sure) check the value of a result. In this case

G4ParticleDefinition *particle = particleTable->FindParticle("geantino");

particleTable->FindParticle returns a variable called particle, a pointer to a G4ParticleDefinition. It may or may not be valid. For example, if “geantino” is misspelt, it would be invalid. So generally, in C/C++, you should check. Perhaps the simplest way is to

  assert(particle);

which would cause the program to exit. (You may need #include <cassert> at the top.) Or

  if(!particle) {...print some message and `exit(1)`...}

Anyway, that was just a thought. It might be perfectly OK.

Where did you get all this code from? What are you trying to do? Your GeneratePrimaries looks very strange. You test fParticleGun->GetParticleDefinition() (which, don’t forget, might be invalid), then if geantino, fParticleGun->SetParticleDefinition(ion);. What on earth are you trying to do?

In any case, all you need to do is instantiate a G4ParticleGun

MyPrimaryGenerator::MyPrimaryGenerator()
{
  fParticleGun = new G4ParticleGun;
}

and

void MyGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
  fParticleGun->GeneratePrimaryVertex(anEvent);
}

and use the commands /gun/....

Hope this helps.

John

Sorry. That should be

void MyPrimaryGenerator::GeneratePrimaries(G4Event* anEvent)
{
  fParticleGun->GeneratePrimaryVertex(anEvent);
}

Hi again Jorsh

There is a more general particle generator called G4GeneralParticleSource. Here is how I use it (below). It comes with lots of messengers and the associated commands - /gps/.... See Geant4 General Particle Source — Book For Application Developers 11.2 documentation.

For example, if you want to make a beam of ions, type this in at initialisation or put it in one of your .mac files;

/gps/particle ion
/gps/ion 27 60
/gps/position 0 0 -1 m
/gps/energy 1 MeV
/gps/direction 0 0 1

or whatever.

If this cannot handle your use case, then you will have to write your own generator action, but I would try this simple approach first.

Hope that helps.

John

MyGeneratorAction.hh:

#ifndef MyGeneratorAction_hh
#define MyGeneratorAction_hh 1
#include "G4VUserPrimaryGeneratorAction.hh"
class G4GeneralParticleSource;
class MyGeneratorAction : public G4VUserPrimaryGeneratorAction
{
public:
  MyGeneratorAction();
  ~MyGeneratorAction();
  void GeneratePrimaries(G4Event*);
private:
  G4GeneralParticleSource* fpParticleGun;
}
#endif

MyGeneratorAction.cc:

#include "MyGeneratorAction.hh"
#include "G4GeneralParticleSource.hh"
MyGeneratorAction::MyGeneratorAction()
{
  fpParticleGun  = new G4GeneralParticleSource;
}
MyGeneratorAction::~MyGeneratorAction()
{
  delete fpParticleGun;
}
void MyGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
  fpParticleGun->GeneratePrimaryVertex(anEvent);
}
1 Like