From c7e80833bd99de8db361c31fd060df08b2495d08 Mon Sep 17 00:00:00 2001 From: Olivier Couet Date: Thu, 20 Feb 2020 10:33:57 +0100 Subject: [PATCH 1/4] Make sure changed labels are kept after a clone (fix ROOT-10580) --- hist/hist/inc/TF1.h | 4 +++- hist/hist/src/TAxis.cxx | 12 ++++++++++++ hist/hist/src/TF1.cxx | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/hist/hist/inc/TF1.h b/hist/hist/inc/TF1.h index 322862f389700..2f7215aa8081c 100644 --- a/hist/hist/inc/TF1.h +++ b/hist/hist/inc/TF1.h @@ -413,6 +413,7 @@ class TF1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { static Bool_t DefaultAddToGlobalList(Bool_t on = kTRUE); virtual void Browse(TBrowser *b); virtual void Copy(TObject &f1) const; + TObject* Clone(const char* newname=0) const; virtual Double_t Derivative(Double_t x, Double_t *params = 0, Double_t epsilon = 0.001) const; virtual Double_t Derivative2(Double_t x, Double_t *params = 0, Double_t epsilon = 0.001) const; virtual Double_t Derivative3(Double_t x, Double_t *params = 0, Double_t epsilon = 0.001) const; @@ -612,6 +613,7 @@ class TF1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { void SetFunction(PtrObj &p, MemFn memFn); template void SetFunction(Func f); + virtual void SetHistogram(TH1 *h) {fHistogram = h;} virtual void SetMaximum(Double_t maximum = -1111); // *MENU* virtual void SetMinimum(Double_t minimum = -1111); // *MENU* virtual void SetNDF(Int_t ndf); @@ -774,7 +776,7 @@ T TF1::EvalPar(const T *x, const Double_t *params) //////////////////////////////////////////////////////////////////////////////// /// Eval for vectorized functions // template -// T TF1::Eval(T x, T y, T z, T t) const +// T TF1::Eval(T x, T y, T z, T t) const // { // if (fType == EFType::kFormula) // return fFormula->Eval(x, y, z, t); diff --git a/hist/hist/src/TAxis.cxx b/hist/hist/src/TAxis.cxx index e5a306db04354..29092f2e455b5 100644 --- a/hist/hist/src/TAxis.cxx +++ b/hist/hist/src/TAxis.cxx @@ -243,6 +243,18 @@ void TAxis::Copy(TObject &obj) const delete axis.fModLabs; axis.fModLabs = 0; } + if (fModLabs) { + TIter next(fModLabs); + TAxisModLab *modlabel; + if(! axis.fModLabs) { + axis.fModLabs = new TList(); + } + while( (modlabel=(TAxisModLab*)next()) ) { + TAxisModLab *copyModLabel = new TAxisModLab(*modlabel); + axis.fModLabs->Add(copyModLabel); + copyModLabel->SetUniqueID(modlabel->GetUniqueID()); + } + } } //////////////////////////////////////////////////////////////////////////////// diff --git a/hist/hist/src/TF1.cxx b/hist/hist/src/TF1.cxx index 53f2471f2394a..03c53b5b2ca7e 100644 --- a/hist/hist/src/TF1.cxx +++ b/hist/hist/src/TF1.cxx @@ -1059,6 +1059,25 @@ void TF1::Copy(TObject &obj) const } +//////////////////////////////////////////////////////////////////////////////// +/// Make a complete copy of the underlying object. If 'newname' is set, +/// the copy's name will be set to that name. + +TObject* TF1::Clone(const char* newname) const +{ + + TF1* obj = (TF1*)IsA()->GetNew()(0); + Copy(*obj); + + if(newname && strlen(newname) ) { + obj->SetName(newname); + } + + obj->SetHistogram(fHistogram); + return obj; +} + + //////////////////////////////////////////////////////////////////////////////// /// Returns the first derivative of the function at point x, /// computed by Richardson's extrapolation method (use 2 derivative estimates From 3b511341ac5daf4a467a848f0c011d95bb7dfd1c Mon Sep 17 00:00:00 2001 From: Olivier Couet Date: Thu, 20 Feb 2020 16:39:03 +0100 Subject: [PATCH 2/4] new version using TObject::Clone... --- hist/hist/inc/TF1.h | 1 - hist/hist/src/TF1.cxx | 11 ++++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/hist/hist/inc/TF1.h b/hist/hist/inc/TF1.h index 2f7215aa8081c..4901a5039b38d 100644 --- a/hist/hist/inc/TF1.h +++ b/hist/hist/inc/TF1.h @@ -613,7 +613,6 @@ class TF1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { void SetFunction(PtrObj &p, MemFn memFn); template void SetFunction(Func f); - virtual void SetHistogram(TH1 *h) {fHistogram = h;} virtual void SetMaximum(Double_t maximum = -1111); // *MENU* virtual void SetMinimum(Double_t minimum = -1111); // *MENU* virtual void SetNDF(Int_t ndf); diff --git a/hist/hist/src/TF1.cxx b/hist/hist/src/TF1.cxx index 03c53b5b2ca7e..9fa6f3725f469 100644 --- a/hist/hist/src/TF1.cxx +++ b/hist/hist/src/TF1.cxx @@ -1066,14 +1066,11 @@ void TF1::Copy(TObject &obj) const TObject* TF1::Clone(const char* newname) const { - TF1* obj = (TF1*)IsA()->GetNew()(0); - Copy(*obj); - - if(newname && strlen(newname) ) { - obj->SetName(newname); + TF1* obj = (TF1*) TObject::Clone(newname); + if (fHistogram) { + obj->fHistogram = (TH1*)fHistogram->IsA()->GetNew()(0); + fHistogram->Copy(*obj->fHistogram); } - - obj->SetHistogram(fHistogram); return obj; } From a011b61aba1d19f7a36f87637ff2473a05ca39b0 Mon Sep 17 00:00:00 2001 From: Olivier Couet Date: Fri, 21 Feb 2020 13:52:00 +0100 Subject: [PATCH 3/4] Fix to avoid double delete of fHistogram --- hist/hist/src/TF1.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/hist/hist/src/TF1.cxx b/hist/hist/src/TF1.cxx index 9fa6f3725f469..e89ce73899398 100644 --- a/hist/hist/src/TF1.cxx +++ b/hist/hist/src/TF1.cxx @@ -1063,14 +1063,16 @@ void TF1::Copy(TObject &obj) const /// Make a complete copy of the underlying object. If 'newname' is set, /// the copy's name will be set to that name. -TObject* TF1::Clone(const char* newname) const +TObject* TF1::Clone(const char*) const { - TF1* obj = (TF1*) TObject::Clone(newname); + TF1* obj = (TF1*) TObject::Clone(); + if (fHistogram) { - obj->fHistogram = (TH1*)fHistogram->IsA()->GetNew()(0); - fHistogram->Copy(*obj->fHistogram); + obj->fHistogram = (TH1*)fHistogram->Clone(); + obj->fHistogram->SetDirectory(0); } + return obj; } From 77395a9fe68ad5fba0e267c956cb1fb966758f16 Mon Sep 17 00:00:00 2001 From: Olivier Couet Date: Fri, 21 Feb 2020 14:28:03 +0100 Subject: [PATCH 4/4] Use TNamed::Clone remove spaces --- hist/hist/src/TF1.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hist/hist/src/TF1.cxx b/hist/hist/src/TF1.cxx index e89ce73899398..69e125a04d331 100644 --- a/hist/hist/src/TF1.cxx +++ b/hist/hist/src/TF1.cxx @@ -951,7 +951,7 @@ TF1::~TF1() if (fFormula) delete fFormula; if (fParams) delete fParams; - if (fFunctor) delete fFunctor; + if (fFunctor) delete fFunctor; } @@ -1066,7 +1066,7 @@ void TF1::Copy(TObject &obj) const TObject* TF1::Clone(const char*) const { - TF1* obj = (TF1*) TObject::Clone(); + TF1* obj = (TF1*) TNamed::Clone(); if (fHistogram) { obj->fHistogram = (TH1*)fHistogram->Clone();