Re: TClonesArray "=" operator

From: Paul Russo <russo_at_fnal.gov>
Date: Tue, 12 Sep 2006 11:42:19 -0500


Diego,

Change your MyTest constructor from:

MyTest::MyTest(){
  fContainerTest = TClonesArray("TH1F",1000); }

to:

MyTest::MyTest()
: fContainerTest("TH1F", 1000)
{
}

By the time you get to the body of your constructor all data members of a class type have already had their default constructors called, so it is too late for a TClonesArray. You must use the member initializer syntax to change the constructor call for the TClonesArray to be a non-default call.

Diego Faso wrote:
> Hello, I'm using TClonesArray and I have some problem with the use of
> the "=" operator: I need to have a TClonesArray object as a data-member
> of my class, and then declare the contained-class-name in my constructor
> (macro "TestTClonesArray_Class.C").
> Running the TestTClonesArray_Class.C macro I get the following
> error message:
> Error in <TClonesArray::operator=>:
> cannot copy TClonesArray's when classes are different
>
> Since I can not call TClonesArray constructor in my header file,
> I tried to use a TClonesArray pointer as data-member
> (TestTClonesArray_Class_Pointer.C).
> It works, but I'd rather use "TClonesArray fContainerTest" instead of
> "TClonesArray *fContainerTest", since adding new objects is easier.
>
> The attached macro "TestTClonesArray.C" is simply a test of TClonesArray.
>
> In short my question is:
> How can I fix the "TestTClonesArray_Class.C" in order to use
> "TClonesArray fContainerTest" instead of "TClonesArray *fContainerTest" as
> data-member?
>
> I'm using root-5.13/02 on Gentoo-Linux-2006 (kernel 2.6.16-gentoo-r9)
> My gcc version is: 3.4.6
>
> Thank you in advance.
> Diego
>
>
> ------------------------------------------------------------------------
>
> #include <Riostream.h>
> #include "TClonesArray.h"
> #include "TH1F.h"
> #include "TCanvas.h"
> #include "TString.h"
>
> TClonesArray fContainerTest("TH1F",1000);
>
> void TestTClonesArray(){
>
> TString fHistoName;
>
> // Filling
> for(Int_t nh=0;nh<10;nh++){
> fHistoName.Form("Histogram_%d",nh);
> new (fContainerTest[nh]) TH1F(fHistoName,fHistoName,100,-2,2);
> ((TH1F *)fContainerTest[nh])->FillRandom("gaus",10*(100+nh));
> }
>
> // Drawing
> TCanvas *cshow = new TCanvas("ShowCanvas","Show Canvas",10,10,900,700);
> cshow->Divide(5,2);
> for(Int_t nh=0;nh<10;nh++){
> cshow->cd(nh+1);
> ((TH1F *)fContainerTest[nh])->Draw();
> }
>
> }
>
>
> ------------------------------------------------------------------------
>
> #include <Riostream.h>
> #include "TClonesArray.h"
> #include "TH1F.h"
> #include "TCanvas.h"
> #include "TString.h"
>
>
> class MyTest: public TObject{
>
> private:
> TCanvas *cshow;
> TString fHistoName;
> TClonesArray fContainerTest;
>
> public:
> MyTest();
> ~MyTest();
> void FillContainer();
> void DrawContainer();
>
> ClassDef(MyTest,0)
> };
>
>
> ClassImp(MyTest)
>
> MyTest::MyTest(){
> fContainerTest = TClonesArray("TH1F",1000);
> }
>
> MyTest::~MyTest(){
> fContainerTest.Delete();
> }
>
> void MyTest::FillContainer(){
>
> cout << "Ciao" << endl;
>
> // // Filling
> for(Int_t nh=0;nh<10;nh++){
> fHistoName.Form("Histogram_%d",nh);
> new (fContainerTest[nh]) TH1F(fHistoName,fHistoName,100,-2,2);
> ((TH1F *)fContainerTest[nh])->FillRandom("gaus",10*(100+nh));
> }
> }
>
> void MyTest::DrawContainer(){
> // Drawing
> cshow = new TCanvas("ShowCanvas","Show Canvas",10,10,900,700);
> cshow->Divide(5,2);
> for(Int_t nh=0;nh<10;nh++){
> cshow->cd(nh+1);
> ((TH1F *)fContainerTest[nh])->Draw();
> }
> }
>
>
>
> //_________________________________
> void TestTClonesArray_Class(){
>
> MyTest *cont_test = new MyTest();
> cont_test->FillContainer();
> cont_test->DrawContainer();
> }
>
>
> ------------------------------------------------------------------------
>
> #include <Riostream.h>
> #include "TClonesArray.h"
> #include "TH1F.h"
> #include "TCanvas.h"
> #include "TString.h"
>
>
> class MyTest: public TObject{
>
> private:
> TCanvas *cshow;
> TString fHistoName;
> TClonesArray *fContainerTest;
>
> public:
> MyTest();
> ~MyTest();
> void FillContainer();
> void DrawContainer();
>
> ClassDef(MyTest,0)
> };
>
>
> ClassImp(MyTest)
>
> MyTest::MyTest(){
> fContainerTest = new TClonesArray("TH1F",1000);
> }
>
> MyTest::~MyTest(){
> fContainerTest->Delete();
> delete fContainerTest;
> }
>
> void MyTest::FillContainer(){
>
> cout << "Ciao" << endl;
>
> // // Filling
> for(Int_t nh=0;nh<10;nh++){
> fHistoName.Form("Histogram_%d",nh);
> // fContainerTest->New(fHistoName,fHistoName,100,-2,2);
> TH1F *his = (TH1F *)fContainerTest->New(nh);
> his->SetName(fHistoName);
> his->SetTitle(fHistoName);
> his->SetBins(100,-2,2);
>
> ((TH1F *)fContainerTest->At(nh))->FillRandom("gaus",10*(100+nh));
> }
> }
>
> void MyTest::DrawContainer(){
> // Drawing
> cshow = new TCanvas("ShowCanvas","Show Canvas",10,10,900,700);
> cshow->Divide(5,2);
> for(Int_t nh=0;nh<10;nh++){
> cshow->cd(nh+1);
> ((TH1F *)fContainerTest->At(nh))->Draw();
> }
> }
>
>
>
> //_________________________________
> void TestTClonesArray_Class_Pointer(){
>
> MyTest *cont_test = new MyTest();
> cont_test->FillContainer();
> cont_test->DrawContainer();
> }
Received on Tue Sep 12 2006 - 18:42:30 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:01 MET