For a 1-dim spectrum, I can use TSpectrum->Search to find peaks. If the option is not “goff”, I can also draw marks on the peaks that I found. But I can only draw marks without any peak information. How can I also display the bin location of each peak on the histogram?

Another question, how can I get the histogram which is displayed in the current actively pad, no matter what is its name?

Thank you very much!

``````Int_t npeaks = 30;
Double_t fpeaks(Double_t *x, Double_t *par) {
Double_t result = par + par*x;
for (Int_t p=0;p<npeaks;p++) {
Double_t norm  = par[3*p+2];
Double_t mean  = par[3*p+3];
Double_t sigma = par[3*p+4];
result += norm*TMath::Gaus(x,mean,sigma);
}
return result;
}
void dpeaks(Int_t np=10) {
npeaks = np;
TH1F *h = new TH1F("h","test",500,0,1000);
//generate n peaks at random
Double_t par;
par = 0.8;
par = -0.6/1000;
Int_t p;
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);
f->SetNpx(1000);
f->SetParameters(par);
TCanvas *c1 = new TCanvas("c1","c1",10,10,700,700);
c1->cd(1);
h->FillRandom("f",200000);
h->Draw();
TH1F *h2 = (TH1F*)h->Clone("h2");
//Use TSpectrum to find the peak candidates
TSpectrum *s = new TSpectrum(2*npeaks);
Int_t nfound = s->Search(h,2,"");
printf("Found %d candidate peaks to fit\n",nfound);

// Draw the peaks values
Float_t *xpeaks = s->GetPositionX();
Float_t *ypeaks = s->GetPositionY();
char val;
TLatex l;
l.SetTextSize(0.02);
for (p=0;p<nfound;p++) {
Float_t xp = xpeaks[p];
Float_t yp = ypeaks[p];
sprintf(val,"%g",yp);
l.DrawLatex(xp,yp,val);
}
}``````

Look at the part “Draw the peaks values”.

To answer your 2nd question I think you should use code similar to the following one:

``````      TH2 *h2;