Saving generic TH1 in TTree


I have an interesting problem which I cannot obviously solve myself, so I have to turn to experts. I need to save large number of histograms in ROOT file and then to retrieve individual histograms in a fastest possible way. Saving them like we usually do as entries in the directory tree is not efficient, there is an overhead for creating directory entries, keys, etc., and searching in directories is slower than we would like when we read the data back.

Apparently what we want is a kind of direct access by index which TTree provides. It would be nice if we could store all histograms as entries in the tree branches, but apparently it’s not an easy task. Main problem is that we have a large variety of histogram types as well (TH1I, TH2S, TProfile, etc., practically every histogram type is possible). All histogram types have a common base class TH1, but apparently it is not possible to store generic TH1 type in a tree (or I fail to see it). I did try to create a non-splitting branch with TH1 type but it fails at run time. One option I guess still available is to create separate branch for each individual histogram type but I’d like to avoid having 20 branches, it will complicate our indexing a lot.

So can anyone tell me if what I want (storing all types of histograms in one branch) is possible at all?