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 !