#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", "gaus(0) + pol1(3)", 400., 435.); // linear background f1->SetParameters(2800., 415., 2., 0., 0.); h->Fit("f1", "BR+"); TF1 *f2 = new TF1("f2", "gaus(0) + pol1(3)", 830., 846.); // linear background f2->SetParameters(1000., 838., 2., 0., 0.); h->Fit("f2", "BR+"); TF1 *f3 = new TF1("f3", "gaus(0) + pol1(3)", 1160., 1200.); f3->SetParameters(2700., 1178., 2., 0., 0.); h->Fit("f3", "BR+"); TF1 *f4 = new TF1("f4", "gaus(0) + pol1(3)", 1512., 1526.); // clean peak, no background f4->SetParameters(280., 1519., 3., 0., 0.); h->Fit("f4", "BR+"); TF1 *f5 = new TF1("f5", "gaus(0) + pol1(3)", 2654., 2674.); // clean peak, no background f5->SetParameters(580., 2664., 3., 0., 0.); h->Fit("f5", "BR+"); TF1 *f6 = new TF1("f6", "gaus(0) + pol1(3)", 2954., 2978.); // clean peak, no background f6->SetParameters(200., 2966., 3., 0., 0.); h->Fit("f6", "BR+"); TF1 *f7 = new TF1("f7", "gaus(0) + pol1(3)", 3280., 3316.); // clean peak, no background f7->SetParameters(540., 3296., 3., 0., 0.); h->Fit("f7", "BR+"); TF1 *f8 = new TF1("f8", "gaus(0) + pol1(3)", 3790., 3820.); // clean peak, no background f8->SetParameters(420., 3805., 3., 0., 0.); h->Fit("f8", "BR+"); TF1 *f9 = new TF1("f9", "gaus", 4795., 4835.); // clean peak, no background f9->SetParameters(500., 4815., 3.); h->Fit("f9", "BR+"); // e.g. "BR+" or "LBR+" //TSpectrum *s = new TSpectrum(2*npeaks); //Int_t nfound = s->Search(h,2,"",0.06); // printf("Found %d candidate peaks to fit\n",nfound); /*ifstream in; in.open("eu_gu_1.txt"); auto f = TFile::Open("basic.root","RECREATE"); TH1F *back = (TH1F*) f->Get("back1"); back->SetTitle("Estimation of background with decreasing window"); back->GetXaxis()->SetRange(1,n); back->Draw("L");*/ /*TSpectrum *s = new TSpectrum(); for (i = 0; i < n; i++) source[i]=back->GetBinContent(i + 1); // Estimate the background s->Background(source,n,6,TSpectrum::kBackDecreasingWindow, TSpectrum::kBackOrder2,kFALSE, TSpectrum::kBackSmoothing3,kFALSE); // Draw the estimated background for (i = 0; i < n; i++) h->SetBinContent(i + 1,source[i]); h->SetLineColor(kRed); h->Draw("SAME L"); in.close(); f->Write();*/ TSpectrum *s = new TSpectrum(); s->Background(h->GetArray() + 1, n, 10, TSpectrum::kBackDecreasingWindow, TSpectrum::kBackOrder8, kTRUE, TSpectrum::kBackSmoothing5, kTRUE); h->Draw("AL"); //Area 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; } //Estimate background using TSpectrum::Background //TH1 *hb = s->Background(h,20,"same"); // char *Background(s, n, 20); /*TGraph *g1 = new TGraph("eu_gu_1.txt"); s->Background(g1->GetY(), g1->GetN(), 10, TSpectrum::kBackDecreasingWindow, TSpectrum::kBackOrder8, kTRUE, TSpectrum::kBackSmoothing5, kTRUE); g1->Draw("AL"); for (i = 0; i < n; i++) h->SetBinContent(i + 1,source[i]); h->SetLineColor(kRed); h->SetMaximum(1000.);//along Y h->SetMinimum(0.); h->Draw("SAME L");*/