Aug-05-2019, 07:35 PM
Hey, all! I'm relatively new to Python; been working in ROOT mainly for HEP stuff, but I'm still coming up in my coding skills. I've been trying to transition to Python for its robustness with machine learning, but I've hit a snag when migrating some of my skills from ROOT. Specifically, I can't figure out how to fit 2D histograms.
I have a histogram that I've filled like so:
This is pretty easy to fit in ROOT with an n-degree polynomial (or a slue of other functions), but I can't find anything that points me in the right direction for doing the same in Python. In ROOT it looks like this:
hMIPvRefMult->Draw("colz");
TF1 *f = ((TF1*)(gROOT->GetFunction(Form("pol%d",p))));
f->FixParameter(0,0.);
TFitResultPtr r = hMIPvRefMult->Fit(f, "S");
TMatrixDSym cov = r->GetCovarianceMatrix();
Here, hMIPvRefMult is the 2D histogram (much like the image shown from Python, but not nearly as pretty!). I simply grab the polynomial function (here a pol%d so I can run it autonomously for many different degreed polynomials; I'd love to do that in Python, too), then fit it with the hMIPvRefMult->fit() method. I could even more easily fit with:
hMIPvRefMult->fit(pol3);
I'm thinking there has to be some sort of Python analogue with respect to 2D histogram fitting since everything else ROOT does has a similar, but simpler and cleaner, method in Python. So, any ideas as to where I could go to learn this in Python? All the histogram fitting tutorials I've seen are essentially fitting 1D histograms with gaussians; it's nice, but not the functionality I need.
Thanks!
I have a histogram that I've filled like so:
plt.hist2d(mipVref[0], mipVref[1], bins=[ 100, 100], cmap=plt.cm.get_cmap("hot"))The x's and y's are data from 2 detectors for the same events, and the result looks to be a third degree polynomial (ish; this is obviously somewhat subjective due to the data smear which is expected from fluctuations):
This is pretty easy to fit in ROOT with an n-degree polynomial (or a slue of other functions), but I can't find anything that points me in the right direction for doing the same in Python. In ROOT it looks like this:
hMIPvRefMult->Draw("colz");
TF1 *f = ((TF1*)(gROOT->GetFunction(Form("pol%d",p))));
f->FixParameter(0,0.);
TFitResultPtr r = hMIPvRefMult->Fit(f, "S");
TMatrixDSym cov = r->GetCovarianceMatrix();
Here, hMIPvRefMult is the 2D histogram (much like the image shown from Python, but not nearly as pretty!). I simply grab the polynomial function (here a pol%d so I can run it autonomously for many different degreed polynomials; I'd love to do that in Python, too), then fit it with the hMIPvRefMult->fit() method. I could even more easily fit with:
hMIPvRefMult->fit(pol3);
I'm thinking there has to be some sort of Python analogue with respect to 2D histogram fitting since everything else ROOT does has a similar, but simpler and cleaner, method in Python. So, any ideas as to where I could go to learn this in Python? All the histogram fitting tutorials I've seen are essentially fitting 1D histograms with gaussians; it's nice, but not the functionality I need.
Thanks!