Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
13#include "TROOT.h"
14#include "TBuffer.h"
15#include "TGraphErrors.h"
16#include "TStyle.h"
17#include "TMath.h"
18#include "TVirtualPad.h"
19#include "TH1.h"
20#include "TF1.h"
21#include "TVectorD.h"
22#include "TSystem.h"
23#include "strtok.h"
24
25#include <iostream>
26#include <fstream>
27#include <cstring>
28#include <string>
29
30
31
32////////////////////////////////////////////////////////////////////////////////
33
34/** \class TGraphErrors
35 \ingroup Graphs
36A TGraphErrors is a TGraph with error bars.
37
38The TGraphErrors painting is performed thanks to the TGraphPainter
39class. All details about the various painting options are given in this class.
40
41The picture below gives an example:
42
43Begin_Macro(source)
44{
45 auto c1 = new TCanvas("c1","A Simple Graph with error bars",200,10,700,500);
46 c1->SetFillColor(42);
47 c1->SetGrid();
48 c1->GetFrame()->SetFillColor(21);
49 c1->GetFrame()->SetBorderSize(12);
50 const Int_t n = 10;
51 Double_t x[n] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
52 Double_t y[n] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
53 Double_t ex[n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
54 Double_t ey[n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};
55 auto gr = new TGraphErrors(n,x,y,ex,ey);
56 gr->SetTitle("TGraphErrors Example");
57 gr->SetMarkerColor(4);
58 gr->SetMarkerStyle(21);
59 gr->Draw("ALP");
60}
61End_Macro
62*/
63
64
65////////////////////////////////////////////////////////////////////////////////
66/// TGraphErrors default constructor.
67
69{
70 if (!CtorAllocate()) return;
71}
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// TGraphErrors normal constructor.
76///
77/// the arrays are preset to zero
78
80 : TGraph(n)
81{
82 if (!CtorAllocate()) return;
84}
85
86
87////////////////////////////////////////////////////////////////////////////////
88/// TGraphErrors normal constructor.
89///
90/// if ex or ey are null, the corresponding arrays are preset to zero
91
93 : TGraph(n, x, y)
94{
95 if (!CtorAllocate()) return;
96
97 for (Int_t i = 0; i < n; i++) {
98 if (ex) fEX[i] = ex[i];
99 else fEX[i] = 0;
100 if (ey) fEY[i] = ey[i];
101 else fEY[i] = 0;
102 }
103}
104
105
106////////////////////////////////////////////////////////////////////////////////
107/// TGraphErrors normal constructor.
108///
109/// if ex or ey are null, the corresponding arrays are preset to zero
110
112 : TGraph(n, x, y)
113{
114 if (!CtorAllocate()) return;
115
116 n = sizeof(Double_t) * fNpoints;
117 if (ex) memcpy(fEX, ex, n);
118 else memset(fEX, 0, n);
119 if (ey) memcpy(fEY, ey, n);
120 else memset(fEY, 0, n);
121}
122
123
124////////////////////////////////////////////////////////////////////////////////
125/// Constructor with four vectors of floats in input.
126///
127/// A grapherrors is built with the X coordinates taken from vx and Y coord from vy
128/// and the errors from vectors vex and vey.
129/// The number of points in the graph is the minimum of number of points
130/// in vx and vy.
131
133 : TGraph(TMath::Min(vx.GetNrows(), vy.GetNrows()), vx.GetMatrixArray(), vy.GetMatrixArray() )
134{
135 if (!CtorAllocate()) return;
136 Int_t ivexlow = vex.GetLwb();
137 Int_t iveylow = vey.GetLwb();
138 for (Int_t i = 0; i < fNpoints; i++) {
139 fEX[i] = vex(i + ivexlow);
140 fEY[i] = vey(i + iveylow);
141 }
142}
143
144
145////////////////////////////////////////////////////////////////////////////////
146/// Constructor with four vectors of doubles in input.
147///
148/// A grapherrors is built with the X coordinates taken from vx and Y coord from vy
149/// and the errors from vectors vex and vey.
150/// The number of points in the graph is the minimum of number of points
151/// in vx and vy.
152
154 : TGraph(TMath::Min(vx.GetNrows(), vy.GetNrows()), vx.GetMatrixArray(), vy.GetMatrixArray() )
155{
156 if (!CtorAllocate()) return;
157 Int_t ivexlow = vex.GetLwb();
158 Int_t iveylow = vey.GetLwb();
159 for (Int_t i = 0; i < fNpoints; i++) {
160 fEX[i] = vex(i + ivexlow);
161 fEY[i] = vey(i + iveylow);
162 }
163}
164
165
166////////////////////////////////////////////////////////////////////////////////
167/// TGraphErrors copy constructor.
168
170 : TGraph(gr)
171{
172 if (!CtorAllocate()) return;
173
174 Int_t n = sizeof(Double_t) * fNpoints;
175 memcpy(fEX, gr.fEX, n);
176 memcpy(fEY, gr.fEY, n);
177}
178
179
180////////////////////////////////////////////////////////////////////////////////
181/// TGraphErrors assignment operator.
182
184{
185 if (this != &gr) {
187 // N.B CtorAllocate does not delete arrays
188 if (fEX) delete [] fEX;
189 if (fEY) delete [] fEY;
190 if (!CtorAllocate()) return *this;
191
192 Int_t n = sizeof(Double_t) * fNpoints;
193 memcpy(fEX, gr.fEX, n);
194 memcpy(fEY, gr.fEY, n);
195 }
196 return *this;
197}
198
199
200////////////////////////////////////////////////////////////////////////////////
201/// TGraphErrors constructor importing its parameters from the TH1 object passed as argument
202
204 : TGraph(h)
205{
206 if (!CtorAllocate()) return;
207
208 for (Int_t i = 0; i < fNpoints; i++) {
209 fEX[i] = h->GetBinWidth(i + 1) * gStyle->GetErrorX();
210 fEY[i] = h->GetBinError(i + 1);
211 }
212}
213
214
215////////////////////////////////////////////////////////////////////////////////
216/// GraphErrors constructor reading input from `filename`.
217///
218/// `filename` is assumed to contain at least 2 columns of numbers
219///
220/// Convention for format (default=`"%lg %lg %lg %lg"`)
221///
222/// - format = `"%lg %lg"` read only 2 first columns into X,Y
223/// - format = `"%lg %lg %lg"` read only 3 first columns into X,Y and EY
224/// - format = `"%lg %lg %lg %lg"` read only 4 first columns into X,Y,EX,EY.
225///
226/// For files separated by a specific delimiter different from ' ' and `\\t` (e.g. `;` in csv files)
227/// you can avoid using `%*s` to bypass this delimiter by explicitly specify the `option` argument,
228/// e.g. `option=" \\t,;"` for columns of figures separated by any of these characters (`' ', '\\t', ',', ';'`)
229/// used once (e.g. `"1;1"`) or in a combined way (`" 1;,;; 1"`).
230///
231/// Note in that case, the instantiation is about 2 times slower.
232/// In case a delimiter is specified, the format `"%lg %lg %lg"` will read X,Y,EX.
233
235 : TGraph(100)
236{
237 if (!CtorAllocate()) return;
238 Double_t x, y, ex, ey;
241 std::ifstream infile(fname.Data());
242 if (!infile.good()) {
243 MakeZombie();
244 Error("TGraphErrors", "Cannot open file: %s, TGraphErrors is Zombie", filename);
245 fNpoints = 0;
246 return;
247 }
248 std::string line;
249 Int_t np = 0;
250
251 if (strcmp(option, "") == 0) { // No delimiters specified (standard constructor).
252
253 Int_t ncol = CalculateScanfFields(format); //count number of columns in format
254 Int_t res;
255 while (std::getline(infile, line, '\n')) {
256 ex = ey = 0;
257 if (ncol < 3) {
258 res = sscanf(line.c_str(), format, &x, &y);
259 } else if (ncol < 4) {
260 res = sscanf(line.c_str(), format, &x, &y, &ey);
261 } else {
262 res = sscanf(line.c_str(), format, &x, &y, &ex, &ey);
263 }
264 if (res < 2) {
265 continue; //skip empty and ill-formed lines
266 }
267 SetPoint(np, x, y);
269 np++;
270 }
271 Set(np);
272
273 } else { // A delimiter has been specified in "option"
274
275 // Checking format and creating its boolean equivalent
277 format_.ReplaceAll(" ", "") ;
278 format_.ReplaceAll("\t", "") ;
279 format_.ReplaceAll("lg", "") ;
280 format_.ReplaceAll("s", "") ;
281 format_.ReplaceAll("%*", "0") ;
282 format_.ReplaceAll("%", "1") ;
283 if (!format_.IsDigit()) {
284 Error("TGraphErrors", "Incorrect input format! Allowed format tags are {\"%%lg\",\"%%*lg\" or \"%%*s\"}");
285 return ;
286 }
287 Int_t ntokens = format_.Length() ;
288 if (ntokens < 2) {
289 Error("TGraphErrors", "Incorrect input format! Only %d tag(s) in format whereas at least 2 \"%%lg\" tags are expected!", ntokens);
290 return ;
291 }
294 for (Int_t idx = 0; idx < ntokens; idx++) {
295 isTokenToBeSaved[idx] = TString::Format("%c", format_[idx]).Atoi() ; //atoi(&format_[idx]) does not work for some reason...
296 if (isTokenToBeSaved[idx] == 1) {
298 }
299 }
300 if (ntokens >= 2 && (ntokensToBeSaved < 2 || ntokensToBeSaved > 4)) { //first condition not to repeat the previous error message
301 Error("TGraphErrors", "Incorrect input format! There are %d \"%%lg\" tag(s) in format whereas 2,3 or 4 are expected!", ntokensToBeSaved);
302 delete [] isTokenToBeSaved ;
303 return ;
304 }
305
306 // Initializing loop variables
307 Bool_t isLineToBeSkipped = kFALSE; //empty and ill-formed lines
308 char *token = nullptr;
309 TString token_str = "";
310 Int_t token_idx = 0;
311 Double_t value[4]; //x,y,ex,ey buffers
312 for (Int_t k = 0; k < 4; k++)
313 value[k] = 0.;
314 Int_t value_idx = 0;
315
316 // Looping
317 char *rest;
318 while (std::getline(infile, line, '\n')) {
319 if (!line.empty()) {
320 if (line[line.size() - 1] == char(13)) { // removing DOS CR character
321 line.erase(line.end() - 1, line.end()) ;
322 }
323 token = R__STRTOK_R(const_cast<char *>(line.c_str()), option, &rest);
324 while (token != nullptr && value_idx < ntokensToBeSaved) {
326 token_str = TString(token) ;
327 token_str.ReplaceAll("\t", "") ;
328 if (!token_str.IsFloat()) {
330 break ;
331 } else {
332 value[value_idx] = token_str.Atof() ;
333 value_idx++ ;
334 }
335 }
336 token = R__STRTOK_R(nullptr, option, &rest); // next token
337 token_idx++ ;
338 }
339 if (!isLineToBeSkipped && value_idx > 1) { //i.e. 2,3 or 4
340 x = value[0];
341 y = value[1];
342 ex = value[2];
343 ey = value[3];
344 SetPoint(np, x, y);
346 np++ ;
347 }
348 }
350 token = nullptr;
351 token_idx = 0;
352 value_idx = 0;
353 }
354 Set(np) ;
355
356 // Cleaning
357 delete [] isTokenToBeSaved;
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/// Add a point with errorbars to the graph.
375
377{
378 AddPoint(x, y); // fNpoints will increase automatically
380}
381
382////////////////////////////////////////////////////////////////////////////////
383/// Apply function to all the data points \f$ y = f(x,y) \f$.
384///
385/// The error is calculated as \f$ ey=(f(x,y+ey)-f(x,y-ey))/2 \f$.
386/// This is the same as \f$ error(fy) = df/dy * ey \f$ for small errors.
387///
388/// For generic functions the symmetric errors might become non-symmetric
389/// and are averaged here. Use TGraphAsymmErrors if desired.
390///
391/// Error on \f$ x \f$ doesn't change.
392///
393/// function suggested/implemented by Miroslav Helbich <helbich@mail.desy.de>
394
396{
397 Double_t x, y, ex, ey;
398
399 if (fHistogram) {
400 delete fHistogram;
401 fHistogram = nullptr;
402 }
403 for (Int_t i = 0; i < GetN(); i++) {
404 GetPoint(i, x, y);
405 ex = GetErrorX(i);
406 ey = GetErrorY(i);
407
408 SetPoint(i, x, f->Eval(x, y));
409 SetPointError(i, ex, TMath::Abs(f->Eval(x, y + ey) - f->Eval(x, y - ey)) / 2.);
410 }
411 if (gPad) gPad->Modified();
412}
413
414////////////////////////////////////////////////////////////////////////////////
415/// Apply function to all the data points \f$ x = f(x,y) \f$.
416///
417/// The error is calculated as \f$ ex=(f(x+ex,y)-f(x-ex,y))/2 \f$.
418/// This is the same as \f$ error(fx) = df/dx * ex \f$ for small errors.
419///
420/// For generic functions the symmetric errors might become non-symmetric
421/// and are averaged here. Use TGraphAsymmErrors if desired.
422///
423/// Error on \f$ y \f$ doesn't change.
424
426{
427 Double_t x, y, ex, ey;
428
429 if (fHistogram) {
430 delete fHistogram;
431 fHistogram = nullptr;
432 }
433 for (Int_t i = 0; i < GetN(); i++) {
434 GetPoint(i, x, y);
435 ex = GetErrorX(i);
436 ey = GetErrorY(i);
437
438 SetPoint(i, f->Eval(x,y), y);
439 SetPointError(i, TMath::Abs(f->Eval(x + ex, y) - f->Eval(x - ex, y)) / 2. , ey);
440 }
441 if (gPad) gPad->Modified();
442}
443
444
445////////////////////////////////////////////////////////////////////////////////
446/// Calculate scan fields.
447
449{
450 Int_t fields = 0;
451 while ((fmt = strchr(fmt, '%'))) {
453 while (*(++fmt)) {
454 if ('[' == *fmt) {
455 if (*++fmt && '^' == *fmt) ++fmt; // "%[^]a]"
456 if (*++fmt && ']' == *fmt) ++fmt; // "%[]a]" or "%[^]a]"
457 while (*fmt && *fmt != ']')
458 ++fmt;
459 if (!skip) ++fields;
460 break;
461 }
462 if ('%' == *fmt) break; // %% literal %
463 if ('*' == *fmt) {
464 skip = kTRUE; // %*d -- skip a number
465 } else if (strchr("dDiouxXxfegEscpn", *fmt)) {
466 if (!skip) ++fields;
467 break;
468 }
469 // skip modifiers & field width
470 }
471 }
472 return fields;
473}
474
475
476////////////////////////////////////////////////////////////////////////////////
477/// Compute range.
478
480{
482
483 for (Int_t i = 0; i < fNpoints; i++) {
484 if (fX[i] - fEX[i] < xmin) {
485 if (gPad && gPad->GetLogx()) {
486 if (fEX[i] < fX[i]) xmin = fX[i] - fEX[i];
487 else xmin = TMath::Min(xmin, fX[i] / 3);
488 } else {
489 xmin = fX[i] - fEX[i];
490 }
491 }
492 if (fX[i] + fEX[i] > xmax) xmax = fX[i] + fEX[i];
493 if (fY[i] - fEY[i] < ymin) {
494 if (gPad && gPad->GetLogy()) {
495 if (fEY[i] < fY[i]) ymin = fY[i] - fEY[i];
496 else ymin = TMath::Min(ymin, fY[i] / 3);
497 } else {
498 ymin = fY[i] - fEY[i];
499 }
500 }
501 if (fY[i] + fEY[i] > ymax) ymax = fY[i] + fEY[i];
502 }
503}
504
505
506////////////////////////////////////////////////////////////////////////////////
507/// Copy and release.
508
511{
513 if (newarrays) {
514 delete[] fX;
515 fX = newarrays[2];
516 delete[] fY;
517 fY = newarrays[3];
518 delete[] fEX;
519 fEX = newarrays[0];
520 delete[] fEY;
521 fEY = newarrays[1];
522 delete[] newarrays;
523 }
524}
525
526
527////////////////////////////////////////////////////////////////////////////////
528/// Copy errors from `fEX` and `fEY` to `arrays[0]` and `arrays[1]`
529/// or to `fEX` and `fEY` if `arrays == 0` and `ibegin != iend`.
530
533{
534 if (TGraph::CopyPoints(arrays ? arrays + 2 : nullptr, ibegin, iend, obegin)) {
535 Int_t n = (iend - ibegin) * sizeof(Double_t);
536 if (arrays) {
537 memmove(&arrays[0][obegin], &fEX[ibegin], n);
538 memmove(&arrays[1][obegin], &fEY[ibegin], n);
539 } else {
540 memmove(&fEX[obegin], &fEX[ibegin], n);
541 memmove(&fEY[obegin], &fEY[ibegin], n);
542 }
543 return kTRUE;
544 } else {
545 return kFALSE;
546 }
547}
548
549
550////////////////////////////////////////////////////////////////////////////////
551/// Constructor allocate.
552///
553/// Note: This function should be called only from the constructor
554/// since it does not delete previously existing arrays.
555
557{
558
559 if (!fNpoints) {
560 fEX = fEY = nullptr;
561 return kFALSE;
562 } else {
563 fEX = new Double_t[fMaxSize];
564 fEY = new Double_t[fMaxSize];
565 }
566 return kTRUE;
567}
568
569////////////////////////////////////////////////////////////////////////////////
570/// Protected function to perform the merge operation of a graph with errors.
571
573{
574 if (g->GetN() == 0) return kFALSE;
575
576 Double_t * ex = g->GetEX();
577 Double_t * ey = g->GetEY();
578 if (ex == nullptr || ey == nullptr) {
579 if (g->IsA() != TGraph::Class() )
580 Warning("DoMerge","Merging a %s is not compatible with a TGraphErrors - errors will be ignored",g->IsA()->GetName());
581 return TGraph::DoMerge(g);
582 }
583 for (Int_t i = 0 ; i < g->GetN(); i++) {
584 Int_t ipoint = GetN();
585 Double_t x = g->GetX()[i];
586 Double_t y = g->GetY()[i];
587 SetPoint(ipoint, x, y);
588 SetPointError( ipoint, ex[i], ey[i] );
589 }
590 return kTRUE;
591}
592
593
594////////////////////////////////////////////////////////////////////////////////
595/// Set zero values for point arrays in the range `[begin, end]`.
596
598{
599 if (!from_ctor) {
600 TGraph::FillZero(begin, end, from_ctor);
601 }
602 Int_t n = (end - begin) * sizeof(Double_t);
603 memset(fEX + begin, 0, n);
604 memset(fEY + begin, 0, n);
605}
606
607
608////////////////////////////////////////////////////////////////////////////////
609/// It returns the error along X at point `i`.
610
612{
613 if (i < 0 || i >= fNpoints) return -1;
614 if (fEX) return fEX[i];
615 return -1;
616}
617
618
619////////////////////////////////////////////////////////////////////////////////
620/// It returns the error along Y at point `i`.
621
623{
624 if (i < 0 || i >= fNpoints) return -1;
625 if (fEY) return fEY[i];
626 return -1;
627}
628
629
630////////////////////////////////////////////////////////////////////////////////
631/// It returns the error along X at point `i`. For TGraphErrors this method is
632/// the same as GetErrorX.
633
635{
636 if (i < 0 || i >= fNpoints) return -1;
637 if (fEX) return fEX[i];
638 return -1;
639}
640
641
642////////////////////////////////////////////////////////////////////////////////
643/// It returns the error along X at point `i`. For TGraphErrors this method is
644/// the same as GetErrorX.
645
647{
648 if (i < 0 || i >= fNpoints) return -1;
649 if (fEX) return fEX[i];
650 return -1;
651}
652
653
654////////////////////////////////////////////////////////////////////////////////
655/// It returns the error along Y at point `i`. For TGraphErrors this method is
656/// the same as GetErrorY.
657
659{
660 if (i < 0 || i >= fNpoints) return -1;
661 if (fEY) return fEY[i];
662 return -1;
663}
664
665
666////////////////////////////////////////////////////////////////////////////////
667/// It returns the error along Y at point `i`. For TGraphErrors this method is
668/// the same as GetErrorY.
669
671{
672 if (i < 0 || i >= fNpoints) return -1;
673 if (fEY) return fEY[i];
674 return -1;
675}
676
677////////////////////////////////////////////////////////////////////////////////
678/// Adds all graphs with errors from the collection to this graph.
679/// Returns the total number of points in the result or -1 in case of an error.
680
682{
683 TIter next(li);
684 while (TObject* o = next()) {
685 TGraph *g = dynamic_cast<TGraph*>(o);
686 if (!g) {
687 Error("Merge",
688 "Cannot merge - an object which doesn't inherit from TGraph found in the list");
689 return -1;
690 }
691 int n0 = GetN();
692 int n1 = n0+g->GetN();
693 Set(n1);
694 Double_t * x = g->GetX();
695 Double_t * y = g->GetY();
696 Double_t * ex = g->GetEX();
697 Double_t * ey = g->GetEY();
698 for (Int_t i = 0 ; i < g->GetN(); i++) {
699 SetPoint(n0+i, x[i], y[i]);
700 if (ex) fEX[n0+i] = ex[i];
701 if (ey) fEY[n0+i] = ey[i];
702 }
703 }
704 return GetN();
705}
706
707////////////////////////////////////////////////////////////////////////////////
708/// Print graph and errors values.
709
711{
712 for (Int_t i = 0; i < fNpoints; i++) {
713 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]);
714 }
715}
716
717
718////////////////////////////////////////////////////////////////////////////////
719/// Save primitive as a C++ statement(s) on output stream out
720
721void TGraphErrors::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
722{
723 auto xname = SavePrimitiveVector(out, "gre_fx", fNpoints, fX, kTRUE);
724 auto yname = SavePrimitiveVector(out, "gre_fy", fNpoints, fY);
725 auto exname = SavePrimitiveVector(out, "gre_fex", fNpoints, fEX);
726 auto eyname = SavePrimitiveVector(out, "gre_fey", fNpoints, fEY);
727
729 out, Class(), "gre",
730 TString::Format("%d, %s.data(), %s.data(), %s.data(), %s.data()", fNpoints, xname.Data(), yname.Data(), exname.Data(), eyname.Data()), kFALSE);
731
733}
734
735////////////////////////////////////////////////////////////////////////////////
736/// Multiply the values and errors of a TGraphErrors by a constant c1.
737///
738/// If option contains "x" the x values and errors are scaled
739/// If option contains "y" the y values and errors are scaled
740/// If option contains "xy" both x and y values and errors are scaled
741
743{
745 TString opt = option; opt.ToLower();
746 if (opt.Contains("x") && GetEX()) {
747 for (Int_t i=0; i<GetN(); i++)
748 GetEX()[i] *= c1;
749 }
750 if (opt.Contains("y") && GetEY()) {
751 for (Int_t i=0; i<GetN(); i++)
752 GetEY()[i] *= c1;
753 }
754}
755
756////////////////////////////////////////////////////////////////////////////////
757/// Set `ex` and `ey` values for point pointed by the mouse.
758
760{
761 if (!gPad) {
762 Error("SetPointError", "Cannot be used without gPad, requires last mouse position");
763 return;
764 }
765
766 Int_t px = gPad->GetEventX();
767 Int_t py = gPad->GetEventY();
768
769 //localize point to be deleted
770 Int_t ipoint = -2;
771 Int_t i;
772 // start with a small window (in case the mouse is very close to one point)
773 for (i = 0; i < fNpoints; i++) {
774 Int_t dpx = px - gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
775 Int_t dpy = py - gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
776 if (dpx * dpx + dpy * dpy < 25) {
777 ipoint = i;
778 break;
779 }
780 }
781 if (ipoint == -2) return;
782
783 fEX[ipoint] = ex;
784 fEY[ipoint] = ey;
785 gPad->Modified();
786}
787
788
789////////////////////////////////////////////////////////////////////////////////
790/// Set `ex` and `ey` values for point number i.
791
793{
794 if (i < 0) return;
795 if (i >= fNpoints) {
796 // re-allocate the object
797 TGraphErrors::SetPoint(i, 0, 0);
798 }
799 fEX[i] = ex;
800 fEY[i] = ey;
801}
802
803
804////////////////////////////////////////////////////////////////////////////////
805/// Stream an object of class TGraphErrors.
806
808{
809 if (b.IsReading()) {
811 Version_t R__v = b.ReadVersion(&R__s, &R__c);
812 if (R__v > 2) {
813 b.ReadClassBuffer(TGraphErrors::Class(), this, R__v, R__s, R__c);
814 return;
815 }
816 //====process old versions before automatic schema evolution
818 fEX = new Double_t[fNpoints];
819 fEY = new Double_t[fNpoints];
820 if (R__v < 2) {
821 Float_t *ex = new Float_t[fNpoints];
822 Float_t *ey = new Float_t[fNpoints];
823 b.ReadFastArray(ex, fNpoints);
824 b.ReadFastArray(ey, fNpoints);
825 for (Int_t i = 0; i < fNpoints; i++) {
826 fEX[i] = ex[i];
827 fEY[i] = ey[i];
828 }
829 delete [] ey;
830 delete [] ex;
831 } else {
832 b.ReadFastArray(fEX, fNpoints);
833 b.ReadFastArray(fEY, fNpoints);
834 }
835 b.CheckByteCount(R__s, R__c, TGraphErrors::IsA());
836 //====end of old versions
837
838 } else {
839 b.WriteClassBuffer(TGraphErrors::Class(), this);
840 }
841}
842
843////////////////////////////////////////////////////////////////////////////////
844/// Swap points.
845
852
853////////////////////////////////////////////////////////////////////////////////
854/// Update the fX, fY, fEX, and fEY arrays with the sorted values.
855
857{
858 std::vector<Double_t> fEXSorted(numSortedPoints);
859 std::vector<Double_t> fEYSorted(numSortedPoints);
860
861 // Fill the sorted X and Y error values based on the sorted indices
862 std::generate(fEXSorted.begin(), fEXSorted.end(),
863 [begin = low, &sorting_indices, this]() mutable { return fEX[sorting_indices[begin++]]; });
864 std::generate(fEYSorted.begin(), fEYSorted.end(),
865 [begin = low, &sorting_indices, this]() mutable { return fEY[sorting_indices[begin++]]; });
866
867 // Copy the sorted X and Y error values back to the original arrays
868 std::copy(fEXSorted.begin(), fEXSorted.end(), fEX + low);
869 std::copy(fEYSorted.begin(), fEYSorted.end(), fEY + low);
870
872}
#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
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
float xmin
float ymin
float xmax
float ymax
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
#define gPad
const_iterator begin() const
const_iterator end() const
Buffer base class used for serializing objects.
Definition TBuffer.h:43
Collection abstract base class.
Definition TCollection.h:65
1-Dim function class
Definition TF1.h:182
A TGraphErrors is a TGraph with error bars.
void ComputeRange(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax) const override
Compute range.
void UpdateArrays(const std::vector< Int_t > &sorting_indices, Int_t numSortedPoints, Int_t low) override
Update the fX, fY, fEX, and fEY arrays with the sorted values.
Double_t GetErrorY(Int_t bin) const override
It returns the error along Y at point i.
Double_t * GetEX() const override
Double_t GetErrorX(Int_t bin) const override
It returns the error along X at point i.
Double_t * fEY
[fNpoints] array of Y errors
~TGraphErrors() override
TGraphErrors default destructor.
static TClass * Class()
void Scale(Double_t c1=1., Option_t *option="y") override
Multiply the values and errors of a TGraphErrors by a constant c1.
static Int_t CalculateScanfFields(const char *fmt)
Calculate scan fields.
void Streamer(TBuffer &) override
Stream an object of class TGraphErrors.
void FillZero(Int_t begin, Int_t end, Bool_t from_ctor=kTRUE) override
Set zero values for point arrays in the range [begin, end].
virtual void AddPointError(Double_t x, Double_t y, Double_t ex=0., Double_t ey=0.)
Add a point with errorbars to the graph.
Double_t * fEX
[fNpoints] array of X errors
void Print(Option_t *chopt="") const override
Print graph and errors values.
virtual void SetPointError(Double_t ex, Double_t ey)
Set ex and ey values for point pointed by the mouse.
Bool_t DoMerge(const TGraph *g) override
Protected function to perform the merge operation of a graph with errors.
Double_t * GetEY() const override
void SwapPoints(Int_t pos1, Int_t pos2) override
Swap points.
virtual void ApplyX(TF1 *f)
Apply function to all the data points .
Double_t GetErrorXhigh(Int_t bin) const override
It returns the error along X at point i.
Double_t GetErrorYlow(Int_t bin) const override
It returns the error along Y at point i.
Double_t GetErrorYhigh(Int_t bin) const override
It returns the error along Y at point i.
TGraphErrors & operator=(const TGraphErrors &gr)
TGraphErrors assignment operator.
Bool_t CopyPoints(Double_t **arrays, Int_t ibegin, Int_t iend, Int_t obegin) override
Copy errors from fEX and fEY to arrays[0] and arrays[1] or to fEX and fEY if arrays == 0 and ibegin !...
TClass * IsA() const override
Int_t Merge(TCollection *list) override
Adds all graphs with errors from the collection to this graph.
void CopyAndRelease(Double_t **newarrays, Int_t ibegin, Int_t iend, Int_t obegin) override
Copy and release.
TGraphErrors()
TGraphErrors default constructor.
Double_t GetErrorXlow(Int_t bin) const override
It returns the error along X at point i.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
void Apply(TF1 *f) override
Apply function to all the data points .
Bool_t CtorAllocate()
Constructor allocate.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
static TClass * Class()
virtual void AddPoint(Double_t x, Double_t y)
Append a new point to the graph.
Definition TGraph.h:97
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:2290
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
virtual void UpdateArrays(const std::vector< Int_t > &sorting_indices, Int_t numSortedPoints, Int_t low)
Update the fX and fY arrays with the sorted values.
Definition TGraph.cxx:2540
Int_t GetN() const
Definition TGraph.h:131
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:732
virtual void Scale(Double_t c1=1., Option_t *option="y")
Multiply the values of a TGraph by a constant c1.
Definition TGraph.cxx:2207
static void SwapValues(Double_t *arr, Int_t pos1, Int_t pos2)
Swap values.
Definition TGraph.cxx:2559
void Streamer(TBuffer &) override
Stream an object of class TGraph.
Definition TGraph.cxx:2464
virtual Bool_t DoMerge(const TGraph *g)
protected function to perform the merge operation of a graph
Definition TGraph.cxx:2624
virtual void SwapPoints(Int_t pos1, Int_t pos2)
Swap points.
Definition TGraph.cxx:2531
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:1103
void SaveHistogramAndFunctions(std::ostream &out, const char *varname, Option_t *option)
Save histogram and list of functions of TGraph as C++ statement Used in all TGraph-derived classes.
Definition TGraph.cxx:2163
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:2225
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:1533
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:780
TGraph & operator=(const TGraph &)
Equal operator for this graph.
Definition TGraph.cxx:233
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
Mother of all ROOT objects.
Definition TObject.h:41
static TString SavePrimitiveVector(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Bool_t empty_line=kFALSE)
Save array in the output stream "out" as vector.
Definition TObject.cxx:788
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
void MakeZombie()
Definition TObject.h:53
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:771
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
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:2384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
Float_t GetErrorX() const
Definition TStyle.h:188
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1285
TVectorT.
Definition TVectorT.h:29
TLine * line
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
Double_t ey[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
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)
Returns the smallest of a and b.
Definition TMathBase.h:199
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124