Problems with retrieving data from a TTree

Dear all,

I am trying to read data from a TTree and to store a part of it in an histogram.
To do so, I made a program going like this :

[code] #include <stdio.h>
#include <stdlib.h>
#include
#include
#include
#include
#include <string.h>
#include
#include “TGraph.h”
#include “TSpectrum.h”
#include “TH1.h”
#include “TH2.h”
#include “TF1.h”
#include “TFile.h”
#include “TF1.h”
#include “TROOT.h”
#include “TTree.h”
#include “TChain.h”
#include “TBranch.h”
#include “TMath.h”
#include “TGraph.h”
#include “TGraph2D.h”
#include “TCutG.h”
#include “TCanvas.h”
#include “TStyle.h”
#include “TArrow.h”
#include “TLine.h”
#include “TProfile.h”
#include “TInterpreter.h”
#include “TApplication.h”

using namespace std;

TApplication *myapp=new TApplication("myapp",0,0);

void tree_reader(char *file, char *treename, int bornes)
{
    Double_t fCH3_Q1 = 0.0;
    Double_t fCH3_Q2 = 0.0;
    Double_t fCH3_Q3 = 0.0;
    Double_t fCH4_Q1 = 0.0;
    Double_t fCH4_Q2 = 0.0;
    Double_t fCH4_Q3 = 0.0;
    Double_t fCH3_Qratio = 0.0;
    Double_t fCH4_Qratio = 0.0;
 
    TFile *f = TFile::Open(file);

    if (f == 0)
      {
        printf("Error: cannot open %s\n",file);
        return;
      }

    TTree *T  = (TTree*)f->Get(treename);
     gROOT->Reset();
    TH1D *spectre_CH3_Q3_NN = new TH1D(treename,"CH3_Q3_NN",1024,0.,900000.);
 
    T->SetBranchAddress("CH3_Q1",&fCH3_Q1);
    T->SetBranchAddress("CH3_Q2",&fCH3_Q2);
    T->SetBranchAddress("CH3_Q3",&fCH3_Q3);
    T->SetBranchAddress("CH4_Q1",&fCH4_Q1);
    T->SetBranchAddress("CH4_Q2",&fCH4_Q2);
    T->SetBranchAddress("CH4_Q3",&fCH4_Q3);
 
    Long64_t nentries = T->GetEntries();

    for (Long64_t i=0;i<nentries;i++)
      {
            T->GetEntry(i);
           spectre_CH3_Q3_NN->Fill(fCH3_Q3);
       }
 
    TFile *hf = new TFile("Directs_Cond_NN.root","UPDATE");
    spectre_CH3_Q3_NN->Write();
    hf->Close();
   
    f->Close();
 
}


///---------Main---------------------///
int main (int argc, char** argv) {
 
  if(argc < 2)
    {
      printf ("usage : \n");
      printf ("        %s  filename.root filename case_number\n", argv[0]);
      printf ("\n");
      return EXIT_SUCCESS;
    }
 
  int x = atoi(argv[3]);
 
  tree_reader(argv[1],argv[2],x);
 
  return EXIT_SUCCESS;

}[/code]

I run this code in a loop so that I read trees from different experiments. Everything seems to look fine : I have the right number of entries in each run and the histograms are well defined.
The thing is that every histogram is filled with zeros (values…). Every single value from each branch is equal to zero in the histograms I created, and that is not the case when using the TreeViewer to look at he branches etc…

Does anyone have any suggestion about this problem ?

Thank you in advance !

Sylvain.

Furthermore, when I try to display the entries by adding the function Show() in the loop :

for (Long64_t i=0;i<nentries;i++) { T->GetEntry(i); T->Show(i); spectre_CH3_Q3_NN->Fill(fCH3_Q3); }
then I get these errors :

Error in <TTree::Show()>: Cannot read entry 104580 (I/O error) Error in <TTree::Show()>: Cannot read entry 104581 (I/O error) Error in <TTree::Show()>: Cannot read entry 104582 (I/O error) Error in <TTree::Show()>: Cannot read entry 104583 (I/O error) Error in <TTree::Show()>: Cannot read entry 104584 (I/O error) Error in <TTree::Show()>: Cannot read entry 104585 (I/O error)

Which means I doing something wrong but I’m really not seeing it…

  1. remove “gROOT->Reset();” completely -> see http://root.cern.ch/root/html/TROOT.html#TROOT:Reset
  2. maybe move “new TApplication” to inside of your “main” (btw. I assume you do NOT use this code as interpreted nor ACLiC precompiled -> i.e. I assume you compile and link a standalone executable)

Hi,

I assume the problem is that CH3_Q3 is not stored as a simple double (in a branch created by a leaflist).
Check carefully the output of T->Print();

Cheers,
Philippe.

Yes, that’s what I do.
Removing “gROOT->Reset();” and/or moving the TApplication did not change the output, I still get “(I/O error)”.

I built my trees with Double_t variables declared in branches like that :

t1.Branch("CH3_Q1",&CH3_Q1,"CH3_Q1/D"); t1.Branch("CH3_Q2",&CH3_Q32"CH3_Q2/D"); t1.Branch("CH3_Q3",&CH3_Q3,"CH3_Q3/D"); t1.Branch("CH4_Q1",&CH4_Q1,"CH4_Q1/D"); t1.Branch("CH4_Q2",&CH4_Q2,"CH4_Q2/D"); t1.Branch("CH4_Q3",&CH4_Q3,"CH4_Q3/D");
And “T->Print()” gives me this :

[code]******************************************************************************
*Tree :Run10_Direct: Run10_Direct *
*Entries : 1171371 : Total = 112662127 bytes File Size = 56368692 *

  •    :          : Tree compression factor =   2.00                       *
    

*Br 0 :CH3_Q1 : CH3_Q1/D *
*Entries : 1171371 : Total Size= 18776968 bytes File Size = 9394782 *
*Baskets : 294 : Basket Size= 32000 bytes Compression= 2.00 *

*Br 1 :CH3_Q2 : CH3_Q2/D *
*Entries : 1171371 : Total Size= 18776968 bytes File Size = 9394782 *
*Baskets : 294 : Basket Size= 32000 bytes Compression= 2.00 *

*Br 2 :CH3_Q3 : CH3_Q3/D *
*Entries : 1171371 : Total Size= 18776968 bytes File Size = 9394782 *
*Baskets : 294 : Basket Size= 32000 bytes Compression= 2.00 *

*Br 3 :CH4_Q1 : CH4_Q1/D *
*Entries : 1171371 : Total Size= 18776968 bytes File Size = 9394782 *
*Baskets : 294 : Basket Size= 32000 bytes Compression= 2.00 *

*Br 4 :CH4_Q2 : CH4_Q2/D *
*Entries : 1171371 : Total Size= 18776968 bytes File Size = 9394782 *
*Baskets : 294 : Basket Size= 32000 bytes Compression= 2.00 *

*Br 5 :CH4_Q3 : CH4_Q3/D *
*Entries : 1171371 : Total Size= 18776968 bytes File Size = 9394782 *
*Baskets : 294 : Basket Size= 32000 bytes Compression= 2.00 *
[/code]
As it’s the first time I use a TTree, I’m not really comfortable with it but I guess I’m only dealing with Doubles here…

If your root file is not too big, post it here, or give a link to it so that we can try it.
Looking at your code again … why do you use “UPDATE” for your “hf”? Maybe you want to say “RECREATE”?

Ok, here it is.

Thank you !
Run10_Direct.root (2 MB)

Because I am extracting only one part of the tree into an histogram. I’m using the other branches to make conditions upon filling the histogram or not. Finally, I run the code several times in a shell script to treat several runs and store these histograms in one single root file (to lighten the data analysis…)

There’s no ntuple inside of your root file. There are histograms and they are all non-empty, simply try “CH3_Q1->Draw()” for example.

root [0] TFile f("Run10_Direct.root") root [1] f->ls() TFile** Run10_Direct.root TFile* Run10_Direct.root KEY: TH1D CH3_Q1;1 Run10_Direct.root KEY: TH1D CH4_Q1;1 Run10_Direct.root KEY: TH1D CH3_Q2;1 Run10_Direct.root KEY: TH1D CH4_Q2;1 Run10_Direct.root KEY: TH1D CH3_Q3;1 Run10_Direct.root KEY: TH1D CH4_Q3;1 Run10_Direct.root KEY: TH2D CH3_discri;1 Run10_Direct.root KEY: TH2D CH4_discri;1 Run10_Direct.root root [2] CH3_Q1->Draw()

Oh… I sent you a wrong file… my bad !
The smallest I have is about 8MB and the forum seems not to accept it…

But my terminal says :

root [0] TFile f("Run10_Direct.root") root [1] f->ls() TFile** Run10_Direct.root TFile* Run10_Direct.root KEY: TTree Run10_Direct;1 Run10_Direct root [2]

Hi,

So, so far there is nothing obviously wrong and it should work. To make any further progress we would need access to ROOT files so that we can reproduce the problem and/or assert whether the file is corrupted. Can you post this file on some public web server and/or just email it to me.

Cheers,
Philippe.

Okay, so I found my error… When creating the histogram :

Which was supposed to be stored in an another root file, I used the same name than the tree. Apparently, it was conflicting with the tree…
Now it just works fine with a different histogram name !

Nonetheless, thank you all for your time and help !

Cheers,
Sylvain.