Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 auto 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/// TGraph2DErrors normal constructor
85/// the arrays are preset to zero
86
88 : TGraph2D(n)
89{
90 if (n <= 0) {
91 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
92 return;
93 }
94
95 fEX = new Double_t[n];
96 fEY = new Double_t[n];
97 fEZ = new Double_t[n];
98
99 for (Int_t i=0;i<n;i++) {
100 fEX[i] = 0;
101 fEY[i] = 0;
102 fEZ[i] = 0;
103 }
104}
105
106
107////////////////////////////////////////////////////////////////////////////////
108/// TGraph2DErrors constructor with doubles vectors as input.
109
112 :TGraph2D(n, x, y, z)
113{
114 if (n <= 0) {
115 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
116 return;
117 }
118
119 fEX = new Double_t[n];
120 fEY = new Double_t[n];
121 fEZ = new Double_t[n];
122
123 for (Int_t i=0;i<n;i++) {
124 if (ex) fEX[i] = ex[i];
125 else fEX[i] = 0;
126 if (ey) fEY[i] = ey[i];
127 else fEY[i] = 0;
128 if (ez) fEZ[i] = ez[i];
129 else fEZ[i] = 0;
130 }
131}
132
133
134////////////////////////////////////////////////////////////////////////////////
135/// TGraph2DErrors destructor.
136
138{
139 delete [] fEX;
140 delete [] fEY;
141 delete [] fEZ;
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Copy constructor.
146/// Copy everything except list of functions
147
149: TGraph2D(g), fEX(nullptr), fEY(nullptr), fEZ(nullptr)
150{
151 if (fSize > 0) {
152 fEX = new Double_t[fSize];
153 fEY = new Double_t[fSize];
154 fEZ = new Double_t[fSize];
155 for (Int_t n = 0; n < fSize; n++) {
156 fEX[n] = g.fEX[n];
157 fEY[n] = g.fEY[n];
158 fEZ[n] = g.fEZ[n];
159 }
160 }
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Assignment operator
165/// Copy everything except list of functions
166
168{
169 if (this == &g) return *this;
170
171 // call operator= on TGraph2D
172 this->TGraph2D::operator=(static_cast<const TGraph2D&>(g) );
173
174 // delete before existing contained objects
175 if (fEX) delete [] fEX;
176 if (fEY) delete [] fEY;
177 if (fEZ) delete [] fEZ;
178
179 fEX = (fSize > 0) ? new Double_t[fSize] : nullptr;
180 fEY = (fSize > 0) ? new Double_t[fSize] : nullptr;
181 fEZ = (fSize > 0) ? new Double_t[fSize] : nullptr;
182
183
184 // copy error arrays
185 for (Int_t n = 0; n < fSize; n++) {
186 fEX[n] = g.fEX[n];
187 fEY[n] = g.fEY[n];
188 fEZ[n] = g.fEZ[n];
189 }
190 return *this;
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Add a point with errorbars to the graph.
195
197{
198 AddPoint(x, y, z); // this will increase fNpoints by one
199 SetPointError(fNpoints - 1, ex, ey, ez);
200}
201
202////////////////////////////////////////////////////////////////////////////////
203/// This function is called by Graph2DFitChisquare.
204/// It returns the error along X at point i.
205
207{
208 if (i < 0 || i >= fNpoints) return -1;
209 if (fEX) return fEX[i];
210 return -1;
211}
212
213
214////////////////////////////////////////////////////////////////////////////////
215/// This function is called by Graph2DFitChisquare.
216/// It returns the error along Y at point i.
217
219{
220 if (i < 0 || i >= fNpoints) return -1;
221 if (fEY) return fEY[i];
222 return -1;
223}
224
225
226////////////////////////////////////////////////////////////////////////////////
227/// This function is called by Graph2DFitChisquare.
228/// It returns the error along Z at point i.
229
231{
232 if (i < 0 || i >= fNpoints) return -1;
233 if (fEZ) return fEZ[i];
234 return -1;
235}
236
237
238////////////////////////////////////////////////////////////////////////////////
239/// Returns the X maximum with errors.
240
242{
243 Double_t v = fX[0]+fEX[0];
244 for (Int_t i=1; i<fNpoints; i++) if (fX[i]+fEX[i]>v) v=fX[i]+fEX[i];
245 return v;
246}
247
248
249////////////////////////////////////////////////////////////////////////////////
250/// Returns the X minimum with errors.
251
253{
254 Double_t v = fX[0]-fEX[0];
255 for (Int_t i=1; i<fNpoints; i++) if (fX[i]-fEX[i]<v) v=fX[i]-fEX[i];
256 return v;
257}
258
259
260////////////////////////////////////////////////////////////////////////////////
261/// Returns the Y maximum with errors.
262
264{
265 Double_t v = fY[0]+fEY[0];
266 for (Int_t i=1; i<fNpoints; i++) if (fY[i]+fEY[i]>v) v=fY[i]+fEY[i];
267 return v;
268}
269
270
271////////////////////////////////////////////////////////////////////////////////
272/// Returns the Y minimum with errors.
273
275{
276 Double_t v = fY[0]-fEY[0];
277 for (Int_t i=1; i<fNpoints; i++) if (fY[i]-fEY[i]<v) v=fY[i]-fEY[i];
278 return v;
279}
280
281
282////////////////////////////////////////////////////////////////////////////////
283/// Returns the Z maximum with errors.
284
286{
287 Double_t v = fZ[0]+fEZ[0];
288 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]+fEZ[i]>v) v=fZ[i]+fEZ[i];
289 return v;
290}
291
292
293////////////////////////////////////////////////////////////////////////////////
294/// Returns the Z minimum with errors.
295
297{
298 Double_t v = fZ[0]-fEZ[0];
299 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]-fEZ[i]<v) v=fZ[i]-fEZ[i];
300 return v;
301}
302
303
304////////////////////////////////////////////////////////////////////////////////
305/// Print 2D graph and errors values.
306
308{
309 for (Int_t i = 0; i < fNpoints; i++) {
310 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]);
311 }
312}
313
314////////////////////////////////////////////////////////////////////////////////
315/// Multiply the values and errors of a TGraph2DErrors by a constant c1.
316///
317/// If option contains "x" the x values and errors are scaled
318/// If option contains "y" the y values and errors are scaled
319/// If option contains "z" the z values and errors are scaled
320/// If option contains "xyz" all three x, y and z values and errors are scaled
321
323{
325 TString opt = option; opt.ToLower();
326 if (opt.Contains("x") && GetEX()) {
327 for (Int_t i=0; i<GetN(); i++)
328 GetEX()[i] *= c1;
329 }
330 if (opt.Contains("y") && GetEY()) {
331 for (Int_t i=0; i<GetN(); i++)
332 GetEY()[i] *= c1;
333 }
334 if (opt.Contains("z") && GetEZ()) {
335 for (Int_t i=0; i<GetN(); i++)
336 GetEZ()[i] *= c1;
337 }
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Set number of points in the 2D graph.
342/// Existing coordinates are preserved.
343/// New coordinates above fNpoints are preset to 0.
344
346{
347 if (n < 0) n = 0;
348 if (n == fNpoints) return;
349 if (n > fNpoints) SetPointError(n,0,0,0);
350 fNpoints = n;
351}
352
353////////////////////////////////////////////////////////////////////////////////
354/// Deletes point number ipoint
355
357{
358 if (ipoint < 0) return -1;
359 if (ipoint >= fNpoints) return -1;
360
361 fNpoints--;
362 Double_t *newX = new Double_t[fNpoints];
363 Double_t *newY = new Double_t[fNpoints];
364 Double_t *newZ = new Double_t[fNpoints];
365 Double_t *newEX = new Double_t[fNpoints];
366 Double_t *newEY = new Double_t[fNpoints];
367 Double_t *newEZ = new Double_t[fNpoints];
368
369 Int_t j = -1;
370 for (Int_t i = 0; i < fNpoints + 1; i++) {
371 if (i == ipoint) continue;
372 j++;
373 newX[j] = fX[i];
374 newY[j] = fY[i];
375 newZ[j] = fZ[i];
376 newEX[j] = fEX[i];
377 newEY[j] = fEY[i];
378 newEZ[j] = fEZ[i];
379 }
380 delete [] fX;
381 delete [] fY;
382 delete [] fZ;
383 delete [] fEX;
384 delete [] fEY;
385 delete [] fEZ;
386 fX = newX;
387 fY = newY;
388 fZ = newZ;
389 fEX = newEX;
390 fEY = newEY;
391 fEZ = newEZ;
392 fSize = fNpoints;
393 if (fHistogram) {
394 delete fHistogram;
395 fHistogram = nullptr;
396 fDelaunay = nullptr;
397 }
398 return ipoint;
399}
400
401////////////////////////////////////////////////////////////////////////////////
402/// Set x, y and z values for point number i
403
405{
406 if (i < 0) return;
407 if (i >= fNpoints) {
408 // re-allocate the object
409 Double_t *savex = new Double_t[i+1];
410 Double_t *savey = new Double_t[i+1];
411 Double_t *savez = new Double_t[i+1];
412 Double_t *saveex = new Double_t[i+1];
413 Double_t *saveey = new Double_t[i+1];
414 Double_t *saveez = new Double_t[i+1];
415 if (fNpoints > 0) {
416 memcpy(savex, fX, fNpoints*sizeof(Double_t));
417 memcpy(savey, fY, fNpoints*sizeof(Double_t));
418 memcpy(savez, fZ, fNpoints*sizeof(Double_t));
419 memcpy(saveex,fEX,fNpoints*sizeof(Double_t));
420 memcpy(saveey,fEY,fNpoints*sizeof(Double_t));
421 memcpy(saveez,fEZ,fNpoints*sizeof(Double_t));
422 }
423 if (fX) delete [] fX;
424 if (fY) delete [] fY;
425 if (fZ) delete [] fZ;
426 if (fEX) delete [] fEX;
427 if (fEY) delete [] fEY;
428 if (fEZ) delete [] fEZ;
429 fX = savex;
430 fY = savey;
431 fZ = savez;
432 fEX = saveex;
433 fEY = saveey;
434 fEZ = saveez;
435 fNpoints = i+1;
436 }
437 fX[i] = x;
438 fY[i] = y;
439 fZ[i] = z;
440}
441
442
443////////////////////////////////////////////////////////////////////////////////
444/// Set ex, ey and ez values for point number i
445
447{
448 if (i < 0) return;
449 if (i >= fNpoints) {
450 // re-allocate the object
452 }
453 fEX[i] = ex;
454 fEY[i] = ey;
455 fEZ[i] = ez;
456}
457
458
459////////////////////////////////////////////////////////////////////////////////
460/// Stream an object of class TGraph2DErrors.
461
463{
464 if (b.IsReading()) {
465 UInt_t R__s, R__c;
466 Version_t R__v = b.ReadVersion(&R__s, &R__c);
467 b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
468 } else {
469 b.WriteClassBuffer(TGraph2DErrors::Class(),this);
470 }
471}
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
short Version_t
Definition RtypesCore.h:65
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t option
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.
virtual void AddPointError(Double_t x, Double_t y, Double_t z, Double_t ex=0., Double_t ey=0., Double_t ez=0.)
Add a point with errorbars to the graph.
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:122
TGraph2D & operator=(const TGraph2D &)
Graph2D operator "=".
Definition TGraph2D.cxx:557
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.
virtual void AddPoint(Double_t x, Double_t y, Double_t z)
Append a new point to the graph.
Definition TGraph2D.h:93
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:1005
Basic string class.
Definition TString.h:139
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
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