void ReFit(int lo, int hi) { TF1* floo = (TF1*) gROOT->FindObject("floo"); if (floo == 0) return; TH1D* hCe = (TH1D*) gROOT->FindObject("hCe"); if (hCe == 0) return; hCe->GetXaxis()->SetRangeUser(lo,hi); double max = hCe->GetMaximum(); int binmax = hCe->GetMaximumBin(); double xmax = hCe->GetXaxis()->GetBinCenter(binmax); floo->SetParameters(max,xmax,10,max*0.1,50,20,5); hCe->Fit(floo,"Q","",lo,hi); }
TF1 * tripleGaussFit(TH1 * hist, bool autorange, double inner=0.01, double outer=0.1){ /** * @brief performes a double gaussian Fit */ Double_t parameters[9] = {0,0,0,0,0,0,0,0,0}; double center = hist->GetMean(); if (autorange){ double inner = fabs(center - hist->GetXaxis()->GetXmax()) /10; double outer = fabs(center - hist->GetXaxis()->GetXmax()) /10*8; } double middle = (outer - inner)/2; TF1 * fit1 = new TF1("fit1", "gaus", center-inner, center+inner ); hist->Fit(fit1, "0R"); fit1->GetParameters(¶meters[0]); TF1 * fitmid = new TF1("fitmid", "gaus", center-middle, center+middle ); fitmid->SetParameter(1, parameters[1]); hist->Fit(fitmid, "0R"); fitmid->GetParameters(¶meters[3]); TF1 * fit2 = new TF1("fit2", "gaus", center-outer, center+outer ); fit2->SetParameter(1, parameters[1]); hist->Fit(fit2, "0R"); fit2->GetParameters(¶meters[6]); TF1 * fitproper = new TF1("fitproper", "gaus(0)+gaus(3)+gaus(6)", center-outer, center+outer); fitproper->SetParameters(parameters); fitproper->SetParName(0, "Const.(inner)"); fitproper->SetParName(1, "Mean (inner)"); fitproper->SetParName(2, "Sigma (inner)"); fitproper->SetParName(3, "Const.(middle)"); fitproper->SetParName(4, "Mean (middle)"); fitproper->SetParName(5, "Sigma (middle)"); fitproper->SetParName(6, "Const.(outer)"); fitproper->SetParName(7, "Mean (outer)"); fitproper->SetParName(8, "Sigma (outer)"); hist->Fit(fitproper, "0R"); // fitproper->SetLineColor(hist->GetLineColor()); // fitproper->SetLineWidth(hist->GetLineWidth()); //// fitproper->SetLineStyle(2); return fitproper; }
TF1 * residualFit(double pars[], const std::string & index) { ResidualFit * fobj = new ResidualFit; TF1 * functionToFit = new TF1(("fp"+index).c_str(), fobj, 60, 120, fobj->parNum(), "ResidualFit"); // double pars[] = { -2.30972e-02, 7.94253e-01, 7.40701e-05, 2.79889e-06, // -2.04844e-08, -5.78370e-43, 6.84145e-02, -6.21316e+00, // -6.90792e-03, 6.43131e-01, -2.03049e-03, 3.70415e-05, // -1.69014e-07 }; functionToFit->SetParameters(pars); return functionToFit; }
float getBkgErr(float mass) { static bool isInit=false; static TF1 ebErr2Func("ebErrFunc","pol4",0,3000); static TF1 eeErr2Func("eeErrFunc","pol4",0,3000); static TF1 pdfFunc("pdfFunc","pol3",0,3000); static TF1 ewkFunc("ewkFunc","pol2",0,3000); static TF1 muonErrFunc("muonEffFunc","pol2",0,3000); if(!isInit){ ebErr2Func.SetParameters(19.2,6.79E-3,4.32E-5,9.81E-9,7.18E-12); eeErr2Func.SetParameters(21.2,6.79E-3,4.32E-5,9.81E-9,7.18E-12); pdfFunc.SetParameters(4.15,1.83E-3,2.68E-6); ewkFunc.SetParameters(-1,4.2E-3); muonErrFunc.SetParameters(1.045,1.043E-5,3.378E-8); } //if(isMuon){ //float pdfErr = pdfFunc.Eval(mass); // float ewkErr = ewkFunc.Eval(mass); return muonErrFunc.Eval(mass)-1; //} }
Double_t calc_dNdY(const char *data_file = "pt_RFE", Bool_t show = kFALSE) { TGraphErrors *g = new TGraphErrors(data_file); if (g->IsZombie()) return; TF1 *flt = new TF1("flt", LevyTsallis, 0., 5., 4); flt->SetParameters(2.93483e-02, 2.80382e-01, 8.10224e+00, 1.01944e+00); flt->FixParameter(3, 1.019445); Double_t fitmin = 0.25, fitmax = 5.25; // !!! g->Fit(flt, "Q", "", fitmin, fitmax); g->Fit(flt, "Q", "", fitmin, fitmax); g->Fit(flt, "Q", "", fitmin, fitmax); Double_t first, last; // !!! bining sensibility !!! Int_t graph_range[2] = {2, 23}; // [2] = 0.05 - 0.01, [23] = 4.70 + 0.01 // interval 1 first = 0.0; // !!! last = g->GetX()[graph_range[0]] - g->GetErrorX(graph_range[0]); Double_t fi1 = flt->Integral(first, last); // interval 2 first = last; last = g->GetX()[graph_range[1]] + g->GetErrorX(graph_range[1]); Double_t fi2 = flt->Integral(first, last); Double_t gi2 = 0.0; for (Int_t ip = graph_range[0]; ip <= graph_range[1]; ip++) gi2 += g->GetY()[ip]*g->GetErrorX(ip)*2.0; // interval 3 first = last; last = 30.0; // !!! Double_t fi3 = flt->Integral(first, last); Double_t result = fi1 + gi2 + fi3; if (!show) { delete g; delete flt; return result; } Printf("function: %f \t %f \t %f", fi1, fi2, fi3); Printf("graph: \t \t \t %f", gi2); Printf("result => %f", result); g->Draw("AP"); gPad->SetLogy(); return result; }
void fitCauchy() { TCanvas * c1 = new TCanvas("mycan","mycan",800,600); TFile * data = new TFile("~/unpacker/sort.root"); fstream file1; file1.open("gaus3.par"); TH1F * dist = (TH1F*)data->Get("/corr/Li7/Li7_theta_reactCoM_12C")->Clone("1"); Double_t fparams[9] = {6.,1.,12.,1.,18.,1.,20000,15000,10000}; TF1 * f = new TF1("f",Cauchy3,0,180,9); TF1 * f2 = new TF1("f2",Gaus3,0,180,9); f2->SetParameters(fparams); f->SetParameters(fparams); dist->Fit("f2","M0"); dist->Draw(); f2->Draw("same"); if(file1.is_open()) cout <<"file1 is open" << endl; else cout << "failed to load file1" << endl; for(int i=0;i<9;i++) { file1 << f2->GetParameter(i) << endl; } TCanvas * c2 = new TCanvas("mycan2","mycan2",800,600); TH1F * check = new TH1F("check","",125,0,40); for(int i =0;i<300000;i++) { float r = f2->GetRandom(); check->Fill(r); } check->Draw(); Double_t par[9]; // for(int i=0;i<9;i++) //{ // file1 >> par[i] >> endl; // cout << "par[" << i << "] = " << par[i] << endl; //} //cout << "what" << endl; //file1.close(); return; }
TF1* fitTools::fitResponseGraph( TGraphErrors* graph, std::string funcType, std::string funcName, const std::string& option, float rangeMax, float rangeMin) { TF1* fitFunction; if( funcType=="rpf" ) { fitFunction = new TF1(funcName.c_str(), rpf, rangeMin, rangeMax, 6); //will have to fix the range issue! fitFunction->SetParameters(100,0.85,4.2,80,250,1.); fitFunction->SetParLimits(1,0.5,1.0); fitFunction->SetParLimits(2,1.,10.); fitFunction->SetParLimits(4, 100., 500.); fitFunction->SetParameter(0, 0.6); fitFunction->SetParameter(5, 1.); //fitFunction->SetParameter(1, -0.6); } else if( funcType=="powerlaw" ) { fitFunction = new TF1(funcName.c_str(), "[0] - [1]/(pow(x, [2]))"); fitFunction->SetRange(rangeMin, rangeMax); fitFunction->SetParameters(1., 1., 0.3); } else if( funcType=="powerlawL2L3" ) { fitFunction = new TF1(funcName.c_str(), "1. + [0]/(pow(x, [1]))"); fitFunction->SetRange(rangeMin, rangeMax); fitFunction->SetParameters(0.7, 0.7); //fitFunction->SetParLimits(0, 0.5, 2.); //fitFunction->SetParLimits(1, 0.5, 2.); } else if( funcType=="powerlaw_corr" ) { fitFunction = new TF1(funcName.c_str(), "[0] - [1]/(pow(x, [2])) + [3]/x"); fitFunction->SetRange(rangeMin, rangeMax); fitFunction->SetParameters(1., 1., 0.3, 1.); } else { std::cout << "Function '" << funcType << "' not implemented yet for fitResponseGraph. Exiting." << std::endl; exit(119); } graph->Fit(fitFunction, option.c_str()); return fitFunction; }
double findDelPhiAngle(TH1D *input, int parorErr){ TF1 *fitter = new TF1("fitter","gaus",-2,2); //input->Scale(1./(input->GetEntries())); fitter->SetParameters(1./input->GetMaximum(),-0.1,1.0); input->Fit("fitter","qN0","",-1.5,1.5); if(parorErr==1){ return fitter->GetParameter(1); } else if(parorErr==2){ return fitter->GetParError(1); } else return 0; }
void qa2() { //Fill a 1-D histogram from a parametric function TCanvas *c1 = new TCanvas("c1","The FillRandom example",0,0,700,500); c1->SetFillColor(18); gBenchmark->Start("fillrandom"); // // A function (any dimension) or a formula may reference // an already defined formula // TFormula *form1 = new TFormula("form1","abs(sin(x)/x)"); TF1 *sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10); sqroot->SetParameters(10,4,1,20); // // Create a one dimensional histogram (one float per bin) // and fill it following the distribution in function sqroot. // TH1F *h1f = new TH1F("h1f","Test random numbers",200,0,10); h1f->SetFillColor(45); h1f->FillRandom("sqroot",100000); h1f->Draw(); TPaveLabel *lfunction = new TPaveLabel(5,39,9.8,46,"The sqroot function"); lfunction->SetFillColor(41); c1->SetGridx(); c1->SetGridy(); c1->GetFrame()->SetFillColor(42); c1->GetFrame()->SetBorderMode(-1); c1->GetFrame()->SetBorderSize(5); h1f->SetDirectory(0); c1->Update(); sqroot->SetParameters(200,4,1,20); }
TF1* fitPixelBeam(TH1D* hist, double pixelWidth, double beamSigma, bool display) { TF1* conv = new TF1("conv", "[2] + [4] * 0.5 * ( TMath::Erf(([0]/2 + [3] - x)/(sqrt(2)*[1])) + TMath::Erf(([0]/2 + [3] + x)/(sqrt(2)*[1])) )"); conv->SetParNames("Width", "Sigma", "Background", "Offset", "Scale"); const unsigned int numBins = hist->GetNbinsX(); const double limitLow = hist->GetXaxis()->GetBinLowEdge(1); const double limitHigh = hist->GetXaxis()->GetBinUpEdge(numBins); // Estimate the background using the +/- 10% edges of the histogram double background = 0; const unsigned int numBackgroundBins = numBins * 0.1 + 1; for (unsigned int n = 0; n < numBackgroundBins; n++) { background += hist->GetBinContent(n + 1); background += hist->GetBinContent(numBins - n); } background /= (double)(2 * numBackgroundBins); // Estimate the scale double scale = 0; for (unsigned int bin = 1; bin <= numBins; bin++) if (hist->GetBinContent(bin) > scale) scale = hist->GetBinContent(bin); double offset = 0; conv->SetRange(limitLow, limitHigh); conv->SetParameters(pixelWidth, beamSigma, background, offset, scale); conv->SetParLimits(0, 0.1 * pixelWidth, 100 * pixelWidth); conv->SetParLimits(1, 0, 100 * beamSigma); conv->SetParLimits(2, 0, scale); conv->SetParLimits(3, -pixelWidth, pixelWidth); conv->SetParLimits(4, 0.1 * scale, 10 * scale); hist->Fit("conv", "QR0B"); if (display) { TCanvas* can = new TCanvas(); conv->SetLineColor(46); conv->SetLineWidth(1); hist->Draw(); conv->Draw("SAME"); can->Update(); can->WaitPrimitive(); } return conv; }
void birks() { TF1 *bf4 = new TF1("birks4",birksf,0,100,2); bf4->SetParameters(4.6345e-2,0.0); bf4->SetLineWidth(2); bf4->SetLineColor(7); bf4->GetXaxis()->SetTitle("-dE/dx [MeV/g cm^{2}]"); bf4->GetYaxis()->SetTitle("Birks Factor"); bf4->Draw(); TF1 *bf = new TF1("birks",birksf,0,100,2); bf->SetParameters(1.29e-2,9.59e-6); bf->SetLineWidth(2); bf->SetLineColor(3); bf->Draw("SAME"); TF1 *bf1 = new TF1("birks1",birksf,0,100,2); bf1->SetParameters(8.35e-3,0.0); bf1->SetLineWidth(2); bf1->SetLineColor(4); bf1->Draw("SAME"); TF1 *bf2 = new TF1("birks2",birksf,0,100,2); bf2->SetParameters(1.31e-2,0.0); bf2->SetLineWidth(2); bf2->SetLineColor(2); bf2->Draw("SAME"); TF1 *bf3 = new TF1("birks3",birksf,0,100,2); bf3->SetParameters(1.59e-2,0.0); bf3->SetLineWidth(2); bf3->SetLineColor(6); bf3->Draw("SAME"); TF1 *bf5 = new TF1("birks5",birksf,0,100,2); bf5->SetParameters(6.8552e-4,0.0); bf5->SetLineWidth(2); bf5->SetLineColor(21); bf5->Draw("SAME"); TF1 *bf6 = new TF1("birks6",birksf,0,100,2); bf6->SetParameters(3.5175e-3,0.0); bf6->SetLineWidth(2); bf6->SetLineColor(8); bf6->Draw("SAME"); TLegend *legend = new TLegend(.44, .64, .99, .99); legend->AddEntry(bf, "CMS/ATLAS"); legend->AddEntry(bf2, "Standard Birks law."); legend->AddEntry(bf1, "Zeus SCSN38, lower limit (-35%) IEEE TNS Vol. 39 NO.4 (1992), 511"); legend->AddEntry(bf3, "Zeus SCSN38, upper limit (+21%) IEEE TNS Vol. 39 NO.4 (1992), 511"); legend->AddEntry(bf4, "BGO, NIM A439 (2000) 158-166"); legend->AddEntry(bf5, "CsI(Tl), NIM A439 (2000) 158-166"); legend->AddEntry(bf6, "GSO(Ge), NIM A439 (2000) 158-166"); legend->Draw(); }
int main(int argc, char const *argv[]) { TFile *myFile = new TFile("output.root","RECREATE"); Float_t E1,Px1,Py1,Pz1,E2,Px2,Py2,Pz2; Double_t mass = 0; TH1D *m12 = new TH1D("m12","m12",60,0.99,1.08); TF1 *myBW = new TF1("myBW","TMath::BreitWigner(x,[0],[1])", 1.01, 1.03); myBW->SetParName(0,"Mass"); myBW->SetParName(1,"#Gamma"); TFile *inputFile = new TFile("data.root"); TTree *data_tree = (TTree*)inputFile->Get("data"); data_tree->SetBranchAddress("E1", &E1); data_tree->SetBranchAddress("Px1", &Px1); data_tree->SetBranchAddress("Py1", &Py1); data_tree->SetBranchAddress("Pz1", &Pz1); data_tree->SetBranchAddress("E2", &E2); data_tree->SetBranchAddress("Px2", &Px2); data_tree->SetBranchAddress("Py2", &Py2); data_tree->SetBranchAddress("Pz2", &Pz2); TLorentzVector vec1(0.0,0.0,0.0,0.0); TLorentzVector vec2(0.0,0.0,0.0,0.0); int count = data_tree->GetEntries(); for (int i = 0; i < count; i++) { data_tree->GetEntry(i); vec1.SetPxPyPzE(Px1,Py1,Pz1,E1); vec2.SetPxPyPzE(Px2,Py2,Pz2,E2); mass = (vec1 + vec2).M(); m12->Fill(mass); } myBW->SetParameters(0,1.02,0.05); myBW->SetParLimits(0,1.01,1.03); m12->Fit("myBW","","",0,2); m12->GetXaxis()->SetTitle("Mass (GeV)"); gStyle->SetOptFit(1111); myFile->cd(); myFile->Write(); myFile->Close(); return 0; }
void XZFit(){ if (xerr[0] == 0 && xerr[1] == 0 && xerr[2] == 0 && zerr[0] == 0 && zerr[1] == 0 && zerr[2] == 0) {cout << "ERRORE: Incertezze non specificate" << endl;} else { // TGraphErrors *graph1 = new TGraphErrors(3,x,y,xerr,yerr); // TF1* fitfunc1 = new TF1("fittingfunction","[0]+[1]*x",-100,100); // xyfitfunc = fitfunc1; // xygraph = graph1; delete xzfitfunc; //Dealloca prima di riallocarne uno nuovo delete xzgraph; xzfitfunc = new TF1("xzfittingfunction", "[0]+[1]*x",-100,100); xzgraph = new TGraphErrors(3,xv,yv,xerr,zerr); xzfitfunc->SetParameters(30,-2); xzgraph->Fit(xzfitfunc,"0S"); } };
void fitExclude() { //Create a source function TF1 *f1 = new TF1("f1","[0] +[1]*x +gaus(2)",0,5); f1->SetParameters(6,-1,5,3,0.2); // create and fill histogram according to the source function TH1F *h = new TH1F("h","background + signal",100,0,5); h->FillRandom("f1",2000); TF1 *fl = new TF1("fl",fline,0,5,2); fl->SetParameters(2,-1); //fit only the linear background excluding the signal area reject = kTRUE; h->Fit(fl,"0"); reject = kFALSE; //store 2 separate functions for visualization TF1 *fleft = new TF1("fleft",fline,0,2.5,2); fleft->SetParameters(fl->GetParameters()); h->GetListOfFunctions()->Add(fleft); gROOT->GetListOfFunctions()->Remove(fleft); TF1 *fright = new TF1("fright",fline,3.5,5,2); fright->SetParameters(fl->GetParameters()); h->GetListOfFunctions()->Add(fright); gROOT->GetListOfFunctions()->Remove(fright); h->Draw(); }
TF1* GetFitFunc_ZYAM_MC(TH1D* h) { TH1D* hcorrphi = (TH1D*)h->Clone(h->GetName()); double histminY = hcorrphi->GetBinContent(10); double histminX = 1.0; //hcorrphi->SetAxisRange(-0.01,1.2,"X"); TF1* fitfunc = new TF1(Form("fitfunc_%s",h->GetName()),"[0]+[1]*(x-[2])*(x-[2])",0.8,2.8); //std 0.15-1.8 fitfunc->SetParameters(histminY,0.0002,histminX); fitfunc->SetParLimits(1,0,1000); fitfunc->SetParLimits(2,0.05,1000); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%s",h->GetName()),"RNO"); return fitfunc; }
void TestFit(Int_t n,Int_t n2=10000){ Double_t *x = new Double_t[n2]; Double_t *y = new Double_t[n2]; Double_t par[] = {1,0,1,1}; TF1 *f = 0; Int_t norm = n*100000/n2; // TGraph *gr=0; for (Int_t i=0;i<n2;i++){x[i]=gRandom->Rndm();} // // for (Int_t i=0;i<n2;i++){y[i]=x[i]+0.1*gRandom->Gaus();} gr = new TGraph(n2,x,y); f = new TF1("f","pol1",-10,10); TestFitG(gr,f,norm); // for (Int_t i=0;i<n2;i++){x[i]=gRandom->Rndm()*10.-5.; y[i] = TMath::BreitWigner(x[i],0,1)+gRandom->Rndm();} gr = new TGraph(n2/50,x,y); f = new TF1("f","[0]*TMath::BreitWigner(x,[1],[2])",-10,10); f->SetParameters(par); TestFitG(gr,f,norm); for (Int_t i=0;i<n2;i++){x[i]=gRandom->Rndm()*10.-5.; y[i] = 1+TMath::BreitWigner(x[i],0,1)+gRandom->Rndm();} gr = new TGraph(n2/50,x,y); f = new TF1("f","[0]*TMath::BreitWigner(x,[1],[2])+[3]",-10,10); f->SetParameters(par); TestFitG(gr,f,norm); // for (Int_t i=0;i<n2;i++){x[i]=gRandom->Rndm()*10.-5.; y[i] = TMath::Landau(x[i],0,1)+gRandom->Rndm();} gr = new TGraph(n2/50,x,y); f = new TF1("f","[0]*TMath::Landau(x,[1],[2])",-10,10); f->SetParameters(par); TestFitG(gr,f,norm); f = new TF1("f","landau",-10,10); f->SetParameters(par); TestFitG(gr,f,norm); for (Int_t i=0;i<n2;i++){x[i]=gRandom->Rndm()*10.-5.; y[i] = TMath::Gaus(x[i],0,1)+gRandom->Rndm();} gr = new TGraph(n2/50,x,y); f = new TF1("f","[0]*TMath::Gaus(x,[1],[2])",-10,10); f->SetParameters(par); TestFitG(gr,f,norm); f = new TF1("f","gaus",-10,10); f->SetParameters(par); TestFitG(gr,f,norm); for (Int_t i=0;i<n2;i++){x[i]=gRandom->Rndm()*10.-5.; y[i] = 2+TMath::Gaus(x[i],0,1)+gRandom->Rndm();} f = new TF1("f","gaus+[3]",-10,10); f->SetParameters(par); TestFitG(gr,f,norm); delete [] x; delete [] y; }
void fithist() { //fit function ftotal to signal + background histgen(); TFile *f = new TFile("background.root"); background = (TH1F*)f->Get("background"); //pointer used in ftotal TH1F *result = (TH1F*)f->Get("result"); TF1 *ftot = new TF1("ftot",ftotal,0,10,4); Double_t norm = result->GetMaximum(); ftot->SetParameters(0.5*norm,5,.2,norm); ftot->SetParLimits(0,.3*norm,norm); result->Fit("ftot","b"); }
TF1* GetFitFunc_ZYAM_AllPhysics(TH1D* h) { TH1D* hcorrphi = (TH1D*)h->Clone(h->GetName()); double histminY = hcorrphi->GetBinContent(10); double histminX = 1.0; hcorrphi->SetAxisRange(-0.01,1.5,"X"); TF1* fitfunc = new TF1(Form("fitfunc_%s",h->GetName()),"[0]+[1]*(x-[2])*(x-[2])+[3]*(x-[2])*(x-[2])*(x-[2])",0.5,1.65); //std 0.6 1.55 vs pT ; 0.6 1.8 vs eta fitfunc->SetParameters(histminY,0.0002,histminX,0.0001); fitfunc->SetParLimits(1,0,1000); fitfunc->SetParLimits(2,0.5,1000); // fitfunc->SetParLimits(3,0,1000); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%s",h->GetName()),"RNO"); return fitfunc; }
TF1 *langaufit(TH1F *his, Double_t *fitrange, Double_t *startvalues, Double_t *parlimitslo, Double_t *parlimitshi, Double_t *fitparams, Double_t *fiterrors, Double_t *ChiSqr, Int_t *NDF) { // Once again, here are the Landau * Gaussian parameters: // par[0]=Width (scale) parameter of Landau density // par[1]=Most Probable (MP, location) parameter of Landau density // par[2]=Total area (integral -inf to inf, normalization constant) // par[3]=Width (sigma) of convoluted Gaussian function // // Variables for langaufit call: // his histogram to fit // fitrange[2] lo and hi boundaries of fit range // startvalues[4] reasonable start values for the fit // parlimitslo[4] lower parameter limits // parlimitshi[4] upper parameter limits // fitparams[4] returns the final fit parameters // fiterrors[4] returns the final fit errors // ChiSqr returns the chi square // NDF returns ndf Int_t i; Char_t FunName[100]; sprintf(FunName,"Fitfcn_%s",his->GetName()); TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName); if (ffitold) delete ffitold; TF1 *ffit = new TF1(FunName,langaufun,fitrange[0],fitrange[1],4); ffit->SetParameters(startvalues); ffit->SetParNames("Width","MP","Area","GSigma"); for (i=0; i<4; i++) { ffit->SetParLimits(i, parlimitslo[i], parlimitshi[i]); } his->Fit(FunName,"RB0"); // fit within specified range, use ParLimits, do not plot ffit->GetParameters(fitparams); // obtain fit parameters for (i=0; i<4; i++) { fiterrors[i] = ffit->GetParError(i); // obtain fit parameter errors } ChiSqr[0] = ffit->GetChisquare(); // obtain chi^2 NDF[0] = ffit->GetNDF(); // obtain ndf return (ffit); // return fit function }
void FindPeak(TH1 *hm, int * i, char * namefile){ int np =5, p, max = 0; int npeaks = TMath::Abs(np); double par[3000]; par[0] = 0.8; par[1] = -0.6/1000; for (p=0;p<npeaks;p++) { par[3*p+2] = 1; par[3*p+3] = 10+gRandom->Rndm()*980; par[3*p+4] = 3+2*gRandom->Rndm(); } TSpectrum *s = new TSpectrum(2*npeaks,1); int nfound = s->Search(hm,2,"",0.10); printf("Found %d candidate peaks to fit\n",nfound); TH1 *hb = s->Background(hm,20,"same"); if (np <0) return; // loope over peaks TF1 *fline = new TF1("fline","pol1",0,1000); hm->Fit("fline","qn"); par[0] = fline->GetParameter(0); par[1] = fline->GetParameter(1); npeaks = 0; float *xpeaks = s->GetPositionX(); for (p=0;p<nfound;p++) { float xp = xpeaks[p]; int bin = hm->GetXaxis()->FindBin(xp); float yp = hm->GetBinContent(bin); if (yp-TMath::Sqrt(yp) < fline->Eval(xp)) continue; par[3*npeaks+2] = yp; par[3*npeaks+3] = xp; par[3*npeaks+4] = 3; npeaks++; } printf("Found %d useful peaks to fit\n",npeaks); printf("Now fitting: Be patient\n"); if (max < npeaks) max = npeaks; TF1 *fit = new TF1("fit",fpeaks,0,1000,2+3*npeaks); TVirtualFitter::Fitter(hm,10+3*npeaks); fit->SetParameters(par); fit->SetNpx(1000); hm->Fit("fit"); }
TF1 * gaussFit(TH1 * hist, double inner) { Double_t parameters[6] = {0,0,0,0,0,0}; double center =0;// hist->GetMean(); TF1 * fit = new TF1("fit", "gaus", center-inner, center+inner ); hist->Fit(fit, "Q0R"); fit->GetParameters(¶meters[0]); fit->SetParameters(parameters); fit->SetParName(0, "Const."); fit->SetParName(1, "Mean"); fit->SetParName(2, "Sigma"); return fit; }
Double_t NtrueOverNmeas (Double_t ptMin, Double_t ptMax) { //Double_t n = 6; //Double_t n = 5.78121e+00; // from ref level Double_t n = 5.95950e+00; // from reco level Double_t c1 = 5.23359e-01; Double_t c2 = 1.15226e-01; Double_t c3 = -7.96949e-03; TF1 *fTrue = new TF1("fMeas","([1]+[2]*log(x)+[3]*log(x)*log(x)+[2]+2*[3]*log(x))/exp([0]*log(x*([1]+[2]*log(x)+[3]*log(x)*log(x))))",0,500); fTrue->SetParameters(n,c1,c2,c3); TF1 *fMeas = new TF1("fTrue","1/exp([0]*log(x))",0,500); fMeas->SetParameter(0,n); return fTrue->Integral(ptMin,ptMax)/fMeas->Integral(ptMin,ptMax); }
void GetRandomTest(){ double k0=1.39; double k1 = 0.425; double theta0 = 3.41; double theta1 = 1.30; int iNpart=2; double k_=k0+k1*(iNpart-2); double theta_=theta0+theta1*TMath::Log(iNpart-1); TF1 *f = new TF1("f","TMath::GammaDist(x,[0],0,[1])",0,200); f->SetParameters(k1,theta_); cout<<"Value at 0 = "<<f->Eval(0)<<endl; cout<<"Value at 1e-11 = "<<f->Eval(1e-11)<<endl; cout<<"Integral 1= "<<f->Integral(f->GetXmin(),f->GetXmax())<<endl; f->SetRange(1e-12,200); cout<<"Integral 2= "<<f->Integral(f->GetXmin(),f->GetXmax())<<endl; cout<<"fXmin = "<<f->GetXmin()<<"\tfXmax = "<<f->GetXmax()<<"\tfNpx = "<<f->GetNpx()<<endl; f->SetNpx(1e5); TCanvas *c1 = new TCanvas(); c1->SetLogy(); cout<<"f mean = "<<f->Mean(0,200)<<endl; cout<<"math mean = "<<f->GetParameter(0)*f->GetParameter(1)<<endl; TH1D* h = new TH1D("h","h",1000,0,200); for(int i=0;i<1e6;i++){ double para = f->GetRandom(); h->Fill(para); } h->Scale(1.0/h->Integral()*1000/200); h->GetYaxis()->SetRangeUser(1e-10,1); h->SetMarkerStyle(24); h->SetMarkerColor(4); h->SetMarkerSize(1.1); TLegend *leg = new TLegend(0.6,0.7,0.8,0.9); leg->SetFillColor(0); leg->SetFillStyle(0); leg->SetBorderSize(0); leg->SetTextFont(42); leg->SetTextSize(0.03); leg->AddEntry(f,"function","lp"); leg->AddEntry(h,"filled histogram","lp"); h->Draw("P"); f->Draw("same"); leg->Draw("same"); cout<<"h mean = "<<h->GetMean(1)<<endl; c1->Print("TestGetRandom.png"); }
TF1* linLorentzianFit(TH1* histoY){ TString name = histoY->GetName() + TString("Fit"); // Fit slices projected along Y from bins in X TF1 *fit = new TF1(name,lorentzianPlusLinear,70,110,5); fit->SetParameters(histoY->GetMaximum(),histoY->GetRMS(),histoY->GetMean(),10,-0.1); fit->SetParNames("norm","width","mean","offset","slope"); fit->SetParLimits(1,-10,10); //fit->SetParLimits(2,85,95); //fit->SetParLimits(2,90,93); fit->SetParLimits(2,2,4); fit->SetParLimits(3,0,100); fit->SetParLimits(4,-1,0); fit->SetLineWidth(2); //histoY -> Fit(name,"0","",85,97); histoY -> Fit(name,"0","",2,4); return fit; }
TH1D* GetJetCorrFunc1D_ZYA1(int itrg, int jass) { TH1D* hcorrphi = (TH1D*)GetRawCorrFunc1D_ratio(itrg,jass); TH1D* hcorrphi_clone = (TH1D*)hcorrphi->Clone(Form("corrphi_clone_itrg%d_jass%d",itrg,jass)); hcorrphi->SetAxisRange(0.5,2.0,"X"); double histminY = hcorrphi->GetMinimum(); TF1* fitfunc = new TF1("fitfunc","[0]+[1]*x",0.8,1.2); fitfunc->SetParameters(histminY,0); fitfunc->FixParameter(1,0); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit("fitfunc","RNO"); float level = fitfunc->GetParameter(0); for(int ibin=1;ibin<=hcorrphi_clone->GetNbinsX();ibin++) hcorrphi_clone->SetBinContent(ibin,hcorrphi_clone->GetBinContent(ibin)-level); float max = hcorrphi_clone->GetBinContent(hcorrphi_clone->GetMaximumBin()); hcorrphi_clone->SetAxisRange(ymin,max*1.3,"Y"); hcorrphi_clone->SetAxisRange(-PI/2.,3.*PI/2.,"X"); delete fitfunc; return hcorrphi_clone; }
TH1D* GetJetCorrFunc1D_doublegaussian(int itrg, int jass) { TH1D* hcorrphi = (TH1D*)GetRawCorrFunc1D_ratio(itrg,jass); TF1* fitfunc = new TF1("fitfunc",doubleGaussian,-PI/2.,3.*PI/2.,5); fitfunc->SetParameters(hcorrphi->GetMinimum(),hcorrphi->GetMaximum()-hcorrphi->GetMinimum(),0.3,hcorrphi->GetMaximum(hcorrphi->GetMaximum())-hcorrphi->GetMinimum(),0.5); fitfunc->SetParLimits(0,0,100000); fitfunc->SetParLimits(1,0,100000); fitfunc->SetParLimits(2,0,100000); fitfunc->SetParLimits(3,0,100000); fitfunc->SetParLimits(4,0,100000); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit("fitfunc","RNO"); TH1D* hcorrphi_clone = (TH1D*)hcorrphi->Clone(Form("corrphi_clone_itrg%d_jass%d",itrg,jass)); float level = fitfunc->GetParameter(0); for(int ibin=1;ibin<=hcorrphi_clone->GetNbinsX();ibin++) hcorrphi_clone->SetBinContent(ibin,hcorrphi_clone->GetBinContent(ibin)-level); float max = hcorrphi_clone->GetBinContent(hcorrphi_clone->GetMaximumBin()); hcorrphi_clone->SetAxisRange(ymin,max*1.3,"Y"); delete fitfunc; return hcorrphi_clone; }
TF1 * doubleGaussFit(TH1 * hist, bool autorange, double inner, double outer){ /** * @brief performes a double gaussian Fit */ Double_t parameters[6] = {0,0,0,0,0,0}; double center = hist->GetMean(); if (autorange){ double inner = fabs(center - hist->GetXaxis()->GetXmax()) /10; double outer = fabs(center - hist->GetXaxis()->GetXmax()) /10*8; } TF1 * fit1 = new TF1("fit1", "gaus", center-inner, center+inner ); hist->Fit(fit1, "Q0R"); fit1->GetParameters(¶meters[0]); TF1 * fit2 = new TF1("fit2", "gaus", center-outer, center+outer ); hist->Fit(fit2, "Q0R"); fit2->GetParameters(¶meters[3]); TF1 * fitproper = new TF1("fitproper", "gaus(0)+gaus(3)", center-outer, center+outer); fitproper->SetParameters(parameters); fitproper->SetParName(0, "Const.(inner)"); fitproper->SetParName(1, "Mean (inner)"); fitproper->SetParName(2, "Sigma (inner)"); fitproper->SetParName(3, "Const.(outer)"); fitproper->SetParName(4, "Mean (outer)"); fitproper->SetParName(5, "Sigma (outer)"); hist->Fit(fitproper, "Q0R"); fitproper->SetLineColor(hist->GetLineColor()); fitproper->SetLineWidth(hist->GetLineWidth()); fitproper->SetLineStyle(2); return fitproper; }
TF1* lorentzianFit(TH1* histoY, const TString & resonanceType){ TString name = histoY->GetName() + TString("Fit"); // Fit slices projected along Y from bins in X TF1 *fit = 0; if( resonanceType == "JPsi" || resonanceType == "Psi2S" ) { fit = new TF1(name, lorentzian, 3.09, 3.15, 3); } if( resonanceType.Contains("Upsilon") ) { fit = new TF1(name, lorentzian, 9, 10, 3); } if( resonanceType == "Z" ) { fit = new TF1(name, lorentzian, 80, 105, 3); } fit->SetParameters(histoY->GetMaximum(),histoY->GetRMS(),histoY->GetMean()); fit->SetParNames("norm","width","mean"); fit->SetLineWidth(2); histoY->Fit( name,"R0" ); return fit; }
// Simulation void ElectronSimulation::simulate(TrialDataSet& eSimData, float shapingTime, float shapingPower) { // Create modelling function TF1 * func = new TF1("currentFunction", this->currentFunction, 0, 200, 2); func->SetParameters(shapingPower, shapingTime); eSimData.shapingTime = shapingTime; eSimData.shapingPower = shapingPower; // Set uniform random starting time eSimData.startingTime = randomUniform(0.0, period); setMeasurementTimes(eSimData); setChargeValues(eSimData, func); setCurrentValues(eSimData); setVoltageValuesPreNoise(eSimData); setVoltageValuesPostNoise(eSimData); setDigitalReadoutValues(eSimData); }
TF1* GetFitFunc_ZYAM_pp(TH1D* h) { TH1D* hcorrphi = (TH1D*)h->Clone(h->GetName()); // double histminY = hcorrphi->GetBinContent(hcorrphi->GetMinimumBin()); // double histminX = hcorrphi->GetBinCenter(hcorrphi->GetMinimumBin()); double histminY = hcorrphi->GetBinContent(hcorrphi->FindBin(1.2)); double histminX = hcorrphi->GetBinCenter(hcorrphi->FindBin(1.2)); //hcorrphi->SetAxisRange(-0.01,1.2,"X"); TF1* fitfunc = new TF1(Form("fitfunc_%s",h->GetName()),"[0]+[1]*(x-[2])*(x-[2])+[3]*abs((x-[2])*(x-[2])*(x-[2]))",0.5,2.5); //std 0.6 1.55 vs pT ; 0.6 1.8 vs eta fitfunc->SetParameters(histminY,0.002,histminX,0.0); fitfunc->SetParLimits(1,0,1000); fitfunc->SetParLimits(2,0.2,2.0); // fitfunc->SetParLimits(3,0,100); fitfunc->FixParameter(3,0); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%s",h->GetName()),"NO","",histminX-0.6,histminX+0.6); fitfunc->SetName(Form("fitfunc_%s_%.3f",h->GetName(),fitfunc->GetParameter(0))); return fitfunc; }