#include "TFile.h" #include "TCanvas.h" #include "TStyle.h" #include "TH1.h" #include "TH2.h" #include "TH3.h" #include "TGaxis.h" #include "TRandom.h" #include "TLegend.h" #include "TPaveStats.h" void fitspettro() { TFile *f = TFile::Open("H:/cors/cors_plot.root"); if (f == 0) { // if we cannot open the file, print an error message and return immediatly printf("Error: cannot open cors_plot.root!\n"); return; } f->ls(); TH1F *hene = (TH1F *)f->Get("hene"); if (hene == 0) { printf("Error getting an histogram from the file!\n"); return; } TCanvas *c14 = new TCanvas("c14","hists with different scales",1024,748); c14->SetLogx(); c14->SetLogy(); hene->SetLineColor(kBlue); hene->Draw(); c14->Update(); TF1 *fitspettro = new TF1 ("fitspettro","x^(-[0])",0,1000); hene->Fit("fitspettro"); Double_t chi2 = fitspettro->GetChisquare(); Double_t c0=fitspettro->GetParameter(0); Double_t e0=fitspettro->GetParError(0); TLegend* leg = new TLegend(0.2, 0.7, .4, .85); leg->SetNColumns(1); leg->AddEntry(fitspettro, "Fit", "l"); leg->Draw("same"); TPaveStats *stats1 = (TPaveStats*)fitspettro->GetListOfFunctions()->FindObject("stats"); stats1->SetTextColor(kRed); stats1->SetX1NDC(0.80); stats1->SetX2NDC(0.98); c14->Update; gROOT->ProcessLine("gROOT->SetBatch()"); c14.Print("H:/cors/fitspettro.pdf"); delete c14; gROOT->ProcessLine("gROOT->SetBatch(kFALSE)"); }