Comparing the contents of two TLists

Hi!

I’d like to know if there is a way to compare the contents of two TLists in ROOT?

I’ve got two THStacks which each have a list of histograms(TH1Ds), and I would like to compare both of them to make sure they both have elements of the same name.

Thanks in advance!

The various ways to iterate on the TList are explained here:
https://root.cern/doc/master/classTList.html
You can then use GetName() on each elements of both lists and compare them

Hi @couet:

I’ve tried the first option, but ROOT crashes after the first line :frowning:

What I’ve did was the following:
for const auto&& stack_Ge: *GetListOfPrimitives())
and ROOT crashed. (stack_Ge) is my THStack with the TH1Ds.

the following example can be a good start :slight_smile:

{
   auto st1 = new THStack();
   auto h1st1 = new TH1F("h1", "h1", 10, 0 , 1); st1->Add(h1st1);
   auto h2st1 = new TH1F("h2", "h2", 10, 0 , 1); st1->Add(h2st1);
   auto h3st1 = new TH1F("h3", "h3", 10, 0 , 1); st1->Add(h3st1);

   auto st2 = new THStack();
   auto h1st2 = new TH1F("h1", "h1", 10, 0 , 1); st2->Add(h1st2);
   auto h2st2 = new TH1F("h4", "h4", 10, 0 , 1); st2->Add(h2st2);
   auto h3st2 = new TH1F("h3", "h3", 10, 0 , 1); st2->Add(h3st2);

   auto l1 = st1->GetHists();
   auto l2 = st2->GetHists();

   TIter n1(l1);
   TIter n2(l2);

   TH1F    *h1, *h2;
   TObject *o1, *o2;

   while ((o1 = n1())) {
      h1 = (TH1F*) o1;
      printf("%s \n",h1->GetName());
   }

   while ((o2 = n2())) {
      h2 = (TH1F*) o2;
      printf("%s \n",h2->GetName());
   }
}
1 Like

Hi @couet:

thanks for the reply, but I have a question or two, regarding your solution:

  • I don’t understand why there are two while loops, neither do I understand the reason for defining the new variables h1, h2.
  • I don’t understand what the TIter objects n1(l1) and n2(l2) does, and I’ve tried looking in the documentation but I can’t find the part which explains this to me. (my apologies if it is listed but I didn’t see it due to my inexperience)

Thanks again for your help!

As I said it was just an example showing how to loop on THStacks. You can now elaborate for this example. For instance the following version does a printout of the histograms names in each stack. In one loop:

{
   auto st1 = new THStack();
   auto h1st1 = new TH1F("h1", "h1", 10, 0 , 1); st1->Add(h1st1);
   auto h2st1 = new TH1F("h2", "h2", 10, 0 , 1); st1->Add(h2st1);
   auto h3st1 = new TH1F("h3", "h3", 10, 0 , 1); st1->Add(h3st1);

   auto st2 = new THStack();
   auto h1st2 = new TH1F("h1", "h1", 10, 0 , 1); st2->Add(h1st2);
   auto h2st2 = new TH1F("h4", "h4", 10, 0 , 1); st2->Add(h2st2);
   auto h3st2 = new TH1F("h3", "h3", 10, 0 , 1); st2->Add(h3st2);

   auto l1 = st1->GetHists();
   auto l2 = st2->GetHists();

   TIter n1(l1);
   TIter n2(l2);

   TH1F    *h1, *h2;
   TObject *o1, *o2;

   while ((o1 = n1())) {
      h1 = (TH1F*) o1;
      o2 = n2();
      h2 = (TH1F*) o2;
      printf(" %s - %s\n",h1->GetName(),h2->GetName());
   }
}
1 Like

@couet:

Thanks for those hints! I’m still trying out a few combinations to get what I want, but thanks a lot for pointing me in the right direction!