READING Multiple Trees in the same root File

Hey all,

I have a code that would read in multiple text files, each text files have two column (a time and a Voltage), and create a tree (With the same name) for each file with each tree containing two branches(accordingly to two column of the text file). So one general .root file with multiple trees would be print out at the end. Is there a way to chain these trees together to store all time and Voltage branch into a RawDataTree with two branch called total time and total Voltage?

I wonder is there a better way to do this? Basically bypass the multiple trees step and directly read in multiple text files and storing each component as a branch with (#number of entries in one text file * x amount of files)… I’m not sure how to structure this code. Any suggestion?

//…
TTree *t = new TTree(“t”, “my tree”);
t->ReadFile(“file_1.txt”, “time/D:Voltage”);
t->ReadFile(“file_2.txt”);
t->ReadFile(“file_3.txt”);
// …

That’s What I had and was printing out multiple trees

[code]void Initialize(string &File_Name) {
//Declare Trees
TTree *tRaw = new TTree(“RawDataTree”, “Tree Containing Raw Data”);
tRaw->ReadFile(File_Name.c_str(), “time/F:Voltage/F”);
//Write Tree
tRaw->Write();

}

//Make Emtry Files and fill it with trees of Raw Data
void Made(int &start, int &end, int &min, int &max, int &Ch , int &run){
//Initialize Channel
switch(Ch) {
case 1: //Initialize Run Number
for(int i = 1; i <= run; i++){
//Initialize Decible
for(int j = min; j <= max; j+=5){
//Declare variables to write files
ostringstream fout;
fout << “test_” << i << “" << j << "” << Ch << “.root”;
string File_Name = fout.str();
//Create Root Files for each Run per decible
TFile *f = new TFile(File_Name.c_str(),“RECREATE”);
for(int k = start; k <= end; k++){
ostringstream name, inFile;
inFile << “Ch1_set”<< k << “" << i << "” << j << “_.txt”;
string File_Name = inFile.str();
Initialize(File_Name);
}
f->Close();
}
}
}[/code]

[quote]That’s What I had and was printing out multiple trees[/quote]For each text file, the code call Initialized that creates a new TTree object (i.e. the code explicitly request many TTree objects). You need to refactor the code so that the TTree is created only once.

Cheers,
Philippe.

[quote=“pcanal”][quote]That’s What I had and was printing out multiple trees[/quote]For each text file, the code call Initialized that creates a new TTree object (i.e. the code explicitly request many TTree objects). You need to refactor the code so that the TTree is created only once.

Cheers,
Philippe.[/quote]

I see. Thanks :smiley:

So i revise the code to declare the tree only once. However, When I tried to append each of the txt files into the tree, It would print out x amount of files i declare but each files would have an extra 2500 entries from the previous file to it. ie tree 1 2500, tree 2 5000, tree 3 7500 etc.
Is there a fix to this that would only give me one tree with the total entries?

This is the code THat I’m working with

//Initialize Channel switch(Ch) { case 1: //Initialize Run Number for(int i = 1; i <= run; i++){ //Initialize Decible for(int j = min; j <= max; j+=5){ //Declare variables to write files ostringstream fout; fout << "test_" << i << "_" << j << "_" << Ch << ".root"; string File_Name = fout.str(); //Create Root Files for each Run per decible TFile *f = new TFile(File_Name.c_str(),"RECREATE"); //Declare Trees TTree *tRaw = new TTree("RawDataTree","Tree Containing Raw Data"); for(int k = start; k <= end; k++){ ostringstream name, inFile; inFile << "Ch1_set"<< k << "_" << i << "_" << j << "_.txt"; string File_Name = inFile.str(); tRaw->ReadFile(File_Name.c_str(), "time/F:Voltage/F"); //Write Tree } tRaw->Write(); f->Close(); } }

Try something like this: //Initialize Channel switch(Ch) { case 1: //Initialize Run Number for(int i = 1; i <= run; i++) { //Initialize Decible for(int j = min; j <= max; j += 5) { //Create Root Files for each Run per decible TFile *f = new TFile(TString::Format("test_%d_%d_%d.root", i, j, Ch), "RECREATE"); //Declare Trees TTree *tRaw = new TTree("RawDataTree", "Tree Containing Raw Data"); for(int k = start; k <= end; k++) { tRaw->ReadFile(TString::Format("Ch%d_set%d_%d_%d_.txt", Ch, k, i, j), ((k == start) ? "time/F:Voltage/F" : "")); } //Write Tree tRaw->Write(); delete f; // automatically deletes "tRaw", too } } }

That code still did the same thing as what i describe earlier

Do you still have this problem?