ROOT  6.06/09
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 
71 TGraphErrors::TGraphErrors(): TGraph()
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 
95 TGraphErrors::TGraphErrors(Int_t n, const Float_t *x, const Float_t *y, const Float_t *ex, const Float_t *ey)
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 
114 TGraphErrors::TGraphErrors(Int_t n, const Double_t *x, const Double_t *y, const Double_t *ex, const Double_t *ey)
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 3 columns of numbers
219 /// convention for format (default="%lg %lg %lg %lg)
220 /// format = "%lg %lg" read only 2 first columns into X,Y
221 /// format = "%lg %lg %lg" read only 3 first columns into X,Y and EY
222 /// format = "%lg %lg %lg %lg" read only 4 first columns into X,Y,EX,EY.
223 ///
224 /// For files separated by a specific delimiter different from ' ' and '\t' (e.g. ';' in csv files)
225 /// you can avoid using %*s to bypass this delimiter by explicitly specify the "option" argument,
226 /// e.g. option=" \t,;" for columns of figures separated by any of these characters (' ', '\t', ',', ';')
227 /// used once (e.g. "1;1") or in a combined way (" 1;,;; 1").
228 /// Note in that case, the instanciation is about 2 times slower.
229 /// In case a delimiter is specified, the format "%lg %lg %lg" will read X,Y,EX.
230 
231 TGraphErrors::TGraphErrors(const char *filename, const char *format, Option_t *option)
232  : TGraph(100)
233 {
234  if (!CtorAllocate()) return;
235  Double_t x, y, ex, ey;
236  TString fname = filename;
237  gSystem->ExpandPathName(fname);
238  std::ifstream infile(fname.Data());
239  if (!infile.good()) {
240  MakeZombie();
241  Error("TGraphErrors", "Cannot open file: %s, TGraphErrors is Zombie", filename);
242  fNpoints = 0;
243  return;
244  }
245  std::string line;
246  Int_t np = 0;
247 
248  if (strcmp(option, "") == 0) { // No delimiters specified (standard constructor).
249 
250  Int_t ncol = CalculateScanfFields(format); //count number of columns in format
251  Int_t res;
252  while (std::getline(infile, line, '\n')) {
253  ex = ey = 0;
254  if (ncol < 3) {
255  res = sscanf(line.c_str(), format, &x, &y);
256  } else if (ncol < 4) {
257  res = sscanf(line.c_str(), format, &x, &y, &ey);
258  } else {
259  res = sscanf(line.c_str(), format, &x, &y, &ex, &ey);
260  }
261  if (res < 2) {
262  continue; //skip empty and ill-formed lines
263  }
264  SetPoint(np, x, y);
265  SetPointError(np, ex, ey);
266  np++;
267  }
268  Set(np);
269 
270  } else { // A delimiter has been specified in "option"
271 
272  // Checking format and creating its boolean equivalent
273  TString format_ = TString(format) ;
274  format_.ReplaceAll(" ", "") ;
275  format_.ReplaceAll("\t", "") ;
276  format_.ReplaceAll("lg", "") ;
277  format_.ReplaceAll("s", "") ;
278  format_.ReplaceAll("%*", "0") ;
279  format_.ReplaceAll("%", "1") ;
280  if (!format_.IsDigit()) {
281  Error("TGraphErrors", "Incorrect input format! Allowed format tags are {\"%%lg\",\"%%*lg\" or \"%%*s\"}");
282  return ;
283  }
284  Int_t ntokens = format_.Length() ;
285  if (ntokens < 2) {
286  Error("TGraphErrors", "Incorrect input format! Only %d tag(s) in format whereas at least 2 \"%%lg\" tags are expected!", ntokens);
287  return ;
288  }
289  Int_t ntokensToBeSaved = 0 ;
290  Bool_t * isTokenToBeSaved = new Bool_t [ntokens] ;
291  for (Int_t idx = 0; idx < ntokens; idx++) {
292  isTokenToBeSaved[idx] = TString::Format("%c", format_[idx]).Atoi() ; //atoi(&format_[idx]) does not work for some reason...
293  if (isTokenToBeSaved[idx] == 1) {
294  ntokensToBeSaved++ ;
295  }
296  }
297  if (ntokens >= 2 && (ntokensToBeSaved < 2 || ntokensToBeSaved > 4)) { //first condition not to repeat the previous error message
298  Error("TGraphErrors", "Incorrect input format! There are %d \"%%lg\" tag(s) in format whereas 2,3 or 4 are expected!", ntokensToBeSaved);
299  delete [] isTokenToBeSaved ;
300  return ;
301  }
302 
303  // Initializing loop variables
304  Bool_t isLineToBeSkipped = kFALSE ; //empty and ill-formed lines
305  char * token = NULL ;
306  TString token_str = "" ;
307  Int_t token_idx = 0 ;
308  Double_t * value = new Double_t [4] ; //x,y,ex,ey buffers
309  for (Int_t k = 0; k < 4; k++) {
310  value[k] = 0. ;
311  }
312  Int_t value_idx = 0 ;
313 
314  // Looping
315  while (std::getline(infile, line, '\n')) {
316  if (line != "") {
317  if (line[line.size() - 1] == char(13)) { // removing DOS CR character
318  line.erase(line.end() - 1, line.end()) ;
319  }
320  token = strtok(const_cast<char*>(line.c_str()), option) ;
321  while (token != NULL && value_idx < ntokensToBeSaved) {
322  if (isTokenToBeSaved[token_idx]) {
323  token_str = TString(token) ;
324  token_str.ReplaceAll("\t", "") ;
325  if (!token_str.IsFloat()) {
326  isLineToBeSkipped = kTRUE ;
327  break ;
328  } else {
329  value[value_idx] = token_str.Atof() ;
330  value_idx++ ;
331  }
332  }
333  token = strtok(NULL, option) ; //next token
334  token_idx++ ;
335  }
336  if (!isLineToBeSkipped && value_idx > 1) { //i.e. 2,3 or 4
337  x = value[0] ;
338  y = value[1] ;
339  ex = value[2] ;
340  ey = value[3] ;
341  SetPoint(np, x, y) ;
342  SetPointError(np, ex, ey);
343  np++ ;
344  }
345  }
346  isLineToBeSkipped = kFALSE ;
347  token = NULL ;
348  token_idx = 0 ;
349  value_idx = 0 ;
350  }
351  Set(np) ;
352 
353  // Cleaning
354  delete [] isTokenToBeSaved ;
355  delete [] value ;
356  delete token ;
357  }
358  infile.close();
359 }
360 
361 
362 ////////////////////////////////////////////////////////////////////////////////
363 /// TGraphErrors default destructor.
364 
366 {
367  delete [] fEX;
368  delete [] fEY;
369 }
370 
371 
372 ////////////////////////////////////////////////////////////////////////////////
373 /// apply function to all the data points
374 /// y = f(x,y)
375 ///
376 /// The error is calculated as ey=(f(x,y+ey)-f(x,y-ey))/2
377 /// This is the same as error(fy) = df/dy * ey for small errors
378 ///
379 /// For generic functions the symmetric errors might become non-symmetric
380 /// and are averaged here. Use TGraphAsymmErrors if desired.
381 ///
382 /// error on x doesn't change
383 /// function suggested/implemented by Miroslav Helbich <helbich@mail.desy.de>
384 
386 {
387  Double_t x, y, ex, ey;
388 
389  if (fHistogram) {
390  delete fHistogram;
391  fHistogram = 0;
392  }
393  for (Int_t i = 0; i < GetN(); i++) {
394  GetPoint(i, x, y);
395  ex = GetErrorX(i);
396  ey = GetErrorY(i);
397 
398  SetPoint(i, x, f->Eval(x, y));
399  SetPointError(i, ex, TMath::Abs(f->Eval(x, y + ey) - f->Eval(x, y - ey)) / 2.);
400  }
401  if (gPad) gPad->Modified();
402 }
403 
404 
405 ////////////////////////////////////////////////////////////////////////////////
406 /// Calculate scan fields.
407 
409 {
410  Int_t fields = 0;
411  while ((fmt = strchr(fmt, '%'))) {
412  Bool_t skip = kFALSE;
413  while (*(++fmt)) {
414  if ('[' == *fmt) {
415  if (*++fmt && '^' == *fmt) ++fmt; // "%[^]a]"
416  if (*++fmt && ']' == *fmt) ++fmt; // "%[]a]" or "%[^]a]"
417  while (*fmt && *fmt != ']')
418  ++fmt;
419  if (!skip) ++fields;
420  break;
421  }
422  if ('%' == *fmt) break; // %% literal %
423  if ('*' == *fmt) {
424  skip = kTRUE; // %*d -- skip a number
425  } else if (strchr("dDiouxXxfegEscpn", *fmt)) {
426  if (!skip) ++fields;
427  break;
428  }
429  // skip modifiers & field width
430  }
431  }
432  return fields;
433 }
434 
435 
436 ////////////////////////////////////////////////////////////////////////////////
437 /// Compute range.
438 
440 {
441  TGraph::ComputeRange(xmin, ymin, xmax, ymax);
442 
443  for (Int_t i = 0; i < fNpoints; i++) {
444  if (fX[i] - fEX[i] < xmin) {
445  if (gPad && gPad->GetLogx()) {
446  if (fEX[i] < fX[i]) xmin = fX[i] - fEX[i];
447  else xmin = TMath::Min(xmin, fX[i] / 3);
448  } else {
449  xmin = fX[i] - fEX[i];
450  }
451  }
452  if (fX[i] + fEX[i] > xmax) xmax = fX[i] + fEX[i];
453  if (fY[i] - fEY[i] < ymin) {
454  if (gPad && gPad->GetLogy()) {
455  if (fEY[i] < fY[i]) ymin = fY[i] - fEY[i];
456  else ymin = TMath::Min(ymin, fY[i] / 3);
457  } else {
458  ymin = fY[i] - fEY[i];
459  }
460  }
461  if (fY[i] + fEY[i] > ymax) ymax = fY[i] + fEY[i];
462  }
463 }
464 
465 
466 ////////////////////////////////////////////////////////////////////////////////
467 /// Copy and release.
468 
470  Int_t ibegin, Int_t iend, Int_t obegin)
471 {
472  CopyPoints(newarrays, ibegin, iend, obegin);
473  if (newarrays) {
474  delete[] fX;
475  fX = newarrays[2];
476  delete[] fY;
477  fY = newarrays[3];
478  delete[] fEX;
479  fEX = newarrays[0];
480  delete[] fEY;
481  fEY = newarrays[1];
482  delete[] newarrays;
483  }
484 }
485 
486 
487 ////////////////////////////////////////////////////////////////////////////////
488 /// Copy errors from fEX and fEY to arrays[0] and arrays[1]
489 /// or to fX and fY. Copy points.
490 
492  Int_t obegin)
493 {
494  if (TGraph::CopyPoints(arrays ? arrays + 2 : 0, ibegin, iend, obegin)) {
495  Int_t n = (iend - ibegin) * sizeof(Double_t);
496  if (arrays) {
497  memmove(&arrays[0][obegin], &fEX[ibegin], n);
498  memmove(&arrays[1][obegin], &fEY[ibegin], n);
499  } else {
500  memmove(&fEX[obegin], &fEX[ibegin], n);
501  memmove(&fEY[obegin], &fEY[ibegin], n);
502  }
503  return kTRUE;
504  } else {
505  return kFALSE;
506  }
507 }
508 
509 
510 ////////////////////////////////////////////////////////////////////////////////
511 /// Constructor allocate.
512 ///Note: This function should be called only from the constructor
513 /// since it does not delete previously existing arrays
514 
516 {
517 
518  if (!fNpoints) {
519  fEX = fEY = 0;
520  return kFALSE;
521  } else {
522  fEX = new Double_t[fMaxSize];
523  fEY = new Double_t[fMaxSize];
524  }
525  return kTRUE;
526 }
527 
528 ////////////////////////////////////////////////////////////////////////////////
529 /// protected function to perform the merge operation of a graph with errors
530 
532 {
533  if (g->GetN() == 0) return kFALSE;
534 
535  Double_t * ex = g->GetEX();
536  Double_t * ey = g->GetEY();
537  if (ex == 0 || ey == 0 ) {
538  if (g->IsA() != TGraph::Class() )
539  Warning("DoMerge","Merging a %s is not compatible with a TGraphErrors - errors will be ignored",g->IsA()->GetName());
540  return TGraph::DoMerge(g);
541  }
542  for (Int_t i = 0 ; i < g->GetN(); i++) {
543  Int_t ipoint = GetN();
544  Double_t x = g->GetX()[i];
545  Double_t y = g->GetY()[i];
546  SetPoint(ipoint, x, y);
547  SetPointError( ipoint, ex[i], ey[i] );
548  }
549  return kTRUE;
550 }
551 
552 
553 ////////////////////////////////////////////////////////////////////////////////
554 /// Set zero values for point arrays in the range [begin, end]
555 
556 void TGraphErrors::FillZero(Int_t begin, Int_t end, Bool_t from_ctor)
557 {
558  if (!from_ctor) {
559  TGraph::FillZero(begin, end, from_ctor);
560  }
561  Int_t n = (end - begin) * sizeof(Double_t);
562  memset(fEX + begin, 0, n);
563  memset(fEY + begin, 0, n);
564 }
565 
566 
567 ////////////////////////////////////////////////////////////////////////////////
568 /// This function is called by GraphFitChisquare.
569 /// It returns the error along X at point i.
570 
572 {
573  if (i < 0 || i >= fNpoints) return -1;
574  if (fEX) return fEX[i];
575  return -1;
576 }
577 
578 
579 ////////////////////////////////////////////////////////////////////////////////
580 /// This function is called by GraphFitChisquare.
581 /// It returns the error along Y at point i.
582 
584 {
585  if (i < 0 || i >= fNpoints) return -1;
586  if (fEY) return fEY[i];
587  return -1;
588 }
589 
590 
591 ////////////////////////////////////////////////////////////////////////////////
592 /// This function is called by GraphFitChisquare.
593 /// It returns the error along X at point i.
594 
596 {
597  if (i < 0 || i >= fNpoints) return -1;
598  if (fEX) return fEX[i];
599  return -1;
600 }
601 
602 
603 ////////////////////////////////////////////////////////////////////////////////
604 /// This function is called by GraphFitChisquare.
605 /// It returns the error along X at point i.
606 
608 {
609  if (i < 0 || i >= fNpoints) return -1;
610  if (fEX) return fEX[i];
611  return -1;
612 }
613 
614 
615 ////////////////////////////////////////////////////////////////////////////////
616 /// This function is called by GraphFitChisquare.
617 /// It returns the error along X at point i.
618 
620 {
621  if (i < 0 || i >= fNpoints) return -1;
622  if (fEY) return fEY[i];
623  return -1;
624 }
625 
626 
627 ////////////////////////////////////////////////////////////////////////////////
628 /// This function is called by GraphFitChisquare.
629 /// It returns the error along X at point i.
630 
632 {
633  if (i < 0 || i >= fNpoints) return -1;
634  if (fEY) return fEY[i];
635  return -1;
636 }
637 
638 ////////////////////////////////////////////////////////////////////////////////
639 /// Adds all graphs with errors from the collection to this graph.
640 /// Returns the total number of poins in the result or -1 in case of an error.
641 
643 {
644  TIter next(li);
645  while (TObject* o = next()) {
646  TGraph *g = dynamic_cast<TGraph*>(o);
647  if (!g) {
648  Error("Merge",
649  "Cannot merge - an object which doesn't inherit from TGraph found in the list");
650  return -1;
651  }
652  int n0 = GetN();
653  int n1 = n0+g->GetN();
654  Set(n1);
655  Double_t * x = g->GetX();
656  Double_t * y = g->GetY();
657  Double_t * ex = g->GetEX();
658  Double_t * ey = g->GetEY();
659  for (Int_t i = 0 ; i < g->GetN(); i++) {
660  SetPoint(n0+i, x[i], y[i]);
661  if (ex) fEX[n0+i] = ex[i];
662  if (ey) fEY[n0+i] = ey[i];
663  }
664  }
665  return GetN();
666 }
667 
668 ////////////////////////////////////////////////////////////////////////////////
669 /// Print graph and errors values.
670 
672 {
673  for (Int_t i = 0; i < fNpoints; i++) {
674  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]);
675  }
676 }
677 
678 
679 ////////////////////////////////////////////////////////////////////////////////
680 /// Save primitive as a C++ statement(s) on output stream out
681 
682 void TGraphErrors::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
683 {
684  char quote = '"';
685  out << " " << std::endl;
686  static Int_t frameNumber = 1000;
687  frameNumber++;
688 
689  Int_t i;
690  TString fXName = TString(GetName()) + Form("_fx%d",frameNumber);
691  TString fYName = TString(GetName()) + Form("_fy%d",frameNumber);
692  TString fEXName = TString(GetName()) + Form("_fex%d",frameNumber);
693  TString fEYName = TString(GetName()) + Form("_fey%d",frameNumber);
694  out << " Double_t " << fXName << "[" << fNpoints << "] = {" << std::endl;
695  for (i = 0; i < fNpoints-1; i++) out << " " << fX[i] << "," << std::endl;
696  out << " " << fX[fNpoints-1] << "};" << std::endl;
697  out << " Double_t " << fYName << "[" << fNpoints << "] = {" << std::endl;
698  for (i = 0; i < fNpoints-1; i++) out << " " << fY[i] << "," << std::endl;
699  out << " " << fY[fNpoints-1] << "};" << std::endl;
700  out << " Double_t " << fEXName << "[" << fNpoints << "] = {" << std::endl;
701  for (i = 0; i < fNpoints-1; i++) out << " " << fEX[i] << "," << std::endl;
702  out << " " << fEX[fNpoints-1] << "};" << std::endl;
703  out << " Double_t " << fEYName << "[" << fNpoints << "] = {" << std::endl;
704  for (i = 0; i < fNpoints-1; i++) out << " " << fEY[i] << "," << std::endl;
705  out << " " << fEY[fNpoints-1] << "};" << std::endl;
706 
707  if (gROOT->ClassSaved(TGraph::Class())) out << " ";
708  else out << " TGraphErrors *";
709  out << "gre = new TGraphErrors(" << fNpoints << ","
710  << fXName << "," << fYName << ","
711  << fEXName << "," << fEYName << ");"
712  << std::endl;
713 
714  out << " gre->SetName(" << quote << GetName() << quote << ");" << std::endl;
715  out << " gre->SetTitle(" << quote << GetTitle() << quote << ");" << std::endl;
716 
717  SaveFillAttributes(out, "gre", 0, 1001);
718  SaveLineAttributes(out, "gre", 1, 1, 1);
719  SaveMarkerAttributes(out, "gre", 1, 1, 1);
720 
721  if (fHistogram) {
722  TString hname = fHistogram->GetName();
723  hname += frameNumber;
724  fHistogram->SetName(Form("Graph_%s", hname.Data()));
725  fHistogram->SavePrimitive(out, "nodraw");
726  out << " gre->SetHistogram(" << fHistogram->GetName() << ");" << std::endl;
727  out << " " << std::endl;
728  }
729 
730  // save list of functions
732  TObject *obj;
733  while ((obj = next())) {
734  obj->SavePrimitive(out, Form("nodraw #%d\n",++frameNumber));
735  if (obj->InheritsFrom("TPaveStats")) {
736  out << " gre->GetListOfFunctions()->Add(ptstats);" << std::endl;
737  out << " ptstats->SetParent(gre->GetListOfFunctions());" << std::endl;
738  } else {
739  out << " gre->GetListOfFunctions()->Add("
740  << Form("%s%d",obj->GetName(),frameNumber) << ");" << std::endl;
741  }
742  }
743 
744  const char *l = strstr(option, "multigraph");
745  if (l) {
746  out << " multigraph->Add(gre," << quote << l + 10 << quote << ");" << std::endl;
747  } else {
748  out << " gre->Draw(" << quote << option << quote << ");" << std::endl;
749  }
750 }
751 
752 
753 ////////////////////////////////////////////////////////////////////////////////
754 /// Set ex and ey values for point pointed by the mouse.
755 
757 {
758  Int_t px = gPad->GetEventX();
759  Int_t py = gPad->GetEventY();
760 
761  //localize point to be deleted
762  Int_t ipoint = -2;
763  Int_t i;
764  // start with a small window (in case the mouse is very close to one point)
765  for (i = 0; i < fNpoints; i++) {
766  Int_t dpx = px - gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
767  Int_t dpy = py - gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
768  if (dpx * dpx + dpy * dpy < 25) {
769  ipoint = i;
770  break;
771  }
772  }
773  if (ipoint == -2) return;
774 
775  fEX[ipoint] = ex;
776  fEY[ipoint] = ey;
777  gPad->Modified();
778 }
779 
780 
781 ////////////////////////////////////////////////////////////////////////////////
782 /// Set ex and ey values for point number i.
783 
785 {
786  if (i < 0) return;
787  if (i >= fNpoints) {
788  // re-allocate the object
789  TGraphErrors::SetPoint(i, 0, 0);
790  }
791  fEX[i] = ex;
792  fEY[i] = ey;
793 }
794 
795 
796 ////////////////////////////////////////////////////////////////////////////////
797 /// Stream an object of class TGraphErrors.
798 
799 void TGraphErrors::Streamer(TBuffer &b)
800 {
801  if (b.IsReading()) {
802  UInt_t R__s, R__c;
803  Version_t R__v = b.ReadVersion(&R__s, &R__c);
804  if (R__v > 2) {
805  b.ReadClassBuffer(TGraphErrors::Class(), this, R__v, R__s, R__c);
806  return;
807  }
808  //====process old versions before automatic schema evolution
809  TGraph::Streamer(b);
810  fEX = new Double_t[fNpoints];
811  fEY = new Double_t[fNpoints];
812  if (R__v < 2) {
813  Float_t *ex = new Float_t[fNpoints];
814  Float_t *ey = new Float_t[fNpoints];
815  b.ReadFastArray(ex, fNpoints);
816  b.ReadFastArray(ey, fNpoints);
817  for (Int_t i = 0; i < fNpoints; i++) {
818  fEX[i] = ex[i];
819  fEY[i] = ey[i];
820  }
821  delete [] ey;
822  delete [] ex;
823  } else {
826  }
827  b.CheckByteCount(R__s, R__c, TGraphErrors::IsA());
828  //====end of old versions
829 
830  } else {
832  }
833 }
834 
835 
836 ////////////////////////////////////////////////////////////////////////////////
837 /// Swap points
838 
840 {
841  SwapValues(fEX, pos1, pos2);
842  SwapValues(fEY, pos1, pos2);
843  TGraph::SwapPoints(pos1, pos2);
844 }
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TH1.cxx:6723
Int_t fNpoints
Current dimension of arrays fX and fY.
Definition: TGraph.h:58
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
float xmin
Definition: THbookFile.cxx:93
Double_t * fX
Definition: TGraph.h:59
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void Print(Option_t *chopt="") const
Print graph and errors values.
Bool_t IsReading() const
Definition: TBuffer.h:81
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:487
short Version_t
Definition: RtypesCore.h:61
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
Ssiz_t Length() const
Definition: TString.h:390
TLine * line
float Float_t
Definition: RtypesCore.h:53
Double_t GetErrorX(Int_t bin) const
This function is called by GraphFitChisquare.
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
Double_t GetErrorXlow(Int_t bin) const
This function is called by GraphFitChisquare.
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:423
TH1 * h
Definition: legend2.C:5
Double_t Atof() const
Return floating-point value contained in string.
Definition: TString.cxx:2030
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Double_t GetErrorYhigh(Int_t bin) const
This function is called by GraphFitChisquare.
static const char * filename()
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
#define gROOT
Definition: TROOT.h:340
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)
Int_t GetN() const
Definition: TGraph.h:132
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 Double_t * GetEY() const
Definition: TGraph.h:142
virtual Double_t GetBinWidth(Int_t bin) const
return bin width for 1D historam Better to use h1.GetXaxis().GetBinWidth(bin)
Definition: TH1.cxx:8492
const char * Data() const
Definition: TString.h:349
Double_t * GetY() const
Definition: TGraph.h:140
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:688
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:2334
void Class()
Definition: Class.C:29
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition: TGraph.cxx:180
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:257
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 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:640
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1964
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:226
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
char * out
Definition: TBase64.cxx:29
float ymax
Definition: THbookFile.cxx:93
Double_t * GetX() const
Definition: TGraph.h:139
virtual void SwapPoints(Int_t pos1, Int_t pos2)
Swap points.
Definition: TGraph.cxx:2295
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
return
Definition: TBase64.cxx:62
TGraphErrors & operator=(const TGraphErrors &gr)
TGraphErrors assignment operator.
static void SwapValues(Double_t *arr, Int_t pos1, Int_t pos2)
Swap values.
Definition: TGraph.cxx:2304
Collection abstract base class.
Definition: TCollection.h:48
TClass * IsA() const
virtual Bool_t DoMerge(const TGraph *g)
protected function to perform the merge operation of a graph
Definition: TGraph.cxx:2368
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:229
char * Form(const char *fmt,...)
TLine * l
Definition: textangle.C:4
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
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
Float_t GetErrorX() const
Definition: TStyle.h:196
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].
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8288
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
Double_t * fEX
Definition: TGraphErrors.h:31
#define ClassImp(name)
Definition: Rtypes.h:279
double f(double x)
Int_t GetLwb() const
Definition: TVectorT.h:79
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
double Double_t
Definition: RtypesCore.h:55
Double_t GetErrorY(Int_t bin) const
This function is called by GraphFitChisquare.
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition: TString.cxx:1834
Double_t y[n]
Definition: legend1.C:17
Double_t ey[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:80
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:1551
Double_t GetErrorYlow(Int_t bin) const
This function is called by GraphFitChisquare.
Mother of all ROOT objects.
Definition: TObject.h:58
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1806
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:988
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2127
Double_t * fY
Definition: TGraph.h:60
Bool_t CtorAllocate()
Constructor allocate.
1-Dim function class
Definition: TF1.h:149
void MakeZombie()
Definition: TObject.h:68
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:1185
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:28
#define NULL
Definition: Rtypes.h:82
#define gPad
Definition: TVirtualPad.h:288
static Int_t CalculateScanfFields(const char *fmt)
Calculate scan fields.
Double_t * fEY
Definition: TGraphErrors.h:32
virtual Double_t * GetEX() const
Definition: TGraph.h:141
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1243
Int_t fMaxSize
Definition: TGraph.h:57
virtual void SetPointError(Double_t ex, Double_t ey)
Set ex and ey values for point pointed by the mouse.
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:2076
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition: TH1.cxx:8395
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
float value
Definition: math.cpp:443
Double_t GetErrorXhigh(Int_t bin) const
This function is called by GraphFitChisquare.
static char * skip(char **buf, const char *delimiters)
Definition: civetweb.c:1014
Double_t ex[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out".
Definition: TObject.cxx:702
virtual void ComputeRange(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax) const
Compute range.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904