Last active
November 1, 2017 13:36
-
-
Save raggleton/b9ff6fb3fe8fd8486827 to your computer and use it in GitHub Desktop.
How to loop over a TList / histograms in a THStack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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