Skip to content

Commit

Permalink
New constructors for TAxis, THnSparse and friends
Browse files Browse the repository at this point in the history
* New constructors for TAxis, THnSparse and friends.

* Implement review comments

---------

Co-authored-by: Jonas Rembser <[email protected]>
  • Loading branch information
klenze and guitargeek authored Dec 8, 2024
1 parent 8fcd2a8 commit 1104c2e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 21 deletions.
8 changes: 5 additions & 3 deletions hist/hist/inc/TAxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "TNamed.h"
#include "TAttAxis.h"
#include "TArrayD.h"
#include <vector>

class THashList;
class TAxisModLab;
Expand All @@ -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; ///<! Object owning this axis
THashList *fLabels; ///< List of labels
TList *fModLabs; ///< List of modified labels
TObject *fParent = nullptr; ///<! Object owning this axis
THashList *fLabels = nullptr; ///< List of labels
TList *fModLabs = nullptr; ///< List of modified labels

/// TAxis extra status bits (stored in fBits2)
enum {
Expand Down Expand Up @@ -79,6 +80,7 @@ class TAxis : public TNamed, public TAttAxis {
TAxis();
TAxis(Int_t nbins, Double_t xmin, Double_t xmax);
TAxis(Int_t nbins, const Double_t *xbins);
TAxis(std::vector<double> const &bins);
TAxis(const TAxis &axis);
~TAxis() override;
TAxis& operator=(const TAxis&);
Expand Down
4 changes: 4 additions & 0 deletions hist/hist/inc/THnBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "TFitResultPtr.h"
#include "TObjArray.h"
#include "TArrayD.h"
#include <vector>


class TAxis;
class TH1;
Expand Down Expand Up @@ -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<TAxis>& axes);

THnBase(const char *name, const char *title, Int_t dim, const Int_t *nbins,
const std::vector<std::vector<double>> &xbins);

Expand Down
19 changes: 10 additions & 9 deletions hist/hist/inc/THnSparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]; }
Expand All @@ -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<TAxis>& axes,
Int_t chunksize = 1024 * 16);

~THnSparse() override;

static THnSparse* CreateSparse(const char* name, const char* title,
Expand Down Expand Up @@ -206,11 +211,7 @@ class THnSparse: public THnBase {
template <class CONT>
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:
Expand Down
13 changes: 4 additions & 9 deletions hist/hist/src/TAxis.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ TAxis::TAxis()
fXmax = 1;
fFirst = 0;
fLast = 0;
fParent = nullptr;
fLabels = nullptr;
fModLabs = nullptr;
fBits2 = 0;
fTimeDisplay = false;
}
Expand All @@ -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);
}

Expand All @@ -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<double> const &bins):
TAxis(bins.size()-1, bins.data())
{}

////////////////////////////////////////////////////////////////////////////////
/// Destructor.

Expand Down
13 changes: 13 additions & 0 deletions hist/hist/src/THnBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ fIntegral(0), fIntegralStatus(kNoInt)
fAxes.SetOwner();
}

THnBase::THnBase(const char* name, const char* title, const std::vector<TAxis>& 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<std::vector<double>> &xbins)
: TNamed(name, title), fNdimensions(dim), fAxes(dim), fBrowsables(dim), fEntries(0), fTsumw(0), fTsumw2(-1.),
Expand Down
21 changes: 21 additions & 0 deletions hist/hist/src/THnSparse.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<TAxis>& 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<dim; i++)
nbins[i]=axes.at(i).GetNbins();
fCompactCoord = new THnSparseCompactBinCoord(dim, nbins);
fBinContent.SetOwner();
delete[] nbins;
}

////////////////////////////////////////////////////////////////////////////////
/// Destruct a THnSparse

Expand Down

0 comments on commit 1104c2e

Please sign in to comment.