Logo ROOT  
Reference Guide
TGraph2DErrors.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id: TGraph2DErrors.cxx,v 1.00
2// Author: Olivier Couet
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <iostream>
13#include "TBuffer.h"
14#include "TGraph2DErrors.h"
15#include "TH2.h"
16#include "TVirtualPad.h"
17#include "TVirtualFitter.h"
18#include "THLimitsFinder.h"
19
21
22/** \class TGraph2DErrors
23 \ingroup Graphs
24Graph 2D class with errors.
25
26A TGraph2DErrors is a TGraph2D with errors. It behaves like a TGraph2D and has
27the same drawing options.
28
29The **"ERR"** drawing option allows to display the error bars. The
30following example shows how to use it:
31
32Begin_Macro(source)
33{
34 auto c = new TCanvas("c","TGraph2DErrors example",0,0,600,600);
35 Double_t P = 6.;
36 Int_t np = 200;
37
38 Double_t *rx=0, *ry=0, *rz=0;
39 Double_t *ex=0, *ey=0, *ez=0;
40
41 rx = new Double_t[np];
42 ry = new Double_t[np];
43 rz = new Double_t[np];
44 ex = new Double_t[np];
45 ey = new Double_t[np];
46 ez = new Double_t[np];
47
48 auto r = new TRandom();
49
50 for (Int_t N=0; N<np;N++) {
51 rx[N] = 2*P*(r->Rndm(N))-P;
52 ry[N] = 2*P*(r->Rndm(N))-P;
53 rz[N] = rx[N]*rx[N]-ry[N]*ry[N];
54 rx[N] = 10.+rx[N];
55 ry[N] = 10.+ry[N];
56 rz[N] = 40.+rz[N];
57 ex[N] = r->Rndm(N);
58 ey[N] = r->Rndm(N);
59 ez[N] = 10*r->Rndm(N);
60 }
61
62 TGraph2DErrors *g = new TGraph2DErrors(np, rx, ry, rz, ex, ey, ez);
63 g->SetTitle("TGraph2D with error bars: option \"ERR\"");
64 g->SetFillColor(29);
65 g->SetMarkerSize(0.8);
66 g->SetMarkerStyle(20);
67 g->SetMarkerColor(kRed);
68 g->SetLineColor(kBlue-3);
69 g->SetLineWidth(2);
70 gPad->SetLogy(1);
71 g->Draw("err p0");
72}
73End_Macro
74*/
75
76
77////////////////////////////////////////////////////////////////////////////////
78/// TGraph2DErrors default constructor
79
81{
82}
83
84
85////////////////////////////////////////////////////////////////////////////////
86/// TGraph2DErrors normal constructor
87/// the arrays are preset to zero
88
90 : TGraph2D(n)
91{
92 if (n <= 0) {
93 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
94 return;
95 }
96
97 fEX = new Double_t[n];
98 fEY = new Double_t[n];
99 fEZ = new Double_t[n];
100
101 for (Int_t i=0;i<n;i++) {
102 fEX[i] = 0;
103 fEY[i] = 0;
104 fEZ[i] = 0;
105 }
106}
107
108
109////////////////////////////////////////////////////////////////////////////////
110/// TGraph2DErrors constructor with doubles vectors as input.
111
114 :TGraph2D(n, x, y, z)
115{
116 if (n <= 0) {
117 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
118 return;
119 }
120
121 fEX = new Double_t[n];
122 fEY = new Double_t[n];
123 fEZ = new Double_t[n];
124
125 for (Int_t i=0;i<n;i++) {
126 if (ex) fEX[i] = ex[i];
127 else fEX[i] = 0;
128 if (ey) fEY[i] = ey[i];
129 else fEY[i] = 0;
130 if (ez) fEZ[i] = ez[i];
131 else fEZ[i] = 0;
132 }
133}
134
135
136////////////////////////////////////////////////////////////////////////////////
137/// TGraph2DErrors destructor.
138
140{
141 delete [] fEX;
142 delete [] fEY;
143 delete [] fEZ;
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Copy constructor.
148/// Copy everything except list of functions
149
151: TGraph2D(g), fEX(0), fEY(0), fEZ(0)
152{
153 if (fSize > 0) {
154 fEX = new Double_t[fSize];
155 fEY = new Double_t[fSize];
156 fEZ = new Double_t[fSize];
157 for (Int_t n = 0; n < fSize; n++) {
158 fEX[n] = g.fEX[n];
159 fEY[n] = g.fEY[n];
160 fEZ[n] = g.fEZ[n];
161 }
162 }
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Assignment operator
167/// Copy everything except list of functions
168
170{
171 if (this == &g) return *this;
172
173 // call operator= on TGraph2D
174 this->TGraph2D::operator=(static_cast<const TGraph2D&>(g) );
175
176 // delete before existing contained objects
177 if (fEX) delete [] fEX;
178 if (fEY) delete [] fEY;
179 if (fEZ) delete [] fEZ;
180
181 fEX = (fSize > 0) ? new Double_t[fSize] : nullptr;
182 fEY = (fSize > 0) ? new Double_t[fSize] : nullptr;
183 fEZ = (fSize > 0) ? new Double_t[fSize] : nullptr;
184
185
186 // copy error arrays
187 for (Int_t n = 0; n < fSize; n++) {
188 fEX[n] = g.fEX[n];
189 fEY[n] = g.fEY[n];
190 fEZ[n] = g.fEZ[n];
191 }
192 return *this;
193}
194////////////////////////////////////////////////////////////////////////////////
195/// This function is called by Graph2DFitChisquare.
196/// It returns the error along X at point i.
197
199{
200 if (i < 0 || i >= fNpoints) return -1;
201 if (fEX) return fEX[i];
202 return -1;
203}
204
205
206////////////////////////////////////////////////////////////////////////////////
207/// This function is called by Graph2DFitChisquare.
208/// It returns the error along Y at point i.
209
211{
212 if (i < 0 || i >= fNpoints) return -1;
213 if (fEY) return fEY[i];
214 return -1;
215}
216
217
218////////////////////////////////////////////////////////////////////////////////
219/// This function is called by Graph2DFitChisquare.
220/// It returns the error along Z at point i.
221
223{
224 if (i < 0 || i >= fNpoints) return -1;
225 if (fEZ) return fEZ[i];
226 return -1;
227}
228
229
230////////////////////////////////////////////////////////////////////////////////
231/// Returns the X maximum with errors.
232
234{
235 Double_t v = fX[0]+fEX[0];
236 for (Int_t i=1; i<fNpoints; i++) if (fX[i]+fEX[i]>v) v=fX[i]+fEX[i];
237 return v;
238}
239
240
241////////////////////////////////////////////////////////////////////////////////
242/// Returns the X minimum with errors.
243
245{
246 Double_t v = fX[0]-fEX[0];
247 for (Int_t i=1; i<fNpoints; i++) if (fX[i]-fEX[i]<v) v=fX[i]-fEX[i];
248 return v;
249}
250
251
252////////////////////////////////////////////////////////////////////////////////
253/// Returns the Y maximum with errors.
254
256{
257 Double_t v = fY[0]+fEY[0];
258 for (Int_t i=1; i<fNpoints; i++) if (fY[i]+fEY[i]>v) v=fY[i]+fEY[i];
259 return v;
260}
261
262
263////////////////////////////////////////////////////////////////////////////////
264/// Returns the Y minimum with errors.
265
267{
268 Double_t v = fY[0]-fEY[0];
269 for (Int_t i=1; i<fNpoints; i++) if (fY[i]-fEY[i]<v) v=fY[i]-fEY[i];
270 return v;
271}
272
273
274////////////////////////////////////////////////////////////////////////////////
275/// Returns the Z maximum with errors.
276
278{
279 Double_t v = fZ[0]+fEZ[0];
280 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]+fEZ[i]>v) v=fZ[i]+fEZ[i];
281 return v;
282}
283
284
285////////////////////////////////////////////////////////////////////////////////
286/// Returns the Z minimum with errors.
287
289{
290 Double_t v = fZ[0]-fEZ[0];
291 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]-fEZ[i]<v) v=fZ[i]-fEZ[i];
292 return v;
293}
294
295
296////////////////////////////////////////////////////////////////////////////////
297/// Print 2D graph and errors values.
298
300{
301 for (Int_t i = 0; i < fNpoints; i++) {
302 printf("x[%d]=%g, y[%d]=%g, z[%d]=%g, ex[%d]=%g, ey[%d]=%g, ez[%d]=%g\n", i, fX[i], i, fY[i], i, fZ[i], i, fEX[i], i, fEY[i], i, fEZ[i]);
303 }
304}
305
306////////////////////////////////////////////////////////////////////////////////
307/// Multiply the values and errors of a TGraph2DErrors by a constant c1.
308///
309/// If option contains "x" the x values and errors are scaled
310/// If option contains "y" the y values and errors are scaled
311/// If option contains "z" the z values and errors are scaled
312/// If option contains "xyz" all three x, y and z values and errors are scaled
313
315{
317 TString opt = option; opt.ToLower();
318 if (opt.Contains("x") && GetEX()) {
319 for (Int_t i=0; i<GetN(); i++)
320 GetEX()[i] *= c1;
321 }
322 if (opt.Contains("y") && GetEY()) {
323 for (Int_t i=0; i<GetN(); i++)
324 GetEY()[i] *= c1;
325 }
326 if (opt.Contains("z") && GetEZ()) {
327 for (Int_t i=0; i<GetN(); i++)
328 GetEZ()[i] *= c1;
329 }
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// Set number of points in the 2D graph.
334/// Existing coordinates are preserved.
335/// New coordinates above fNpoints are preset to 0.
336
338{
339 if (n < 0) n = 0;
340 if (n == fNpoints) return;
341 if (n > fNpoints) SetPointError(n,0,0,0);
342 fNpoints = n;
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Deletes point number ipoint
347
349{
350 if (ipoint < 0) return -1;
351 if (ipoint >= fNpoints) return -1;
352
353 fNpoints--;
354 Double_t *newX = new Double_t[fNpoints];
355 Double_t *newY = new Double_t[fNpoints];
356 Double_t *newZ = new Double_t[fNpoints];
357 Double_t *newEX = new Double_t[fNpoints];
358 Double_t *newEY = new Double_t[fNpoints];
359 Double_t *newEZ = new Double_t[fNpoints];
360
361 Int_t j = -1;
362 for (Int_t i = 0; i < fNpoints + 1; i++) {
363 if (i == ipoint) continue;
364 j++;
365 newX[j] = fX[i];
366 newY[j] = fY[i];
367 newZ[j] = fZ[i];
368 newEX[j] = fEX[i];
369 newEY[j] = fEY[i];
370 newEZ[j] = fEZ[i];
371 }
372 delete [] fX;
373 delete [] fY;
374 delete [] fZ;
375 delete [] fEX;
376 delete [] fEY;
377 delete [] fEZ;
378 fX = newX;
379 fY = newY;
380 fZ = newZ;
381 fEX = newEX;
382 fEY = newEY;
383 fEZ = newEZ;
384 fSize = fNpoints;
385 if (fHistogram) {
386 delete fHistogram;
387 fHistogram = nullptr;
388 fDelaunay = nullptr;
389 }
390 return ipoint;
391}
392
393////////////////////////////////////////////////////////////////////////////////
394/// Set x, y and z values for point number i
395
397{
398 if (i < 0) return;
399 if (i >= fNpoints) {
400 // re-allocate the object
401 Double_t *savex = new Double_t[i+1];
402 Double_t *savey = new Double_t[i+1];
403 Double_t *savez = new Double_t[i+1];
404 Double_t *saveex = new Double_t[i+1];
405 Double_t *saveey = new Double_t[i+1];
406 Double_t *saveez = new Double_t[i+1];
407 if (fNpoints > 0) {
408 memcpy(savex, fX, fNpoints*sizeof(Double_t));
409 memcpy(savey, fY, fNpoints*sizeof(Double_t));
410 memcpy(savez, fZ, fNpoints*sizeof(Double_t));
411 memcpy(saveex,fEX,fNpoints*sizeof(Double_t));
412 memcpy(saveey,fEY,fNpoints*sizeof(Double_t));
413 memcpy(saveez,fEZ,fNpoints*sizeof(Double_t));
414 }
415 if (fX) delete [] fX;
416 if (fY) delete [] fY;
417 if (fZ) delete [] fZ;
418 if (fEX) delete [] fEX;
419 if (fEY) delete [] fEY;
420 if (fEZ) delete [] fEZ;
421 fX = savex;
422 fY = savey;
423 fZ = savez;
424 fEX = saveex;
425 fEY = saveey;
426 fEZ = saveez;
427 fNpoints = i+1;
428 }
429 fX[i] = x;
430 fY[i] = y;
431 fZ[i] = z;
432}
433
434
435////////////////////////////////////////////////////////////////////////////////
436/// Set ex, ey and ez values for point number i
437
439{
440 if (i < 0) return;
441 if (i >= fNpoints) {
442 // re-allocate the object
444 }
445 fEX[i] = ex;
446 fEY[i] = ey;
447 fEZ[i] = ez;
448}
449
450
451////////////////////////////////////////////////////////////////////////////////
452/// Stream an object of class TGraph2DErrors.
453
455{
456 if (b.IsReading()) {
457 UInt_t R__s, R__c;
458 Version_t R__v = b.ReadVersion(&R__s, &R__c);
459 b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
460 } else {
461 b.WriteClassBuffer(TGraph2DErrors::Class(),this);
462 }
463}
short Version_t
Definition: RtypesCore.h:65
const char Option_t
Definition: RtypesCore.h:66
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t g
Buffer base class used for serializing objects.
Definition: TBuffer.h:43
Graph 2D class with errors.
void Streamer(TBuffer &) override
Stream an object of class TGraph2DErrors.
Double_t GetErrorZ(Int_t bin) const override
This function is called by Graph2DFitChisquare.
~TGraph2DErrors() override
TGraph2DErrors destructor.
Double_t GetYminE() const override
Returns the Y minimum with errors.
Double_t GetXminE() const override
Returns the X minimum with errors.
Double_t * GetEZ() const override
Double_t * fEY
[fNpoints] array of Y errors
Double_t * GetEY() const override
void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z) override
Set x, y and z values for point number i.
void Scale(Double_t c1=1., Option_t *option="z") override
Multiply the values and errors of a TGraph2DErrors by a constant c1.
Double_t * fEZ
[fNpoints] array of Z errors
Double_t * GetEX() const override
virtual void SetPointError(Int_t i, Double_t ex, Double_t ey, Double_t ez)
Set ex, ey and ez values for point number i.
Double_t GetErrorX(Int_t bin) const override
This function is called by Graph2DFitChisquare.
void Print(Option_t *chopt="") const override
Print 2D graph and errors values.
Double_t GetZminE() const override
Returns the Z minimum with errors.
TGraph2DErrors & operator=(const TGraph2DErrors &)
Assignment operator Copy everything except list of functions.
Double_t GetErrorY(Int_t bin) const override
This function is called by Graph2DFitChisquare.
Double_t * fEX
[fNpoints] array of X errors
TGraph2DErrors()
TGraph2DErrors default constructor.
Double_t GetYmaxE() const override
Returns the Y maximum with errors.
Double_t GetXmaxE() const override
Returns the X maximum with errors.
Double_t GetZmaxE() const override
Returns the Z maximum with errors.
void Set(Int_t n) override
Set number of points in the 2D graph.
static TClass * Class()
Int_t RemovePoint(Int_t ipoint)
Deletes point number ipoint.
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:41
Int_t fNpoints
Number of points in the data set.
Definition: TGraph2D.h:45
Double_t * fZ
[fNpoints]
Definition: TGraph2D.h:52
TH2D * fHistogram
!2D histogram of z values linearly interpolated on the triangles
Definition: TGraph2D.h:58
TObject * fDelaunay
! Pointer to Delaunay interpolator object
Definition: TGraph2D.h:59
Int_t GetN() const
Definition: TGraph2D.h:120
TGraph2D & operator=(const TGraph2D &)
Graph2D operator "=".
Definition: TGraph2D.cxx:566
Double_t * fX
[fNpoints]
Definition: TGraph2D.h:50
Double_t * fY
[fNpoints] Data set to be plotted
Definition: TGraph2D.h:51
virtual void Scale(Double_t c1=1., Option_t *option="z")
Multiply the values of a TGraph2D by a constant c1.
Definition: TGraph2D.cxx:1478
Int_t fSize
!Real size of fX, fY and fZ
Definition: TGraph2D.h:49
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
Basic string class.
Definition: TString.h:136
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1150
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
Double_t y[n]
Definition: legend1.C:17
return c1
Definition: legend1.C:41
Double_t x[n]
Definition: legend1.C:17
Double_t ey[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Double_t ex[n]
Definition: legend1.C:17