Logo ROOT   6.16/01
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
38A TGraphErrors is a TGraph with error bars.
39
40The TGraphErrors painting is performed thanks to the TGraphPainter
41class. All details about the various painting options are given in this class.
42
43The picture below gives an example:
44
45Begin_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}
64End_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;
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
134TGraphErrors::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
154TGraphErrors::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) {
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
232TGraphErrors::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 char *rest;
317 while (std::getline(infile, line, '\n')) {
318 if (line != "") {
319 if (line[line.size() - 1] == char(13)) { // removing DOS CR character
320 line.erase(line.end() - 1, line.end()) ;
321 }
322 token = R__STRTOK_R(const_cast<char *>(line.c_str()), option, &rest);
323 while (token != NULL && value_idx < ntokensToBeSaved) {
324 if (isTokenToBeSaved[token_idx]) {
325 token_str = TString(token) ;
326 token_str.ReplaceAll("\t", "") ;
327 if (!token_str.IsFloat()) {
328 isLineToBeSkipped = kTRUE ;
329 break ;
330 } else {
331 value[value_idx] = token_str.Atof() ;
332 value_idx++ ;
333 }
334 }
335 token = R__STRTOK_R(NULL, option, &rest); // next token
336 token_idx++ ;
337 }
338 if (!isLineToBeSkipped && value_idx > 1) { //i.e. 2,3 or 4
339 x = value[0] ;
340 y = value[1] ;
341 ex = value[2] ;
342 ey = value[3] ;
343 SetPoint(np, x, y) ;
344 SetPointError(np, ex, ey);
345 np++ ;
346 }
347 }
348 isLineToBeSkipped = kFALSE ;
349 token = NULL ;
350 token_idx = 0 ;
351 value_idx = 0 ;
352 }
353 Set(np) ;
354
355 // Cleaning
356 delete [] isTokenToBeSaved ;
357 delete [] value ;
358 delete token ;
359 }
360 infile.close();
361}
362
363
364////////////////////////////////////////////////////////////////////////////////
365/// TGraphErrors default destructor.
366
368{
369 delete [] fEX;
370 delete [] fEY;
371}
372
373
374////////////////////////////////////////////////////////////////////////////////
375/// apply function to all the data points
376/// y = f(x,y)
377///
378/// The error is calculated as ey=(f(x,y+ey)-f(x,y-ey))/2
379/// This is the same as error(fy) = df/dy * ey for small errors
380///
381/// For generic functions the symmetric errors might become non-symmetric
382/// and are averaged here. Use TGraphAsymmErrors if desired.
383///
384/// error on x doesn't change
385/// function suggested/implemented by Miroslav Helbich <helbich@mail.desy.de>
386
388{
389 Double_t x, y, ex, ey;
390
391 if (fHistogram) {
392 delete fHistogram;
393 fHistogram = 0;
394 }
395 for (Int_t i = 0; i < GetN(); i++) {
396 GetPoint(i, x, y);
397 ex = GetErrorX(i);
398 ey = GetErrorY(i);
399
400 SetPoint(i, x, f->Eval(x, y));
401 SetPointError(i, ex, TMath::Abs(f->Eval(x, y + ey) - f->Eval(x, y - ey)) / 2.);
402 }
403 if (gPad) gPad->Modified();
404}
405
406
407////////////////////////////////////////////////////////////////////////////////
408/// Calculate scan fields.
409
411{
412 Int_t fields = 0;
413 while ((fmt = strchr(fmt, '%'))) {
414 Bool_t skip = kFALSE;
415 while (*(++fmt)) {
416 if ('[' == *fmt) {
417 if (*++fmt && '^' == *fmt) ++fmt; // "%[^]a]"
418 if (*++fmt && ']' == *fmt) ++fmt; // "%[]a]" or "%[^]a]"
419 while (*fmt && *fmt != ']')
420 ++fmt;
421 if (!skip) ++fields;
422 break;
423 }
424 if ('%' == *fmt) break; // %% literal %
425 if ('*' == *fmt) {
426 skip = kTRUE; // %*d -- skip a number
427 } else if (strchr("dDiouxXxfegEscpn", *fmt)) {
428 if (!skip) ++fields;
429 break;
430 }
431 // skip modifiers & field width
432 }
433 }
434 return fields;
435}
436
437
438////////////////////////////////////////////////////////////////////////////////
439/// Compute range.
440
442{
444
445 for (Int_t i = 0; i < fNpoints; i++) {
446 if (fX[i] - fEX[i] < xmin) {
447 if (gPad && gPad->GetLogx()) {
448 if (fEX[i] < fX[i]) xmin = fX[i] - fEX[i];
449 else xmin = TMath::Min(xmin, fX[i] / 3);
450 } else {
451 xmin = fX[i] - fEX[i];
452 }
453 }
454 if (fX[i] + fEX[i] > xmax) xmax = fX[i] + fEX[i];
455 if (fY[i] - fEY[i] < ymin) {
456 if (gPad && gPad->GetLogy()) {
457 if (fEY[i] < fY[i]) ymin = fY[i] - fEY[i];
458 else ymin = TMath::Min(ymin, fY[i] / 3);
459 } else {
460 ymin = fY[i] - fEY[i];
461 }
462 }
463 if (fY[i] + fEY[i] > ymax) ymax = fY[i] + fEY[i];
464 }
465}
466
467
468////////////////////////////////////////////////////////////////////////////////
469/// Copy and release.
470
472 Int_t ibegin, Int_t iend, Int_t obegin)
473{
474 CopyPoints(newarrays, ibegin, iend, obegin);
475 if (newarrays) {
476 delete[] fX;
477 fX = newarrays[2];
478 delete[] fY;
479 fY = newarrays[3];
480 delete[] fEX;
481 fEX = newarrays[0];
482 delete[] fEY;
483 fEY = newarrays[1];
484 delete[] newarrays;
485 }
486}
487
488
489////////////////////////////////////////////////////////////////////////////////
490/// Copy errors from fEX and fEY to arrays[0] and arrays[1]
491/// or to fX and fY. Copy points.
492
494 Int_t obegin)
495{
496 if (TGraph::CopyPoints(arrays ? arrays + 2 : 0, ibegin, iend, obegin)) {
497 Int_t n = (iend - ibegin) * sizeof(Double_t);
498 if (arrays) {
499 memmove(&arrays[0][obegin], &fEX[ibegin], n);
500 memmove(&arrays[1][obegin], &fEY[ibegin], n);
501 } else {
502 memmove(&fEX[obegin], &fEX[ibegin], n);
503 memmove(&fEY[obegin], &fEY[ibegin], n);
504 }
505 return kTRUE;
506 } else {
507 return kFALSE;
508 }
509}
510
511
512////////////////////////////////////////////////////////////////////////////////
513/// Constructor allocate.
514///Note: This function should be called only from the constructor
515/// since it does not delete previously existing arrays
516
518{
519
520 if (!fNpoints) {
521 fEX = fEY = 0;
522 return kFALSE;
523 } else {
524 fEX = new Double_t[fMaxSize];
525 fEY = new Double_t[fMaxSize];
526 }
527 return kTRUE;
528}
529
530////////////////////////////////////////////////////////////////////////////////
531/// protected function to perform the merge operation of a graph with errors
532
534{
535 if (g->GetN() == 0) return kFALSE;
536
537 Double_t * ex = g->GetEX();
538 Double_t * ey = g->GetEY();
539 if (ex == 0 || ey == 0 ) {
540 if (g->IsA() != TGraph::Class() )
541 Warning("DoMerge","Merging a %s is not compatible with a TGraphErrors - errors will be ignored",g->IsA()->GetName());
542 return TGraph::DoMerge(g);
543 }
544 for (Int_t i = 0 ; i < g->GetN(); i++) {
545 Int_t ipoint = GetN();
546 Double_t x = g->GetX()[i];
547 Double_t y = g->GetY()[i];
548 SetPoint(ipoint, x, y);
549 SetPointError( ipoint, ex[i], ey[i] );
550 }
551 return kTRUE;
552}
553
554
555////////////////////////////////////////////////////////////////////////////////
556/// Set zero values for point arrays in the range [begin, end]
557
558void TGraphErrors::FillZero(Int_t begin, Int_t end, Bool_t from_ctor)
559{
560 if (!from_ctor) {
561 TGraph::FillZero(begin, end, from_ctor);
562 }
563 Int_t n = (end - begin) * sizeof(Double_t);
564 memset(fEX + begin, 0, n);
565 memset(fEY + begin, 0, n);
566}
567
568
569////////////////////////////////////////////////////////////////////////////////
570/// This function is called by GraphFitChisquare.
571/// It returns the error along X at point i.
572
574{
575 if (i < 0 || i >= fNpoints) return -1;
576 if (fEX) return fEX[i];
577 return -1;
578}
579
580
581////////////////////////////////////////////////////////////////////////////////
582/// This function is called by GraphFitChisquare.
583/// It returns the error along Y at point i.
584
586{
587 if (i < 0 || i >= fNpoints) return -1;
588 if (fEY) return fEY[i];
589 return -1;
590}
591
592
593////////////////////////////////////////////////////////////////////////////////
594/// This function is called by GraphFitChisquare.
595/// It returns the error along X at point i.
596
598{
599 if (i < 0 || i >= fNpoints) return -1;
600 if (fEX) return fEX[i];
601 return -1;
602}
603
604
605////////////////////////////////////////////////////////////////////////////////
606/// This function is called by GraphFitChisquare.
607/// It returns the error along X at point i.
608
610{
611 if (i < 0 || i >= fNpoints) return -1;
612 if (fEX) return fEX[i];
613 return -1;
614}
615
616
617////////////////////////////////////////////////////////////////////////////////
618/// This function is called by GraphFitChisquare.
619/// It returns the error along X at point i.
620
622{
623 if (i < 0 || i >= fNpoints) return -1;
624 if (fEY) return fEY[i];
625 return -1;
626}
627
628
629////////////////////////////////////////////////////////////////////////////////
630/// This function is called by GraphFitChisquare.
631/// It returns the error along X at point i.
632
634{
635 if (i < 0 || i >= fNpoints) return -1;
636 if (fEY) return fEY[i];
637 return -1;
638}
639
640////////////////////////////////////////////////////////////////////////////////
641/// Adds all graphs with errors from the collection to this graph.
642/// Returns the total number of poins in the result or -1 in case of an error.
643
645{
646 TIter next(li);
647 while (TObject* o = next()) {
648 TGraph *g = dynamic_cast<TGraph*>(o);
649 if (!g) {
650 Error("Merge",
651 "Cannot merge - an object which doesn't inherit from TGraph found in the list");
652 return -1;
653 }
654 int n0 = GetN();
655 int n1 = n0+g->GetN();
656 Set(n1);
657 Double_t * x = g->GetX();
658 Double_t * y = g->GetY();
659 Double_t * ex = g->GetEX();
660 Double_t * ey = g->GetEY();
661 for (Int_t i = 0 ; i < g->GetN(); i++) {
662 SetPoint(n0+i, x[i], y[i]);
663 if (ex) fEX[n0+i] = ex[i];
664 if (ey) fEY[n0+i] = ey[i];
665 }
666 }
667 return GetN();
668}
669
670////////////////////////////////////////////////////////////////////////////////
671/// Print graph and errors values.
672
674{
675 for (Int_t i = 0; i < fNpoints; i++) {
676 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]);
677 }
678}
679
680
681////////////////////////////////////////////////////////////////////////////////
682/// Save primitive as a C++ statement(s) on output stream out
683
684void TGraphErrors::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
685{
686 char quote = '"';
687 out << " " << std::endl;
688 static Int_t frameNumber = 1000;
689 frameNumber++;
690
691 Int_t i;
692 TString fXName = TString(GetName()) + Form("_fx%d",frameNumber);
693 TString fYName = TString(GetName()) + Form("_fy%d",frameNumber);
694 TString fEXName = TString(GetName()) + Form("_fex%d",frameNumber);
695 TString fEYName = TString(GetName()) + Form("_fey%d",frameNumber);
696 out << " Double_t " << fXName << "[" << fNpoints << "] = {" << std::endl;
697 for (i = 0; i < fNpoints-1; i++) out << " " << fX[i] << "," << std::endl;
698 out << " " << fX[fNpoints-1] << "};" << std::endl;
699 out << " Double_t " << fYName << "[" << fNpoints << "] = {" << std::endl;
700 for (i = 0; i < fNpoints-1; i++) out << " " << fY[i] << "," << std::endl;
701 out << " " << fY[fNpoints-1] << "};" << std::endl;
702 out << " Double_t " << fEXName << "[" << fNpoints << "] = {" << std::endl;
703 for (i = 0; i < fNpoints-1; i++) out << " " << fEX[i] << "," << std::endl;
704 out << " " << fEX[fNpoints-1] << "};" << std::endl;
705 out << " Double_t " << fEYName << "[" << fNpoints << "] = {" << std::endl;
706 for (i = 0; i < fNpoints-1; i++) out << " " << fEY[i] << "," << std::endl;
707 out << " " << fEY[fNpoints-1] << "};" << std::endl;
708
709 if (gROOT->ClassSaved(TGraphErrors::Class())) out << " ";
710 else out << " TGraphErrors *";
711 out << "gre = new TGraphErrors(" << fNpoints << ","
712 << fXName << "," << fYName << ","
713 << fEXName << "," << fEYName << ");"
714 << std::endl;
715
716 out << " gre->SetName(" << quote << GetName() << quote << ");" << std::endl;
717 out << " gre->SetTitle(" << quote << GetTitle() << quote << ");" << std::endl;
718
719 SaveFillAttributes(out, "gre", 0, 1001);
720 SaveLineAttributes(out, "gre", 1, 1, 1);
721 SaveMarkerAttributes(out, "gre", 1, 1, 1);
722
723 if (fHistogram) {
724 TString hname = fHistogram->GetName();
725 hname += frameNumber;
726 fHistogram->SetName(Form("Graph_%s", hname.Data()));
727 fHistogram->SavePrimitive(out, "nodraw");
728 out << " gre->SetHistogram(" << fHistogram->GetName() << ");" << std::endl;
729 out << " " << std::endl;
730 }
731
732 // save list of functions
733 TIter next(fFunctions);
734 TObject *obj;
735 while ((obj = next())) {
736 obj->SavePrimitive(out, Form("nodraw #%d\n",++frameNumber));
737 if (obj->InheritsFrom("TPaveStats")) {
738 out << " gre->GetListOfFunctions()->Add(ptstats);" << std::endl;
739 out << " ptstats->SetParent(gre->GetListOfFunctions());" << std::endl;
740 } else {
741 TString objname;
742 objname.Form("%s%d",obj->GetName(),frameNumber);
743 if (obj->InheritsFrom("TF1")) {
744 out << " " << objname << "->SetParent(gre);\n";
745 }
746 out << " gre->GetListOfFunctions()->Add("
747 << objname << ");" << std::endl;
748 }
749 }
750
751 const char *l = strstr(option, "multigraph");
752 if (l) {
753 out << " multigraph->Add(gre," << quote << l + 10 << quote << ");" << std::endl;
754 } else {
755 out << " gre->Draw(" << quote << option << quote << ");" << std::endl;
756 }
757}
758
759
760////////////////////////////////////////////////////////////////////////////////
761/// Set ex and ey values for point pointed by the mouse.
762
764{
765 Int_t px = gPad->GetEventX();
766 Int_t py = gPad->GetEventY();
767
768 //localize point to be deleted
769 Int_t ipoint = -2;
770 Int_t i;
771 // start with a small window (in case the mouse is very close to one point)
772 for (i = 0; i < fNpoints; i++) {
773 Int_t dpx = px - gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
774 Int_t dpy = py - gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
775 if (dpx * dpx + dpy * dpy < 25) {
776 ipoint = i;
777 break;
778 }
779 }
780 if (ipoint == -2) return;
781
782 fEX[ipoint] = ex;
783 fEY[ipoint] = ey;
784 gPad->Modified();
785}
786
787
788////////////////////////////////////////////////////////////////////////////////
789/// Set ex and ey values for point number i.
790
792{
793 if (i < 0) return;
794 if (i >= fNpoints) {
795 // re-allocate the object
796 TGraphErrors::SetPoint(i, 0, 0);
797 }
798 fEX[i] = ex;
799 fEY[i] = ey;
800}
801
802
803////////////////////////////////////////////////////////////////////////////////
804/// Stream an object of class TGraphErrors.
805
806void TGraphErrors::Streamer(TBuffer &b)
807{
808 if (b.IsReading()) {
809 UInt_t R__s, R__c;
810 Version_t R__v = b.ReadVersion(&R__s, &R__c);
811 if (R__v > 2) {
812 b.ReadClassBuffer(TGraphErrors::Class(), this, R__v, R__s, R__c);
813 return;
814 }
815 //====process old versions before automatic schema evolution
816 TGraph::Streamer(b);
817 fEX = new Double_t[fNpoints];
818 fEY = new Double_t[fNpoints];
819 if (R__v < 2) {
820 Float_t *ex = new Float_t[fNpoints];
821 Float_t *ey = new Float_t[fNpoints];
822 b.ReadFastArray(ex, fNpoints);
823 b.ReadFastArray(ey, fNpoints);
824 for (Int_t i = 0; i < fNpoints; i++) {
825 fEX[i] = ex[i];
826 fEY[i] = ey[i];
827 }
828 delete [] ey;
829 delete [] ex;
830 } else {
831 b.ReadFastArray(fEX, fNpoints);
832 b.ReadFastArray(fEY, fNpoints);
833 }
834 b.CheckByteCount(R__s, R__c, TGraphErrors::IsA());
835 //====end of old versions
836
837 } else {
838 b.WriteClassBuffer(TGraphErrors::Class(), this);
839 }
840}
841
842
843////////////////////////////////////////////////////////////////////////////////
844/// Swap points
845
847{
848 SwapValues(fEX, pos1, pos2);
849 SwapValues(fEY, pos1, pos2);
850 TGraph::SwapPoints(pos1, pos2);
851}
void Class()
Definition: Class.C:29
#define b(i)
Definition: RSha256.hxx:100
#define f(i)
Definition: RSha256.hxx:104
#define g(i)
Definition: RSha256.hxx:105
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
short Version_t
Definition: RtypesCore.h:61
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:363
char * R__STRTOK_R(char *str, const char *delim, char **saveptr)
Definition: Rtypes.h:484
float xmin
Definition: THbookFile.cxx:93
float ymin
Definition: THbookFile.cxx:93
float xmax
Definition: THbookFile.cxx:93
float ymax
Definition: THbookFile.cxx:93
#define gROOT
Definition: TROOT.h:410
char * Form(const char *fmt,...)
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
#define gPad
Definition: TVirtualPad.h:286
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:233
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:262
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:245
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Collection abstract base class.
Definition: TCollection.h:63
1-Dim function class
Definition: TF1.h:211
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:26
Double_t GetErrorXhigh(Int_t bin) const
This function is called by GraphFitChisquare.
Double_t GetErrorXlow(Int_t bin) const
This function is called by GraphFitChisquare.
virtual Int_t Merge(TCollection *list)
Adds all graphs with errors from the collection to this graph.
Double_t GetErrorYlow(Int_t bin) const
This function is called by GraphFitChisquare.
Double_t * fEY
[fNpoints] array of Y errors
Definition: TGraphErrors.h:30
Double_t GetErrorX(Int_t bin) const
This function is called by GraphFitChisquare.
static Int_t CalculateScanfFields(const char *fmt)
Calculate scan fields.
virtual void ComputeRange(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax) const
Compute range.
Double_t * fEX
[fNpoints] array of X errors
Definition: TGraphErrors.h:29
virtual void SwapPoints(Int_t pos1, Int_t pos2)
Swap points.
virtual void Apply(TF1 *f)
apply function to all the data points y = f(x,y)
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 SetPointError(Double_t ex, Double_t ey)
Set ex and ey values for point pointed by the mouse.
virtual Bool_t DoMerge(const TGraph *g)
protected function to perform the merge operation of a graph with errors
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Double_t GetErrorY(Int_t bin) const
This function is called by GraphFitChisquare.
Double_t GetErrorYhigh(Int_t bin) const
This function is called by GraphFitChisquare.
TGraphErrors & operator=(const TGraphErrors &gr)
TGraphErrors assignment operator.
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 void Print(Option_t *chopt="") const
Print graph and errors values.
virtual ~TGraphErrors()
TGraphErrors default destructor.
TGraphErrors()
TGraphErrors default constructor.
Bool_t CtorAllocate()
Constructor allocate.
virtual void CopyAndRelease(Double_t **newarrays, Int_t ibegin, Int_t iend, Int_t obegin)
Copy and release.
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
Int_t fNpoints
Number of points <= fMaxSize.
Definition: TGraph.h:46
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2200
Int_t fMaxSize
!Current dimension of arrays fX and fY
Definition: TGraph.h:45
TH1F * fHistogram
Pointer to histogram used for drawing axis.
Definition: TGraph.h:50
Int_t GetN() const
Definition: TGraph.h:123
Double_t * fY
[fNpoints] array of Y points
Definition: TGraph.h:48
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
TList * fFunctions
Pointer to list of functions (fits and user)
Definition: TGraph.h:49
static void SwapValues(Double_t *arr, Int_t pos1, Int_t pos2)
Swap values.
Definition: TGraph.cxx:2397
virtual Bool_t DoMerge(const TGraph *g)
protected function to perform the merge operation of a graph
Definition: TGraph.cxx:2461
virtual void SwapPoints(Int_t pos1, Int_t pos2)
Swap points.
Definition: TGraph.cxx:2388
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:1016
Double_t * fX
[fNpoints] array of X points
Definition: TGraph.h:47
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:2135
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:1582
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 !...
Definition: TGraph.cxx:695
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition: TGraph.cxx:187
The TH1 histogram class.
Definition: TH1.h:56
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TH1.cxx:6661
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8282
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out".
Definition: TObject.cxx:664
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
void MakeZombie()
Definition: TObject.h:49
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1896
Double_t Atof() const
Return floating-point value contained in string.
Definition: TString.cxx:1962
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition: TString.cxx:1766
const char * Data() const
Definition: TString.h:364
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1738
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
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:2286
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2264
Float_t GetErrorX() const
Definition: TStyle.h:174
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1264
TVectorT.
Definition: TVectorT.h:27
Int_t GetLwb() const
Definition: TVectorT.h:73
TLine * line
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Double_t ey[n]
Definition: legend1.C:17
TGraphErrors * gr
Definition: legend1.C:25
Double_t ex[n]
Definition: legend1.C:17
TMath.
Definition: TMathBase.h:35
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:180
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
auto * l
Definition: textangle.C:4