# Vector entries in TMVAClassification and Application

**URL:** <https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124>\
**Category:** TMVA\
**Created:** [August 5, 2018, 6:57pm UTC](https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124 "2018-08-05T18:57:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KarenDmn](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@KarenDmn](https://root-forum.cern.ch/u/KarenDmn)\
**Post date:** [August 5, 2018, 6:57pm UTC](https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124/1 "2018-08-05T18:57:09Z")

</div>

Hello dear ROOTforum,

I have trouble with TMVA’s macros. The tree source has branches type vector, in the part of dataloader I put this

```auto
  dataloader->AddVariable( "B_J_px2", "B_J_px2", "units", 'F' );

```

and works fine. But in the part of Application, I stated the same variables in the part of classification (like tutorial explain)

```auto
TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used

   Float_t B_J_px2; ...
   reader->AddVariable( "B_J_px2", &B_J_px2 );...

TFile* datafile = TFile::Open("Datafile.root");
std::cout << "TMVAClassificationApp :Using input file: " << datafile->GetName() <<std::endl; `

std::cout << "--- Select signal sample" << std::endl;
TFile* signalfile = new TFile("MC_file.root"); //open the file
TTree* signaltree = (TTree*)signalfile->Get("rootuple/ntuple");
  
signaltree->SetBranchAddress( "B_J_px2", &B_J_px2 );

```

The application macro compiles well, but when I execute it I got this:

```auto
Error in <TTree::SetBranchAddress>: The pointer type given "Float_t" (5) does not correspond to the type needed "vector<float>" by the branch: B_J_px2

```

I thought that the solution it could be put ‘vector’ in the part of dataloader, I mean

```auto
 dataloader->AddVariable( "B_J_px2", "B_J_px2", "units", 'vector<float> type' );

```

but reading TMVA documentation ([https://root.cern/doc/v612/classTMVA\_1\_1DataLoader.html#af2de13debc441fd2c4f1cd826ef175f9](https://root.cern/doc/v612/classTMVA_1_1DataLoader.html#af2de13debc441fd2c4f1cd826ef175f9)) I saw that not is possible.

I saw this contribution in the forum ([Vector in Branch](https://root-forum.cern.ch/t/vector-in-branch/12796)) but I can’t do that, due to I have to redeclare the variables and it isn’t allowed.

Another thing that I think it could be the solution is reading vector branches as arrays of float variables, in the SetBranchesAddress, but I don’t know if it affects the reading of weights.

Could you please help me with this? I attach the macros of classification and application and a link with the root files. [TMVAClassification\_Bmesonfinal.C](https://root-forum.cern.ch/uploads/default/original/3X/1/0/100075184f2559b6ac66885537f76f1c0bedc56e.C) (23.0 KB)  
[TMVAClassificationApplication\_Bmesonfinal.C](https://root-forum.cern.ch/uploads/default/original/3X/d/4/d487e765e123c96b38cf0619fdf54e620cd68e3b.C) (29.3 KB)

Links: [https://drive.google.com/open?id=1bHeUDKmW74VC4dOjK2OYQkfCfjcaJS9a](https://drive.google.com/open?id=1bHeUDKmW74VC4dOjK2OYQkfCfjcaJS9a)  
[https://drive.google.com/open?id=1WOzMChyvWe-H64w-eihM0gfkkVUPpqR4](https://drive.google.com/open?id=1WOzMChyvWe-H64w-eihM0gfkkVUPpqR4)

Thanks for your attention and sorry for bothering you,  
Cheers,  
Karen

---

<div class="post-metadata">

**Author:** ![amadio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amadio/32/12562_2.png) [@amadio](https://root-forum.cern.ch/u/amadio)\
**Post date:** [August 7, 2018, 6:56am UTC](https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124/2 "2018-08-07T06:56:20Z")

</div>

> [@KarenDmn](#):
>
> Float\_t B\_J\_px2;

It looks like this is the problem. If the branch in the file is really a vector, it is not possible to use the address of a single float in `SetBranchAddress()`. You need to use an equivalent vector type in that case. Using a vector in a branch is discussed also in this other [post](https://root-forum.cern.ch/t/vector-in-branch/12796/5), if you’d like to have a look.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 7, 2018, 7:05am UTC](https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124/3 "2018-08-07T07:05:16Z")

</div>

Note that the way to post code is to enclose it with three back-quotes as described [here](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292/2).

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [August 9, 2018, 1:25pm UTC](https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124/4 "2018-08-09T13:25:01Z")

</div>

Hi Karen,

Do note that adding a vector valued variable will create one TMVA event for each entry in the vector/array. If you have 10 entries in your tree and your array leaf has 10 elements you would add in total 100 event to TMVA.

It might be the case that you want to add each entry in the vector as a separate feature. In this case you would have to do something in line with

```auto
dataloader->AddVariable("B_J_px2[0]"); // Add 1st element as separate feature
dataloader->AddVariable("B_J_px2[1]"); // Add 2nd element as separate feature
dataloader->AddVariable("B_J_px2[2]"); // Add 3rd element as separate feature

```

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![KarenDmn](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@KarenDmn](https://root-forum.cern.ch/u/KarenDmn)\
**Post date:** [August 30, 2018, 9:55am UTC](https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124/5 "2018-08-30T09:55:54Z")

</div>

Hi kialbert,

I do that you suggest in this form, I put only one vectorial branch of my tree source and I wrote the code as:

```auto
...
 dataloader->AddVariable( "nB", "nB", "units", 'i' );
 dataloader->AddVariable( "nMu", "nMu", "units", 'i' );
//Vector branch: dataloader->AddVariable( "B_px", "B_px", "units", 'F' ); 
TBranch *branch = (TBranch*)datatree->GetBranch("B_px");
Long64_t nentries = branch->GetEntries();

for(Long64_t i=0; i<= nentries - 1; i++)
  {
   dataloader->AddVariable("B_px[i]", "B_px[i]", "units", 'F' );
  }
...

```

Because of the branch has a lot of entries (~40,000), I could compile the code but in the execution, just read the input variables and says “MLP … Building network” and then it does nothing.

After, I saw the TBrowser, and when I tried the open the resulting root file out is an error:

```auto
Error in <TFile::ReadBuffer>: error reading all requested bytes from file TMVAClassification_Bmesonprueba.root, got 268 of 300
Error in <TFile::Init>: TMVAClassification_Bmesonprueba.root failed to read the file type data.

```

I tried with type Int\_t, instead of Long64\_t and neither works. What else can I do?

Really thanks for your attention

Cheers,  
Karen

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [August 30, 2018, 1:38pm UTC](https://root-forum.cern.ch/t/vector-entries-in-tmvaclassification-and-application/30124/6 "2018-08-30T13:38:08Z")

</div>

Hi,

You don’t want to add a variable per entry in the branch, rather you want to add a variable for each element in the vector. (Assuming the branch `B_px` is still a vector).

```auto
Int_t vector_size = 5;
for(Long64_t i=0; i < vector_size; ++i) {
   TString branch_name = Form("B_px[%i]", i);
   dataloader->AddVariable(branch_name, branch_name, "units", 'F' );
}

```

If the vector is of variable size we need to consider another approach. Building a classifier (especially and old one like MLP) with 40000 inputs can indeed take some time to initialise and run.

Cheers,  
Kim
