Skip to content

Instantly share code, notes, and snippets.

@KonstantinSchubert
Created December 5, 2014 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KonstantinSchubert/bb586f15b85a06b70869 to your computer and use it in GitHub Desktop.
Save KonstantinSchubert/bb586f15b85a06b70869 to your computer and use it in GitHub Desktop.
CERN ROOT: A function that gets the first tree out of a .root -file.
TTree* getAnyTree(TDirectory* dirHandle){
// Returns a pointer to a TTree from within the file. Useful if you know there is only one tree in the file, but don't knowe which and where exactly.
// within ROOT, a TTree is a TObject and a TDirectory is a TObject. But a Tree is not a TDirectory and vice versa.
TObject* objectHandle = NULL;
do{
const char* key = dirHandle->GetListOfKeys()->At(0)->GetName(); // Get the first key in the directory
objectHandle = dirHandle->Get(key); // Get the first object.
dirHandle = dynamic_cast<TDirectory*> (objectHandle); // try to cast object into directory
}while( dirHandle!=NULL); // If it can be casted into a TDirectory we didn't yet find what we were looking for.
return dynamic_cast<TTree*> (objectHandle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment