Energy calibration of gamma ray-detector

The energy calibration curve for Ge or Scintillation detector is usually described by the following 2 degrees polynomial function:

//suppose you have five gamma peaks for the calibration.

Float_t energy[5]={121.78, 244.7, 344.28, 411.12, 443.96};//fill with your 152Eu gamma peak energies
Float_t channel[5]={100,200,300,400,500};//fill with your channel numbers from gamma peak fitting
TGraph *g=new TGraph;
g->SetPoint(5, channel, energy);//fill data to a Graph
g->Fit("pol2");//fit the graph with pol2

then calibrate your spectrum with the parameters obtained above

void calibration(TH1D *h0)
{
    double p0=0;//fill with your fitted parameter values
    double p1=1;//
    double p2=0;//
// TH1D *h0 - raw gamma spectrum
    TH1D *g0=new TH1D("g0","g0",2500,0,2500);//g0 - calibrated spectrum
    TRandom3 *r=new TRandom3(0);
    for(int i=0;i<2500;i++){
        Long64_t eN=h0->GetBinContent(i);
        Double_t e=h0->GetBinCenter(i);
        for(Long64_t j=0;j<eN;j++){
            Double_t ea=e+r->Rndm()*0.2-0.1;
            ea=p2*ea*ea+p1*ea+p0;
            g0->Fill(ea);
        }
        if(i%1000==0) cout<<i<<endl;
    }
    TFile *f=new TFile("gammacali.root","recreate");
    g0->Write();
    f->Close();
}