#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" #include "TGraph.h" #include "TSystem.h" #include "TTree.h" #include "TTreePlayer.h" #include "TF1.h" #include "TCut.h" #include "TPaletteAxis.h" #include "stdio.h" #include "stdlib.h" #include "string.h" void background() { double t; int n=3; int i=0; int entry=1; gStyle->SetOptStat(000000); gStyle->SetOptFit(0); //no print stat TString myplot = TString::Format("background.png"); FILE * infile=fopen("backgrounddata.txt", "r"); TGaxis::SetMaxDigits(n); if(infile==NULL) { printf("Error opening file"); exit(1); } TCanvas *c0 = new TCanvas("c0","",1280,1024); TH1F *graph1 = new TH1F("graph1","Hours",10,0,10); while( fscanf(infile,"%lf",&t)==1) { std::cout << "entry = " << entry << "\t time = " << t << std::endl; entry=entry+1; graph1->Fill(t); } graph1->SetTitle(""); graph1->GetXaxis()->SetTitle("Counts/s"); TF1 *f = new TF1("f", "[0]*TMath::Power(([1]/[2]),(x/[2]))*(TMath::Exp(-([1]/[2])))/TMath::Gamma((x/[2])+1.)", 0, 10); f->SetParameters(1, 1, 1); // you MUST set non-zero initial values for parameters graph1->GetYaxis()->SetTitle(""); graph1->GetYaxis()->SetTitleSize(40); graph1->GetYaxis()->SetTitleFont(43); graph1->GetYaxis()->SetLabelFont(43); graph1->GetYaxis()->SetLabelSize(40); graph1->GetXaxis()->SetTitleSize(40); graph1->GetXaxis()->SetTitleFont(43); graph1->GetXaxis()->SetLabelFont(43); graph1->GetXaxis()->SetLabelSize(40); graph1->SetFillColorAlpha(kBlue, 0.35); graph1->DrawNormalized(); //graph1->Draw(); gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn //graph1->Fit("gaus"); graph1->Fit(f, "R"); // "R" = fit between "xmin" and "xmax" of the "f" gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn //TPaveStats *statsgraph1 = (TPaveStats*)graph1->GetListOfFunctions()->FindObject("stats"); //statsgraph1->SetTextColor(kBlue); //statsgraph1->SetX1NDC(0.80); statsgraph1->SetX2NDC(0.98); //statsgraph1->SetY1NDC(0.77); statsgraph1->SetY2NDC(0.92); gPad->Update(); c0->Print(myplot); }