def preserve_set_th1_add_directory(state=True):
    """
    Context manager to temporarily set TH1.AddDirectory() state
    """
    with LOCK:
        status = ROOT.TH1.AddDirectoryStatus()
        try:
            ROOT.TH1.AddDirectory(state)
            yield
        finally:
            ROOT.TH1.AddDirectory(status)
Context manager to temporarily set TH1.AddDirectory() state