Save as macro for THStack histograms

Hello everyone,

I faced with some problem and has come to the following simplified code that reproduces the problem:

	Float_t values_set[5] = {45.3, 44.7, 45.0, 44.95, 45.12};

	TCanvas *can = new TCanvas( "my_C_canvas", "Histogram Stack Example", 200, 50, 600, 400 );

	THStack *hs = new THStack();
	hs->SetName( "hstack" );

	TH1D *h1 = new TH1D( "h1", "First hist", 5, 0, 6 );

	for (int i=0; i<5; i++) {
		h1->SetBinContent( i+1, i, values_set[i] );
		h1->SetBinError( i+1, i, 0.001 );

	hs->Add( h1, "E1" );
	hs->Draw( "nostack" );

I fire up the code, the Canvas window opens, further I select
File -> Save As -> my_C_canvas.C
But there are at least two issues in this saved C-macro.
Histograms are declared and named as

TH1D *h1__1 = new TH1D("h1__1","First hist",5,0,6);

But adding to the stack looks as


Accordingly I got such error

my_C_canvas.C:69:16: error: use of
      undeclared identifier 'h1'

If I manually replace Add(h1,“E1”) with Add(h1__1,“E1”), my_C_canvas.C starts working,
but here I see the second issue -
I have h1__1->SetBinError only for the first three Bins
and my histogram looks wrong with large error for the last two Bins.

My ROOT version is 6.09/01.

I attached all examples the C and PyROOT scripts (I prefer to work with PyROOT and first reproduced problem with it).
my_PY_canvas.C (2.72 KB) (468 Bytes)
my_C_canvas.C (2.67 KB)
attempt.C (473 Bytes)


Yeah, the problem was solved. Thanks a lot!
And the second one with SetBinError() was really mine.

Good :slight_smile: