Tree and histogram read data from .txt file

Hi!

I began to use root this week. I have a list of 10000 data (I saved it in txt file) so I want to make a histogram but I don´t know how read data. I tried with the example:

#include “Riostream.h"
void basic() {
// Read data from an ascii file and create a root file with an histogram and an ntuple.
// see a variant of this macro in basic2.C
//Author: Rene Brun
// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
TString dir = gSystem->UnixPathName(“dat.txt”);
dir.ReplaceAll(“basic.C”,”");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sbasic.dat",dir.Data()));

Float_t x,y,z;
Int_t nlines = 0;
TFile *f = new TFile(“basic.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“x distribution”,100,-4,4);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y:z”);

while (1) {
in >> x ;
if (!in.good()) break;
//if (nlines < 5) printf(“x=%8f, y=%8f, z=%8f\n”,x,y,z);
h1->Fill(x);
//ntuple->Fill(x,y,z);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();
}

I made some changes but it doesn`t work:

Error: Missing one of ‘,;=():’ expected at or after line 2.
Error: Unexpected end of file (G__fgetstream():2) basic.cc:36:

Could some one help me please?

I use ubuntu and sorry for my english but I try.

Attach your “data txt” file here (let’s say create a small file with the first 100 entries).

datos.txt (992 Bytes)

.
basic.C (1.65 KB)

Thanks!! I really appreciate your help.

Can you help and me please, I need your email, i don t know how attach files?

When you reply / edit / write a message then there is an icon (a horizontal bar with an arrow pointing up in the middle) named “Upload” (click it and you will be able to “add an image or a file”).

New users were not allowed to add attachments, that was the forum’s default. I have fixed that now, please try again, @sds.

Axel

Now, I have one probelm, I will send you my link from ROOT tutorial: http://www.phy.pmf.unizg.hr/~bosnar/pfec/uvod-root-mion.pdf

I want to make a histogram but I don´t know how read data from these tutorial, I have numbers:

  1719
  1013
  2632
  1350
  1177
  1957
  3561
   766
  1289
  1433
  2639
   708
   327
  2907
   826
    34
   768
   258
  2038
  2599 
  3146
   880
   505
  3231
   150
  1306
  1765
   745
  1297
  1326
   273
   275
  2659
  1163
   342
   532
  1419
    46
   259
  1109
  2149
   452
  2371
   110
   974
  1130
   563
   250
   500
  2255
   973
  1308
  1069
   142
   199
   218
  1435
    78
  3659
  1501
   977
   377
   121
  2337
   664
  2703
  1893
  2415
  2159
  1950
  2091
   472
   221
   145
   747
  1192
  1065
   212
   173
  1468
  3525
  1484
   682
   583
   715
   304
  1353
  1474
  3569
  2012
   202
   335
  1976
  1191
  3071
  1465
   693
    64
  1189
  3474
  1347
   137
   558
  2546
   440
   392
  3402
   416
   378
  1563
   332
   280
   875
   560
   973
  1090
  1571
   495
  1416
  1824
   227
  2283
   265
  3254
   883
  1146
  2520
  2364
   411
   876
   894
  1480
  2507
  1040
   680
   674
   230
  2649
   480
  2230
  1171
   261
  1293
   288
  1710
   241
   320
   161
    76
  3283
  1067
   193
   435
  2126
   413
   1474
  3569
  2012
   202
   335
  1976
  1191
  3071
  1465
   693
    64
  1189
  3474
  1347
   137
   558
  2546
   440
   392
  3402
   416
   378
  1563
   332
   280
   875
   560
   973
  1090
  1571
   495
  1416
  1824
   227
  2283
   265
  3254
   883
  1146
  2520
  2364
   411
   876
   100
  3601
  1367
   158
  1975
   217
  1755
    84
   501
    59
   129
   154
   881
  3720
  1383
   419
   512
  2055
   217
  2645
  3204
  1939
   214
  3733
  3138
  3011
    84
  2139
   946
  2387
  1048
  3107
   115
   463
  1844
   213
  1952
  1637
  1225
  1128
  1232
   463
  1194
  1347
   224
  1475
  2012
  1976
  1094
  3469
  2818
  1338
   129
  1319
  1384
  1915
   159
  1491
   279
  2912
   606
    86
  1570
   197
   318
   117
   981
   675
   259
   569
  3046
   840
  1958
   597
   353
  3267
   923
   865
   922
  1546
  3423
   541
  2483
    66
  2197
   219
  2942
   156
  1782
  2109
  2611
    68
  1973
    60
   484
  1589
  1765
  1950
  2003
    71
  2290
   795
  3269
   146
  3281
   712
   949
  1306
  1141
  1544
  1825
   742
   272
   249
  1508
   697
  2299
   373
    99
  1486
    96
  1128
   627
   821
   547
  2308
  2954
  1616
   364
  3515
  1120
  1502
    73
   260
   257
   612
   505
   627
   536
  1152
  2759
  2059
   126
   961
   477
   363
  2102
  1292
   527
   340
   312
   872
  1405
  1103
  2218
  1621
   382
  1159
  3367
   613
   795
  1267
   168
   308
    41
  1321
   226
  1172
   964
   802
   236
  1642
  2591
   302
  1473
  1444
   663
  1628
   311
  1569
  1585
  2532
   973
  1785
   451
   384
   571
  1606
   770
  1062
  3177
  2459
  1381
  1231
   155
  2236
  1810
   291
  1026
  2746
  3298
  1028
  1250
  2148
   492
   996
    28
   785
  1992
   586
  1871
   334
   258
  1774
  1599
   141
    97
   456
    93
   487
    31
   956
   419
  2446
    91
   314
   404
  2456
   350
   783
  1037
   352
   289
   948
   438
  1720
  1488
  1411
  2537
   474
   344
   164
  1273
   140
  1223
  3548
   617
  1408
  1437
   878
    52
   718
  1883
   114
  2447
  1719
  1013
  2632
  1350
  1177
  1957
  3561
   766
  1289
  1433
  2639
   708
   327
  2907
   826
    34
   768
   258
  2038
  2599
  2604
  2785
   748
  1120
  1316
   139
   497
   188
   327
  1707
  3078
   274
   133
   481
  2346
  2728
  1710
  1097
    40
   162
    60
    33
  1351
   102
   242
  2994
  3252
  1695
   192
   119
   472
  2722
  2208
   375
   535
   120
   990
  2675
  1077
   268
  2444
   324
  1053
   631
  1973
   618
   961
  1815
  1754
   113
   285
   264
   229
   728
  1587
  2219
  1380
  1145
   116
   979
  2169
  1502
   138
   214
  1053
  1003
  1613
  2120
    30
  1429
  2375
  3134
   260
  2012
  2050
  1696
  3032
  2243
   131
  1559
  1017
  1258
   454
  2326
   227
  1159
  1251
  1232
  1458
  3732
  1908
  1510
  1066
  2284
   461
   221
  1018
   175
  1139
   452
   683
  1841
   550
   251
  2534
  1424
   660
   399
  1697
  1111
   467
  1003
  1903
   902
  1095
   180
   117
  2119
  2305
  1670
  1991
   600
  1122
   619
   655
  1061
   134
   348
   746
   775
  1708
   784
   865
  1530
  3146
   880
   505
  3231
   150
  1306
  1765
   745
  1297
  1326
   273
   275
  2659
  1163
   342
   532
  1419
    46
   259
  1109
  2149
   452
  2371
  1335
  1128
  1934
  3649
  2045
   200
   184
   478
  2198
   227
  1500
  1855
  3705
  2623
   631
   231
   519
  1824
  1591
  1350
  1327
    91
   879
   781
  2644
    57
   588
   606
  1127
   168
   690
  1271
   277
   769
  1579
   832
   270
   839
   265
   364
  1153
   790
   238
   169
  1251
  3253
   652
  1000
  1309
    95
   142
   413
  1098
  1029
   428
   113
    53
   281
  1288
   125
    59
   502
   223
  1298
  2886
  1361
   558
    40
   314
  3240
  1668
   209
  2026
   779
   734
  1685
   195
  1114
  3775
   301
  1784
   631
  2394
  1758
  1321
   943
  2662
   611
  2280
  2034
  1677
  1274
  3762
   890
  1329
  3054
  2444
    99
  1503
   641
  1504
  1141
   478
   325
   228
   310
   765
  1415
   597
    39
  2178
   702
   881
   320
  2138
   319
  1098
  1563
  3245
   130
  2404
   950
  1264
  1779
  1177
  1550
    77
  1577
   317
  1340
  3651
   669
   750
  3252
   793
   208
  1396
  1484
   111
  1288
  1955
  1027
   463
  1872
  1347
  1599
   142
   369
  1046
   514
   772
  1283
  2037
  2410
   240
  1747
   998
  1706
   923
  3736
    50
   419
   323
  1271
  1090
   953
   551
    82
   267
  2215
   613
   305
  1623
   551
   181
  1477
   319
  2292
  2288
   530
  1651
   540
  1792
  2279
   328
  3260
    30
   622
  2819
   548
  2442
  3335
  1614
  1542
    47
  1403
   796
   280
  1266
  1647
  1234
  1009
   541
  3412
    56
   557
   333
   971
  1437
   432
  3599
  1494
  1326
  2534
   438
  1448
   783
   874
   128
   335
   721
  1153
   449
  1051
  2240
   946
   508
  2954
  2043
   675
  3156
  1363
  1128
   310
  2331
   461
   222
  1413
  2060
  3523
    32
   322
   448
  1613
  1361
   394
   195
  3616
  1326
   682
  1328
  1090
   589
  3379
    96
   183
   451
  2307
   663
   366
   215
   210
   202
  1565
   329
   231
  1109
   757
   682
   985
  1113
  2411
  1010
  1174
    26
   492
   437
   721
  2335
   173
  2126
  1206
   311
   913
   309
   456
   472
  3366
  1273
   109
  3017
  1432
  1833
   155
  2013
  1361
  1269
  2029
   198
    66
   191
   618
  1466
   362
  1266
   512
  1177
  1955
  2036
  2234
  2703
  1200
   132
  2689
  2746
   709
   735
  2113
  2486
  3144
  1221
   298
  2257
   539
   188
  1599
  1752
   173
   446
  2519
  2044
    82
  2368
  3181
  2095
   550
   314
   833
  1179
   223
    85
   957
  1357
  1522
  2004
  1390
  3594
   146
  1881
  1108
  1810
  1201
   996
   880
  3066
  1721
  1408
  2011
    73
  1569
  1747
  3507
  1166
  2557
    50
   491
   550
  2293
  3273
   369
  3725
  2193
  1825
  2090
  2895
  1262
  1553
   339
   372
  1836
    78
  1162
  1055
  1111
  1534
   303
   425
   122
  1368
  2187
   257
  3118
  1504
  3167
  3165
   688
   735
  1802
  1026
  1416
  3106
   525
  2031
  1369
  2970
  1272
  2392
   747
  1189
  3474
  1347
   137
   558
  2546
   440
   392
  3402
   416
   378
  1563
   332
   280
   875
   560
   973
  1090
  1571
   495
  1416
  1824
   227
  2283
   265
  3254
   883
  1146
  2520
  2364
   411
   876
   521
  1616
   414
  1043
  1227
  3334
   155
   350
  3212
   933
  2761
   476
  1663
  2277
   479
  3396
   721
  1254
   347
  1078
   215
   190
  2483
  1044
  1982
  2505
    97
   198
  3234
   637
  1741
    95
   276
    30
   745
   867
  2712
  2069
  1108
   174
  1789
   164
  2537
   187
   115
    47
  2515
  2946
  2572
  2649
  1755
   439
   567
  3100
  2041
   287
   455
   828
   816
   137
    74
  2716
  1746
   765
   314
  2154
  1058
   714
  1557
  1407
  2459
  2275
  2663
  1132
   344
    41
   871
   254
  1071
   418
  1688
   422
   826
  1292
   208
  2691
   553
   360
   470
  2351
   712
  1194
   727
  3560
  3591
   150
  1101
  1697
  3353
   545
  3054
  1904
   628
    83
  1474
  3569
  2012
   202
   335
  1976
  1191
  3071
  1465
   693
    64
  1189
  3474
  1347
   137
   558
  2546
   440
   392
  3402
   416
   378
  1563
   332
   280
   875
   560
   973
  1090
  1571
   495
  1416
  1824
   227
  2283
   265
  3254
   883
  1146
  2520
  2364
   411
   876
  3241
   995
  1737
  1516
  1920
   410
   618
   891
    64
   134
   617
  2938
   351
  1071
  3346
   738
   255
    79
   988
  1308
   940
   492
  2029
  3482
   684
   651
  3098
  2269
   134
  1362
    94
  2190
   308
  1903
  2463
   315
  1324
   626
   592
   109
   488
  2558
   978
   954
  1975
  2961
  1800
  3645
  1023
   257
   651
  1948
   278
  1325
   174
   789
   493
  2108
   872
  2563
  1565
   249
   157
  3146
   645
   120
   876
  1225
  1694
   425
   629
  2160
  1700
    49
   623
  1791
   708
  3769
   144
  2154
   430
  1874
   292
  1141
   533
  1101
  3411
   396
  2468
   768
  2853
  1248
  1811
   285
   849
  2866
  3248
   144
   915
  1613
  1232
   437
   890
    90
   492
   842
    53
  2762
  2320
  2770
  3629
   162
   265
   220
  1474
  3569
  2012
   202
   335
  1976
  1191
  3071
  1465
   693
    64
   30
  1429
  2375
  3134
   260
  2012
  2050
  1696
  3032
  2243
   131
  1559
  1017
  1258
   454
  2326
   227
  1159
  1251
  1232
  1458
  3732
  1908
  1510
  1066
  2284
   461
  394
   195
  3616
  1326
   682

Can you help me? Thank you.

Save your numbers into a text file. For instance mydata.txt and then run the macro Wile posted earlier:

root [0] .x basic.C("mydata.txt", "mydata.root");

Can you write whole code for it?

I have this, but I don t know:

void primjer(){
TCanvas *time = new TCanvas("c1","mion" ,100,130,1000,600);
time ->Divide(1,1);
time ->cd(1);
TH1F *h1 = new TH1F("h1","tdc kanal" ,4096,0,4096);
h1->Draw();
ifstream in;
Float_t t;
Int_t nlines= 0;
in.open("mion.root", ios::in);
while (1) {
in >> t;
if (!in.good()) break;
h1->Fill(t);
nlines++;
}
in.close();
TH1F *h2 = h1->Rebin(40,"h2");

TF1 fit1("fit1","[0]*TMath::Exp(-x/[1])");
fit1.SetParameter(1,2);
h2 -> Fit("fit1","RLE","" ,100,3500);
time ->cd(2);
h2->Draw();
int nbins = h2->GetXaxis()->GetNbins();
TH1F *h3= new TH1F("h3","vrijeme",nbins ,0,5);
for (int i=1;i<=nbins;i++)
{
double y = h2->GetBinContent(i);
double x = h2->GetXaxis()->GetBinCenter(i);
double xnew = 0.00125*x;
h3->Fill(xnew,y);
}
h3 -> Fit("fit1","RLE","" ,0.2,4.8);
time ->cd(1);
h3->Draw();



}

primjer.C (918 Bytes)
mion.txt (9.4 KB)

Thank you very much. I would like to ask if you get some graphic that you send these graphic me in JPG … thanks again.

If you can of course.

1 Like

I sent question on your email, and I would just to ask can you fitting this graphics… with linear fit for example…

I should get these valuables, for example: Entries, Means, RMS, ndf, Prob, p0, p1 and p2…

Po is No, p1 iz muon lifetime and p2 is constant b…

Can you fit this and order parametres… I urgently need these valables… Thank you very much?

To see the fit parameters on the plots add the following line in your macro:

gStyle->SetOptFit(111111)

I was not able to get these graphics, and therefore I can not see the parameters.

Please, if you have the free time, just send me a screenshot or picture from these values.

Endless thanks and thank you on the added line.

Just execute the macro I posted before and add in it the gStyle line I sent you … Are you able to execute the macro ?

No, No, I can not able to execute macro, so I wanted to ask you to do if you can.

Indeed, just can not get these prints you sent me, and that was an urgent need to have the value of this parameter fit.

If you are able to do so, endless thanks.

That’s a weird request …
Will you ask the forum to execute your macro each you need to do a new plot ? …
Ok I do it once but in the future you should find a way to execute your macros yourself…
You simply cannot work this way …

you simply need to enter root by typing root at the shell prompt and then .x yourmacro.C at the root prompt.