Statistics box illegal pointer

I’m getting the following error

Error: illegal pointer to class object stat_tautau[0] 0x0 667 FILE:bg_pt_comp.C

which is for the line

stat_tautau [0]->SetOptStat(1110);
near the end of my macro file. I’m trying to put statistic boxes onto my histogram. From what I’ve read it seems that updating my graph in right place may be the problem.

Jody.

gROOT->Reset();

#include “TFile.h”
#include “TH1.h”
#include “TCanvas.h”
#include “TStyle.h”
#include “TPostScript.h”
#include “TLegend.h”
#include “TROOT.h”
#include “TGraphErrors.h”
#include “TDatime.h”

using std::cout;
using std::endl;
using std::vector;

void bg_pt_comp(){

TFile *tautau_MC = new TFile (“tautauMCcut_160707.root”);
TFile *ttbar_MC = new TFile (“ttbar_MC_bgcut_160707a.root”);
TFile *w_MC = new TFile (“wtop_MCc_160707.root”);

//making histograms for all mass regions
TH1F pt_tautau_c1 = (TH1F) tautau_MC->Get(“pt_dimu_c1”);
TH1F pt_ttbar_c1 = (TH1F) ttbar_MC->Get(“pt_dimu_c1”);
TH1F pt_w_c1 = (TH1F) w_MC->Get(“pt_dimu_c1”);
TH1F pt_tautau_c2 = (TH1F) tautau_MC->Get(“pt_dimu_c2”);
TH1F pt_ttbar_c2 = (TH1F) ttbar_MC->Get(“pt_dimu_c2”);
TH1F pt_w_c2 = (TH1F) w_MC->Get(“pt_dimu_c2”);
TH1F pt_tautau_c3 = (TH1F) tautau_MC->Get(“pt_dimu_c3”);
TH1F pt_ttbar_c3 = (TH1F) ttbar_MC->Get(“pt_dimu_c3”);
TH1F pt_w_c3 = (TH1F) w_MC->Get(“pt_dimu_c3”);

TPaveStats* stat_ttbar[3];//pointers to statistics
TPaveStats* stat_tautau[3];
TPaveStats* stat_w[3];

//cout << Ntautau_c1+Ntautau_c2+ Ntautau_c3<< endl;

pt_tautau_c1->SetLineColor(2);
pt_tautau_c1->SetLineWidth(2.0);
pt_tautau_c1->SetMarkerStyle(8);
pt_tautau_c1->SetMarkerColor(2);
pt_tautau_c1->SetMarkerSize(0.7);
pt_tautau_c1->SetStats(kTRUE);

pt_ttbar_c1->SetLineColor(1);
pt_ttbar_c1->SetLineWidth(2.0);
pt_ttbar_c1->SetMarkerStyle(8);
pt_ttbar_c1->SetMarkerColor(1);
pt_ttbar_c1->SetMarkerSize(0.7);
pt_ttbar_c1->SetStats(kTRUE);

pt_w_c1->SetLineColor(4);
pt_w_c1->SetLineWidth(2.0);
pt_w_c1->SetMarkerStyle(8);
pt_w_c1->SetMarkerColor(4);
pt_w_c1->SetMarkerSize(0.7);
pt_w_c1->SetStats(kTRUE);

gROOT->SetStyle(“Plain”);
TCanvas *myCanvas = new TCanvas(“myCanvas”,“Comparison”);
myCanvas->Divide(1,3);
myCanvas->cd(1);

myCanvas_1->Update();
stat_tautau [0]=(TPaveStats*) pt_tautau_c1->GetListOfFunctions()->FindObject(“stats”);
stat_ttbar [0]=(TPaveStats*) pt_ttbar_c1->GetListOfFunctions()->FindObject(“stats”);
stats_w [0]=(TPaveStats*) pt_w_c1->GetListOfFunctions()->FindObject(“stats”);

// stat_tautau [1]=(TPaveStats*) pt_tautau_c2->GetListOfFunctions()->FindObject(“stats”);
// stat_ttbar [1]=(TPaveStats*) pt_ttbar_c2->GetListOfFunctions()->FindObject(“stats”);
// stat_w [1]=(TPaveStats*) pt_w_c2->GetListOfFunctions()->FindObject(“stats”);

// stat_tautau [2]=(TPaveStats*) pt_tautau_c3->GetListOfFunctions()->FindObject(“stats”);
// stat_ttbar [2]=(TPaveStats*) pt_ttbar_c3->GetListOfFunctions()->FindObject(“stats”);
// stat_w [2]=(TPaveStats*) pt_w_c3->GetListOfFunctions()->FindObject(“stats”);

pt_tautau_c1 ->Draw(“ep”);
pt_ttbar_c1->Draw(“ep same”);
pt_w_c1->Draw(“ep same”);
myCanvas_1->Update();

pt_tautau_c1->SetName(“pt_tautau_c1”); //need to set names so TLegend doesnt make
pt_ttbar_c1->SetName(“pt_ttbar_c1”); //them both same colour
pt_w_c1->SetName(“pt_w_c1”);

TLegend *leg_c1 = new TLegend(0.8,0.7,0.9,0.4,"");//position of corners of legend
leg_c1->AddEntry(“pt_ttbar_c1”,"ttbar ",“l”);
leg_c1->AddEntry(“pt_tautau_c1”,“tautau”, “l” );
leg_c1->AddEntry(“pt_w_c1”,“w”,“l”);
leg_c1->Draw();
myCanvas_1->RedrawAxis();

myCanvas_1->Update();
stat_tautau [0]->SetOptStat(1110);

myCanvas_1->RedrawAxis();

}//end of bg_pt_comp

Yes you need a gPad->Update() to access the PaveStat from the histogram. But looking at your code it seems you have it.
Can you send a small runable macro reproducing the problem ?

This is a but down problem that throughs up the same error

Error: illegal pointer to class object stat_tautau[0] 0x0 667 FILE:small.C LINE:13

void small(){
TFile tautau_MC = new TFile (“tautauMCcut_160707.root”);
TH1F pt_tautau_c1 = (TH1F) tautau_MC->Get(“pt_dimu_c1”);
TPaveStats
stat_tautau[3];
gROOT->SetStyle(“Plain”);
TCanvas myCanvas = new TCanvas(“myCanvas”,“Comparison”);
myCanvas->Divide(1,3);
myCanvas->cd(1);
myCanvas_1->Update();
stat_tautau [0]=(TPaveStats
) pt_tautau_c1->GetListOfFunctions()->FindObject(“stats”);
pt_tautau_c1 ->Draw(“ep”);
myCanvas_1->Update();
stat_tautau [0]->SetOptStat(1110);
myCanvas_1->RedrawAxis();
}

I was obliged to modify your example because I do not have the file you are using in the example you sent. In my modified version I am using the histogram “hpx” in the file “hsimple.root” generated by the macro “$ROOTSYS/tutorials/hsimple.C”. My macro is the following and works perfectly with the latest ROOT version on Linux. Can you try it in your environment ?

{
        gROOT->SetStyle("Plain");
        TFile *file = new TFile ("hsimple.root");
        TH1F *hpx = (TH1F*) file->Get("hpx");
        TPaveStats* stat[3];
        TCanvas *myCanvas = new TCanvas("myCanvas","Comparison");
        myCanvas->Divide(1,3);
        myCanvas->cd(1);
        myCanvas_1->Update();
        stat[0] = (TPaveStats*) hpx->GetListOfFunctions()->FindObject("stats");
        hpx ->Draw("ep");
        myCanvas_1->Update();
        stat[0]->SetOptStat(1110);
        myCanvas_1->RedrawAxis();
}

I’ve tried the code using the hsimple.root from the tutorial folder and found it works fine. When using my .root file, and others I have, it doesnt work.

hsimple.root has some histograms and 2D graphs, as well as an ntuple with a few branches.
My root file only has histograms.

It seems that some difference between the files means it gives this error.

Thanks,
Jody.

The differences you mentionned should not be the explanation of the problem. When you do:

stat[0] = (TPaveStats*) hpx->GetListOfFunctions()->FindObject("stats");

is stat[0] defined ? may be your histograms simply do not have any statistics stored ?

My understanding from the root user guide is that unless you explicity use TH1::SetStats(kFALSE) histograms will have statistics in them. The program used to generate the root files doesn’t seem to do this (while it doesn’t actively do anything to add statistics to the histograms either.

Using couts I found that
stat[0] 0
&stat[0] 0xb11cf38
stat[0] 0
which means that it isn’t pointing to anything correct? So something is wrong in
(TPaveStats
) hpx->GetListOfFunctions()->FindObject(“stats”);

Yes it behaves just like your histograms have no stats. To go further we need to have a running example showing the problem.

When I look at the histogram through the root TBrowser it include a stats box. Is this generated when opening it then?

For a working example do you mean you need the root file I’m working with. I’ve attached it here.
tausimple.root (34.7 KB)

ok I see. Things are done in the wrong order. Here it is:

void small(){
        TFile *tautau_MC = new TFile ("tausimple.root");
        TH1F *pt_tautau_c1 = (TH1F*) tautau_MC->Get("pt_dimu_c1");
        TPaveStats* stat_tautau[3];
        gROOT->SetStyle("Plain");
        TCanvas *myCanvas = new TCanvas("myCanvas","Comparison");
        myCanvas->Divide(1,3);
        myCanvas->cd(1);
        myCanvas_1->Update();
        pt_tautau_c1 ->Draw("ep");
        myCanvas_1->Update();
        stat_tautau [0]=(TPaveStats*) pt_tautau_c1->GetListOfFunctions()->FindObject("stats");
        stat_tautau [0]->SetOptStat(1110);
        myCanvas_1->RedrawAxis();
}

Brilliant! I don’t understand why this order works but I can always put it down to root being fussy.

Thanks a lot.

Jody

It is simply because the stats box is created at the painting time. That is why you first need to Draw() the histo to put it in the TPad list and then perform an Update() to force the painting (and therefore the stats box creation). I read to quickly your macro at first and when I saw that you had put an Update() I thought you knew about this mechanism… My fault … sorry.

thank you for your time. Now to find the next bug…