# Transforming TTree with many variables into a 2D image to use as input for a CNN?

**URL:** <https://root-forum.cern.ch/t/transforming-ttree-with-many-variables-into-a-2d-image-to-use-as-input-for-a-cnn/56873>\
**Category:** TMVA\
**Created:** [October 26, 2023, 2:31pm UTC](https://root-forum.cern.ch/t/transforming-ttree-with-many-variables-into-a-2d-image-to-use-as-input-for-a-cnn/56873 "2023-10-26T14:31:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abayyari](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abayyari/32/35034_2.png) [@abayyari](https://root-forum.cern.ch/u/abayyari)\
**Post date:** [October 26, 2023, 2:31pm UTC](https://root-forum.cern.ch/t/transforming-ttree-with-many-variables-into-a-2d-image-to-use-as-input-for-a-cnn/56873/1 "2023-10-26T14:31:15Z")

</div>

I have a root file that has 360 variables in the TTree. Each variable outputs a 1 or 0 to represent an on or off pixel (18 variables for each row, with a total of 20 rows). Now I want to transform these into a 2D image to feed into the TMVA\_CNN\_Classification code. How do I properly do that? The example root file is already in an image format, but I’m not sure how to transform my TTree in such a way. Could I simply load each variable one by one in the correct order, and the code knows that it’s taking it in as an image? Such as:

loader.AddVariable(“var1”,“var1”‘I’)  
…  
loader.AddVariable(“var360”,“var360”‘I’)

loader.PrepareTrainingAndTestTree(  
mycuts,  
mycutb,  
nTrain\_Signal=nTrainSig,  
nTrain\_Background=nTrainBkg,  
SplitMode=“Random”,  
SplitSeed=100,  
NormMode=“NumEvents”,  
V=False,  
CalcCorrelations=False,  
)

model = Sequential()  
model.add(Reshape((20, 18, 1), input\_shape=(360,)))  
… etc.

Or do I have to do another step in between? Thank you so much.

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [October 26, 2023, 3:01pm UTC](https://root-forum.cern.ch/t/transforming-ttree-with-many-variables-into-a-2d-image-to-use-as-input-for-a-cnn/56873/2 "2023-10-26T15:01:50Z")

</div>

Hi @abayyari,

thank you for the question. Maybe @moneta could help?

Cheers,  
Marta

---

<div class="post-metadata">

**Author:** ![abayyari](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abayyari/32/35034_2.png) [@abayyari](https://root-forum.cern.ch/u/abayyari)\
**Post date:** [October 26, 2023, 6:38pm UTC](https://root-forum.cern.ch/t/transforming-ttree-with-many-variables-into-a-2d-image-to-use-as-input-for-a-cnn/56873/3 "2023-10-26T18:38:40Z")

</div>

Thanks for your suggestion, I think it worked and gave good results based off of my ROC plot, but want to be sure it was input correctly.

 ![ROC](https://root-forum.cern.ch/uploads/default/original/3X/9/1/91aeff9288daee96db735127ace34314c4400b45.jpeg)

---

<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:** [January 9, 2024, 10:22am UTC](https://root-forum.cern.ch/t/transforming-ttree-with-many-variables-into-a-2d-image-to-use-as-input-for-a-cnn/56873/4 "2024-01-09T10:22:36Z")

</div>

Hi,  
Yes you can add teh variables one by one as you are doing or you can add directly an array using `DataLoader::AddVariableArray` as in [this tutorial](https://root.cern.ch/doc/master/TMVA __CNN__ Classification_8C.html).

Best regards

Lorenzo
