00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <string.h>
00013
00014 #include "Riostream.h"
00015 #include "TROOT.h"
00016 #include "TGraphErrors.h"
00017 #include "TStyle.h"
00018 #include "TMath.h"
00019 #include "TArrow.h"
00020 #include "TBox.h"
00021 #include "TVirtualPad.h"
00022 #include "TH1.h"
00023 #include "TF1.h"
00024 #include "TVector.h"
00025 #include "TVectorD.h"
00026 #include "TStyle.h"
00027 #include "TClass.h"
00028 #include <string>
00029
00030 ClassImp(TGraphErrors)
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 TGraphErrors::TGraphErrors(): TGraph()
00069 {
00070
00071
00072 if (!CtorAllocate()) return;
00073 }
00074
00075
00076
00077 TGraphErrors::TGraphErrors(Int_t n)
00078 : TGraph(n)
00079 {
00080
00081
00082
00083
00084 if (!CtorAllocate()) return;
00085 FillZero(0, fNpoints);
00086 }
00087
00088
00089
00090 TGraphErrors::TGraphErrors(Int_t n, const Float_t *x, const Float_t *y, const Float_t *ex, const Float_t *ey)
00091 : TGraph(n,x,y)
00092 {
00093
00094
00095
00096
00097 if (!CtorAllocate()) return;
00098
00099 for (Int_t i=0;i<n;i++) {
00100 if (ex) fEX[i] = ex[i];
00101 else fEX[i] = 0;
00102 if (ey) fEY[i] = ey[i];
00103 else fEY[i] = 0;
00104 }
00105 }
00106
00107
00108
00109 TGraphErrors::TGraphErrors(Int_t n, const Double_t *x, const Double_t *y, const Double_t *ex, const Double_t *ey)
00110 : TGraph(n,x,y)
00111 {
00112
00113
00114
00115
00116 if (!CtorAllocate()) return;
00117
00118 n = sizeof(Double_t)*fNpoints;
00119 if (ex) memcpy(fEX, ex, n);
00120 else memset(fEX, 0, n);
00121 if (ey) memcpy(fEY, ey, n);
00122 else memset(fEY, 0, n);
00123 }
00124
00125
00126
00127 TGraphErrors::TGraphErrors(const TVectorF &vx, const TVectorF &vy, const TVectorF &vex, const TVectorF &vey)
00128 :TGraph()
00129 {
00130
00131
00132
00133
00134
00135
00136 fNpoints = TMath::Min(vx.GetNrows(), vy.GetNrows());
00137 if (!TGraph::CtorAllocate()) return;
00138 if (!CtorAllocate()) return;
00139 Int_t ivxlow = vx.GetLwb();
00140 Int_t ivylow = vy.GetLwb();
00141 Int_t ivexlow = vex.GetLwb();
00142 Int_t iveylow = vey.GetLwb();
00143 for (Int_t i=0;i<fNpoints;i++) {
00144 fX[i] = vx(i+ivxlow);
00145 fY[i] = vy(i+ivylow);
00146 fEX[i] = vex(i+ivexlow);
00147 fEY[i] = vey(i+iveylow);
00148 }
00149 }
00150
00151
00152
00153 TGraphErrors::TGraphErrors(const TVectorD &vx, const TVectorD &vy, const TVectorD &vex, const TVectorD &vey)
00154 :TGraph()
00155 {
00156
00157
00158
00159
00160
00161
00162 fNpoints = TMath::Min(vx.GetNrows(), vy.GetNrows());
00163 if (!TGraph::CtorAllocate()) return;
00164 if (!CtorAllocate()) return;
00165 Int_t ivxlow = vx.GetLwb();
00166 Int_t ivylow = vy.GetLwb();
00167 Int_t ivexlow = vex.GetLwb();
00168 Int_t iveylow = vey.GetLwb();
00169 for (Int_t i=0;i<fNpoints;i++) {
00170 fX[i] = vx(i+ivxlow);
00171 fY[i] = vy(i+ivylow);
00172 fEX[i] = vex(i+ivexlow);
00173 fEY[i] = vey(i+iveylow);
00174 }
00175 }
00176
00177
00178
00179 TGraphErrors::TGraphErrors(const TGraphErrors &gr)
00180 : TGraph(gr)
00181 {
00182
00183
00184 if (!CtorAllocate()) return;
00185
00186 Int_t n = sizeof(Double_t)*fNpoints;
00187 memcpy(fEX, gr.fEX, n);
00188 memcpy(fEY, gr.fEY, n);
00189 }
00190
00191
00192
00193 TGraphErrors& TGraphErrors::operator=(const TGraphErrors &gr)
00194 {
00195
00196
00197 if(this!=&gr) {
00198 TGraph::operator=(gr);
00199 if (!CtorAllocate()) return *this;
00200
00201 Int_t n = sizeof(Double_t)*fNpoints;
00202 memcpy(fEX, gr.fEX, n);
00203 memcpy(fEY, gr.fEY, n);
00204 }
00205 return *this;
00206 }
00207
00208
00209
00210 TGraphErrors::TGraphErrors(const TH1 *h)
00211 : TGraph(h)
00212 {
00213
00214
00215 if (!CtorAllocate()) return;
00216
00217 for (Int_t i=0;i<fNpoints;i++) {
00218 fEX[i] = h->GetBinWidth(i+1)*gStyle->GetErrorX();
00219 fEY[i] = h->GetBinError(i+1);
00220 }
00221 }
00222
00223
00224
00225 TGraphErrors::TGraphErrors(const char *filename, const char *format, Option_t *)
00226 : TGraph(100)
00227 {
00228
00229
00230
00231
00232
00233
00234
00235 if (!CtorAllocate()) return;
00236 Double_t x,y,ex,ey;
00237 ifstream infile(filename);
00238 if(!infile.good()){
00239 MakeZombie();
00240 Error("TGrapherrors", "Cannot open file: %s, TGraphErrors is Zombie",filename);
00241 fNpoints = 0;
00242 return;
00243 }
00244
00245 Int_t ncol = CalculateScanfFields(format);
00246 Int_t np = 0;
00247
00248 std::string line;
00249 while(std::getline(infile,line,'\n')){
00250 ex=ey=0;
00251 Int_t res;
00252 if (ncol < 3) {
00253 res = sscanf(line.c_str(),format,&x, &y);
00254 } else if(ncol <4) {
00255 res = sscanf(line.c_str(),format,&x, &y, &ey);
00256 } else {
00257 res = sscanf(line.c_str(),format,&x, &y, &ex, &ey);
00258 }
00259 if (res < 2) {
00260
00261 continue;
00262 }
00263 SetPoint(np,x,y);
00264 SetPointError(np,ex,ey);
00265 np++;
00266 }
00267 Set(np);
00268 }
00269
00270
00271
00272 TGraphErrors::~TGraphErrors()
00273 {
00274
00275
00276 delete [] fEX;
00277 delete [] fEY;
00278 }
00279
00280
00281
00282 void TGraphErrors::Apply(TF1 *f)
00283 {
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 Double_t x,y,ex,ey;
00297
00298 for (Int_t i=0;i<GetN();i++) {
00299 GetPoint(i,x,y);
00300 ex=GetErrorX(i);
00301 ey=GetErrorY(i);
00302
00303 SetPoint(i,x,f->Eval(x,y));
00304 SetPointError(i,ex,TMath::Abs(f->Eval(x,y+ey) - f->Eval(x,y-ey))/2.);
00305 }
00306 }
00307
00308
00309
00310 Int_t TGraphErrors::CalculateScanfFields(const char *fmt)
00311 {
00312
00313
00314 Int_t fields = 0;
00315 while ((fmt = strchr(fmt, '%'))) {
00316 Bool_t skip = kFALSE;
00317 while (*(++fmt)) {
00318 if ('[' == *fmt) {
00319 if (*++fmt && '^' == *fmt) ++fmt;
00320 if (*++fmt && ']' == *fmt) ++fmt;
00321 while (*fmt && *fmt != ']')
00322 ++fmt;
00323 if (!skip) ++fields;
00324 break;
00325 }
00326 if ('%' == *fmt) break;
00327 if ('*' == *fmt) {
00328 skip = kTRUE;
00329 } else if (strchr("dDiouxXxfegEscpn", *fmt)) {
00330 if (!skip) ++fields;
00331 break;
00332 }
00333
00334 }
00335 }
00336 return fields;
00337 }
00338
00339
00340
00341 void TGraphErrors::ComputeRange(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax) const
00342 {
00343
00344
00345 TGraph::ComputeRange(xmin,ymin,xmax,ymax);
00346
00347 for (Int_t i=0;i<fNpoints;i++) {
00348 if (fX[i] -fEX[i] < xmin) {
00349 if (gPad && gPad->GetLogx()) {
00350 if (fEX[i] < fX[i]) xmin = fX[i]-fEX[i];
00351 else xmin = TMath::Min(xmin,fX[i]/3);
00352 } else {
00353 xmin = fX[i]-fEX[i];
00354 }
00355 }
00356 if (fX[i] +fEX[i] > xmax) xmax = fX[i]+fEX[i];
00357 if (fY[i] -fEY[i] < ymin) {
00358 if (gPad && gPad->GetLogy()) {
00359 if (fEY[i] < fY[i]) ymin = fY[i]-fEY[i];
00360 else ymin = TMath::Min(ymin,fY[i]/3);
00361 } else {
00362 ymin = fY[i]-fEY[i];
00363 }
00364 }
00365 if (fY[i] +fEY[i] > ymax) ymax = fY[i]+fEY[i];
00366 }
00367 }
00368
00369
00370
00371 void TGraphErrors::CopyAndRelease(Double_t **newarrays,
00372 Int_t ibegin, Int_t iend, Int_t obegin)
00373 {
00374
00375
00376 CopyPoints(newarrays, ibegin, iend, obegin);
00377 if (newarrays) {
00378 delete[] fX;
00379 fX = newarrays[2];
00380 delete[] fY;
00381 fY = newarrays[3];
00382 delete[] fEX;
00383 fEX = newarrays[0];
00384 delete[] fEY;
00385 fEY = newarrays[1];
00386 delete[] newarrays;
00387 }
00388 }
00389
00390
00391
00392 Bool_t TGraphErrors::CopyPoints(Double_t **arrays, Int_t ibegin, Int_t iend,
00393 Int_t obegin)
00394 {
00395
00396
00397
00398 if (TGraph::CopyPoints(arrays ? arrays+2 : 0, ibegin, iend, obegin)) {
00399 Int_t n = (iend - ibegin)*sizeof(Double_t);
00400 if (arrays) {
00401 memmove(&arrays[0][obegin], &fEX[ibegin], n);
00402 memmove(&arrays[1][obegin], &fEY[ibegin], n);
00403 } else {
00404 memmove(&fEX[obegin], &fEX[ibegin], n);
00405 memmove(&fEY[obegin], &fEY[ibegin], n);
00406 }
00407 return kTRUE;
00408 } else {
00409 return kFALSE;
00410 }
00411 }
00412
00413
00414
00415 Bool_t TGraphErrors::CtorAllocate()
00416 {
00417
00418
00419 if (!fNpoints) {
00420 fEX = fEY = 0;
00421 return kFALSE;
00422 } else {
00423 fEX = new Double_t[fMaxSize];
00424 fEY = new Double_t[fMaxSize];
00425 }
00426 return kTRUE;
00427 }
00428
00429
00430
00431 void TGraphErrors::FillZero(Int_t begin, Int_t end, Bool_t from_ctor)
00432 {
00433
00434
00435 if (!from_ctor) {
00436 TGraph::FillZero(begin, end, from_ctor);
00437 }
00438 Int_t n = (end - begin)*sizeof(Double_t);
00439 memset(fEX + begin, 0, n);
00440 memset(fEY + begin, 0, n);
00441 }
00442
00443
00444
00445 Double_t TGraphErrors::GetErrorX(Int_t i) const
00446 {
00447
00448
00449
00450 if (i < 0 || i >= fNpoints) return -1;
00451 if (fEX) return fEX[i];
00452 return -1;
00453 }
00454
00455
00456
00457 Double_t TGraphErrors::GetErrorY(Int_t i) const
00458 {
00459
00460
00461
00462 if (i < 0 || i >= fNpoints) return -1;
00463 if (fEY) return fEY[i];
00464 return -1;
00465 }
00466
00467
00468
00469 Double_t TGraphErrors::GetErrorXhigh(Int_t i) const
00470 {
00471
00472
00473
00474 if (i < 0 || i >= fNpoints) return -1;
00475 if (fEX) return fEX[i];
00476 return -1;
00477 }
00478
00479
00480
00481 Double_t TGraphErrors::GetErrorXlow(Int_t i) const
00482 {
00483
00484
00485
00486 if (i < 0 || i >= fNpoints) return -1;
00487 if (fEX) return fEX[i];
00488 return -1;
00489 }
00490
00491
00492
00493 Double_t TGraphErrors::GetErrorYhigh(Int_t i) const
00494 {
00495
00496
00497
00498 if (i < 0 || i >= fNpoints) return -1;
00499 if (fEY) return fEY[i];
00500 return -1;
00501 }
00502
00503
00504
00505 Double_t TGraphErrors::GetErrorYlow(Int_t i) const
00506 {
00507
00508
00509
00510 if (i < 0 || i >= fNpoints) return -1;
00511 if (fEY) return fEY[i];
00512 return -1;
00513 }
00514
00515
00516
00517 void TGraphErrors::Print(Option_t *) const
00518 {
00519
00520
00521 for (Int_t i=0;i<fNpoints;i++) {
00522 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]);
00523 }
00524 }
00525
00526
00527
00528 void TGraphErrors::SavePrimitive(ostream &out, Option_t *option )
00529 {
00530
00531
00532 char quote = '"';
00533 out<<" "<<endl;
00534 if (gROOT->ClassSaved(TGraphErrors::Class())) {
00535 out<<" ";
00536 } else {
00537 out<<" TGraphErrors *";
00538 }
00539 out<<"gre = new TGraphErrors("<<fNpoints<<");"<<endl;
00540 out<<" gre->SetName("<<quote<<GetName()<<quote<<");"<<endl;
00541 out<<" gre->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
00542
00543 SaveFillAttributes(out,"gre",0,1001);
00544 SaveLineAttributes(out,"gre",1,1,1);
00545 SaveMarkerAttributes(out,"gre",1,1,1);
00546
00547 for (Int_t i=0;i<fNpoints;i++) {
00548 out<<" gre->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<endl;
00549 out<<" gre->SetPointError("<<i<<","<<fEX[i]<<","<<fEY[i]<<");"<<endl;
00550 }
00551
00552 static Int_t frameNumber = 0;
00553 if (fHistogram) {
00554 frameNumber++;
00555 TString hname = fHistogram->GetName();
00556 hname += frameNumber;
00557 fHistogram->SetName(hname.Data());
00558 fHistogram->SavePrimitive(out,"nodraw");
00559 out<<" gre->SetHistogram("<<fHistogram->GetName()<<");"<<endl;
00560 out<<" "<<endl;
00561 }
00562
00563
00564 TIter next(fFunctions);
00565 TObject *obj;
00566 while ((obj=next())) {
00567 obj->SavePrimitive(out,"nodraw");
00568 if (obj->InheritsFrom("TPaveStats")) {
00569 out<<" gre->GetListOfFunctions()->Add(ptstats);"<<endl;
00570 out<<" ptstats->SetParent(gre->GetListOfFunctions());"<<endl;
00571 } else {
00572 out<<" gre->GetListOfFunctions()->Add("<<obj->GetName()<<");"<<endl;
00573 }
00574 }
00575
00576 const char *l = strstr(option,"multigraph");
00577 if (l) {
00578 out<<" multigraph->Add(gre,"<<quote<<l+10<<quote<<");"<<endl;
00579 } else {
00580 out<<" gre->Draw("<<quote<<option<<quote<<");"<<endl;
00581 }
00582 }
00583
00584
00585
00586 void TGraphErrors::SetPointError(Double_t ex, Double_t ey)
00587 {
00588
00589
00590 Int_t px = gPad->GetEventX();
00591 Int_t py = gPad->GetEventY();
00592
00593
00594 Int_t ipoint = -2;
00595 Int_t i;
00596
00597 for (i=0;i<fNpoints;i++) {
00598 Int_t dpx = px - gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
00599 Int_t dpy = py - gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
00600 if (dpx*dpx+dpy*dpy < 25) {ipoint = i; break;}
00601 }
00602 if (ipoint == -2) return;
00603
00604 fEX[ipoint] = ex;
00605 fEY[ipoint] = ey;
00606 gPad->Modified();
00607 }
00608
00609
00610
00611 void TGraphErrors::SetPointError(Int_t i, Double_t ex, Double_t ey)
00612 {
00613
00614
00615 if (i < 0) return;
00616 if (i >= fNpoints) {
00617
00618 TGraphErrors::SetPoint(i,0,0);
00619 }
00620 fEX[i] = ex;
00621 fEY[i] = ey;
00622 }
00623
00624
00625
00626 void TGraphErrors::Streamer(TBuffer &b)
00627 {
00628
00629
00630 if (b.IsReading()) {
00631 UInt_t R__s, R__c;
00632 Version_t R__v = b.ReadVersion(&R__s, &R__c);
00633 if (R__v > 2) {
00634 b.ReadClassBuffer(TGraphErrors::Class(), this, R__v, R__s, R__c);
00635 return;
00636 }
00637
00638 TGraph::Streamer(b);
00639 fEX = new Double_t[fNpoints];
00640 fEY = new Double_t[fNpoints];
00641 if (R__v < 2) {
00642 Float_t *ex = new Float_t[fNpoints];
00643 Float_t *ey = new Float_t[fNpoints];
00644 b.ReadFastArray(ex,fNpoints);
00645 b.ReadFastArray(ey,fNpoints);
00646 for (Int_t i=0;i<fNpoints;i++) {
00647 fEX[i] = ex[i];
00648 fEY[i] = ey[i];
00649 }
00650 delete [] ey;
00651 delete [] ex;
00652 } else {
00653 b.ReadFastArray(fEX,fNpoints);
00654 b.ReadFastArray(fEY,fNpoints);
00655 }
00656 b.CheckByteCount(R__s, R__c, TGraphErrors::IsA());
00657
00658
00659 } else {
00660 b.WriteClassBuffer(TGraphErrors::Class(),this);
00661 }
00662 }
00663
00664
00665
00666 void TGraphErrors::SwapPoints(Int_t pos1, Int_t pos2)
00667 {
00668
00669
00670 SwapValues(fEX, pos1, pos2);
00671 SwapValues(fEY, pos1, pos2);
00672 TGraph::SwapPoints(pos1, pos2);
00673 }