Re: [ROOT] access to the class member for ROOT I/O

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Jun 04 2004 - 18:04:54 MEST


Hi,

When you  create your Tree, replace:

  tree->Branch("A","A",&a);
  tree->Branch("B","B",&b);
by
  tree->Branch("A","A.",&a);
  tree->Branch("B","B.",&b);

see the TTree::Branch documentation:

      IMPORTANT NOTE about branch names
    In case two or more master branches contain subbranches with
    identical names, one must add a "." (dot) character at the end
    of the master branch name. This will force the name of the subbranch
    to be master.subbranch instead of simply subbranch.
    This situation happens when the top level object (say event)
    has two or more members referencing the same class.
    For example, if a Tree has two branches B1 and B2 corresponding
    to objects of the same class MyClass, one can do:
       tree.Branch("B1.","MyClass",&b1,8000,1);
       tree.Branch("B2.","MyClass",&b2,8000,1);
    if MyClass has 3 members a,b,c, the two instructions above will generate
    subbranches called B1.a, B1.b ,B1.c, B2.a, B2.b, B2.c

Rene Brun

Hajime Nanjyo wrote:
> 
> Dear Rooters,
> 
> I have a question about how to access a class member for the ROOT I/O
> from the command "tree->Draw(...)".
> 
> I made two simple classes, A and B with a member 'int val'.
> Then the ROOT file with the two classes was created as follows.
> ******************************************************************************
> *Tree    :tree      : test                                                   *
> *Entries :       10 : Total =            6018 bytes  File  Size = 1093 *
> *        :          : Tree compression factor =   1.00                       *
> ******************************************************************************
> *Branch  :A                                                                  *
> *Entries :       10 : BranchElement (see below)                              *
> *............................................................................*
> *Br    0 :fUniqueID :                                                        *
> *Entries :       10 : Total  Size=        634 bytes  One basket in memory    *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> *Br    1 :fBits     :                                                        *
> *Entries :       10 : Total  Size=        610 bytes  One basket in memory    *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> *Br    2 :val       :                                                        *
> *Entries :       10 : Total  Size=        598 bytes  One basket in memory    *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> *Branch  :B                                                                  *
> *Entries :       10 : BranchElement (see below)                              *
> *............................................................................*
> *Br    3 :fUniqueID :                                                        *
> *Entries :       10 : Total  Size=        634 bytes  One basket in memory    *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> *Br    4 :fBits     :                                                        *
> *Entries :       10 : Total  Size=        610 bytes  One basket in memory    *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> *Br    5 :val       :                                                        *
> *Entries :       10 : Total  Size=        598 bytes  One basket in memory    *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> .
> 
> Then I performed 3 kinds of tree->Draw() as,
>   1.) tree->Draw("val");
>   2.) tree->Draw("A.val");
>   3.) tree->Draw("B.val");
> But I only got the same results (I can't get access to the B's 'val').
> 
> Could you tell me how to draw the B's 'val'.
> 
> I used ROOT on current CVS under RH9 with gcc3.2.2.
> The simple codes are as follows.
> 
> ///////////////////////////////////////////////
> //A.h
> ///////////////////////////////////////////////
> #ifndef A_h
> #define A_h 1
> 
> #include "TObject.h"
> 
> class A : public TObject
> {
> public:
>   A();
>   ~A();
> 
>   int    val;
> 
>   ClassDef(A,1)
> };
> 
> #endif
> ///////////////////////////////////////////////
> //A.C
> ///////////////////////////////////////////////
> #include "A.h"
> 
> #if !defined(__CINT__)
> ClassImp(A);
> #endif
> 
> A::A() {;}
> A::~A() {;}
> ///////////////////////////////////////////////
> //B.h
> ///////////////////////////////////////////////
> #ifndef B_h
> #define B_h 1
> 
> #include "TObject.h"
> 
> class B : public TObject
> {
> public:
>   B();
>   ~B();
> 
>   int    val;
> 
>   ClassDef(B,1)
> };
> 
> #endif
> ///////////////////////////////////////////////
> //B.C
> ///////////////////////////////////////////////
> #include "B.h"
> 
> #if !defined(__CINT__)
> ClassImp(B);
> #endif
> 
> B::B() {;}
> B::~B() {;}
> ///////////////////////////////////////////////
> //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
> ///////////////////////////////////////////////
> //BLinkDef.h
> ///////////////////////////////////////////////
> #ifdef __CINT__
> #pragma link off all globals;
> #pragma link off all classes;
> #pragma link off all functions;
> #pragma link C++ class B+;
> #endif
> ///////////////////////////////////////////////
> //write.C
> ///////////////////////////////////////////////
> #include <iostream>
> #include <iomanip>
> #include <fstream>
> #include <cstdlib>
> #include "TApplication.h"
> #include "TFile.h"
> #include "TTree.h"
> 
> #include "A.h"
> #include "B.h"
> 
> int main(int argc,char** argv) {
> 
>   TApplication theApp("App", &argc, argv);
>   gApplication->Init();
> 
>   argc=theApp.Argc();
>   argv=theApp.Argv();
> 
>   //clonedummy input.root output.root
>   if(argc!=2) {
>     std::cout << "Argument Error!" << std::endl;
>     std::exit(1);
>   }
> 
>   std::cout << "Output  file : " << argv[1] << std::endl;
> 
>   TFile* tfo = new TFile(argv[1],"RECREATE");
>   TTree* tree = new TTree("tree","test");
> 
>   A*   a = new A();
>   B*   b = new B();
> 
>   tree->Branch("A","A",&a);
>   tree->Branch("B","B",&b);
> 
>   for (Int_t jentry=0; jentry<10;jentry++) {
>     a->val=1;
>     b->val=10;
> 
>     tree->Fill();
>   }
>   tree->Write();
>   tfo->Close();
> 
>   return 0;
> }
> ///////////////////////////////////////////////
> //make.sh
> ///////////////////////////////////////////////
> rootcint -f ADict.C -c A.h ALinkDef.h
> rootcint -f BDict.C -c B.h BLinkDef.h
> g++ -o write `rootconf ` *.C
> 
> g++ --shared `root-config --cflags` `root-config --libs` -I. \
> -o libData.so \
> A.C ADict.C B.C BDict.C
> ///////////////////////////////////////////////
> //run.sh
> ///////////////////////////////////////////////
> ./write test.root
> ///////////////////////////////////////////////
> //read.C
> ///////////////////////////////////////////////
> {
>  gSystem->Load("libData.so");
>  TFile* tf = new TFile("test.root");
>  tree->Print();
>  tree->Draw("val");
>  tree->Draw("A.val");
>  tree->Draw("B.val");
> }
> ///////////////////////////////////////////////
> 
> Best Regards,
> Hajime



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:08 MET