Problem with initializing an array of TH1D objects

Dear Root Experts,

I have the following problem: I have an array of TH1D objects and I want to use the same style TH1D to initialize them all. If I do this initialization explicitly by specifying the index of each object in the array than everything is perfectly fine (see attached macro withoutLoop.C), but if I use the loop, than the code crashes (see attached macro withLoop.C).

Can anybody suggest what I am missing here?

I am using ROOT 5.24/00 (tags/v5-24-00@29258, Jul 01 2009, 12:22:53 on linuxx8664gcc)

Cheers,
Ante
withLoop.C (250 Bytes)
withoutLoop.C (286 Bytes)

I suggest to use one of the 2 methods shown in the example below

Rene

[code]void withLoop()
{
TH1D style(“styleName”,“styleTitle”,1,0,1);
TH1D *histArray[2];

for(Int_t i=0;i<2;i++) {
histArray[i]= (TH1D*)style.Clone(Form(“styleName%d”,i));
cout<<(histArray[i])->GetName()<<endl;
}
}
void withLoop2() {
TH1D style(“styleName”,“styleTitle”,1,0,1);
TH1D *histArray[2];

for(Int_t i=0;i<2;i++) {
histArray[i]= new TH1D(style);
histArray[i]->SetName(Form(“styleName%d”,i));
cout<<(histArray[i])->GetName()<<endl;
}
}
[/code]

Dear Rene,

Thanks for your quick and helpful reply!

However, I have one additional small question related to the arrays of TH1D objects:

Namely, if I have:

TH1D *histArray[2];
TList *list;

and I want to add all elements of histArray to the list without looping, I cannot of course naively use

list->Add(histArray);

because that is equivalent to

list->Add(&histArray[0]);

Could you suggest how to proceed here?

Thanks you very much for your time.

Cheers,
Ante

You can insert directly your histograms inside the list as shown below. You can add a Tlist to a TList, but not insert the objects of a list into another list.

Rene

[code]void withLoop()
{
TH1D style(“styleName”,“styleTitle”,1,0,1);
TList listH;

for(Int_t i=0;i<2;i++) {
TH1D h= (TH1D)style.Clone(Form(“styleName%d”,i));
listH.Add(h);
cout<GetName()<<endl;
}
listH.ls();
}
[/code]

Dear Rene,

Thanks for your reply!

I know that I can add histograms directly to TList in the way you have outlined or that I can have nested TLists, but my question was going in a slightly different direction.

Namely, if I have an array of histograms and a list

TH1D *histArray[n];
TList *list;

than I thought (naively) that perhaps it is possible to call method Add() of TList in the following way (or something similar)

list->Add([] histArray);

and than the list ‘would know’ that I want to store all entries of histArray, without a need to loop over all entries of histArray in my code. This would become very handy for multi-dimensional arrays.

In principle the analogous syntax could work if I want to access entries of histArray from the list, but I do not claim that I really understand all possible issues and sideeffects here :wink:

So I will stick to what is available now in Root and organize my histograms in the nested lists, instead of arrays.

Thanks once again for your replies.

Cheers,
Ante