From 1104c2e0a2866bde7b549f674558b3e5d58a70be Mon Sep 17 00:00:00 2001 From: klenze Date: Sun, 8 Dec 2024 12:28:13 +0100 Subject: [PATCH] New constructors for TAxis, THnSparse and friends * New constructors for TAxis, THnSparse and friends. * Implement review comments --------- Co-authored-by: Jonas Rembser --- hist/hist/inc/TAxis.h | 8 +++++--- hist/hist/inc/THnBase.h | 4 ++++ hist/hist/inc/THnSparse.h | 19 ++++++++++--------- hist/hist/src/TAxis.cxx | 13 ++++--------- hist/hist/src/THnBase.cxx | 13 +++++++++++++ hist/hist/src/THnSparse.cxx | 21 +++++++++++++++++++++ 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/hist/hist/inc/TAxis.h b/hist/hist/inc/TAxis.h index d057e6ae13137..69c8b59806892 100644 --- a/hist/hist/inc/TAxis.h +++ b/hist/hist/inc/TAxis.h @@ -24,6 +24,7 @@ #include "TNamed.h" #include "TAttAxis.h" #include "TArrayD.h" +#include class THashList; class TAxisModLab; @@ -40,9 +41,9 @@ class TAxis : public TNamed, public TAttAxis { UShort_t fBits2; ///< Second bit status word Bool_t fTimeDisplay; ///< On/off displaying time values instead of numerics TString fTimeFormat; ///< Date&time format, ex: 09/12/99 12:34:00 - TObject *fParent; /// const &bins); TAxis(const TAxis &axis); ~TAxis() override; TAxis& operator=(const TAxis&); diff --git a/hist/hist/inc/THnBase.h b/hist/hist/inc/THnBase.h index 47ac7ec4227c2..791d4f8662914 100644 --- a/hist/hist/inc/THnBase.h +++ b/hist/hist/inc/THnBase.h @@ -25,6 +25,8 @@ #include "TFitResultPtr.h" #include "TObjArray.h" #include "TArrayD.h" +#include + class TAxis; class TH1; @@ -63,6 +65,8 @@ class THnBase: public TNamed { THnBase(const char *name, const char *title, Int_t dim, const Int_t *nbins, const Double_t *xmin, const Double_t *xmax); + THnBase(const char* name, const char* title, const std::vector& axes); + THnBase(const char *name, const char *title, Int_t dim, const Int_t *nbins, const std::vector> &xbins); diff --git a/hist/hist/inc/THnSparse.h b/hist/hist/inc/THnSparse.h index 25d4b7eb4e88d..7cbffa9029eee 100644 --- a/hist/hist/inc/THnSparse.h +++ b/hist/hist/inc/THnSparse.h @@ -48,10 +48,6 @@ class THnSparse: public THnBase { protected: - THnSparse(); - THnSparse(const char* name, const char* title, Int_t dim, - const Int_t* nbins, const Double_t* xmin, const Double_t* xmax, - Int_t chunksize); THnSparseCompactBinCoord* GetCompactCoord() const; THnSparseArrayChunk* GetChunk(Int_t idx) const { return (THnSparseArrayChunk*) fBinContent[idx]; } @@ -72,6 +68,15 @@ class THnSparse: public THnBase { void InitStorage(Int_t* nbins, Int_t chunkSize) override; public: + + THnSparse(); + THnSparse(const char* name, const char* title, Int_t dim, + const Int_t* nbins, const Double_t* xmin = nullptr, const Double_t* xmax = nullptr, + Int_t chunksize = 1024 * 16); + THnSparse(const char* name, const char* title, + const std::vector& axes, + Int_t chunksize = 1024 * 16); + ~THnSparse() override; static THnSparse* CreateSparse(const char* name, const char* title, @@ -206,11 +211,7 @@ class THnSparse: public THnBase { template class THnSparseT: public THnSparse { public: - THnSparseT() {} - THnSparseT(const char* name, const char* title, Int_t dim, - const Int_t* nbins, const Double_t* xmin = nullptr, - const Double_t* xmax = nullptr, Int_t chunksize = 1024 * 16): - THnSparse(name, title, dim, nbins, xmin, xmax, chunksize) {} + using THnSparse::THnSparse; TArray* GenerateArray() const override { return new CONT(GetChunkSize()); } private: diff --git a/hist/hist/src/TAxis.cxx b/hist/hist/src/TAxis.cxx index e5dabe3e375ca..60d209a1ea991 100644 --- a/hist/hist/src/TAxis.cxx +++ b/hist/hist/src/TAxis.cxx @@ -54,9 +54,6 @@ TAxis::TAxis() fXmax = 1; fFirst = 0; fLast = 0; - fParent = nullptr; - fLabels = nullptr; - fModLabs = nullptr; fBits2 = 0; fTimeDisplay = false; } @@ -66,9 +63,6 @@ TAxis::TAxis() TAxis::TAxis(Int_t nbins,Double_t xlow,Double_t xup) { - fParent = nullptr; - fLabels = nullptr; - fModLabs = nullptr; Set(nbins,xlow,xup); } @@ -77,12 +71,13 @@ TAxis::TAxis(Int_t nbins,Double_t xlow,Double_t xup) TAxis::TAxis(Int_t nbins,const Double_t *xbins) { - fParent = nullptr; - fLabels = nullptr; - fModLabs = nullptr; Set(nbins,xbins); } +TAxis::TAxis(std::vector const &bins): + TAxis(bins.size()-1, bins.data()) +{} + //////////////////////////////////////////////////////////////////////////////// /// Destructor. diff --git a/hist/hist/src/THnBase.cxx b/hist/hist/src/THnBase.cxx index 3e59a0d3fa10a..c80fa674b2e0b 100644 --- a/hist/hist/src/THnBase.cxx +++ b/hist/hist/src/THnBase.cxx @@ -64,6 +64,19 @@ fIntegral(0), fIntegralStatus(kNoInt) fAxes.SetOwner(); } +THnBase::THnBase(const char* name, const char* title, const std::vector& axes): + TNamed(name, title), fNdimensions(axes.size()), fAxes(axes.size()), fBrowsables(axes.size()), + fEntries(0), fTsumw(0), fTsumw2(-1.), fTsumwx(axes.size()), fTsumwx2(axes.size()), + fIntegral(0), fIntegralStatus(kNoInt) +{ + size_t i{}; + for (auto& a: axes) + fAxes.AddAtAndExpand(a.Clone(), i++); + // Assuming SetTitle is done by TNamed. + fAxes.SetOwner(); +} + + THnBase::THnBase(const char *name, const char *title, Int_t dim, const Int_t *nbins, const std::vector> &xbins) : TNamed(name, title), fNdimensions(dim), fAxes(dim), fBrowsables(dim), fEntries(0), fTsumw(0), fTsumw2(-1.), diff --git a/hist/hist/src/THnSparse.cxx b/hist/hist/src/THnSparse.cxx index 4f61f8f656f5b..338cb4fa56025 100644 --- a/hist/hist/src/THnSparse.cxx +++ b/hist/hist/src/THnSparse.cxx @@ -610,6 +610,27 @@ THnSparse::THnSparse(const char* name, const char* title, Int_t dim, fBinContent.SetOwner(); } +//////////////////////////////////////////////////////////////////////////////// +/// Construct a THnSparse with chunksize as the size of the chunks. +/// "axes" is a vector of TAxis, its size sets the number of dimensions. +/// This method is convenient for passing the Axes on the fly: +/// auto h0= new THnSparseI{"h0", "", {{100, -50., 50.}, {50, -1000., 1000.}}}; + +THnSparse::THnSparse(const char* name, const char* title, + const std::vector& axes, + Int_t chunksize): +THnBase(name, title, axes), + fChunkSize(chunksize), fFilledBins(0), fCompactCoord(nullptr) +{ + const size_t dim=axes.size(); + auto nbins=new Int_t[dim]; + for (size_t i=0; i