SetOption Method in TGraph/TGraphErrors

Hi,

When working with histograms I can call the method TH1::SetOption to fix the default option when the histogram are drawn. I wonder which method I can use to achieve the same purpose when using TGraph/TGraphError objects. I tried this:

TGraph *GetGraph(){

  Int_t y[4] = {5,9,8,10};
  Int_t x[4] = {1,2,3,4};

  TGraph *gr = new TGraph(4,x,y);
  gr->Draw("goff");
  gr->SetDrawOption("AB");
  //gr->SetOption("AB");

  return gr;

}

TGraph *fgr = GetGraph();
fgr->Draw();

But it looks like I can’t use "goff" in this context and SetOption() is not available in TGraph,

Best,

goff does not exists for TGraph. All the possible option are documented
.
There no equivalent of the histogram method SetOption.

SetDrawOption works if you Draw() before calling SetDrawOption.

Thank you @couet, Is there any chance to implement SetOption on the TGraph Class? I’m not sure about how to implement your suggestion in this context (returning a pointer to a TGraph object). For instance Test.C (278 Bytes) produces a “Break Illegal construction message” I guess it is because I don’t have a TCanvas and I don’t want to create a canvas in the GetGraph function.

1 Like

I would second this.

2 Likes

I can have a look at it.

Seems to me SetDrawOption does what you need:

TGraph *GetGraph(){

  Int_t y[4] = {5,9,8,10};
  Int_t x[4] = {1,2,3,4};

  TGraph *gr = new TGraph(4,x,y);
  gr->Draw();
  gr->SetDrawOption("AB");

  return gr;

}

void Test(){
   TGraph *fgr = GetGraph();
   fgr->SetFillColor(kBlue);
   fgr->Print("AB.png");
}

I believe the point here is to avoid drawing the graph. You would like to have a generator function that creates the graph and sets the default draw options.

1 Like

So you would like something like that drawing a bar chart ???

TGraph *GetGraph(){

  Int_t y[4] = {5,9,8,10};
  Int_t x[4] = {1,2,3,4};

  TGraph *gr = new TGraph(4,x,y);
  gr->SetDrawOption("AB");

  return gr;

}

void Test(){
   TGraph *fgr = GetGraph();
   fgr->SetFillColor(kBlue);
   fgr->Draw();
}

Exactly. Right now you get a line graph.

Yes because calling Draw() without option means “use default option” . So if we do what you are looking for I am afraid some unwanted side effects may quickly show because the meaning of “not specifying the option” will change … So that change has to be checked carefully . May be you can open a Jira Ticket with it to not lose track ?

Just to clarify, we re trying to mimic the behavior for TH1 via TH1::SetOption with TGraph objects. You believe this will lead to unforeseen issues.

Yes I got that.

I think the default behavior should be looked closely to not break existing macros.
But surely something can be done … For instance if the default option set by SetDrawOption is not empty then we use it instead of the default option . I guess that should fulfil the requirement.

I opened a ticket: https://sft.its.cern.ch/jira/browse/ROOT-9138

2 Likes

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