TGraphErrors marker colour persistency

Hi All,

I have a few macros I use to extract data from a TTree and make TGraphErrors. The first function pulls out the relevant arrays, creates the TGraphErrors, names them and saves them in a TFile. I then have a second macro which takes a few of the graphs and plots several of them on the same canvas. I differentiate them by changing the marker colour for each graph. I save the resulting canvas in the same file. All well and good so far, however when I reload the canvas after closing root I find the marker colour for each graph has reverted to my default. I have tried re-saving the graphs as well as the canvas but to no avail. Could anyone tell me how I can save a TGraphErrors such that the marker colour persists?

Thanks very much,

Adam

I cannot reproduce this problem.
Could you send the shortest possible script/file reproducing it?

Rene

Hi Rene,

Please see the attached. For me, when I run this script the TGraphErrors have the correct marker colours initially, but when I later reload the saved file out.root, the colour information is gone.

Thanks,

Adam
graphProb.C (1.01 KB)

I o not see any problem with the marker colors except that your graph has a dot as default marker, as a result it is invisible in the canvas. Simply change the marker style as shown in your modified macro below

Rene

[code]int graphProb()
{
Double_t x = 0.0, y = 0.0, z=0.0, sigx = 0.5, sigy = 0.5, sigz = 0.5;

TFile f1("out.root","recreate");
TCanvas * can1 = new TCanvas();
can1->SetName("can1");

TTree * t1 = new TTree("t1","tree");
t1->Branch("x",&x,"x/D");
t1->Branch("sigx",&sigx,"sigx/D");
t1->Branch("y",&y,"y/D");
t1->Branch("sigy",&sigy,"sigy/D");
t1->Branch("z",&z,"z/D");
t1->Branch("sigz",&sigz,"sigz/D");

t1->Fill();

x = 1.0;
y = 1.0;
z = 2.0;
t1->Fill();

x = 2.0;
y = 4.0;
z = 8.0;
t1->Fill();

x = 3.0;
y = 9.0;
z = 18.0;
t1->Fill();

t1->Draw("x:z:sigx:sigz","","goff");
TGraphErrors *gr1 = new TGraphErrors(t1->GetSelectedRows(),t1->GetV1(),t1->GetV2(),t1->GetV3(),t1->GetV4());
gr1->SetMarkerColor(kRed);
gr1->SetMarkerStyle(21);
gr1->SetName("gr1");
gr1->Draw("AP");

t1->Draw("x:y:sigx:sigy","","goff");
TGraphErrors *gr2 = new TGraphErrors(t1->GetSelectedRows(),t1->GetV1(),t1->GetV2(),t1->GetV3(),t1->GetV4());
gr2->SetMarkerColor(kBlue);
gr2->SetMarkerStyle(21);
gr2->SetName("gr2");
gr2->Draw("P");

gr1->Write();
gr2->Write();
can1->Write();

}
[/code]

Thanks very much Rene, I traced the problem to a line in my rootlogon.C

gROOT->ForceStyle();

Once this was removed my colours persisted when later opening a root file. It did however lead to another small issue; if I add a TLegend, as in the below code, each key entry now seems to have a black background, rather than the usual white. This is seems to be due to the TGraphErrors strangely being set to have their Fill colour set to black. I know this can be changed back to white with the SetFillColor(kWhite) method on each graph individually, but is there a way of using gStlye to correct this when I create the graphs (I practice I tend to draw and save a lot of graphs)?

Again, thanks for your time,

Adam

[code]int graphProb()
{
gStyle->SetFillColor(kWhite);

Double_t x = 0.0, y = 0.0, z=0.0, sigx = 0.5, sigy = 0.5, sigz = 0.5;

TFile f1(“out.root”,“recreate”);
TCanvas * can1 = new TCanvas();
can1->SetName(“can1”);

TTree * t1 = new TTree(“t1”,“tree”);
t1->Branch(“x”,&x,“x/D”);
t1->Branch(“sigx”,&sigx,“sigx/D”);
t1->Branch(“y”,&y,“y/D”);
t1->Branch(“sigy”,&sigy,“sigy/D”);
t1->Branch(“z”,&z,“z/D”);
t1->Branch(“sigz”,&sigz,“sigz/D”);

t1->Fill();

x = 1.0;
y = 1.0;
z = 2.0;
t1->Fill();

x = 2.0;
y = 4.0;
z = 8.0;
t1->Fill();

x = 3.0;
y = 9.0;
z = 18.0;
t1->Fill();

t1->Draw(“x:z:sigx:sigz”,"",“goff”);
TGraphErrors *gr1 = new TGraphErrors(t1->GetSelectedRows(),t1->GetV1(),t1->GetV2(),t1->GetV3(),t1->GetV4());
gr1->SetMarkerColor(kRed);
gr1->SetMarkerStyle(21);
gr1->SetName(“gr1”);
gr1->Draw(“AP”);

t1->Draw(“x:y:sigx:sigy”,"",“goff”);
TGraphErrors *gr2 = new TGraphErrors(t1->GetSelectedRows(),t1->GetV1(),t1->GetV2(),t1->GetV3(),t1->GetV4());
gr2->SetMarkerColor(kBlue);
gr2->SetMarkerStyle(21);
gr2->SetName(“gr2”);
gr2->Draw(“P”);

TLegend * leg1 = new TLegend(0.2,0.65,0.3,0.8);
leg1->AddEntry(gr1,“gr1”);
leg1->AddEntry(gr2,“gr2”);
leg1->Draw();

gr1->Write();
gr2->Write();
can1->Write();
}[/code]

By default AddEntry has the option “plf” for line, polymarker, fill area.
If you want the legend for on Line’s and Polymarker’s attributes, simply remove the option “F”. Use only “LP” in AddEntry.
See:
root.cern.ch/root/html/TLegend.html