Histogram status: "MIA"

Hi,

Can somebody please explain to me why histogram h30 is empty after the execution of the macro? a “h30->Draw()” from the command line produces an empty plot.

Thanks.

–Christos

===========================================

int run(void)
{

TH1F * h10 = new TH1F(“h10”,“hist 10”, 100, -10, 10);
TH1F * h20 = new TH1F(“h20”,“hist 20”, 100, -10, 10);
TH1F * h30 = new TH1F(“h30”,“hist 30”, 100, -10, 10);

for(Int_t i = 0; i != 1000; ++i)
{
h10->Fill(gRandom->Gaus(0,2));
h20->Fill(gRandom->Gaus(0,3));
}

h30 = (TH1F *) h10->GetAsymmetry(h20);

h30->SetNameTitle(“h30”,“hist 30”);
h30->Draw();
return 0;
}

This is because the (TH1F *) h10->GetAsymmetry(h20) pointer ceases to exist at the end of the macro-block! ](*,)

All I had to do was replace
h30 = (TH1F *) h10->GetAsymmetry(h20);

by
h30 = new TH1F(* (TH1F*)h10->GetAsymmetry(h20));

(even though the syntax is a little, em, funny)…

–Christos

Your second attempt is worst than the first one.
In your first script, you should not create h30. It is created
by TH1::GetAsymmetry

Rene

Hi,

Could you be a bit more specific, i.e. what should I do instead? I want h30 to stay in memory, that’s why I use new. In the first script, that did not work (obviously). In the second script, I removed the first instantiation:
TH1F * h30 = new TH1F(“h30”,“hist 30”, 100, -10, 10);
since I use new when I call the GetAsymmetry method.

Cheers,

–Christos

Hi Christo,

Use this piece of code to make your job (h30 remains after execution)
I have comented out the lines that you don’t need.

//int run(void)
{

TH1F * h10 = new TH1F(“h10”,“hist 10”, 100, -10, 10);
TH1F * h20 = new TH1F(“h20”,“hist 20”, 100, -10, 10);
TH1F * h30 = new TH1F(“h30”,“hist 30”, 100, -10, 10);

for(Int_t i = 0; i != 1000; ++i)
{
h10->Fill(gRandom->Gaus(0,2));
h20->Fill(gRandom->Gaus(0,3));
}

h30 = (TH1F *) h10->GetAsymmetry(h20);

h30->SetNameTitle(“h30”,“hist 30”);
h30->Draw();

//return 0;

}

Stelios.

Ah, but I want to compile my code!

–Christos

Hi Christos

Then you have to declared some global variable.

[code]#include “TH1F.h”
#include “TRandom.h”

TH1F * gh10 = 0;
TH1F * gh20 = 0;
TH1F * gh30 = 0;

int run(void)
{

gh10 = new TH1F(“h10”,“hist 10”, 100, -10, 10);
gh20 = new TH1F(“h20”,“hist 20”, 100, -10, 10);

for(Int_t i = 0; i != 1000; ++i)
{
gh10->Fill(gRandom->Gaus(0,2));
gh20->Fill(gRandom->Gaus(0,3));
}

gh30 = (TH1F *) gh10->GetAsymmetry(gh20);

gh30->SetNameTitle(“h30”,“hist 30”);
gh30->Draw();

return 0;

}
[/code]

Cheers,
Philippe

The developer is ALWAYS right! :slight_smile:

Thanks Philippe!

–Christos