#include "TFile.h" #include "TROOT.h" #include "TH1.h" #include "TF1.h" #include "TSpectrum.h" #include "TCanvas.h" #include "TMath.h" void Ajay() { TFile *f = TFile::Open("eff_6.root", "READ"); //Open a root file if ((!f) || f->IsZombie()) { std::cout << "Failed to open eff_6.root!" << std::endl; return; } TH1F *h2 = (TH1F*)f->Get("h")->Clone(); TSpectrum *s = new TSpectrum(); // Define ā€œsā€ TH1 *hb = s->Background(h2,20,"Compton"); // Generate background h2->Add(hb,-1); // Subtract background h2->SetLineColor(kBlack); // Set different line color h2->Draw(); // Draw background Int_t nfound = s->Search(h2,2,"",0.15); printf("Found %d candidate peaks to fit\n",nfound); f->Get("h")->Draw("same"); hb->Draw("same"); // Draw background over it Double_t *xpeaks = s->GetPositionX(); Double_t *epeaks = new Double_t [8]{121.78, 244.69, 344.27, 778.90, 964.13, 1085.869, 1112.11, 1408.01}; std::sort(xpeaks, xpeaks + 8); TGraph *g = new TGraph(nfound,&(xpeaks[0]),&(epeaks[0])); TF1 *f1 = new TF1("f1","pol2",1,3); f1->SetParameters(3, 0.3, 3); g->SetMarkerStyle(3); g->SetTitle("Energy(kev) Vs Channel;Channel;Energy"); g->Fit("f1","Q"); g->Draw("AP"); gStyle->SetOptFit(1111); }