Empty TTree while reading a .dat file

Hi everyone, I made a simple TTree in this program that reads a .dat file:

#include "Riostream.h"
#include "TTimer.h"
#include "TROOT.h"
#include "TStyle.h"
#include "TFile.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TNtuple.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TF1.h"
#include "TProfile.h"
#include "TLegend.h"

//--------------------Creation of a ttree with the data from the acquisition file
void make_TTree(){
 
  gROOT->Reset();

  ifstream file;

struct staff_t {
    Int_t idgame;
    Int_t setnumber;
    Int_t actnumber;
    Int_t touchnumber;
    Int_t idplayer;
    Int_t idscout;
    TString team;
    TString found;
    TString balltype;
    TString effect;
    TString comb;
    Int_t azone;
    Int_t bzone;
    TString sunzone;
    TString ext1;
    Int_t ext2;
    Int_t ext3;
    TString custom;
    TString changeball;
    TString attackRD;
    Int_t homesetter;
    Int_t visitorsetter;
  };
  staff_t staff;
  
  file.open("19mus.dat");
 
  Int_t nlines = 0;

  cout<<"----------------------------------------------"<<endl;
  cout<<"ho aperto il file"<<endl;
  cout<<"----------------------------------------------"<<endl;
  

  TFile *f = new TFile("summ.root","RECREATE");
  TTree *tree = new TTree("tree","data");
  TBranch* b_staff = tree->Branch("staff", &staff, "idgame:setnumber:actnumber:touchnumber:idplayer:idscout:team:found:balltype:effect:comb:azone:bzone:sunzone:ext1:ext2:ext3:custom:changeball:attackRD:homesetter:visitorsetter");
  
  cout<<"ho creato il tfile, il ttree e la branch"<<endl;
  cout<<"----------------------------------------------"<<endl;
  
  while (1) {
  //while (fgets(&line,80,fp)){
    file>>staff.idgame>>staff.setnumber>>staff.actnumber>>staff.touchnumber>>staff.idplayer>>staff.idscout>>staff.team>>staff.found>>staff.balltype>>staff.effect>>staff.comb>>staff.azone>>staff.bzone>>staff.sunzone>>staff.ext1>>staff.ext2>>staff.ext3>>staff.custom>>staff.changeball>>staff.attackRD>>staff.homesetter>>staff.visitorsetter;
    
    if (!file.good()) break;
       
    tree->Fill();
    
    nlines++;
  }
  
  tree->Print();
  cout << " found " << nlines << " events " << endl;
  file.close();
  f->Write();
}

Even if the file is not empty, the output is

**************************************************************************
*Tree    :tree : data                                       *
*Entries :        0 : Total =            2684 bytes  File  Size =          0 *
*        :          : Tree compression factor =   1.00                       *
**************************************************************************
*Br    0 :staff     : idgame:setnumber:actnumber:touchnumber:idplayer:       *
*         | idscout:team:found:balltype:effect:comb:azone:bzone:sunzone:ext1:*
*         | ext2:ext3:custom:changeball:attackRD:homesetter:visitorsetter    *
*Entries :        0 : Total  Size=       2292 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
 found 0 events

I cannot figure where is the issue. Any help?

Thanks!


_ROOT Version: 16
_Platform: Ubuntu 18.04


Hi,

aren’t you missing a tree->Write(); statement before closing the file?

Cheers,
D

dpiparo: But apparently the tree is empty before closing the file.
Chiara: maybe the dat file is not being read. Try adding this just after the file.open line:

  if (!file) cout << "File not found!" << endl;

Also, print some values to check; e.g. something like this inside the while loop:

    if (nlines < 2) cout << staff.idgame << " " << staff.visitorsetter << endl;

and see if the output coincides with the dat file contents.
And you need to specify the variable types in the branch: this line

TBranch* b_staff = tree->Branch("staff", &staff, "idgame:setnumber:actnumber:touchnumber:idplayer:idscout:team:found:balltype:effect:comb:azone:bzone:sunzone:ext1:ext2:ext3:custom:changeball:attackRD:homesetter:visitorsetter");

should be:

TBranch* b_staff = tree->Branch("staff", &staff.idgame, "idgame/I:setnumber:actnumber:touchnumber:idplayer:idscout:team/C:found:balltype:effect:comb:azone/I:bzone:sunzone/C:ext1:ext2/I:ext3:custom/C:changeball:attackRD:homesetter/I:visitorsetter");

Hi everyone, thanks to @dastudillo and @dpiparo for answering me. Turns out that the problem was that the .dat file wasn’t written correctly, and I could find out thanks to

if (nlines < 2) cout << staff.idgame << " " << staff.visitorsetter << endl;

Thank you!!

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