Copy existing histograms into a tree

Hi! I have a file with many existing histograms and I’d like to put them all into a Tree. There’s too many for me to manually add them all with SetBranchAddress etc. I’ve had a look at copyFiles.C but I can’t see how to adapt it to change the structure of the file.

I need them to be in a tree because a macro I want to use requires a chain of these trees as input.

Sorry I haven’t offered an attempt as a starting point, I really don’t know where to start! Initialising all the histograms and setting the branch addresses, I don’t know how to get the input for those functions. For looping over all histograms I guess it’ll be this style of thing:

  while ((key = (TKey*)nextkey())) {
          const char *classname = key->GetClassName();
         ...
         }

Thanks for any help or tips! :slight_smile:


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi @KatR,
sorry for the late reply! TTree is a columnar data format, i.e. in first approximation you can think of its contents as arranged on a table: where do you want to put your histograms? One per row with one columns, one per column with one row, or do you need several columns with N rows each?

In any case, to add a histogram to a TTree, TTree::Branch might be the simplest approach – you don’t have to write it many times manually, you can use a for loop:

for (auto *h : histograms) {
  tree->Branch(h->GetName(), &h);
}
tree->Fill();

…or something of the sort depending on your answer to the question above.
Hope this helps!
Cheers,
Enrico