Getting TColors to work in a saved macro from PyROOT

I have a PyROOT script which builds a canvas, then saves it to a C macro. When I run the macro, the canvas is correctly reconstructed save for the colors, which all default to:

This issue has been covered before, but not in the PyROOT context. In a C application, you can apparently fix this by calling TApplication:
http://root.cern.ch/phpBB3//viewtopic.php?f=3&t=6909&p=28754

Is there an equivalent way of doing this in PyROOT? I can’t figure out how to pass the correct argc and argv to the TApplication constructor in python. And looking at other posts, it looks like there’s a TPyROOTApplication; is that automatically created when you import ROOT, or is there some way to create an instance of that object?

Or am I going about solving this the complete wrong way? :slight_smile:

For the record, here’s how I’m loading ROOT in my python script:

    try:
        import ROOT
        import libPyROOT
    except ImportError:
        sys.exit(1)
    ROOT.gROOT.SetBatch()
    ROOT.TH1.SetDefaultSumw2()
    ROOT.gErrorIgnoreLevel = ROOT.kWarning
    if os.path.exists('rootlogon.C'):
        ROOT.gROOT.Macro('rootlogon.C')
    elif os.path.exists(os.path.expanduser('~/.rootlogon.C')):
        ROOT.gROOT.Macro(os.path.expanduser('~/.rootlogon.C'))

Hi,

you can create your own TApplication with PyROOT, but to me it looks like a red herring. The difference in the scripts from the thread that you linked isn’t so much the TApplication (sure, that’s there as well), but primarily that the “failing” script (i.e. wrong colors) is run in batch and the “succeeding” script/program (i.e. correct colors) is not. I’ll bet that certain graphics objects need to be created that aren’t in batch.

Cheers,
Wim

Wim,

Objects produced in batch should be identical to objects produced in an interactive session (of course on gif, ps, pdf,etc)

Rene

Rene,

then it’s a bug somewhere … Try this:

[code]#include <TH1F.h>
#include <TCanvas.h>

int test() {
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”);
}[/code]
to compare the results of running not in batch and in batch:

[code]$ root -l -q test.C
$ cat printed.C
// …
ci = TColor::GetColor("#cc00ff");
hist->SetFillColor(ci);

ci = TColor::GetColor("#ff0000");
hist->SetLineColor(ci);
// …
$ root -l -q -b test.C
$ cat printed.C
// …
ci = TColor::GetColor("#000000");
hist->SetFillColor(ci);

ci = TColor::GetColor("#000000");
hist->SetLineColor(ci);
// …[/code]
The above is with CINT, so there is a TRint TApplication derived instance in both cases, the only difference being batch/no-batch. It’s also SVN trunk.

I’ll throw it into savannah then.

Cheers,
Wim

Hi,

for reference, the bug report is here:

savannah.cern.ch/bugs/index.php?70886

Cheers,
Wim

Now fixed. Thanks for reporting.