RE: [ROOT] CINT/pointers in ROOT/TGraphErrors

From: Philippe Canal (pcanal@fnal.gov)
Date: Thu Aug 02 2001 - 21:00:40 MEST


Hi,

> I am trying to copy the pointers of the two TGraphErrors grmean and
> grsigma to the ones I pass to the function, mean1 and sigma1.

In order to strictly do that you need to pass to your function either the
address or a reference to the variable you want to copy the value of
the pointer to.  So you would write:

void regx(TH2F *h8877, char *hname, int colorint, int pflag, TGraphErrors&
	*mean1, TGraphErrors& *sigma1, char *name1, char *name2)
{
....
	mean1 = grmean;
....
}

What you code does is to copy the __objects__ themself.  This would work
in most C++ case; however most ROOT object to do copy themself very well.

So a better version of your code would be 
{ main function 
.....
TGraphErrors *recomean = 0;
TGraphErrors *recosigma = 0;
regx(pt_genrec,"pt_genrec",4,1,recomean,recosigma,"Mean Pt with Fit",
	"Sigma Pt");
....

void regx(TH2F *h8877, char *hname, int colorint, int pflag, TGraphErrors&
	*mean1, TGraphErrors& *sigma1, char *name1, char *name2)
{
....
	mean1 = grmean->Clone(name1);
....


> recomean->Draw("AP");
> recomean->GetHistogram()->SetXTitle("Pt of Generated Electrons");
>	^--Line 771

TGraph::GetHistogram return a non-zero value only after the graph as
been painted.  So you need to do:

  recomean->Draw("AP");
  c11->Update();
  recomean->GetHistogram()->SetXTitle("Pt of Generated Electrons");

Cheers,
Philippe.

-----Original Message-----
From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Shawn Kwang
Sent: Thursday, August 02, 2001 12:09 PM
To: roottalk@pcroot.cern.ch
Subject: [ROOT] CINT/pointers in ROOT/TGraphErrors


Roottalk members,

First here is the code in question:
==================================================================
...

void regx(TH2F *h8877, char *hname, int colorint, int pflag, TGraphErrors
	*mean1, TGraphErrors *sigma1, char *name1, char *name2)
{

<snip>

  TGraphErrors *grmean = new TGraphErrors(nhitbins,x,y,ex,ey);

  TGraphErrors *grsigma = new TGraphErrors(nhitbins,x,sigmay,ex,errey);

  //copy graphs to passed variables
  *mean1 =  *grmean;		<-----Problem???
  mean1->SetName(name1);
  *sigma1 = *grsigma;		<-----Problem???
  sigma1->SetName(name2);

}

{ main function (not actually main(), but another function which runs
everything)
<snip>

TGraphErrors *recomean = new TGraphErrors();
TGraphErrors *recosigma = new TGraphErrors();
regx(pt_genrec,"pt_genrec",4,1,recomean,recosigma,"Mean Pt with Fit",
	"Sigma Pt");

<snip>

TCanvas *c11=new TCanvas("c11","Mean Pt of PMCS  and RECO electrons");
recomean->Draw("AP");
recomean->GetHistogram()->SetXTitle("Pt of Generated Electrons");
		^--Line 771

...
}
=================================================================

When I compile this program using KCC and ROOT header files, the program
runs fine.  When I try to run the program as a ROOT macro it crashes at
the GetHistogram() line.

Here is the error ROOT gives:
Error in <TGraphErrors::PaintGraph>: illegal number of points (0)
Error: illegal pointer to class object GetHistogram() 0x0 134
FILE:/home/skwang/CAPR/CAPR.C LINE:771
*** Interpreter error recovered ***


I suspect the problem is where I have inserted the two "Problem" arrows.  
I am trying to copy the pointers of the two TGraphErrors grmean and
grsigma to the ones I pass to the function, mean1 and sigma1.  The reason
I am doing it this way is because the function regx is run fives times on
five different histograms, thereby producing ten TGraphErrors.  From what
I can tell the pointers mean1 and sigma1 are not being assigned correctly,
and do not point to grmean and grsigma respectively.  This means that
recomean and recosigma (in the main function) are effectively empty and
when I try to access their histograms, the program crashes.

What I want is how to assign the pointers mean1 and sigma1 to the
TGraphErrors that are created by grmean and grsigma.  I tried using
Clone() but that gives the same error.

i.e. mean1 = (TGraphErrors*)grmean->Clone();

I suspect the problem is with my understanding of pointers in C++ and how
Root handles them.  Finally, if there is a better way to do what I do
above please let me know.

Thanks in advance,
Shawn Kwang

--
					skwang@wam.umd.edu
					skwang@glue.umd.edu



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:54 MET