# Queries about TMVA Application

**URL:** <https://root-forum.cern.ch/t/queries-about-tmva-application/40000>\
**Category:** TMVA\
**Created:** [June 20, 2020, 12:36pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000 "2020-06-20T12:36:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 20, 2020, 12:36pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/1 "2020-06-20T12:36:26Z")

</div>

Hi,  
I was running TMVA Application after training and testing. But the output histogram that I got doesn’t seem right. I am getting only a vertical peak at ‘-0.2’ (as in the attached figure). Could you check the macro and point out if anything wrong. I actually didn’t understand how the application one works. It would be good if you could explain a bit like why we are only feeding the input of the signal and not the background. The Training and Application macros along with the signal and background input files can be found [here](https://www.dropbox.com/sh/ue9tcnhmpkcda22/AACwebpsWkFzkvra0T9aSCAOa?dl=0).

![Canvas_1](https://root-forum.cern.ch/uploads/default/original/3X/e/4/e4e2eea7fb9f95df6acbe8f0628adcb1d8537fb9.png)

Regards,  
Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 7:37am UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/2 "2020-06-22T07:37:26Z")

</div>

Hello,

does the training work correctly? Did you e.g. try the `TMVAGUI.C` to check that the models respond correctly? (A broken model can also create responses like this …)

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 7:39am UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/3 "2020-06-22T07:39:22Z")

</div>

Yes, the training worked and the TMVAGUI also appeared.  
Thanks Stephan.

Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 7:40am UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/4 "2020-06-22T07:40:15Z")

</div>

I found the problem:  
You are never writing into the variables that you gave to the reader. See the assignments in the loop in the example code here:  
[https://root.cern.ch/doc/master/classTMVA\_1\_1Reader.html](https://root.cern.ch/doc/master/classTMVA_1_1Reader.html)

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 7:42am UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/5 "2020-06-22T07:42:47Z")

</div>

I guess what can also work is to attach the variables _both_ to the reader _and_ as branches to the tree, so the tree directly loads them in a place where the reader picks them up.

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 8:16am UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/6 "2020-06-22T08:16:21Z")

</div>

Thanks again, Stephan. I got the mistakes. And corrected it. Could you please check the code again; because, I am getting the same kind of result again. Here’s the code.  
[Application.C](https://root-forum.cern.ch/uploads/short-url/ihxPFAnFVKb244WoOaPcHlq56GL.C) (3.6 KB)  
 ![Canvas_1](https://root-forum.cern.ch/uploads/default/original/3X/b/e/bee145e1df4a990f616b59d5691fa714104d854e.png)

Thanks,  
Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 12:34pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/7 "2020-06-22T12:34:53Z")

</div>

I cannot run your example, but can you simply test if the values of `zlepM, zdPHI` etc change when you load the next event? Just print them.

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 1:08pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/8 "2020-06-22T13:08:07Z")

</div>

Yes, I checked and there is something wrong. I am getting values ‘0’ for all events. That means it’s not getting loaded to the reader, right? But, how to fix this?

Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 1:10pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/9 "2020-06-22T13:10:08Z")

</div>

Can you first check that values are getting loaded from the tree?

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 1:22pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/10 "2020-06-22T13:22:12Z")

</div>

Yes. So when I added the second print option it printed ‘0’ for all, but no error. When I added the first one to check the variables from the TTree then I got errors. The event loop was this:

```auto
for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
 if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;

 theTree->GetEntry(ievt);
//Print variables from the Tree
if(ievt>1000 && ievt<1010){cout<< "lepPt = " <<lepPt<<"\t MissingET = "<<zMissingET<<endl;}

zlepPt = lepPt;
zETAlep = ETAlep;
zPHIlep = PHIlep;
zlepM = lepM;
zdPHI = dPHI;
zdRll = dRll;
zaxialMET = axialMET;
zfracPT =fracPT;
zMissingET = MissingET;
zETAmiss = ETAmiss;
zPHImiss = PHImiss;

//Print variables for the Reader
if(ievt>1000 && ievt<1010){cout<< "zlepPt = " <<zlepPt<<"\t zMissingET = "<<zMissingET<<endl;}

if (Use["BDTF"]) histBdtF ->Fill( reader->EvaluateMVA( "BDTF method" ) );
}

```

The error I got is this:

 ![Screenshot from 2020-06-22 18-50-27](https://root-forum.cern.ch/uploads/default/original/3X/4/d/4d71b470d1ad080a5504bb2fa0ce4e43f468cc87.png)

Saumyen

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 1:29pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/11 "2020-06-22T13:29:24Z")

</div>

I guess this is because the variables were saved as Double\_t and here I am calling as Float\_t. So I changed it. Then I am getting this:

 ![Screenshot from 2020-06-22 18-58-14](https://root-forum.cern.ch/uploads/default/original/3X/6/c/6c149a4fafbef03b21967de5ff521c9346c098ac.png)

Should I change the variables for the Reader too to Float\_t?

Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 1:52pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/12 "2020-06-22T13:52:41Z")

</div>

The reader takes floats, as you can see in the documentation I linked at the beginning.  
So yes, make it floats, and assign from the doubles to the floats, so the conversion to floats happens automatically.

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 1:59pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/13 "2020-06-22T13:59:07Z")

</div>

Okay, I see. Then what can be the fix?  
By the way, in the input file, there are NaN values to the variables. In the Training macro, I put a cut. But how to put the cut in Application?

Regards,  
Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 2:29pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/14 "2020-06-22T14:29:19Z")

</div>

The fix:

1. Make a double, connect to tree.
2. Make a float, connect to reader.
3. After loading an event, assign:  
`inputReader = outputTree`.

How to cut?  
Just skip the event.

```auto
if (std::isnan(value))
  continue;

```

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 4:15pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/15 "2020-06-22T16:15:01Z")

</div>

Okay, so the cut thing never worked I tried different argument for the `if`-loop, like `if(std::isnan(axialMET))` or `if (!std::isnan(axialMET))` or `if(!TMath::IsNaN(axialMET))` or `if(TMath::IsNaN(axialMET))`. None worked. So I tried with an input that doesn’t have a NaN. And It worked fine (I guess). I got this output histo.  
 ![Canvas_1](https://root-forum.cern.ch/uploads/default/original/3X/c/9/c9a6884e1715a172d9a0d4a5d67dcf894ece4078.png)  
This looks fine, right, compared to the test output!  
 ![mva_BDTF](https://root-forum.cern.ch/uploads/default/original/3X/1/b/1b25d32a44359978c7006e8d563a0c30a74cb720.png)

Thank you so so much, Stephan, for this help.

But, what about the other question? I mean why we are only giving input of the signal and not of the background? How do we compare and confirm the separation from the background?

Thanks again,  
Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 4:32pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/16 "2020-06-22T16:32:38Z")

</div>

If you get real NaNs, cutting them out should work. Also note that every comparison with a NaN will always fail, so even this

```auto
if (!(0 < MET && MET < 100)
 || !(0 < PT && PT < 150 ) )
  continue;

```

etc will cut out NaNs. You have to make sure, though, that you test **all** variables that go into the MVA.

I don’t see the other question, so I couldn’t answer it. It doesn’t make sense to only give the input of the signal, so I wouldn’t know why one would do that.  
How do you confirm separation? You run separately for signal and background, and check that the values are different. That’s what the blue and yellow plot from the `TMVAGui.C` is.

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 5:21pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/17 "2020-06-22T17:21:25Z")

</div>

Thanks Stephan again.  
Actually, whichever application macro I saw so far they only calling the signal tree and not the background one (even if it’s in the same file). The TMVAClassificationApplication.C file in the tutorial directory, that also call the Signal tree only. So I was wondering if it’s like you call the signal first and then the background separately and then superimpose the two histograms manually later. Is it the case, or I am getting it wrong?

Another question was if I should use (as input) in application the same input files that I trained with. Or, I can use different input files of the same model.  
**Sorry** if these questions sound trivial. I am actually trying to understand what is the purpose of this application and how it works.

Regards,  
Saumyen

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 22, 2020, 5:26pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/18 "2020-06-22T17:26:38Z")

</div>

> [@Saumyenk](#):
>
> So I was wondering if it’s like you call the signal first and then the background separately and then superimpose the two histograms manually later. Is it the case?

Yes.

> [@Saumyenk](#):
>
> Another question was if I should use (as input) in application the same input files that I trained with. Or, I can use different input files of the same model.

If you use the same input for training and application, you can just test the model, but you cannot use this for anything serious, since models usually perform better on their training data.  
The whole point of machine learning is that you can later apply your trained model to new data, so it can make some predictions.  
So yes, you _can_ use the same files, but it’s not advisable unless you want to run some tests.

> [@Saumyenk](#):
>
> **Sorry** if these questions sound trivial. I am actually trying to understand what is the purpose of this application and how it works.

The purpose is to evaluate the trained model on new data. That can either be simulations where you know the result already. In this case it’s to test how good the model is. Or it’s data where you don’t know what it is, and here you use the classifier to make a prediction for you.

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [June 22, 2020, 6:26pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/19 "2020-06-22T18:26:20Z")

</div>

Thank you so much Stephan for the help and the explanation. Thanks a lot. This will help me…

Regards,  
Saumyen

---

<div class="post-metadata">

**Author:** ![faze75373046](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@faze75373046](https://root-forum.cern.ch/u/faze75373046)\
**Post date:** [October 23, 2021, 6:17pm UTC](https://root-forum.cern.ch/t/queries-about-tmva-application/40000/20 "2021-10-23T18:17:22Z")

</div>

sorry , how could you get the second picture ? TMVAClassificationApplication.C’s output is just TMVApp.root ,this root file doesnot includes the second picture.(my english is poor)

[Next page](https://root-forum.cern.ch/t/queries-about-tmva-application/40000.md?page=2)
