# How to save an existing histogram into TTree

**URL:** <https://root-forum.cern.ch/t/how-to-save-an-existing-histogram-into-ttree/31264>\
**Category:** TMVA\
**Tags:** tree, python, hist\
**Created:** [October 24, 2018, 12:14pm UTC](https://root-forum.cern.ch/t/how-to-save-an-existing-histogram-into-ttree/31264 "2018-10-24T12:14:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![psiddire](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/f17d59/32.png) [@psiddire](https://root-forum.cern.ch/u/psiddire)\
**Post date:** [October 24, 2018, 12:14pm UTC](https://root-forum.cern.ch/t/how-to-save-an-existing-histogram-into-ttree/31264/1 "2018-10-24T12:14:50Z")

</div>

I have some histograms stored in a TDirectoryFile in a root file. I am trying to save some of those histograms into a TTree in a new root file. I tried the following but I am unable to write the histograms to the tree:

```auto
file = 'DYJetsToLL_M-50_TuneCP5_13TeV-amcatnloFXFX-pythia8_v14-v1.root'
f = TFile("DYJetsToLL.root","RECREATE")
t = TTree('TreeS', 'Test')
MuTaufile = TFile(file)
lfv_histo = MuTaufile.Get('TightOS')
hlist = lfv_histo.GetListOfKeys()
iter = TIter(hlist)
for i in iter:
    histo = MuTaufile.Get('TightOS/'+i.GetName())
    histo.Write(t)
f.Write()

```

What should I do?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 24, 2018, 5:44pm UTC](https://root-forum.cern.ch/t/how-to-save-an-existing-histogram-into-ttree/31264/2 "2018-10-24T17:44:49Z")

</div>

> I am trying to save some of those histograms into a TTree in a new root file.

What is the end purpose of this transformation? (knowning that might influence what needs to be done).

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![psiddire](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/f17d59/32.png) [@psiddire](https://root-forum.cern.ch/u/psiddire)\
**Post date:** [October 24, 2018, 7:36pm UTC](https://root-forum.cern.ch/t/how-to-save-an-existing-histogram-into-ttree/31264/3 "2018-10-24T19:36:48Z")

</div>

So, in the TDirectoryFile I have the following variables filled: mPt, tPt, mtCollinearMass, mtVisibleMass. I am trying to save them in a tree because I want to input the tree into a BDT in TMVA for some classification of signal and background. I know that I can run the analyzer to fill the TTree instead of saving the histograms in a TDirectoryFile but the analyzer takes atleast several hours to run. So, I though there will be a workaround for this.

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [October 25, 2018, 11:10am UTC](https://root-forum.cern.ch/t/how-to-save-an-existing-histogram-into-ttree/31264/4 "2018-10-25T11:10:40Z")

</div>

Hi,

A histogram is aggregate data and does not save data points for each individual event, as such it’s unsuitable to use as input to TMVA and I’m afraid you’ll have to re-run the generation.

Cheers,  
Kim

---

<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:** [November 8, 2018, 11:10am UTC](https://root-forum.cern.ch/t/how-to-save-an-existing-histogram-into-ttree/31264/5 "2018-11-08T11:10:46Z")

</div>

Closed on November 8, 2018, 11:10am UTC

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