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 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/// Multiply the values and errors of a TGraph2DErrors by a constant c1.
312///
313/// If option contains "x" the x values and errors are scaled
314/// If option contains "y" the y values and errors are scaled
315/// If option contains "z" the z values and errors are scaled
316/// If option contains "xyz" all three x, y and z values and errors are scaled
317
319{
320 TGraph2D::Scale(c1, option);
321 TString opt = option; opt.ToLower();
322 if (opt.Contains("x") && GetEX()) {
323 for (Int_t i=0; i<GetN(); i++)
324 GetEX()[i] *= c1;
325 }
326 if (opt.Contains("y") && GetEY()) {
327 for (Int_t i=0; i<GetN(); i++)
328 GetEY()[i] *= c1;
329 }
330 if (opt.Contains("z") && GetEZ()) {
331 for (Int_t i=0; i<GetN(); i++)
332 GetEZ()[i] *= c1;
333 }
334}
335
336////////////////////////////////////////////////////////////////////////////////
337/// Set number of points in the 2D graph.
338/// Existing coordinates are preserved.
339/// New coordinates above fNpoints are preset to 0.
340
342{
343 if (n < 0) n = 0;
344 if (n == fNpoints) return;
345 if (n > fNpoints) SetPointError(n,0,0,0);
346 fNpoints = n;
347}
348
349////////////////////////////////////////////////////////////////////////////////
350/// Deletes point number ipoint
351
353{
354 if (ipoint < 0) return -1;
355 if (ipoint >= fNpoints) return -1;
356
357 fNpoints--;
358 Double_t *newX = new Double_t[fNpoints];
359 Double_t *newY = new Double_t[fNpoints];
360 Double_t *newZ = new Double_t[fNpoints];
361 Double_t *newEX = new Double_t[fNpoints];
362 Double_t *newEY = new Double_t[fNpoints];
363 Double_t *newEZ = new Double_t[fNpoints];
364
365 Int_t j = -1;
366 for (Int_t i = 0; i < fNpoints + 1; i++) {
367 if (i == ipoint) continue;
368 j++;
369 newX[j] = fX[i];
370 newY[j] = fY[i];
371 newZ[j] = fZ[i];
372 newEX[j] = fEX[i];
373 newEY[j] = fEY[i];
374 newEZ[j] = fEZ[i];
375 }
376 delete [] fX;
377 delete [] fY;
378 delete [] fZ;
379 delete [] fEX;
380 delete [] fEY;
381 delete [] fEZ;
382 fX = newX;
383 fY = newY;
384 fZ = newZ;
385 fEX = newEX;
386 fEY = newEY;
387 fEZ = newEZ;
388 fSize = fNpoints;
389 if (fHistogram) {
390 delete fHistogram;
391 fHistogram = nullptr;
392 fDelaunay = nullptr;
393 }
394 return ipoint;
395}
396
397////////////////////////////////////////////////////////////////////////////////
398/// Set x, y and z values for point number i
399
401{
402 if (i < 0) return;
403 if (i >= fNpoints) {
404 // re-allocate the object
405 Double_t *savex = new Double_t[i+1];
406 Double_t *savey = new Double_t[i+1];
407 Double_t *savez = new Double_t[i+1];
408 Double_t *saveex = new Double_t[i+1];
409 Double_t *saveey = new Double_t[i+1];
410 Double_t *saveez = new Double_t[i+1];
411 if (fNpoints > 0) {
412 memcpy(savex, fX, fNpoints*sizeof(Double_t));
413 memcpy(savey, fY, fNpoints*sizeof(Double_t));
414 memcpy(savez, fZ, fNpoints*sizeof(Double_t));
415 memcpy(saveex,fEX,fNpoints*sizeof(Double_t));
416 memcpy(saveey,fEY,fNpoints*sizeof(Double_t));
417 memcpy(saveez,fEZ,fNpoints*sizeof(Double_t));
418 }
419 if (fX) delete [] fX;
420 if (fY) delete [] fY;
421 if (fZ) delete [] fZ;
422 if (fEX) delete [] fEX;
423 if (fEY) delete [] fEY;
424 if (fEZ) delete [] fEZ;
425 fX = savex;
426 fY = savey;
427 fZ = savez;
428 fEX = saveex;
429 fEY = saveey;
430 fEZ = saveez;
431 fNpoints = i+1;
432 }
433 fX[i] = x;
434 fY[i] = y;
435 fZ[i] = z;
436}
437
438
439////////////////////////////////////////////////////////////////////////////////
440/// Set ex, ey and ez values for point number i
441
443{
444 if (i < 0) return;
445 if (i >= fNpoints) {
446 // re-allocate the object
448 }
449 fEX[i] = ex;
450 fEY[i] = ey;
451 fEZ[i] = ez;
452}
453
454
455////////////////////////////////////////////////////////////////////////////////
456/// Stream an object of class TGraphErrors.
457
458void TGraph2DErrors::Streamer(TBuffer &b)
459{
460 if (b.IsReading()) {
461 UInt_t R__s, R__c;
462 Version_t R__v = b.ReadVersion(&R__s, &R__c);
463 b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
464 } else {
465 b.WriteClassBuffer(TGraph2DErrors::Class(),this);
466 }
467}
#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 * GetEX() const
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.
virtual void Scale(Double_t c1=1., Option_t *option="z")
Multiply the values and errors of a TGraph2DErrors by a constant c1.
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.
Double_t * GetEY() const
Double_t * GetEZ() const
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:120
TGraph2D & operator=(const TGraph2D &)
Graph2D operator "=".
Definition TGraph2D.cxx:566
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.
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:963
Basic string class.
Definition TString.h:136
void ToLower()
Change string to lower-case.
Definition TString.cxx:1150
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
return c1
Definition legend1.C:41
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