Fill TH1F histogram using its name

Hi,

Can anyone help me how I can fill histograms using their
names (not the name of the TH1F object).

Here is a typical example: I have histograms
in an array:

TH1F *hp[5];
char * NAMESA[5]={“pt”,“eta”,“eta1”,“eta2”,“eta3”};
// where NAMESA[i] are unique names.

for (Int_t i=0; i<Nmax; i++ ) {
hp[i] = new TH1F(NAMESA[i],NAMESA[i],100,0,10);
}

latter in my program I do not want to fill the
histograms using hp[0]->Fill(…), hp[1]->Fill(…),
etc. statement since I do not know the index number
of my objects hp[] (when the array hp[] is very big).
What I know about my histograms
is only unique names in NAMESA[], like pt, eta …

So, the question is how I can fill appropriate histogram
hp[] using only it’s name, like pt, eta, eta1.
Obviously, the method should be fast and efficient.

thanks, sergei

You can use the two functions defined in TH1.h as

extern TH1 *R__H(Int_t hid); extern TH1 *R__H(const char *hname);
and implemented as:

/[code]/_____________________________________________________
TH1 *R__H(Int_t hid)
{
//return pointer to histogram with name
// hid if id >=0
// h_id if id <0

char hname[20];
if(hid >= 0) sprintf(hname,“h%d”,hid);
else sprintf(hname,“h_%d”,hid);
return (TH1*)gDirectory->Get(hname);
}

//_____________________________________________________
TH1 *R__H(const char * hname)
{
//return pointer to histogram with name hname

return (TH1*)gDirectory->Get(hname);
}[/code]
Rene

Thanks, Rene

This works. I’m sure a lot of people will like this
function (below). This will look like the standard HBOOK
hf1 function, One could add weights, extend it to 2D
etc… It will also make the transition to Root easier + no need
for much typing, And there are too many cases when the histogram
should be filled using it’s name., // function to fill 1D histogram based on it's name void Hf1(const char * name, Double_t val) { (TH1*)R__H(name)->Fill( val ); };

[quote=“brun”]You can use the two functions defined in TH1.h as

extern TH1 *R__H(Int_t hid); extern TH1 *R__H(const char *hname);
and implemented as:

/[code]/_____________________________________________________
TH1 *R__H(Int_t hid)
{
//return pointer to histogram with name
// hid if id >=0
// h_id if id <0

char hname[20];
if(hid >= 0) sprintf(hname,“h%d”,hid);
else sprintf(hname,“h_%d”,hid);
return (TH1*)gDirectory->Get(hname);
}

//_____________________________________________________
TH1 *R__H(const char * hname)
{
//return pointer to histogram with name hname

return (TH1*)gDirectory->Get(hname);
}[/code]
Rene[/quote]

You do not need this function!!
The R__H functions already return a pointer to the histogram, such that
you can use any of the TH1::Fill functions.

There are many reasons why we do not make publicity for these two
functions
-performance penalty. Event with a hashtable algorithm, the method is slower than using directly the pointer.
-Although this would loolk like a natural thing when coming from hbook,
it is not a good idea when using classes. you better keep the pointer
to your histograms in some classes.

Rene