Loop over TObjArray in ROOT5

Hi, I’m trying to get some ROOT6 code working under ROOT5. I have some range-based for loops over some TObjArrays, and I don’t really know how to convert them to work with ROOT5 (i.e. when doing .L mycode.C+)

The code fragment in question is:

    double get_nll(const std::vector<int> & clusters, Geom::Side side = Geom::BOTH) const
      double min_value = std::numeric_limits<double>::max();
      double min_index = -1;
      int index = 0;
      for(const auto& tobjp : *(toas.at(side)))  // <--- This line breaks ACliC
        const double this_value = nll(clusters,static_cast<const TH1D*>(tobjp));
        if(this_value < min_value)
          min_value = this_value;
          min_index = index;
      return (*ranges.at(side))[min_index];

The error message I get when trying to .L mycode.C+ is:

/Users/jfcaron/Projects/Proto2BeamTest2/Likelihood.C:264:29: error: invalid range expression of type 'TObjArray'; no viable 'begin' function available for(const auto& tobjp : *(toas.at(side)))

Now in ROOT6 TObjArrays have Begin() and End() methods that can be used for the range-for, but not in ROOT5 (afaik). Is the only way to do a loop over an integer index from 0 from toa.GetEntries()? Are there any performance implications of an integer-index loop over the range-for?


Hi Jean-François,

You can use the TIter interface in both ROOT 5 and 6:

TIter iObj(&toas);
while (TObject* obj = iObj()) {

But for a TObjArray, the most performant access is indeed through toas.At(idx).

Cheers, Axel.