How to access histogram of TF1 drawn with DrawCopy()


in order to draw a TF1 with different parameter settings to differend TPads in a TCanvas, I used the methode DrawCopy(), since using Draw() would update all pads to the latest parameter setting when updating the TCanvas. Unfortunately it seems that I cannot access the histogram of the TF1 by using TF1::GetHistogram() to configure the style, like it is possible after using Draw().

I already tried as well with gPad->GetListOfPrimitives()->At(…), but also it didn’t work.

Can somebody tell me how to access the histogram, perhaps via the TPad?

Best regards and thanks,

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18-04
Platform: Not Provided
Compiler: Not Provided


DrawCopy returns the new TF1 that is created (the copy), what happens if you invoke GetHistogram on that copy?

Otherwise @couet can give a hand when he is back next week.


thanks for the answer! Accessing the pointer to the copy seems like a good idea I somehow overlooked. However, when I type in ROOT prompt:

root [1] TF1 *f1 = new TF1("f1","gaus",0,10);f1->SetParameters(1,5,0.3)
root [2] f1->Draw()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [3] f1->GetHistogram()->Draw()
root [4] TF1 *f2 = (TF1*)f1->DrawCopy()
(TF1 *) 0x4dc55e0
root [5] f2->Draw()

 *** Break *** segmentation violation

Something seems to be wrong with the returned pointer to the copy…



If you do f2->Draw(), that copy is already drawn in DrawCopy, I don’t know whether that matters, @couet ?

Does it work to do f2->GetHistogram()->Draw() ?

f2->GetHistogram()->Draw() also gives a segfault.

Funny thing, when I do e.g. f2->GetHistogram()->SetTitle("a;b;c") then the titles are updated on the plot, and f2->GetHistogram()->GetEntries() prints 200.000. But f2->Draw() or drawing the histogram directly crashes.

I think some style changes indeed work (SetLabelSize, SetNdivisions,…), since those things are being immediately updated, while others like axis ranges are not.

I see, @couet is back on Monday, so I suggest we wait for him to explain this behaviour better.

I see the crash with:

   TF1 *f1 = new TF1("f1","gaus",0,10);f1->SetParameters(1,5,0.3);
   TF1 *f2 = new TF1();
   f2 = (TF1*)f1->DrawCopy();

Print() is ok … though … weird.

The errors looks to be when drawing again in the same pad. This works:

   TF1 *f1 = new TF1("f1","gaus",0,10);f1->SetParameters(1,5,0.3);
   TF1 * f2 = (TF1*)f1->DrawCopy();
   new TCanvas(); f2->Draw();


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