Re: storing histogramms

Rene Brun (Rene.Brun@cern.ch)
Tue, 02 Feb 1999 09:06:02 +0100


Florian Schopper wrote:
>
> Dear Rooters,
>
> we encountered a probably simple problem, which we couldn't find in
> the roottalk list, though.
>
> When we allocate Histogramms with new in the constructor of MChip,
> and store the MChip class into a root-file,
> we get a segmentation violation error when accessing the restored
> histogramms.
>
> Is it necessary to write a default constructor for MChip which allocates
> the histogramms again?
>
> thanks,
> Florian
>
> class MChip : public TObject
> {
> private:
>
> TH1F *CommonModeHist;
> TH1F *NHitHist;
>
> public:
> MChip(){}
>
> MChip(Int_t nChannel, TString Appelation );
>
> ClassDef(MChip,1)
> } ;
>
> The Constructor looks something like that:
>
> MChip::MChip(Int_t N, TString Appelation)
> {
> CommonModeHist = new TH1F("CommonMode","CommonMode",50,-25.0,25.0);
> CommonModeHist->SetDirectory(0);
> NHitHist = new TH1F("NHits","Number of Hits",10,0,10);
> HitHist->SetDirectory(0);
>
> }

Hi Florian,
All your classes must have a default constructor. This default
constructor is used by Root I/O in particular to create an instance
of a class before invoking its Streamer method when you read an object
from a file.
The default constructor should not internally allocate objects
or dynamic arrays. If you do so, you must be cautious in the
corresponding destructor.
Below, you will find a slightly modified example with the script
to create a shared lib.

Rene Brun

//----------script
rootcint -f cint.C -c MChip.h MChipLinkDef.h
g++ -fno-rtti -fno-exceptions -fPIC -I$ROOTSYS/include -c cint.C MChip.C
g++ -g -Wl,-soname,MChip.so -shared cint.o MChip.o -o MChip.so

//----------file MChip.h

#include <TNamed.h>
#include <TH1.h>

class MChip : public TObject
{
private:

TH1F *CommonModeHist;
TH1F *NHitHist;

public:
MChip(){}

MChip(Int_t nChannel, TString Appelation );

ClassDef(MChip,1)
} ;

//-----------file MChipLinkDef.h
#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;

#pragma link C++ class MChip;

#endif

//---------file MChip.C
#include "MChip.h"

ClassImp(MChip)

MChip::MChip(Int_t N, TString Appelation)
{
CommonModeHist = new TH1F("CommonMode","CommonMode",50,-25.0,25.0);
CommonModeHist->SetDirectory(0);
NHitHist = new TH1F("NHits","Number of Hits",10,0,10);
NHitHist->SetDirectory(0);

}