TH2F into TImage problems

Hi all,

This concerns ROOT v4.04.02 on RHEL 3.

I wanted to make a TImage of a TH2F object, but ran into a snag. I basically used the same code as in the tutoral macro, hist2image.C. I’ve included a simple macro and the .root file which contains the histogram. I don’t understand why, but the code crashes when calling SetImage (it’s noted in the macro file).

The histogram does have many bins (3600 by 1500), so I wonder if this could be part of the problem.

The histogram in this example is created from a much more complicated piece of code. The odd thing is, if I use my full code to make the histogram, then try the TImage commands on the root command line, it almost works. It doesn’t crash, but the output is completely off (the color axis goes from 0 to 10^231).

If the histogram is drawn with “colz”, the color axis goes from roughly -5 to 5 in values, so I don’t know why the TImage::Draw() gives such an odd range.

Anyone have a suggestion?

Thanks,
Curtis
sigmaMap.root (1.6 MB)
tryme.C (910 Bytes)

The array returned by sigmamap->GetArray for a TH2F is a Float_t*.
You cannot cast it to a Double_t*.
Either change your TH2F to a TH2D or copy your data to a local Double_t* array as shown below

Rene

[code]void tryme() {
TFile *f1 = new TFile(“sigmaMap.root”,“READ”);
TH2F *sigmaMap = (TH2F )f1->Get(“sigmaMap”);
Int_t nx = sigmaMap->GetNbinsX();
Int_t ny = sigmaMap->GetNbinsY();
Double_t array = new Double_t[nxny];
for (Int_t j=0;j<ny;j++) {
for (Int_t i=0;i<nx;i++) {
array[nx
j+i] = sigmaMap->GetCellContent(i+1,j+1);
}
}
sigmaMap->SetTitle("");
gStyle->SetOptStat(kFALSE);

TCanvas *c1 = new TCanvas(“c1”,"",800,600);
c1->ToggleEventStatus();
c1->SetRightMargin(0.2);
c1->SetLeftMargin(0.01);
c1->SetTopMargin(0.01);
c1->SetBottomMargin(0.01);

cout << "sig map " << sigmaMap->GetArray() << endl;
cout << "bins x " << sigmaMap->GetNbinsX() << endl;
cout << "bins y " << sigmaMap->GetNbinsY() << endl;
cout << "gHist " << gHistImagePalette << endl;

TImage *img = TImage::Create();
// code crashes on the next line
//img->SetImage((const Double_t *)sigmaMap->GetArray(),nx+2,y+2,gHistImagePalette);
img->SetImage(array,nx,ny,gHistImagePalette);
img->Draw();
img->StartPaletteEditor();
}[/code]

[quote=“brun”]The array returned by sigmamap->GetArray for a TH2F is a Float_t*.
You cannot cast it to a Double_t*.
Either change your TH2F to a TH2D or copy your data to a local Double_t* array as shown below

Rene
[/quote]

Thank you for the explanation. I was able to use your version of the code just fine!

Curtis