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 Hist
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 TCanvas *c = new TCanvas("c","Graph2DErrors 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 TRandom *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 *dte = new TGraph2DErrors(np, rx, ry, rz, ex, ey, ez);
63 dte->SetTitle("TGraph2D with error bars: option \"ERR\"");
64 dte->SetFillColor(29);
65 dte->SetMarkerSize(0.8);
66 dte->SetMarkerStyle(20);
67 dte->SetMarkerColor(kRed);
68 dte->SetLineColor(kBlue-3);
69 dte->SetLineWidth(2);
70 dte->Draw("err p0");
71 gPad->SetLogy(1);
72 return c;
73}
74End_Macro
75*/
76
77
78////////////////////////////////////////////////////////////////////////////////
79/// TGraph2DErrors default constructor
80
82{
83 fEX = 0;
84 fEY = 0;
85 fEZ = 0;
86}
87
88
89////////////////////////////////////////////////////////////////////////////////
90/// TGraph2DErrors normal constructor
91/// the arrays are preset to zero
92
94 : TGraph2D(n)
95{
96 if (n <= 0) {
97 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
98 return;
99 }
100
101 fEX = new Double_t[n];
102 fEY = new Double_t[n];
103 fEZ = new Double_t[n];
104
105 for (Int_t i=0;i<n;i++) {
106 fEX[i] = 0;
107 fEY[i] = 0;
108 fEZ[i] = 0;
109 }
110}
111
112
113////////////////////////////////////////////////////////////////////////////////
114/// TGraph2DErrors constructor with doubles vectors as input.
115
118 :TGraph2D(n, x, y, z)
119{
120 if (n <= 0) {
121 Error("TGraphErrors", "Invalid number of points (%d)", n);
122 return;
123 }
124
125 fEX = new Double_t[n];
126 fEY = new Double_t[n];
127 fEZ = new Double_t[n];
128
129 for (Int_t i=0;i<n;i++) {
130 if (ex) fEX[i] = ex[i];
131 else fEX[i] = 0;
132 if (ey) fEY[i] = ey[i];
133 else fEY[i] = 0;
134 if (ez) fEZ[i] = ez[i];
135 else fEZ[i] = 0;
136 }
137}
138
139
140////////////////////////////////////////////////////////////////////////////////
141/// TGraph2DErrors destructor.
142
144{
145 delete [] fEX;
146 delete [] fEY;
147 delete [] fEZ;
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Copy constructor.
152/// Copy everything except list of functions
153
155: TGraph2D(g), fEX(0), fEY(0), fEZ(0)
156{
157 if (fSize > 0) {
158 fEX = new Double_t[fSize];
159 fEY = new Double_t[fSize];
160 fEZ = new Double_t[fSize];
161 for (Int_t n = 0; n < fSize; n++) {
162 fEX[n] = g.fEX[n];
163 fEY[n] = g.fEY[n];
164 fEZ[n] = g.fEZ[n];
165 }
166 }
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Assignment operator
171/// Copy everything except list of functions
172
174{
175 if (this == &g) return *this;
176
177 // call operator= on TGraph2D
178 this->TGraph2D::operator=(static_cast<const TGraph2D&>(g) );
179
180 // delete before existing contained objects
181 if (fEX) delete [] fEX;
182 if (fEY) delete [] fEY;
183 if (fEZ) delete [] fEZ;
184
185 fEX = (fSize > 0) ? new Double_t[fSize] : 0;
186 fEY = (fSize > 0) ? new Double_t[fSize] : 0;
187 fEZ = (fSize > 0) ? new Double_t[fSize] : 0;
188
189
190 // copy error arrays
191 for (Int_t n = 0; n < fSize; n++) {
192 fEX[n] = g.fEX[n];
193 fEY[n] = g.fEY[n];
194 fEZ[n] = g.fEZ[n];
195 }
196 return *this;
197}
198////////////////////////////////////////////////////////////////////////////////
199/// This function is called by Graph2DFitChisquare.
200/// It returns the error along X at point i.
201
203{
204 if (i < 0 || i >= fNpoints) return -1;
205 if (fEX) return fEX[i];
206 return -1;
207}
208
209
210////////////////////////////////////////////////////////////////////////////////
211/// This function is called by Graph2DFitChisquare.
212/// It returns the error along X at point i.
213
215{
216 if (i < 0 || i >= fNpoints) return -1;
217 if (fEY) return fEY[i];
218 return -1;
219}
220
221
222////////////////////////////////////////////////////////////////////////////////
223/// This function is called by Graph2DFitChisquare.
224/// It returns the error along X at point i.
225
227{
228 if (i < 0 || i >= fNpoints) return -1;
229 if (fEZ) return fEZ[i];
230 return -1;
231}
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Returns the X maximum with errors.
236
238{
239 Double_t v = fX[0]+fEX[0];
240 for (Int_t i=1; i<fNpoints; i++) if (fX[i]+fEX[i]>v) v=fX[i]+fEX[i];
241 return v;
242}
243
244
245////////////////////////////////////////////////////////////////////////////////
246/// Returns the X minimum with errors.
247
249{
250 Double_t v = fX[0]-fEX[0];
251 for (Int_t i=1; i<fNpoints; i++) if (fX[i]-fEX[i]<v) v=fX[i]-fEX[i];
252 return v;
253}
254
255
256////////////////////////////////////////////////////////////////////////////////
257/// Returns the Y maximum with errors.
258
260{
261 Double_t v = fY[0]+fEY[0];
262 for (Int_t i=1; i<fNpoints; i++) if (fY[i]+fEY[i]>v) v=fY[i]+fEY[i];
263 return v;
264}
265
266
267////////////////////////////////////////////////////////////////////////////////
268/// Returns the Y minimum with errors.
269
271{
272 Double_t v = fY[0]+fEY[0];
273 for (Int_t i=1; i<fNpoints; i++) if (fY[i]-fEY[i]<v) v=fY[i]-fEY[i];
274 return v;
275}
276
277
278////////////////////////////////////////////////////////////////////////////////
279/// Returns the Z maximum with errors.
280
282{
283 Double_t v = fZ[0]+fEZ[0];
284 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]+fEZ[i]>v) v=fZ[i]+fEZ[i];
285 return v;
286}
287
288
289////////////////////////////////////////////////////////////////////////////////
290/// Returns the Z minimum with errors.
291
293{
294 Double_t v = fZ[0]+fEZ[0];
295 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]-fEZ[i]<v) v=fZ[i]-fEZ[i];
296 return v;
297}
298
299
300////////////////////////////////////////////////////////////////////////////////
301/// Print 2D graph and errors values.
302
304{
305 for (Int_t i = 0; i < fNpoints; i++) {
306 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]);
307 }
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// Set number of points in the 2D graph.
312/// Existing coordinates are preserved.
313/// New coordinates above fNpoints are preset to 0.
314
316{
317 if (n < 0) n = 0;
318 if (n == fNpoints) return;
319 if (n > fNpoints) SetPointError(n,0,0,0);
320 fNpoints = n;
321}
322
323////////////////////////////////////////////////////////////////////////////////
324/// Deletes point number ipoint
325
327{
328 if (ipoint < 0) return -1;
329 if (ipoint >= fNpoints) return -1;
330
331 fNpoints--;
332 Double_t *newX = new Double_t[fNpoints];
333 Double_t *newY = new Double_t[fNpoints];
334 Double_t *newZ = new Double_t[fNpoints];
335 Double_t *newEX = new Double_t[fNpoints];
336 Double_t *newEY = new Double_t[fNpoints];
337 Double_t *newEZ = new Double_t[fNpoints];
338
339 Int_t j = -1;
340 for (Int_t i = 0; i < fNpoints + 1; i++) {
341 if (i == ipoint) continue;
342 j++;
343 newX[j] = fX[i];
344 newY[j] = fY[i];
345 newZ[j] = fZ[i];
346 newEX[j] = fEX[i];
347 newEY[j] = fEY[i];
348 newEZ[j] = fEZ[i];
349 }
350 delete [] fX;
351 delete [] fY;
352 delete [] fZ;
353 delete [] fEX;
354 delete [] fEY;
355 delete [] fEZ;
356 fX = newX;
357 fY = newY;
358 fZ = newZ;
359 fEX = newEX;
360 fEY = newEY;
361 fEZ = newEZ;
362 fSize = fNpoints;
363 if (fHistogram) {
364 delete fHistogram;
365 fHistogram = nullptr;
366 fDelaunay = nullptr;
367 }
368 return ipoint;
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Set x, y and z values for point number i
373
375{
376 if (i < 0) return;
377 if (i >= fNpoints) {
378 // re-allocate the object
379 Double_t *savex = new Double_t[i+1];
380 Double_t *savey = new Double_t[i+1];
381 Double_t *savez = new Double_t[i+1];
382 Double_t *saveex = new Double_t[i+1];
383 Double_t *saveey = new Double_t[i+1];
384 Double_t *saveez = new Double_t[i+1];
385 if (fNpoints > 0) {
386 memcpy(savex, fX, fNpoints*sizeof(Double_t));
387 memcpy(savey, fY, fNpoints*sizeof(Double_t));
388 memcpy(savez, fZ, fNpoints*sizeof(Double_t));
389 memcpy(saveex,fEX,fNpoints*sizeof(Double_t));
390 memcpy(saveey,fEY,fNpoints*sizeof(Double_t));
391 memcpy(saveez,fEZ,fNpoints*sizeof(Double_t));
392 }
393 if (fX) delete [] fX;
394 if (fY) delete [] fY;
395 if (fZ) delete [] fZ;
396 if (fEX) delete [] fEX;
397 if (fEY) delete [] fEY;
398 if (fEZ) delete [] fEZ;
399 fX = savex;
400 fY = savey;
401 fZ = savez;
402 fEX = saveex;
403 fEY = saveey;
404 fEZ = saveez;
405 fNpoints = i+1;
406 }
407 fX[i] = x;
408 fY[i] = y;
409 fZ[i] = z;
410}
411
412
413////////////////////////////////////////////////////////////////////////////////
414/// Set ex, ey and ez values for point number i
415
417{
418 if (i < 0) return;
419 if (i >= fNpoints) {
420 // re-allocate the object
422 }
423 fEX[i] = ex;
424 fEY[i] = ey;
425 fEZ[i] = ez;
426}
427
428
429////////////////////////////////////////////////////////////////////////////////
430/// Stream an object of class TGraphErrors.
431
432void TGraph2DErrors::Streamer(TBuffer &b)
433{
434 if (b.IsReading()) {
435 UInt_t R__s, R__c;
436 Version_t R__v = b.ReadVersion(&R__s, &R__c);
437 b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
438 } else {
439 b.WriteClassBuffer(TGraph2DErrors::Class(),this);
440 }
441}
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
short Version_t
Definition RtypesCore.h:65
unsigned int UInt_t
Definition RtypesCore.h:46
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
Buffer base class used for serializing objects.
Definition TBuffer.h:43
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:539
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:893
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