Hi, I just would like to add to Rene's message that one can also use the script compiler to add a class. For example, in a file called ABCClass.C we define the class. Then we can use an unamed macro to compile the class with the script compiler and use it to fill trees ..... ----------------- ABCClass.C ------------- #include "TObject.h" // define the ABC class and make it inherit from // TObject so that we can write ABC to a root file class ABC : public TObject { public: Float_t a,b,c,p; ABC():a(0),b(0),c(0),p(0){}; // Define the class for the cint dictionary ClassDef(ABC,1) }; // Call the ClassImp macro to give the ABC class RTTI and // full I/O capabilities. #if !defined(__CINT__) ClassImp(ABC); #endif ------------------ Then we can use the script compiler to compile the class and build a tree. Below is a macro that does just this .... ------------------ ABCWriteClass.C ----------- { // check to see if the class ABC is in the dictionary // if it is not in the dictionary compile it with the script // compiler. if (!TClassTable::GetDict("ABC")) { gSystem->CompileMacro("ABCClass.C"); } // Open the ASCII file for reading ifstream asciiFile("ABC.txt"); // create a TFile object, using RECREATE to overwrite if it exists TFile *hfile = new TFile("ABCwithClass.root","RECREATE","Exercise 1"); // create a TTree object with the name T, title : A ROOT tree, // and the default buffer size of 64 MB TTree *tree = new TTree("T","A ROOT tree"); // create an ABC object. This must be on the heap to // be added to the tree correctly. ABC *v = new ABC; // add a branch called abcBranch, it contains ABCSTURCTURE , and the // leaves are: a,b,c. // add another branch for the lenght of this vectorv TBranch *abcBranch = tree->Branch("abcBranch","ABC",&v); // read the values from the ASCII file and fill the tree while (asciiFile >> v->a >> v->b >> v->c) { v->p = (sqrt((v->a * v->a)+(v->b * v->b)+(v->c * v->c))); tree->Fill(); } hfile->Write(); // Draw the tree TCanvas *myCanvas = new TCanvas ("myCanvas","My Canvas", 0,0,600,400); myCanvas->Divide(2,2); myCanvas->cd(1); tree->Draw("a"); myCanvas->cd(2); tree->Draw("b"); myCanvas->cd(3); tree->Draw("c"); myCanvas->cd(4); tree->Draw("p"); // to display a vs. p on the command line type this line at the root prompt: // Oak->Draw("a:p"); } -----Original Message----- From: Rene Brun <Rene.Brun@cern.ch> To: Maurizio Ungaro <ungaro@maurizio.phys.rpi.edu> Cc: Dariusz Miskowiec <D.Miskowiec@gsi.de>; roottalk@hpsalo.cern.ch <roottalk@hpsalo.cern.ch> Date: Thursday, November 18, 1999 3:52 PM Subject: Re: Simple tree >Hi Maurizio, >I have already posted several times the following example. >I have slightly extended it to show how >to create a Tree in a compiled program and read it with >two small macros. Your short example cannot work because you >try to create a branch with a pointer to a class not derived >from TObject. > >You will find below the following files: > A.h the definition file for a small class A > A.cxx the corresponding implementation file > ALinkDef.h the file containing the rootcint directives > AMain.cxx the main program to create the Tree > ARead.C a small macro to loop on the file A.root generated by Amain > Asimple.C a small macro to plot one Tree variable > goA a small script showing how to run rootcint,link,etc on Linux > >In this short example, I also illustrate how to create a shared lib. >You should look at the examples in $ROOTSYS/test/MainEvent.cxx for a more >elaborated Tree example. >I also suggest you read the nice tutorials developped by the FNAL Root >support team at URL: http://www-pat.fnal.gov/root/ > >Rene Brun > >I suggest the following steps using Root version 2.23: > 1- execute goA > 2- run A > 3- execute the following Root interactive session > Root > .x ARead.C > 4- execute the following Root interactive session > Root > .x Asimple.C > >#--------------file goA >rootcint -f Acint.cxx -c A.h ALinkDef.h >g++ -g -fPIC -I$ROOTSYS/include -c Acint.cxx A.cxx Amain.cxx >g++ -g -Wl,-soname,A.so -shared Acint.o A.o -o A.so >g++ -g -o A Amain.o A.so -L$ROOTSYS/lib \ > -lCore -lCint -lTree -lm -ldl -rdynamic > >//------------------------------ A.h >#ifndef __A_HH__ >#define __A_HH__ >#include "TObject.h" > >class A : public TObject { >public: > > A(); > virtual ~A(); > > Float_t fA; > Float_t fB; > > ClassDef(A,1) //My small class example >}; > >#endif >//------------------------------- A.cxx >#include "A.h" > > ClassImp(A) > >A::A() { > fA = 1; > fB = 2; >} > >A::~A() { >} > >//---------------------------file ALinkDef.h > >#ifdef __CINT__ > >#pragma link off all globals; >#pragma link off all classes; >#pragma link off all functions; > >#pragma link C++ class A; >#endif > >//------------------------------macro ARead.C >void Aread() >{ > //example of macro to read the Tree generated in Amain.cxx > > gSystem->Load("A"); > TFile *f = new TFile("A.root"); > TTree *T = (TTree*)f->Get("T"); > A *a=0; > T->SetBranchAddress("aa",&a); > > Int_t nentries = (Int_t)T->GetEntries(); > for (Int_t i=0;i<nentries;i++) { > T->GetEntry(i); > if (a->fA >0.001) continue; > printf("Print entry:%d\n",i); > a->Dump(); > } >} > >//------------------------------macro Asimple.C >{ > // small macro plotting the distribution of variable fB > gROOT->Reset(); > gSystem->Load("A"); > TFile *f = new TFile("A.root"); > T->Draw("fB","fA<0.5"); >} > >On Thu, 18 Nov 1999, Maurizio Ungaro wrote: > >> I'm trying to build a very simple tree >> with my class A this way: >> >> >> class A >> { >> public: >> Int_t a; >> Int_t b; >> }; >> >> >> >> A *CC; >> >> >> TROOT simple("simple","trees"); >> >> main() >> { >> >> Int_t split = 0; >> Int_t bsize = 32000; >> >> >> // Create a ROOT Tree >> >> TTree *tree = new TTree("T","An example of ROOT tree"); >> tree->Branch("aa","A",&CC,bsize,split); >> >> >> return 0; >> } >> >> >> and I'm getting the message >> >> Error in <TTree::BranchObject>: Cannot find class:A >> >> >> What am I doing wrong? >> > >
This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:43 MET