Home | News | Documentation | Download

Plot Histogram from leaf

I have this root file with two leaves, one with energy deposition (edep) and kinetic energy (eK). I want to plot them both on one histogram but I do not know how to write the macro for that.

radioprotection_t1.root (35.6 KB)
ROOT Version: root_v6.18.04
Platform: Windows


Try searching the documentation or this forum, there are lots of examples. A simple start:

TFile *f = new TFile("file.root");
TTree *t = (TTree*)f->Get("tree");
t->Draw("x");
t->Draw("y","","same");

of course, using your corresponding file, tree and leaf names.

{
  TFile *f = TFile::Open("radioprotection_t1.root");
  TTree *t101; f->GetObject("radioprotection_ntuple/101", t101);
  TTree *t102; f->GetObject("radioprotection_ntuple/102", t102);
  TTree *t103; f->GetObject("radioprotection_ntuple/103", t103);
  Long64_t n101 = 0, n102 = 0, n103 = 0;
  if (t101) {
    n101 = t101->GetEntries();
    std::cout << std::endl << "t101 entries: " << n101 << std::endl;
    t101->Print();
  }
  if (t102) {
    n102 = t102->GetEntries();
    std::cout << std::endl << "t102 entries: " << n102 << std::endl;
    t102->Print();
  }
  if (t103) {
    n103 = t103->GetEntries();
    std::cout << std::endl << "t103 entries: " << n103 << std::endl;
    t103->Print();
  }
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(1, 2);
  c->cd(1);
  if (t101) t101->Draw("Ek");
  c->cd(2);
  if (t102) t102->Draw("edep");
  c->cd(0);
}

I kind of meant having Ek and Edep on the same graph. This gives one canvas. Is there a way to do that?

I have tried variations but it doesn;t seem to work maybe because it’s windows? @Wile_E_Coyote would really appreciate your help. Thank you so much :slight_smile:

Try:

  // ...
  gROOT->cd(); // any newly created histograms should go here
  TH1D *h_Ek = new TH1D("h_Ek", "Energy;Ek;counts", 100, 0., 50000.);
  h_Ek->SetLineColor(kGreen);
  if (t101) t101->Project("h_Ek", "Ek");
  TH1D *h_edep = new TH1D("h_edep", "Energy;edep;counts", 100, 0., 50000.);
  h_edep->SetLineColor(kRed);
  if (t102) t102->Project("h_edep", "edep * 10000"); // note: "scaled" edep
  THStack *hs = new THStack("hs", "Energy;Ek or edep;counts");
  hs->Add(h_Ek);
  hs->Add(h_edep);
  TCanvas *c_hs = new TCanvas("c_hs", "c_hs");
  hs->Draw("NOSTACK");
  gPad->SetLogy(1);

Is there a windows guide to ROOTCern I can refer to?

There is nothing specific to Windows, almost everything should work the same than on other platforms, excepted building from source. If you have a Windows related ROOT issue, you can simply create a topic on this forum