Getting Contours From TH2D.
Illustrates how to find peaks in histograms.
This script generates a random number of gaussian peaks on top of a linear background. The position of the peaks is found via TSpectrum and injected as initial values of parameters to make a global fit. The background is computed and drawn on top of the original histogram.
To execute this example, do:
root > .x peaks.C (
generate 10 peaks by
default)
root > .x peaks.C++ (use the compiler)
root > .x peaks.C++(30) (generates 30 peaks)
To execute only the first part of the script (without fitting) specify a negative value for the number of peaks, eg
for (
Int_t p=0;p<npeaks;p++) {
}
}
void peaks(
Int_t np=10) {
TH1F *h =
new TH1F(
"h",
"test",500,0,1000);
par[0] = 0.8;
par[1] = -0.6/1000;
for (p=0;p<npeaks;p++) {
par[3*p+2] = 1;
par[3*p+3] = 10+gRandom->Rndm()*980;
par[3*p+4] = 3+2*gRandom->Rndm();
}
TF1 *f =
new TF1(
"f",fpeaks,0,1000,2+3*npeaks);
printf("Found %d candidate peaks to fit\n",nfound);
if (np <0) return;
TF1 *fline =
new TF1(
"fline",
"pol1",0,1000);
npeaks = 0;
for (p=0;p<nfound;p++) {
par[3*npeaks+2] = yp;
par[3*npeaks+3] = xp;
par[3*npeaks+4] = 3;
npeaks++;
}
printf("Found %d useful peaks to fit\n",npeaks);
printf("Now fitting: Be patient\n");
TF1 *fit =
new TF1(
"fit",fpeaks,0,1000,2+3*npeaks);
}
- Author
- Rene Brun
Definition in file peaks.C.