# Temporarily turn off warnings in PyROOT

**URL:** <https://root-forum.cern.ch/t/temporarily-turn-off-warnings-in-pyroot/18837>\
**Category:** Evolution Proposals, Apps & Ideas\
**Created:** [February 14, 2015, 3:40am UTC](https://root-forum.cern.ch/t/temporarily-turn-off-warnings-in-pyroot/18837 "2015-02-14T03:40:07Z")\
**Posts on this page:** 3\
**Page:** 1

<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 14, 2015, 3:40am UTC](https://root-forum.cern.ch/t/temporarily-turn-off-warnings-in-pyroot/18837/1 "2015-02-14T03:40:07Z")

</div>

Here is a simple context-manager class that you can use to temporarily raise the print level for a few commands:

```auto
import ROOT
class Quiet:
    """Context manager for silencing certain ROOT operations. Usage:
    with Quiet(level = ROOT.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=ROOT.kInfo + 1):
        self.level = level

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

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

```

I use it when saving canvases to suppress the “file created” message:

```auto
with MyROOT.Quiet():
  c1.SaveAs("somefile.pdf")
```

---

<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 16, 2016, 7:20am UTC](https://root-forum.cern.ch/t/temporarily-turn-off-warnings-in-pyroot/18837/2 "2016-02-16T07:20:57Z")

</div>

Here are some simple classes that play a similar role in C++. Where in Python you used a context manager and the “with” keyword, in C++ you just create a scope (with { and }) and instantiate a Quiet or Batch object. When the scope ends, the destructors return the ErrorIgnoreLevel and Batch status to what it was before.

```auto
// In a Utility.h file:
  class Quiet
  { // Usage:
    // { Quiet q(kWarning+1);
    // StuffThatProducedOutput();
    // }
  public:
    Quiet(Int_t level = kInfo+1);
    ~Quiet();
  private:
    Int_t prev;
  };

  class Batch
  {
  public:
    Batch(Bool_t b = kTRUE);
    ~Batch();
  private:
    Bool_t prev;
  };
  
  // In a Utility.C file:
  Quiet::Quiet(Int_t level) : prev(gErrorIgnoreLevel) { gErrorIgnoreLevel = level;};
  Quiet::~Quiet() {gErrorIgnoreLevel = prev;};

  Batch::Batch(Bool_t b) : prev(gROOT->IsBatch()) { gROOT->SetBatch(b) ;};
  Batch::~Batch() { gROOT->SetBatch(prev) ;};
```

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [February 16, 2016, 11:04am UTC](https://root-forum.cern.ch/t/temporarily-turn-off-warnings-in-pyroot/18837/3 "2016-02-16T11:04:05Z")

</div>

Hi Jean-François,

thanks for sharing these snippets.  
Indeed RAII for C++ and context-managers for Python are indeed powerful tools/paradigms.

Cheers,  
Danilo
