Problems with the output TTree from 2 different ASCII files

Hi Rooters,

Again a stupid problem
:confused:

I read 2 ASCII files in 2 different ways and I show the data on the screen, but when I create an object (thant later I will store in a ROOT file an clean) and I try to fill the members inside the “while” loop that I’m using to read, the output ROOT file contains only crap when I’m trying to recover what my new ROOT file contains. I have tried almost all the possibilities: I had created one function to fill the object, I had store the values in a vector tha is being read by an external “for” loop, I had change the shape of the object… always crap, I’m storing different numbers that I’m seein in my screen!

Here you have the part of the code that I use to create the output ROOT file (a part of the main):

[code]…

TFile hfile(RootOut,“RECREATE”,“Data from Raw files”);

ifstream infile;

//trees and branches---------------------------
CRawData *entry = 0; //the object “entry”, the class CRawData
TTree *tree = new TTree(“Pino”,“RAW data tree”);
tree->Branch(“entries”,“CRawData”,&entry,16000,2);

//opening the files-------------------------------------
FILE *file = fopen(RawIn, “r”);
infile.open(SlowCIn);
Int_t ent = 0; //number of entries

while (!feof(file)) {
reading(file,Traw,Trigch,Datach,th1,th2,th3); //function that reads from one file
cout << "Raw Time passed " << Traw << " TrigCh " << Trigch << endl;
// here I obtein proper values
entry = new CRawData(); // the object again

  //reading from SlowCIn
  infile >> TimeStart >> Time >> Gas_Pres >> Gas_Temp >> GasTA >> GasTB >> O2cont >> HVFcage;
 // put the data on the screen
  cout << " Times       " << TimeStart << " " << Time << endl;
  cout << " Gas P and T " << Gas_Pres << "  " << Gas_Temp << endl;
  cout << " TA and TB   " << GasTA << "     " << GasTB << endl;
  cout << " O2 and HVFcage " << O2cont << " " << HVFcage << endl;

  //building a spectrum
  making(th1,th2,th3,array);
  for (int i=0;i<1024; i++){
  //cout << "    spectrum bin    : " << array[i] << endl;
  }

  //store the data in a CRawData object-------------------
  build(TimeStart,Time,Gas_Pres,Gas_Temp,GasTA,GasTB,O2cont,HVFcage,array,entry);
  tree->Fill();
  ent++;
  entry->Clear();

}
tree->Print();
cout << " Number of entries : " << ent << endl;
infile.close();
printf( “Reading File SlowCIn Closed\n” );
//“SLowCIn” closed---------------------------------------
if( !fclose(file) )
printf( “Reading File RawIn Closed\n” );
else
{
printf( “Error: file NOT CLOSED\n” );
return 1;
}
hfile.Write();
hfile.Close();
cout<< " Writting File RootOut Closed "<< endl;

-------------------------------------------------------[/code]
With this I’m expecting a Tree with a branch and lot of leaves with serialized information about the members of my object.
After that I try to read one leaf (for example “GasTA”) with this program:

[code]---------------------------------------------------------

int main(int argc, char **argv) //main program
{

//the application
TApplication *myApp = new TApplication(“myApp”,&argc, argv);

TFile *f= new TFile(“algo.root”);
TTree Pino = (TTree)f->Get(“Pino”);
CRawData entry = new CRawData();
Pino->SetBranchAddress(“entries”,&entry);
Pino->SetBranchStatus("
",1);
Int_t eventos = (int)Pino->GetEntries();

cout << “Events in the file :” << eventos << endl;
Int_t muestra;
cout << “Events to analyze ?:” << endl;
cin >> muestra;

 for(Int_t i=0;i< muestra;i++){
 if (i >0){
     entry->Clear();}
 if (Pino->GetEvent(i)<0){
     cout << "la cagaste !" << endl;
     continue;
 }
 cout << "Gas TA" << entry->getGasTB() << endl; //here I'm reading
 }//end for
 //close the file
 f->Close();
 //run myApp object
 myApp->Run();
 return 0;

}// end of main

------------------------------------------------------[/code]

Well, this is a long post, I know, but surely the answer will be short. I need to solve it, so, if I solve it before receiving an answer, I will post the solution 8)

Thanks in advance!
Cheers!

Hi,

In you writing code you have:

CRawData *entry = 0; //the object "entry", the class CRawData TTree *tree = new TTree("Pino","RAW data tree"); tree->Branch("entries","CRawData",&entry,16000,2); ... while (!feof(file)) { ... entry = new CRawData(); // the object again ... tree->Fill(); ent++; entry->Clear(); }
However, since you do not call SetBranchAddress after the new CRawData, the branch is not looking at that object. In addition, you probably have a memory leak (i.e. there is one CRawData per loop iteration which is created and never deleted).
If you really have to recreate the object in each iteration, you can do:CRawData *entry = 0; //the object "entry", the class CRawData TTree *tree = new TTree("Pino","RAW data tree"); tree->Branch("entries","CRawData",&entry,16000,2); ... while (!feof(file)) { ... entry = new CRawData(); // the object again tree->SetBranchAddress("entries",&entry); ... tree->Fill(); ent++; delete entry; entry = 0; }Otherwise you can do (for better performance):

CRawData *entry = new CRawData(); //the object "entry", the class CRawData TTree *tree = new TTree("Pino","RAW data tree"); tree->Branch("entries","CRawData",&entry,16000,2); ... while (!feof(file)) { ... // Do no call new CRawData, reuse the object tree->Fill(); ent++; entry->Clear(); }Cheers,
Philippe.

Thanks!!
:smiley:

This idea of use & rewrite the same object is COOL ! And I’m saving 2 lines of code. But I’m sorry but the problem was in another place: in the definition of my class “CRawData” I was written

void CRawData::SetTimeStart(Double_t t1){ t1 = TimeStart;}

instead of

void CRawData::SetTimeStart(Double_t t1){ this->TimeStart=t1;}

I know NOW that the 1st way is not the proper one if you’re setting under runtime, so to say. Under my conditions you MUST use a “this” pointer.

Sorry for my ingnorance, I had been “fighting with the trees” and I thought that the bug was there, but it wasn’t only there

:blush:

Thanks a lot !