# Trouble writing to file w/ TMVA

**URL:** <https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298>\
**Category:** TMVA\
**Created:** [June 26, 2017, 8:46am UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298 "2017-06-26T08:46:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Zheng](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/c4cdca/32.png) [@Daniel\_Zheng](https://root-forum.cern.ch/u/Daniel_Zheng)\
**Post date:** [June 26, 2017, 8:46am UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298/1 "2017-06-26T08:46:23Z")

</div>

The following is my code for a BDT using TMVA. I need it to write the BDT response to a file, but that just is not happening… I suspect I’m not using the dataloader correctly, but I have messed with things with no success for a few days now. Any help would be appreciated.

```auto
signal_file=TFile('signal.root') #signal
background_file=TFile('background.root') #background
tree_s=signal_file.Get('microtuple')
tree_b=background_file.Get('microtuple')

# why not writing to file?
out_file=TFile('alllcuts_BDT.root', 'RECREATE')

factory = TMVA.Factory("TMVAClassification",out_file,"V:!Silent:Color:Transformations=I:DrawProgressBar:AnalysisType=Classification"); 

dataloader = TMVA.DataLoader()

dataloader.AddVariable('jj_mass', 'F')
dataloader.AddVariable('met_et', 'F')

dataloader.AddSignalTree(tree_s)
dataloader.AddBackgroundTree(tree_b)

# additional cuts
signal_cut=TCut('jj_mass>1000000 && met_et>150000')
background_cut=TCut('jj_mass>1000000 && met_et>150000')

dataloader.PrepareTrainingAndTestTree(signal_cut, background_cut, ':'.join(['nTrain_Signal=0', 'nTrain_Background=0',
                        'SplitMode=Random', 'NormMode=NumEvents', '!V']))

method = factory.BookMethod(dataloader, TMVA.Types.kBDT, 'BDTG', ':'.join(['!H', '!V', 'NTrees=850',
                     'MaxDepth=3', 'BoostType=Grad', 'Shrinkage=1.0',
                    'SeparationType=GiniIndex', 'nCuts=20', 'PruneMethod=NoPruning']))

factory.TrainAllMethods()
factory.TestAllMethods()
factory.EvaluateAllMethods()
print('done')

```

---

<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:** [June 26, 2017, 9:24am UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298/2 "2017-06-26T09:24:29Z")

</div>

Hi,

Can you try running with

```auto
// Save the output
   out_file->Close();

```

in the very end? This should make sure the file and all contents gets written to disk.

---

<div class="post-metadata">

**Author:** ![Daniel\_Zheng](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/c4cdca/32.png) [@Daniel\_Zheng](https://root-forum.cern.ch/u/Daniel_Zheng)\
**Post date:** [June 26, 2017, 9:42am UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298/3 "2017-06-26T09:42:34Z")

</div>

Hi, Thanks for the suggestion! I tried that and it didn’t work. In the file that is created (allcuts\_BDT.root), there is only one folder named default;1 … this corresponds to the argument i feed the dataloader constructer TMVA.Dataloader(). If I do dataloader = TMVA.DataLoader(“foldername”) then the output root file also has that folder. Except it’s blank…

---

<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:** [June 26, 2017, 12:49pm UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298/4 "2017-06-26T12:49:02Z")

</div>

Ok, too bad 😕

Could you try running from c++ instead of pyROOT just to make sure that it is unrelated to that?

What is the output of TMVA when you run your script?

---

<div class="post-metadata">

**Author:** ![Daniel\_Zheng](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/c4cdca/32.png) [@Daniel\_Zheng](https://root-forum.cern.ch/u/Daniel_Zheng)\
**Post date:** [June 26, 2017, 1:22pm UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298/5 "2017-06-26T13:22:57Z")

</div>

Success!!! I realized I was doing output\_file.close() instead of output\_file.Close()… My bad. Thank you so much, it works fine now!

---

<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:** [June 26, 2017, 1:26pm UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298/6 "2017-06-26T13:26:41Z")

</div>

Glad I could help! Please mark this post as solved to make it easier for people to find answers in the future 🙂

---

<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:** [July 10, 2017, 1:26pm UTC](https://root-forum.cern.ch/t/trouble-writing-to-file-w-tmva/25298/7 "2017-07-10T13:26:54Z")

</div>

Closed on July 10, 2017, 1:26pm UTC

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