Problem with projections. Bug?

I have strange problem with projections of histograms. This is example code:

void histo(){
	TH3D *h3 = new TH3D("glob","glob",100,0,1,100,0,1,100,0,1);
	TH3D *h4 = new TH3D("glob","glob",100,0,1,100,0,1,100,0,1);
	TH1D *h1 = h3->ProjectionX("x");
//	h1->SetName("dname");
	TH1D *h2 = h4->ProjectionX("x");
	TCanvas *c1 = new TCanvas();

I suppose that this problem is caused by overwriting histogram with the same name in current directory. I could SetDirectory(0) but I’m not sure how memory management then looks (I would like to Draw this on TBrowser, or just save in ROOT in TCanvas).
I have only one “bad” solution - giving “random names”. Any better idea?

Why is giving different names a bad solution? Don’t use “random” names, you “good” names instead.

Simply don’t give two histograms the same name. You are using h3 and h4 for different purposes, so use different names! Easiest fix: use “glob1” and “glob2”. (please use better names)

And here:

Simply don’t specify a name at all. The default name is (name of source histogram)+"_px" which is usually a good one (unless you need a special name).

So do something like this:

void histo(){
	TH3 *h3 = new TH3D("glob1","glob",100,0,1,100,0,1,100,0,1);
	TH3 *h4 = new TH3D("glob2","glob",100,0,1,100,0,1,100,0,1);
	TH1 *h3_px = h3->ProjectionX();
	TH1 *h4_px = h4->ProjectionX();
	TCanvas *c1 = new TCanvas();

Note also that I have renames h1 to h3_px, and h2 to h4_px. So you can already see from the name that h3_px is a projectionx of h3.

1 Like

The problem is that I have 3D histograms with the same name in different files. It’s fine when I want to draw single projection. But problem occurs when I want to draw “SAME” histograms and compare results from different files.
Of course I can rename them - but this require additional lines in macro - something that I would like to avoid.

Try this:

TFile *file1 = new TFile("file1.root");
TH3 *hist1 = (TH3*) file1->Get("histName");

TFile *file2 = new TFile("file2.root");
TH3* hist2 = (TH3*) file2->Get("histName");

Then you will have the pointers to each histogram separately with no ambiguity.

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