TGraph2DErrors.cxx

Go to the documentation of this file.
00001 // @(#)root/hist:$Id: TGraph2DErrors.cxx,v 1.00
00002 // Author: Olivier Couet
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "TROOT.h"
00014 #include "TClass.h"
00015 #include "TGraph2DErrors.h"
00016 #include "TMath.h"
00017 #include "TPolyMarker.h"
00018 #include "TVirtualPad.h"
00019 #include "TVirtualFitter.h"
00020 #include "THLimitsFinder.h"
00021 #include "TStyle.h"
00022 
00023 ClassImp(TGraph2DErrors)
00024 
00025 //______________________________________________________________________________
00026 /* Begin_Html
00027 <center><h2>Graph 2D class with errors</h2></center>
00028 A TGraph2DErrors is a TGraph2D with errors. It behaves like a TGraph2D and has
00029 the same drawing options.
00030 <p>
00031 The <tt>"ERR"</tt> drawing option allows to display the error bars. The
00032 following example shows how to use it:
00033 
00034 End_Html
00035 Begin_Macro(source)
00036 {
00037    TCanvas *c = new TCanvas("c","Graph2DErrors example",0,0,600,600);
00038    Double_t P = 6.;
00039    Int_t np   = 200;
00040 
00041    Double_t *rx=0, *ry=0, *rz=0;
00042    Double_t *ex=0, *ey=0, *ez=0;
00043 
00044    rx = new Double_t[np];
00045    ry = new Double_t[np];
00046    rz = new Double_t[np];
00047    ex = new Double_t[np];
00048    ey = new Double_t[np];
00049    ez = new Double_t[np];
00050 
00051    TRandom *r = new TRandom();
00052 
00053    for (Int_t N=0; N<np;N++) {
00054       rx[N] = 2*P*(r->Rndm(N))-P;
00055       ry[N] = 2*P*(r->Rndm(N))-P;
00056       rz[N] = rx[N]*rx[N]-ry[N]*ry[N];
00057       rx[N] = 10.+rx[N];
00058       ry[N] = 10.+ry[N];
00059       rz[N] = 40.+rz[N];
00060       ex[N] = r->Rndm(N);
00061       ey[N] = r->Rndm(N);
00062       ez[N] = 10*r->Rndm(N);
00063    }
00064 
00065    TGraph2DErrors *dte = new TGraph2DErrors(np, rx, ry, rz, ex, ey, ez);
00066    dte->SetTitle("TGraph2D with error bars: option \"ERR\"");
00067    dte->SetFillColor(29);
00068    dte->SetMarkerSize(0.8);
00069    dte->SetMarkerStyle(20);
00070    dte->SetMarkerColor(kRed);
00071    dte->SetLineColor(kBlue-3);
00072    dte->SetLineWidth(2);
00073    dte->Draw("err p0");
00074    gPad->SetLogy(1);
00075    return c;
00076 }
00077 End_Macro
00078 */
00079 
00080 
00081 //______________________________________________________________________________
00082 TGraph2DErrors::TGraph2DErrors(): TGraph2D()
00083 {
00084    // TGraph2DErrors default constructor
00085 
00086    fEX = 0;
00087    fEY = 0;
00088    fEZ = 0;
00089 }
00090 
00091 
00092 //______________________________________________________________________________
00093 TGraph2DErrors::TGraph2DErrors(Int_t n)
00094                : TGraph2D(n)
00095 {
00096    // TGraph2DErrors normal constructor
00097    // the arrays are preset to zero
00098 
00099    if (n <= 0) {
00100       Error("TGraph2DErrors", "Invalid number of points (%d)", n);
00101       return;
00102    }
00103 
00104    fEX = new Double_t[n];
00105    fEY = new Double_t[n];
00106    fEZ = new Double_t[n];
00107 
00108    for (Int_t i=0;i<n;i++) {
00109       fEX[i] = 0;
00110       fEY[i] = 0;
00111       fEZ[i] = 0;
00112    }
00113 }
00114 
00115 
00116 //______________________________________________________________________________
00117 TGraph2DErrors::TGraph2DErrors(Int_t n, Double_t *x, Double_t *y, Double_t *z,
00118                                Double_t *ex, Double_t *ey, Double_t *ez, Option_t *)
00119                :TGraph2D(n, x, y, z)
00120 {
00121    // TGraph2DErrors constructor with doubles vectors as input.
00122 
00123    if (n <= 0) {
00124       Error("TGraphErrors", "Invalid number of points (%d)", n);
00125       return;
00126    }
00127 
00128    fEX = new Double_t[n];
00129    fEY = new Double_t[n];
00130    fEZ = new Double_t[n];
00131 
00132    for (Int_t i=0;i<n;i++) {
00133       if (ex) fEX[i] = ex[i];
00134       else    fEX[i] = 0;
00135       if (ey) fEY[i] = ey[i];
00136       else    fEY[i] = 0;
00137       if (ez) fEZ[i] = ez[i];
00138       else    fEZ[i] = 0;
00139    }
00140 }
00141 
00142 
00143 //______________________________________________________________________________
00144 TGraph2DErrors::~TGraph2DErrors()
00145 {
00146    // TGraph2DErrors destructor.
00147 
00148    delete [] fEX;
00149    delete [] fEY;
00150    delete [] fEZ;
00151 }
00152 
00153 
00154 //______________________________________________________________________________
00155 Double_t TGraph2DErrors::GetErrorX(Int_t i) const
00156 {
00157    // This function is called by Graph2DFitChisquare.
00158    // It returns the error along X at point i.
00159 
00160    if (i < 0 || i >= fNpoints) return -1;
00161    if (fEX) return fEX[i];
00162    return -1;
00163 }
00164 
00165 
00166 //______________________________________________________________________________
00167 Double_t TGraph2DErrors::GetErrorY(Int_t i) const
00168 {
00169    // This function is called by Graph2DFitChisquare.
00170    // It returns the error along X at point i.
00171 
00172    if (i < 0 || i >= fNpoints) return -1;
00173    if (fEY) return fEY[i];
00174    return -1;
00175 }
00176 
00177 
00178 //______________________________________________________________________________
00179 Double_t TGraph2DErrors::GetErrorZ(Int_t i) const
00180 {
00181    // This function is called by Graph2DFitChisquare.
00182    // It returns the error along X at point i.
00183 
00184    if (i < 0 || i >= fNpoints) return -1;
00185    if (fEZ) return fEZ[i];
00186    return -1;
00187 }
00188 
00189 
00190 //______________________________________________________________________________
00191 Double_t TGraph2DErrors::GetXmaxE() const
00192 {
00193    // Returns the X maximum with errors.
00194 
00195    Double_t v = fX[0]+fEX[0];
00196    for (Int_t i=1; i<fNpoints; i++) if (fX[i]+fEX[i]>v) v=fX[i]+fEX[i];
00197    return v;
00198 }
00199 
00200 
00201 //______________________________________________________________________________
00202 Double_t TGraph2DErrors::GetXminE() const
00203 {
00204    // Returns the X minimum with errors.
00205 
00206    Double_t v = fX[0]-fEX[0];
00207    for (Int_t i=1; i<fNpoints; i++) if (fX[i]-fEX[i]<v) v=fX[i]-fEX[i];
00208    return v;
00209 }
00210 
00211 
00212 //______________________________________________________________________________
00213 Double_t TGraph2DErrors::GetYmaxE() const
00214 {
00215    // Returns the Y maximum with errors.
00216 
00217    Double_t v = fY[0]+fEY[0];
00218    for (Int_t i=1; i<fNpoints; i++) if (fY[i]+fEY[i]>v) v=fY[i]+fEY[i];
00219    return v;
00220 }
00221 
00222 
00223 //______________________________________________________________________________
00224 Double_t TGraph2DErrors::GetYminE() const
00225 {
00226    // Returns the Y minimum with errors.
00227 
00228    Double_t v = fY[0]+fEY[0];
00229    for (Int_t i=1; i<fNpoints; i++) if (fY[i]-fEY[i]<v) v=fY[i]-fEY[i];
00230    return v;
00231 }
00232 
00233 
00234 //______________________________________________________________________________
00235 Double_t TGraph2DErrors::GetZmaxE() const
00236 {
00237    // Returns the Z maximum with errors.
00238 
00239    Double_t v = fZ[0]+fEZ[0];
00240    for (Int_t i=1; i<fNpoints; i++) if (fZ[i]+fEZ[i]>v) v=fZ[i]+fEZ[i];
00241    return v;
00242 }
00243 
00244 
00245 //______________________________________________________________________________
00246 Double_t TGraph2DErrors::GetZminE() const
00247 {
00248    // Returns the Z minimum with errors.
00249 
00250    Double_t v = fZ[0]+fEZ[0];
00251    for (Int_t i=1; i<fNpoints; i++) if (fZ[i]-fEZ[i]<v) v=fZ[i]-fEZ[i];
00252    return v;
00253 }
00254 
00255 
00256 //______________________________________________________________________________
00257 void TGraph2DErrors::Set(Int_t n)
00258 {
00259    // Set number of points in the 2D graph.
00260    // Existing coordinates are preserved.
00261    // New coordinates above fNpoints are preset to 0.
00262 
00263    if (n < 0) n = 0;
00264    if (n == fNpoints) return;
00265    if (n >  fNpoints) SetPointError(n,0,0,0);
00266    fNpoints = n;
00267 }
00268 
00269 
00270 //______________________________________________________________________________
00271 void TGraph2DErrors::SetPoint(Int_t i, Double_t x, Double_t y, Double_t z)
00272 {
00273    // Set x, y and z values for point number i
00274 
00275    if (i < 0) return;
00276    if (i >= fNpoints) {
00277    // re-allocate the object
00278       Double_t *savex  = new Double_t[i+1];
00279       Double_t *savey  = new Double_t[i+1];
00280       Double_t *savez  = new Double_t[i+1];
00281       Double_t *saveex = new Double_t[i+1];
00282       Double_t *saveey = new Double_t[i+1];
00283       Double_t *saveez = new Double_t[i+1];
00284       if (fNpoints > 0) {
00285          memcpy(savex, fX, fNpoints*sizeof(Double_t));
00286          memcpy(savey, fY, fNpoints*sizeof(Double_t));
00287          memcpy(savez, fZ, fNpoints*sizeof(Double_t));
00288          memcpy(saveex,fEX,fNpoints*sizeof(Double_t));
00289          memcpy(saveey,fEY,fNpoints*sizeof(Double_t));
00290          memcpy(saveez,fEZ,fNpoints*sizeof(Double_t));
00291       }
00292       if (fX)  delete [] fX;
00293       if (fY)  delete [] fY;
00294       if (fZ)  delete [] fZ;
00295       if (fEX) delete [] fEX;
00296       if (fEY) delete [] fEY;
00297       if (fEZ) delete [] fEZ;
00298       fX  = savex;
00299       fY  = savey;
00300       fZ  = savez;
00301       fEX = saveex;
00302       fEY = saveey;
00303       fEZ = saveez;
00304       fNpoints = i+1;
00305    }
00306    fX[i] = x;
00307    fY[i] = y;
00308    fZ[i] = z;
00309 }
00310 
00311 
00312 //______________________________________________________________________________
00313 void TGraph2DErrors::SetPointError(Int_t i, Double_t ex, Double_t ey, Double_t ez)
00314 {
00315    // Set ex, ey and ez values for point number i
00316 
00317    if (i < 0) return;
00318    if (i >= fNpoints) {
00319       // re-allocate the object
00320       TGraph2DErrors::SetPoint(i,0,0,0);
00321    }
00322    fEX[i] = ex;
00323    fEY[i] = ey;
00324    fEZ[i] = ez;
00325 }
00326 
00327 
00328 //______________________________________________________________________________
00329 void TGraph2DErrors::Streamer(TBuffer &b)
00330 {
00331    // Stream an object of class TGraphErrors.
00332 
00333    if (b.IsReading()) {
00334       UInt_t R__s, R__c;
00335       Version_t R__v = b.ReadVersion(&R__s, &R__c);
00336       b.ReadClassBuffer(TGraph2DErrors::Class(), this, R__v, R__s, R__c);
00337    } else {
00338       b.WriteClassBuffer(TGraph2DErrors::Class(),this);
00339    }
00340 }

Generated on Tue Jul 5 14:24:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1