Python Forum
How to fit a 2D histogram like in ROOT?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fit a 2D histogram like in ROOT?
#1
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:
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):
[Image: IFq3ytLNgQFsRCCc4Xuc7n41wSRd7bcVns9hsNfF...peArA=s800]
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!
Reply
#2
(Aug-05-2019, 07:35 PM)Zandar Wrote: Specifically, I can't figure out how to fit 2D histograms.
Is this about computing probability density estimations, e.g. using kernel approaches?


Consider seaborn package. Also, take a look at Scipy. These functions can be used to get smooth pdf-estimations by sample data.
Reply
#3
(Aug-05-2019, 11:54 PM)scidam Wrote:
(Aug-05-2019, 07:35 PM)Zandar Wrote: Specifically, I can't figure out how to fit 2D histograms.
Is this about computing probability density estimations, e.g. using kernel approaches?


Consider seaborn package. Also, take a look at Scipy. These functions can be used to get smooth pdf-estimations by sample data.

Thanks for the reply! It's about getting parameters in order to normalise the distributions to each other; so, for the above graph, running the x-axis parameter (nMIP) through an equation in order to make the graph a straight line. For instance, here's a plot I made doing that with a Keras/TensorFlow machine learning algorithm (very simple one; I'm still learning):

[Image: XLiyRLD2r0z6Rs9apUtT2yUUwJNGkWp-asbyhiNr...97-h953-no]

I've also used Seaborn a bit for plotting, but I'm having trouble using it for the functionality mentioned.

I'll show what I'm trying to do with ROOT plots for clarity. Here's a plot of the distribution, fitted with an order 3 polynomial:

[Image: BQnkVSQvT5gRs1pvGZ09GBvyO0n-JWoiTA7rviBl...41-h639-no]

I can get the parameters of the polynomial fit and then simply redraw the histogram, but instead of nMIP I use p0+p1*nMIP+p2*nMIP^2+p3*nMIP^3, which gives this:

[Image: xUeR3Gci7BuKshSPGwkwidRJHIafa_zVA8EiIb_Y...33-h638-no]

The fit is done on the first histogram as outlined in the OP. I've played around with Seaborn a bit and it seems like a very powerful tool (as well as scipy curve_fit), but I can't find out how to use them for fitting 2D histograms. I can pull out information, which is I think what you're referring to with probability density estimations, but I can't actually fit to a function like this. It's very possible that I simply don't know how to use kernel approaches to do this (I'm not familiar with that concept; I've done a ton of math so far for this degree path, but not a lot of coding).

Thanks for the feedback!

And just because we all like pretty plots, here's using seaborn to plot the ML graph as a kde with jet colours:

[Image: 9-EGb20go1X1NRpPtJ30WT9skL60Sv4lpeUHPTlE...00-h796-no]

Python really is a step up in display and simplicity vs ROOT.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  histogram error alyssaantarctica 1 3,714 Jul-09-2018, 10:46 PM
Last Post: Larz60+
  Plot Probability Density of an Histogram Python_TLS 1 2,853 Jun-28-2018, 06:19 AM
Last Post: scidam
  histogram with matplotlib vaugirard 10 6,154 Jun-01-2018, 04:13 AM
Last Post: vaugirard
  Help creating a histogram using an array rubadub 0 2,160 Apr-17-2018, 08:57 PM
Last Post: rubadub
  Histogram help dandatadan 1 2,513 Nov-25-2017, 06:07 AM
Last Post: heiner55
  mcerp: error while plotting a histogram rakhmadiev 2 4,477 Apr-02-2017, 09:08 PM
Last Post: rakhmadiev
  plotting histogram vvv 1 3,261 Mar-10-2017, 11:47 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020