Home | News | Documentation | Download

Draw multiple trajectories on a TCanvas


_ROOT Version: 6.12/06
_Platform: Fedora 27
_Compiler: gcc version 7.3.1

I am trying to accomplish the following task : I have a set of start (X, Y, Z) and end (X', Y', Z') coordinates and I would like to plot on a canvas a straight line from the start to the and point.

Any idea on how to do it?
It has to be mentioned that the there is a big number of point (1e6) therefore I thought it was not convenient to define 1e6 TGraphs and draw them on the same canvas.

Thanks in advance!


Those are TLine objects. They are fairly cheap; you should be able to draw 1E6 of them…

But it’ll probably be better to implement your own graphical object, with its own painter - but that’s much more work, and only worth it if your code is meant to be used by many and / or supposed to survive for many years.

NB, if these lines are all connected then you can use a TPolyLine!


Thank you very much for your reply which helped a lot!

Is there a TLine that can be constructed in a 3D world i,e. with (x, y, z) coordinates?

Thanks in advance!


@agheata @bellenot is there something in the graphical TGeo world that you could recommend to Atha?


Take a look in the Basic 3D graphics, for example the TPolyLine3D


Thank you very much everyone for their help!
I was able to do what I wanted by using the TPolyLine3D as suggested by many of you.

An example code (maybe not the most sophisticated so I am open to suggestions on making it better) follows!

#include "TH3.h"
#include "TFile.h"
#include "TArrayD.h"
#include "TCanvas.h"
#include "TPolyLine3D.h"

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;
using std::cout;
using std::endl;

void trajectories3D(TString filenane_start, TString filename_end, int n_trajectories){

	std::ifstream file_start; //Assumes an ascii file with 3 columns (x, y, z) - start points
	std::ifstream file_end; //Assumes an ascii file with 3 columns (x, y, z) -  end  points
	std::vector<float> X1, X2, Y1, Y2, Z1, Z2;
	float x = 0, y = 0, z = 0;
		file_start >> x >> y >> z;
		if (!file_start.good()) break;
    x=0.; y=0.; z=0.;
		file_end >> x >> y >> z;
		if (!file_end.good()) break;

	TH3D *hdummy = new TH3D("hdummy", "", 2, -15, 15, 2, -15, 15, 2, -0.5, 20.5);
	hdummy->GetXaxis()->SetRangeUser(-15, 15);
	hdummy->GetYaxis()->SetRangeUser(-15, 15);
	hdummy->GetZaxis()->SetRangeUser(-0.5, 10);

	TPolyLine3D *trajectories[n_trajectories];
	//TPolyLine3D *test = new TPolyLine3D(2) [n_trajectories];
	for (int i=0; i<n_trajectories; ++i){
		trajectories[i] = new TPolyLine3D(2);
		trajectories[i]->SetPoint(0, X1[i], Y1[i], Z1[i]);
		trajectories[i]->SetPoint(1, X2[i], Y2[i], Z2[i]);


An example output can be seen here