# TMVA::Reader::AddVariable: How to cast Double\_t type variable to Float\_t

**URL:** <https://root-forum.cern.ch/t/tmva-how-to-cast-double-t-type-variable-to-float-t/48928>\
**Category:** TMVA\
**Tags:** tmva\
**Created:** [February 23, 2022, 3:22pm UTC](https://root-forum.cern.ch/t/tmva-how-to-cast-double-t-type-variable-to-float-t/48928 "2022-02-23T15:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shubh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shubh/32/28435_2.png) [@shubh](https://root-forum.cern.ch/u/shubh)\
**Post date:** [February 23, 2022, 3:22pm UTC](https://root-forum.cern.ch/t/tmva-how-to-cast-double-t-type-variable-to-float-t/48928/1 "2022-02-23T15:22:39Z")

</div>

Dear Experts,

While evaluating the MVA response through `TMVA::Reader::AddVariable`. I faced the problem of the method `AddVariable` only accepting `Float_t` variables (or `Int_t`), but not `Double_t`. I can import variables of type double from my nTuple to train an MVA method with the TMVA::DataLoader but cannot through ‘TMVA::Reader::AddVariable’ because of the reason as stated.  
[train.C](https://root-forum.cern.ch/uploads/short-url/ioVgBGMQcuutKfusQG99iDpvHpF.C) (2.4 KB)

Along with the MVA response, I am also want to save the probability and rarity distribution. The macro apply.C is used to save their distribution. see  
[apply.C](https://root-forum.cern.ch/uploads/short-url/iGrT09AsgQw8674sqS6sgbFWIVx.C) (3.8 KB)

The data coming from a TTree, which I did not create that already has `Double_t` branches, so to use them, the only way was casting to `Float_t`.

Kindly please suggest how to cast the `Double_t` type variables to `Float_t` type or some other solution which can take `Double_t` type variables.

Please find the attached sample for the signal and background used for training.  
[sig\_train.root](https://root-forum.cern.ch/uploads/short-url/a186CgmZaaACSbVSokHIcdhMYSp.root) (2.3 MB)  
[bkg\_train.root](https://root-forum.cern.ch/uploads/short-url/gr1MlqvQZ95sH5KH422HIn2dfcv.root) (2.3 MB)

Thank you.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [February 28, 2022, 4:29pm UTC](https://root-forum.cern.ch/t/tmva-how-to-cast-double-t-type-variable-to-float-t/48928/2 "2022-02-28T16:29:39Z")

</div>

Hi,

You can use an input double from the Tree, but you need to pass a float pointer in `Reader::AddVariable`. Like in this example (here the input Tree is having an integer and a float is used in AddVariable):

> <https://github.com/root-project/root/blob/637c945acc713babe7fe6caebfc19988f4fb8e12/tmva/tmva/test/crossvalidation/TestCrossValidationIntVar.cxx#L108>

You pass a double pointer in `Tree::SetBranchAddress` as following:

```auto
double xd; 
float xf;
tree->SetBranchAddress("variable",&xd);
reader->AddVariable("variable",&xf); 

```

and in the event loop you do:

```auto
tree->GetEntry(ievt);
xf = xd; 
reader->EvaluateMVA("MyBDT");

```

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![shubh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shubh/32/28435_2.png) [@shubh](https://root-forum.cern.ch/u/shubh)\
**Post date:** [March 5, 2022, 9:04am UTC](https://root-forum.cern.ch/t/tmva-how-to-cast-double-t-type-variable-to-float-t/48928/3 "2022-03-05T09:04:51Z")

</div>

Thank you for your suggestions. I have implemented it in my code, and it works.
