modified: GainMatchSX3.C multidimfit now runs but does not work as expected,

the fit curve is empty.
This commit is contained in:
Vignesh Sitaraman 2025-07-09 15:30:12 -04:00
parent a8d4e8f0f6
commit dd2ec66db1

View File

@ -224,6 +224,9 @@ Bool_t GainMatchSX3::Process(Long64_t entry)
void GainMatchSX3::Terminate() void GainMatchSX3::Terminate()
{ {
// --- Store fit coefficients in memory ---
std::map<std::tuple<int, int, int, int>, TVectorD> fitCoefficients;
const int MAX_DET = 24; const int MAX_DET = 24;
const int MAX_UP = 4; const int MAX_UP = 4;
const int MAX_DOWN = 4; const int MAX_DOWN = 4;
@ -240,112 +243,115 @@ void GainMatchSX3::Terminate()
} }
// === Loop over all (id, bk, up, dn) combinations === // === Loop over all (id, bk, up, dn) combinations ===
for (const auto &kv : dataPoints) for (const auto &kv : dataPoints) {
{
auto [id, bk, u, d] = kv.first; auto [id, bk, u, d] = kv.first;
const auto &pts = kv.second; const auto &pts = kv.second;
if (pts.size() < 5) if (pts.size() < 20) continue;
continue;
int N = pts.size(); std::vector<double> x_bk, x_up, y_fsum;
double *x0 = new double[N]; for (const auto &pr : pts) {
double *x1 = new double[N];
double *x2 = new double[N];
double *y = new double[N];
int mPowers[] = {1,1,1};
// Fill arrays
for (int i = 0; i < N; ++i)
{
double eBk, eUp, eDn; double eBk, eUp, eDn;
std::tie(eBk, eUp, eDn) = pts[i]; std::tie(eBk, eUp, eDn) = pr;
x0[i] = eBk; if (eBk > 0 && eUp > 0 && eDn > 0) {
x1[i] = eUp; x_bk.push_back(eBk);
x2[i] = eDn; x_up.push_back(eUp);
y[i] = eUp + eDn; // Target is front sum y_fsum.push_back(eUp + eDn);
}
} }
// Build MultiDim Fit int nPoints = y_fsum.size();
TMultiDimFit *mdf = new TMultiDimFit(3, TMultiDimFit::kMonomials, "v"); if (nPoints < 20) continue;
mdf->SetMaxPowers(mPowers); // Up to quadratic terms
mdf->SetMaxTerms(3); // Limit number of terms kept
// Add points TMultiDimFit *mdf = new TMultiDimFit(2, TMultiDimFit::kMonomials);
for (int i = 0; i < N; ++i) mdf->SetMaxPowers(new Int_t[2]{1, 1});
{ mdf->SetMinAngle(10);
double vars[3] = {x0[i], x1[i], x2[i]}; mdf->SetMinRelativeError(1e-4);
mdf->AddRow(vars, y[i]);
double *x_row = new double[2];
for (int i = 0; i < nPoints; ++i) {
x_row[0] = x_bk[i];
x_row[1] = x_up[i];
mdf->AddRow(x_row, y_fsum[i]);
}
delete[] x_row;
mdf->Fit();
const TVectorD *coeffs = mdf->GetCoefficients();
if (!coeffs || coeffs->GetNoElements() == 0 || !TMath::Finite((*coeffs)(0))) {
std::cerr << "Fit failed for Det" << id << " B" << bk << " U" << u << " D" << d << std::endl;
delete mdf;
continue;
} }
mdf->MakeHistograms(); // Store coefficients in the map and write to file
mdf->FindParameterization(); fitCoefficients[kv.first] = *coeffs;
mdf->Print("a"); // Print coefficients
mdf->
// Save result string
TString formula;
mdf->GetFunctions(formula);
outFile << id << " " << bk << " " << u << " " << d << " " << formula.Data() << std::endl;
printf("Det %d Bk %d Up %d Dn %d — MultiDimFit formula: %s\n", id, bk, u, d, formula.Data());
gainValid[id][bk][u][d] = true; // Save as "valid" so we can use it int nCoeffs = mdf->GetNCoefficients();
outFile << id << " " << bk << " " << u << " " << d;
printf("Fit for Det%d B%d U%d D%d -> ", id, bk, u, d);
for (int i = 0; i < nCoeffs; ++i) {
outFile << " " << (*coeffs)(i);
printf("p%d: %.4f ", i, (*coeffs)(i));
}
outFile << std::endl;
printf("\n");
// Clean up
delete[] x0;
delete[] x1;
delete[] x2;
delete[] y;
delete mdf; delete mdf;
} }
outFile.close(); outFile.close();
std::cout << "MultiDim fits complete.\n"; std::cout << "Multi-dimensional gain matching complete. Results saved." << std::endl;
// === Example histogram after correction === // --- Stage 2: Apply corrections and create new histograms ---
TH2F *hFVB = new TH2F("hFVB", "Corrected Up+Dn vs Back;Back E;Corrected Up+Dn E", std::cout << "--- Stage 2: Applying Corrections and Visualizing Results ---" << std::endl;
400, 0, 16000, 400, 0, 16000); TH2F *hCorrectedFvB = new TH2F("hCorrectedFvB", "Gain Corrected Data;Predicted Front Sum (from fit);Measured Front Sum", 400, 0, 16000, 400, 0, 16000);
for (const auto &kv : dataPoints) for (const auto &kv : dataPoints) {
{ // Find the coefficients for this segment
auto [id, bk, u, d] = kv.first; if (fitCoefficients.find(kv.first) == fitCoefficients.end()) {
if (!gainValid[id][bk][u][d]) continue; // Skip if no valid fit was found
continue; }
const TVectorD& coeffs = fitCoefficients[kv.first];
double p0 = coeffs(0);
double p1 = coeffs(1);
double p2 = coeffs(2);
// Recreate the fitted model to evaluate // Loop over the data points for this segment and apply the correction
TMultiDimFit *mdf = new TMultiDimFit(3, TMultiDimFit::kMonomials, "v");
mdf->SetMaxPowers(2);
mdf->SetMaxTerms(10);
// Re-fill points to refit (if needed — or you can serialize coefficients instead)
const auto &pts = kv.second; const auto &pts = kv.second;
for (const auto &pr : pts) for (const auto &pr : pts) {
{
double eBk, eUp, eDn; double eBk, eUp, eDn;
std::tie(eBk, eUp, eDn) = pr; std::tie(eBk, eUp, eDn) = pr;
double vars[3] = {eBk, eUp, eDn};
double y = eUp + eDn; // Calculate the predicted front sum using the fit parameters
mdf->AddRow(vars, y); double predicted_front_sum = p0 + p1 * eBk + p2 * eUp;
// The measured front sum is just the raw sum
double measured_front_sum = eUp + eDn;
// Fill the corrected histogram
hCorrectedFvB->Fill(predicted_front_sum, measured_front_sum);
}
} }
mdf->FindParameterization(); // --- Stage 3: Draw the comparison canvases ---
gStyle->SetOptStat(1110);
TCanvas *c1 = new TCanvas("c1", "Gain Correction Results", 1200, 600);
c1->Divide(2, 1);
// Fill histogram with corrected "front" from model c1->cd(1);
for (const auto &pr : kv.second) hSX3FvsB_g->SetTitle("Before Correction (Gated)");
{ hSX3FvsB_g->GetXaxis()->SetTitle("Measured Front Sum (E_Up + E_Dn)");
double eBk, eUp, eDn; hSX3FvsB_g->GetYaxis()->SetTitle("Measured Back E");
std::tie(eBk, eUp, eDn) = pr; hSX3FvsB_g->Draw("colz");
double vars[3] = {eBk, eUp, eDn};
double correctedFront = mdf->Eval(vars);
if (eBk == 0 || correctedFront == 0)
continue;
hFVB->Fill(eBk, correctedFront);
}
delete mdf; c1->cd(2);
} hCorrectedFvB->SetTitle("After Correction");
hCorrectedFvB->Draw("colz");
// Save histogram if needed // Draw a perfect y=x line for comparison
// TFile *outHist = new TFile("sx3_multidimfit_hists.root", "RECREATE"); TF1 *diag = new TF1("diag", "x", 0, 16000);
// hFVB->Write(); diag->SetLineColor(kRed);
// outHist->Close(); diag->SetLineWidth(2);
diag->Draw("same");
} }