Accessing TH1D and manipulating them?

Hi!

I have a number of TH1D files in a ROOT file and I would like to read them out one by one and plot them.

I currently have written the following:

void histo_redraw() { TString filename = "file.root"; TFile* file = TFile::Open(filename); TCanvas* c = (TCanvas*) file->Get("c2"); c->ls(); }

This shows me the following:
Screenshot%20from%202019-07-01%2016-31-53

I would like to draw the TH1Ds all into a new canvas and save them. I’ve tried c->GetObject("muon", histoMuon); but it says that there is “no member named GetObject in TCanvas”.

Could anyone give me an idea on how to proceed?

Try something like this:

  TString filename = "file.root";
  TFile* file = TFile::Open(filename);
  TCanvas* c = (TCanvas*) file->Get("c2");
  c->ls();
  c->Draw();
  TH1D *measurement = (TH1D*)c->GetPrimitive("measurement");
  TH1D *Muons = (TH1D*)c->GetPrimitive("Muons");
    (...etc...)
    (...change to another canvas)
  measurement->Draw();
    (...etc...)

Note that if you want to draw the histos in the same “original” canvas (“c”), you should get them all before drawing them, since drawing will clear the canvas (unless you use “same”).

1 Like

Hi @dastudillo:

I somehow get the error that null passed to a callee that requires a non-null argument [-Wnonnull], especially when I’m plotting the Muons and innerPbShielding_Pb210 TH1Ds. Any idea why?

Can you post your code and output?

Ok, those histograms are inside a THStack. To get them from there you can try:

   THStack *hs = (THStack*)c->GetPrimitive("stack_all");
   TH1D *Muons = (TH1D*)hs->GetHists()->FindObject("Muons");
   ...etc
1 Like

It worked! Thanks a lot!

Great, cheers!

  • Daniel
1 Like