Logo ROOT   6.12/07
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
27 Graph 2D class with errors.
28 
29 A TGraph2DErrors is a TGraph2D with errors. It behaves like a TGraph2D and has
30 the same drawing options.
31 
32 The **"ERR"** drawing option allows to display the error bars. The
33 following example shows how to use it:
34 
35 Begin_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 }
77 End_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 
120  Double_t *ex, Double_t *ey, Double_t *ez, Option_t *)
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 /// Set number of points in the 2D graph.
305 /// Existing coordinates are preserved.
306 /// New coordinates above fNpoints are preset to 0.
307 
309 {
310  if (n < 0) n = 0;
311  if (n == fNpoints) return;
312  if (n > fNpoints) SetPointError(n,0,0,0);
313  fNpoints = n;
314 }
315 
316 
317 ////////////////////////////////////////////////////////////////////////////////
318 /// Set x, y and z values for point number i
319 
321 {
322  if (i < 0) return;
323  if (i >= fNpoints) {
324  // re-allocate the object
325  Double_t *savex = new Double_t[i+1];
326  Double_t *savey = new Double_t[i+1];
327  Double_t *savez = new Double_t[i+1];
328  Double_t *saveex = new Double_t[i+1];
329  Double_t *saveey = new Double_t[i+1];
330  Double_t *saveez = new Double_t[i+1];
331  if (fNpoints > 0) {
332  memcpy(savex, fX, fNpoints*sizeof(Double_t));
333  memcpy(savey, fY, fNpoints*sizeof(Double_t));
334  memcpy(savez, fZ, fNpoints*sizeof(Double_t));
335  memcpy(saveex,fEX,fNpoints*sizeof(Double_t));
336  memcpy(saveey,fEY,fNpoints*sizeof(Double_t));
337  memcpy(saveez,fEZ,fNpoints*sizeof(Double_t));
338  }
339  if (fX) delete [] fX;
340  if (fY) delete [] fY;
341  if (fZ) delete [] fZ;
342  if (fEX) delete [] fEX;
343  if (fEY) delete [] fEY;
344  if (fEZ) delete [] fEZ;
345  fX = savex;
346  fY = savey;
347  fZ = savez;
348  fEX = saveex;
349  fEY = saveey;
350  fEZ = saveez;
351  fNpoints = i+1;
352  }
353  fX[i] = x;
354  fY[i] = y;
355  fZ[i] = z;
356 }
357 
358 
359 ////////////////////////////////////////////////////////////////////////////////
360 /// Set ex, ey and ez values for point number i
361 
363 {
364  if (i < 0) return;
365  if (i >= fNpoints) {
366  // re-allocate the object
367  TGraph2DErrors::SetPoint(i,0,0,0);
368  }
369  fEX[i] = ex;
370  fEY[i] = ey;
371  fEZ[i] = ez;
372 }
373 
374 
375 ////////////////////////////////////////////////////////////////////////////////
376 /// Stream an object of class TGraphErrors.
377 
378 void TGraph2DErrors::Streamer(TBuffer &b)
379 {
380  if (b.IsReading()) {
381  UInt_t R__s, R__c;
382  Version_t R__v = b.ReadVersion(&R__s, &R__c);
383  b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
384  } else {
386  }
387 }
Bool_t IsReading() const
Definition: TBuffer.h:83
virtual ~TGraph2DErrors()
TGraph2DErrors destructor.
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void Set(Int_t n)
Set number of points in the 2D graph.
short Version_t
Definition: RtypesCore.h:61
Double_t * fX
[fNpoints]
Definition: TGraph2D.h:49
TGraph2DErrors & operator=(const TGraph2DErrors &)
Assignment operator Copy everything except list of functions.
const char Option_t
Definition: RtypesCore.h:62
Double_t GetErrorZ(Int_t bin) const
This function is called by Graph2DFitChisquare.
TGraph2D & operator=(const TGraph2D &)
Graph2D operator "=".
Definition: TGraph2D.cxx:534
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Double_t GetXmaxE() const
Returns the X maximum with errors.
int Int_t
Definition: RtypesCore.h:41
Double_t GetZmaxE() const
Returns the Z maximum with errors.
Double_t GetYmaxE() const
Returns the Y maximum with errors.
Double_t * fEY
[fNpoints] array of Y errors
Double_t x[n]
Definition: legend1.C:17
void Class()
Definition: Class.C:29
Double_t * fEX
[fNpoints] array of X 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.
Int_t fSize
!Real size of fX, fY and fZ
Definition: TGraph2D.h:48
Double_t GetYminE() const
Returns the Y minimum with errors.
TGraph2DErrors()
TGraph2DErrors default constructor.
SVector< double, 2 > v
Definition: Dict.h:5
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
Int_t fNpoints
Number of points in the data set.
Definition: TGraph2D.h:44
Double_t GetXminE() const
Returns the X minimum with errors.
Double_t GetZminE() const
Returns the Z minimum with errors.
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
#define ClassImp(name)
Definition: Rtypes.h:359
Double_t GetErrorY(Int_t bin) const
This function is called by Graph2DFitChisquare.
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 Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
Double_t ey[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
Double_t * fZ
[fNpoints]
Definition: TGraph2D.h:51
Double_t * fEZ
[fNpoints] array of Z errors
Double_t GetErrorX(Int_t bin) const
This function is called by Graph2DFitChisquare.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:40
Double_t ex[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
Double_t * fY
[fNpoints] Data set to be plotted
Definition: TGraph2D.h:50
Graph 2D class with errors.
static constexpr double g