I’ve got a problem with reading a simple entry from a tree. Hopefully the answer is simlpe as well
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.
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;
}
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.