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;
	file_start.open(filenane_start);
	float x = 0, y = 0, z = 0;
	
  	while(1){
		file_start >> x >> y >> z;
		if (!file_start.good()) break;
		X1.push_back(x);
		Y1.push_back(y);
		Z1.push_back(z);
	}
    file_start.close();
    
    x=0.; y=0.; z=0.;
  
	file_end.open(filename_end);
    while(1){
		file_end >> x >> y >> z;
		if (!file_end.good()) break;
		X2.push_back(10.*x);
		Y2.push_back(10.*y);
		Z2.push_back(10.*z);
	}
    file_end.close();

	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);
	hdummy->SetLineColor(kWhite);	
	hdummy->Draw();

	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]);
		trajectories[i]->Draw();
	}

}

An example output can be seen here

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