Index problem of histogram array

Hi,

I declared an array of 1-D histogram like this:

char hnm[20].htt[100];
TH1F *PMTes[11][3][3];
for(Int_t i=0;i<11;i++)
{
for(Int_t j=0;j<3;j++)
{
for(Int_t k=0;k<3;k++)
{
sprintf(hnm,“pmtes%id%ic%i”,i,j,k); sprintf(htt,“PMT#=%i,EC-SC,side %i,cut %i”,i+8,j-1,k);
PMTes[i][j][k]=new TH1F(hnm,htt,100,-10,10);
}
}
}
And I’ve tried to fill those histograms like this:
Int_t iseg,isg,ipt,ipmt
Float_t dt_sc_sc;
Int_t nev=t->GetEntries();
for(Int_t j=0; j<=nev;j++)
{ t->GetEntry(j); isg=0; ipmt=0;
iseg=(cc_segm[0]-(cc_segm[0]/1000)*1000)/10-1;
ipt=cc_segm[0]/1000-1; isg=iseg-8; ipmt=ipt+1;
dt_ec_sc=ec_t[0]-sc_t[0];
PMTes[isg][ipmt][0]->Fill(dt_ec_sc);
}

Eventually, I saw " segmentation violation" message. Is it impossible to use the variable as an index of a histogram? I mean, Like “ipt” or “isg” in this code. In C/C++, I know that the index should be constant when it is declared but could be variable when the array is accessed after its declaration. How about in ROOT? I’m using the version of 5.18/00.

Thank you

Could you post the shortest possible RUNNING script reproducing your problem?

Rene

I attached the source code.

You will need the Ntuple file for running the code.
jlab.org/~hkkang/ntp_50860_p … hbook.root

Thanks again.
module_Xsosicut_pmt.C (2.81 KB)

Hi,

I would suggest to compile your macro with ACLiC. Then you will be able to:

  • see compilation warning, like uninitialized variable:
    [color=red]warning C4700: uninitialized local variable ‘ipmt’ used[/color] :exclamation:
  • debug your code, in particular this line:
    [color=blue]ipt=cc_segm[cc[0]-1]/1000; isg=iseg-8;[/color]
    which may lead to isg being -1 when iseg == 7 (and then used as array index)!!! :open_mouth:

Cheers,
Bertrand.

Thanks Bertrand,

I will try to use ACLiC. Anyway, iseg can have the number bigger than 8. That’s why I put iseg-8.

Anyway, you should check to be sure it is greather or equal to zero before to use it as array index! Otherwise, don’t be surprised to have crashes in your code :wink:
– Bertrand.

BTW, you should review this part of the code:

iseg=(cc_segm[cc[0]-1]-(cc_segm[cc[0]-1]/1000)*1000)/10-1; ipt=cc_segm[cc[0]-1]/1000; isg=iseg-8;
Because iseg has values going from 7 to 17…

– Bertrand.

I’m sure that iseg is varing from 8 to 18.
So I put isg is equal to iseg-8. And “isg” is used as an index.

Sorry. I reviewed the code. one of early event has iseg=7.

Really thanks.

Well, I added printf(“isg=%d iseg=%d (isg=iseg-7)\n”,isg,iseg); in the code and it was printing values going from 7 to 17 for iseg on my machine… But anyway, You know better then me what you are doing :wink:
And using ACLiC will allow you to solve the problem anyway. :slight_smile:
Cheers, Bertrand.