Settings not saved to source from Canvas

Hi,

If I run:

[code]#include “TFile.h”
#include “TCanvas.h”
#include “TH1F.h”

void MakePlots()
{
TFile ofile(“plots.root”,“recreate”);

TCanvas can(“can”,"",800,600);
TH1F *h = new TH1F(“h”,"",100,-10,10);
h->FillRandom(“gaus”, 10000);
h->SetLineColor(1);
h->Draw();

can.Write();

ofile.Close();
}

int main()
{
MakePlots();

return 0;
}[/code]

I make a file with a canvas containing a black histogram. If from this canvas I do:

The computer should produce the same plot but as source code. If I then do:

I should see the black histogram, but I see a blue one. In the same way other settings seem to be lost in the canvas -> source transition. Is there a way to tell ROOT to keep these settings?

I am trying to keep my plots as .cxx files instead of .pdf files and as I build my talk/paper I want these .pdf files to be generated. In this way I wont have to commit images but just code. However the fact that these settings get lost changes my plots.

Cheers.

Do you have gROOT->ForceStyle() in your rootlogon.C ?

Hi,

No, If I add that line before:

I get a default blue histogram. Without it I get the black histogram, however if I do:

And I check in plot.cxx for the line:

I cant find it. By the way, I am using Scientific Linux 6 in ROOT 6.04/14.

Cheers

It look like color 1 is a special case. If I execute:

{
   TH1F *h = new TH1F("h","",100,-10,10);
   h->FillRandom("gaus", 10000);
   h->SetLineColor(kRed);
   h->Draw();
   c1->Update();
   c1->Print("c1.C");
}

And then:

$ root c1.C

Then it is red.

Hi,

Yes I noticed that too. I have an overlay of black and red and the black histogram is changed to blue. Is this a ROOT problem and is there an easy way to fix it?

Cheers.

{
   TH1F *h = new TH1F("h","",100,-10,10);
   h->FillRandom("gaus", 10000);
   TColor *black=new TColor(12345, 0., 0., 0.);
   h->SetLineColor(12345);
   h->Draw();
   c1->Update();
   c1->Print("c1.C");
}

Hi,

Yes, that works in that small example you wrote. I made some tests and It seems that the TColor object has to be declared before SaveAs is called. In the code below:

[code]#include “TH1F.h”
#include “TFile.h”
#include “TCanvas.h”
#include “TColor.h”

#include

//TColor gcolor(12345,0,0,0); //IF GLOBAL
//******************************
class Plotter
{
public:
Plotter();
void MakeHistogram();
void SavePlots();
private:
//TColor m_color; //IF DATA MEMEBER
std::vector<TCanvas*> m_v_canvas;

};
//******************************
Plotter::Plotter()
{
//m_color = TColor(12345, 0, 0, 0); //IF DATA MEMEBER
}
//******************************
void Plotter::MakeHistogram()
{
TCanvas *can = new TCanvas(“can”,"",800,600);
TH1F *h = new TH1F(“h”,"",100,-10,10);
h->FillRandom(“gaus”,1000);
h->SetLineColor(12345);
h->Draw();

m_v_canvas.push_back(can);
}
//******************************
void Plotter::SavePlots()
{
TColor col(12345, 0, 0, 0); //IF LOCAL WHEN SAVING
m_v_canvas[0]->SaveAs(“plot.cxx”);
}
//******************************
int main()
{
Plotter obj;
obj.MakeHistogram();
obj.SavePlots();

return 0;
}
[/code]

I am using it as:

  1. Global: It works, but I would not make that a global variable.
  2. Local when saving: It works too and I like to make it local.
  3. Data member: In case it needs to be used in other functions. It does not work despite it should be available.

Cheers.