Clearing a TGraph

Hi there,

Is there a way to somehow clear a TGraph ?
Here’s my problem.
I have this piece of code that works great, but when I run it twice, the output is different and I don’t know why.
The vectors are cleared properly so it doesn’t come from there.

Thank you very much,

  TGraph *gr_kaon = new TGraph(vec_xx_kaon.size(),&vec_xx_kaon[0],&vec_yy_kaon[0]);
  gr_kaon->SetMarkerStyle(21);
  gr_kaon->SetMarkerColor(kBlue-6);
  gr_kaon->SetMarkerSize(0.8);
  gr_kaon->GetXaxis()->SetLimits(-50.,50.);
  gr_kaon->GetYaxis()->SetRangeUser(-50.,50.);

  TF1 *gr_kaon_fit;
  double a_fit_kaon = 0.;
  double b_fit_kaon = 0.;

   if(vec_xx_kaon.size()>0){
    gr_kaon->Fit("pol1","QCM");
    gr_kaon_fit = gr_kaon->GetFunction("pol1");
    gr_kaon_fit->SetLineWidth(2);
    gr_kaon_fit->SetLineColor(4);
    a_fit_kaon = gr_kaon_fit->GetParameter(1);
    b_fit_kaon = gr_kaon_fit->GetParameter(0);
endl;
  }
  cout << "OUTPUT: " << a_fit_kaon << "  " << b_fit_kaon << endl;

Your example is not complete and even incorrect (there is this endl; alone …) …can you post some thing we can run ?
As a suggestion may be you can delete the graph (if it exist) before creating it …

How ?
can I just do someting like gr_kaon->Delete() ??

I can’t give you an example you can run, because the code is REALLY long (this is just a very tiny part of it, but it was to illustrate the problem I have)

As for the “endl;”, it’s just a typo. Sorry

Like any C++ object, using delete:

delete gr_kaon;

In addition, you should not forget that the pointer will then be invalid and should not be used. If a derefernce is performed after delete, you may cause a segmentation violation. Some advocate for setting it to nullptr after calling delete unless it will be used for something else.

delete gr_kaon;
gr_kaon = nullptr;
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.