# Comparing the contents of two TLists

**URL:** <https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116>\
**Category:** Newbie\
**Created:** [July 11, 2019, 11:48am UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116 "2019-07-11T11:48:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 11, 2019, 11:48am UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116/1 "2019-07-11T11:48:25Z")

</div>

Hi!

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

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

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 11, 2019, 11:53am UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116/2 "2019-07-11T11:53:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 11, 2019, 12:07pm UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116/3 "2019-07-11T12:07:01Z")

</div>

Hi @couet:

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

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 11, 2019, 12:42pm UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116/4 "2019-07-11T12:42:16Z")

</div>

the following example can be a good start 🙂

```auto
{
   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());
   }
}

```

---

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 11, 2019, 12:56pm UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116/5 "2019-07-11T12:56:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 11, 2019, 1:14pm UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116/6 "2019-07-11T13:14:12Z")

</div>

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
{
   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());
   }
}

```

---

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 11, 2019, 1:17pm UTC](https://root-forum.cern.ch/t/comparing-the-contents-of-two-tlists/35116/7 "2019-07-11T13:17:38Z")

</div>

@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!
