Analysis of long Double data with TTree?

Hello,

I have a problem while analysis of long Double data type with TTree. I want to analyse following data as:

Time_(s) s0
0.00000E+000 0.00000E+000
5.09966E-002 0.00000E+000
6.62959E-001 0.00000E+000
2.09959E+000 0.00000E+000
4.38372E+000 0.00000E+000
7.62890E+000 0.00000E+000
1.17973E+001 0.00000E+000
1.69840E+001 0.00000E+000
2.31787E+001 0.00000E+000
3.04986E+001 0.00000E+000
3.90199E+001 1.04312E+003
4.88280E+001 6.56040E+003
5.99976E+001 1.70816E+004
7.26286E+001 2.81410E+004

For this my code is as follows:

void Analyse() {
  
//DecIaration of Ieaves types
long double time, s0;
   
   FILE *fp = fopen("C:/Analyse.data", "r");
   char line[128];
   //create the file, the Tree and a few branches
   TFile *rootf = new TFile("Analyse.root", "RECREATE");
   
   TTree *tree = new TTree("tree","Tree");	
   
   tree->Branch("time",&time,"time/D");
   tree->Branch("s0",&s0,"s0/D");       
            
    while (fgets(&line[0],127,fp)) {
      sscanf(&line[0],"%lf%lf",&zeit,&s0);    
        tree->Fill();
   }
  tree->Print(); 
  tree->Write();
  fclose(fp);
  rootf->Write();
  //Analysis
   TFile *f1 = TFile::Open("Analyse.root");
   //read a tree from a root file
   TTree *T1 = (TTree*)f1->Get("tree");
    TCanvas *c = new TCanvas("c", "c", 0, 0, 1000, 1000);
    T1->Draw("s0:time");
     gPad->Modified(); 
     gPad->Update();
     TGraph *gr = (TGraph *)gPad->GetPrimitive("Graph")->Clone();
     TGraph *gr = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
     gr->Draw("AC");      [color=#FF0000]//Here I am not getting a proper curve as I expected for analysis. I want a proper Curve for analysis and for fitting a function[/color]
     gr->SetMarkerStyle(2);
     gr->SetLineColor(16);
}

what is going wrong? Is there other way to analyse long double data type? Please guide me.

You do not “Fit” anything so there is no “fitting function” which can be drawn.
See also TGraphPainter and TGraph::Fit.

Hello,

Thanks. I want to fit a function afterwards. But currently I am not getting a proper graph as I expected.
I wants to fit the following function:

TF1 *f = new TF1(“f”, “exp([0]+[1]*x)”, -1,8);
gr->Fit(f, “R”);

Here I am attaching .pdf file of a graph plot which I am gtting with the above code. A graph plot itself is not as I want.
Analyse.pdf (25.5 KB)

[code]#include “TFile.h”
#include “TTree.h”
#include “TCanvas.h”
#include “TPad.h”
#include “TGraph.h”

#include

void Analyse() {
//DecIaration of Ieaves types
Double_t time, s0;

//create the file, the Tree and a few branches
TFile *rootf = new TFile(“Analyse.root”, “RECREATE”);

TTree *tree = new TTree(“tree”,“Tree”);

tree->Branch(“time”,&time,“time/D”);
tree->Branch(“s0”,&s0,“s0/D”);

FILE *fp = fopen(“C:/Analyse.data”, “r”);
char line[128];
while (fgets(&line[0],127,fp)) {
sscanf(&line[0],"%lf%lf",&time,&s0);
tree->Fill();
}
fclose(fp);

tree->Print();
tree->Write();
delete rootf;

//Analysis
TCanvas *c = new TCanvas(“c”, “c”, 0, 0, 1000, 1000);
c->Divide(1, 2);

TFile *f1 = TFile::Open(“Analyse.root”);
//read a tree from a root file
TTree *T1; f1->GetObject(“tree”, T1);

c->cd(1);
T1->SetMarkerStyle(20);
T1->Draw(“s0:time”);
gPad->Modified(); gPad->Update();

c->cd(2);
// TGraph *gr = (TGraph *)gPad->GetPrimitive(“Graph”)->Clone();
TGraph *gr = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
gr->SetMarkerStyle(20);
gr->SetLineColor(16);
// “expo” = “exp([0]+[1]*x)” http://root.cern.ch/root/html/TFormula.html
gr->Fit(“expo”);
gr->Draw(“AP”);
gPad->Modified(); gPad->Update();
}[/code]

Hello,

Thanks for the guidance. It works. But I don’t want to use, gr->SetMarkerStyle(20); and gr->Draw(“AP”); Instead I want gr->Draw(“AC”); Means a axis with a smoot curve. When I use “AC” option then I got the output as file attached here with.
Any soluction to get a smooth curve ?

Why don’t you simply change … gr->Draw(“AP”); … into … gr->Draw(“AC”); … and see what happens?

For representing a result in the form of a Curve as per requirment. So I want to use “AC” option instead “AP”.

Note that with AC and AL points are connected together in the order they appear in the X vector. So if the X vector is not sorted you will get something similar to the plot you posted. Also be careful wih th C option it might add some artefacts when the points are random, far to each other, or with a big gradient between them. Have a first try with L instead of C.

Do not hesitate, use it.

Hello,

Yes, you are right. Its an experimental data to be analysed. Some of the data changes with a big gradient between them. I tried with temporary deleting those records then with AC it works. But as I want to analyse a data I have to use “AC” option. I tried with “AL” option but I gets the output as a file attached here with, which is not relevant.

You said if the X Vector is not sorted. Could you explain in detail? I understand, I need to sort my values which are representing on X axis. But I can not do so because, its an experimental data and need to be get ploted as it is e.g: time Vs dataValues. Could you further guide me to overcome with these problem?

Thank you in advance.

I do not see the file… And please USE AL … you will get problem with AC

So do not use neither AC or AL … you will get grap plots impossible to read.
use AP as stated before.

[quote=“Pree”]You said if the X Vector is not sorted. Could you explain in detail? I understand, I need to sort my values which are representing on X axis. But I can not do so because, its an experimental data and need to be get ploted as it is e.g: time Vs dataValues. Could you further guide me to overcome with these problem?
[/quote]

If you need data sorted you have to sort it.
There is a nice std::sort function, which will do a work for you. You only have to think how to organize a data in a such way, that you sort not individual x-values, but (x,y) pairs, using x as a criteria.
This will require at least 3 additional buffers (one for pair, two for 2 separate xs/ys arrays ), and it’s quite ugly, but at least it’ll work. If you still need a help, I can give a code sketch.

BTW I think we can add some special hint parameter for TGraph (or additional constructor) to let it know, that it should sort data (though it can be non-trivial sometimes).

Or even the ROOT one: TMath:Sort()

There exists TGraph::Sort.
Simply try to add “gr->Sort();”, before you “Fit” and/or “Draw” it.

Yes true :slight_smile: I forgot this one :slight_smile:

Hello All,

Thanks a lot for a guidance. Yes, it works after using gr->Sort();