Skip to content

Commit afb9521

Browse files
bendavideguiraud
authored andcommitted
migrate THn and associated classes from C-style arrays to std::vector
this allows default constructors and destructors to be generated
1 parent a56c0e1 commit afb9521

File tree

5 files changed

+59
-88
lines changed

5 files changed

+59
-88
lines changed

hist/hist/inc/THn.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ class THnSparse;
2828
class TF1;
2929

3030
class THn: public THnBase {
31-
private:
32-
THn(const THn&); // Not implemented
33-
THn& operator=(const THn&); // Not implemented
3431

3532
protected:
3633
void AllocCoordBuf() const;
3734
void InitStorage(Int_t* nbins, Int_t chunkSize);
3835

39-
THn(): fCoordBuf() {}
36+
THn() = default;
4037
THn(const char* name, const char* title, Int_t dim, const Int_t* nbins,
4138
const Double_t* xmin, const Double_t* xmax);
4239

@@ -57,18 +54,20 @@ class THn: public THnBase {
5754
return GetArray().GetBin(idx);
5855
}
5956
Long64_t GetBin(const Double_t* x) const {
60-
if (!fCoordBuf) AllocCoordBuf();
57+
if (fCoordBuf.empty())
58+
AllocCoordBuf();
6159
for (Int_t d = 0; d < fNdimensions; ++d) {
6260
fCoordBuf[d] = GetAxis(d)->FindFixBin(x[d]);
6361
}
64-
return GetArray().GetBin(fCoordBuf);
62+
return GetArray().GetBin(fCoordBuf.data());
6563
}
6664
Long64_t GetBin(const char* name[]) const {
67-
if (!fCoordBuf) AllocCoordBuf();
65+
if (fCoordBuf.empty())
66+
AllocCoordBuf();
6867
for (Int_t d = 0; d < fNdimensions; ++d) {
6968
fCoordBuf[d] = GetAxis(d)->FindBin(name[d]);
7069
}
71-
return GetArray().GetBin(fCoordBuf);
70+
return GetArray().GetBin(fCoordBuf.data());
7271
}
7372

7473
Long64_t GetBin(const Int_t* idx, Bool_t /*allocate*/ = kTRUE) {
@@ -176,7 +175,7 @@ class THn: public THnBase {
176175

177176
protected:
178177
TNDArrayT<Double_t> fSumw2; // bin error, lazy allocation happens in TNDArrayT
179-
mutable Int_t* fCoordBuf; //! Temporary buffer
178+
mutable std::vector<Int_t> fCoordBuf; //! Temporary buffer
180179

181180
ClassDef(THn, 1); //Base class for multi-dimensional histogram
182181
};

hist/hist/inc/THnBase.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,29 @@ class THnBase: public TNamed {
5050
Double_t fTsumw2; ///< Total sum of weights squared; -1 if no errors are calculated
5151
TArrayD fTsumwx; ///< Total sum of weight*X for each dimension
5252
TArrayD fTsumwx2; ///< Total sum of weight*X*X for each dimension
53-
Double_t *fIntegral; ///<! Array with bin weight sums
53+
std::vector<Double_t> fIntegral; ///<! vector with bin weight sums
5454
enum {
5555
kNoInt,
5656
kValidInt,
5757
kInvalidInt
5858
} fIntegralStatus; ///<! status of integral
5959

60-
private:
61-
THnBase(const THnBase&); // Not implemented
62-
THnBase& operator=(const THnBase&); // Not implemented
63-
6460
protected:
65-
THnBase():
66-
fNdimensions(0), fEntries(0),
67-
fTsumw(0), fTsumw2(-1.), fIntegral(0), fIntegralStatus(kNoInt)
68-
{}
69-
70-
THnBase(const char* name, const char* title, Int_t dim,
71-
const Int_t* nbins, const Double_t* xmin, const Double_t* xmax);
72-
73-
void UpdateXStat(const Double_t *x, Double_t w = 1.) {
74-
if (GetCalculateErrors()) {
75-
for (Int_t d = 0; d < fNdimensions; ++d) {
76-
const Double_t xd = x[d];
77-
fTsumwx[d] += w * xd;
78-
fTsumwx2[d] += w * xd * xd;
79-
}
80-
}
81-
}
61+
THnBase() : fNdimensions(0), fEntries(0), fTsumw(0), fTsumw2(-1.), fIntegral(), fIntegralStatus(kNoInt) {}
62+
63+
THnBase(const char *name, const char *title, Int_t dim, const Int_t *nbins, const Double_t *xmin,
64+
const Double_t *xmax);
65+
66+
void UpdateXStat(const Double_t *x, Double_t w = 1.)
67+
{
68+
if (GetCalculateErrors()) {
69+
for (Int_t d = 0; d < fNdimensions; ++d) {
70+
const Double_t xd = x[d];
71+
fTsumwx[d] += w * xd;
72+
fTsumwx2[d] += w * xd * xd;
73+
}
74+
}
75+
}
8276

8377
/// Increment the statistics due to filled weight "w",
8478
void FillBinBase(Double_t w) {

hist/hist/inc/TNDArray.h

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,17 @@ to double: double value = arr[0][1][2];
4545

4646
class TNDArray: public TObject {
4747
public:
48-
TNDArray(): fNdimPlusOne(), fSizes() {}
48+
TNDArray() : fSizes() {}
4949

50-
TNDArray(Int_t ndim, const Int_t* nbins, bool addOverflow = false):
51-
fNdimPlusOne(), fSizes() {
50+
TNDArray(Int_t ndim, const Int_t *nbins, bool addOverflow = false) : fSizes()
51+
{
5252
TNDArray::Init(ndim, nbins, addOverflow);
5353
}
54-
~TNDArray() {
55-
delete[] fSizes;
56-
}
5754

5855
virtual void Init(Int_t ndim, const Int_t* nbins, bool addOverflow = false) {
5956
// Calculate fSize based on ndim dimensions, nbins for each dimension,
6057
// possibly adding over- and underflow bin to each dimensions' nbins.
61-
delete[] fSizes;
62-
fNdimPlusOne = ndim + 1;
63-
fSizes = new Long64_t[ndim + 1];
58+
fSizes.resize(ndim + 1);
6459
Int_t overBins = addOverflow ? 2 : 0;
6560
fSizes[ndim] = 1;
6661
for (Int_t i = 0; i < ndim; ++i) {
@@ -70,14 +65,14 @@ class TNDArray: public TObject {
7065

7166
virtual void Reset(Option_t* option = "") = 0;
7267

73-
Int_t GetNdimensions() const { return fNdimPlusOne - 1; }
68+
Int_t GetNdimensions() const { return fSizes.size() - 1; }
7469
Long64_t GetNbins() const { return fSizes[0]; }
7570
Long64_t GetCellSize(Int_t dim) const { return fSizes[dim + 1]; }
7671

7772
Long64_t GetBin(const Int_t* idx) const {
7873
// Get the linear bin number for each dimension's bin index
79-
Long64_t bin = idx[fNdimPlusOne - 2];
80-
for (Int_t d = 0; d < fNdimPlusOne - 2; ++d) {
74+
Long64_t bin = idx[fSizes.size() - 2];
75+
for (unsigned int d = 0; d < fSizes.size() - 2; ++d) {
8176
bin += fSizes[d + 1] * idx[d];
8277
}
8378
return bin;
@@ -87,14 +82,9 @@ class TNDArray: public TObject {
8782
virtual void SetAsDouble(ULong64_t linidx, Double_t value) = 0;
8883
virtual void AddAt(ULong64_t linidx, Double_t value) = 0;
8984

90-
private:
91-
TNDArray(const TNDArray&); // intentionally not implemented
92-
TNDArray& operator=(const TNDArray&); // intentionally not implemented
93-
9485
protected:
95-
Int_t fNdimPlusOne; ///< Number of dimensions plus one
96-
Long64_t* fSizes; ///<[fNdimPlusOne] bin count
97-
ClassDef(TNDArray, 1); ///< Base for n-dimensional array
86+
std::vector<Long64_t> fSizes; ///< bin count
87+
ClassDef(TNDArray, 2); ///< Base for n-dimensional array
9888
};
9989

10090
template <typename T>
@@ -123,38 +113,25 @@ class TNDArrayRef {
123113
template <typename T>
124114
class TNDArrayT: public TNDArray {
125115
public:
126-
TNDArrayT(): fNumData(), fData() {}
116+
TNDArrayT() : fData() {}
127117

128-
TNDArrayT(Int_t ndim, const Int_t* nbins, bool addOverflow = false):
129-
TNDArray(ndim, nbins, addOverflow),
130-
fNumData(), fData() {
131-
fNumData = fSizes[0];
132-
}
133-
~TNDArrayT() {
134-
delete[] fData;
135-
}
118+
TNDArrayT(Int_t ndim, const Int_t *nbins, bool addOverflow = false) : TNDArray(ndim, nbins, addOverflow), fData() {}
136119

137120
void Init(Int_t ndim, const Int_t* nbins, bool addOverflow = false) {
138-
delete[] fData;
139-
fData = 0;
121+
fData.clear();
140122
TNDArray::Init(ndim, nbins, addOverflow);
141-
fNumData = fSizes[0];
142123
}
143124

144125
void Reset(Option_t* /*option*/ = "") {
145126
// Reset the content
146-
147-
// Use placement-new with value initialization:
148-
if (fData) {
149-
new (fData) T[fNumData]();
150-
}
127+
fData.assign(fSizes[0], T());
151128
}
152129

153130
#ifndef __CINT__
154131
TNDArrayRef<T> operator[](Int_t idx) const {
155132
if (!fData) return TNDArrayRef<T>(0, 0);
156133
R__ASSERT(idx < fSizes[0] / fSizes[1] && "index out of range!");
157-
return TNDArrayRef<T>(fData + idx * fSizes[1], fSizes + 2);
134+
return TNDArrayRef<T>(fData.data() + idx * fSizes[1], fSizes.data() + 2);
158135
}
159136
#endif // __CINT__
160137

@@ -165,31 +142,35 @@ class TNDArrayT: public TNDArray {
165142
return At(GetBin(idx));
166143
}
167144
T At(ULong64_t linidx) const {
168-
if (!fData) return T();
145+
if (fData.empty())
146+
return T();
169147
return fData[linidx];
170148
}
171149
T& At(ULong64_t linidx) {
172-
if (!fData) fData = new T[fNumData]();
150+
if (fData.empty())
151+
fData.resize(fSizes[0], T());
173152
return fData[linidx];
174153
}
175154

176155
Double_t AtAsDouble(ULong64_t linidx) const {
177-
if (!fData) return 0.;
156+
if (fData.empty())
157+
return 0.;
178158
return fData[linidx];
179159
}
180160
void SetAsDouble(ULong64_t linidx, Double_t value) {
181-
if (!fData) fData = new T[fNumData]();
161+
if (fData.empty())
162+
fData.resize(fSizes[0], T());
182163
fData[linidx] = (T) value;
183164
}
184165
void AddAt(ULong64_t linidx, Double_t value) {
185-
if (!fData) fData = new T[fNumData]();
166+
if (fData.empty())
167+
fData.resize(fSizes[0], T());
186168
fData[linidx] += (T) value;
187169
}
188170

189171
protected:
190-
int fNumData; // number of bins, product of fSizes
191-
T* fData; //[fNumData] data
192-
ClassDef(TNDArrayT, 1); // N-dimensional array
172+
std::vector<T> fData; // data
173+
ClassDef(TNDArrayT, 2); // N-dimensional array
193174
};
194175

195176
// FIXME: Remove once we implement https://sft.its.cern.ch/jira/browse/ROOT-6284

hist/hist/src/THn.cxx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ THn::THn(const char* name, const char* title,
192192

193193
THn::~THn()
194194
{
195-
delete [] fCoordBuf;
196195
}
197196

198197

@@ -226,15 +225,15 @@ void THn::Sumw2() {
226225

227226
void THn::AllocCoordBuf() const
228227
{
229-
fCoordBuf = new Int_t[fNdimensions]();
228+
fCoordBuf.assign(fNdimensions, 0);
230229
}
231230

232231
////////////////////////////////////////////////////////////////////////////////
233232
/// Initialize the storage of a histogram created via Init()
234233

235234
void THn::InitStorage(Int_t* nbins, Int_t /*chunkSize*/)
236235
{
237-
fCoordBuf = new Int_t[fNdimensions]();
236+
fCoordBuf.assign(fNdimensions, 0);
238237
GetArray().Init(fNdimensions, nbins, true /*addOverflow*/);
239238
fSumw2.Init(fNdimensions, nbins, true /*addOverflow*/);
240239
}

hist/hist/src/THnBase.cxx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ fIntegral(0), fIntegralStatus(kNoInt)
6666
/// Destruct a THnBase
6767

6868
THnBase::~THnBase() {
69-
if (fIntegralStatus != kNoInt) delete [] fIntegral;
69+
if (fIntegralStatus != kNoInt)
70+
fIntegral.clear();
7071
}
7172

7273

@@ -398,7 +399,7 @@ void THnBase::GetRandom(Double_t *rand, Bool_t subBinRandom /* = kTRUE */)
398399

399400
// generate a random bin
400401
Double_t p = gRandom->Rndm();
401-
Long64_t idx = TMath::BinarySearch(GetNbins() + 1, fIntegral, p);
402+
Long64_t idx = TMath::BinarySearch(GetNbins() + 1, fIntegral.data(), p);
402403
const Int_t nStaticBins = 40;
403404
Int_t bin[nStaticBins];
404405
Int_t* pBin = bin;
@@ -1154,8 +1155,7 @@ void THnBase::ResetBase(Option_t * /*option = ""*/)
11541155
fTsumw = 0.;
11551156
fTsumw2 = -1.;
11561157
if (fIntegralStatus != kNoInt) {
1157-
delete [] fIntegral;
1158-
fIntegral = nullptr;
1158+
fIntegral.clear();
11591159
fIntegralStatus = kNoInt;
11601160
}
11611161
}
@@ -1167,8 +1167,7 @@ Double_t THnBase::ComputeIntegral()
11671167
{
11681168
// delete old integral
11691169
if (fIntegralStatus != kNoInt) {
1170-
delete [] fIntegral;
1171-
fIntegral = nullptr;
1170+
fIntegral.clear();
11721171
fIntegralStatus = kNoInt;
11731172
}
11741173

@@ -1179,7 +1178,7 @@ Double_t THnBase::ComputeIntegral()
11791178
}
11801179

11811180
// allocate integral array
1182-
fIntegral = new Double_t [GetNbins() + 1];
1181+
fIntegral.resize(GetNbins() + 1);
11831182
fIntegral[0] = 0.;
11841183

11851184
// fill integral array with contents of regular bins (non over/underflow)
@@ -1208,8 +1207,7 @@ Double_t THnBase::ComputeIntegral()
12081207
// check sum of weights
12091208
if (fIntegral[GetNbins()] == 0.) {
12101209
Error("ComputeIntegral", "No hits in regular bins (non over/underflow).");
1211-
delete [] fIntegral;
1212-
fIntegral = nullptr;
1210+
fIntegral.clear();
12131211
return 0.;
12141212
}
12151213

0 commit comments

Comments
 (0)