TTree add extra zero values while creating tree

Hello,

I am trying to create a TTree from .data file. In the created tree it shows a peak for zero value but I do not have that much zero values in my .data file. Also when I tries to create a Graph from TTree its showing a peak on zero (I know its a peak from that zero values in TTree).

My .data file contains the data as follows:
0.00000E+000 0.00000E+000
9.17901E-002 0.00000E+000
1.67826E+000 0.00000E+000
6.01173E+000 0.00000E+000
1.33240E+001 0.00000E+000
2.35291E+001 0.00000E+000
3.55956E+001 0.00000E+000
4.56842E+001 0.00000E+000
5.84960E+001 0.00000E+000
7.44566E+001 0.00000E+000
9.27239E+001 1.76700E+003
1.37519E+002 5.03136E+004
1.65527E+002 7.21957E+004
1.97128E+002 9.13714E+004
2.32802E+002 1.08158E+005
2.73069E+002 1.23016E+005
3.20092E+002 1.35843E+005
3.72603E+002 1.47928E+005
4.29188E+002 1.58354E+005
4.89731E+002 1.66193E+005
5.52941E+002 1.74687E+005
6.21189E+002 1.81530E+005
6.93685E+002 1.88233E+005
7.70277E+002 1.93716E+005
8.50568E+002 1.98671E+005
9.32825E+002 2.03386E+005
1.02123E+003 2.07703E+005

My code is as below:
data.h
#include “TROOT.h”
#include

class data {
public:
float Time,S0;
};

main.c

#include “data.h”

data d;

main()
{
treew();
drawGraph();
}

void treew() {
FILE *fp = fopen(“data.data”,“r”);
char line[81];

TFile *f = new TFile(“ABC.root”,“RECREATE”);

TTree *tree = new TTree(“T”,“Data from ascii file”);

tree->Branch(“Time”,&d.Time,“Time/F”);
tree->Branch(“S0”,&d.S0,“S0/F”);
// fill the tree from the values in ASCII file
while (fgets(&line,80,fp)) {
sscanf(&line[0],"%f%f",&d.Time,&d.S0);
tree->Fill();
}
// check what the tree looks like
tree->Print();
fclose(fp);
f->Write();
}

void drawGraph()
{
TGraph *gr1;

 TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
 dir.ReplaceAll("main.c","");
 dir.ReplaceAll("/./","/");
  
     TFile *f1 = TFile::Open("ABC.root");
     TTree *T1 = (TTree*)f1->Get("T");
TCanvas *c = new TCanvas("Tree", "Tree", 0, 0, 1000, 1000);
        
     T1->Draw("S0:Time", "", "same");  
     gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());  
     gr1->Sort();  
     gr1->SetLineColor(55);
  
 TMultiGraph *mg = new TMultiGraph();
     mg -> Add(gr1);
     mg -> Draw("AC");
  rootFile->Close(); 

}

The t and s1 values appears in TTree and Graph as shown in the file attached here with.

Please guide me how to overcome with this error OR what further modifications to be needed.

Thank you.




Hi,

the code you posted is invalid C++ (did you try to compile your code?). When reading the data file you call

char line[81];
fgets(&line,80,fp);

&line is a pointer to a char[81], not the char* fgets expects. Since arrays
automatically decay to pointers you could

fgets(line,80,fp);

A much simpler way would be to call TTree::ReadFile

tree->ReadFile("data.data");

This shouldn’t only safe you some lines of code, but also the potential for
bugs in your code.

HTH,

Benjamin

Thanks for a reply. Sorry, while copying a code I did a mistek. My updated code for treew() is as follows:

void treew() {
FILE *fp = fopen(“data.data”,“r”);
char line[81];

TFile *f = new TFile(“ABC.root”,“RECREATE”);

TTree *tree = new TTree(“T”,“Data from ascii file”);

tree->Branch(“Time”,&d.Time,“Time/F”);
tree->Branch(“S0”,&d.S0,“S0/F”);
// fill the tree from the values in ASCII file
while (fgets(&line[0],80,fp)) {
sscanf(&line[0],"%f%f",&d.Time,&d.S0);
tree->Fill();
}
// check what the tree looks like
tree->Print();
fclose(fp);
f->Write();
}

But I still did not found a solution to my problem.

Hi,

what is the actual data you use? The sample you posted has 27 records while your histograms mention 82 entries.

My actual data file is attached here with.

Hello,

As you see in data.data file there are 42 records but for drawing a graph its taking 82 entries. I think this is because of that peak values at zero. Do you have any idea to overcome with this error?

Thank you.

Hi,

I cannot see it.

Sorry. I was facing some problem while attaching a file. My data.data file contains the following data:

0.00000E+000 0.00000E+000
9.17901E-002 0.00000E+000
1.67826E+000 0.00000E+000
6.01173E+000 0.00000E+000
1.33240E+001 0.00000E+000
2.35291E+001 0.00000E+000
3.55956E+001 0.00000E+000
4.56842E+001 0.00000E+000
5.84960E+001 0.00000E+000
7.44566E+001 0.00000E+000
9.27239E+001 1.76700E+003
1.13996E+002 2.55468E+004
1.37519E+002 5.03136E+004
1.65527E+002 7.21957E+004
1.97128E+002 9.13714E+004
2.32802E+002 1.08158E+005
2.73069E+002 1.23016E+005
3.20092E+002 1.35843E+005
3.72603E+002 1.47928E+005
4.29188E+002 1.58354E+005
4.89731E+002 1.66193E+005
5.52941E+002 1.74687E+005
6.21189E+002 1.81530E+005
6.93685E+002 1.88233E+005
7.70277E+002 1.93716E+005
8.50568E+002 1.98671E+005
9.32825E+002 2.03386E+005
1.02123E+003 2.07703E+005
1.11369E+003 2.11648E+005
1.21024E+003 2.14760E+005
1.31062E+003 2.18587E+005
1.41348E+003 2.21255E+005
1.52181E+003 2.24274E+005
1.63422E+003 2.26832E+005
1.75069E+003 2.29445E+005
1.87124E+003 2.31670E+005
1.99569E+003 2.33955E+005
2.12121E+003 2.35802E+005
2.25313E+003 2.37735E+005
2.38953E+003 2.39557E+005
2.53002E+003 2.41274E+005
2.86383E+003 2.41606E+005

Sorry,

I cannot reproduce this.

Here are some plots from the data, I zoomed on the smaller x-values and what I see in the histograms matches what I see in your data.






Thanks. Oh… I do not know why you can not execute. But its working for me. You have zoomed on x-values. How? I mean in graph that peak on zero value is not appearing, and that’s the solution which I am looking for.

Would you please explain me how you did this?

Thank you.

Hello,

I have checked by zoom in x and y values. It is working for first 25 values only from .data file and if we take all the values then it is showing a peak at zero. Otherwise for first 25 values it is not showing a peak at zero in Graph.

Thanks.

Hi,

Were you able to solve this problem? If not, could you please upload all the files needed to reproduce the problem (for example it ‘seems’ that in text above you mentioned something about it not working for more than 25 values but the example contains roughly that many). Also did you try use TTree::ReadFile instead of your own parsing?

Cheers,
Philippe.