# Question about TMVA::DataLoader::AddVariable()

**URL:** <https://root-forum.cern.ch/t/question-about-tmva-addvariable/60455>\
**Category:** TMVA\
**Created:** [August 9, 2024, 6:15am UTC](https://root-forum.cern.ch/t/question-about-tmva-addvariable/60455 "2024-08-09T06:15:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TField](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tfield/32/36014_2.png) [@TField](https://root-forum.cern.ch/u/TField)\
**Post date:** [August 9, 2024, 6:15am UTC](https://root-forum.cern.ch/t/question-about-tmva-addvariable/60455/1 "2024-08-09T06:15:20Z")

</div>

Can TMVA::DataLoader::AddVariable() method deal with more sophiscated input variable rather than, for exmple, dataloader-\>AddVariable(“myvar1 := var1+var2”, ‘F’);

For example,  
//////////////////////////////////////////////////////////////////////////  
Float\_t minNonZero=2.0;  
for (int i = 0; i \< 7; i++) {  
if ((fE.rata[i] != 0) && (fE.rata[i] \< minNonZero)) {  
minNonZero = fE.rata[i] ;  
}  
}  
fE.minrata=minNonZero;  
////////////////////////////////////////////////////////////////////////////  
dataloader-\>AddVariable( “minrata”, ‘F’ );  
////////////////////////////////////////////////////////////////////////////

How could I combine the two scrips in Classification.cxx, instead of doing the loop adding new branch in original tree then dataloader-\>AddVariable()?

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [August 9, 2024, 6:54am UTC](https://root-forum.cern.ch/t/question-about-tmva-addvariable/60455/2 "2024-08-09T06:54:46Z")

</div>

Hello @TField, thanks for the question.  
I think @moneta will be able to help you with this.

---

<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:** [August 9, 2024, 8:05am UTC](https://root-forum.cern.ch/t/question-about-tmva-addvariable/60455/3 "2024-08-09T08:05:36Z")

</div>

Hello,  
You can use in `AddVariable` any expression supported by `TTreeFormula`, i.e. an expression written as a string. It is the same syntax used also in `TTree::Draw`.

Lorenzo

---

<div class="post-metadata">

**Author:** ![TField](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tfield/32/36014_2.png) [@TField](https://root-forum.cern.ch/u/TField)\
**Post date:** [August 9, 2024, 12:26pm UTC](https://root-forum.cern.ch/t/question-about-tmva-addvariable/60455/4 "2024-08-09T12:26:00Z")

</div>

So only simple expression can be used in AddVariable(), rather than, for example, “for loop”, etc.🙃  
However, I am wondering whether there is a way, for example, select the second smallest array element putting in the AddVariable() methods without doing the selection in another script:  
//////////////////////////////////////////////////////////////////////////////////  
“THIS IS FOR DEMONSTRATION”  
Float\_t a=0,k1,k2;  
tr-\>SetBranchAddress(“evt.r1”, &k1);  
tr-\>SetBranchAddress(“evt.r2”, &k2);  
a=k1+k2;  
dataloader-\>AddVariable( “a”, ‘F’ );  
//////////////////////////////////////////////////////////////////////////////////  
So is there any way to process the variable before dataloader::AddVariable(), then put it in, in the same scope.  
Thanks
