How to calculate slope ans the intercept of linear

Dear Rooters,

Can you Please help me with these.
the attached file contain some data of x and y, How can i draw it in linear and calculate the line slope and intercept,

Thanks in advanced

root.txt (74 Bytes)

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

thank you very much for youe quick help… I Appreciate this
Can you please help to make it in macro.C!!

Double_t MyMacro()
   TGraph *MyGraph = new TGraph("root.txt");
   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");
   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 [0] .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 [0] .L MyMacro.C
root [1] Double_t slope, intercept;
root [2] MyMacro(slope, intercept);

Minimizer is Linear
Chi2                      =      23280.1
NDf                       =            4
p0                        =     -105.084   +/-   71.7279
p1                        =      1.14967   +/-   0.0130304
root [3] slope
root [4] intercept
root [5]

attached my Code for analyzing my data,

bool GetSlope(double &slope, double &intercept)
   TGraph *graph = new TGraph("root.txt");
   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";
}"Energy vs Counts.txt");"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.