Colors not saved in .C file

Dear rooters,

I have a very simple code that creates a canvas and save the canvas into a .C file.
If I run the code with Cint there is no problem, but when I compile it with g++ and run the main program, all the colors in the .C file are set to #0 like:

ci = TColor::GetColor("#000000");
hist->SetFillColor(ci);
ci = TColor::GetColor("#000000");
hist->SetLineColor(ci);

Below you can see an example of the code I was referring to. Any idea of what could be happening?
I am using root-5.20 slc4

#include <TH1F.h>
#include <TCanvas.h>

int main(int argc, char *argv[])
{

TCanvas *c2 = new TCanvas();

TH1F *hist = new TH1F(“hist”,“hist”,20,0,1);
hist->Fill(0.4);

hist->SetFillColor( kViolet );
hist->SetLineColor( kRed );
hist->Draw();

c2->Print(“printed.C”,“cxx”);

}

thanks for the help.

carlos

Hi,

You probably need to create a TApplication object.

Cheers,
Philippe.

TApplication* theApp = new TApplication("App", &argc, argv);

Hi Philippe,

thanks for the reply.
I tried to add the line you suggest at the beginning of the main, but It does not help. I still get
the color code
ci = TColor::GetColor("#000000");
in the generated macro .C.

any other suggestion?

cheers, carlos

I just tried your example with the latest ROOT version. I modified your example the following way:

#include <TROOT.h>
#include <TApplication.h>
#include <TH1F.h>
#include <TCanvas.h>

int main(int argc, char *argv[])
{
   TApplication* theApp = new TApplication("App", &argc, argv); 
   TCanvas *c2 = new TCanvas();
   TH1F *hist = new TH1F("hist","hist",20,0,1);
   hist->Fill(0.4);
   hist->SetFillColor( kViolet );
   hist->SetLineColor( kRed );
   hist->Draw();
   c2->Print("printed.C","cxx");
} 

I link the following way:

+ g++ -g -pipe -Wall -W -Woverloaded-virtual -fPIC -Iinclude -pthread -I /home/couet/root/include -o violet.o -c violet.cxx

+ g++ -O2 -m32 violet.o -L/home/couet/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic -o violet

And when I execute the program I obtain the following macro:

{
//=========Macro generated from canvas: c1/c1
//=========  (Tue Jul  1 15:57:36 2008) by ROOT version5.21/01
   TCanvas *c1 = new TCanvas("c1", "c1",15,49,700,500);
   c1->SetHighLightColor(2);
   c1->Range(0,0,1,1);
   c1->SetBorderSize(2);
   c1->SetFrameFillColor(0);
   
   TH1 *hist = new TH1F("hist","hist",20,0,1);
   hist->SetBinContent(9,1);
   hist->SetEntries(1);

   Int_t ci;   // for color index setting
   ci = TColor::GetColor("#cc00ff");
   hist->SetFillColor(ci);

   ci = TColor::GetColor("#ff0000");
   hist->SetLineColor(ci);
   hist->Draw("");
   c1->Modified();
   c1->cd();
   c1->SetSelected(c1);
}

I am running on linux. So for me all is fine.

upps, yes sorry, I did something wrong when I tried to add the line with the creation of the TApplication object (dont know what).

I tried again with the code you posted and certainly the instantiation of the TApplication solved the problem.

Thanks again for the help.

cheers, carlos