Want to create a 2D histogram from two different branches in TTree

Hi, new to ROOT! I am trying to create a 2D histogram using data from two different branches. We are getting a 2D histogram, but we aren’t sure if it is filling correctly, because it does not look the same as when we made the histogram using a different software.

So far this is what we have, with the doppler_energy branch being branch12 and doppler_energy_corrected being branch 14 declared up above!

All->SetBranchAddress("doppler_energy",&doppler_energy);

for(int i = 0; i < branch12->GetEntries();i++){
    All->GetEntry(i);
    
}

All->SetBranchAddress("doppler_energy_corrected", &doppler_energy_corrected);

for(int i = 0; i < branch14->GetEntries();i++){
    All->GetEntry(i);
    
    h9->Fill(doppler_energy_corrected,doppler_energy);
    
}


//Drawing and saving the histogram
//cc->SetLogy(); //sets a logrithmic axis
h9->Draw(" ");
h9->SetLineColor(kBlue);
h9->SetMarkerColor(kRed);
cc->SaveAs("doppler_energy.pdf");

h9->Write();

Please read tips for efficient and successful posting and posting code

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


Hi @lfisher5,
indeed that code has some weird constructs, but if I parse it correctly, it should in the end do what you expect.

This loads those values into the corresponding variable for each of the entries, but does nothing with them.

This actually loads entry values and, for each of them, fills the histogram.

The histogram might look different e.g. because of (missing) normalization or because the axis ranges are different. You can check that and also inspect the bin contents manually (e.g. after replacing the data with some dummy data that you can simply verify) using the TH2 methods (expand the “Public methods inherited from TH2” at that link).

A simpler way to obtain the same 2D histogram is RDataFrame. This is all the code you need:

ROOT::RDataFrame df("treename", "filename.root");
auto h9 = df.Histo2D("doppler_energy_corrected", "doppler_energy");

h9->Draw(...);

Hope this helps!
Enrico