Re: [ROOT] probleme avec TClonesArray

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Oct 28 2003 - 09:23:50 MET


Hi Benoit,

I have modified your files to get your program running correctly in split mode.
Note that the use of a TClonesArray is only efficient if you keep
the storage between events (see the static TClonesArray like we do
in $ROOTSYS/test/Event).

Rene Brun

Benoit Revenu wrote:
> 
> dear rooters,
> 
> I'm using root 3-05-07 on linux
> 
> I'm trying to write a TTree containing data in a TFile. When the split is
> 0 or 1, the file is big (good sign!). When split >=2, the file is very
> small. I verified that the data instance is corerctly filled at each step
> of the process but I don't undertstand the behavior with the split.
> 
> In attachements are the tiny files with simple code.
> 
> Any help ?
> 
> --
> 
>                 Benoît
> 
>   --------------------------------------------------------------------------------
>                 Name: LinkDef.h
>    LinkDef.h    Type: Plain Text (TEXT/PLAIN)
>             Encoding: BASE64
> 
>               Name: main.cc
>    main.cc    Type: Plain Text (TEXT/PLAIN)
>           Encoding: BASE64
> 
>               Name: test.cc
>    test.cc    Type: Plain Text (TEXT/PLAIN)
>           Encoding: BASE64
> 
>              Name: test.h
>    test.h    Type: Plain Text (TEXT/PLAIN)
>          Encoding: BASE64

#include "test.h"
#include <fstream>
#include <TFile.h>
#include <TTree.h>

#define NEVTS 15

int main()
{
  int bufsize, split = 99;
  TFile * file = new TFile("toto.root","RECREATE","toto");
  TTree * tree = new TTree("tree","testtree");
  Test * br = new Test();
  bufsize = 256000;
  if (split)  bufsize /= 4;
  TBranch * b = tree->Branch("Tests","Test",&br,bufsize,split);
  Test * test = new Test();
  data * pdata;
  int nbytes = 0;
  for( int i=1;i<=NEVTS;i++ )
    {
      test->Set(i);
      for( int j=1;j<=NDATA;j++ )
	{
	  data dt(sin(double(i*j)));
	  test->Add(dt);
	}
      *br = *test;
      /* test      
	 for( int k=0;k<NDATA;k++ )
	 {
	 pdata = (data*)((*(br->ftcl))[k]);
	 cout << "write->" << pdata->val << endl;
	 }
      */
      nbytes += tree->Fill();
      cout << "tree : " << nbytes << endl;
      //      test->Clear();
      //      br->Clear();
    }

  tree->Print();
  file->Write();
  file->ls();
  
  delete test;
  delete br;
  delete tree;
  delete file;
}


/**
 @file SdEvent.cc
 @author Benoit Revenu
 @brief this file implements the quantities defined in SdEvent.h
*/

#include "test.h"
#include <string>
#include <sstream>
#include <fstream>

ClassImp(data)
ClassImp(Test)
ClassImp(Toto)
 TClonesArray *Toto::fgtcl = 0;

  Toto::Toto()
  {
    count=0;
    if (!fgtcl) fgtcl = new TClonesArray("data",NDATA);
    ftcl = fgtcl;
  }


#ifndef _TEST_H_
#define _TEST_H_

#include <string>
#include <vector>
#include <iostream>

#include "TROOT.h"
#include "TObject.h"
#include "TClonesArray.h"

using namespace std;


class data : public TObject
{

public:

  data(double d=-1) {val=d;}
  virtual ~data(){}
  double val;
  ClassDef(data,1)

};

#define NDATA 1000

class Toto : public TObject
{
public:
  
  Toto();
  TClonesArray *ftcl;
  static TClonesArray *fgtcl;
  void Clear()
  {
    ftcl->Clear();
    count = 0;
  }
  void Add(const data& dt)
  {
    TClonesArray &bla = *ftcl;
    new(bla[count++]) data(dt);
  }
  int count;
  ClassDef(Toto,1)

};

class Test : public Toto
{
public:
  
  Test(int truc=10){machin=truc;}

  virtual ~Test(){}
  int machin;
  void Set(int i) {machin=i;}

  ClassDef(Test,1)

};

#endif



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:16 MET