Why can't I access this histogram from the branch?

I am absolutely sure I’m missing something obvious, but I have a root file with two trees and histograms in their branches. I open the file, get the tree and then start trying to do things with the histograms in those branches.

TFile *f1 = TFile::Open("Histos.root");
if (!f1 || f1->IsZombie())
{
    printf("Can't open f1!\n");
    return;
}
vector<int> *h1;
 TTree *t1 = (TTree*)f1->Get("HistoAlgo");
   t1->SetBranchAddress("m_nominal",&h1);

h1->SetDirectory(gROOT);

I get the following errors:
test.C:47:5: error: no member named ‘SetDirectory’ in 'std::vector<int, std::allocator >'

h1->SetDirectory(gROOT);

I have also tried using TH1F* h1 = (TH1F*)gDirectory->Get(“m_nominal”); but this does not find m_nominal, which I have checked does indeed exist in the file.

Thanks for any help, sorry if this is a really silly syntax error!


Please read tips for efficient and successful posting and posting code

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


I guess @pcanal can help you.

You probably mean vector<TH1F*>?
Are you sure of what is in your ROOT file? Can you post the result of t1->Print()?

But h1 is ntrack of a jet, which should be a vector of integers, right? Unless I misunderstood what I’m supposed to declare there and it’s not the variable in the branch. The print statement outputs:

******************************************************************************
*Tree    :HistoAlgo : HistoAlgo ntuple                                       *
*Entries : 265614114 : Total =     39357976751 bytes  File  Size = 7385401049 *
*        :          : Tree compression factor =   5.33                       *
******************************************************************************
*Br    0 :m_nominal : vector<int>                                            *
*Entries :265614114 : Total  Size= 3899929411 bytes  File Size  = 656192313 *
*Baskets :    87598 : Basket Size=   25600000 bytes  Compression=   5.94     *
*............................................................................*
*Br    1 :h1_central_rjet_Ntrk_Tracking1 : vector<int>                       *
*Entries :265614114 : Total  Size= 3901778673 bytes  File Size  = 659151539 *
*Baskets :    87672 : Basket Size=   25600000 bytes  Compression=   5.92     *
*............................................................................*
*Br    2 :h1_central_rjet_Ntrk_Tracking2 : vector<int>                       *
*Entries :265614114 : Total  Size= 3901777373 bytes  File Size  = 659154800 *
*Baskets :    87662 : Basket Size=   25600000 bytes  Compression=   5.92     *
*............................................................................*
*Br    3 :h1_central_rjet_Ntrk_Tracking3 : vector<int>                       *
*Entries :265614114 : Total  Size= 3901776723 bytes  File Size  = 659220584 *
*Baskets :    87657 : Basket Size=   25600000 bytes  Compression=   5.92     *
*............................................................................*
*Br    4 :h1_central_rjet_Ntrk_Tracking4 : vector<int>                       *
*Entries :265614114 : Total  Size= 4073954581 bytes  File Size  = 703499037 *
*Baskets :    90692 : Basket Size=   25600000 bytes  Compression=   5.79     *
*............................................................................*
*Br    5 :m_nominal_pt : vector<int>                                         *
*Entries :265614114 : Total  Size= 3900193673 bytes  File Size  = 763238385 *
*Baskets :    87611 : Basket Size=   25600000 bytes  Compression=   5.11     *
*............................................................................*
*Br    6 :h1_central_rjet_pt_Tracking1 : vector<int>                         *
*Entries :265614114 : Total  Size= 3901599609 bytes  File Size  = 764281191 *
*Baskets :    87643 : Basket Size=   25600000 bytes  Compression=   5.10     *
*............................................................................*
*Br    7 :h1_central_rjet_pt_Tracking2 : vector<int>                         *
*Entries :265614114 : Total  Size= 3901597689 bytes  File Size  = 764279025 *
*Baskets :    87628 : Basket Size=   25600000 bytes  Compression=   5.10     *
*............................................................................*
*Br    8 :h1_central_rjet_pt_Tracking3 : vector<int>                         *
*Entries :265614114 : Total  Size= 3901597433 bytes  File Size  = 764282700 *
*Baskets :    87626 : Basket Size=   25600000 bytes  Compression=   5.10     *
*............................................................................*
*Br    9 :h1_central_rjet_pt_Tracking4 : vector<int>                         *
*Entries :265614114 : Total  Size= 4073768837 bytes  File Size  = 984180219 *
*Baskets :    90658 : Basket Size=   25600000 bytes  Compression=   4.14     *
*............................................................................*```

Actually that’s weird that pt is showing as a vector int, that’s not right… I screwed up making the tree I think. But when I’ve fixed that I’ll still have the same format and still need a way to access it

Well, if they are vector<int>, then they are not histograms, and you cannot call h1->SetDirectory(gROOT); you have to read the vector and then fill an histogram with their values. Something like this:

   TH1::AddDirectory(false);
   TH1I h1 = new TH1I("h1", "my histogram", 100, 0, 0);
   vector<int> *v1 = nullptr;
   TTree *t1 = (TTree*)f1->Get("HistoAlgo");
   t1->SetBranchAddress("m_nominal",&v1);
   for(const auto& value: v1) {
      h1->Fill(value);
   }
   h1->Draw();
1 Like

Oh dear, I knew it would be something silly… Thanks, that was driving me crazy! I think I’m hugely overcomplicating what I need to do, I’ll have a think about whether I want to store histos or variables. Thanks again!

1 Like

You’re very welcome! And note that saving histograms in files is pretty straightforward, but this depends on what you want to do with your data