# No way to overwrite using Snapshot

**URL:** <https://root-forum.cern.ch/t/no-way-to-overwrite-using-snapshot/37936>\
**Category:** ROOT\
**Tags:** rdataframe, feature\
**Created:** [February 11, 2020, 5:32pm UTC](https://root-forum.cern.ch/t/no-way-to-overwrite-using-snapshot/37936 "2020-02-11T17:32:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [February 11, 2020, 5:32pm UTC](https://root-forum.cern.ch/t/no-way-to-overwrite-using-snapshot/37936/1 "2020-02-11T17:32:33Z")

</div>

There is currently no way to overwrite existing output using `Snapshot` and one ends up with multiple cycles:

```python
In [10]: f.hi.ls()
TDirectoryFile* hi	hi
 KEY: TTree	there;2	there
 KEY: TTree	there;1	there

```

I have made a pull request ([PR4965](https://github.com/root-project/root/pull/4965)) adding this feature. Reproducer follows.

* * *

```python

In [2]: df = ROOT.RDataFrame(10).Define('e', 'rdfentry_')
df.
In [3]: df.Snapshot('hi/there', 'test.root')
Out[3]: <ROOT.ROOT::RDF::RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0x7fc3a7011730>

In [4]: df2 = ROOT.RDataFrame(10).Define('e', 'rdfentry_ * 10')

In [5]: snapopts = ROOT.RDF.RSnapshotOptions()

In [6]: snapopts.fMode = 'UPDATE'

In [7]: df2.Snapshot('hi/there', 'test.root', '', snapopts)
Error in <TFile::mkdir>: An object with name hi exists already
Out[7]: <ROOT.ROOT::RDF::RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0x7fc3a3df8150>

In [8]: f = ROOT.TFile.Open('test.root')

In [9]: f.ls()
TFile** test.root	
 TFile* test.root	
  KEY: TDirectoryFile	hi;1	hi

In [10]: f.hi.ls()
TDirectoryFile* hi	hi
 KEY: TTree	there;2	there
 KEY: TTree	there;1	there

```

after fix:

```python
In [1]: import ROOT

In [2]: df = ROOT.RDataFrame(10).Define('e', 'rdfentry_')

In [3]: df.Snapshot('hi/there', 'test.root')
Out[3]: <ROOT.ROOT::RDF::RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0x7fa041f1d1b0>

In [5]: df2 = ROOT.RDataFrame(10).Define('e', 'rdfentry_ * 10')

In [7]: snapopts = ROOT.RDF.RSnapshotOptions()

In [8]: snapopts.fMode = 'UPDATE'

In [9]: snapopts.fOverwrite = True

In [10]: df2.Snapshot('hi/there', 'test.root', '', snapopts)
Error in <TFile::mkdir>: An object with name hi exists already
Out[10]: <ROOT.ROOT::RDF::RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0x7fa04102be70>

In [11]: f = ROOT.TFile.Open('test.root')

In [12]: f.ls()
TFile** test.root	
 TFile* test.root	
  KEY: TDirectoryFile	hi;1	hi

In [13]: f.hi.ls()
TDirectoryFile* hi	hi
 KEY: TTree	there;1	there

```

* * *

_ROOT Version:_ master  
_Platform:_ macOS  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![swunsch](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@swunsch](https://root-forum.cern.ch/u/swunsch)\
**Post date:** [February 12, 2020, 9:00am UTC](https://root-forum.cern.ch/t/no-way-to-overwrite-using-snapshot/37936/2 "2020-02-12T09:00:56Z")

</div>

The discussion is moved to the [linked PR](https://github.com/root-project/root/pull/4965)!

---

<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:** [February 26, 2020, 9:05am UTC](https://root-forum.cern.ch/t/no-way-to-overwrite-using-snapshot/37936/3 "2020-02-26T09:05:33Z")

</div>

Closed on February 26, 2020, 9:05am UTC

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