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