Re: Copy constructor fot TH1F

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue May 19 1998 - 14:37:01 MEST


Chris Jillings wrote:
> 
> Hi,
>     I have the following macro which I run off the command line of 2.00/05
> on Linux. If the last four lines are commented out it works as I would
> expect. When the last four lines are added, the interprter complains about
> not being able to handle the copy constructor...
> 
> Error: Can't call TH1F::TH1F() in current scope
> FILE:/usr/home/chris/analysis/qdc/clicks/./chargespec.C LINE:68
> *** Interpreter error recovered ***
> 
>     Thanks,
> 
> Chris
> 
> sumchargespec(TFile* fin) {
>   char hname[100];
>   sprintf(hname,"qdcintime");
>   TH1F* hintime = (TH1F*)fin->Get(hname);
>   sprintf(hname,"qdcearly");
>   TH1F* hearly = (TH1F*)fin->Get(hname);
>   TCanvas *c1 = new TCanvas("c1","c1",1);
>   c1->SetFillColor(kWhite);
>   TPad* p1 = new TPad("p1","p1",0.02,0.52,0.98,0.98,kWhite);
>   TPad* p2 = new TPad("p2","p2",0.02,0.02,0.98,0.48,kWhite);
>   p1->Draw();
>   p2->Draw();
> 
> 
>   Float_t xlow = 40;
>   Float_t xhigh = 90;
>   Int_t low = hintime->GetXaxis()->FindBin(xlow);
>   Int_t high = hintime->GetXaxis()->FindBin(xhigh);
>   Float_t intimeArea = hintime->Integral(low,high);
>   Int_t low = hearly->GetXaxis()->FindBin(xlow);
>   Int_t high = hearly->GetXaxis()->FindBin(xhigh);
>   Float_t earlyArea = hearly->Integral(low,high);
> 
>   printf("Early area = %f\n",earlyArea);
>   printf("In time area = %f\n",intimeArea);
> 
> 
>   if( intimeArea!=0.0 )
>     hintime->Scale(earlyArea/intimeArea);
> 
>   p1->cd();
>   hearly->Draw();
>   hintime->Draw("same");
> 
>   TH1F* hdiff = new TH1F(hearly);  // TH1F hdiff(hearly); also fails
>   hdiff->Add(hintime,-1.0);
>   p2->cd();
>   hdiff->Draw();
> 
> }
>
Chris,
The copy constructor expects a reference (not a pointer) to an object.
You should replace

 TH1F *hdiff = new TH1F(hearly)
by
 TH1F *hdiff = new TH1F(*hearly).

You can also do (this is valid for any TObject based object)
 TH1F *hdiff = (TH1F*)hearly->Clone()

The later solution is the recommended way to proceed.

Rene Brun



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:33 MET