Skip to content

Commit d5fe25f

Browse files
authored
Fix TF1::DrawCopy issue (#19627)
* Implement and use TF1::SetHistogram * Add SetDirectory(nullptr); * Add a test * clang format
1 parent 868cbb0 commit d5fe25f

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

hist/hist/src/TF1.cxx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,13 @@ void TF1::Copy(TObject &obj) const
10291029
((TF1 &)obj).fParMax = fParMax;
10301030
((TF1 &)obj).fParent = fParent;
10311031
((TF1 &)obj).fSave = fSave;
1032-
((TF1 &)obj).fHistogram = nullptr;
1032+
if (fHistogram) {
1033+
auto *h1 = (TH1 *)fHistogram->Clone();
1034+
h1->SetDirectory(nullptr);
1035+
((TF1 &)obj).fHistogram = h1;
1036+
} else {
1037+
((TF1 &)obj).fHistogram = nullptr;
1038+
}
10331039
((TF1 &)obj).fMethodCall = nullptr;
10341040
((TF1 &)obj).fNormalized = fNormalized;
10351041
((TF1 &)obj).fNormIntegral = fNormIntegral;

hist/hist/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ROOT_ADD_GTEST(testMapCppName test_MapCppName.cxx LIBRARIES Hist Gpad)
2828
ROOT_ADD_GTEST(testTGraphSorting test_TGraph_sorting.cxx LIBRARIES Hist)
2929
ROOT_ADD_GTEST(testSpline test_spline.cxx LIBRARIES Hist)
3030
ROOT_ADD_GTEST(testTF1Simple test_tf1_simple.cxx LIBRARIES Hist RIO)
31+
ROOT_ADD_GTEST(testTF1DrawCopy test_tf1_drawcopy.cxx LIBRARIES Hist)
3132

3233
if(fftw3)
3334
ROOT_ADD_GTEST(testTF1 test_tf1.cxx LIBRARIES Hist)

hist/hist/test/test_tf1_drawcopy.cxx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "TF1.h"
2+
#include "TAxis.h"
3+
4+
#include "gtest/gtest.h"
5+
6+
// issue #13122
7+
TEST(TF1, DrawCopy)
8+
{
9+
auto f = new TF1("f", "x*x", -3, 3);
10+
f->GetXaxis()->SetTitle("xtitle");
11+
auto fcopy = f->DrawCopy();
12+
EXPECT_STREQ("xtitle", fcopy->GetXaxis()->GetTitle());
13+
}

0 commit comments

Comments
 (0)