# Delete TH1 from rootfile

**URL:** <https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932>\
**Category:** ROOT\
**Created:** [November 22, 2019, 6:36pm UTC](https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932 "2019-11-22T18:36:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [November 22, 2019, 6:36pm UTC](https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932/1 "2019-11-22T18:36:09Z")

</div>

Hi, I’m trying to delete a few histograms in my rootfile, to hadd it with another smaller rootfile with the correct histograms, this is what I do:

> TFile \*f = TFile::Open(“NTUP\_PILEUP.pool.root.1”,“update”);  
> f-\>cd(“PileupReweighting”)  
> f-\>Delete(“pileup\_TH1”);

So it’s not complaining but it doesn’t delete the histograms.

I also tried:

> gDirectory-\>Delete(“pileup\_TH1”)

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 23, 2019, 8:42am UTC](https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932/2 "2019-11-23T08:42:51Z")

</div>

```cpp
TFile *f = TFile::Open("NTUP_PILEUP.pool.root.1", "update");
f->cd("PileupReweighting");
gDirectory->Delete("pileup_TH1;*"); // all "cycles"
// f->Write();
delete f;

```

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [November 23, 2019, 12:03pm UTC](https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932/3 "2019-11-23T12:03:06Z")

</div>

It worked, thanks a lot!

Btw what are these cycles and why do we have to delete “f”?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 23, 2019, 3:41pm UTC](https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932/4 "2019-11-23T15:41:26Z")

</div>

The “`delete f;`” will “close” the opened file (and cleanup RAM).

> [@Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2):
>
> Search for “cycle” and/or “namecycle”, for example in: In a Nutshell: [Input and Output](https://root.cern.ch/d/input-and-output) . ROOT User’s Guide: [Input/Output](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#inputoutput) : [Retrieving Objects from Disk](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#retrieving-objects-from-disk) , [Subdirectories and Navigation](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#subdirectories-and-navigation) . Just for reference: [some additional notes about GetObject, Get, FindObjectAny, FindKeyAny, ReadObj, ReadObjAny](https://root-forum.cern.ch/t/broken-tdirectoryfile-findobjectany-and-tdirectoryfile-findkeyany/20943/16) .

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2019, 12:04pm UTC](https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932/5 "2019-11-25T12:04:37Z")

</div>

Also the command line tool `rootrm` might be of some help in that case (surely simpler)

---

<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:** [December 9, 2019, 12:04pm UTC](https://root-forum.cern.ch/t/delete-th1-from-rootfile/36932/6 "2019-12-09T12:04:40Z")

</div>

Closed on December 9, 2019, 12:04pm UTC

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