# TMVA code issues for fitting data using BDT

**URL:** <https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198>\
**Category:** TMVA\
**Tags:** hist, root\
**Created:** [November 10, 2024, 6:21pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198 "2024-11-10T18:21:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 10, 2024, 6:21pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/1 "2024-11-10T18:21:54Z")

</div>

Hello root expert,

I use TMVA code for fitting data using BDT method.I complete more 70% code steps but isssues toward me and i cant fix them especially in the end part code

```auto
        // Add data to the DataLoader
        loader.AddTrainingEvent({b1_pt, b1_eta, b2_pt, b2_eta, h_mass,h_pt,h_eta,DR_b,jet_pt,jet_eta,jet_phi,jet_ET,jet_mass,
                                 jet_DeltaEta, jet_Deltaphi, jet_bTag, jet_EhadOverEem, missingET_MET,missingEt_Phi,scalarHT_HT});
    }                 
    

```

.Also my last question if i want to put cuts on variables what is the steps to do that ?  
[bb.root](https://root-forum.cern.ch/uploads/short-url/1cThJVrTJEGNp1PQjRIFu2WrUrM.root) (58.0 KB)  
[BDT\_Classification.cpp](https://root-forum.cern.ch/uploads/short-url/4JYWfkhOTvbQenJdesBlgdzB4wE.cpp) (6.2 KB)  
[h90BP1\_bb.root](https://root-forum.cern.ch/uploads/short-url/wqRi1hhgRX3v3QnrH25hitZmAvp.root) (77.6 KB)  
[h90BP2\_bb.root](https://root-forum.cern.ch/uploads/short-url/nX8EO3Lt1yjFtIAPkCnjSFrt3M6.root) (76.8 KB)  
[h95BP1\_bb.root](https://root-forum.cern.ch/uploads/short-url/5vBuKAQo7xfPi3MFvCyOZ8GUWq6.root) (77.0 KB)  
[h96BP1\_bb.root](https://root-forum.cern.ch/uploads/short-url/dSVdJ2YTKK66XbAHOKQKrGZyoLM.root) (77.7 KB)  
[h96BP2\_bb.root](https://root-forum.cern.ch/uploads/short-url/gDbUHRN903kh3QO6ssUe8NrIrSp.root) (78.0 KB)

[z\_bb.root](https://root-forum.cern.ch/uploads/short-url/oTldFLgm9aIp0EaRXkBpkCvKqZR.root) (65.4 KB)  
[histAnalysis.h](https://root-forum.cern.ch/uploads/short-url/tVO60VUOyWGQIEFhpQREwmPgiwX.h) (83.9 KB)  
[h96BP3\_bb.root](https://root-forum.cern.ch/uploads/short-url/vhUpBZwjLFYf2hGPYWOSLdrhyR7.root) (78.7 KB)

[h96BP6\_bb.root](https://root-forum.cern.ch/uploads/short-url/qiDokVxZMmRgeHzGSFcGbSb9mxg.root) (76.0 KB)  
[histAnalysis.C](https://root-forum.cern.ch/uploads/short-url/aVrYQV2wY5fzjQ4wuTfZ5j5fwkX.C) (13.6 KB)  
[h95BP3\_bb.root](https://root-forum.cern.ch/uploads/short-url/c3C6NxfnWNf5sooUoydxtmYE9K4.root) (77.9 KB)  
[h\_bb.root](https://root-forum.cern.ch/uploads/short-url/9fHCqBXbPxUzpbuJf9Sc0CvLGIV.root) (63.0 KB)

.I will give them all complete data file.h,file.c,BDT.C,and file.roots  
thanks  
Ahmed

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 10, 2024, 7:39pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/2 "2024-11-10T19:39:37Z")

</div>

Hi Ahmed,

Thanks for the post. Let me add the expert in the loop @moneta .  
In the meantime, could you please perhaps be a bit more specific? What are the issues you are facing?

Best,  
D

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 10, 2024, 7:52pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/3 "2024-11-10T19:52:29Z")

</div>

This code can not be runned due some issues such as mentioned , previously.

في الأحد، ١٠ نوفمبر ٢٠٢٤، ٩:٤٩ م Danilo via ROOT Forum \<[root.discourse@cern.ch](mailto:root.discourse@cern.ch)\> كتب:

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 10, 2024, 8:18pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/4 "2024-11-10T20:18:38Z")

</div>

Forshort time,i started by Histanalysis.c to generate file root .this part is completed. After than i began to use BDT\_classification.C that based on TMVAclassification that use BDTmethod.some issues appeared as i mention in first successive message…  
Thanks  
Ahmed

في الأحد، ١٠ نوفمبر ٢٠٢٤، ٩:٥٢ م Ahmed Sayed \<[theoriticanphysics554@gmail.com](mailto:theoriticanphysics554@gmail.com)\> كتب:

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 10, 2024, 10:31pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/5 "2024-11-10T22:31:13Z")

</div>

@yus  
@Danilo

---

<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:** [November 11, 2024, 10:57am UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/6 "2024-11-11T10:57:38Z")

</div>

Hi,

Looking at your code, it seems a bit strange. You assign teh events for training directly from the histogram. But Is your plan to train on the bin content values and not on the variables? An histogram is like an event situated at the bin center and weighted by the bin content.

Also, the error you are getting is propbably because you assign only the training event, but you need to add also test events and in addition you need to call the function `PrepareTrainingAndTestTree` as it is done in the tutorial `TMVAClassification.C`

Lorenzo

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 11, 2024, 2:44pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/7 "2024-11-11T14:44:04Z")

</div>

Hi,  
I still to try to debug my issues as you mentiond but the code not run. could you check these issues  
Thanks  
Ahmed  
[BDT\_Classification.C](https://root-forum.cern.ch/uploads/short-url/kXeDMzgddwwoXzyxj0EU8Ovt4ct.C) (7.3 KB)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 11, 2024, 5:46pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/8 "2024-11-11T17:46:49Z")

</div>

Is there exist a solution for these issues

Thanks  
Ahmed

---

<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:** [November 12, 2024, 9:16am UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/9 "2024-11-12T09:16:22Z")

</div>

Hi,  
You need to add the testing events and call the `PrepareTrainingAndTestTree` fucntion, otherwise it will not work  
However, as I mentioned early, your code does not make much sense to me,. by adding bin contents as training values. You need first on how you want to train your BDT and which events you want to use

Lorenzo

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 12, 2024, 1:00pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/10 "2024-11-12T13:00:43Z")

</div>

Hello Dr @moneta,  
I understand your mean partially.do you mean to i use TMVA must be for TTree not use hostograms ?. Because when i add PrepareTrainingAndTestTree  
,run issues appeared me .

في الثلاثاء، ١٢ نوفمبر ٢٠٢٤، ١١:٢٦ ص Lorenzo Moneta via ROOT Forum \<[root.discourse@cern.ch](mailto:root.discourse@cern.ch)\> كتب:

---

<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:** [November 12, 2024, 1:58pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/11 "2024-11-12T13:58:59Z")

</div>

Hi,  
The issue happens because you need to add training signal and traiing background events and also testing events for the two classes.  
I attach here an example working training from histograms. It uses only one variable, but you can extend to your case of multiple variables. It is important you add both training and test events for both classes. If you have a single histogram you would need to split the histogram in two.

Lorenzo

[exampleTMVA\_histogram.C](https://root-forum.cern.ch/uploads/short-url/eEylPseIR6XdyNPKrgkGobOSlUE.C) (2.2 KB)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 12, 2024, 5:36pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/12 "2024-11-12T17:36:26Z")

</div>

Hi Dr Lorenzo,  
i follow all steps by adding train and test event but can nothing any data after running the code

```auto

ahmeedsayed@EndeavourOS TMVAHist]$ root -l
root [0] .L BDT_Classification.cpp
root [1] BDT_Classification()
Factory : Booking method: BDT
:
<WARNING> : Method BDT is not capable of handling classification with 0 classes.
Factory : Train all methods
: ...nothing found to train
Factory : Test all methods
: ...nothing found to test
Factory : Evaluate all methods
: ...nothing found to evaluate
BDT training completed using histograms!
(int) 0
root [2]

```

please insert my code (BDT\_Classification) to solve my issues

Notes: to understand me completely in short i start by histAnalysis.C by putting inputfiles.root to constract output histograms.root that i used after that  
in TMV (BDT\_Classification.cpp) as input.root .

thanks  
Ahmed

> **(attachments)**
>
> [BDT\_Classification.cpp](https://root-forum.cern.ch/uploads/short-url/hT05axCK1hbftvfKfqTmYkc48IL.cpp) (8.26 KB)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 14, 2024, 12:54am UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/13 "2024-11-14T00:54:51Z")

</div>

Hello Dr @moneta ,  
Is there a solution to work the code correctly?.

Thanks,  
Ahmed

---

<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:** [November 14, 2024, 9:27am UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/14 "2024-11-14T09:27:52Z")

</div>

Hi,  
Did you do as in my code example by adding test, training events for both signal and background and calling also PrepareTrainingAndTestTree ?

I guess you probably missed one of these steps,

Lorenzo

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 14, 2024, 10:55am UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/15 "2024-11-14T10:55:00Z")

</div>

Yes i do as your example.

Thanks  
Ahmed

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 14, 2024, 1:53pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/16 "2024-11-14T13:53:58Z")

</div>

@moneta

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 14, 2024, 6:43pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/17 "2024-11-14T18:43:55Z")

</div>

Hi @moneta ,  
Are you mean in TMVA\_hist in you example  
In histogram to split ?! .for one histogram split into two for both of signal and background such as  
you mentioned hs1training,hs2test,hb  
1training and hb2test wothin TMVA or within  
the code of histAbalysis.C

Could you explain by example at least  
One variable in my code BDT\_Classification.C  
To avoid the confusion at me ,please ?

Thanks  
Ahmed

---

<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:** [November 15, 2024, 10:30am UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/18 "2024-11-15T10:30:50Z")

</div>

Hi,  
Can you please share your code and I can try to adapt for your use case. Are you using the histograms as input distribution for your variables (e.g. b1\_pt, b1\_eta, etc…) ? You don’t have access to the values used to fill the histograms? Because it would be better to train directly on the ROOT TTree with the initial varaibles than on the histograms

Lorenzo

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 15, 2024, 12:01pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/19 "2024-11-15T12:01:20Z")

</div>

Hi Dr @moneta ,  
I actually was still confusion to appy your steps till i applied them successfully.Thank alot Dr Moneta for your a great effort But still i have a single problem espectially if I apply cuts, the code make issues such that

```auto
[ahmeedsayed@EndeavourOS TMVAHist]$ root -l 
root [0] .L BDT_Classification.cpp 
root [1] BDT_Classification() 
DataSetInfo : [dataset] : Added class "Signal"
DataSetInfo : [dataset] : Added class "Background"
                         : Add Tree TrainAssignTree_Signal of type Signal with 3000 events
                         : Add Tree TestAssignTree_Signal of type Signal with 3000 events
                         : Add Tree TrainAssignTree_Background of type Background with 3000 events
                         : Add Tree TestAssignTree_Background of type Background with 3000 events
                         : Dataset[dataset] : Class index : 0 name : Signal
                         : Dataset[dataset] : Class index : 1 name : Background
Factory : Booking method: BDT
                         : 
                         : Rebuilding Dataset dataset
                         : Building event vectors for type 0 Signal
                         : Dataset[dataset] : create input formulas for tree TrainAssignTree_Signal
                         : Building event vectors for type 1 Signal
                         : Dataset[dataset] : create input formulas for tree TestAssignTree_Signal
                         : Building event vectors for type 0 Background
                         : Dataset[dataset] : create input formulas for tree TrainAssignTree_Background
                         : Building event vectors for type 1 Background
                         : Dataset[dataset] : create input formulas for tree TestAssignTree_Background
DataSetFactory : [dataset] : Number of events in input trees
                         : Dataset[dataset] : Signal requirement: "(b1_pt > 40 && b2_pt > 30 && h_mass > 40 && h_pt > 40 && h_eta < 10 && scalarHT_HT > 60 )"
                         : Dataset[dataset] : Signal -- number of events passed: 0 / sum of weights: 0    
                         : Dataset[dataset] : Signal -- efficiency : 0     
                         : Dataset[dataset] : Background requirement: "(b1_pt > 40 && b2_pt > 30 && h_mass > 40 && h_pt > 40 && h_eta < 10 && scalarHT_HT > 60 )"
                         : Dataset[dataset] : Background -- number of events passed: 0 / sum of weights: 0    
                         : Dataset[dataset] : Background -- efficiency : 0     
                         : Dataset[dataset] : you have opted for interpreting the requested number of training/testing events
                         : to be the number of events AFTER your preselection cuts
                         : 
<FATAL> : Dataset[dataset] : More events requested for training (3000) than available (0)!
***> abort program execution
Error in <TRint::HandleTermInput()>: std::runtime_error caught: FATAL error
root [2] 

```

But without cuts the code is runned successfully.

Thanks  
Ahmed  
[BDT\_Classification.cpp](https://root-forum.cern.ch/uploads/short-url/lz3tfA51FUCtvfLzCfHw3wZ7m5M.cpp) (15.6 KB)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Sayed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahmed_sayed/32/31955_2.png) [@Ahmed\_Sayed](https://root-forum.cern.ch/u/Ahmed_Sayed)\
**Post date:** [November 16, 2024, 3:10pm UTC](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198/20 "2024-11-16T15:10:29Z")

</div>

@moneta

[Next page](https://root-forum.cern.ch/t/tmva-code-issues-for-fitting-data-using-bdt/62198.md?page=2)
