#include #include #include std::vector initialParams = {-9.25005e-06, 0.00201326, 5, -0.000335604, 0.00201326, -2.49909e-07, -5.22527e-11, -5.50176e-14}; Double_t rangeLow = 0; Double_t rangeHigh = 3000; Int_t nPars = 5; Double_t fitf(Double_t *x, Double_t *par) { Double_t X = x[0]; Double_t func = par[0]*TMath::Exp(par[1]*X); Int_t N = (int) par[2]; for( int n = 0; n < N; n++ ) { Double_t monomio = par[3+n]; for( int m = 0; m < n; m++ ) monomio *= x[0]; func += monomio; } return func; } void BxFit_0() { std::ifstream file("outFinal_Xp.txt"); if (!file.is_open()) { std::cerr << "Error: Unable to open file out.txt" << std::endl; return; } // Variables to store data double z, Bx, By, Bz; std::vector xData, yData; // Read data from the file while (file >> z >> Bx >> By >> Bz ) { xData.push_back(z); yData.push_back(Bx); } file.close(); // Create a TGraph object TGraph *graph = new TGraph(xData.size(), &xData[0], &yData[0]); // Optionally customize the appearance of the graph graph->SetLineColor(kBlue); graph->SetLineWidth(1); graph->SetMarkerStyle(20); graph->SetMarkerSize(0.5); // Create a canvas to draw the graph TCanvas *canvas = new TCanvas("canvas", "Graph from ASCII file", 800, 600); // Optionally, add a title and labels graph->SetTitle("Graph from ASCII file"); graph->GetXaxis()->SetTitle("X Axis Title"); graph->GetYaxis()->SetTitle("Y Axis Title"); graph->GetXaxis()->SetLimits(rangeLow,rangeHigh); graph->GetYaxis()->SetRangeUser(-0.01,0); graph->Draw("AP"); // A: Axis, P: Points, L: Lines // Update the canvas canvas->Update(); // Creates a Root function based on function fitf above TF1 *fn = new TF1("fitf", fitf, rangeLow, rangeHigh, 3+nPars); for( int n = 0; n < initialParams.size(); n++ ) fn->SetParameter(n, initialParams[n] ); /////// Fixing or limiting parameters of exponential ////// fn->SetParLimits(0,0.8*initialParams[0],1.2*initialParams[0]); fn->SetParLimits(1,0.8*initialParams[1],1.2*initialParams[1]); //fn->FixParameter(0,initialParams[0]); //fn->FixParameter(1,initialParams[1]); fn->FixParameter(2,nPars); // We do a first fit considering only the ATan function graph->Fit("fitf","R", "", rangeLow, rangeHigh); fn->SetLineColor(kRed); fn->Draw("same"); canvas->Update(); }