# \`AssertClass\` for \`TClonesArray\`

**URL:** <https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733>\
**Category:** Newbie\
**Created:** [September 20, 2024, 1:37pm UTC](https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733 "2024-09-20T13:37:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [September 20, 2024, 1:37pm UTC](https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733/1 "2024-09-20T13:37:29Z")

</div>

I see that TCollection has an [`AssertClass`](https://root.cern/doc/master/classTCollection.html#a54d17025bcc640ac5a4f0ffa64cc819a) member function to assert that all objects derive from some specific class. Good!  
From looking at the implementation, it iterates over all elements. For the generic case, this is fine!

If I have a `TClonesArray`, then it should be possible to do this more efficient, right?

I guess, I could do something like

```cpp
assert(clonesarray.GetClass()->InheritsFrom(expectedclass));

```

Is this the recommended way?

* * *

_ROOT Version:_ master  
_Platform:_ any  
_Compiler:_ any

* * *

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 20, 2024, 1:58pm UTC](https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733/2 "2024-09-20T13:58:36Z")

</div>

This is a good first steps however there are (totally unsupported) weird ways to insert objects of the wrong type into a `TClonesArray`.

---

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [September 20, 2024, 2:25pm UTC](https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733/3 "2024-09-20T14:25:25Z")

</div>

If those weird ways are totally unsupport, then they’re that, right?

So unless I want to support those, my suggested way should be fine?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 20, 2024, 3:01pm UTC](https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733/4 "2024-09-20T15:01:35Z")

</div>

It depends. What is the purpose of calling this tests?

---

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [September 21, 2024, 6:37pm UTC](https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733/5 "2024-09-21T18:37:26Z")

</div>

Purpose: Basically to assert (as in: check for programming error) in a library that a passed in TCloneArray actually has a class derived from some expected one. Especially to later on perform an efficient `static_cast`.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 23, 2024, 4:47pm UTC](https://root-forum.cern.ch/t/assertclass-for-tclonesarray/61733/6 "2024-09-23T16:47:27Z")

</div>

If it is an assert that is run in debug mode then I would still do the full check. If it is an on-all-the-time check then indeed the simpler check should be enough.
