Skip to content

Commit ff7482e

Browse files
committed
Fix several issues with TScatter
1. Memory leak in TScatter constructor when not col/size specified 2. Store margin and scale into C script 3. Restore hist name after storing into C script 4. Provide initializers for all members 5. Directly set proper range for axis historgam
1 parent a42ee3f commit ff7482e

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

hist/hist/inc/TScatter.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ class TH2F;
3232
class TScatter : public TNamed, public TAttLine, public TAttFill, public TAttMarker {
3333

3434
protected:
35-
Int_t fMaxSize; ///<!Current dimension of arrays fX and fY
36-
Int_t fNpoints; ///< Number of points <= fMaxSize
35+
Int_t fMaxSize{-1}; ///<!Current dimension of arrays fX and fY
36+
Int_t fNpoints{-1}; ///< Number of points <= fMaxSize
3737
TH2F *fHistogram{nullptr}; ///< Pointer to histogram used for drawing axis
3838
TGraph *fGraph{nullptr}; ///< Pointer to graph holding X and Y positions
3939
Double_t *fColor{nullptr}; ///< [fNpoints] array of colors
4040
Double_t *fSize{nullptr}; ///< [fNpoints] array of marker sizes
41-
Double_t fScale; ///< Largest marker size used to paint the markers
42-
Double_t fMargin; ///< Margin around the plot in %
41+
Double_t fScale{5.}; ///< Largest marker size used to paint the markers
42+
Double_t fMargin{.1}; ///< Margin around the plot in %
4343

4444
public:
4545
TScatter();

hist/hist/src/TScatter.cxx

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,8 @@ TScatter can be drawn with the following options:
5757

5858
TScatter::TScatter()
5959
{
60-
fNpoints = -1;
61-
fMaxSize = -1;
62-
63-
fGraph = nullptr;
64-
fHistogram = nullptr;
65-
fScale = 5.;
66-
fMargin = 0.1;
67-
fSize = nullptr;
68-
fColor = nullptr;
6960
}
7061

71-
7262
////////////////////////////////////////////////////////////////////////////////
7363
/// TScatter normal constructor.
7464
///
@@ -79,7 +69,6 @@ TScatter::TScatter(Int_t n)
7969
fGraph = new TGraph(n);
8070
fNpoints = fGraph->GetN();
8171
fMaxSize = fGraph->GetMaxSize();
82-
fHistogram = nullptr;
8372

8473
fColor = new Double_t[fMaxSize];
8574
fSize = new Double_t[fMaxSize];
@@ -101,16 +90,16 @@ TScatter::TScatter(Int_t n, const Double_t *x, const Double_t *y, const Double_t
10190
fGraph = new TGraph(n, x, y);
10291
fNpoints = fGraph->GetN();
10392
fMaxSize = fGraph->GetMaxSize();
104-
fHistogram = nullptr;
105-
106-
fColor = new Double_t[fMaxSize];
107-
fSize = new Double_t[fMaxSize];
10893

109-
n = sizeof(Double_t) * fNpoints;
110-
if (col) memcpy(fColor, col, n);
111-
else fColor = nullptr;
112-
if (size) memcpy(fSize, size, n);
113-
else fSize = nullptr;
94+
Int_t bufsize = sizeof(Double_t) * fNpoints;
95+
if (col) {
96+
fColor = new Double_t[fMaxSize];
97+
memcpy(fColor, col, bufsize);
98+
}
99+
if (size) {
100+
fSize = new Double_t[fMaxSize];
101+
memcpy(fSize, size, bufsize);
102+
}
114103

115104
fScale = 5.;
116105
fMargin = 0.1;
@@ -138,8 +127,9 @@ TScatter::~TScatter()
138127
Int_t TScatter::DistancetoPrimitive(Int_t px, Int_t py)
139128
{
140129
TVirtualGraphPainter *painter = TVirtualGraphPainter::GetPainter();
141-
if (painter) return painter->DistancetoPrimitiveHelper(this->GetGraph(), px, py);
142-
else return 0;
130+
if (painter)
131+
return painter->DistancetoPrimitiveHelper(this->GetGraph(), px, py);
132+
return 0;
143133
}
144134

145135

@@ -170,23 +160,19 @@ TH2F *TScatter::GetHistogram() const
170160
// do not add the histogram to gDirectory
171161
// use local TDirectory::TContect that will set temporarly gDirectory to a nullptr and
172162
// will avoid that histogram is added in the global directory
173-
{
174-
TDirectory::TContext ctx(nullptr);
175-
double rwxmin, rwymin, rwxmax, rwymax;
176-
int npt = 50;
177-
fGraph->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
178-
double dx = (rwxmax-rwxmin)*fMargin;
179-
double dy = (rwymax-rwymin)*fMargin;
180-
auto h = new TH2F(TString::Format("%s_h",GetName()),GetTitle(),npt,rwxmin,rwxmax,npt,rwymin,rwymax);
163+
TDirectory::TContext ctx(nullptr);
164+
double rwxmin, rwymin, rwxmax, rwymax;
165+
int npt = 50;
166+
fGraph->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
167+
double dx = (rwxmax-rwxmin)*fMargin;
168+
double dy = (rwymax-rwymin)*fMargin;
169+
auto h = new TH2F(TString::Format("%s_h",GetName()),GetTitle(),npt,rwxmin-dx,rwxmax+dx,npt,rwymin-dy,rwymax+dy);
181170
// h->SetMinimum(rwymin-dy);
182171
// h->SetMaximum(rwymax+dy);
183-
h->GetXaxis()->SetLimits(rwxmin-dx,rwxmax+dx);
184-
h->GetYaxis()->SetLimits(rwymin-dy,rwymax+dy);
185-
h->SetBit(TH1::kNoStats);
186-
h->SetDirectory(0);
187-
h->Sumw2(kFALSE);
188-
((TScatter*)this)->fHistogram = h;
189-
}
172+
h->SetBit(TH1::kNoStats);
173+
h->SetDirectory(nullptr);
174+
h->Sumw2(kFALSE);
175+
const_cast<TScatter *>(this)->fHistogram = h;
190176
}
191177
return fHistogram;
192178
}
@@ -258,27 +244,29 @@ void TScatter::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
258244
for (i = 0; i < fNpoints-1; i++) out << " " << fSize[i] << "," << std::endl;
259245
out << " " << fSize[fNpoints-1] << "};" << std::endl;
260246

261-
if (gROOT->ClassSaved(TScatter::Class())) out << " ";
262-
else out << " TScatter *";
263-
out << "scat = new TScatter(" << fNpoints << ","
264-
<< fXName << "," << fYName << ","
265-
<< fColorName << "," << fSizeName << ");"
266-
<< std::endl;
247+
if (gROOT->ClassSaved(TScatter::Class()))
248+
out << " ";
249+
else
250+
out << " TScatter *";
251+
out << "scat = new TScatter(" << fNpoints << "," << fXName << "," << fYName << ","
252+
<< fColorName << "," << fSizeName << ");" << std::endl;
267253

268254
out << " scat->SetName(" << quote << GetName() << quote << ");" << std::endl;
269255
out << " scat->SetTitle(" << quote << GetTitle() << quote << ");" << std::endl;
256+
out << " scat->SetMargin(" << GetMargin() << ");" << std::endl;
257+
out << " scat->SetScale(" << GetScale() << ");" << std::endl;
270258

271259
SaveFillAttributes(out, "scat", 0, 1001);
272260
SaveLineAttributes(out, "scat", 1, 1, 1);
273261
SaveMarkerAttributes(out, "scat", 1, 1, 1);
274262

275263
if (fHistogram) {
276264
TString hname = fHistogram->GetName();
277-
hname += frameNumber;
278-
fHistogram->SetName(TString::Format("Graph_%s", hname.Data()));
265+
fHistogram->SetName(TString::Format("Graph_%s%d", hname.Data(), frameNumber));
279266
fHistogram->SavePrimitive(out, "nodraw");
280267
out << " scat->SetHistogram(" << fHistogram->GetName() << ");" << std::endl;
281268
out << " " << std::endl;
269+
fHistogram->SetName(hname);
282270
}
283271

284272
out << " scat->Draw(" << quote << option << quote << ");" << std::endl;

0 commit comments

Comments
 (0)