Set style from a function

I want to change a style from a function:

import ROOT

def my_style():
  my_style = ROOT.TStyle("MYSTYLE","My style")
  my_style.SetOptStat(0)                      
  return my_style 

def set_style():
  style = my_style()
  ROOT.gROOT.SetStyle("MYSTYLE")
  ROOT.gROOT.ForceStyle()

h = ROOT.TH1F("h","h",100,0,100)
h.Fill(20) 
set_style()
h.Draw()

but the Stat is still here.

The object constructors use the current style. Creating a new style does not change the existing object attributes.
see also gROOT->ForceStyle and TObject::UseCurrentSyle, eg myHist.UseCurrentStyle()

Rene

ok, but this (C++) works:

TStyle* AtlasStyle()
{
  TStyle *atlasStyle = new TStyle("ATLAS","Atlas style");

  // use plain black on white colors
  Int_t icol=0; // WHITE
  atlasStyle->SetFrameBorderMode(icol);
.........
  return atlasStyle;
}

void SetAtlasStyle ()
{
  TStyle* atlasStyle = AtlasStyle();
  gROOT->SetStyle("ATLAS");
  gROOT->ForceStyle();
}

see doc or gROOT->ForceStyle and UseCurrentStyle at the bottom of page 141 of
the Users Guide at root.cern.ch/download/doc/Users_Guide_5_26.pdf

Rene

[quote=“brun”]see doc or gROOT->ForceStyle and UseCurrentStyle at the bottom of page 141 of
the Users Guide at root.cern.ch/download/doc/Users_Guide_5_26.pdf

Rene[/quote]

the problem is not this. The python code and C++ code are equivalent, but the C++ works, python not. There is no difference if first I set the style first and then I create the histogram:

set_style() 
h = ROOT.TH1F("h","h",100,0,100) 
h.Fill(20) 
h.Draw() 

Hi,

the difference between the C++ version and the python one is the un-matched new in C++. In order to achieve the same with python, one has to do “ROOT.SetOwnership( my_style, False )”.

I looked at the ownership rules specific to the TStyle class, and it appears that it can’t in any way be managed externally (it’s ctor and dtor always talk to gROOT’s ListOfStyles). I’ve therefore changed the code in Pythonize.cxx to make TStyle instances always be not-owned by python. New code is in trunk.

Cheers,
Wim