# 4D interpolation

**URL:** <https://root-forum.cern.ch/t/4d-interpolation/20572>\
**Category:** TMVA\
**Created:** [February 8, 2016, 9:26am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572 "2016-02-08T09:26:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 8, 2016, 9:26am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/1 "2016-02-08T09:26:53Z")

</div>

Hi,

I have an array of structures with four variables (x,y,z,v), every combination of these four variables define the center of a box and define two more values f(x,y,z,v) -\>E\_1 and E\_2. I have a lot of boxes (combinations). I’m creating random numbers inside the boxes to fill all the space, and I need a 4d interpolation routine. I mean, for every point (x\_i,y\_i,z\_i,v\_i) I need to find out the value of the function in that point f(xi,yi,zi,vi)-\>E\_1\_i,E\_2\_i  
For the moment, I’m associating to each box the values at the center of the box, so the function takes the same values in all the box. I know this is wrong and I was trying to improve it using a 4D interpolation.

When I did this in two dimensions I’ve created my interpolation routine, but now that the problem is more complex I was trying to find if there was something already available. I found the TMultiDimFit class, but I’m not sure if this is what I need or how to use it with my specific case.

If you need more details, I can provide them.

Thanks

---

<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:** [February 8, 2016, 9:38am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/2 "2016-02-08T09:38:45Z")

</div>

Hi,

Are your points distributed in a regular grid, so each box has the same volume or not ?  
If they are regular it is easier to do it.  
I would not use TultiDim fit but you can use several methods in TMVA to perform regression, such as an artificial neural network. I guess they will perform better.

It seems to me however that you have two target values, and in such case we don’t have any method available in ROOT. Maybe it exists something in R, that you can use via the ROOT-R interface

Best Regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 8, 2016, 9:54am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/3 "2016-02-08T09:54:55Z")

</div>

The boxes don’t have the same size. The step in x(+2), y(+2), and z(+20) is constant. v has not always the same step (most of the times is 20, but from time to time takes a different value). Also, the boxes in the edges are half the size that they will be if they were not at the edges. So, they are not regular.

I believe having to target values is not a problem because one does not influence the other. So I think this could be done in two steps like if every x,y,z,v provide one value first and then the other.

---

<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:** [February 8, 2016, 10:01am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/4 "2016-02-08T10:01:09Z")

</div>

Hi,

If the two target are independent, then you can use any of the regression method of TMVA. See the tutorial example tutorials/tmva/TMVARegression.C

Lorenzo

---

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 8, 2016, 10:08am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/5 "2016-02-08T10:08:26Z")

</div>

Ok, I will look into that. I will let you know how it goes.

Thanks!!

---

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 8, 2016, 12:19pm UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/6 "2016-02-08T12:19:36Z")

</div>

Hi again!

I have a few questions regarding how to implement this.

1. My data are in a txt which I read and storage into a structure. So, I have something like this info[i].x, info[i].y, info[i].z and info[i].v. These are the variables I have to add to factory, right? How should I include this? where I include the value of the function for the points included on the file.

2. I need to read the data from an ascii file, I had a look at TMVAClassification . I should include something like this, isn’t it?

3. I don’t need to include a background, so I guess I just ignore that part, right?

4. Do I need cuts?

5. Now is time to choose the method, to train, to test and evaluate. The output is storage in a root file then, there I guess you can see the full interpolation function. So, I guess I will have to evaluate that function in a point and then it will return a value.

Thank you so much

---

<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:** [February 8, 2016, 2:18pm UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/7 "2016-02-08T14:18:15Z")

</div>

Hi again,

First you should look at TMVARegression.C and not TMVAClassification.C Classification is to distinguish signal from background, while regression is to estimate a function value (your problem).

Yes, x, y, z and v are the variables you should give to the Factory and they are the corresponding branches in your input tree.  
You can give as input a text file or you can always create a ROOT TTree from a text file too.

For the regression you don;t need the background, but you need to provide the target variables (E1 or E2 in your case). this is done using the AddTarget() method.

The output of the training phase is basically a function that can be evaluated for every point given by the user.  
You can see how the evaluation works in the TMVARegressionApplication.C macro

Lorenzo

---

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 9, 2016, 7:33am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/8 "2016-02-09T07:33:16Z")

</div>

I did what you suggested and now the macro is running but I have an error which says:

`Processing tmva.C...
--- Factory : You are running ROOT Version: 6.04/02, Jul 14, 2015
--- Factory : 
--- Factory : _/_/_/_/_/ _| _| _| _| _|_|   
--- Factory : _/ _|_| _|_| _| _| _| _| 
--- Factory : _/ _| _| _| _| _| _|_|_|_| 
--- Factory : _/ _| _| _| _| _| _| 
--- Factory : _/ _| _| _| _| _| 
--- Factory : 
--- Factory : ___________ TMVA Version 4.2.1, Feb 5, 2015
--- Factory : 
--- DataSetInfo : Added class "Signal" with internal class number 0
--- Factory : Add Tree dist_tree of type Signal with 692686 events
--- TMVARegression : Using input file: data.root
--- DataSetInfo : Class index : 0 name : Signal
--- Factory : Booking method: KNN
--- <WARNING> Factory : Method KNN is not capable of handling classification with 1 classes.
--- <FATAL> Factory : You want to do classification training, but specified less than two classes.
***> abort program execution`

I guess I’m missing something in my macro. Any hint?

Thanks!

---

<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:** [February 9, 2016, 10:12am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/9 "2016-02-09T10:12:24Z")

</div>

Hi,

It looks to me you are still configuring the Factory for Classification instead of Regression. Can you please upload your macro

Thank you

Lorenzo

---

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 9, 2016, 10:25am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/10 "2016-02-09T10:25:27Z")

</div>

[code] // Create a new root output file  
TString outfileName( “TMVAReg.root” );  
TFile\* outputFile = TFile::Open( outfileName, “RECREATE” );

```
	// Create the factory object. Later you can choose the methods
	// whose performance you'd like to investigate. The factory will
// then run the performance analysis for you.
//
 	// The first argument is the base of the name of all the
 	// weightfiles in the directory weight/ 
 	//
 	// The second argument is the output file for the training results
 	// All TMVA output can be suppressed by removing the "!" (not) in 
 	// front of the "Silent" argument in the option string

  	TMVA::Factory *factory = new TMVA::Factory( "TMVARegression", outputFile, "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );

  	// Read training and test data (see TMVAClassification for reading ASCII files)
  	// load the signal and background event samples from ROOT trees
auto signalFile = TFile::Open("data.root","READ");
auto signalTree = static_cast<TTree*>(signalFile->Get("dist_tree"));

factory->AddSignalTree(signalTree,1.0);

// Define the input variables that shall be used for the MVA training
factory->AddVariable( "theta_p", "Polar angle proton", "degrees", 'D' );
factory->AddVariable( "theta_n", "Polar angle neutron", "degrees", 'D' );
factory->AddVariable( "delta_phi", "Difference in azimuthal angle", "degrees", 'D' );
factory->AddVariable( "S", "Kinematic variable", "degrees", 'D' );

// Add the variable carrying the regression target
factory->AddTarget( "E_p" ); 

  	std::cout << "--- TMVARegression : Using input file: " << signalFile->GetName() << std::endl;

  	// --- Register the regression tree
  //	TTree *regTree = (TTree*)input->Get("TreeR");

// global event weights per tree (see below for setting event-wise weights)
 	Double_t regWeight = 1.0;

// Apply additional cuts on the signal and background samples (can be different)
TCut mycut = ""; // for example: TCut mycut = "abs(var1)<0.5 && abs(var2-0.5)<1";

// tell the factory to use all remaining events in the trees after training for testing:
  	factory->PrepareTrainingAndTestTree( mycut, "nTrain_Regression=1000:nTest_Regression=0:SplitMode=Random:NormMode=NumEvents:!V" );
  	// factory->PrepareTrainingAndTestTree( mycut, 
  	// "nTrain_Regression=0:nTest_Regression=0:SplitMode=Random:NormMode=NumEvents:!V" );

  	// If no numbers of events are given, half of the events in the tree are used 
  	// for training, and the other half for testing:
  	// factory->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );  
    
	// ---- Book MVA methods
  	//
  	// please lookup the various method configuration options in the corresponding cxx files, eg:
  	// src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
  	// it is possible to preset ranges in the option string in which the cut optimisation should be done:
  	// "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable

  	// K-Nearest Neighbour classifier (KNN)

factory->BookMethod( TMVA::Types::kKNN, "KNN", "nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" );
  
// ---- Now you can tell the factory to train, test, and evaluate the MVAs

  	// Train MVAs using the set of training events
  	factory->TrainAllMethods();

  	// ---- Evaluate all MVAs using the set of test events
  	factory->TestAllMethods();

  	// ----- Evaluate and compare performance of all configured MVAs
  	factory->EvaluateAllMethods();    

  	// --------------------------------------------------------------
  
  	// Save the output
  	outputFile->Close();

  	std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
  	std::cout << "==> TMVARegression is done!" << std::endl;      

  	delete factory;

```

[/code]

---

<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:** [February 9, 2016, 11:14am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/11 "2016-02-09T11:14:40Z")

</div>

Hi,

You should use AddRegressionTree and not AddSignalTree. Please follow the TMVARegression macro and not TMVAClassification

Lorenzo

---

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 9, 2016, 11:37am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/12 "2016-02-09T11:37:38Z")

</div>

Working now!! Thank you so much!! Now, I have to study the output and the different methods, but it is running!

---

<div class="post-metadata">

**Author:** ![palomilla](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@palomilla](https://root-forum.cern.ch/u/palomilla)\
**Post date:** [February 12, 2016, 11:36am UTC](https://root-forum.cern.ch/t/4d-interpolation/20572/13 "2016-02-12T11:36:32Z")

</div>

Hi again!

Everything seems to be working properly, but I’m not able to find the function which depends on my four variables. I run something like TMVARegression and TMVARegressionApplication adapted to my variables.

Any hint about what I’m doing wrong?

I tried to use as well the macros on page 32 of the manual I get tons of complaints about TMVAGlob.C, so I copied the needed parts of this macro inside the macros on page 32.

Regards,

Paloma
