Possible bug regarding a nested TObjArray

If you have a TObjArray with other TObjArray inside and you try to apply a method to the nested TObjArrays you are told that TObject has no member called whatever you are trying to use, be it At() or Add() or anything else.

It seems that Root asumes that something inside a TObjArray won’t be a TCollection.

Here is the shortest code I’ve made that produces this mistake:

#include "TObjArray.h"
#include "TH2D.h"

int main() 
{
TObjArray* myArray = new TObjArray(1);
TObjArray* myNestedArray = new TObjArray(1);
myArray->Add(myNestedArray);
TH2D*  h1  = new TH2D("h1", "H1", 9, 0, 1.8, 1000, -20, 20);
myArray->At(0)->At(0)->Add(h1);
}

Try:

int main() 
{
   TObjArray* myArray = new TObjArray(1);
   TObjArray* myNestedArray = new TObjArray(1);
   myArray->Add(myNestedArray);
   TH2D*  h1  = new TH2D("h1", "H1", 9, 0, 1.8, 1000, -20, 20);
   ((TObjArray *)myArray->At(0))->Add(h1);
   ((TObjArray *)myArray->At(0))->At(0)->Print();
}
1 Like

Thanks a lot, but now my question is: why does this work?.

I would expect that myArray would have two things inside: myNestedArray and h1, but instead it appears that h1 is inside myNestedArray.

I need to understand that because, ideally, I would like for myNestedArray to have more that one entry, and to be able to modify them as needed

You’re welcome, and well, it works because TObjArray::At(idx) returns a pointer to a TObject, hence the need of casting to the proper inherited class (TObjArray*)

Indeed. If you want h1 into myArray, then do: myArray->Add(h1);
It seems obvious enough to me…

1 Like

It seems obvious enough to me…

I was never the first one to understand something, but I was the one solving everyone’s doubts before the exam.

But i think I get it now.

1 Like

This may help to see where is going what :wink:

#include <iostream>
#include "TObjArray.h"
#include "TH2D.h"

int main()  
{
   TObjArray* myArray = new TObjArray(1);
   TObjArray* myNestedArray = new TObjArray(1);
   myArray->Add(myNestedArray);
   TH2D*  h1  = new TH2D("h1", "H1", 9, 0, 1.8, 1000, -20, 20);
   TH2D*  h2  = new TH2D("h2", "H2", 9, 0, 1.8, 1000, -20, 20);
   TH2D*  h3  = new TH2D("h3", "H3", 9, 0, 1.8, 1000, -20, 20);
   TH2D*  h4  = new TH2D("h4", "H4", 9, 0, 1.8, 1000, -20, 20);
   myArray->Add(h1);
   myArray->Add(h2);

   ((TObjArray *)myArray->At(0))->Add(h3);
   ((TObjArray *)myArray->At(0))->Add(h4);

   std::cout << "content of myArray->At(0):" << std::endl;
   myArray->At(0)->Print();
   std::cout << std::endl;

   std::cout << "content of myArray->At(1):" << std::endl;
   myArray->At(1)->Print();
   std::cout << "content of myArray->At(2):" << std::endl;
   myArray->At(2)->Print();
   std::cout << std::endl;

   std::cout << "content of myArray->At(0)->At(0):" << std::endl;
   ((TObjArray *)myArray->At(0))->At(0)->Print();
   std::cout << "content of myArray->At(0)->At(1):" << std::endl;
   ((TObjArray *)myArray->At(0))->At(1)->Print();
   std::cout << std::endl;
}
1 Like

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