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 "Riostream.h"
13#include "TBuffer.h"
14#include "TGraph2DErrors.h"
15#include "TMath.h"
16#include "TH2.h"
17#include "TVirtualPad.h"
18#include "TVirtualFitter.h"
19#include "THLimitsFinder.h"
20#include "TStyle.h"
21
23
24/** \class TGraph2DErrors
25 \ingroup Hist
26Graph 2D class with errors.
27
28A TGraph2DErrors is a TGraph2D with errors. It behaves like a TGraph2D and has
29the same drawing options.
30
31The **"ERR"** drawing option allows to display the error bars. The
32following example shows how to use it:
33
34Begin_Macro(source)
35{
36 TCanvas *c = new TCanvas("c","Graph2DErrors example",0,0,600,600);
37 Double_t P = 6.;
38 Int_t np = 200;
39
40 Double_t *rx=0, *ry=0, *rz=0;
41 Double_t *ex=0, *ey=0, *ez=0;
42
43 rx = new Double_t[np];
44 ry = new Double_t[np];
45 rz = new Double_t[np];
46 ex = new Double_t[np];
47 ey = new Double_t[np];
48 ez = new Double_t[np];
49
50 TRandom *r = new TRandom();
51
52 for (Int_t N=0; N<np;N++) {
53 rx[N] = 2*P*(r->Rndm(N))-P;
54 ry[N] = 2*P*(r->Rndm(N))-P;
55 rz[N] = rx[N]*rx[N]-ry[N]*ry[N];
56 rx[N] = 10.+rx[N];
57 ry[N] = 10.+ry[N];
58 rz[N] = 40.+rz[N];
59 ex[N] = r->Rndm(N);
60 ey[N] = r->Rndm(N);
61 ez[N] = 10*r->Rndm(N);
62 }
63
64 TGraph2DErrors *dte = new TGraph2DErrors(np, rx, ry, rz, ex, ey, ez);
65 dte->SetTitle("TGraph2D with error bars: option \"ERR\"");
66 dte->SetFillColor(29);
67 dte->SetMarkerSize(0.8);
68 dte->SetMarkerStyle(20);
69 dte->SetMarkerColor(kRed);
70 dte->SetLineColor(kBlue-3);
71 dte->SetLineWidth(2);
72 dte->Draw("err p0");
73 gPad->SetLogy(1);
74 return c;
75}
76End_Macro
77*/
78
79
80////////////////////////////////////////////////////////////////////////////////
81/// TGraph2DErrors default constructor
82
84{
85 fEX = 0;
86 fEY = 0;
87 fEZ = 0;
88}
89
90
91////////////////////////////////////////////////////////////////////////////////
92/// TGraph2DErrors normal constructor
93/// the arrays are preset to zero
94
96 : TGraph2D(n)
97{
98 if (n <= 0) {
99 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
100 return;
101 }
102
103 fEX = new Double_t[n];
104 fEY = new Double_t[n];
105 fEZ = new Double_t[n];
106
107 for (Int_t i=0;i<n;i++) {
108 fEX[i] = 0;
109 fEY[i] = 0;
110 fEZ[i] = 0;
111 }
112}
113
114
115////////////////////////////////////////////////////////////////////////////////
116/// TGraph2DErrors constructor with doubles vectors as input.
117
120 :TGraph2D(n, x, y, z)
121{
122 if (n <= 0) {
123 Error("TGraphErrors", "Invalid number of points (%d)", n);
124 return;
125 }
126
127 fEX = new Double_t[n];
128 fEY = new Double_t[n];
129 fEZ = new Double_t[n];
130
131 for (Int_t i=0;i<n;i++) {
132 if (ex) fEX[i] = ex[i];
133 else fEX[i] = 0;
134 if (ey) fEY[i] = ey[i];
135 else fEY[i] = 0;
136 if (ez) fEZ[i] = ez[i];
137 else fEZ[i] = 0;
138 }
139}
140
141
142////////////////////////////////////////////////////////////////////////////////
143/// TGraph2DErrors destructor.
144
146{
147 delete [] fEX;
148 delete [] fEY;
149 delete [] fEZ;
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Copy constructor.
154/// Copy everything except list of functions
155
157: TGraph2D(g), fEX(0), fEY(0), fEZ(0)
158{
159 if (fSize > 0) {
160 fEX = new Double_t[fSize];
161 fEY = new Double_t[fSize];
162 fEZ = new Double_t[fSize];
163 for (Int_t n = 0; n < fSize; n++) {
164 fEX[n] = g.fEX[n];
165 fEY[n] = g.fEY[n];
166 fEZ[n] = g.fEZ[n];
167 }
168 }
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Assignment operator
173/// Copy everything except list of functions
174
176{
177 if (this == &g) return *this;
178
179 // call operator= on TGraph2D
180 this->TGraph2D::operator=(static_cast<const TGraph2D&>(g) );
181
182 // delete before existing contained objects
183 if (fEX) delete [] fEX;
184 if (fEY) delete [] fEY;
185 if (fEZ) delete [] fEZ;
186
187 fEX = (fSize > 0) ? new Double_t[fSize] : 0;
188 fEY = (fSize > 0) ? new Double_t[fSize] : 0;
189 fEZ = (fSize > 0) ? new Double_t[fSize] : 0;
190
191
192 // copy error arrays
193 for (Int_t n = 0; n < fSize; n++) {
194 fEX[n] = g.fEX[n];
195 fEY[n] = g.fEY[n];
196 fEZ[n] = g.fEZ[n];
197 }
198 return *this;
199}
200////////////////////////////////////////////////////////////////////////////////
201/// This function is called by Graph2DFitChisquare.
202/// It returns the error along X at point i.
203
205{
206 if (i < 0 || i >= fNpoints) return -1;
207 if (fEX) return fEX[i];
208 return -1;
209}
210
211
212////////////////////////////////////////////////////////////////////////////////
213/// This function is called by Graph2DFitChisquare.
214/// It returns the error along X at point i.
215
217{
218 if (i < 0 || i >= fNpoints) return -1;
219 if (fEY) return fEY[i];
220 return -1;
221}
222
223
224////////////////////////////////////////////////////////////////////////////////
225/// This function is called by Graph2DFitChisquare.
226/// It returns the error along X at point i.
227
229{
230 if (i < 0 || i >= fNpoints) return -1;
231 if (fEZ) return fEZ[i];
232 return -1;
233}
234
235
236////////////////////////////////////////////////////////////////////////////////
237/// Returns the X maximum with errors.
238
240{
241 Double_t v = fX[0]+fEX[0];
242 for (Int_t i=1; i<fNpoints; i++) if (fX[i]+fEX[i]>v) v=fX[i]+fEX[i];
243 return v;
244}
245
246
247////////////////////////////////////////////////////////////////////////////////
248/// Returns the X minimum with errors.
249
251{
252 Double_t v = fX[0]-fEX[0];
253 for (Int_t i=1; i<fNpoints; i++) if (fX[i]-fEX[i]<v) v=fX[i]-fEX[i];
254 return v;
255}
256
257
258////////////////////////////////////////////////////////////////////////////////
259/// Returns the Y maximum with errors.
260
262{
263 Double_t v = fY[0]+fEY[0];
264 for (Int_t i=1; i<fNpoints; i++) if (fY[i]+fEY[i]>v) v=fY[i]+fEY[i];
265 return v;
266}
267
268
269////////////////////////////////////////////////////////////////////////////////
270/// Returns the Y minimum with errors.
271
273{
274 Double_t v = fY[0]+fEY[0];
275 for (Int_t i=1; i<fNpoints; i++) if (fY[i]-fEY[i]<v) v=fY[i]-fEY[i];
276 return v;
277}
278
279
280////////////////////////////////////////////////////////////////////////////////
281/// Returns the Z maximum with errors.
282
284{
285 Double_t v = fZ[0]+fEZ[0];
286 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]+fEZ[i]>v) v=fZ[i]+fEZ[i];
287 return v;
288}
289
290
291////////////////////////////////////////////////////////////////////////////////
292/// Returns the Z minimum with errors.
293
295{
296 Double_t v = fZ[0]+fEZ[0];
297 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]-fEZ[i]<v) v=fZ[i]-fEZ[i];
298 return v;
299}
300
301
302////////////////////////////////////////////////////////////////////////////////
303/// Print 2D graph and errors values.
304
306{
307 for (Int_t i = 0; i < fNpoints; i++) {
308 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]);
309 }
310}
311
312////////////////////////////////////////////////////////////////////////////////
313/// Set number of points in the 2D graph.
314/// Existing coordinates are preserved.
315/// New coordinates above fNpoints are preset to 0.
316
318{
319 if (n < 0) n = 0;
320 if (n == fNpoints) return;
321 if (n > fNpoints) SetPointError(n,0,0,0);
322 fNpoints = n;
323}
324
325////////////////////////////////////////////////////////////////////////////////
326/// Deletes point number ipoint
327
329{
330 if (ipoint < 0) return -1;
331 if (ipoint >= fNpoints) return -1;
332
333 fNpoints--;
334 Double_t *newX = new Double_t[fNpoints];
335 Double_t *newY = new Double_t[fNpoints];
336 Double_t *newZ = new Double_t[fNpoints];
337 Double_t *newEX = new Double_t[fNpoints];
338 Double_t *newEY = new Double_t[fNpoints];
339 Double_t *newEZ = new Double_t[fNpoints];
340
341 Int_t j = -1;
342 for (Int_t i = 0; i < fNpoints + 1; i++) {
343 if (i == ipoint) continue;
344 j++;
345 newX[j] = fX[i];
346 newY[j] = fY[i];
347 newZ[j] = fZ[i];
348 newEX[j] = fEX[i];
349 newEY[j] = fEY[i];
350 newEZ[j] = fEZ[i];
351 }
352 delete [] fX;
353 delete [] fY;
354 delete [] fZ;
355 delete [] fEX;
356 delete [] fEY;
357 delete [] fEZ;
358 fX = newX;
359 fY = newY;
360 fZ = newZ;
361 fEX = newEX;
362 fEY = newEY;
363 fEZ = newEZ;
364 fSize = fNpoints;
365 if (fHistogram) {
366 delete fHistogram;
367 fHistogram = nullptr;
368 fDelaunay = nullptr;
369 }
370 return ipoint;
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Set x, y and z values for point number i
375
377{
378 if (i < 0) return;
379 if (i >= fNpoints) {
380 // re-allocate the object
381 Double_t *savex = new Double_t[i+1];
382 Double_t *savey = new Double_t[i+1];
383 Double_t *savez = new Double_t[i+1];
384 Double_t *saveex = new Double_t[i+1];
385 Double_t *saveey = new Double_t[i+1];
386 Double_t *saveez = new Double_t[i+1];
387 if (fNpoints > 0) {
388 memcpy(savex, fX, fNpoints*sizeof(Double_t));
389 memcpy(savey, fY, fNpoints*sizeof(Double_t));
390 memcpy(savez, fZ, fNpoints*sizeof(Double_t));
391 memcpy(saveex,fEX,fNpoints*sizeof(Double_t));
392 memcpy(saveey,fEY,fNpoints*sizeof(Double_t));
393 memcpy(saveez,fEZ,fNpoints*sizeof(Double_t));
394 }
395 if (fX) delete [] fX;
396 if (fY) delete [] fY;
397 if (fZ) delete [] fZ;
398 if (fEX) delete [] fEX;
399 if (fEY) delete [] fEY;
400 if (fEZ) delete [] fEZ;
401 fX = savex;
402 fY = savey;
403 fZ = savez;
404 fEX = saveex;
405 fEY = saveey;
406 fEZ = saveez;
407 fNpoints = i+1;
408 }
409 fX[i] = x;
410 fY[i] = y;
411 fZ[i] = z;
412}
413
414
415////////////////////////////////////////////////////////////////////////////////
416/// Set ex, ey and ez values for point number i
417
419{
420 if (i < 0) return;
421 if (i >= fNpoints) {
422 // re-allocate the object
424 }
425 fEX[i] = ex;
426 fEY[i] = ey;
427 fEZ[i] = ez;
428}
429
430
431////////////////////////////////////////////////////////////////////////////////
432/// Stream an object of class TGraphErrors.
433
434void TGraph2DErrors::Streamer(TBuffer &b)
435{
436 if (b.IsReading()) {
437 UInt_t R__s, R__c;
438 Version_t R__v = b.ReadVersion(&R__s, &R__c);
439 b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
440 } else {
441 b.WriteClassBuffer(TGraph2DErrors::Class(),this);
442 }
443}
int Int_t
Definition: CPyCppyy.h:43
unsigned int UInt_t
Definition: CPyCppyy.h:44
void Class()
Definition: Class.C:29
#define b(i)
Definition: RSha256.hxx:100
#define g(i)
Definition: RSha256.hxx:105
short Version_t
Definition: RtypesCore.h:63
double Double_t
Definition: RtypesCore.h:57
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
Graph 2D class with errors.
Double_t GetYmaxE() const
Returns the Y maximum with errors.
Double_t GetXminE() const
Returns the X minimum with errors.
Double_t GetZminE() const
Returns the Z minimum with errors.
Double_t * fEY
[fNpoints] array of Y errors
Double_t GetZmaxE() const
Returns the Z maximum with errors.
virtual void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z)
Set x, y and z values for point number i.
Double_t GetErrorX(Int_t bin) const
This function is called by Graph2DFitChisquare.
virtual void Print(Option_t *chopt="") const
Print 2D graph and errors values.
Double_t * fEZ
[fNpoints] array of Z errors
Double_t GetErrorY(Int_t bin) const
This function is called by Graph2DFitChisquare.
virtual void Set(Int_t n)
Set number of points in the 2D graph.
Double_t GetXmaxE() const
Returns the X maximum with errors.
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.
TGraph2DErrors & operator=(const TGraph2DErrors &)
Assignment operator Copy everything except list of functions.
Double_t * fEX
[fNpoints] array of X errors
Double_t GetErrorZ(Int_t bin) const
This function is called by Graph2DFitChisquare.
virtual ~TGraph2DErrors()
TGraph2DErrors destructor.
TGraph2DErrors()
TGraph2DErrors default constructor.
Double_t GetYminE() const
Returns the Y minimum with errors.
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
TGraph2D & operator=(const TGraph2D &)
Graph2D operator "=".
Definition: TGraph2D.cxx:535
Double_t * fX
[fNpoints]
Definition: TGraph2D.h:50
Double_t * fY
[fNpoints] Data set to be plotted
Definition: TGraph2D.h:51
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:891
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Double_t ey[n]
Definition: legend1.C:17
Double_t ex[n]
Definition: legend1.C:17