8.7.  Enhanced Trajectory Drawing

8.7.1.  Default Configuration

Trajectory drawing styles are specified through trajectory drawing models. Each drawing model has a default configuration provided through a G4VisTrajContext object. The default context settings are shown below.

Property Default Setting
Line colour grey
Line visibility true
Draw line true
Draw auxiliary points false
Auxiliary point type squares
Auxiliary point size 2 pixels or mm*
Auxiliary point size type screen
Auxiliary point fill style filled
Auxiliary point colour magenta
Auxiliary point visibility true
Draw step point false
Step point type circles
Step point size 2 pixels or mm*
Step point size type screen
Step point fill style filled
Step point colour yellow
Step point visibility true
Time slice interval 0

* Depending on size type. If size type == screen, pixels are assumed and no unit need be supplied. If size type == world, a unit must be supplied, e.g., 10 cm.


  • Different visualisation drivers handle trajectory configuration in different ways, so trajectories may not necessarily get displayed as you have configured them.

8.7.2.  Trajectory Drawing Models

A trajectory drawing model can override the default context according to the properties of a given trajectory. The following models are supplied with the Geant4 distribution:

  • G4TrajectoryGenericDrawer (generic)
  • G4TrajectoryDrawByCharge (drawByCharge)
  • G4TrajectoryDrawByParticleID (drawByParticleID)
  • G4TrajectoryDrawByOriginVolume (drawByOriginVolume)
  • G4TrajectoryDrawByTouchedVolume (drawByTouchedVolume)
  • G4TrajectoryDrawByAttribute (drawByAttribute)

Both the context and model properties can be configured by the user. The models are described briefly below, followed by some example configuration commands.


This model simply draws all trajectories in the same style, with the properties provided by the context.


This is the default model - if no model is specified by the user, this model will be constructed automatically. The trajectory lines are coloured according to charge, with all other configuration parameters provided by the default context. The default colouring scheme is shown below.

        Charge          Colour  
        1               Blue
        -1              Red
        0               Green


This model colours trajectory lines according to particle type. All other configuration parameters are provided by the default context. By default, all trajectories are coloured grey. Chosen particle types can be highlighted with specified colours.


This model colours trajectory lines according to the trajectory's originating volume name. The volume can be either a logical or physical volume. Physical volume takes precedence over logical volume. All trajectories are coloured grey by default.


This model colours trajectory lines if it touches one or more volumes according to the physical volume name(s). It requires rich trajectories, G4RichTrajectory (/vis/scene/add/trajectories rich). All trajectories are coloured grey by default.


This model draws trajectories based on the HepRep style attributes associated with trajectories. Each attribute drawer can be configured with interval and/or single value data. A new context object is created for each interval/single value. This makes it possible to have different step point markers etc, as well as line colour for trajectory attributes falling into different intervals, or matching single values. The single value data should override the interval data, allowing specific values to be highlighted. Units should be specified on the command line if the attribute unit is specified either as a G4BestUnit or if the unit is part of the value string.

8.7.3.  Controlling from Commands

Multiple trajectory models can be created and configured using commands in the "/vis/modeling/trajectories/" directory. It is then possible to list available models and select one to be current.

Model configuration commands are generated dynamically when a model is instantiated. These commands apply directly to that instance. This makes it possible to have multiple instances of the drawByCharge model for example, each independently configurable through it's own set of commands.

See the interactive help for more information on the available commands.  Example commands

#Create a generic model named generic-0 by default


#Configure context to colour all trajectories red

/vis/modeling/trajectories/generic-0/default/setLineColour red

#Create a drawByCharge model named drawCharge-0 by default (Subsequent models will be named drawByCharge-1, drawByCharge-2, etc.)


#Create a drawByCharge model named testChargeModel

/vis/modeling/trajectories/create/drawByCharge testChargeModel

#Configure drawByCharge-0 model

/vis/modeling/trajectories/drawByCharge-0/set 1 red
/vis/modeling/trajectories/drawByCharge-0/set -1 red
/vis/modeling/trajectories/drawByCharge-0/set 0 white

#Configure testCharge model through G4Colour components

/vis/modeling/trajectories/testChargeModel/setRGBA 1 0 1 1 1
/vis/modeling/trajectories/testChargeModel/setRGBA -1 0.5 0.5 0.5 1
/vis/modeling/trajectories/testChargeModel/setRGBA 0 1 1 0 1

#Create a drawByParticleID model named drawByParticleID-0


#Configure drawByParticleID-0 model

/vis/modeling/trajectories/drawByParticleID-0/set gamma red
/vis/modeling/trajectories/drawByParticleID-0/setRGBA e+ 1 0 1 1

#List available models


#select drawByParticleID-0 to be current

/vis/modeling/trajectories/select drawByParticleID-0

#Create a drawByAttribute model named drawByAttribute-0


#Configure drawByAttribute-0 model

/vis/modeling/trajectories/drawByAttribute-0/verbose true

#Select attribute "CPN"

/vis/modeling/trajectories/drawByAttribute-0/setAttribute CPN

#Configure single value data

/vis/modeling/trajectories/drawByAttribute-0/addValue brem_key  eBrem
/vis/modeling/trajectories/drawByAttribute-0/addValue annihil_key annihil
/vis/modeling/trajectories/drawByAttribute-0/addValue decay_key Decay
/vis/modeling/trajectories/drawByAttribute-0/addValue muIon_key muIoni
/vis/modeling/trajectories/drawByAttribute-0/addValue eIon_key  eIoni

/vis/modeling/trajectories/drawByAttribute-0/brem_key/setLineColour     red
/vis/modeling/trajectories/drawByAttribute-0/annihil_key/setLineColour  green
/vis/modeling/trajectories/drawByAttribute-0/decay_key/setLineColour    cyan
/vis/modeling/trajectories/drawByAttribute-0/eIon_key/setLineColour     yellow
/vis/modeling/trajectories/drawByAttribute-0/muIon_key/setLineColour magenta

#Create a drawByAttribute model named drawByAttribute-1


#Select "IMag" attribute

/vis/modeling/trajectories/drawByAttribute-1/setAttribute IMag

#Configure interval data

/vis/modeling/trajectories/drawByAttribute-1/addInterval interval1 0.0 keV 2.5MeV
/vis/modeling/trajectories/drawByAttribute-1/addInterval interval2 2.5 MeV 5 MeV
/vis/modeling/trajectories/drawByAttribute-1/addInterval interval3 5 MeV 7.5 MeV
/vis/modeling/trajectories/drawByAttribute-1/addInterval interval4 7.5 MeV 10 MeV
/vis/modeling/trajectories/drawByAttribute-1/addInterval interval5 10 MeV 12.5 MeV
/vis/modeling/trajectories/drawByAttribute-1/addInterval interval6 12.5 MeV 10000 MeV

/vis/modeling/trajectories/drawByAttribute-1/interval1/setLineColourRGBA 0.8 0 0.8 1
/vis/modeling/trajectories/drawByAttribute-1/interval2/setLineColourRGBA 0.23 0.41 1 1
/vis/modeling/trajectories/drawByAttribute-1/interval3/setLineColourRGBA 0 1 0 1
/vis/modeling/trajectories/drawByAttribute-1/interval4/setLineColourRGBA 1 1 0 1
/vis/modeling/trajectories/drawByAttribute-1/interval5/setLineColourRGBA 1 0.3 0 1
/vis/modeling/trajectories/drawByAttribute-1/interval6/setLineColourRGBA 1 0 0 1

#Create a drawByEncounteredVolume model named drawByEncounteredVolume-0


#Change the color for a specific encountered shape

  /vis/modeling/trajectories/drawByEncounteredVolume-0/set Shape1 cyan

8.7.4.  Controlling from Compiled Code

It is possible to use the enhanced trajectory drawing functionality in compiled code as well as from commands. Multiple trajectory models can be instantiated, configured and registered with G4VisManager. Once registered, the models are owned by G4VisManager, and must not be deleted by the user.

Only one model may be current. For example:

  G4VisManager* visManager = new G4VisExecutive;

  G4TrajectoryDrawByParticleID* model = new G4TrajectoryDrawByParticleID;
  G4TrajectoryDrawByParticleID* model2 = new G4TrajectoryDrawByParticleID("test");

  model->Set("gamma", "green");
  model->Set("e+", "magenta");
  model->Set("e-", G4Colour(0.3, 0.3, 0.3));



8.7.5.  Drawing by time

To draw by time, you need to use G4RichTrajectory, for example:

/vis/scene/add/trajectories rich


/vis/scene/add/trajectories rich smooth

When you run, you need to create a trajectory model and set the time slice interval (remembering that paticles are often relativistic and travel 30 cm/ns):

/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true
/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 5
/vis/modeling/trajectories/drawByCharge-0/default/setDrawAuxPts true
/vis/modeling/trajectories/drawByCharge-0/default/setAuxPtsSize 5
/vis/modeling/trajectories/drawByCharge-0/default/setTimeSliceInterval 0.1 ns

and use a graphics driver that can display by time:

/vis/open OGL
/vis/scene/add/trajectories rich
/vis/ogl/set/startTime 0.5 ns
/vis/ogl/set/endTime 0.8 ns

A good way to see the particles moving through the detector is:

/vis/ogl/set/fade 1
/vis/ogl/set/displayHeadTime true
/control/alias timeRange 1
/control/loop movie.loop startTime -{timeRange} 40 0.1

where fade gives a vapour-trail effect, displayHeadTime displays the time of the leading edge as 2D text, and movie.loop is a macro file:

/vis/ogl/set/startTime {startTime} ns {timeRange} ns

From there, it's straightforward to Section 8.10 make a movie.