Custom trajectory colors

Hi,

I am trying to visualize trajectories in SteppingAction in Geant4 v11.1.1. I have the following code which is inspired by the code in the users tutorial:

void SteppingAction::UserSteppingAction(const G4Step* step)
{
  G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();

  if (pVVisManager) {

    G4ThreeVector preStepPoint = step->GetPreStepPoint()->GetPosition();
    G4ThreeVector postStepPoint = step->GetPostStepPoint()->GetPosition();
    G4double charge = step->GetTrack()->GetDynamicParticle()->GetCharge();
    G4String particleName = step->GetTrack()->GetParticleDefinition()->GetParticleName();

    G4Polyline polyline;
    G4Colour colour;
    if      (charge < 0.) colour = G4Colour(0., 0., 1.); 
    else if (charge > 0.) colour = G4Colour(0., 1., 0.); 
    else                  colour = G4Colour(1., 0., 0.); 

    G4VisAttributes attribs(colour);
    polyline.SetVisAttributes(attribs);
    polyline.push_back(preStepPoint);
    polyline.push_back(postStepPoint);

    //----- Call a drawing method for G4Polyline
    pVVisManager -> Draw(polyline);
  }
}

This should draw proton as green, but I still see it as blue, no matter what I do. I did check, that in each step, the if statement gets the correct charge and selects the correct color.

I know there is a better way to do this particular thing by /vis/modeling/trajectories. This is just a testing example for me, so I can implement more sophisticated selections on particle properties and colors.

Cheers,
Martin

That’s an old way. Now we have

/vis/scene/add/trajectories

Look at examples/base/B1/vis/Mac and read the Book for Application Developers.

Mmm. I see you are “just testing”. You should know that C++ in-line drawing doesn’t work in multithreading mode, so all your work would be ignored. That you get anything at all must be because you have issued some commands to draw trajectories.

I was able to draw trajectories using

/vis/scene/add/trajectories

but I was hoping it can be overridden by the tutorial code I have shown. Do I understand it correctly, that one should always use

/vis/modeling/trajectories/create/drawByAttribute

or the other trajectories models to control visualisation?

/vis/modeling/trajectories/create/drawByAttribute is quite advanced. It is used for trajectories with attributes. (If the tutorial suggests “one should always use” it, it is wrong, and I will try to get it corrected. To which tutorial were you referring?)

Have a look at examples/basic/B1/vis.mac. It shows how to change colour.

Sorry, I have probably phrased that wrongly. The tutorial suggests using all trajectory drawing models like G4TrajectoryDrawByCharge, G4TrajectoryDrawByParticleID, etc. These are well explained in for instance examples/basic/B1/vis.mac.

However, it also suggests using the method I have in my original post.

Ah, OK. Your original posting—drawing from C++ code—is fine, but, as I said, will not work in multithreading mode. The argument is: allowing multiple threads simultaneously to draw doesn’t make much sense (which event are you seeing?) and would require some sophisticated software development. Since visualisation is predominantly used for diagnosis on relatively few events, sequential mode would be adequate.

Let us know if you’re still having problems.

John

I found the problem. In my .mac, I had /vis/scene/add/trajectories, which draw over my custom color lines. By removing this command, I can use the code I have posted. Thank you @allison.

There is still a small cosmetic problem. The first event is not showing the tracks when I execute the following macro:

/run/initialize

/tracking/verbose 1
/vis/open OGL 600x600-0+0
/vis/drawVolume
/run/beamOn 1

For the second and all subsequent events when I run /run/beamOn 1, I get to see the tacks. The only way to see the first event is to omit the /run/beamOn 1 in the .mac file and run it myself, which is not ideal. /vis/viewer/flush or vis/viewer/refresh have no effect. Is there some other command that may show the tracks?

OK. Great! I suspected this.

“first event is not showing”: that’s curious. Are you using the X11 or the Qt viewer? We are having issues with the Qt viewer (OGLSQt) when the /run/beamOn is in the macro file.

I have OGLSQt. With /vis/scene/add/trajectories I can see even the first event.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.