Re: find object help

Fons Rademakers (Fons.Rademakers@cern.ch)
Mon, 17 Feb 1997 19:25:32 +0100


Jacek M. Holeczek wrote:
>
> Hi,
> A small note - some time ago there were notes about ROOT extentions to C++
> somewhere on the root www page - in "C++ Coding Conventions" I think.
> Now I cannot find it any more. Are they moved, or are they lost ?

They are at the bottom of this page:
http://root.cern.ch/root/CintInterpreter.html

> Another point - is there somewhere a postscript version of the "Taligent
> rules" ?

No, not that I know of (You can always buy the book.

> My real questions are here.
> I need to write a small event unpacking function in form :
> ...
> if ( exists_histogram_with_name_EventLength ) EventLength->Fill(...);
> ...
> if ( exists_histogram_with_name_RawCamac0 ) RawCamac0->Fill(...);
> ...
> if ( exists_ntuple_with_name_MiniNtuple ) MiniNtuple->Fill(...);
> ...
> What I need are pieces of code "exists_histogram_with_name_..." and
> "exists_ntuple_with_name_..." :
> 1. both "search_routines" should scan ONLY the MEMORY in "current"
> subdirectory for a certain histogram/ntuple - this makes the
> problem that if the user has opened a file, the gDirectory
> points to that file -> is there a way to get the LAST path
> which was used in memory ( RAM ) ? ( In worst case I could assume
> that all histograms/ntuples are allowed to exist in gROOT only. )

Use pointers to the objects instead of trying to find them by name
everytime.
That is, look for them once (assuming they come from file) using either
gFile->Get("EventLength") (searches whole file) or
gDirectory->Get("EventLenght")
(searches only the directory).

> 2. It would be good to check both that an object with the given
> name exists ( "EventLength", "RawCamac0", "MiniNtuple" ), and
> that it belongs to proper class ( "TH1S", "TNtuple" ).
> In case the TNtuple is too pure, I might switch to TTree with
> one branch ( in this case I would have to chect whether the branch
> exists and contains proper leafs - or maybe I should simply get an
> address of a leaf with a particular name independent of where
> it actualy "hangs" ).

Use the TObject::IsA() member function to check if an object is of the
expected class:

if (!strcmp(obj->IsA()->GetName(), "TH1S")) {
// we have a THIS object...

}

or, much faster (all inline):

if (obj->IsA() == TH1S::Class()) {
// we have a TH1S object...

}

> 3. both routines should be very fast - assuming that I need to
> fill 2000 (raw) histograms I would like to be able to analyze
> 2000 events/second, thus such a "search" should work faster then
> 4e6 times per second ( God, am I stupid ? )

Use as much as possible direct access to all object via pointers.

> 4. maybe I should simply forget these "if (..)" and simply Fill(...)
> histograms/ntuples, and if they don't exist, they will not be
> created/filled ( hopefully this will behave quietly ).

You'll need some object to fill.

-- Fons.

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland          Phone: +41 22 7679248
E-Mail: Fons.Rademakers@cern.ch              Fax:   +41 22 7822601