code: def preserve_current_canvas():
      
      old = ROOT.gPad
      try:
          yield
      finally:
          if old:
              old.cd()
          elif ROOT.gPad:
              # Put things back how they were before.
              with invisible_canvas():
                  # This is a round-about way of resetting gPad to None.
                  # No other technique I tried could do it.
                  pass
docs: Context manager which ensures that the current canvas remains the current
      canvas when the context is left.