#include #include #include "TGraph2D.h" #include "TCanvas.h" //#include "TFitFunc.h" #include "TFitResult.h" void FitMuonTrajectory(const char* filename) { // Open the file std::ifstream infile("outputevents_xyz.txt"); if (!infile) { std::cerr << "Error opening file: " << filename << std::endl; return; } // Arrays to store coordinates double x[3], y[3], z[3]; // Read coordinates from file for (int i = 0; i < 3; ++i) { infile >> x[i] >> y[i] >> z[i]; } // Close the file infile.close(); // Create a TGraph2D to hold the points TGraph2D *graph = new TGraph2D(3, x, y, z); graph->SetMarkerStyle(20); graph->SetMarkerSize(1.5); // Create a TFitFunc to fit a straight line (z = a*x + b*y + c) TF1 *fit = new TF1("fitFunc", "[0] + [1]*x + [2]*y",-100, 100); // Fit the function to the graph TFitResultPtr fitResult = graph->Fit(fitFunc, "Q"); // Check if the fit converged successfully if (fitResult.Get() != nullptr && fitResult->IsValid()) { // Print fit results std::cout << "Fit parameters: intercept = " << fitFunc->GetParameter(0) << ", slope_x = " << fitFunc->GetParameter(1) << ", slope_y = " << fitFunc->GetParameter(2) << std::endl; } else { std::cerr << "Fit failed or did not converge!" << std::endl; }*/ // Draw the graph and the fit TCanvas *canvas = new TCanvas("canvas", "Muon Trajectory Fit", 800, 600); graph->Draw("P"); fitFunc->Draw("same"); canvas->Draw(); }