//*CMZ :  2.21/07 01/03/99  09.32.45  by  Rene Brun
//*CMZ :  2.21/06 12/02/99  15.31.46  by  Rene Brun
//*CMZ :  2.20/05 15/12/98  09.17.20  by  Rene Brun
//*CMZ :  2.00/08 19/05/98  11.12.07  by  Rene Brun
//*CMZ :  1.03/09 06/12/97  17.22.07  by  Fons Rademakers
//*-- Author :    Rene Brun   15/09/96

//*KEEP,CopyRight,T=C.
/*************************************************************************
 * Copyright(c) 1995-1999, The ROOT System, All rights reserved.         *
 * Authors: Rene Brun, Fons Rademakers.                                  *
 * For list of contributors see $ROOTSYS/AA_CREDITS.                     *
 *                                                                       *
 * Permission to use, copy, modify and distribute this software and its  *
 * documentation for non-commercial purposes is hereby granted without   *
 * fee, provided that the above copyright notice appears in all copies   *
 * and that both the copyright notice and this permission notice appear  *
 * in the supporting documentation. The authors make no claims about the *
 * suitability of this software for any purpose. It is provided "as is"  *
 * without express or implied warranty.                                  *
 *************************************************************************/
//*KEND.

#include <string.h>
#include <fstream.h>

//*KEEP,TROOT.
#include "TROOT.h"
//*KEEP,TGraphErrors,T=C++.
#include "TGraphErrors.h"
//*KEEP,TH1.
#include "TH1.h"
//*KEEP,TLine.
#include "TLine.h"
//*KEEP,TText.
#include "TText.h"
//*KEEP,TStyle.
#include "TStyle.h"
//*KEEP,TMath.
#include "TMath.h"
//*KEEP,TVirtualPad.
#include "TVirtualPad.h"
//*KEND.

ClassImp(TGraphErrors)

//______________________________________________________________________________
//
//   A TGraphErrors is a TGraph with error bars.
//   The various format options to draw a TGraphErrors are explained in
//     TGraphErrors::Paint.
//
//  The picture below has been generated by the following macro:
//------------------------------------------------------------------------
//{
//   gROOT->Reset();
//   c1 = new TCanvas("c1","A Simple Graph with error bars",200,10,700,500);
//
//   c1->SetFillColor(42);
//   c1->SetGrid();
//   c1->GetFrame()->SetFillColor(21);
//   c1->GetFrame()->SetBorderSize(12);
//
//   Int_t n = 10;
//   Float_t x[n]  = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
//   Float_t y[n]  = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
//   Float_t ex[n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
//   Float_t ey[n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};
//   gr = new TGraphErrors(n,x,y,ex,ey);
//   gr->SetTitle("TGraphErrors Example");
//   gr->SetMarkerColor(4);
//   gr->SetMarkerStyle(21);
//   gr->Draw("ALP");
//
//   c1->Update();
//}
//
/*

*/
//
//

//______________________________________________________________________________
 TGraphErrors::TGraphErrors(): TGraph()
{
//*-*-*-*-*-*-*-*-*-*-*TGraphErrors default constructor*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ================================
   fEX       = 0;
   fEY       = 0;
}


//______________________________________________________________________________
 TGraphErrors::TGraphErrors(Int_t n, Float_t *x, Float_t *y, Float_t *ex, Float_t *ey)
       : TGraph(n,x,y)
{
//*-*-*-*-*-*-*-*-*-*-*TGraphErrors normal constructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===============================
//
//  if ex or ey are null, the corresponding arrays are preset to zero

   if (n <= 0) {
      Error("TGraphErrors", "illegal number of points (%d)", n);
      return;
   }

   fEX       = new Float_t[n];
   fEY       = new Float_t[n];

   for (Int_t i=0;i<n;i++) {
      if (ex) fEX[i] = ex[i];
      else    fEX[i] = 0;
      if (ey) fEY[i] = ey[i];
      else    fEY[i] = 0;
   }
}

//______________________________________________________________________________
 TGraphErrors::~TGraphErrors()
{
//*-*-*-*-*-*-*-*-*-*-*TGraphErrors default destructor*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  ===============================

   delete [] fEX;
   delete [] fEY;
}

//______________________________________________________________________________
 void TGraphErrors::ComputeRange(Float_t &xmin, Float_t &ymin, Float_t &xmax, Float_t &ymax)
{
  for (Int_t i=0;i<fNpoints;i++) {
     if (fX[i] -fEX[i] < xmin) xmin = fX[i]-fEX[i];
     if (fX[i] +fEX[i] > xmax) xmax = fX[i]+fEX[i];
     if (fY[i] -fEY[i] < ymin) ymin = fY[i]-fEY[i];
     if (fY[i] +fEY[i] > ymax) ymax = fY[i]+fEY[i];
  }
}

//______________________________________________________________________________
 Float_t TGraphErrors::GetErrorX(Int_t i)
{
//    This function is called by GraphFitChisquare.
//    It returns the error along X at point i.

   if (i < 0 || i >= fNpoints) return -1;
   if (fEX) return fEX[i];
   return -1;
}

//______________________________________________________________________________
 Float_t TGraphErrors::GetErrorY(Int_t i)
{
//    This function is called by GraphFitChisquare.
//    It returns the error along Y at point i.

   if (i < 0 || i >= fNpoints) return -1;
   if (fEY) return fEY[i];
   return -1;
}

//______________________________________________________________________________
 void TGraphErrors::Paint(Option_t *option)
{
//*-*-*-*-*-*-*-*-*Draw this TGraphErrors with its current attributes*-*-*-*-*
//*-*              ==================================================

   const Int_t BASEMARKER=8;
   Float_t s2x, s2y, symbolsize;
   Float_t x, y, ex, ey, xl1, xl2, xr1, xr2, yup1, yup2, ylow1, ylow2, tx, ty;
   static Float_t cxx[11] = {1,1,0.6,0.6,1,1,0.6,0.5,1,0.6,0.6};
   static Float_t cyy[11] = {1,1,1,1,1,1,1,1,1,0.5,0.6};

   TGraph::Paint(option);

   TAttLine::Modify();

   symbolsize  = GetMarkerSize();
   Int_t mark  = GetMarkerStyle();
   Float_t cx  = 0;
   Float_t cy  = 0;
   if (mark >= 20 && mark < 31) {
      cx = cxx[mark-20];
      cy = cyy[mark-20];
   }

//*-*-      define the offset of the error bars due to the symbol size
   s2x  = gPad->PixeltoX(Int_t(0.5*symbolsize*BASEMARKER)) - gPad->PixeltoX(0);
   s2y  =-gPad->PixeltoY(Int_t(0.5*symbolsize*BASEMARKER)) + gPad->PixeltoY(0);
   tx   = 0.50*s2x;
   ty   = 0.50*s2y;

   for (Int_t i=0;i<fNpoints;i++) {
      x  = gPad->XtoPad(fX[i]);
      y  = gPad->YtoPad(fY[i]);
      if (y < gPad->GetUymin()) continue;
      if (y > gPad->GetUymax()) continue;
      ex = fEX[i];
      ey = fEY[i];
      xl1 = x - s2x*cx;
      xl2 = gPad->XtoPad(fX[i] - ex);
      if (xl1 > xl2) {
         gPad->PaintLine(xl1,y,xl2,y);
         gPad->PaintLine(xl2,y-ty,xl2,y+ty);
      }
      xr1 = x + s2x*cx;
      xr2 = gPad->XtoPad(fX[i] + ex);
      if (xr1 < xr2) {
         gPad->PaintLine(xr1,y,xr2,y);
         gPad->PaintLine(xr2,y-ty,xr2,y+ty);
      }
      yup1 = y + s2y*cy;
      yup2 = gPad->YtoPad(fY[i] + ey);
      if (yup2 > gPad->GetUymax()) yup2 =  gPad->GetUymax();
      if (yup2 > yup1) {
         gPad->PaintLine(x,yup1,x,yup2);
         gPad->PaintLine(x-tx,yup2,x+tx,yup2);
      }
      ylow1 = y - s2y*cy;
      ylow2 = gPad->YtoPad(fY[i] - ey);
      if (ylow2 < gPad->GetUymin()) ylow2 =  gPad->GetUymin();
      if (ylow2 < ylow1) {
         gPad->PaintLine(x,ylow1,x,ylow2);
         gPad->PaintLine(x-tx,ylow2,x+tx,ylow2);
      }
   }
}


//______________________________________________________________________________
 void TGraphErrors::Print(Option_t *)
{
//*-*-*-*-*-*-*-*-*-*-*Print graph and errors values*-*-*-*-*-*-*-*-*-*-*-*
//*-*                  =============================
//

   for (Int_t i=0;i<fNpoints;i++) {
      printf("x[%d]=%g, y[%d]=%g, ex[%d]=%g, ey[%d]=%gn",i,fX[i],i,fY[i],i,fEX[i],i,fEY[i]);
   }
}

//______________________________________________________________________________
 void TGraphErrors::SavePrimitive(ofstream &out, Option_t *option)
{
    // Save primitive as a C++ statement(s) on output stream out

   char quote = '"';
   out<<"   "<<endl;
   if (gROOT->ClassSaved(TGraphErrors::Class())) {
       out<<"   ";
   } else {
       out<<"   TGraphErrors *";
   }
   out<<"gre = new TGraphErrors("<<fNpoints<<");"<<endl;
   out<<"   gre->SetName("<<quote<<GetName()<<quote<<");"<<endl;
   out<<"   gre->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;

   SaveFillAttributes(out,"gre",0,1001);
   SaveLineAttributes(out,"gre",1,1,1);
   SaveMarkerAttributes(out,"gre",1,1,1);

   for (Int_t i=0;i<fNpoints;i++) {
      out<<"   gre->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<endl;
      out<<"   gre->SetPointError("<<i<<","<<fEX[i]<<","<<fEY[i]<<");"<<endl;
   }
   out<<"   gre->Draw("
      <<quote<<option<<quote<<");"<<endl;
}

//______________________________________________________________________________
 void TGraphErrors::SetPoint(Int_t i, Float_t x, Float_t y)
{
//*-*-*-*-*-*-*-*-*-*-*Set x and y values for point number i*-*-*-*-*-*-*-*-*
//*-*                  =====================================

   if (i < 0) return;
   if (i >= fNpoints) {
   // re-allocate the object
      Float_t *savex  = new Float_t[i+1];
      Float_t *savey  = new Float_t[i+1];
      Float_t *saveex = new Float_t[i+1];
      Float_t *saveey = new Float_t[i+1];
      if (fNpoints > 0) {
         memcpy(savex, fX, fNpoints*sizeof(Float_t));
         memcpy(savey, fY, fNpoints*sizeof(Float_t));
         memcpy(saveex,fEX,fNpoints*sizeof(Float_t));
         memcpy(saveey,fEY,fNpoints*sizeof(Float_t));
      }
      if (fX)  delete [] fX;
      if (fY)  delete [] fY;
      if (fEX) delete [] fEX;
      if (fEY) delete [] fEY;
      fX  = savex;
      fY  = savey;
      fEX = saveex;
      fEY = saveey;
      fNpoints = i+1;
   }
   fX[i] = x;
   fY[i] = y;
}

//______________________________________________________________________________
 void TGraphErrors::SetPointError(Int_t i, Float_t ex, Float_t ey)
{
//*-*-*-*-*-*-*-*-*-*-*Set ex and ey values for point number i*-*-*-*-*-*-*-*
//*-*                  =======================================

   if (i < 0) return;
   if (i >= fNpoints) {
   // re-allocate the object
      TGraphErrors::SetPoint(i,0,0);
   }
   fEX[i] = ex;
   fEY[i] = ey;
}

//______________________________________________________________________________
 void TGraphErrors::Streamer(TBuffer &b)
{
   // Stream an object of class TGraphErrors.

   if (b.IsReading()) {
      b.ReadVersion();  //Version_t v = b.ReadVersion();
      TGraph::Streamer(b);
      fEX = new Float_t[fNpoints];
      fEY = new Float_t[fNpoints];
      b.ReadFastArray(fEX,fNpoints);
      b.ReadFastArray(fEY,fNpoints);
   } else {
      b.WriteVersion(TGraphErrors::IsA());
      TGraph::Streamer(b);
      b.WriteFastArray(fEX,fNpoints);
      b.WriteFastArray(fEY,fNpoints);
   }
}


ROOT page - Class index - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.