How to display xPosition of a fitted peak on the pad?

_ROOT Version: 5.34/09
_Platform: Linux 3.11.10-100.fc18.x86_64
_Compiler: gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)

I am learning to fit peaks in a gamma-ray spectrum obtained with a high-purity germanium detector. I am successfully able to get reasonable fits to several peaks. A code “peaks.C” was of great help in this.

My question now is - How to display the positions (X-coordinate) of the fitted peaks on the Pad itself, just above the peak?

Thanking you.


If x is the x position of the peaks and y the height the the peak do

TText *t = new TText(x,y,Form("%g",x));

Dear Couet,

Thank you very much for your suggestion. I tried it, but it is not working. Please find attached code. You will see that I am trying the same thing in two different ways, but neither of them is working.

Please help.



pfit.C (2.7 KB)

How should I call fit ? what are the inout parameters ?

I compiled it with

.L pfit.C++

Followed by


where “hist_name” is histogram name and
xLo, xHi defines window to search peaks for fitting.

Ok but what should be the value of the input parameters … ? I 'd like to run your macro to see what’s wrong

If you have a root file which contains some histograms then you can first load that file first and then do the following:


assuming that there is atleast one peak between channels 300 and 500 in histogram “hist_name”.

Dear Couet,

Below is the log of the command which I used to run the macro.
root [0] TFile *f = new TFile(“hist.root”);
root [1] .ls
root [2] .L pfit.C++
root [3] pfit(“Addbackspectra00”,450,750);

I am attaching the macro and “hist.root” for your reference, which contains one histogram. You will see that the macro writes “Ajay” at the desired place but NOT the X-coordinates of the fitted peaks.

Please help.


pfit.C (2.7 KB)

hist.root (9.6 KB)

The h2->Fit("fit"); call clears the pad and draws everything anew so, any previous TText is gone (note: you can see the “Ajay” TText because it is drawn afterwards so, draw your another “add-ons” after the fit, too).

Okay! Then what is the solution to my original query?

Yes, it is! I have just solved it. Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.