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
20
21/** \class TGraph2DErrors
22 \ingroup Graphs
23Graph 2D class with errors.
24
25A TGraph2DErrors is a TGraph2D with errors. It behaves like a TGraph2D and has
26the same drawing options.
27
28The **"ERR"** drawing option allows to display the error bars. The
29following example shows how to use it:
30
31Begin_Macro(source)
32{
33 auto c = new TCanvas("c","TGraph2DErrors example",0,0,600,600);
34
35 Double_t P = 6.;
36 const Int_t np = 200;
37 std::vector<Double_t> rx(np), ry(np), rz(np), ex(np), ey(np), ez(np);
38 TRandom r;
39
40 for (Int_t N=0; N<np;N++) {
41 rx[N] = 2*P*(r.Rndm(N))-P;
42 ry[N] = 2*P*(r.Rndm(N))-P;
43 rz[N] = rx[N]*rx[N]-ry[N]*ry[N];
44 rx[N] += 10.;
45 ry[N] += 10.;
46 rz[N] += 40.;
47 ex[N] = r.Rndm(N);
48 ey[N] = r.Rndm(N);
49 ez[N] = 10*r.Rndm(N);
50 }
51
52 auto g = new TGraph2DErrors(np, rx.data(), ry.data(), rz.data(), ex.data(), ey.data(), ez.data());
53
54 g->SetTitle("TGraph2D with error bars: option \"ERR\"");
55 g->SetFillColor(29);
56 g->SetMarkerSize(0.8);
57 g->SetMarkerStyle(20);
58 g->SetMarkerColor(kRed);
59 g->SetLineColor(kBlue-3);
60 g->SetLineWidth(2);
61 gPad->SetLogy(1);
62 g->Draw("err p0");
63}
64End_Macro
65*/
66
67
68////////////////////////////////////////////////////////////////////////////////
69/// TGraph2DErrors default constructor
70
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// TGraph2DErrors normal constructor
76/// the arrays are preset to zero
77
79 : TGraph2D(n)
80{
81 if (n <= 0) {
82 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
83 return;
84 }
85
86 fEX = new Double_t[n];
87 fEY = new Double_t[n];
88 fEZ = new Double_t[n];
89
90 for (Int_t i=0;i<n;i++) {
91 fEX[i] = 0;
92 fEY[i] = 0;
93 fEZ[i] = 0;
94 }
95}
96
97
98////////////////////////////////////////////////////////////////////////////////
99/// TGraph2DErrors constructor with doubles vectors as input.
100
103 :TGraph2D(n, x, y, z)
104{
105 if (n <= 0) {
106 Error("TGraph2DErrors", "Invalid number of points (%d)", n);
107 return;
108 }
109
110 fEX = new Double_t[n];
111 fEY = new Double_t[n];
112 fEZ = new Double_t[n];
113
114 for (Int_t i=0;i<n;i++) {
115 if (ex) fEX[i] = ex[i];
116 else fEX[i] = 0;
117 if (ey) fEY[i] = ey[i];
118 else fEY[i] = 0;
119 if (ez) fEZ[i] = ez[i];
120 else fEZ[i] = 0;
121 }
122}
123
124
125////////////////////////////////////////////////////////////////////////////////
126/// TGraph2DErrors destructor.
127
129{
130 delete [] fEX;
131 delete [] fEY;
132 delete [] fEZ;
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Copy constructor.
137/// Copy everything except list of functions
138
140: TGraph2D(g), fEX(nullptr), fEY(nullptr), fEZ(nullptr)
141{
142 if (fSize > 0) {
143 fEX = new Double_t[fSize];
144 fEY = new Double_t[fSize];
145 fEZ = new Double_t[fSize];
146 for (Int_t n = 0; n < fSize; n++) {
147 fEX[n] = g.fEX[n];
148 fEY[n] = g.fEY[n];
149 fEZ[n] = g.fEZ[n];
150 }
151 }
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Assignment operator
156/// Copy everything except list of functions
157
159{
160 if (this == &g) return *this;
161
162 // call operator= on TGraph2D
163 this->TGraph2D::operator=(static_cast<const TGraph2D&>(g) );
164
165 // delete before existing contained objects
166 if (fEX) delete [] fEX;
167 if (fEY) delete [] fEY;
168 if (fEZ) delete [] fEZ;
169
170 fEX = (fSize > 0) ? new Double_t[fSize] : nullptr;
171 fEY = (fSize > 0) ? new Double_t[fSize] : nullptr;
172 fEZ = (fSize > 0) ? new Double_t[fSize] : nullptr;
173
174
175 // copy error arrays
176 for (Int_t n = 0; n < fSize; n++) {
177 fEX[n] = g.fEX[n];
178 fEY[n] = g.fEY[n];
179 fEZ[n] = g.fEZ[n];
180 }
181 return *this;
182}
183
184////////////////////////////////////////////////////////////////////////////////
185/// Add a point with errorbars to the graph.
186
188{
189 AddPoint(x, y, z); // this will increase fNpoints by one
190 SetPointError(fNpoints - 1, ex, ey, ez);
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// This function is called by Graph2DFitChisquare.
195/// It returns the error along X at point i.
196
198{
199 if (i < 0 || i >= fNpoints) return -1;
200 if (fEX) return fEX[i];
201 return -1;
202}
203
204
205////////////////////////////////////////////////////////////////////////////////
206/// This function is called by Graph2DFitChisquare.
207/// It returns the error along Y at point i.
208
210{
211 if (i < 0 || i >= fNpoints) return -1;
212 if (fEY) return fEY[i];
213 return -1;
214}
215
216
217////////////////////////////////////////////////////////////////////////////////
218/// This function is called by Graph2DFitChisquare.
219/// It returns the error along Z at point i.
220
222{
223 if (i < 0 || i >= fNpoints) return -1;
224 if (fEZ) return fEZ[i];
225 return -1;
226}
227
228
229////////////////////////////////////////////////////////////////////////////////
230/// Returns the X maximum with errors.
231
233{
234 Double_t v = fX[0]+fEX[0];
235 for (Int_t i=1; i<fNpoints; i++) if (fX[i]+fEX[i]>v) v=fX[i]+fEX[i];
236 return v;
237}
238
239
240////////////////////////////////////////////////////////////////////////////////
241/// Returns the X minimum with errors.
242
244{
245 Double_t v = fX[0]-fEX[0];
246 for (Int_t i=1; i<fNpoints; i++) if (fX[i]-fEX[i]<v) v=fX[i]-fEX[i];
247 return v;
248}
249
250
251////////////////////////////////////////////////////////////////////////////////
252/// Returns the Y maximum with errors.
253
255{
256 Double_t v = fY[0]+fEY[0];
257 for (Int_t i=1; i<fNpoints; i++) if (fY[i]+fEY[i]>v) v=fY[i]+fEY[i];
258 return v;
259}
260
261
262////////////////////////////////////////////////////////////////////////////////
263/// Returns the Y minimum with errors.
264
266{
267 Double_t v = fY[0]-fEY[0];
268 for (Int_t i=1; i<fNpoints; i++) if (fY[i]-fEY[i]<v) v=fY[i]-fEY[i];
269 return v;
270}
271
272
273////////////////////////////////////////////////////////////////////////////////
274/// Returns the Z maximum with errors.
275
277{
278 Double_t v = fZ[0]+fEZ[0];
279 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]+fEZ[i]>v) v=fZ[i]+fEZ[i];
280 return v;
281}
282
283
284////////////////////////////////////////////////////////////////////////////////
285/// Returns the Z minimum with errors.
286
288{
289 Double_t v = fZ[0]-fEZ[0];
290 for (Int_t i=1; i<fNpoints; i++) if (fZ[i]-fEZ[i]<v) v=fZ[i]-fEZ[i];
291 return v;
292}
293
294
295////////////////////////////////////////////////////////////////////////////////
296/// Print 2D graph and errors values.
297
299{
300 for (Int_t i = 0; i < fNpoints; i++) {
301 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]);
302 }
303}
304
305////////////////////////////////////////////////////////////////////////////////
306/// Multiply the values and errors of a TGraph2DErrors by a constant c1.
307///
308/// If option contains "x" the x values and errors are scaled
309/// If option contains "y" the y values and errors are scaled
310/// If option contains "z" the z values and errors are scaled
311/// If option contains "xyz" all three x, y and z values and errors are scaled
312
314{
316 TString opt = option; opt.ToLower();
317 if (opt.Contains("x") && GetEX()) {
318 for (Int_t i=0; i<GetN(); i++)
319 GetEX()[i] *= c1;
320 }
321 if (opt.Contains("y") && GetEY()) {
322 for (Int_t i=0; i<GetN(); i++)
323 GetEY()[i] *= c1;
324 }
325 if (opt.Contains("z") && GetEZ()) {
326 for (Int_t i=0; i<GetN(); i++)
327 GetEZ()[i] *= c1;
328 }
329}
330
331////////////////////////////////////////////////////////////////////////////////
332/// Set number of points in the 2D graph.
333/// Existing coordinates are preserved.
334/// New coordinates above fNpoints are preset to 0.
335
337{
338 if (n < 0) n = 0;
339 if (n == fNpoints) return;
340 if (n > fNpoints) SetPointError(n,0,0,0);
341 fNpoints = n;
342}
343
344////////////////////////////////////////////////////////////////////////////////
345/// Deletes point number ipoint
346
348{
349 if (ipoint < 0) return -1;
350 if (ipoint >= fNpoints) return -1;
351
352 fNpoints--;
359
360 Int_t j = -1;
361 for (Int_t i = 0; i < fNpoints + 1; i++) {
362 if (i == ipoint) continue;
363 j++;
364 newX[j] = fX[i];
365 newY[j] = fY[i];
366 newZ[j] = fZ[i];
367 newEX[j] = fEX[i];
368 newEY[j] = fEY[i];
369 newEZ[j] = fEZ[i];
370 }
371 delete [] fX;
372 delete [] fY;
373 delete [] fZ;
374 delete [] fEX;
375 delete [] fEY;
376 delete [] fEZ;
377 fX = newX;
378 fY = newY;
379 fZ = newZ;
380 fEX = newEX;
381 fEY = newEY;
382 fEZ = newEZ;
383 fSize = fNpoints;
384 if (fHistogram) {
385 delete fHistogram;
386 fHistogram = nullptr;
387 fDelaunay = nullptr;
388 }
389 return ipoint;
390}
391
392////////////////////////////////////////////////////////////////////////////////
393/// Set x, y and z values for point number i
394
396{
397 if (i < 0) return;
398 if (i >= fNpoints) {
399 // re-allocate the object
400 Double_t *savex = new Double_t[i+1];
401 Double_t *savey = new Double_t[i+1];
402 Double_t *savez = new Double_t[i+1];
403 Double_t *saveex = new Double_t[i+1];
404 Double_t *saveey = new Double_t[i+1];
405 Double_t *saveez = new Double_t[i+1];
406 if (fNpoints > 0) {
407 memcpy(savex, fX, fNpoints*sizeof(Double_t));
408 memcpy(savey, fY, fNpoints*sizeof(Double_t));
409 memcpy(savez, fZ, fNpoints*sizeof(Double_t));
413 }
414 if (fX) delete [] fX;
415 if (fY) delete [] fY;
416 if (fZ) delete [] fZ;
417 if (fEX) delete [] fEX;
418 if (fEY) delete [] fEY;
419 if (fEZ) delete [] fEZ;
420 fX = savex;
421 fY = savey;
422 fZ = savez;
423 fEX = saveex;
424 fEY = saveey;
425 fEZ = saveez;
426 fNpoints = i+1;
427 }
428 fX[i] = x;
429 fY[i] = y;
430 fZ[i] = z;
431}
432
433
434////////////////////////////////////////////////////////////////////////////////
435/// Set ex, ey and ez values for point number i
436
438{
439 if (i < 0) return;
440 if (i >= fNpoints) {
441 // re-allocate the object
443 }
444 fEX[i] = ex;
445 fEY[i] = ey;
446 fEZ[i] = ez;
447}
448
449
450////////////////////////////////////////////////////////////////////////////////
451/// Saves primitive as a C++ statement(s) on output stream out
452
454{
455 TString arrx = SavePrimitiveVector(out, "gr2derr_x", fNpoints, fX, kTRUE);
456 TString arry = SavePrimitiveVector(out, "gr2derr_y", fNpoints, fY);
457 TString arrz = SavePrimitiveVector(out, "gr2derr_z", fNpoints, fZ);
458 TString arrex = SavePrimitiveVector(out, "gr2derr_ex", fNpoints, fEX);
459 TString arrey = SavePrimitiveVector(out, "gr2derr_ey", fNpoints, fEY);
460 TString arrez = SavePrimitiveVector(out, "gr2derr_ez", fNpoints, fEZ);
461
462 SavePrimitiveConstructor(out, Class(), "gr2derr",
463 TString::Format("%d, %s.data(), %s.data(), %s.data(), %s.data(), %s.data(), %s.data()",
464 fNpoints, arrx.Data(), arry.Data(), arrz.Data(), arrex.Data(), arrey.Data(),
465 arrez.Data()),
466 kFALSE);
467
468 if (strcmp(GetName(), "Graph2D"))
469 out << " gr2derr->SetName(\"" << TString(GetName()).ReplaceSpecialCppChars() << "\");\n";
470
471 TString title = GetTitle();
472 if (fHistogram)
473 title = TString(fHistogram->GetTitle()) + ";" + fHistogram->GetXaxis()->GetTitle() + ";" +
475
476 out << " gr2derr->SetTitle(\"" << title.ReplaceSpecialCppChars() << "\");\n";
477
478 if (!fDirectory)
479 out << " gr2derr->SetDirectory(nullptr);\n";
480
481 SaveFillAttributes(out, "gr2derr", 0, 1001);
482 SaveLineAttributes(out, "gr2derr", 1, 1, 1);
483 SaveMarkerAttributes(out, "gr2derr", 1, 1, 1);
484
486
487 SavePrimitiveDraw(out, "gr2derr", option);
488}
489
490////////////////////////////////////////////////////////////////////////////////
491/// Stream an object of class TGraph2DErrors.
492
494{
495 if (b.IsReading()) {
497 Version_t R__v = b.ReadVersion(&R__s, &R__c);
498 b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
499 } else {
500 b.WriteClassBuffer(TGraph2DErrors::Class(),this);
501 }
502}
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition TAttFill.cxx:238
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition TAttLine.cxx:274
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
const char * GetTitle() const override
Returns title of object.
Definition TAxis.h:137
Buffer base class used for serializing objects.
Definition TBuffer.h:43
Graph 2D class with errors.
void Streamer(TBuffer &) override
Stream an object of class TGraph2DErrors.
Double_t GetErrorZ(Int_t bin) const override
This function is called by Graph2DFitChisquare.
~TGraph2DErrors() override
TGraph2DErrors destructor.
Double_t GetYminE() const override
Returns the Y minimum with errors.
Double_t GetXminE() const override
Returns the X minimum with errors.
Double_t * GetEZ() const override
Double_t * fEY
[fNpoints] array of Y errors
Double_t * GetEY() const override
void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z) override
Set x, y and z values for point number i.
void Scale(Double_t c1=1., Option_t *option="z") override
Multiply the values and errors of a TGraph2DErrors by a constant c1.
Double_t * fEZ
[fNpoints] array of Z errors
Double_t * GetEX() const override
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.
Double_t GetErrorX(Int_t bin) const override
This function is called by Graph2DFitChisquare.
virtual void AddPointError(Double_t x, Double_t y, Double_t z, Double_t ex=0., Double_t ey=0., Double_t ez=0.)
Add a point with errorbars to the graph.
void Print(Option_t *chopt="") const override
Print 2D graph and errors values.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Saves primitive as a C++ statement(s) on output stream out.
Double_t GetZminE() const override
Returns the Z minimum with errors.
TGraph2DErrors & operator=(const TGraph2DErrors &)
Assignment operator Copy everything except list of functions.
Double_t GetErrorY(Int_t bin) const override
This function is called by Graph2DFitChisquare.
Double_t * fEX
[fNpoints] array of X errors
TGraph2DErrors()
TGraph2DErrors default constructor.
Double_t GetYmaxE() const override
Returns the Y maximum with errors.
Double_t GetXmaxE() const override
Returns the X maximum with errors.
Double_t GetZmaxE() const override
Returns the Z maximum with errors.
void Set(Int_t n) override
Set number of points in the 2D graph.
static TClass * Class()
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:121
TGraph2D & operator=(const TGraph2D &)
Graph2D operator "=".
Definition TGraph2D.cxx:556
Double_t * fX
[fNpoints]
Definition TGraph2D.h:50
Double_t * fY
[fNpoints] Data set to be plotted
Definition TGraph2D.h:51
TDirectory * fDirectory
!Pointer to directory holding this 2D graph
Definition TGraph2D.h:60
virtual void Scale(Double_t c1=1., Option_t *option="z")
Multiply the values of a TGraph2D by a constant c1.
virtual void AddPoint(Double_t x, Double_t y, Double_t z)
Append a new point to the graph.
Definition TGraph2D.h:91
TList * fFunctions
Pointer to list of functions (fits and user)
Definition TGraph2D.h:57
Int_t fSize
!Real size of fX, fY and fZ
Definition TGraph2D.h:49
TAxis * GetZaxis()
Definition TH1.h:574
TAxis * GetXaxis()
Definition TH1.h:572
TAxis * GetYaxis()
Definition TH1.h:573
static void SavePrimitiveFunctions(std::ostream &out, const char *varname, TList *lst)
Save list of functions Also can be used by TGraph classes.
Definition TH1.cxx:7471
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
static TString SavePrimitiveVector(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Bool_t empty_line=kFALSE)
Save array in the output stream "out" as vector.
Definition TObject.cxx:788
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:822
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:771
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
Double_t ey[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Double_t ex[n]
Definition legend1.C:17