# How to calculate slope ans the intercept of linear

Dear Rooters,

the attached file contain some data of x and y, How can i draw it in linear and calculate the line slope and intercept,

root.txt (74 Bytes)

``````root  TGraph *MyGraph = new TGraph("root.txt");
root  MyGraph->Draw("A*");
root  MyGraph->Fit("pol1");
root  MyGraph->GetFunction("pol1")->GetParameter(1)
(const Double_t)1.14966507169332940e+000
``````

thank you very much for youe quick help… I Appreciate this

``````Double_t MyMacro()
{
TGraph *MyGraph = new TGraph("root.txt");
MyGraph->Draw("A*");
MyGraph->Fit("pol1");
return MyGraph->GetFunction("pol1")->GetParameter(1);
}
``````
1 Like

Thank you very much, , ,

But Why the output results dose not match with the origin or excel results of sople and the intercept ?

Could you be more precise?

No, it was a mistake, The matching with other peogrammes is 100%. Thanks Alot

Sorry Again
if i will insert your code in another code, how can i delecare a parameters to be the slope and intercept!
For example: I want to make A=slpoe, and B=intercept.
A and B will be used in another code part

``````void MyMacro(Double_t &slope, Double_t &intercept)
{
TGraph *MyGraph = new TGraph("root.txt");
MyGraph->Draw("A*");
MyGraph->Fit("pol1");
slope = MyGraph->GetFunction("pol1")->GetParameter(1);
intercept = MyGraph->GetFunction("pol1")->GetParameter(0);
}
``````

I get this Error:

input_line_9:2:2: error: no matching function for call to ‘MyMacro’
MyMacro() /* invoking function corresponding to ‘.x’ */

/Path/MyMacro.C:1:6: note: candidate function not viable: requires 2 arguments, but 0 were provided
void MyMacro(Double_t &slope, Double_t &intercept)

How did you call the `MyMacro` function?
You should call it that way:

``````Double_t slope, intercept;
MyMacro(slope, intercept);
``````

I call the macro file as follows:

[Baja@h69 /Baja]\$ root -l
root  .x MyMacro.C

as i mention before i will insert this part in another code
Sorry, I don’t understand the way you show me to call the macro

``````C:\Users\bellenot\rootdev>root -l
root  .L MyMacro.C
root  Double_t slope, intercept;
root  MyMacro(slope, intercept);

****************************************
Minimizer is Linear
Chi2                      =      23280.1
NDf                       =            4
p0                        =     -105.084   +/-   71.7279
p1                        =      1.14967   +/-   0.0130304
root  slope
(Double_t)1.14966507169332940e+000
root  intercept
(Double_t)(-1.05083702169989910e+002)
root 
``````

attached my Code for analyzing my data,

``````bool GetSlope(double &slope, double &intercept)
{
TGraph *graph = new TGraph("root.txt");
graph->Fit("pol1");
TF1 *f1 = graph->GetFunction("pol1");
if (f1) {
slope = f1->GetParameter(1);
intercept = f1->GetParameter(0);
return true;
} else {
slope = 0.0;
intercept = 0.0;
return false;
}
}

void h_Integration(TH1F* h, string& filename, double lifetime)
{

...

if (!getSlope(A, B)) {
cerr << "error getting slope from root.txt\n";
return;
}
outputfile.open("Energy vs Counts.txt");
inputfile.open("Channel with Counts.txt");

while (inputfile >> N >> C){
...
``````
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.