Skip to content

Commit

Permalink
[hist] implement adding TGraph with TF1
Browse files Browse the repository at this point in the history
Fixes #9637
  • Loading branch information
ferdymercury authored and couet committed Aug 13, 2024
1 parent 77c76c3 commit 40221a1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions hist/hist/inc/TGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class TGraph : public TNamed, public TAttLine, public TAttFill, public TAttMarke
~TGraph() override;

virtual void AddPoint(Double_t x, Double_t y) { SetPoint(fNpoints, x, y); } ///< Append a new point to the graph.
virtual void Add(TF1 *f, Double_t c1=1);
virtual void Apply(TF1 *f);
void Browse(TBrowser *b) override;
virtual Double_t Chisquare(TF1 *f1, Option_t *option="") const;
Expand Down
23 changes: 20 additions & 3 deletions hist/hist/src/TGraph.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,23 @@ Double_t** TGraph::AllocateArrays(Int_t Narrays, Int_t arraySize)
return newarrays;
}

////////////////////////////////////////////////////////////////////////////////
/// Performs the operation: `y = y + c1*f(x,y)`
/// Errors are not recalculated.
///
/// \param f may be a 1-D function TF1 or 2-d function TF2
/// \param c1 a scaling factor, 1 by default

void TGraph::Add(TF1 *f, Double_t c1)
{
if (fHistogram) SetBit(kResetHisto);

for (Int_t i = 0; i < fNpoints; i++) {
fY[i] += c1*f->Eval(fX[i], fY[i]);
}
if (gPad) gPad->Modified();
}

////////////////////////////////////////////////////////////////////////////////
/// Apply function f to all the data points
/// f may be a 1-D function TF1 or 2-d function TF2
Expand Down Expand Up @@ -2094,14 +2111,14 @@ void TGraph::SaveAs(const char *filename, Option_t *option) const
}
if (InheritsFrom(TGraphErrors::Class()) ) {
if(opt.Contains("title"))
out << "# " << GetXaxis()->GetTitle() << del << "ex" << del << GetYaxis()->GetTitle() << del << "ey" << std::endl;
out << "# " << GetXaxis()->GetTitle() << "\tex\t" << GetYaxis()->GetTitle() << "\tey" << std::endl;
double *ex = this->GetEX();
double *ey = this->GetEY();
for(int i=0 ; i<fNpoints ; i++)
out << fX[i] << del << (ex?ex[i]:0) << del << fY[i] << del << (ey?ey[i]:0) << std::endl;
} else if (InheritsFrom(TGraphAsymmErrors::Class()) || InheritsFrom(TGraphBentErrors::Class())) {
if(opt.Contains("title"))
out << "# " << GetXaxis()->GetTitle() << del << "exl" << del << "exh" << del << GetYaxis()->GetTitle() << del << "eyl" << del << "eyh" << std::endl;
out << "# " << GetXaxis()->GetTitle() << "\texl\t" << "\texh\t" << GetYaxis()->GetTitle() << "\teyl" << "\teyh" << std::endl;
double *exl = this->GetEXlow();
double *exh = this->GetEXhigh();
double *eyl = this->GetEYlow();
Expand All @@ -2110,7 +2127,7 @@ void TGraph::SaveAs(const char *filename, Option_t *option) const
out << fX[i] << del << (exl?exl[i]:0) << del << (exh?exh[i]:0) << del << fY[i] << del << (eyl?eyl[i]:0) << del << (eyh?eyh[i]:0) << std::endl;
} else {
if(opt.Contains("title"))
out << "# " << GetXaxis()->GetTitle() << del << GetYaxis()->GetTitle() << std::endl;
out << "# " << GetXaxis()->GetTitle() << "\t" << GetYaxis()->GetTitle() << std::endl;
for (int i=0 ; i<fNpoints ; i++)
out << fX[i] << del << fY[i] << std::endl;
}
Expand Down

0 comments on commit 40221a1

Please sign in to comment.