// This macro demonstrates how to use combination of functions (TF1) if the // functions are defined as a normal C++ functions. // The components Osz and Pol are constructed with a global pointer // pointing to it. // these pointers are used in the Sumf // // Author: Otto.Schaile@LMU.DE #include "TCanvas.h" #include "TF1.h" #include "TMath.h" #include "Riostream.h" TF1 * wave = 0; // global pointers to be used in sum functions TF1 * line = 0; Double_t Osz(Double_t *x, Double_t *par) { Double_t amplitude = par[0]; // use meaningful names Double_t omega = par[1]; // to improve readability Double_t phase= par[2]; Double_t xa = x[0]; Double_t pi = TMath::Pi(); return amplitude * TMath::Cos(pi * omega * xa + phase); }; //____________________________________________________________________ Double_t Pol(Double_t *x, Double_t *par) { Double_t constant = par[0]; // use meaningful names Double_t slope = par[1]; // to improve readability Double_t xa = x[0]; return (constant + slope * xa); }; //____________________________________________________________________ Double_t SumF (Double_t *x, Double_t *par) { Double_t ca = x[0]; return (wave->Eval(ca) + line->Eval(ca)); } //____________________________________________________________________ void drawit(){ // TCanvas * c1 = new TCanvas("csum","Sum of functions", // 800,50,700,400); // use 100, 100 for icons (gif) TCanvas * c1 = new TCanvas("csum","Sum of functions", 800,50,100,100); c1->SetGridx(); c1->SetGridy(); c1->GetFrame()->SetLineWidth(4); c1->SetBottomMargin(0.2); // define range Double_t from = 0; Double_t to = 4; Double_t amplitude = 4; Double_t omega = 1; Double_t phase = 0; // Double_t phase = 0.25 * TMath::Pi(); wave = new TF1("wave",Osz,from,to, 3); wave->SetParameter(0, amplitude); wave->SetParameter(1, omega); wave->SetParameter(2, phase); Double_t constant = 0; Double_t slope = 3; line = new TF1("ef1",Pol,from,to, 2); line->SetParameter(0, constant); line->SetParameter(1, slope); TF1 * sum = new TF1("sum",SumF,from,to, 3); sum->SetMinimum(-5); sum->SetMaximum(20); sum->Draw(); TAxis *xaxis = sum->GetHistogram()->GetXaxis(); TAxis *yaxis = sum->GetHistogram()->GetYaxis(); yaxis->SetTitle("Amplitude"); yaxis->CenterTitle(); yaxis->SetLabelSize(0.06); yaxis->SetTitleOffset(0.7); yaxis->SetTitleSize(0.06); xaxis->SetTitle("Angle[units of pi]"); xaxis->CenterTitle(); xaxis->SetLabelSize(0.06); xaxis->SetTitleOffset(1); xaxis->SetTitleSize(0.06); sum->SetLineColor(6); sum->SetLineWidth(2); wave->Draw("same"); wave->SetLineColor(2); wave->SetLineWidth(2); line->Draw("same"); line->SetLineColor(4); line->SetLineWidth(2); c1->Modified(kTRUE); };