Problem with TVectorF

Hello all.

I am new in ROOT, so my question probably would be simple.

So, I am reading ASCII file and then write values to the vector. After
that I want to fill histogram with values of vector.

[code] ReadFile(fp,amps,time,number); // This is my own function which
fills arrays amps,time and
number

for(int i=0;i<lines;i++) printf(“amps[%d]=%e time[%d]=%f\n”,i,amps[i],i,time[i]); // This output is for testing

TVectorF *Vamps = new TVectorF(lines,amps);
TVectorF *Vtime = new TVectorF(lines,time);[/code]

``````The printf() testing gives me following:
amps[0]=-1.490000e-14 time[0]=0.000000
amps[1]=-1.430000e-14 time[1]=0.227539
amps[2]=9.283000e-13 time[2]=6.564453
amps[3]=2.680000e-13 time[3]=7.749023
amps[4]=5.740000e-14 time[4]=8.921875
amps[5]=9.200000e-15 time[5]=10.083008
amps[6]=1.400000e-15 time[6]=11.177734
amps[7]=0.000000e+00 time[7]=12.370117
amps[8]=7.000000e-16 time[8]=13.485352

Then line:

[code] Vamps->Print()[/code]

gives following:

Vector (9)  is as follows

|        1  |
``````

0 |-1.49e-14
1 |-1.43e-14
2 |9.283e-13
3 |2.68e-13
4 |5.74e-14
5 |9.2e-15
6 |1.4e-15
7 |0
8 |7e-16

``````and that's fine.

But when I want to acsess data in vector it gives something else.
``````

For example code:

``` Float_t w; w = Vtime[5]; printf("w=%e\n",w);```

gives

w=1.477402e+08

Any ideas?
Thanks.

Hard to say , please post a running example

this is complete file show.c

``````#include <stdio.h>
#include <stdlib.h>

int NumberOfLines(FILE *f) // determines number of lines in file
{
int i=0;
char str[100];
while(!feof(f))
{
fgets(str,100,f);
i++;
}
return i;
}
//---------------------------------------------------------------------
void ReadFile(FILE* file,float *amps,float* time, float* number)
{
struct data_t{

Float_t amps;
Float_t time;
Float_t number;

};
data_t data;

char line[512];
int lines = 0;

while(!feof(file))
{
fgets(line, sizeof(line), file);

sscanf(line,"%g,%g,%g",
&data.amps,
&data.time,
&data.number);

amps[lines] = data.amps;
time[lines] = data.time;
number[lines] = data.number;
lines++;

}

float start=time[0];
for(int j=0;j<lines;j++)
time[j]=time[j]-start;

}
//---------------------------------------------------------------------
TH1F* FileShow(char *file)
{
FILE *fp = fopen(file,"r");

char line[512];

int lines = NumberOfLines(fp);
float *amps = new float[lines];
float *time = new float[lines];
float *number = new float[lines];
rewind(fp);

fclose(fp);
for(int i=0;i<lines;i++) printf("amps[%d]=%e time[%d]=%f\n",i,amps[i],i,time[i]);

TVectorF *Vamps = new TVectorF(lines,amps);
TVectorF *Vtime = new TVectorF(lines,time);

float nBins = 1000;
Axis_t x=0;
Float_t w;
float delta=(Vtime->Max()-Vtime->Min())/nBins;
printf("Time_Min = %f,Time_Max = %f\n",Vtime->Min(),Vtime->Max());
printf("Amps_Min = %e,Amps_Max = %e\n",Vamps->Min(),Vamps->Max());

Vamps->Print();
Vtime->Print();
TH1F *hist = new TH1F("hist","current histogram",nBins,Vtime->Min(),Vtime->Max());
for(int i=0;i<lines;i++)
{
x = Vtime->Min()+(2*i+1)*delta/2;
w = Vtime[5];
printf("w=%e\n",w);
hist->Fill(x,w);
}
return hist;
}
//---------------------------------------------------------------------

void show(char* file)
{

gROOT->Reset();

TCanvas *c1 = new TCanvas("c1","current data",600,800);

TH1F *amps1 = new TH1F();
amps1 = FileShow("temp.dat");

amps1->Draw();

}``````

here is output:

[quote]amps[0]=-1.490000e-14 time[0]=0.000000
amps[1]=-1.430000e-14 time[1]=0.227539
amps[2]=9.283000e-13 time[2]=6.564453
amps[3]=2.680000e-13 time[3]=7.749023
amps[4]=5.740000e-14 time[4]=8.921875
amps[5]=9.200000e-15 time[5]=10.083008
amps[6]=1.400000e-15 time[6]=11.177734
amps[7]=0.000000e+00 time[7]=12.370117
amps[8]=7.000000e-16 time[8]=13.485352
Time_Min = 0.000000,Time_Max = 13.485352
Amps_Min = -1.490000e-14,Amps_Max = 9.283000e-13

Vector (9) is as follows

`````` |        1  |
``````

0 |-1.49e-14
1 |-1.43e-14
2 |9.283e-13
3 |2.68e-13
4 |5.74e-14
5 |9.2e-15
6 |1.4e-15
7 |0
8 |7e-16

Vector (9) is as follows

`````` |        1  |
``````

0 |0
1 |0.227539
2 |6.56445
3 |7.74902
4 |8.92188
5 |10.083
6 |11.1777
7 |12.3701
8 |13.4854

Warning in TH1::Build: Replacing existing histogram: hist (Potential memory leak).
w=1.484993e+08
w=1.484993e+08
w=1.484993e+08
w=1.484993e+08
w=1.484993e+08
w=1.484993e+08
w=1.484993e+08
w=1.484993e+08
w=1.484993e+08
[/quote][/quote]

That looks like C++ mistake:

Vtime[5] should be (*Vtime)[5]

True.

Thank you a lot. Everything’s working now.