Re: [ROOT] Probolems with creating a tree using own class

From: Frankland John (frankland@ganil.fr)
Date: Wed May 28 2003 - 22:05:30 MEST


Hi Alexander

I think your problem comes from using interpreted script rather than 
compiling.
The interpreter lets you get away with all sorts of crimes without 
saying anything
until at some point it just blows up...and you have no idea why.
Judging from the code you sent, as the necessary #include directives are 
missing,
I think it's a safe bet that you never tried to compile your class.
If you did, then you would find that your class is not valid - it has no 
ctor or dtor!
Therefore the line

Test* test=new Test();

has little chance of compiling, and I hate to imagine what the 
interpreter may have got
up to when it went through this.... ;-)
Also, you declare a pointer

TTree* t4

and then access its methods using

t4.Fill()

Will not compile, but the interpreter just carries on regardless...
Finally,

void main(void)

is not a valid declaration for main, which must have "int" return type.
Once again, the ACLiC compiler points out these problems straight
away, whereas CINT just doesn't seem to care.

Try the following: first, modify the header for your class:

Test.hh:
----------------------------------------------------
#include "TObject.h"

class DietzTest :public TObject {
public:

  Int_t fTest;

    DietzTest();
    virtual ~DietzTest();
   
  ClassDef(DietzTest,1)
};
-------------------------------------------------------

and create an implementation file Test.C:
---------------------------------------------------------
#include "Test.hh"

ClassImp(Test)

///////////////////////////////////////////////////////
// Test
//
// Remarkably succinct yet informative
// description for HTML documentation
//
////////////////////////////////////////////////////////

DietzTest::DietzTest()
{
//default ctor
//does nothing but must be present
}

DietzTest::~DietzTest()
{
//dtor
//ditto and ditto again
}
-------------------------------------------------------------------

Now modify your "main" programme main.C:
-------------------------------------------------------------------
#include "Test.hh"
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"

int Dmain(void)
{
  // creating new TFile
  TFile* f=new TFile("tree4.root","RECREATE");

  // creating new TTree
  TTree* t4=new TTree("t4","tree");

  // pointer to new Object
  Test* test=new Test();

  // creating new branch
  t4->Branch("test","Test",&test, 16000,2);

  // filling tree with random values
  for (int ev=0;ev<100;ev++) {
    Int_t value=int(gRandom->Rndm()*100);
    test->fTest=value;
    t4->Fill();
  }

  f->Write();
  f->Close();
  return 0;
}
-------------------------------------------------------------------------------

** Notice that I renamed "main" as "Dmain" - because otherwise after 
compiling and charging
** the shared lib, CINT did not recognise "main()" whereas "Dmain()" 
works fine - does anyone
** know why ?


OK, now all you have to do is to compile and load your class by typing
.L Test.C+

and then do the same for the main program
.L main.C+

and if you type
Dmain()

you'll see that it works - or at least it worked for me!!!
If you follow this prescription for adding your own classes to ROOT you 
shouldn't have too many
problems, and if you use the compiler you should find it easier to know 
when things aren't going
too well!

Hope this helps
Good luck ;-)

-- 
John D. Frankland
Beam Coordinator
GANIL
B.P. 55027
14076 CAEN Cedex 05

tel: +33 (0)231454628
fax: +33 (0)231454665



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