#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(); gStyle->SetOptFit(); 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", "[0] / x / x", 1, 5e3); fitspettro->SetParameter(0, 1.0e6); //TF1 *fitspettro = new TF1("fitspettro", "TMath::Power([0] * x, [1])", 1, 5e3); // fitspettro->SetParameters(1.0e-3, -2.0); fitspettro->SetParameter(0, 1.0e6); hene->Fit("fitspettro"); Double_t chi2 = fitspettro->GetChisquare(); TLegend* leg = new TLegend(0.2, 0.7, .4, .85); leg->SetNColumns(1); leg->AddEntry(fitspettro, "Fit", "l"); leg->Draw("same"); TPaveStats *stats1 = (TPaveStats*)hene->GetListOfFunctions()->FindObject("stats"); stats1->SetTextColor(kBlue); stats1->SetX1NDC(0.80); stats1->SetX2NDC(0.98); gROOT->ProcessLine("gROOT->SetBatch()"); c14.Print("H:/cors/fitspettro.pdf"); // delete c14; gROOT->ProcessLine("gROOT->SetBatch(kFALSE)"); }