Make a TTree from a long file

Hi all,
this is my first message here, so I’m sorry if I’m doing something wrong posting it.

I’m not an expert but I need to make a TTree from an ASCII file full of numerical data.

I did it with a file with 10 columns of data and I used:

while (1){
myfile >> p1 >> p2 >> p3 >> p4 >> p5 >> p6 >> p7 >> p8 >> p9 >> p10;
if ( !myfile.good()) break;

and it was all ok.
But now I have a file with 540 columns of data and if I use the same routine, with … >> p539 >> p540; I don’t obtain results.
I obtain a message like "Error: Line too long in G__exec_statement when processing…"
If I split the command to more lines I don’t obtain correct TTree too.

Do you have some suggestions, like use other operators instead of >> ?
I used getline but the TTree is totally wrong because it read only the first number.

Thank you in advance.

You could try the ReadFile method of TTree, maybe it can handle that many columns.

I’m not an expert of root.
As I understand, the problem of my program is to read so many numbers from a file, line by line (necessary to put inside the tree). Instead the rest of the program works correctly (initializing parameters, create branches…) so if possible I’m searching a method to change only the lines in which I read the file in the correct way.

is this in a macro run through the ROOT interpreter?
Are you actually writing p1 to p540? You might want to use a for loop and read the columns into an array.


Hi Enrico (are you italian too?),

I wrote this simple program because I have the ascii file and I need to put the data in tree branches to do histograms, graphs, etc.

My program is like:

inizializing p1, p2, p3, …, p540;

open files…

make the tree and the branches…

read the file and fill the tree (this is the part that I wrote in my original post)

close the files.

I thought about use a loop and an array but I don’t know how to use with the >> operator; in addition the data are not of the same type (they are double, integer, boolean) and I think I don’t need to read the columns… I need to read line by line but all the 540 data of the first line and then I go to the second line (for this reason I used a while till the end of the file).
All is perfect if I have a file with 10 columns… or 100 columns (I tried)… the problem is with 540 columns for the too long command to read all them… so I need to find a different line command to read all data from a line.

I just tried it, the ReadFile method works with 600 columns without a problem

TTree t = new TTree(“treename”, “title of tree”);
t->ReadFile(“longline.txt”, “p0:p1:p2:p3:p4:p5:p6:p7:p8:p9:p10:p11:p12:p13:p14:p15:p16:p17:p18:p19:p20:p21:p22:p23:p24:p25:p26:p27:p28:p29:p30:p31:p32:p33:p34:p35:p36:p37:p38:p39:p40:p41:p42:p43:p44:p45:p46:p47:p48:p49:p50:p51:p52:p53:p54:p55:p56:p57:p58:p59:p60:p61:p62:p63:p64:p65:p66:p67:p68:p69:p70:p71:p72:p73:p74:p75:p76:p77:p78:p79:p80:p81:p82:p83:p84:p85:p86:p87:p88:p89:p90:p91:p92:p93:p94:p95:p96:p97:p98:p99:p100:p101:p102:p103:p104:p105:p106:p107:p108:p109:p110:p111:p112:p113:p114:p115:p116:p117:p118:p119:p120:p121:p122:p123:p124:p125:p126:p127:p128:p129:p130:p131:p132:p133:p134:p135:p136:p137:p138:p139:p140:p141:p142:p143:p144:p145:p146:p147:p148:p149:p150:p151:p152:p153:p154:p155:p156:p157:p158:p159:p160:p161:p162:p163:p164:p165:p166:p167:p168:p169:p170:p171:p172:p173:p174:p175:p176:p177:p178:p179:p180:p181:p182:p183:p184:p185:p186:p187:p188:p189:p190:p191:p192:p193:p194:p195:p196:p197:p198:p199:p200:p201:p202:p203:p204:p205:p206:p207:p208:p209:p210:p211:p212:p213:p214:p215:p216:p217:p218:p219:p220:p221:p222:p223:p224:p225:p226:p227:p228:p229:p230:p231:p232:p233:p234:p235:p236:p237:p238:p239:p240:p241:p242:p243:p244:p245:p246:p247:p248:p249:p250:p251:p252:p253:p254:p255:p256:p257:p258:p259:p260:p261:p262:p263:p264:p265:p266:p267:p268:p269:p270:p271:p272:p273:p274:p275:p276:p277:p278:p279:p280:p281:p282:p283:p284:p285:p286:p287:p288:p289:p290:p291:p292:p293:p294:p295:p296:p297:p298:p299:p300:p301:p302:p303:p304:p305:p306:p307:p308:p309:p310:p311:p312:p313:p314:p315:p316:p317:p318:p319:p320:p321:p322:p323:p324:p325:p326:p327:p328:p329:p330:p331:p332:p333:p334:p335:p336:p337:p338:p339:p340:p341:p342:p343:p344:p345:p346:p347:p348:p349:p350:p351:p352:p353:p354:p355:p356:p357:p358:p359:p360:p361:p362:p363:p364:p365:p366:p367:p368:p369:p370:p371:p372:p373:p374:p375:p376:p377:p378:p379:p380:p381:p382:p383:p384:p385:p386:p387:p388:p389:p390:p391:p392:p393:p394:p395:p396:p397:p398:p399:p400:p401:p402:p403:p404:p405:p406:p407:p408:p409:p410:p411:p412:p413:p414:p415:p416:p417:p418:p419:p420:p421:p422:p423:p424:p425:p426:p427:p428:p429:p430:p431:p432:p433:p434:p435:p436:p437:p438:p439:p440:p441:p442:p443:p444:p445:p446:p447:p448:p449:p450:p451:p452:p453:p454:p455:p456:p457:p458:p459:p460:p461:p462:p463:p464:p465:p466:p467:p468:p469:p470:p471:p472:p473:p474:p475:p476:p477:p478:p479:p480:p481:p482:p483:p484:p485:p486:p487:p488:p489:p490:p491:p492:p493:p494:p495:p496:p497:p498:p499:p500:p501:p502:p503:p504:p505:p506:p507:p508:p509:p510:p511:p512:p513:p514:p515:p516:p517:p518:p519:p520:p521:p522:p523:p524:p525:p526:p527:p528:p529:p530:p531:p532:p533:p534:p535:p536:p537:p538:p539:p540:p541:p542:p543:p544:p545:p546:p547:p548:p549:p550:p551:p552:p553:p554:p555:p556:p557:p558:p559:p560:p561:p562:p563:p564:p565:p566:p567:p568:p569:p570:p571:p572:p573:p574:p575:p576:p577:p578:p579:p580:p581:p582:p583:p584:p585:p586:p587:p588:p589:p590:p591:p592:p593:p594:p595:p596:p597:p598:p599”);
t->Print(); // for example

You will have to put in the variable type (double, integer, …) if they are not all just floats, as it is written in the documentation I linked earlier.

1 Like

In this way the program made the tree correctly.
The only problem is that now every branch is called p1, p2, p3, etc.
With my method every branch had the correct name of data that I put inside the branch creation command lines… but it’s only a little problem.
Thank you very much.

You can use meaningful names instead of p0, p1, etc., those were just for demonstration.

I know but re-inizializing 540 variables with names instead of p1,p2, etc… is a very long work.

In each case thank you for your solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.