Loading a saved canvas that has a custom palette (via TExec)

(I’m using Root 5.26 on Windows.)

I’m having a crash when I try to Update() a specific canvas that I have loaded via TFile->Get(). (The file was created by canvas->SaveAs();).
The problem seems due to having a custom palette when I created the histogram in this canvas.

I need custom palettes for my different histograms, and used a technique I found in this forum to create the different palettes, using TExec:
tStr1.Form(“gStyle->SetPalette(%d, (Int_t*)0x%x);”, 10, pkr_palette);
fE_palette_pkr = new TExec(“fE_palette_pkr”, tStr1 );

When the histogram is created, to get my custom palette, I call:
th2d->Draw(“SURF1 Z”);
fE_palette_pkr->Draw();
th2d->Draw(“SURF1 Z SAME”);

then I save the canvas with: Canvas->SaveAs(Namestr.Data());

In a different program, I open the root file that I saved the canvas into, get the canvas, attach it to an embeddedcanvas, and update it:
tfile1 = new TFile(tStr3);
if (tfile1->IsOpen())
{
delete EmbCanvas->GetCanvas();
EmbCanvas->AdoptCanvas((TCanvas *)tfile1->Get(tStr2));
EmbCanvas->GetCanvas()->Update();
tfile1->Close();
delete tfile1;
}

When the Update() occurs, there is a crash in
TColor::SetPalette(Int_t ncolors, Int_t *colors)
because colors is the address of the color table when the histogram and canvas were created.

Is there some way to save the original custom palette with the canvas?
Or do I have to re-create the custom palette and use it before I call Update()?

(which would be a problem, because the program that loads and displays the canvas doesn’t have the information that the original custom palette was created from)

I’m not including an actual working (failing) example of this because the code is quite large. But if it’s needed, I will try to make a small running example.

Thanks!
buddy

Could you attach your canvas.root file?

Rene

Here is my .root file of the canvas with the histogram with the custom palette.

Also, 2 screen captures: one of the original display with some green areas due to the palette, and the second displaying the canvas in a TBrowser. The TBrowser displays the whole histogram as black, obviously a side-affect of not having the original palette.

I’m thinking I’m going to have to save the values of the custom palette in the saved canvas somehow. Then, in the display program, load these values, create the TExec, and Draw() with them, before Update() the canvas.





01272010_072641_pkr_chip_x_axis_Coarse_Scan_Plot.root (17.7 KB)

Your canvas contains a TExec named fE_palette_pkr and executing the following code:

gStyle->SetPalette(50, (Int_t*)0x55d6bf0); While this was a legal statement in the job generating the canvas, it is illegal in the ROOT version using this TExec because the array at address 0x55d6bf0 does not exist anymore.

You can read your canvas and remove the TExec object with a code like

TFile *f = new TFile("Plot.root"); TCanvas *c1 = (TCanvas*)f.Get("01272010_072641_pkr_chip_x_axis_Coarse.csi2d"); TExec *tx = (TExec*)c1->GetListOfPrimitives()->FindObject("fE_palette_pkr"); c1->GetListOfPrimitives()->Remove(tx); c1->Draw();
of course, in this case the plot will be drawn with the default palette, not your palette.

Rene

Thanks Rene.

That does work. And I think I’m on track for also saving the original custom palette so I can re-establish it when the canvas is later displayed.

buddy