Writing a variable to a TTree went wrong

Dear all,

I am using the EventLoop package and I am trying to fill a TTree with a particle’s kinematic variables for the particles of each event. I have declared a variable for the number of particles and a vector in the corresponding .h file:

int particle; //! 
vector <double> pt; //!

and then in my main .cxx file:

tree->Branch("particle", &particle);
tree->Branch("pt", &pt);

I have filled an IParticleContainer *leptons with my leptons after a selection and then I fill the branches of my tree as follows:

if(leptons->size() > 0){
   particle = leptons->size();
   for(auto lepton : *leptons){

and then I clear the vector at the end of the event. By doing so, the “particle” number should be the same with the size of the vector for the pt.

The code compiles and runs fine. However, when I use the MakeClass command to read the TTree and I print its components, the size of the pt vector is 0 for a small fraction of events, while it works fine for the others. Are there any ideas about what is causing this problem?

Thank you in advance for your answer.


ROOT Version 6.09/02 Linux/ Ubuntu 16.04 LTS 32-bit
g++ 4:5.3.1-1ubuntu1
g++ -5 5.4.0-6ubuntu1~16.04.9

Do you also reset the value of ‘particle’ to zero?

However, when I use the MakeClass command to read the TTree and I print its components, the size of the pt vector is 0 for a small fraction of events, while it works fine for the others.

MakeClass is not very good at reading vector. It is however surprising that it works for some entries and not others. Try adding



The problem probably does not have to do with MakeClass because when I open the .root file with root -l myfile.root and draw the pt branch directly from the TTree with:


the histogram has less entries than expected. In particular, the sum of the particles is 548 and the entries for the pt are 516. I have also tried to change my vector to a pointer but I got exactly the same result.


Ok. There is a problem in the producing/writing code then. I am unable to guess from the little you shown so far (which looks okay). Can you provide a running example writing on of your files so that we can help you debug it?

You were right about it. The problem was deeper than I first thought. For some reason, when I went back to the beginning to the object selection I noticed that if I run:

vector <double> *pt = nullptr;

TH1F *h_pT=new TH1F("h_pT","h_pT",10, 0., 700.);

const xAOD::MuonContainer* muons = nullptr;
ANA_CHECK(event->retrieve( muons, "Muons" ));

for( auto mu : *muons){

the muon counter and the histogram entries had the same value (1133), but the entries of the pt branch were 2 less (1129). Am I missing something?

Many thanks for your answers, I really appreciate it.


I am slightly more than an a beginner so I dont know much, however,

I have a similar EvenLoop where I have each entry in the tree a vector but mine is defined differently:
in header file:

Int_t Ntracks; //!
Int_t particle; //!
Int_t pt[1000]; //!

Then in main file:

tree->Branch("pt", &tpt,"pt[Ntracks]/I");

so that the size of each vector is the number of tracks in that event. Then in the execute() function I have to initialize the vector components before I can fill them:

 for(int i=0; i<1000; i++)

And then fill them:

 for( auto track : *Tracks ) { 
   if(tNtracks >= 1000) continue;
  tpt[tNtracks-1] = track->pt(); //or whatever value you want to fill it with

so maybe this helps you.
Best of Luck

Maybe, maybe not. We would need to see more of your code. The best would be a standalone reproducer to help us track down your issue.


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