Logo ROOT   6.08/07
Reference Guide
TGraphErrors.cxx
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Rene Brun 15/09/96
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 <string.h>
13 
14 #include "Riostream.h"
15 #include "TROOT.h"
16 #include "TGraphErrors.h"
17 #include "TStyle.h"
18 #include "TMath.h"
19 #include "TArrow.h"
20 #include "TBox.h"
21 #include "TVirtualPad.h"
22 #include "TH1.h"
23 #include "TF1.h"
24 #include "TVector.h"
25 #include "TVectorD.h"
26 #include "TStyle.h"
27 #include "TClass.h"
28 #include "TSystem.h"
29 #include <string>
30 
32 
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 /** \class TGraphErrors
37  \ingroup Hist
38 A TGraphErrors is a TGraph with error bars.
39 
40 The TGraphErrors painting is performed thanks to the TGraphPainter
41 class. All details about the various painting options are given in this class.
42 
43 The picture below gives an example:
44 
45 Begin_Macro(source)
46 {
47  c1 = new TCanvas("c1","A Simple Graph with error bars",200,10,700,500);
48  c1->SetFillColor(42);
49  c1->SetGrid();
50  c1->GetFrame()->SetFillColor(21);
51  c1->GetFrame()->SetBorderSize(12);
52  const Int_t n = 10;
53  Double_t x[n] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
54  Double_t y[n] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
55  Double_t ex[n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
56  Double_t ey[n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};
57  gr = new TGraphErrors(n,x,y,ex,ey);
58  gr->SetTitle("TGraphErrors Example");
59  gr->SetMarkerColor(4);
60  gr->SetMarkerStyle(21);
61  gr->Draw("ALP");
62  return c1;
63 }
64 End_Macro
65 */
66 
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// TGraphErrors default constructor.
70 
72 {
73  if (!CtorAllocate()) return;
74 }
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// TGraphErrors normal constructor.
79 ///
80 /// the arrays are preset to zero
81 
83  : TGraph(n)
84 {
85  if (!CtorAllocate()) return;
86  FillZero(0, fNpoints);
87 }
88 
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// TGraphErrors normal constructor.
92 ///
93 /// if ex or ey are null, the corresponding arrays are preset to zero
94 
96  : TGraph(n, x, y)
97 {
98  if (!CtorAllocate()) return;
99 
100  for (Int_t i = 0; i < n; i++) {
101  if (ex) fEX[i] = ex[i];
102  else fEX[i] = 0;
103  if (ey) fEY[i] = ey[i];
104  else fEY[i] = 0;
105  }
106 }
107 
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// TGraphErrors normal constructor.
111 ///
112 /// if ex or ey are null, the corresponding arrays are preset to zero
113 
115  : TGraph(n, x, y)
116 {
117  if (!CtorAllocate()) return;
118 
119  n = sizeof(Double_t) * fNpoints;
120  if (ex) memcpy(fEX, ex, n);
121  else memset(fEX, 0, n);
122  if (ey) memcpy(fEY, ey, n);
123  else memset(fEY, 0, n);
124 }
125 
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 /// constructor with four vectors of floats in input
129 /// A grapherrors is built with the X coordinates taken from vx and Y coord from vy
130 /// and the errors from vectors vex and vey.
131 /// The number of points in the graph is the minimum of number of points
132 /// in vx and vy.
133 
134 TGraphErrors::TGraphErrors(const TVectorF &vx, const TVectorF &vy, const TVectorF &vex, const TVectorF &vey)
135  : TGraph(TMath::Min(vx.GetNrows(), vy.GetNrows()), vx.GetMatrixArray(), vy.GetMatrixArray() )
136 {
137  if (!CtorAllocate()) return;
138  Int_t ivexlow = vex.GetLwb();
139  Int_t iveylow = vey.GetLwb();
140  for (Int_t i = 0; i < fNpoints; i++) {
141  fEX[i] = vex(i + ivexlow);
142  fEY[i] = vey(i + iveylow);
143  }
144 }
145 
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// constructor with four vectors of doubles in input
149 /// A grapherrors is built with the X coordinates taken from vx and Y coord from vy
150 /// and the errors from vectors vex and vey.
151 /// The number of points in the graph is the minimum of number of points
152 /// in vx and vy.
153 
154 TGraphErrors::TGraphErrors(const TVectorD &vx, const TVectorD &vy, const TVectorD &vex, const TVectorD &vey)
155  : TGraph(TMath::Min(vx.GetNrows(), vy.GetNrows()), vx.GetMatrixArray(), vy.GetMatrixArray() )
156 {
157  if (!CtorAllocate()) return;
158  Int_t ivexlow = vex.GetLwb();
159  Int_t iveylow = vey.GetLwb();
160  for (Int_t i = 0; i < fNpoints; i++) {
161  fEX[i] = vex(i + ivexlow);
162  fEY[i] = vey(i + iveylow);
163  }
164 }
165 
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// TGraphErrors copy constructor
169 
171  : TGraph(gr)
172 {
173  if (!CtorAllocate()) return;
174 
175  Int_t n = sizeof(Double_t) * fNpoints;
176  memcpy(fEX, gr.fEX, n);
177  memcpy(fEY, gr.fEY, n);
178 }
179 
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// TGraphErrors assignment operator
183 
185 {
186  if (this != &gr) {
187  TGraph::operator=(gr);
188  // N.B CtorAllocate does not delete arrays
189  if (fEX) delete [] fEX;
190  if (fEY) delete [] fEY;
191  if (!CtorAllocate()) return *this;
192 
193  Int_t n = sizeof(Double_t) * fNpoints;
194  memcpy(fEX, gr.fEX, n);
195  memcpy(fEY, gr.fEY, n);
196  }
197  return *this;
198 }
199 
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 /// TGraphErrors constructor importing its parameters from the TH1 object passed as argument
203 
205  : TGraph(h)
206 {
207  if (!CtorAllocate()) return;
208 
209  for (Int_t i = 0; i < fNpoints; i++) {
210  fEX[i] = h->GetBinWidth(i + 1) * gStyle->GetErrorX();
211  fEY[i] = h->GetBinError(i + 1);
212  }
213 }
214 
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// GraphErrors constructor reading input from filename
218 /// filename is assumed to contain at least 2 columns of numbers
219 ///
220 /// Convention for format (default="%lg %lg %lg %lg)
221 /// - format = "%lg %lg" read only 2 first columns into X,Y
222 /// - format = "%lg %lg %lg" read only 3 first columns into X,Y and EY
223 /// - format = "%lg %lg %lg %lg" read only 4 first columns into X,Y,EX,EY.
224 ///
225 /// For files separated by a specific delimiter different from ' ' and '\t' (e.g. ';' in csv files)
226 /// you can avoid using %*s to bypass this delimiter by explicitly specify the "option" argument,
227 /// e.g. option=" \t,;" for columns of figures separated by any of these characters (' ', '\t', ',', ';')
228 /// used once (e.g. "1;1") or in a combined way (" 1;,;; 1").
229 /// Note in that case, the instantiation is about 2 times slower.
230 /// In case a delimiter is specified, the format "%lg %lg %lg" will read X,Y,EX.
231 
232 TGraphErrors::TGraphErrors(const char *filename, const char *format, Option_t *option)
233  : TGraph(100)
234 {
235  if (!CtorAllocate()) return;
236  Double_t x, y, ex, ey;
237  TString fname = filename;
238  gSystem->ExpandPathName(fname);
239  std::ifstream infile(fname.Data());
240  if (!infile.good()) {
241  MakeZombie();
242  Error("TGraphErrors", "Cannot open file: %s, TGraphErrors is Zombie", filename);
243  fNpoints = 0;
244  return;
245  }
246  std::string line;
247  Int_t np = 0;
248 
249  if (strcmp(option, "") == 0) { // No delimiters specified (standard constructor).
250 
251  Int_t ncol = CalculateScanfFields(format); //count number of columns in format
252  Int_t res;
253  while (std::getline(infile, line, '\n')) {
254  ex = ey = 0;
255  if (ncol < 3) {
256  res = sscanf(line.c_str(), format, &x, &y);
257  } else if (ncol < 4) {
258  res = sscanf(line.c_str(), format, &x, &y, &ey);
259  } else {
260  res = sscanf(line.c_str(), format, &x, &y, &ex, &ey);
261  }
262  if (res < 2) {
263  continue; //skip empty and ill-formed lines
264  }
265  SetPoint(np, x, y);
266  SetPointError(np, ex, ey);
267  np++;
268  }
269  Set(np);
270 
271  } else { // A delimiter has been specified in "option"
272 
273  // Checking format and creating its boolean equivalent
274  TString format_ = TString(format) ;
275  format_.ReplaceAll(" ", "") ;
276  format_.ReplaceAll("\t", "") ;
277  format_.ReplaceAll("lg", "") ;
278  format_.ReplaceAll("s", "") ;
279  format_.ReplaceAll("%*", "0") ;
280  format_.ReplaceAll("%", "1") ;
281  if (!format_.IsDigit()) {
282  Error("TGraphErrors", "Incorrect input format! Allowed format tags are {\"%%lg\",\"%%*lg\" or \"%%*s\"}");
283  return ;
284  }
285  Int_t ntokens = format_.Length() ;
286  if (ntokens < 2) {
287  Error("TGraphErrors", "Incorrect input format! Only %d tag(s) in format whereas at least 2 \"%%lg\" tags are expected!", ntokens);
288  return ;
289  }
290  Int_t ntokensToBeSaved = 0 ;
291  Bool_t * isTokenToBeSaved = new Bool_t [ntokens] ;
292  for (Int_t idx = 0; idx < ntokens; idx++) {
293  isTokenToBeSaved[idx] = TString::Format("%c", format_[idx]).Atoi() ; //atoi(&format_[idx]) does not work for some reason...
294  if (isTokenToBeSaved[idx] == 1) {
295  ntokensToBeSaved++ ;
296  }
297  }
298  if (ntokens >= 2 && (ntokensToBeSaved < 2 || ntokensToBeSaved > 4)) { //first condition not to repeat the previous error message
299  Error("TGraphErrors", "Incorrect input format! There are %d \"%%lg\" tag(s) in format whereas 2,3 or 4 are expected!", ntokensToBeSaved);
300  delete [] isTokenToBeSaved ;
301  return ;
302  }
303 
304  // Initializing loop variables
305  Bool_t isLineToBeSkipped = kFALSE ; //empty and ill-formed lines
306  char * token = NULL ;
307  TString token_str = "" ;
308  Int_t token_idx = 0 ;
309  Double_t * value = new Double_t [4] ; //x,y,ex,ey buffers
310  for (Int_t k = 0; k < 4; k++) {
311  value[k] = 0. ;
312  }
313  Int_t value_idx = 0 ;
314 
315  // Looping
316  while (std::getline(infile, line, '\n')) {
317  if (line != "") {
318  if (line[line.size() - 1] == char(13)) { // removing DOS CR character
319  line.erase(line.end() - 1, line.end()) ;
320  }
321  token = strtok(const_cast<char*>(line.c_str()), option) ;
322  while (token != NULL && value_idx < ntokensToBeSaved) {
323  if (isTokenToBeSaved[token_idx]) {
324  token_str = TString(token) ;
325  token_str.ReplaceAll("\t", "") ;
326  if (!token_str.IsFloat()) {
327  isLineToBeSkipped = kTRUE ;
328  break ;
329  } else {
330  value[value_idx] = token_str.Atof() ;
331  value_idx++ ;
332  }
333  }
334  token = strtok(NULL, option) ; //next token
335  token_idx++ ;
336  }
337  if (!isLineToBeSkipped && value_idx > 1) { //i.e. 2,3 or 4
338  x = value[0] ;
339  y = value[1] ;
340  ex = value[2] ;
341  ey = value[3] ;
342  SetPoint(np, x, y) ;
343  SetPointError(np, ex, ey);
344  np++ ;
345  }
346  }
347  isLineToBeSkipped = kFALSE ;
348  token = NULL ;
349  token_idx = 0 ;
350  value_idx = 0 ;
351  }
352  Set(np) ;
353 
354  // Cleaning
355  delete [] isTokenToBeSaved ;
356  delete [] value ;
357  delete token ;
358  }
359  infile.close();
360 }
361 
362 
363 ////////////////////////////////////////////////////////////////////////////////
364 /// TGraphErrors default destructor.
365 
367 {
368  delete [] fEX;
369  delete [] fEY;
370 }
371 
372 
373 ////////////////////////////////////////////////////////////////////////////////
374 /// apply function to all the data points
375 /// y = f(x,y)
376 ///
377 /// The error is calculated as ey=(f(x,y+ey)-f(x,y-ey))/2
378 /// This is the same as error(fy) = df/dy * ey for small errors
379 ///
380 /// For generic functions the symmetric errors might become non-symmetric
381 /// and are averaged here. Use TGraphAsymmErrors if desired.
382 ///
383 /// error on x doesn't change
384 /// function suggested/implemented by Miroslav Helbich <helbich@mail.desy.de>
385 
387 {
388  Double_t x, y, ex, ey;
389 
390  if (fHistogram) {
391  delete fHistogram;
392  fHistogram = 0;
393  }
394  for (Int_t i = 0; i < GetN(); i++) {
395  GetPoint(i, x, y);
396  ex = GetErrorX(i);
397  ey = GetErrorY(i);
398 
399  SetPoint(i, x, f->Eval(x, y));
400  SetPointError(i, ex, TMath::Abs(f->Eval(x, y + ey) - f->Eval(x, y - ey)) / 2.);
401  }
402  if (gPad) gPad->Modified();
403 }
404 
405 
406 ////////////////////////////////////////////////////////////////////////////////
407 /// Calculate scan fields.
408 
410 {
411  Int_t fields = 0;
412  while ((fmt = strchr(fmt, '%'))) {
413  Bool_t skip = kFALSE;
414  while (*(++fmt)) {
415  if ('[' == *fmt) {
416  if (*++fmt && '^' == *fmt) ++fmt; // "%[^]a]"
417  if (*++fmt && ']' == *fmt) ++fmt; // "%[]a]" or "%[^]a]"
418  while (*fmt && *fmt != ']')
419  ++fmt;
420  if (!skip) ++fields;
421  break;
422  }
423  if ('%' == *fmt) break; // %% literal %
424  if ('*' == *fmt) {
425  skip = kTRUE; // %*d -- skip a number
426  } else if (strchr("dDiouxXxfegEscpn", *fmt)) {
427  if (!skip) ++fields;
428  break;
429  }
430  // skip modifiers & field width
431  }
432  }
433  return fields;
434 }
435 
436 
437 ////////////////////////////////////////////////////////////////////////////////
438 /// Compute range.
439 
441 {
442  TGraph::ComputeRange(xmin, ymin, xmax, ymax);
443 
444  for (Int_t i = 0; i < fNpoints; i++) {
445  if (fX[i] - fEX[i] < xmin) {
446  if (gPad && gPad->GetLogx()) {
447  if (fEX[i] < fX[i]) xmin = fX[i] - fEX[i];
448  else xmin = TMath::Min(xmin, fX[i] / 3);
449  } else {
450  xmin = fX[i] - fEX[i];
451  }
452  }
453  if (fX[i] + fEX[i] > xmax) xmax = fX[i] + fEX[i];
454  if (fY[i] - fEY[i] < ymin) {
455  if (gPad && gPad->GetLogy()) {
456  if (fEY[i] < fY[i]) ymin = fY[i] - fEY[i];
457  else ymin = TMath::Min(ymin, fY[i] / 3);
458  } else {
459  ymin = fY[i] - fEY[i];
460  }
461  }
462  if (fY[i] + fEY[i] > ymax) ymax = fY[i] + fEY[i];
463  }
464 }
465 
466 
467 ////////////////////////////////////////////////////////////////////////////////
468 /// Copy and release.
469 
471  Int_t ibegin, Int_t iend, Int_t obegin)
472 {
473  CopyPoints(newarrays, ibegin, iend, obegin);
474  if (newarrays) {
475  delete[] fX;
476  fX = newarrays[2];
477  delete[] fY;
478  fY = newarrays[3];
479  delete[] fEX;
480  fEX = newarrays[0];
481  delete[] fEY;
482  fEY = newarrays[1];
483  delete[] newarrays;
484  }
485 }
486 
487 
488 ////////////////////////////////////////////////////////////////////////////////
489 /// Copy errors from fEX and fEY to arrays[0] and arrays[1]
490 /// or to fX and fY. Copy points.
491 
493  Int_t obegin)
494 {
495  if (TGraph::CopyPoints(arrays ? arrays + 2 : 0, ibegin, iend, obegin)) {
496  Int_t n = (iend - ibegin) * sizeof(Double_t);
497  if (arrays) {
498  memmove(&arrays[0][obegin], &fEX[ibegin], n);
499  memmove(&arrays[1][obegin], &fEY[ibegin], n);
500  } else {
501  memmove(&fEX[obegin], &fEX[ibegin], n);
502  memmove(&fEY[obegin], &fEY[ibegin], n);
503  }
504  return kTRUE;
505  } else {
506  return kFALSE;
507  }
508 }
509 
510 
511 ////////////////////////////////////////////////////////////////////////////////
512 /// Constructor allocate.
513 ///Note: This function should be called only from the constructor
514 /// since it does not delete previously existing arrays
515 
517 {
518 
519  if (!fNpoints) {
520  fEX = fEY = 0;
521  return kFALSE;
522  } else {
523  fEX = new Double_t[fMaxSize];
524  fEY = new Double_t[fMaxSize];
525  }
526  return kTRUE;
527 }
528 
529 ////////////////////////////////////////////////////////////////////////////////
530 /// protected function to perform the merge operation of a graph with errors
531 
533 {
534  if (g->GetN() == 0) return kFALSE;
535 
536  Double_t * ex = g->GetEX();
537  Double_t * ey = g->GetEY();
538  if (ex == 0 || ey == 0 ) {
539  if (g->IsA() != TGraph::Class() )
540  Warning("DoMerge","Merging a %s is not compatible with a TGraphErrors - errors will be ignored",g->IsA()->GetName());
541  return TGraph::DoMerge(g);
542  }
543  for (Int_t i = 0 ; i < g->GetN(); i++) {
544  Int_t ipoint = GetN();
545  Double_t x = g->GetX()[i];
546  Double_t y = g->GetY()[i];
547  SetPoint(ipoint, x, y);
548  SetPointError( ipoint, ex[i], ey[i] );
549  }
550  return kTRUE;
551 }
552 
553 
554 ////////////////////////////////////////////////////////////////////////////////
555 /// Set zero values for point arrays in the range [begin, end]
556 
557 void TGraphErrors::FillZero(Int_t begin, Int_t end, Bool_t from_ctor)
558 {
559  if (!from_ctor) {
560  TGraph::FillZero(begin, end, from_ctor);
561  }
562  Int_t n = (end - begin) * sizeof(Double_t);
563  memset(fEX + begin, 0, n);
564  memset(fEY + begin, 0, n);
565 }
566 
567 
568 ////////////////////////////////////////////////////////////////////////////////
569 /// This function is called by GraphFitChisquare.
570 /// It returns the error along X at point i.
571 
573 {
574  if (i < 0 || i >= fNpoints) return -1;
575  if (fEX) return fEX[i];
576  return -1;
577 }
578 
579 
580 ////////////////////////////////////////////////////////////////////////////////
581 /// This function is called by GraphFitChisquare.
582 /// It returns the error along Y at point i.
583 
585 {
586  if (i < 0 || i >= fNpoints) return -1;
587  if (fEY) return fEY[i];
588  return -1;
589 }
590 
591 
592 ////////////////////////////////////////////////////////////////////////////////
593 /// This function is called by GraphFitChisquare.
594 /// It returns the error along X at point i.
595 
597 {
598  if (i < 0 || i >= fNpoints) return -1;
599  if (fEX) return fEX[i];
600  return -1;
601 }
602 
603 
604 ////////////////////////////////////////////////////////////////////////////////
605 /// This function is called by GraphFitChisquare.
606 /// It returns the error along X at point i.
607 
609 {
610  if (i < 0 || i >= fNpoints) return -1;
611  if (fEX) return fEX[i];
612  return -1;
613 }
614 
615 
616 ////////////////////////////////////////////////////////////////////////////////
617 /// This function is called by GraphFitChisquare.
618 /// It returns the error along X at point i.
619 
621 {
622  if (i < 0 || i >= fNpoints) return -1;
623  if (fEY) return fEY[i];
624  return -1;
625 }
626 
627 
628 ////////////////////////////////////////////////////////////////////////////////
629 /// This function is called by GraphFitChisquare.
630 /// It returns the error along X at point i.
631 
633 {
634  if (i < 0 || i >= fNpoints) return -1;
635  if (fEY) return fEY[i];
636  return -1;
637 }
638 
639 ////////////////////////////////////////////////////////////////////////////////
640 /// Adds all graphs with errors from the collection to this graph.
641 /// Returns the total number of poins in the result or -1 in case of an error.
642 
644 {
645  TIter next(li);
646  while (TObject* o = next()) {
647  TGraph *g = dynamic_cast<TGraph*>(o);
648  if (!g) {
649  Error("Merge",
650  "Cannot merge - an object which doesn't inherit from TGraph found in the list");
651  return -1;
652  }
653  int n0 = GetN();
654  int n1 = n0+g->GetN();
655  Set(n1);
656  Double_t * x = g->GetX();
657  Double_t * y = g->GetY();
658  Double_t * ex = g->GetEX();
659  Double_t * ey = g->GetEY();
660  for (Int_t i = 0 ; i < g->GetN(); i++) {
661  SetPoint(n0+i, x[i], y[i]);
662  if (ex) fEX[n0+i] = ex[i];
663  if (ey) fEY[n0+i] = ey[i];
664  }
665  }
666  return GetN();
667 }
668 
669 ////////////////////////////////////////////////////////////////////////////////
670 /// Print graph and errors values.
671 
673 {
674  for (Int_t i = 0; i < fNpoints; i++) {
675  printf("x[%d]=%g, y[%d]=%g, ex[%d]=%g, ey[%d]=%g\n", i, fX[i], i, fY[i], i, fEX[i], i, fEY[i]);
676  }
677 }
678 
679 
680 ////////////////////////////////////////////////////////////////////////////////
681 /// Save primitive as a C++ statement(s) on output stream out
682 
683 void TGraphErrors::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
684 {
685  char quote = '"';
686  out << " " << std::endl;
687  static Int_t frameNumber = 1000;
688  frameNumber++;
689 
690  Int_t i;
691  TString fXName = TString(GetName()) + Form("_fx%d",frameNumber);
692  TString fYName = TString(GetName()) + Form("_fy%d",frameNumber);
693  TString fEXName = TString(GetName()) + Form("_fex%d",frameNumber);
694  TString fEYName = TString(GetName()) + Form("_fey%d",frameNumber);
695  out << " Double_t " << fXName << "[" << fNpoints << "] = {" << std::endl;
696  for (i = 0; i < fNpoints-1; i++) out << " " << fX[i] << "," << std::endl;
697  out << " " << fX[fNpoints-1] << "};" << std::endl;
698  out << " Double_t " << fYName << "[" << fNpoints << "] = {" << std::endl;
699  for (i = 0; i < fNpoints-1; i++) out << " " << fY[i] << "," << std::endl;
700  out << " " << fY[fNpoints-1] << "};" << std::endl;
701  out << " Double_t " << fEXName << "[" << fNpoints << "] = {" << std::endl;
702  for (i = 0; i < fNpoints-1; i++) out << " " << fEX[i] << "," << std::endl;
703  out << " " << fEX[fNpoints-1] << "};" << std::endl;
704  out << " Double_t " << fEYName << "[" << fNpoints << "] = {" << std::endl;
705  for (i = 0; i < fNpoints-1; i++) out << " " << fEY[i] << "," << std::endl;
706  out << " " << fEY[fNpoints-1] << "};" << std::endl;
707 
708  if (gROOT->ClassSaved(TGraph::Class())) out << " ";
709  else out << " TGraphErrors *";
710  out << "gre = new TGraphErrors(" << fNpoints << ","
711  << fXName << "," << fYName << ","
712  << fEXName << "," << fEYName << ");"
713  << std::endl;
714 
715  out << " gre->SetName(" << quote << GetName() << quote << ");" << std::endl;
716  out << " gre->SetTitle(" << quote << GetTitle() << quote << ");" << std::endl;
717 
718  SaveFillAttributes(out, "gre", 0, 1001);
719  SaveLineAttributes(out, "gre", 1, 1, 1);
720  SaveMarkerAttributes(out, "gre", 1, 1, 1);
721 
722  if (fHistogram) {
723  TString hname = fHistogram->GetName();
724  hname += frameNumber;
725  fHistogram->SetName(Form("Graph_%s", hname.Data()));
726  fHistogram->SavePrimitive(out, "nodraw");
727  out << " gre->SetHistogram(" << fHistogram->GetName() << ");" << std::endl;
728  out << " " << std::endl;
729  }
730 
731  // save list of functions
732  TIter next(fFunctions);
733  TObject *obj;
734  while ((obj = next())) {
735  obj->SavePrimitive(out, Form("nodraw #%d\n",++frameNumber));
736  if (obj->InheritsFrom("TPaveStats")) {
737  out << " gre->GetListOfFunctions()->Add(ptstats);" << std::endl;
738  out << " ptstats->SetParent(gre->GetListOfFunctions());" << std::endl;
739  } else {
740  out << " gre->GetListOfFunctions()->Add("
741  << Form("%s%d",obj->GetName(),frameNumber) << ");" << std::endl;
742  }
743  }
744 
745  const char *l = strstr(option, "multigraph");
746  if (l) {
747  out << " multigraph->Add(gre," << quote << l + 10 << quote << ");" << std::endl;
748  } else {
749  out << " gre->Draw(" << quote << option << quote << ");" << std::endl;
750  }
751 }
752 
753 
754 ////////////////////////////////////////////////////////////////////////////////
755 /// Set ex and ey values for point pointed by the mouse.
756 
758 {
759  Int_t px = gPad->GetEventX();
760  Int_t py = gPad->GetEventY();
761 
762  //localize point to be deleted
763  Int_t ipoint = -2;
764  Int_t i;
765  // start with a small window (in case the mouse is very close to one point)
766  for (i = 0; i < fNpoints; i++) {
767  Int_t dpx = px - gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
768  Int_t dpy = py - gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
769  if (dpx * dpx + dpy * dpy < 25) {
770  ipoint = i;
771  break;
772  }
773  }
774  if (ipoint == -2) return;
775 
776  fEX[ipoint] = ex;
777  fEY[ipoint] = ey;
778  gPad->Modified();
779 }
780 
781 
782 ////////////////////////////////////////////////////////////////////////////////
783 /// Set ex and ey values for point number i.
784 
786 {
787  if (i < 0) return;
788  if (i >= fNpoints) {
789  // re-allocate the object
790  TGraphErrors::SetPoint(i, 0, 0);
791  }
792  fEX[i] = ex;
793  fEY[i] = ey;
794 }
795 
796 
797 ////////////////////////////////////////////////////////////////////////////////
798 /// Stream an object of class TGraphErrors.
799 
800 void TGraphErrors::Streamer(TBuffer &b)
801 {
802  if (b.IsReading()) {
803  UInt_t R__s, R__c;
804  Version_t R__v = b.ReadVersion(&R__s, &R__c);
805  if (R__v > 2) {
806  b.ReadClassBuffer(TGraphErrors::Class(), this, R__v, R__s, R__c);
807  return;
808  }
809  //====process old versions before automatic schema evolution
810  TGraph::Streamer(b);
811  fEX = new Double_t[fNpoints];
812  fEY = new Double_t[fNpoints];
813  if (R__v < 2) {
814  Float_t *ex = new Float_t[fNpoints];
815  Float_t *ey = new Float_t[fNpoints];
816  b.ReadFastArray(ex, fNpoints);
817  b.ReadFastArray(ey, fNpoints);
818  for (Int_t i = 0; i < fNpoints; i++) {
819  fEX[i] = ex[i];
820  fEY[i] = ey[i];
821  }
822  delete [] ey;
823  delete [] ex;
824  } else {
827  }
828  b.CheckByteCount(R__s, R__c, TGraphErrors::IsA());
829  //====end of old versions
830 
831  } else {
833  }
834 }
835 
836 
837 ////////////////////////////////////////////////////////////////////////////////
838 /// Swap points
839 
841 {
842  SwapValues(fEX, pos1, pos2);
843  SwapValues(fEY, pos1, pos2);
844  TGraph::SwapPoints(pos1, pos2);
845 }
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TH1.cxx:6471
Int_t fNpoints
Current dimension of arrays fX and fY.
Definition: TGraph.h:58
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
Bool_t IsReading() const
Definition: TBuffer.h:83
float xmin
Definition: THbookFile.cxx:93
Double_t * fX
Definition: TGraph.h:59
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual Double_t * GetEX() const
Definition: TGraph.h:142
Double_t GetErrorYlow(Int_t bin) const
This function is called by GraphFitChisquare.
short Version_t
Definition: RtypesCore.h:61
TLine * line
float Float_t
Definition: RtypesCore.h:53
virtual Bool_t DoMerge(const TGraph *g)
protected function to perform the merge operation of a graph with errors
const char Option_t
Definition: RtypesCore.h:62
virtual ~TGraphErrors()
TGraphErrors default destructor.
float ymin
Definition: THbookFile.cxx:93
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
R__EXTERN TStyle * gStyle
Definition: TStyle.h:418
Double_t GetErrorYhigh(Int_t bin) const
This function is called by GraphFitChisquare.
Int_t GetLwb() const
Definition: TVectorT.h:79
TH1 * h
Definition: legend2.C:5
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition: TString.cxx:1835
TVectorT.
Definition: TMatrixTBase.h:89
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual Double_t * GetEY() const
Definition: TGraph.h:143
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TList * fFunctions
Definition: TGraph.h:61
const Bool_t kFALSE
Definition: Rtypes.h:92
TH1F * fHistogram
Definition: TGraph.h:62
static std::string format(double x, double y, int digits, int width)
const char * Class
Definition: TXMLSetup.cxx:64
TGraphErrors()
TGraphErrors default constructor.
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
virtual Bool_t CopyPoints(Double_t **arrays, Int_t ibegin, Int_t iend, Int_t obegin)
Copy errors from fEX and fEY to arrays[0] and arrays[1] or to fX and fY.
virtual Int_t Merge(TCollection *list)
Adds all graphs with errors from the collection to this graph.
virtual Bool_t CopyPoints(Double_t **newarrays, Int_t ibegin, Int_t iend, Int_t obegin)
Copy points from fX and fY to arrays[0] and arrays[1] or to fX and fY if arrays == 0 and ibegin != ie...
Definition: TGraph.cxx:695
Double_t GetErrorXlow(Int_t bin) const
This function is called by GraphFitChisquare.
Double_t x[n]
Definition: legend1.C:17
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:2335
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition: TGraph.cxx:187
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:260
virtual void SwapPoints(Int_t pos1, Int_t pos2)
Swap points.
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
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.
Definition: TAttMarker.cxx:229
Double_t GetErrorX(Int_t bin) const
This function is called by GraphFitChisquare.
Float_t GetErrorX() const
Definition: TStyle.h:188
float ymax
Definition: THbookFile.cxx:93
virtual Int_t GetPoint(Int_t i, Double_t &x, Double_t &y) const
Get x and y values for point number i.
Definition: TGraph.cxx:1574
virtual void SwapPoints(Int_t pos1, Int_t pos2)
Swap points.
Definition: TGraph.cxx:2323
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
TGraphErrors & operator=(const TGraphErrors &gr)
TGraphErrors assignment operator.
virtual void ComputeRange(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax) const
Compute range.
static void SwapValues(Double_t *arr, Int_t pos1, Int_t pos2)
Swap values.
Definition: TGraph.cxx:2332
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:488
Collection abstract base class.
Definition: TCollection.h:48
virtual Bool_t DoMerge(const TGraph *g)
protected function to perform the merge operation of a graph
Definition: TGraph.cxx:2396
unsigned int UInt_t
Definition: RtypesCore.h:42
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:232
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:925
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:390
Int_t GetN() const
Definition: TGraph.h:133
TLine * l
Definition: textangle.C:4
float xmax
Definition: THbookFile.cxx:93
virtual void Apply(TF1 *f)
apply function to all the data points y = f(x,y)
virtual void CopyAndRelease(Double_t **newarrays, Int_t ibegin, Int_t iend, Int_t obegin)
Copy and release.
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
TGraphErrors * gr
Definition: legend1.C:25
virtual void FillZero(Int_t begin, Int_t end, Bool_t from_ctor=kTRUE)
Set zero values for point arrays in the range [begin, end].
Double_t * GetX() const
Definition: TGraph.h:140
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8071
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
Double_t * fEX
Definition: TGraphErrors.h:31
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition: TF1.cxx:1196
Double_t GetErrorY(Int_t bin) const
This function is called by GraphFitChisquare.
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition: TH1.cxx:8273
#define ClassImp(name)
Definition: Rtypes.h:279
double f(double x)
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
Double_t ey[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:80
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void FillZero(Int_t begin, Int_t end, Bool_t from_ctor=kTRUE)
Set zero values for point arrays in the range [begin, end) Should be redefined in descendant classes...
Definition: TGraph.cxx:1011
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2150
Double_t * fY
Definition: TGraph.h:60
Double_t * GetY() const
Definition: TGraph.h:141
Bool_t CtorAllocate()
Constructor allocate.
1-Dim function class
Definition: TF1.h:149
void MakeZombie()
Definition: TObject.h:47
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:28
#define NULL
Definition: Rtypes.h:82
#define gPad
Definition: TVirtualPad.h:289
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1965
static Int_t CalculateScanfFields(const char *fmt)
Calculate scan fields.
Double_t Atof() const
Return floating-point value contained in string.
Definition: TString.cxx:2031
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1807
Double_t * fEY
Definition: TGraphErrors.h:32
virtual void Print(Option_t *chopt="") const
Print graph and errors values.
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1244
Int_t fMaxSize
Definition: TGraph.h:57
virtual void ComputeRange(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax) const
Compute the x/y range of the points in this graph.
Definition: TGraph.cxx:647
virtual void SetPointError(Double_t ex, Double_t ey)
Set ex and ey values for point pointed by the mouse.
Double_t GetErrorXhigh(Int_t bin) const
This function is called by GraphFitChisquare.
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:416
virtual void Set(Int_t n)
Set number of points in the graph Existing coordinates are preserved New coordinates above fNpoints a...
Definition: TGraph.cxx:2099
const Bool_t kTRUE
Definition: Rtypes.h:91
return
Definition: HLFactory.cxx:514
static char * skip(char **buf, const char *delimiters)
Definition: civetweb.c:2039
Double_t ex[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:911
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition: TH1.cxx:8173
const char * Data() const
Definition: TString.h:349
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out".
Definition: TObject.cxx:709