# Expression weight could not be resolve to a valid formula

**URL:** <https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113>\
**Category:** TMVA\
**Created:** [May 27, 2021, 1:00pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113 "2021-05-27T13:00:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anwar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@anwar](https://root-forum.cern.ch/u/anwar)\
**Post date:** [May 27, 2021, 1:00pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/1 "2021-05-27T13:00:48Z")

</div>

Hello TMVA Experts  
im using TMVAClassification.C Script. I was looking at @moneta github, [tmva-tutorial/notebooks at master · lmoneta/tmva-tutorial · GitHub](https://github.com/lmoneta/tmva-tutorial/tree/master/notebooks)  
and from there i modified the script

```auto
void TMVAClassification() {
   
TMVA::Tools::Instance();

auto outputFile = TFile::Open("TMVA_ClassificationOutput.root", "RECREATE");

TMVA::Factory factory("TMVAClassification", outputFile,
                      "!V:ROC:!Silent:Color:!DrawProgressBar:AnalysisType=Classification" ); 

TString inputFileName = "/path/to/the/file.root";

auto inputFile = TFile::Open( inputFileName );

// --- Register the training and test trees

TTree *signalTree = (TTree*)inputFile->Get("TreeS");
TTree *backgroundTree = (TTree*)inputFile->Get("TreeB");

TMVA::DataLoader * loader = new TMVA::DataLoader("dataset");

// global event weights per tree (see below for setting event-wise weights)
Double_t signalWeight = 1.0;
Double_t backgroundWeight = 1.0;
   
// You can add an arbitrary number of signal or background trees
loader->AddSignalTree ( signalTree, signalWeight );
loader->AddBackgroundTree( backgroundTree, backgroundWeight );

signalTree->Print();

loader->AddVariable( "myvar1 := Jet.PT+Jet.Eta", 'F' );
loader->AddVariable( "myvar2 := Jet.PT-Jet.Eta", "Expression 2", "", 'F' );
loader->AddVariable( "Jet.Phi", "Variable 3", "GeV", 'F' );
loader->AddVariable( "Jet.Mass", "Variable 4", "GeV", 'F' );

// You can add so-called "Spectator variables", which are not used in the MVA training,
// but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the
// input variables, the response values of all trained MVAs, and the spectator variables
loader->AddSpectator( "spec1 := Jet.PT*2", "Spectator 1", "GeV", 'F' );
loader->AddSpectator( "spec2 := Jet.PT*3", "Spectator 2", "GeV", 'F' );

// We can define also the event weights

// Set individual event weights (the variables must exist in the original TTree)
// for signal : factory->SetSignalWeightExpression ("weight1*weight2");
// for background: factory->SetBackgroundWeightExpression("weight1*weight2");
loader->SetBackgroundWeightExpression( "weight" );

// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = "abs(Jet.PT)<200 && abs(Jet.Eta-0.5)<1"; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycutb = "abs(Jet.PT)<200"; // for example: TCut mycutb = "abs(var1)<0.5";

// Tell the factory how to use the training and testing events
//
// If no numbers of events are given, half of the events in the tree are used 
// for training, and the other half for testing:
// loader->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );
// To also specify the number of testing events, use:
// loader->PrepareTrainingAndTestTree( mycut,
// "NSigTrain=3000:NBkgTrain=3000:NSigTest=3000:NBkgTest=3000:SplitMode=Random:!V" );
loader->PrepareTrainingAndTestTree( mycuts, mycutb,
                                    "nTrain_Signal=4000:nTrain_Background=2000:SplitMode=Random:NormMode=NumEvents:!V" );

// Likelihood ("naive Bayes estimator")
factory.BookMethod(loader, TMVA::Types::kLikelihood, "Likelihood",
                           "H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );

// Use a kernel density estimator to approximate the PDFs
factory.BookMethod(loader, TMVA::Types::kLikelihood, "LikelihoodKDE",
                           "!H:!V:!TransformOutput:VarTransform=D:PDFInterpol=KDE:KDEtype=Gauss:KDEiter=Adaptive:KDEFineFactor=0.3:KDEborder=None:NAvEvtPerBin=50" ); 

// Fisher discriminant (same as LD)
factory.BookMethod(loader, TMVA::Types::kFisher, "Fisher", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );

//Boosted Decision Trees
factory.BookMethod(loader,TMVA::Types::kBDT, "BDT",
                   "!V:NTrees=200:MinNodeSize=2.5%:MaxDepth=2:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20" );

//Multi-Layer Perceptron (Neural Network)
factory.BookMethod(loader, TMVA::Types::kMLP, "MLP",
                   "!H:!V:NeuronType=tanh:VarTransform=N:NCycles=100:HiddenLayers=N+5:TestRate=5:!UseRegulator" );

factory.TrainAllMethods();

factory.TestAllMethods();  

factory.EvaluateAllMethods();  

//We enable JavaScript visualisation for the plots
//%jsroot on

auto c1 = factory.GetROCCurve(loader);
c1->Draw();

outputFile->Close();

}

```

But when i run this i got the error

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/c/8c066d57b6f6e2d68ca810120ef0ee350fe134b2.png)

Can some one tell me where is the issue and how to fix.  
Thanks in Advance.

---

<div class="post-metadata">

**Author:** ![RENATO\_QUAGLIANI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renato_quagliani/32/19496_2.png) [@RENATO\_QUAGLIANI](https://root-forum.cern.ch/u/RENATO_QUAGLIANI)\
**Post date:** [May 27, 2021, 1:19pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/2 "2021-05-27T13:19:24Z")

</div>

Just a guess , not an expert here but from the doxygen :

```auto
 void TMVA::DataLoader::SetBackgroundWeightExpression( const TString& variable) 
 {
    DefaultDataSetInfo().SetWeightExpression(variable, "Background");
 }

```

THe `DefaultDataSetInfo` needs to know the variable you want to use as weight i believe.  
I wonder if you need to first AddVariable and then SetWeightExpression with an expression containing the “weight” expression content.  
Cheers

---

<div class="post-metadata">

**Author:** ![RENATO\_QUAGLIANI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renato_quagliani/32/19496_2.png) [@RENATO\_QUAGLIANI](https://root-forum.cern.ch/u/RENATO_QUAGLIANI)\
**Post date:** [May 27, 2021, 1:20pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/3 "2021-05-27T13:20:32Z")

</div>

Also, as the commented code says:

```auto
// Set individual event weights (the variables must exist in the original TTree)
// for signal : factory->SetSignalWeightExpression ("weight1*weight2");
// for background: factory->SetBackgroundWeightExpression("weight1*weight2");

```

→   
`weight` variable need to exist in the original tree. Is that the case ?

---

<div class="post-metadata">

**Author:** ![anwar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@anwar](https://root-forum.cern.ch/u/anwar)\
**Post date:** [May 27, 2021, 1:30pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/4 "2021-05-27T13:30:43Z")

</div>

Thanks for your response.  
Yes, the `weight` variable exist in the tree.

---

<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:** [May 27, 2021, 3:58pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/5 "2021-05-27T15:58:16Z")

</div>

I think there is an issue with the variable `weight` in the tree. Can you please post the full log, that should include the print-out of the background tree (not just the signal tree)

Lorenzo

---

<div class="post-metadata">

**Author:** ![anwar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@anwar](https://root-forum.cern.ch/u/anwar)\
**Post date:** [May 27, 2021, 7:00pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/6 "2021-05-27T19:00:33Z")

</div>

Thanks for your reply  
the file is very heavy and having large number of branches in each tree. i am unable to post it here. can you please provide me your email id so that i could share the file with 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:** [May 28, 2021, 7:10am UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/7 "2021-05-28T07:10:06Z")

</div>

Hi,  
It would be enough for the time being you send me the log file produced by TMVA.  
For the file if it is big you can send me a link (from cernbox or dropbox or Google drive, etc…).  
You can use the direct message from the Forum

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![anwar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@anwar](https://root-forum.cern.ch/u/anwar)\
**Post date:** [May 28, 2021, 1:54pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/8 "2021-05-28T13:54:03Z")

</div>

Hi sir  
[filelog.txt](https://root-forum.cern.ch/uploads/short-url/9ZBI9sh7guTbYM5OhRMAsFlupNo.txt) (120.8 KB)

---

<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:** [May 28, 2021, 4:34pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/9 "2021-05-28T16:34:04Z")

</div>

Hi,

From the log I see there is no `weight` variable in the Tree, there is `Event.Weight` or a `Weight.Weight`

Lorenzo

---

<div class="post-metadata">

**Author:** ![anwar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@anwar](https://root-forum.cern.ch/u/anwar)\
**Post date:** [May 28, 2021, 7:06pm UTC](https://root-forum.cern.ch/t/expression-weight-could-not-be-resolve-to-a-valid-formula/45113/10 "2021-05-28T19:06:21Z")

</div>

Thank You so much sir for your help, it works and i fix the error.  
Thanks again
