Broken TDirectoryFile::FindObjectAny and TDirectoryFile::FindKeyAny

Although it is not recommended, I saw many cases in which people store, in a single file, many histograms (or graphs, or even some “event” objects) which have the same “name”, but they differ in the “cycle” number (I did it myself, too). Each histogram (or graph) keeps a “sub-sample” of data. In the end, you may need to analyze/compare different “sub-samples”, or you may want to add them all (to get the “total result”).
One may argue that, in such cases, one should create a TTree with a branch that keeps these histograms (or graphs), but almost nobody does that.
It is easier to write histograms (or graphs) without changing their “names”, multiple times into a file → well; reading them back can be a bit tricky, as one can see in this thread …

Two small source code examples which utilize “GetObject” and/or “Get”:

In more complex cases, looping through the list of keys of a file (plus “TKey::ReadObj” or “TKey::ReadObjAny”) should always be safe (see also another example: post 5 in “On using a TList to store TH1 / TGraph objects?”).

1 Like