Problem with calling TTree

Hello,

I have recently started to use ROOT to develop my application, so I apologize if my problem has got an obvious answer for you.

While running a little test program defining a TTree in a function and calling a Scan on it from outside I get CINT to crash.

In the loop I create the TTree from a file and I scan it successfully within the function (just for testing). Once back to the main function I want to make use of this TTree and call the same scan() on it. But this second scan sends CINT crashing.
It is unclear to me whether this is a problem with my code, or my use of CINT.

Below is my code:

#include "TTree.h"

void test(){

   TTree *mytree;
   mytree=ReadAbe(2);
   mytree->Print();
   mytree->Show(8);
   mytree->Scan("Chrom");
  
}

TTree *ReadAbe(int v) {
	struct Abe_t {
		Int_t           Chrom;
		Int_t           Start_bp;
		Int_t           End_bp;
		Float_t         Len_Mb;
		Char_t          Start_rs[16];
		Char_t          End_rs[16];
		Int_t           N_rs;
		Int_t           CN;
		Int_t           LOH;
		Float_t         Max_BF;
    };
	
	Abe_t abe;
	//The input file as output from my runof OncoSNP on Enric's dataset
	FILE *fp = fopen("test.dat","r");
	
	char line[500];
	TFile *f = new TFile("Abe.root","RECREATE");

	TTree *tree = new TTree("T","Aberrations from ascii file");
	tree->Branch("Values",&abe.Chrom,"Chrom/I:Start_bp/I:End_bp/I:Len_Mb/F");
	tree->Branch("Start",abe.Start_rs,"Startrs/C");
	tree->Branch("End",abe.End_rs,"Endrs/C");
	tree->Branch("Score",&abe.N_rs,"N_rs/I:CN/I:LOH/I:MaxBF/F");
	fgets(&line,500,fp);
	while (fgets(&line,500,fp)) {
		sscanf(&line[0],"%d %d %d %f %s %s %d %d %d %f",&abe.Chrom,&abe.Start_bp,&abe.End_bp,&abe.Len_Mb,abe.Start_rs,abe.End_rs,&abe.N_rs,&abe.CN,&abe.LOH,&abe.Max_BF);
		tree->Fill();
	}
	cout << "abe.LOH is "<< abe.LOH <<endl;
	
	tree->Print();
	tree->Write();
	tree->Scan("Chrom:Len_Mb");
	fclose(fp);
	delete f;
	return tree ;
}

And here is the way I call it from CINT, with the error message I get:

[code]root [2] .x test.C
abe.LOH is 1


*Tree :T : Aberrations from ascii file *
*Entries : 9 : Total = 3818 bytes File Size = 0 *

  •    :          : Tree compression factor =   1.00                       *
    

*Br 0 :Values : Chrom/I:Start_bp/I:End_bp/I:Len_Mb/F *
*Entries : 9 : Total Size= 1035 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *

*Br 1 :Start : Startrs/C *
*Entries : 9 : Total Size= 753 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *

*Br 2 :End : Endrs/C *
*Entries : 9 : Total Size= 738 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *

*Br 3 :Score : N_rs/I:CN/I:LOH/I:MaxBF/F *
*Entries : 9 : Total Size= 999 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *


  • Row * Chrom * Len_Mb *

  •    0 *         1 * 241.80000 *
    
  •    1 *         2 *     242.5 *
    
  •    2 *         3 *      21.5 *
    
  •    3 *         3 * 42.599998 *
    
  •    4 *         3 * 132.89999 *
    
  •    5 *         4 * 56.599998 *
    
  •    6 *         4 * 131.80000 *
    
  •    7 *         5 *         7 *
    
  •    8 *         5 * 99.900001 *
    

*** Break *** segmentation violation
(no debugging symbols found)
Attaching to program: /proc/8521/exe, process 8521
(no debugging symbols found)…done.
[Thread debugging using libthread_db enabled]
[New Thread 0x7f6e43e556f0 (LWP 8521)]
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)…done.
(no debugging symbols found)
0x00007f6e4188c965 in waitpid () from /lib/libc.so.6
#1 0x00007f6e4182ce11 in ?? () from /lib/libc.so.6
#2 0x00007f6e434f5b90 in TUnixSystem::StackTrace () from /usr/lib/root/libCore.so.5.18
#3 0x00007f6e434f6b05 in TUnixSystem::DispatchSignals () from /usr/lib/root/libCore.so.5.18
#4
#5 0x00007f6e41b3dd70 in ?? () from /lib/libc.so.6
#6 0x00007f6e43661dd3 in ?? () from /usr/lib/root/libCore.so.5.18
#7 0x00007f6e42a5b65a in G__call_cppfunc () from /usr/lib/root/libCint.so.5.18
#8 0x00007f6e42a3bb2e in G__interpret_func () from /usr/lib/root/libCint.so.5.18
#9 0x00007f6e42a2b540 in G__getfunction () from /usr/lib/root/libCint.so.5.18
#10 0x00007f6e42ae7cc2 in G__getstructmem () from /usr/lib/root/libCint.so.5.18
#11 0x00007f6e42ae1748 in G__getvariable () from /usr/lib/root/libCint.so.5.18
#12 0x00007f6e42a012cf in G__getitem () from /usr/lib/root/libCint.so.5.18
#13 0x00007f6e42a06a29 in G__getexpr () from /usr/lib/root/libCint.so.5.18
#14 0x00007f6e42a84837 in G__exec_statement () from /usr/lib/root/libCint.so.5.18
#15 0x00007f6e42a3ca98 in G__interpret_func () from /usr/lib/root/libCint.so.5.18
#16 0x00007f6e42a2b2a6 in G__getfunction () from /usr/lib/root/libCint.so.5.18
#17 0x00007f6e42a013f2 in G__getitem () from /usr/lib/root/libCint.so.5.18
#18 0x00007f6e42a06a29 in G__getexpr () from /usr/lib/root/libCint.so.5.18
#19 0x00007f6e42a120e4 in G__calc_internal () from /usr/lib/root/libCint.so.5.18
#20 0x00007f6e42a8d779 in G__process_cmd () from /usr/lib/root/libCint.so.5.18
#21 0x00007f6e434caba5 in TCint::ProcessLine () from /usr/lib/root/libCore.so.5.18
#22 0x00007f6e43434e6a in TApplication::ExecuteFile () from /usr/lib/root/libCore.so.5.18
#23 0x00007f6e43433a87 in TApplication::ProcessLine () from /usr/lib/root/libCore.so.5.18
#24 0x00007f6e42719add in TRint::HandleTermInput () from /usr/lib/root/libRint.so.5.18
#25 0x00007f6e434f4b24 in TUnixSystem::CheckDescriptors () from /usr/lib/root/libCore.so.5.18
#26 0x00007f6e434f5141 in TUnixSystem::DispatchOneEvent () from /usr/lib/root/libCore.so.5.18
#27 0x00007f6e4348aad6 in TSystem::InnerLoop () from /usr/lib/root/libCore.so.5.18
#28 0x00007f6e4348cba4 in TSystem::Run () from /usr/lib/root/libCore.so.5.18
#29 0x00007f6e43431b9f in TApplication::Run () from /usr/lib/root/libCore.so.5.18
#30 0x00007f6e4271b09e in TRint::Run () from /usr/lib/root/libRint.so.5.18
#31 0x000000000040115d in main ()
The program is running. Quit anyway (and detach it)? (y or n) [answered Y; input not from terminal]
Detaching from program: /proc/8521/exe, process 8521
Root > Function test() busy flag cleared
[/code]

After this it hangs and I need to kill ROOT from another terminal.
Any help with the use of CINT and/or the programming will be appreciated.

Thanks
Jean-Baptiste

Could you try with this version? you had some illegal calls to fgets.
If it does not work,
-post your data file such that we can try ourselves
-indicate which version of root you use

Rene

[code]#include “TTree.h”
#include “TFile.h”
#include
#include

TTree *ReadAbe(int v) {
struct Abe_t {
Int_t Chrom;
Int_t Start_bp;
Int_t End_bp;
Float_t Len_Mb;
Char_t Start_rs[16];
Char_t End_rs[16];
Int_t N_rs;
Int_t CN;
Int_t LOH;
Float_t Max_BF;
};

Abe_t abe;
//The input file as output from my runof OncoSNP on Enric’s dataset
FILE *fp = fopen(“test.dat”,“r”);

char line[500];
TFile *f = new TFile(“Abe.root”,“RECREATE”);

TTree *tree = new TTree(“T”,“Aberrations from ascii file”);
tree->Branch(“Values”,&abe.Chrom,“Chrom/I:Start_bp/I:End_bp/I:Len_Mb/F”);
tree->Branch(“Start”,abe.Start_rs,“Startrs/C”);
tree->Branch(“End”,abe.End_rs,“Endrs/C”);
tree->Branch(“Score”,&abe.N_rs,“N_rs/I:CN/I:LOH/I:MaxBF/F”);
fgets(line,500,fp);
while (fgets(line,500,fp)) {
sscanf(&line[0],"%d %d %d %f %s %s %d %d %d %f",&abe.Chrom,&abe.Start_bp,&abe.End_bp,&abe.Len_Mb,abe.Start_rs,abe.End_rs,&abe.N_rs,&abe.CN,&abe.LOH,&abe.Max_BF);
tree->Fill();
}
cout << "abe.LOH is "<< abe.LOH <<endl;

tree->Print();
tree->Write();
tree->Scan(“Chrom:Len_Mb”);
fclose(fp);
delete f;
return tree ;
}

void test(){

TTree *mytree;
mytree=ReadAbe(2);
mytree->Print();
mytree->Show(8);
mytree->Scan(“Chrom”);

}
[/code]

Dear Rene,

Thank you for the suggestion. You surely noticed that my code is strongly inspired by your cernstaff.C code. :unamused:
I have tried your updated code with ROOT version 5.18/10b and still get a very similar error:

jcazier@stilton:~/devel/Gewan$ root
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version  5.18/00b     10 March 2008   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.18/00b (branches/v5-18-00-patches@22563, Aug 12 2008, 23:54:00 on linuxx8664gcc)

CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .x test2.C
abe.LOH is 1
******************************************************************************
*Tree    :T         : Aberrations from ascii file                            *
*Entries :        9 : Total =            3818 bytes  File  Size =          0 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Br    0 :Values    : Chrom/I:Start_bp/I:End_bp/I:Len_Mb/F                   *
*Entries :        9 : Total  Size=       1035 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :Start     : Startrs/C                                              *
*Entries :        9 : Total  Size=        753 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    2 :End       : Endrs/C                                                *
*Entries :        9 : Total  Size=        738 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    3 :Score     : N_rs/I:CN/I:LOH/I:MaxBF/F                              *
*Entries :        9 : Total  Size=        999 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
************************************
*    Row   *     Chrom *    Len_Mb *
************************************
*        0 *         1 * 241.80000 *
*        1 *         2 *     242.5 *
*        2 *         3 *      21.5 *
*        3 *         3 * 42.599998 *
*        4 *         3 * 132.89999 *
*        5 *         4 * 56.599998 *
*        6 *         4 * 131.80000 *
*        7 *         5 *         7 *
*        8 *         5 * 99.900001 *
************************************

 *** Break *** segmentation violation
(no debugging symbols found)
Attaching to program: /proc/10607/exe, process 10607
(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0x7ff27899b6f0 (LWP 10607)]
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)
0x00007ff2763d2965 in waitpid () from /lib/libc.so.6
#1  0x00007ff276372e11 in ?? () from /lib/libc.so.6
#2  0x00007ff27803bb90 in TUnixSystem::StackTrace () from /usr/lib/root/libCore.so.5.18
#3  0x00007ff27803cb05 in TUnixSystem::DispatchSignals () from /usr/lib/root/libCore.so.5.18
#4  <signal handler called>
#5  0x00007ff276683d70 in ?? () from /lib/libc.so.6
#6  0x00007ff2781a7dd3 in ?? () from /usr/lib/root/libCore.so.5.18
#7  0x00007ff2775a165a in G__call_cppfunc () from /usr/lib/root/libCint.so.5.18
#8  0x00007ff277581b2e in G__interpret_func () from /usr/lib/root/libCint.so.5.18
#9  0x00007ff277571540 in G__getfunction () from /usr/lib/root/libCint.so.5.18
#10 0x00007ff27762dcc2 in G__getstructmem () from /usr/lib/root/libCint.so.5.18
#11 0x00007ff277627748 in G__getvariable () from /usr/lib/root/libCint.so.5.18
#12 0x00007ff2775472cf in G__getitem () from /usr/lib/root/libCint.so.5.18
#13 0x00007ff27754ca29 in G__getexpr () from /usr/lib/root/libCint.so.5.18
#14 0x00007ff2775ca837 in G__exec_statement () from /usr/lib/root/libCint.so.5.18
#15 0x00007ff277582a98 in G__interpret_func () from /usr/lib/root/libCint.so.5.18
#16 0x00007ff2775712a6 in G__getfunction () from /usr/lib/root/libCint.so.5.18
#17 0x00007ff2775473f2 in G__getitem () from /usr/lib/root/libCint.so.5.18
#18 0x00007ff27754ca29 in G__getexpr () from /usr/lib/root/libCint.so.5.18
#19 0x00007ff2775580e4 in G__calc_internal () from /usr/lib/root/libCint.so.5.18
#20 0x00007ff2775d3779 in G__process_cmd () from /usr/lib/root/libCint.so.5.18
#21 0x00007ff278010ba5 in TCint::ProcessLine () from /usr/lib/root/libCore.so.5.18
#22 0x00007ff277f7ae6a in TApplication::ExecuteFile () from /usr/lib/root/libCore.so.5.18
#23 0x00007ff277f79a87 in TApplication::ProcessLine () from /usr/lib/root/libCore.so.5.18
#24 0x00007ff27725fadd in TRint::HandleTermInput () from /usr/lib/root/libRint.so.5.18
#25 0x00007ff27803ab24 in TUnixSystem::CheckDescriptors () from /usr/lib/root/libCore.so.5.18
#26 0x00007ff27803b141 in TUnixSystem::DispatchOneEvent () from /usr/lib/root/libCore.so.5.18
#27 0x00007ff277fd0ad6 in TSystem::InnerLoop () from /usr/lib/root/libCore.so.5.18
#28 0x00007ff277fd2ba4 in TSystem::Run () from /usr/lib/root/libCore.so.5.18
#29 0x00007ff277f77b9f in TApplication::Run () from /usr/lib/root/libCore.so.5.18
#30 0x00007ff27726109e in TRint::Run () from /usr/lib/root/libRint.so.5.18
#31 0x000000000040115d in main ()
The program is running.  Quit anyway (and detach it)? (y or n) [answered Y; input not from terminal]
Detaching from program: /proc/10607/exe, process 10607
Root > Function test2() busy flag cleared

Here is the input file I am using. It is TAB separated, with a header:

jcazier@stilton:~/devel/Gewan$ cat test.dat 
Chromosome      Start Position (bp)     End Position (bp)       Length / Mb     Start Position (rsID)   End Position (rsId)     No. Probes      Copy Number     LOH     Max. Log BF
1         2058911       243904482       241.8   rs884080        rs2027432       489     2       0       0.0000
2           11494       242478932       242.5   rs7594188       rs16747 486     2       0       0.0000
3        43493171        64969335       21.5    rs1351631       rs1983357       43      1       0       19.4501
3          166245        42772735       42.6    rs1516337       rs339681        95      2       0       0.0000
3        65921192       198796315       132.9   rs1864509       rs1864668       274     2       0       0.0000
4         1035095        57590291       56.6    rs935971        rs899631        76      2       1       24.4813
4        59410428       191164601       131.8   rs1509062       rs1915853       233     1       0       158.9349
5        73278467        80312801       7.0     rs283596        rs34999 18      1       0       22.0795
5        80508524       180420866       99.9    rs1020720       rs1053110       176     2       1       92.2248

Let me know if there is anything else you need.

Thanks again for your help
Cheers !
Jean-Baptiste

As I said, post your data file such that we can try. I am suspecting a character string too long for the dimensions (16).

Rene

[quote=“brun”]As I said, post your data file such that we can try. I am suspecting a character string too long for the dimensions (16).

Rene[/quote]

I am a bit confused. I thought I put the input/data file called test.dat in my previous post.
Or do you mean another file ?

Chromosome      Start Position (bp)     End Position (bp)       Length / Mb     Start Position (rsID)   End Position (rsId)     No. Probes      Copy Number     LOH     Max. Log BF
1         2058911       243904482       241.8   rs884080        rs2027432       489     2       0       0.0000
2           11494       242478932       242.5   rs7594188       rs16747 486     2       0       0.0000
3        43493171        64969335       21.5    rs1351631       rs1983357       43      1       0       19.4501
3          166245        42772735       42.6    rs1516337       rs339681        95      2       0       0.0000
3        65921192       198796315       132.9   rs1864509       rs1864668       274     2       0       0.0000
4         1035095        57590291       56.6    rs935971        rs899631        76      2       1       24.4813
4        59410428       191164601       131.8   rs1509062       rs1915853       233     1       0       158.9349
5        73278467        80312801       7.0     rs283596        rs34999 18      1       0       22.0795
5        80508524       180420866       99.9    rs1020720       rs1053110       176     2       1       92.2248

Thanks for your patience.

BW
Jean-Baptiste

You have something wrong at the end of your test program. You delete the file containing the Tree and then return the tree pointer that is invalid.

Rene

[quote=“brun”]You have something wrong at the end of your test program. You delete the file containing the Tree and then return the tree pointer that is invalid.

[/quote]

So simple ! ](*,)

Sorry to have bothered with such simple problem… Still learning. :unamused:
Thanks a lot

Jean-Baptiste