Error when doing projeciont: illegal pointer to class object

Dear experts,
I’m running this script:

#include
#include
#include
#include
#include
#include “TF1.h”
#include “TH1.h”
#include “TH2.h”
#include “TRandom3.h”
#include “TGraph.h”
#include “TGraphErrors.h”
#include “TFile.h”
#include “TVirtualFitter.h”
#include “MiniT.h”
#include "TCutG.h"
using namespace std;
TFile ff(“testWm_et20_d0.root”);
TH2F* tofit=(TH2F*)ff.Get(“EtvsMt”);
TH1F ppp = (TH1F)(tofit->ProjectionY());

I get the error:
Error: illegal pointer to class object tofit 0x0 177 ttt.C:19

The same instructions are correct when using the prompt.
Thanks for your help,
Marianna.

the pointer tofit=0 means that the object named “EtvsMt” does not exist in your file

Rene

1 Like

Precisely speaking, I believe, tofit=0 means that the object named “EtvsMt” does not exist in the main directory of your file “ff”. If it exists in some kind of a subfolder, it will NOT be found by “ff.Get” (you need to find it “manually” yourself). See, for example, here: [url]Draw the 2-D histograms which in the subFolder

But, I get the error when I compile :
.L test.C+, not in esecution. So the error is independent from the “ff” root file.

Bye,
Marianna.

All your three variables (“ff”, “tofit” and “ppp”) are global variables and, according to the source code that you showed, they are initialized immediately when the “object code” gets loaded (that’s what “.L” does).

Ok, I understand, thanks for the explanation.
Anyhow, the histogram is present in the root file and I can execute the lines of the macro on the prompt
without errors.
Bye, Marianna.

Try to execute:
root [0] TFile ff(“testWm_et20_d0.root”);
root [1] ff.ls();
and see what exactly it says about the “EtvsMt”.
I’ve just noticed a bug in your code.
Replace:
TH1F ppp = (TH1F)(tofit->ProjectionY());
with:
TH1D *ppp = tofit->ProjectionY();
See http://root.cern.ch/root/html/TH2.html#TH2:ProjectionY for details.

Doing ff.ls() I get
KEY: TH2F EtvsMt;1
Also substituting with TH1D *ppp = tofit->ProjectionY();
I get this error:
Error: illegal pointer to class object tofit 0x0 1435 ttt.C:19
A precisation: I compile with .L ttt.C+
if I compile simpy .L ttt.C I get no error.

I have no idea what happens then.
The last thing you might try (though I don’t think it will help) is to replace:
TH2F* tofit=(TH2F*)ff.Get(“EtvsMt”);
with:
TH2F tofit=dynamic_cast<TH2F>(ff.Get(“EtvsMt”));
One more thing to try … “.L ttt.C++” (double “+”).

Still, I get the same error applying both the suggestions…

If the root file is small, attach it here (I can try to “inspect” it).
If not, start to:
cry :cry:
or pray [-o<
or both :mrgreen:

VIVE L’AMOUR!
you face a “rootcint” limitation. :-({|=
Modify your code:

#ifndef __MAKECINT__
TH1D *ppp = tofit->ProjectionY();
#else
TH1D *ppp;
#endif

Next time, provide us with the [color=#800080]F[/color][color=#408040]U[/color][color=#FFBF00]L[/color][color=#00FFFF]L[/color] error message that you get. This will make it [color=#FF0000]M[/color][color=#FF4080]U[/color][color=#FFBF80]C[/color][color=#80FF00]H[/color] easier for people to help you. :mrgreen:
A pitiful case, am I not?
Pepe Le Pew.