Different color palettes for different plots with TExec

Dear ROOT team,

I need to plot two 2D histograms, each one with a customized color palette. The histograms should “remember” their color palettes when they are redrawn. Now I know this subject has been discussed before, and I tried to apply the lesson given in root.cern.ch/phpBB2/viewtopic.php?t=2483 , but somehow I must be doing something wrong.

Let me demonstrate this with a slight adaption of the macro given in the aforementioned post:

{
TCanvas *c1 = new TCanvas("c1","c1",400,600);
c1->Divide(1,2);
TH2F *h1 = new TH2F("h1","h1",40,-4,4,40,-4,4);
TH2F *h2 = new TH2F("h2","h2",40,-4,4,40,-4,4);
Double_t a,b;
for (Int_t i=0;i<5000;i++) {
  gRandom->Rannor(a,b);
  h1->Fill(a-1.5,b-1.5);
  h2->Fill(a+1.5,b+1.5);
}

TExec *ex1 = new TExec("ex1","gStyle->SetPalette(0);");
TExec *ex2 = new TExec("ex2","gStyle->SetPalette(1);");

c1->cd(1);
//ex1->Draw();
h1->GetListOfFunctions()->Add(ex1);
h1->Draw("colz");

c1->cd(2);
//ex2->Draw();
h2->GetListOfFunctions()->Add(ex2);
h2->Draw("colz");

c1->Update();

}

Having no luck with ex1->Draw(), I try with GetListOfFunctions()->Add(ex1), but this does not seem to have the desired effect either: I get one palette for the plot, the other palette for the legend, or some weird combination of the two (specially when the macro is executed several times, or a redrawing is triggered with the mouse). The problem is that the palette selection gets executed after the drawing of the histogram. How can I marry a histogram with a color palette once and for all? (it is a pity these color palettes are not properties of the histograms themselves!!)

Many thanks in advance,

Thomas

PS: ROOT 5.16/00

see new version of your script with comments in the code.

Rene

[code]{
TCanvas *c1 = new TCanvas(“c1”,“c1”,400,600);
c1->Divide(1,2);
TH2F *h1 = new TH2F(“h1”,“h1”,40,-4,4,40,-4,4);
TH2F *h2 = new TH2F(“h2”,“h2”,40,-4,4,40,-4,4);
Double_t a,b;
for (Int_t i=0;i<5000;i++) {
gRandom->Rannor(a,b);
h1->Fill(a-1.5,b-1.5);
h2->Fill(a+1.5,b+1.5);
}

TExec *ex1 = new TExec(“ex1”,“gStyle->SetPalette(0);”);
TExec *ex2 = new TExec(“ex2”,“gStyle->SetPalette(1);”);

c1->cd(1);
//because h1->Draw clears the pad (hence remove ex1) we must draw
//the frame first, then set the palette, then draw the histo again with option "same"
h1->Draw(“a”);
ex1->Draw();
h1->Draw(“colz same”);
gPad->Update();

c1->cd(2);
h2->Draw(“a”);
ex2->Draw();
h2->Draw(“colz same”);
gPad->Update();
}
[/code]

Thanks for the prompt reply. This works! Is there a reason you do Draw(“a”)? This makes you lose axis titles and labels. For me, Draw("") works just as well but keeps titles and labels.

Thomas

In fact I just realized that neither Draw("") nor Draw(“a”) have the desired effect when dealing with 3D-like drawing options such as “lego2”, “surf3” etc. The safest way - I think - is to simply apply the same drawing option twice. This means the example macro should be something like this:

{
TCanvas *c1 = new TCanvas("c1","c1",400,600);
c1->Divide(1,2);
TH2F *h1 = new TH2F("h1","h1",40,-4,4,40,-4,4);
TH2F *h2 = new TH2F("h2","h2",40,-4,4,40,-4,4);
Double_t a,b;
for (Int_t i=0;i<5000;i++) {
  gRandom->Rannor(a,b);
  h1->Fill(a-1.5,b-1.5);
  h2->Fill(a+1.5,b+1.5);
}

TExec *ex1 = new TExec("ex1","gStyle->SetPalette(0);");
TExec *ex2 = new TExec("ex2","gStyle->SetPalette(1);");

TString drawOpt="surf2z";

c1->cd(1);
//because h1->Draw clears the pad (hence remove ex1) we must draw
//the frame first, then set the palette, then draw the histo again with option "same"
h1->Draw(drawOpt);
ex1->Draw();
h1->Draw(drawOpt+" same");
gPad->Update();

c1->cd(2);
h2->Draw(drawOpt);
ex2->Draw();
h2->Draw(drawOpt+" same");
gPad->Update();
} 

Do you agree?

Thomas