List Info

Thread: How do I make my object "browsable"




How do I make my object "browsable"
country flaguser name
United Kingdom
2008-03-25 05:51:12

Dear roottalk,

I have a  simple class which inherits from TObject. Its data members are a few ints and doubles and a TClonesArray. I write objects of this type to a TFile.

Is there a way to make this class "browsable" in the TBrowser? I'd like to be able to click on the object in the TBrowser and inspect its data members. I read in the User's Guide that I have to override the method "IsFolder()";  to return true. I've done this but it doesn't seem to be enough.

Do I have to implement my own Browse()  method? When I stick my object into the branch of a TTree, I can browse it's contents. However, I want to avoid using a TTree and just save it to the TFile directly.

Thanks for your help,

        Tobi

P.S. My system info: Mac OS X 10.4, Root v5.17/03


---
Tobias Raufer

R1,2.90, RAL
Chilton, Didcot
OX11 0QX, UK
+44 1235 778842
+44 1235 446733 (fax)
t.m.rauferrl.ac.uk

Re: How do I make my object "browsable"
country flaguser name
Switzerland
2008-03-25 10:13:30
Hi Tobias,

you would need to implement your own version of TParameter
with its own 
Browse method that e.g. creates a histo, fills it, and draws
it. You can 
also call Inspect() from your object's Browse() methods -
just give it a 
try, maybe it does what you want.

Cheers, Axel.

Tobias Raufer wrote:
> Hi Axel & Christian,
> 
> Thanks a lot for your quick replies.
> 
> I had hoped that this would be a bit simpler. It seems
rather cumbersome 
> to have to explicitly wrap each data member in a
TParameter. 
> Nonetheless, it seems to work. I can now see the data
members in the 
> TBrowser.
> 
> One more question: How can I get ROOT to either:
> 
>  draw the value of the data member in a histogram
> 
> or
> 
>  write out the value to the command line.
> 
> I would like on of these actions to be performed when
clicking on the 
> entry in the browser.
> 
> Cheers,
>     Tobi
> 
> 
> -----Original Message-----
> From: Christian Holm Christensen [mailto:cholmnbi.dk]
> Sent: Tue 3/25/2008 12:11
> To: Axel Naumann
> Cc: Tobias Raufer; roottalkcern.ch
> Subject: Re: [ROOT] How do I make my object
"browsable"
> 
> Hi Toby,
> 
> On Tue, 2008-03-25 at 12:13 +0100, Axel Naumann wrote:
>  > Hi Tobias,
>  >
>  > you'll need to re-implement Browse(TBrowser* b).
Check e.g. TBranch's 
> version
>  > which shows a basic example:
>  > <http://root.cern.ch/root/html/src/TBranch.cxx.html#pV
16>
> 
> The problem is your int's and double's - they need to
be wrapped in an
> object of a class for which there's a corresponding
TClass instance in
> the ROOT database.    Since
> 
>         Root> TClass* int_class =
gROOT->GetClass("int");
>         Root> TClass* double_class =
gROOT->GetClass("double");
> 
> both returns null pointers, you can add these members
directly - perhaps
> TBrowser should have the member function
> 
>         template <typename T>
>         void TBrowser::Add(const char* name, const
T& v);
> 
> for these kind of things. 
> 
> Anyway, you need, as mentioned, to wrap your int's and
double's e.g.,
> 
>         class MyClass : public TObject
>         {
>         public:
>           virtual MyClass() {
>             fArray.Delete();
>             if (fDoubleWrap) delete fDoubleWrap;
>             if (fIntWrap)    delete fIntWrap;
>           }
>           MyClass() : fArray(0), fDoubleWrap(0),
fIntWrap(0) {}
>           Bool_t IsFolder() const { return kTRUE; }
>           void Browse(TBrowser* b) {
>              b->Add(&fArray);
>              if (!fDoubleWrap) fDoubleWrap = new 
> TParameter<double>("fDouble",
fDouble);
>              if (!fIntWrap)    fIntWrap    = new
TParameter<int>("fInt", 
> fInt);
>              b->Add(fDoubleWrap);
>              b->Add(fIntWrap);
>           }
>         protected:
>            int                fInt;
>            double             fDouble;
>            TClonesArray       fArray;
>            TParameter<double> fDoubleWrap; //!
Transient
>            TParameter<int>    fIntWrap;    //!
Transient
>         };
>        
> yours,
> 
> --
>  ___  |  Christian Holm Christensen
>   |_| | 
------------------------------------------------------------
-
>     | |  Address: Sankt Hansgade 23, 1. th.  Phone: 
(+45) 35 35 96 91
>      _|           DK-2200 Copenhagen N       Cell:  
(+45) 24 61 85 91
>     _|            Denmark                    Office:
(+45) 353  25 447
>  ____|   Email:   cholmnbi.dk               Web:  
 www.nbi.dk/~cholm
>  | |
> 
> 


Re: How do I make my object "browsable"
country flaguser name
Switzerland
2008-03-25 06:13:59
Hi Tobias,

you'll need to re-implement Browse(TBrowser* b). Check e.g.
TBranch's version 
which shows a basic example: 
<http://root.cern.ch/root/html/src/TBranch.cxx.html#pV
16>

Let us know if you need a bit more details.

Cheers, Axel.

On 2008-03-25 11:51, Tobias Raufer wrote:
> 
> 
> Dear roottalk,
> 
> I have a  simple class which inherits from TObject. Its
data members are 
> a few ints and doubles and a TClonesArray. I write
objects of this type 
> to a TFile.
> 
> Is there a way to make this class "browsable"
in the TBrowser? I'd like 
> to be able to click on the object in the TBrowser and
inspect its data 
> members. I read in the User's Guide that I have to
override the method 
> "IsFolder()"  to return true. I've done this
but it doesn't seem to be 
> enough.
> 
> Do I have to implement my own Browse()  method? When I
stick my object 
> into the branch of a TTree, I can browse it's contents.
However, I want 
> to avoid using a TTree and just save it to the TFile
directly.
> 
> Thanks for your help,
> 
>         Tobi
> 
> P.S. My system info: Mac OS X 10.4, Root v5.17/03
> 
> 
> ---
> Tobias Raufer
> 
> R1,2.90, RAL
> Chilton, Didcot
> OX11 0QX, UK
> +44 1235 778842
> +44 1235 446733 (fax)
> t.m.rauferrl.ac.uk
> 


Re: How do I make my object "browsable"
country flaguser name
Denmark
2008-03-26 03:55:52
Hi Toby,

On Tue, 2008-03-25 at 16:13 +0100, Axel Naumann wrote:
> Hi Tobias,
> 
> you would need to implement your own version of
TParameter with its own 
> Browse method that e.g. creates a histo, fills it, and
draws it. You can 
> also call Inspect() from your object's Browse() methods
- just give it a 
> try, maybe it does what you want.

What you need is something like 

        template <typename T>
        class MyParameter : public TParameter<T>
        {
        public:
          void Browse(TBrowser*) { 
            if (!fHisto) 
              fHisto = new TH1D(GetName(), GetName(), 1,
GetVal()-.5,GetVal()+.5);
            fHisto->SetBinContent(1, GetVal());
            fHisto->Draw();
          }
          MyParameter(const char* name, const T& val) 
            TParameter<T>(name, val),
            fHisto(0)
          {}
          virtual ~MyParameter() { if (fHisto) delete
fHisto; }
        private:
          TH1D* fHisto; //! Transient
        };
        
and then you have to remember to make dictionaries for the
specialisations you need 

        #pragma link C++ class MyParameter<double>+;
        #pragma link C++ class MyParameter<int>+;
        
        
Kind of round-about way of doing it, but I'm afraid it's the
only way. 

Yours,

-- 
 ___  |  Christian Holm Christensen 
  |_| | 
------------------------------------------------------------
-
    | |  Address: Sankt Hansgade 23, 1. th.  Phone:  (+45)
35 35 96 91
     _|           DK-2200 Copenhagen N       Cell:   (+45)
24 61 85 91
    _|            Denmark                    Office: (+45)
353  25 447
 ____|   Email:   cholmnbi.dk               Web:   
www.nbi.dk/~cholm
 | |


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )