#include "Riostream.h" #include "TFile.h" #include "TH1.h" #include "TNtuple.h" #include "TF1.h" #include "TSpectrum.h" void fithisto() { Int_t i, npeaks = 5; TGraph *g = new TGraph("eu_gu_1.txt"); // g->Sort(); // just a precaution Int_t n = g->GetN();//get the no. of points on the TGraph *g Double_t source[n]; Double_t *x = new Double_t[(n + 1)]; x[0] = (3.0 * g->GetX()[0] - g->GetX()[1]) / 2.0; x[n] = (3.0 * g->GetX()[(n - 1)] - g->GetX()[(n - 2)]) / 2.0; for (Int_t i = 1; i < n; i++) { x[i] = (g->GetX()[(i - 1)] + g->GetX()[i]) / 2.0; } TH1D *h = new TH1D("h", "spectrum;x-value(Channels);#counts", n, x); h->FillN(n, g->GetX(), g->GetY()); h->Sumw2(kFALSE); // restore proper errors delete [] x; // no longer needed delete g; // no longer needed TF1 *f1 = new TF1("f1", "gausn(0) + pol1(3)", 400., 435.); // linear background f1->SetParameters(2800., 415., 2., 0., 0.); h->Fit("f1", "LBR+"); TF1 *f2 = new TF1("f2", "gausn(0) + pol1(3)", 830., 846.); // linear background f2->SetParameters(1000., 838., 2., 0., 0.); h->Fit("f2", "LBR+"); TF1 *f3 = new TF1("f3", "gausn(0) + pol1(3)", 1160., 1200.); f3->SetParameters(2700., 1178., 2., 0., 0.); h->Fit("f3", "LBR+"); TF1 *f4 = new TF1("f4", "gausn(0) + pol1(3)", 1512., 1526.); // clean peak, no background f4->SetParameters(280., 1519., 3., 0., 0.); h->Fit("f4", "LBR+"); TF1 *f5 = new TF1("f5", "gausn(0) + pol1(3)", 2654., 2674.); // clean peak, no background f5->SetParameters(580., 2664., 3., 0., 0.); h->Fit("f5", "LBR+"); TF1 *f6 = new TF1("f6", "gausn(0) + pol1(3)", 2954., 2978.); // clean peak, no background f6->SetParameters(200., 2966., 3., 0., 0.); h->Fit("f6", "LBR+"); TF1 *f7 = new TF1("f7", "gausn(0) + pol1(3)", 3280., 3316.); // clean peak, no background f7->SetParameters(540., 3296., 3., 0., 0.); h->Fit("f7", "LBR+"); TF1 *f8 = new TF1("f8", "gausn(0) + pol1(3)", 3790., 3820.); // clean peak, no background f8->SetParameters(420., 3805., 3., 0., 0.); h->Fit("f8", "LBR+"); TF1 *f9 = new TF1("f9", "gausn", 4795., 4835.); // clean peak, no background f9->SetParameters(500., 4815., 3.); h->Fit("f9", "LBR+"); // e.g. "BR+" or "LBR+" //Area1 std::cout << h->Integral(h->FindFixBin(410.), h->FindFixBin(422.)) << std::endl; std::cout << h->Integral(h->FindFixBin(831.), h->FindFixBin(843.)) << std::endl; std::cout << h->Integral(h->FindFixBin(1172.), h->FindFixBin(1184.)) << std::endl; std::cout << h->Integral(h->FindFixBin(1512.), h->FindFixBin(1526.)) << std::endl; std::cout << h->Integral(h->FindFixBin(2656.), h->FindFixBin(2673.)) << std::endl; std::cout << h->Integral(h->FindFixBin(2958.), h->FindFixBin(2976.)) << std::endl; std::cout << h->Integral(h->FindFixBin(3288.), h->FindFixBin(3307.)) << std::endl; std::cout << h->Integral(h->FindFixBin(3794.), h->FindFixBin(3814.)) << std::endl; std::cout << h->Integral(h->FindFixBin(4804.), h->FindFixBin(4826.)) << std::endl; TSpectrum *s = new TSpectrum(2*npeaks); Int_t nfound = s->Search(h,2,"",0.06); printf("Found %d candidate peaks to fit\n",nfound); s->Background(h->GetArray() + 1, n, 10, TSpectrum::kBackDecreasingWindow, TSpectrum::kBackOrder8, kTRUE, TSpectrum::kBackSmoothing15, kTRUE); //h->SetLineColor(kBlack); // h->Draw(""); //gPad->SetLogy(1); //h->SetMaximum(3000.);//along Y //h->SetAxisRange(0., 150.,"Y"); }