Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-21 09:30:30

0001 #include "FitCurveDrawing.h"
0002 #include <TCanvas.h>
0003 #include <TROOT.h>
0004 #include <iostream>
0005 #include <memory>
0006 #include <string>
0007 
0008 // --legacy is a manual negative control: reproduces the pre-fix dangling
0009 // source-model legend entry and fails before attempting its unsafe repaint.
0010 int main(int argc, char** argv) {
0011   gROOT->SetBatch(true);
0012   const bool legacy = argc > 1 && std::string(argv[1]) == "--legacy";
0013   TCanvas canvas("legend_lifetime", "", 600, 400);
0014   for (int layer = 0; layer < 10; ++layer) {
0015     canvas.Clear();
0016     canvas.cd();
0017     auto* legend = new TLegend(.5, .5, .9, .9);
0018     legend->SetBit(TObject::kCanDelete);
0019     TLegendEntry* entry = nullptr;
0020     {
0021       TF1 fit("temporary_layer_fit", "[0]+[1]*x+[2]+[3]", 0., 20.);
0022       fit.SetParameters(2., 3., 4., 5.);
0023       fit.SetLineColor(kRed+3); fit.SetLineStyle(7); fit.SetLineWidth(3);
0024       fit.SetNpx(23);
0025       auto* curve = lfhcal::DrawMipFit(fit, 0., 20.);
0026       if (!curve || curve->GetN() < 1000 || fit.GetNpx() != 23) return 1;
0027       for (int i = 0; i < curve->GetN(); ++i) {
0028         double x, y; curve->GetPoint(i, x, y);
0029         if (y != fit.Eval(x)) return 2;
0030       }
0031       entry = legacy ? legend->AddEntry(&fit, "Landau-Gauss fit", "l")
0032                      : lfhcal::AddMipFitLegendEntry(*legend, fit, "Landau-Gauss fit");
0033       legend->Draw();
0034       canvas.Modified(); canvas.Update();
0035     } // The source TF1 is gone, exactly as after a by-value layer helper.
0036     if (entry->GetObject() != nullptr) {
0037       std::cerr << "FAIL: legend retains a destroyed source TF1\n";
0038       return 3;
0039     }
0040     if (entry->GetLineColor() != kRed+3 || entry->GetLineStyle() != 7 ||
0041         entry->GetLineWidth() != 3 || std::string(entry->GetLabel()) != "Landau-Gauss fit") return 4;
0042     canvas.Modified(); canvas.Update(); // Former next-layer redraw crash.
0043   }
0044   canvas.Clear();
0045   std::cout << "Fit-legend lifetime, style and live-curve values passed across 10 layers\n";
0046   return 0;
0047 }