Re: writing/reading objects to/form a file

From: chiara zampolli <Chiara.Zampolli_at_bo.infn.it>
Date: Mon, 21 Nov 2005 11:33:49 +0000


Dear Axel,

    I'm really sorry to bother you, but it does not work... Now I'm sending you in the attached file the updated version of the macro with the classes, and here below what I try to do. Actually, the testTOF.C macro is only compiled, not ran, and for testing it I work inline. What I try to do is to write this AliTOFCalib object in the file, and then to try to retrieve the array of elements it is made up of:

root [0] .L testTOF1.C++
Info in <TUnixSystem::ACLiC>: creating shared library /yp/exports1/aliprod1/zampolli/Prova/./testTOF1_C.so root [1] AliTOFCalib *tc = new AliTOFCalib();  fPad = 0
root [2] tc->CreateArray()
 fPad = 0x5160a00c
/...some bad stuff here.../

root [6] AliTOFCalParam *p = (AliTOFCalParam*)tc->GetChannel(45)
root [7] p->SetChannel(45)
root [8] TFile *file = new TFile("ciccio.root","recreate")
root [9] tc->Write("ciccio")

(Int_t)141
root [10] file->Close()
root [11] delete file
root [12] TFile *file1 = new TFile("ciccio.root", "read")
root [13] file1->ls()
TFile**         ciccio.root
 TFile*         ciccio.root
  KEY: AliTOFCalib      ciccio;1

root [14] AliTOFCalib *tc1;
root [15] file1->GetObject("ciccio",tc1)  fPad = 0

and here I'm not sure... I need the array, perhaps the Write method is not the correct one... anyway, I have done this:

root [16] tc1->CreateArray()
 fPad = 0x9d44320
root [17] AliTOFCalParam *p1 = (AliTOFCalParam*)tc1->GetChannel(45) root [18] p1->GetChannel()
(const Float_t)6.71154744112407474e+22

obviously...
Any suggestion?

I hope not to bother you too much...
cheers,
chiara

Axel Naumann wrote:

>Hi Chiara,
>
>
>
>>> virtual ~AliTOFCalib(){delete[] fPads;}
>>>
>>>
>>This is the wrong delete operator. You only have one element in fPads
>>(i.e. it's not a C array, as in Int_t *aCArray; aCArray=new Int_t[10],
>>where you have aCArray pointing to 10 Int_ts, and where you need to use
>>delete[], the array deleting operator). See e.g.
>>http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=31
>>
>>I thought that since it was pointing to an array, it should have then been
>>treated as an array... ok, wrong thought.
>>
>>
>
>You're absolutely right, I didn't read your code properly (as Stan
>pointed out)! You _so_ need the array delete[] operator, as you create a
>new array. You still should not create it in your default constructor -
>but you're using the proper delete[]. Sorry about that.
>
>
>
>>> void Browse(TBrowser *b)
>>> {
>>> char name[10];
>>> for(Int_t i=0; i<18; ++i) {
>>> snprintf(name,sizeof(name),"Sector %2.2d\0",i);
>>>
>>>
>>name's size of 10 is not enough for that snprintf.
>>
>>Why? ("Sector "->7)+(%2.2d->2)+(\0)=10... and i is always positive...
>>
>>
>
>See, in this case it's even 11 characters:
>"Sector %2.2d\0" is the char array
>Sector(space) - that's 7,
>%2.2d - that's 2 (just as you pointed out),
>\0 - that's 1
>and then there's the end of string (any "abc" string has 4 chars, as it
>contains a terminating \0 character - you're adding yet another \0, so
>you have 11 chars).
>
>
>
>>You do know that this "i*npad/18" is a pure integer calculation (if npad
>>is a non-floating point variable)? Meaning there is no rounding here,
>>e.g. for npad==11 you get
>>
>>yes, I know, that's exactly what I want.
>>
>>
>
>Okay, just checking :-)
>
>
>
>>This shows that there is a problem with deleting your objects - probably
>>due to the wrong delete[] operator.
>>
>>I'll try to modify it.
>>
>>
>
>So - it's not because of the wrong delete op - as you're using the right
>one :-) It's probably because you create a new object before reading it,
>and because you allocate memory in your default constructor.
>
>
>
>>Thank you Axel. I'll make the changes to the class, and see what I get.
>>
>>
>
>Let me know if there's still a problem with runnign your code. And
>excuses for me not reading your code properly!
>Axel.
>
>
>

#include "TObject.h"
#include "TROOT.h"
#include "TBrowser.h"
#include "TClass.h"
#include <Riostream.h>
#include <stdlib.h>

const Int_t npad=18*96*91;

class AliTOFCalParam: public TObject {
private:
  Float_t fParam[10];
public:
  AliTOFCalParam() {for (Int_t i = 0;i<10;i++) fParam[i]=0;}   virtual ~AliTOFCalParam(){};
  void SetChannel (Float_t param){fParam[1]=param;}   Float_t GetChannel ()const {return fParam[1];}   ClassDef(AliTOFCalParam,1)
};

class AliTOFCalPlate1: public TObject
{
private:
  AliTOFCalParam *fCalp;
  Int_t ciccio;
public:
  AliTOFCalPlate1(){fCalp=0x0;ciccio=0;};   AliTOFCalPlate1(AliTOFCalParam *calp): fCalp(calp) {}   virtual ~AliTOFCalPlate1(){delete[] fCalp;}   ClassDef(AliTOFCalPlate1,1)
};

class AliTOFCalPlate2: public TObject
{
private:
  AliTOFCalParam *fCalp;
public:
  AliTOFCalPlate2(){fCalp=0x0;};
  AliTOFCalPlate2(AliTOFCalParam *calp): fCalp(calp) {}   virtual ~AliTOFCalPlate2(){delete[] fCalp;}   ClassDef(AliTOFCalPlate2,1)
};

class AliTOFCalSector1: public TObject
{
private:
  AliTOFCalParam *fCalp;
public:
  AliTOFCalSector1(){fCalp=0x0;};
  AliTOFCalSector1(AliTOFCalParam *calp): fCalp(calp) {}   virtual ~AliTOFCalSector1(){delete[] fCalp;}   void Browse(TBrowser *b)
  {

    b->Add(new AliTOFCalPlate1(fCalp),        "Plate1");
    b->Add(new AliTOFCalPlate1(&fCalp[19*96]),"Plate2");
    b->Add(new AliTOFCalPlate2(&fCalp[38*96]),"Plate3");
    b->Add(new AliTOFCalPlate1(&fCalp[53*96]),"Plate4");
    b->Add(new AliTOFCalPlate1(&fCalp[72*96]),"Plate5");
  }
  Bool_t     IsFolder() const { return kTRUE; }
  ClassDef(AliTOFCalSector1,1)
};

class AliTOFCalib: public TObject
{
private:
  // AliTOFCalParam fPads[npad];
  AliTOFCalParam * fPads;
public:
  //AliTOFCalib() {fPads= new AliTOFCalParam[npad];cout <<" fPad = " << fPads << endl;}   AliTOFCalib() {fPads=0;cout <<" fPad = " << fPads << endl;}   virtual ~AliTOFCalib(){delete[] fPads;}   void Browse(TBrowser *b)
  {
    char name[10];
    for(Int_t i=0; i<18; ++i) {

      snprintf(name,sizeof(name),"Sector %2.2d",i);
      b->Add(new AliTOFCalSector1(&fPads[i*npad/18]),name);
    }
  }
  void PlotPad(Int_t n) {} // *MENU*
  Bool_t IsFolder() const { return kTRUE; }   void CreateArray() {
    if (fPads==0) {fPads=new AliTOFCalParam[npad];}     cout <<" fPad = " << fPads << endl;
  }
  AliTOFCalParam *GetArray(){return fPads;}   AliTOFCalParam* GetChannel(Int_t i) {return &fPads[i];}   ClassDef(AliTOFCalib,1)
};
ClassImp(AliTOFCalParam)
ClassImp(AliTOFCalPlate1)
ClassImp(AliTOFCalPlate2)
ClassImp(AliTOFCalSector1)
ClassImp(AliTOFCalib)

#ifdef __MAKECINT__ 
#pragma link C++ class  AliTOFCalParam+;
#pragma link C++ class  AliTOFCalPlate1+;
#pragma link C++ class  AliTOFCalPlate2+;
#pragma link C++ class  AliTOFCalSector1+;
#pragma link C++ class AliTOFCalib+;
#endif

Int_t testTOF1 ()
{
  AliTOFCalib *tc = new AliTOFCalib();
  gROOT->GetListOfBrowsables()->Add(tc);   TFile * file = new TFile("prova.root","recreate");   tc->Write("tc");
  file->Close();
  return 0;
} Received on Mon Nov 21 2005 - 11:31:36 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:13 MET