Problems with a pointer

Hi everybody
I’m new in root and i have a problem
Here is my code in C. It’s name is histo.C
{ gROOT->Reset();
#include “TInterpreter.h”
#include “TCanvas.h”
#include “TSystem.h”
#include “TFile.h”
#include “TH2.h”
#include “TNtuple.h”
#include “TPaveLabel.h”
#include “TPaveText.h”
#include “TFrame.h”
#include "TSystem.h"
TCanvas * c1 = new TCanvas(“c”, “c”, 600, 600);
//c1->Divide(1,3);
c1->SetGridx();
c1->SetGridy();
c1->cd(1);
TFile f1(“MET_allEvents_170to300.root”);
TH1D h1=(TH1D)f1->Get(“1”);
h1.SetLineStyle(1);
h1.SetLineColor(4);
//h1->Scale(137);
h1.Draw(“hist”);

And the problem is this
root [0]
Attaching file C:/Users/fernando/Desktop/Facultad/LME/170to300/MET_allEvents_170
to300.root as _file0…
Processing C:\Users\fernando\Desktop\root_v5.34.32\macros\fileopen.C…
root [2] .x histo.C
Error: illegal pointer to class object h1 0x0 1429 C:\Users\fernando\Desktop\Fa
cultad\LME\170to300\histo.C(22)
!!!Dictionary position not recovered because G__unloadfile() is used in a macro!
!!
*** Interpreter error recovered ***
root [3]
I don’t know why is an illegal pointer. The histo is fine when i open it in the canvas but i can’t do this operation
I hope you can help me
Thank you

It looks like the histogram, that you try to retrieve from the ROOT file, does not exist.
Moreover, the line:
TH1D h1=(TH1D)f1->Get(“1”);
suggests that you try to retrieve a histogram with the name “1” which is not really a proper name.
Try “f1.ls();” in order to learn what’s inside of this ROOT file.

Thanks
Actually I tried this
root [0]
Attaching file C:/Users/fernando/Desktop/Facultad/LME/170to300/MET_allEvents_170
to300.root as _file0…
Processing C:\Users\fernando\Desktop\root_v5.34.32\macros\fileopen.C…
root [2] TFile f1(“MET_allEvents_170to300.root”);
root [3] f1.ls();
TFile** MET_allEvents_170to300.root
TFile* MET_allEvents_170to300.root
KEY: TCanvas ;1
root [4]
I tried to write in C the name “;1” but it didn’t help

This ROOT file seems to contain a single TCanvas without a name (so, do not try to fight against this broken file, the one who created it should re-create it with [url=https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2 names given to objects inside[/url]).

Oh ok now I understood
Thank you!

Hello,

since I have a quite similar problem, I didn’t post a new topic.

I’m facing the error “Error: illegal pointer to class object h_sum 0x0 1381” in the line “h_sum->Add(h);” in the macro below. What would be cause to the error?

Any help is appreciated.

Cheers,
Bora

{
  TFile* f_out = new TFile("OUTPUT.root", "RECREATE");

  TFile *f_in = TFile::Open("INPUT.root", "READ");
  if (!f_in) return;
  
  f_in->cd("DIR_NAME");

  TH2F *h_sum;
  int max_k=999;
  const char* underscore    = "_";
  const char* hname_prefix  = "prefix";
  for (int i = 0; i < 2; i++) {
      
      for (int k = 0; k <= 1; k++) {
          TH2F *h;
          gDirectory->GetObject(TString::Format("%s%d%s%d", hname_prefix, i, underscore, k), h);
          if (!h) continue;
          h_sum->Add(h);
      }
  }

  f_out->cd();

  h_sum->Write("h_sum");
  f_out->Close();
    
  delete h;
  delete h_sum;
}

Try: // ... TH2F *h_sum = ((TH2F *)0); // ... if (h_sum) h_sum->Add(h); else h_sum = ((TH2F *)(h->Clone("h_sum"))); delete h; // not needed any more // ...

Thank you very much.