Fitting a TH3F with a linear fit

Hi everyone!
I have been working with ROOT for a while now, but now I have run into a problem that I simply can not figure out. Here is the basic setup, I have 16 points which represent different pixels in our experiment, which are arranged in a 4x4 grid pattern in 3D space. Each pixel is then given a 3D coordinate (x,y,z) and a weight based on data collected in our experiment using TH3F::Fill(x,y,z,w). Then the resulting points then need to be fitted using a 3D linear fit. I have tried using the 3D line fit tutorial from the tutorials page, but it uses TGraph2D, so as far as I know there is no way to “weigh” the points according to a custom function with TGraph2D. The most promising method I have tried thus far involves using TProfiles, using each profile as a “projection” of the data onto the xy,xz and yz planes, trying to fit the individual profiles and then combining the individual 2D fits to create a 3D fit. However; this doesn’t seem to work since it seems as though TProfile overwrites the individual weights I assign in the beginning and then works out its own “errors” for each point thus making the fit unreliable. Any help or suggestions in this matter would be greatly appreciated. Thanks in advance for any help you can provide.

Hi,

you should modify the tutorials/line3Dfit.C to use as x,y,z the bin center of the histogram and weight each distance square using a weight w equal to the inverse of the bin content. If you have variable bins in your histogram you would need to add also the bin volume in the weight.

Best Regards

Lorenzo

Thanks a lot for the speedy reply,
I attempted to implement the solution you suggested with some success. As a test for the new program I decided to only marginally weigh all points except for 1 and then weigh the “exception” heavily (see attached .C file). Wouldn’t this mean that the fitted line should then only go through that one point? However; when I do this the fitted curve goes through no points (see .pdf file). Is this to be expected, or did I do something wrong in the implementation? Also, as I was programming this a question was raised in my mind. In the future, when I calculate the weights in the main function, how can I then pass them to the SumDistance2 function? I don’t see how one can pass this sort of parameter to the SumDistance2 function, since the min->SetFCN(SumDistance2) line only accepts a function with 5 arguments. I don’t want to have to recalculate the weights anew every time I implement the function to be minimized, the weights should just stay constant. Thanks again for your help.

Alexander
linefit.pdf (15.1 KB)
line3Dfitwithweight.C (4.82 KB)

Hi,

it looks to me you did not implement correctly in :

d = (1/data[row][column]*data[row][column])*distance2.....

Here you divide and then multiply so it is equivalent as not applying the weight. Also if your weight is the multiplicative value you apply to data. So you should multiply distance2 with the number of data in your bins.
More data you have, you should apply a larger weight.

For your second question, in the tutorial is used the TVirtualFitter. In this case you need to pass a static function, so if you need to pass extra parameters, you need to make them globals.
Otherwise, you can change the example to use the Minimizer interface (e.g. see tutorial exampleMinimize.C)
In this case you can pass an object which can provide all the needed data.

Best Regards

Lorenzo

Hi again,
I managed to fix the problems and now the program is working as it should. Thanks a lot for you help, if I ever have any other problems I can’t solve in ROOT I know where to go. Thanks again!

Alexander

Dear ROOTers,
It has been a while, but now I have run into some problems with a slightly modified version of my previous program. First, when running the problem with some very simple test data (all points weighted=0 and only the 4 center points weighted =1), the fitter gives me a fit where x0,y0 seem to in the right place (near the origin), but with huge errors (see below). Is there any way I can minimize these errors? Secondly, when I try to draw the TGraph2D, suddenly all these min-max scope errors come up (also see below). I have traced the issue to the fact that all the points that are to be fitted now lie on the xy-plane (z=0), how does this affect the plotter? Thanks for your help again.

Alexander

FCN=5.76 FROM MIGRAD STATUS=CONVERGED 123 CALLS 124 TOTAL
EDM=7.83694e-07 STRATEGY= 1 ERR MATRIX NOT POS-DEF
EXT PARAMETER APPROXIMATE PHYSICAL LIMITS
NO. NAME VALUE ERROR NEGATIVE POSITIVE
1 x0 4.02588e-08 2.87778e+00 -3.60000e+00 3.60000e+00
2 Ax 1.28092e+03 1.65362e+07
3 y0 4.02588e-08 2.87778e+00 -3.60000e+00 3.60000e+00
4 Ay 1.28092e+03 1.65362e+07
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
Error in TView3D::ResetView: Error in min-max scope
Error in TView3D::ResetView: Error in min-max scope
Error in TView3D::ResetView: Error in min-max scope
Error in TView3D::SetRange: problem setting view
Error in TView3D::ResetView: Error in min-max scope
Warning in TCanvas::ResizePad: c1 width changed from 64000 to 10

Warning in TCanvas::ResizePad: c1 height changed from 64000 to 10

root [1] Warning in TCanvas::ResizePad: c1 width changed from 64000 to 10

Warning in TCanvas::ResizePad: c1 height changed from 64000 to 10

Warning in TCanvas::ResizePad: c1 width changed from 64000 to 10

Warning in TCanvas::ResizePad: c1 height changed from 64000 to 10
line3Dfitwithweight.C (3.6 KB)

Hi,

Could you repost this question in a new post in the Math and Stat forum?

Thanks,
Philippe.

No problem, I have moved the post over to the other forum. Thanks.

Alexander