Problem reading a TTree

Hi!

I’ve got a problem with reading a simple entry from a tree. Hopefully the answer is simlpe as well :slight_smile:
I paste a short macro and afterwards the root output. To my understanding the output of the Scan method shows that everything is fine with the tree itself. Just, what am I doing wrong with reading it to the variable tm?
I’m using Root v 3.10/02 on lxplus066.cern.ch running slc3.

Cheers,

Marco

The macro:

{
TTree* tr = (TTree *) gROOT->FindObject(“TDRselBs2KK/100”);
Double_t tm;
tr->SetBranchAddress(“t_min_p”,&tm);
cout <

GetEntries() <GetEntry(2);
cout << tm <Scan(“t_min_p”);
}

The output:

root [0]
Attaching file …/DVNtuples.root…
root [1] .X test.C
12
5.10359e-315


  • Row * t_min_p *

  •    0 * 0.1212302 *
    
  •    1 * 0.2675063 *
    
  •    2 * 0.0712832 *
    
  •    3 * 0.2183122 *
    
  •    4 * 0.0746984 *
    
  •    5 * 0.1151089 *
    
  •    6 * 0.1622145 *
    
  •    7 * 0.1411708 *
    
  •    8 * 0.1039734 *
    
  •    9 * 0.2517077 *
    
  •   10 * 0.2899948 *
    
  •   11 * 0.1995175 *
    

root [2]

Hi,

Please disable Html in your post when including code (otherwise your code appears garbled :frowning: ).

The likely problem is that t_min_p is fact a float and not a double (one would need to look at the result of tr->Print() to know for sure).

Cheers,
Philippe

Hi Philippe,

Thanks for your reply. Changing to float solved the problem. tr->Print() indeed showed that it is a float.

Cheers,

Marco

Hi,
I have a similar problem I can’t understand. I read a root file with a ttree named “analized_tdc1”. I cout one of the leaf defined as “Double_t tdc_time;”. The cout shows a number e+315, instead, if I use Scan() function of the tree, the number is correct. Can you help me?

Here the code:

TFile * f = new TFile(filename.c_str());
TTree * analyzed_tdc1 = (TTree * ) f->Get("analyzed_tdc1");	
tdc1Class * tdc1 = new tdc1Class(analyzed_tdc1);

Long64_t nentries = analyzed_tdc1->GetEntries();
Long64_t nbytes = 0, nb = 0;

for (int jentry=0; jentry<nentries; jentry++) {
	analyzed_tdc1->GetEntry(jentry);
	cout<<"  time= "<< tdc1->tdc_time<<endl;
   }

analyzed_tdc1->Print();

what does it mean?
if I do analyzed_tdc1->Print(); this is the result:

root [2] analyzed_tdc1->Print();
******************************************************************************
*Tree    :analyzed_tdc1: mdaq raw tdc1                                          *
*Entries :   195965 : Total =        11009576 bytes  File  Size =    3441348 *
*        :          : Tree compression factor =   3.20                       *
******************************************************************************
*Br    0 :tdc       : card/I:flag/I:channel/I:bank/I:event/I:marker/I:time/D:*
*         | ttime/D:pos1/D:pos2/D                                            *
*Entries :   195965 : Total  Size=   11009167 bytes  File Size  =    3437554 *
*Baskets :      344 : Basket Size=      32000 bytes  Compression=   3.20     *
*............................................................................*

So, a single “tdc” branch holds a structure (some Int_t and Double_t values). You need to inspect the definition of your “tdc1Class” and ensure that it strictly follows this structure.

I created tdc1Class using the MakeClass() function on the root file, so it should be correct, I think.
The Class definition follows:

//////////////////////////////////////////////////////////
// This class has been automatically generated on
// Thu Sep  1 16:08:43 2022 by ROOT version 6.24/06
// from TTree analyzed_tdc1/mdaq raw tdc1
// found on file: trap2066_track.root
//////////////////////////////////////////////////////////

#ifndef tdc1Class_h
#define tdc1Class_h

#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>

// Header file for the classes stored in the TTree if any.

class tdc1Class {
public :
   TTree          *fChain;   //!pointer to the analyzed TTree or TChain
   Int_t           fCurrent; //!current Tree number in a TChain

// Fixed size dimensions of array or collections stored in the TTree if any.

   // Declaration of leaf types
   Int_t           tdc_card;
   Int_t           tdc_flag;
   Int_t           tdc_channel;
   Int_t           tdc_bank;
   Int_t           tdc_event;
   Int_t           tdc_marker;
   Double_t        tdc_time;
   Double_t        tdc_ttime;
   Double_t        tdc_pos1;
   Double_t        tdc_pos2;

   // List of branches
   TBranch        *b_tdc;   //!

   tdc1Class(TTree *tree=0);
   virtual ~tdc1Class();
   virtual Int_t    Cut(Long64_t entry);
   virtual Int_t    GetEntry(Long64_t entry);
   virtual Long64_t LoadTree(Long64_t entry);
   virtual void     Init(TTree *tree);
   virtual void     Loop();
   virtual Bool_t   Notify();
   virtual void     Show(Long64_t entry = -1);
};

#endif

#ifdef tdc1Class_cxx
tdc1Class::tdc1Class(TTree *tree) : fChain(0) 
{
// if parameter tree is not specified (or zero), connect the file
// used to generate this class and read the Tree.
   if (tree == 0) {
      TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("trap2066_track.root");
      if (!f || !f->IsOpen()) {
         f = new TFile("trap2066_track.root");
      }
      f->GetObject("analyzed_tdc1",tree);

   }
   Init(tree);
}

tdc1Class::~tdc1Class()
{
   if (!fChain) return;
   delete fChain->GetCurrentFile();
}

Int_t tdc1Class::GetEntry(Long64_t entry)
{
// Read contents of entry.
   if (!fChain) return 0;
   return fChain->GetEntry(entry);
}
Long64_t tdc1Class::LoadTree(Long64_t entry)
{
// Set the environment to read one entry
   if (!fChain) return -5;
   Long64_t centry = fChain->LoadTree(entry);
   if (centry < 0) return centry;
   if (fChain->GetTreeNumber() != fCurrent) {
      fCurrent = fChain->GetTreeNumber();
      Notify();
   }
   return centry;
}

void tdc1Class::Init(TTree *tree)
{
   // The Init() function is called when the selector needs to initialize
   // a new tree or chain. Typically here the branch addresses and branch
   // pointers of the tree will be set.
   // It is normally not necessary to make changes to the generated
   // code, but the routine can be extended by the user if needed.
   // Init() will be called many times when running on PROOF
   // (once per file to be processed).

   // Set branch addresses and branch pointers
   if (!tree) return;
   fChain = tree;
   fCurrent = -1;
   fChain->SetMakeClass(1);

   fChain->SetBranchAddress("tdc", &tdc_card, &b_tdc);
   Notify();
}

Bool_t tdc1Class::Notify()
{
   // The Notify() function is called when a new file is opened. This
   // can be either for a new TTree in a TChain or when when a new TTree
   // is started when using PROOF. It is normally not necessary to make changes
   // to the generated code, but the routine can be extended by the
   // user if needed. The return value is currently not used.

   return kTRUE;
}

void tdc1Class::Show(Long64_t entry)
{
// Print contents of entry.
// If entry is not specified, print current entry
   if (!fChain) return;
   fChain->Show(entry);
}
Int_t tdc1Class::Cut(Long64_t entry)
{
// This function may be called from Loop.
// returns  1 if entry is accepted.
// returns -1 otherwise.
   return 1;
}
#endif // #ifdef tdc1Class_cxx

##################################################################
I edit this post because I’m a new user and I can’t reply more than 3 times and I can’t attach files…that’s a stupid rule…

Doing tdc1->Show(0); tdc1->Show(1); tdc1->Show(2); the values print are correct. I’m sorry but I can’t share the root file.
Thanks a lot.

Try:

tdc1->Show(0); std::cout << "tdc_time = " << tdc1->tdc_time << "\n";
tdc1->Show(1); std::cout << "tdc_time = " << tdc1->tdc_time << "\n";
tdc1->Show(2); std::cout << "tdc_time = " << tdc1->tdc_time << "\n";