# TMVA application and root file creation

**URL:** <https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967>\
**Category:** TMVA\
**Created:** [February 16, 2018, 10:36am UTC](https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967 "2018-02-16T10:36:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geo/32/15552_2.png) [@Geo](https://root-forum.cern.ch/u/Geo)\
**Post date:** [February 16, 2018, 10:36am UTC](https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967/1 "2018-02-16T10:36:17Z")

</div>

Hello, I have one question. I want while I am doing the application in TMVA to create some root files and fill them with some variables _after the application of weights_. I know it seems trivial but I have some issues. Any help?

Cheers

George

---

<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:** [February 16, 2018, 11:22am UTC](https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967/2 "2018-02-16T11:22:56Z")

</div>

Hi George,

Could you elaborate on what it is you want to achieve?

If you want to create a separate TTree in a separate TFile I’d suggest you look into TDataFrame (if you’re on ROOT6).

Without TDataFrame you’d do something along the lines of

```auto
auto f = new TFile(“path”);
auto t = new TTree(“name”);

int x = 0;
t->Branch(“name”, “x/I”, &x);

```

And then in your `TMVA::Reader` loop

```auto
while (reader.HasNext()) {
    // ... Get mva value
    x = /* something derived from the application */;
    t->Fill();
}

t->ResetBranchAddresses();
t->Write();

delete t;
delete f; // File is closed and flushed to disk before it’s destroyed

```

This is a sketch only, so there might be typos.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![Geo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geo/32/15552_2.png) [@Geo](https://root-forum.cern.ch/u/Geo)\
**Post date:** [February 16, 2018, 12:26pm UTC](https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967/3 "2018-02-16T12:26:07Z")

</div>

Hello,

I want to apply the weights in an input file, and then save some of the variables (that the weights have been applied on) in new root file. When I do it I get the output

Error in TFile::ReadBuffer: error reading all requested bytes from file post\_TMVA\_wrong\_inpeak.root, got 0 of 31999

Thank you in advance.

George

---

<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:** [February 19, 2018, 6:42pm UTC](https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967/4 "2018-02-19T18:42:56Z")

</div>

Hi,

What weights are we talking about here, the event weights? To what do you want to apply the weights to? Maybe a simple code example could illustrate a bit more what it is you want to do.

The error could stem from not flushing the TFile properly and opening it from a different place. One thing you could try is to close the file when you are done creating it. This flushes output to disk. You can then open it again where you need to read it. E.g.

```auto
{
    auto f = TFile::Open(...);
    /* do some processing */
    f->Close();
}

{
    auto f2 = TFile::Open(...);
    /*Do new processing*/
    f->Close();
}

```

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![Geo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geo/32/15552_2.png) [@Geo](https://root-forum.cern.ch/u/Geo)\
**Post date:** [February 20, 2018, 8:41am UTC](https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967/5 "2018-02-20T08:41:45Z")

</div>

Hello and thank you for the response. I attach the relevant code. By weights I mean those that are created by TMVA to classify if an event is signal like or background like. What I want this code to to is:  
Take the outptut of the tmva application (some of the variables) and save it in a ROOT file, in a branch.

I hope that I helped somewhat.

Thank you in advance.

Cheers  
George[SingalClassificationApplication\_w2.C](https://root-forum.cern.ch/uploads/default/original/2X/5/54b4345659eaab593d587c14469e1bf016f0e1ef.C) (31.2 KB)

---

<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:** [February 21, 2018, 12:51pm UTC](https://root-forum.cern.ch/t/tmva-application-and-root-file-creation/27967/6 "2018-02-21T12:51:44Z")

</div>

Where do you encounter the problem?

In your code you only write to the tree and to a file, so I don’t see where your error can arise.

```auto
TFile *file_in = new TFile("/tmp/gbillis/post_TMVA_wrong_inpeak.root","RECREATE");
file_in->cd();
postTMVA_inpeak->Write(); // Read error here?
file_in->Close();

```

Cheers,  
Kim
