# Quiet mode for pyroot?

**URL:** <https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043>\
**Category:** ROOT\
**Tags:** python\
**Created:** [February 22, 2018, 3:49pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043 "2018-02-22T15:49:12Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![adsherman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adsherman](https://root-forum.cern.ch/u/adsherman)\
**Post date:** [February 22, 2018, 3:49pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/1 "2018-02-22T15:49:12Z")

</div>

I would like to suppress the output from canvas.SaveAs, and I do not seem to be able to. I found this solution but it just complains about a local variable named gErrorIgnoreLevel not being set when I try to modify it. ([Temporarily turn off warnings in PyROOT](https://root-forum.cern.ch/t/temporarily-turn-off-warnings-in-pyroot/18837))

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [February 22, 2018, 4:09pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/2 "2018-02-22T16:09:47Z")

</div>

Are you using the context manager from that post you linked, or just trying to modify gErrorIgnoreLevel directly? Can you post a minimal example code & error message?

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [February 22, 2018, 4:14pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/3 "2018-02-22T16:14:37Z")

</div>

In a former life, I wrote this:

[http://acode-browser2.usatlas.bnl.gov/lxr-rel17/source/atlas/Tools/PyUtils/python/Helpers.py](http://acode-browser2.usatlas.bnl.gov/lxr-rel17/source/atlas/Tools/PyUtils/python/Helpers.py)

---

<div class="post-metadata">

**Author:** ![adsherman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adsherman](https://root-forum.cern.ch/u/adsherman)\
**Post date:** [February 22, 2018, 4:57pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/4 "2018-02-22T16:57:48Z")

</div>

I tried using your code verbatim as well as with removal of the ROOT.\<\> by doing from ROOT import gErrorIgnoreLevel, since I don’t want to import the whole libarary:

```auto
class Quiet:
    """Context manager for silencing certain ROOT operations. Usage:
    with Quiet(level = .kInfo+1):
       foo_that_makes_output

    You can set a higher or lower warning level to ignore different
    kinds of messages. After the end of indentation, the level is set
    back to what it was previously.
    """
    def __init__ (self, level=kInfo + 1):
        self.level = level

    def __enter__ (self):
        self.oldlevel = gErrorIgnoreLevel
        gErrorIgnoreLevel = self.level

    def __exit__ (self, type, value, traceback):
        gErrorIgnoreLevel = self.oldlevel

```

Then I ran:

```auto
with Quiet():
  canvas.SaveAs(self.runConfig["outputPath"] + "/plots/overlay_" + hist_name + ".pdf")

```

I don’t have the error output anymore but it was complaining that gErrorIgnoreLevel was being accessed before definition.

Overall the code is not so easy to minimally reproduce – I am using subprocess to parallelize. I do see that if I pipe STDERR with subprocess.Popen().communicate(), I can collect _all_ of the root output into it. None of it goes to STDOUT, not even the stuff marked at “Info” level. So I suppose I can suppress it this way, but I thought there might be a another way in PyRoot.

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [February 22, 2018, 5:15pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/5 "2018-02-22T17:15:58Z")

</div>

Ah, I think if you remove the ROOT namespace, then python indeed thinks gErrorIgnoreLevel is just a regular local variable, not the global from the module. Try adding the line

```auto
global gErrorIgnoreLevel

```

at the beginning of the **enter** and **exit** methods. That should fix it.

This [blog post](https://www.python-course.eu/python3_global_vs_local_variables.php) seems to adequately explain what’s going on.

---

<div class="post-metadata">

**Author:** ![adsherman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adsherman](https://root-forum.cern.ch/u/adsherman)\
**Post date:** [February 22, 2018, 5:23pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/6 "2018-02-22T17:23:27Z")

</div>

Ah yes – I havent “set” any of the other values I have imported from ROOT in this manner, so that makes sense. However when I try it, it does not change the output to stop showing “Info”'s. It doesn’t error, but it doesn’t seem to work right. Python probably thinks it is a normal variable and not a root variable

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [February 22, 2018, 9:25pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/7 "2018-02-22T21:25:31Z")

</div>

Using ‘global’ is not enough, you absolutely need ROOT.gErrorIgnoreLevel with ROOT being the ROOT module, not the ROOT namespace.

Global variables are implemented as Python properties. So a statement like this:

```auto
from ROOT import gErrorIgnoreLevel

```

translates to something like this (not really, but it illustrates the point well enough):

```auto
gErrorIgnoreLevel = ROOT. __class__.gErrorIgnoreLevel. __get__ (ROOT)

```

i.e. it is the return result of a function call, and so even the “global” gErrorIgnoreLevel is a simply Python int, completely detached from anything ROOT. Changing that global variable then just means updating the entry in the globals() dictionary and never touches the C++ side.

---

<div class="post-metadata">

**Author:** ![adsherman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adsherman](https://root-forum.cern.ch/u/adsherman)\
**Post date:** [February 23, 2018, 12:44pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/8 "2018-02-23T12:44:32Z")

</div>

So is there a solution to this that avoids importing all of ROOT? I currently only import what I need, so it is faster to boot up

Actually this is also relevant for another problem I have ([PyROOT hijacks --help](https://root-forum.cern.ch/t/pyroot-hijacks-help/15207))

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [February 23, 2018, 3:04pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/9 "2018-02-23T15:04:21Z")

</div>

> [@adsherman](#):
>
> So is there a solution to this that avoids importing all of ROOT? I currently only import what I need, so it is faster to boot up

Measure it … If the criterium is startup time, then this:

```auto
from ROOT import gErrorIgnoreLevel

```

is significantly slower than this:

```auto
import ROOT

```

b/c everything in PyROOT is done lazily all the time and the first has to initialize more than the latter. Of course, that initialization still happens on first use of ROOT.gErrorIgnoreLevel.

Iow., there is no such thing as “importing all of ROOT” so by definition you’re avoiding it.

Note that in cppyy master, there is code that allows exploring “all of ROOT” for the express purpose of “importing all of ROOT” (apparently there is a use case), but doing so is actually a rather hard problem.

Aside, only in an inner loop, keeping access through the module can be measurably slower. Then again, for write access, the lookup is both slower and needed b/c it does more work (i.e. also setting the value on the C++ side). But for read-only access, it makes sense to reference the variable locally.

---

<div class="post-metadata">

**Author:** ![adsherman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adsherman](https://root-forum.cern.ch/u/adsherman)\
**Post date:** [February 23, 2018, 4:11pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/10 "2018-02-23T16:11:07Z")

</div>

Really? I guess you learn stuff every day. What about from ROOT import \*? Would this be the same as import ROOT (I would have to refactor a lot of code to add the ROOT namespace, but if it’s faster I would rather have it).

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [February 23, 2018, 4:21pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/11 "2018-02-23T16:21:25Z")

</div>

If you’re already using from ROOT import foo, you should still be able to add import ROOT without breaking anything. Even if you do have to refactor, at least all the ROOT types and classes start with T so it’s easy to search & replace!

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [February 23, 2018, 4:57pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/12 "2018-02-23T16:57:12Z")

</div>

> [@adsherman](#):
>
> What about from ROOT import \*?

That, too, does not do what you probably think it does. (I say “probably” b/c I can’t read your mind, but based on how the question was posed, I can guess.)

Python dictionaries contain a data member that is a pointer to a lookup function. “from ROOT import \*” hijacks that for the global dictionary in the closure where the import happens. The upshot is that yes, “from ROOT import \*” is still lazily implemented.

There are a couple of caveats, though: it doesn’t work with PyPy (where the dictionary implementation is different), it is disabled in PyROOT for p3 (no such problem with cppyy master, though), and it will give you a run-time slowdown. The reason for that last one is that for every new variable not only the closure, but all of ROOT is searched for that name, which isn’t cheap.

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [February 23, 2018, 5:02pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/13 "2018-02-23T17:02:24Z")

</div>

> [@jfcaron](#):
>
> at least all the ROOT types and classes start with T so it’s easy to search & replace!

Up to about half-way 2005 or so, PyROOT actually did filter on ‘T’. Before that, PyLCGDict was used, which was based on a copy of the PyROOT code base. But then experiments moved to PyCintex, which was based on PyROOT proper, and so the filter had to go.

---

<div class="post-metadata">

**Author:** ![adsherman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@adsherman](https://root-forum.cern.ch/u/adsherman)\
**Post date:** [February 23, 2018, 5:17pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/14 "2018-02-23T17:17:00Z")

</div>

Okay – I think Ill refactor to improve the runtime then. Lets see how that goes! Thanks for the help, both of you

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 9, 2018, 5:17pm UTC](https://root-forum.cern.ch/t/quiet-mode-for-pyroot/28043/15 "2018-03-09T17:17:05Z")

</div>

Closed on March 9, 2018, 5:17pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
