TPie with unknown number of slices


I need a root macro that read values from a file and draw a piechart using them.
I don’t know -a priori- the number of slices, which can change from one file to another.

This is what I tried to do:

 * Draw a pie chart from input file. The input file must contain only
 * 2 columns: value + label

void pchart (string input)
	string line;
	string label;
	double y;
	size_t point = 0;
	TCanvas *c = new TCanvas("c", "TPie", 500, 500);
	TPie *pie = new TPie();
	ifstream fileInput (input.c_str());
	while (getline(fileInput, line)) {
		if (line[0] == '#' || line[0] == '\0') continue;
		stringstream(line) >> y >> label;
		pie->SetEntryFillColor(point, point);
		pie->SetEntryVal(point, y);
		pie->SetEntryLabel(point, label.c_str());

A simple input file can be:

1 a
2 b
3 c

The above code produce an empty circle and only shows the first slice as if it were the only one (so it’s not a ‘slice’: it’s the entire pie).

What am I doing wrong?

yes the number of slices has to be know when creating the pie

void piechart2()
   Float_t vals[] = {.2,1.1,.6,.9,2.3};
   Int_t nvals = 5;
   TCanvas *cpie = new TCanvas("cpie","TPie test",700,700);

   TPie *pie2 = new TPie("pie2","",nvals);
   for (int i=0; i<nvals; i++) pie2->SetEntryVal(i, vals[i]);
1 Like

Isn’t there any method like “SetPoint” for TGraph? :frowning:

Actually I have a similar issue with TH1.

Well, ok… I’ll read the file twice in order to get the number of slices (which equals the number of lines).


No. (see TPie doc).
For TH1 it exists with alphanumeric labels.

We can imagine that SetEntryVal does that but it has to be implemented.

For TH1 it exists with alphanumeric labels.


I need to dig the TH1 reference page a bit deeper then.

For TH1 it exists with alphanumeric labels.

So it is a ‘planned feature’? Good.
Well, in the meanwhile, I’ll read the input file twice. :thinking:

Thank couet. :slight_smile:

No you are the first asking …
You are welcome to submit a PR if you need it fast,

1 Like

This is something I can’t do, not in a short time for sure, at least. And by ‘short time’ I mean: I’d need maybe even ~1 month to get confident with the TPie class.

I’d like to help, but well… :frowning:

Ok, so right now you should use your workaround.

1 Like

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