Skip to content

Instantly share code, notes, and snippets.

@raggleton
Last active November 1, 2017 13:36
Show Gist options
  • Save raggleton/b9ff6fb3fe8fd8486827 to your computer and use it in GitHub Desktop.
Save raggleton/b9ff6fb3fe8fd8486827 to your computer and use it in GitHub Desktop.
How to loop over a TList / histograms in a THStack
"""Iterate over hists in a THStack.
Turns out it's way easier than in C++
"""
for hist in my_stack.GetHists():
print type(hist)
print hist.Integral()
/**
* Snippet taken from code - goes through each hist in a THStack and prints Integral of that hist.
* But also handy as a reminder of how to loop through a TList.
* Important notes:
* - I have no idea what is going on. Think it auto gets the object somehow when you call next().
* - next() seems to return the object itself, via a TObject pointer, so you need to cast back to whatever it was. Hope you know!
*
*/
void checkStackNorm(THStack * st) {
TList * histKeys = st->GetHists();
histKeys->Print();
TIter next(histKeys);
TObject* object = 0;
while ((object = next()))
{
cout << "Got an object" << endl;
cout << ((TH1*)object)->Integral() << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment