Arrays of TH1F and named/unnamed macros

Dear Root Talk,

I was curious as to why the following code works when it is a named macro, but complains when it is an unnamed macro. The complaint comes in when I execute the code again and try to delete the previous histograms:

For the unnamed macro I would need to change the starting point from 0 to 1 and adjust the definitions of the arrays appropriately and then it works again.

Thanks
ROOT 5.08 (OS X 10.4.6)

array_hist()
{

char name[4][50];

for(int i=0; i<4; i++)
{
  sprintf(name[i],"hist_array_%d", i);
}


TH1F *h[4], *hist_array[4];

for(int i=0; i<4>FindObject(name[i]);
  if(h[i]) 
    {
      printf("%s deleted\n", name[i]);
      h[i]->Delete();
    }
}



TCanvas *c1 = new TCanvas("c1", "Test Canvas");
c1->Divide(2,2);

for(int i=0; i<4>FillRandom("gaus", 10000);

  c1->cd(i+1);
  hist_array[i]->Draw();

}


}

TH1F *h[4], *hist_array[4]; ... if(h[i])You can not use this pattern since the 2 arrays are initialized with random values.

Philippe.

Thanks for the prompt reply.

If that is the cause, then why does it work then for the named macro case?

Sometimes the random number is 0.

Philippe.

Sorry, but I am not following why changing the macro from unnamed to named would cause the random initialization of the pointers to be different.

What am I missing?

Thanks :slight_smile:

Humm … the named and unamed macro are handled differently and thus the memory pattern is different (and it may even that the rules for initialization is different). Either way the result should be assumed to unpredictable …

Philippe.

Fair enough :slight_smile:

So how would one do this properly?

Or should one just not make arrays of histograms?

for(int i=0; i<4; ++i) { TH1 *h = (TH1*)(FindObject(name[i]); if (h) delete h; }

Philippe

I am getting the following error message when I run the code with your modification:

{

char name[4][50];

for(int i=0; i<4; i++)
{
  sprintf(name[i],"hist_array_%d", i);
}


TH1F *hist_array[4];

for(int i=0; i<4>FindObject(name[i]);
  if(h[i]) 
    {
      printf("%s deleted\n", name[i]);
      h[i]->Delete();
    }
 
  */

  
  TH1 *h = (TH1*)(FindObject(name[i]));
  if(h) delete h;
}

If I modify the code along the same lines:

array_hist()
{

char name[4][50];

for(int i=0; i<4; i++)
{
  sprintf(name[i],"hist_array_%d", i);
}


TH1F *hist_array[4];

for(int i=0; i<4>FindObject(name[i]);
  if(h[i]) 
    {
      printf("%s deleted\n", name[i]);
      h[i]->Delete();
    }
 
  */

  
  /*  TH1 *h = (TH1*)(FindObject(name[i]));
  if(h) delete h;
  */

  TH1F *h = (TH1F*)gROOT->FindObject(name[i]);
  if(h) h->Delete();

}

and use a named macro then it works

with an unnamed macro it still does not work.

Any thoughts?

Thanks for taking the time :slight_smile:

Note: I don’t understand why the code isn’t being displayed properly - see the <4> in the second for loop. I have attached it.
array_hist.cpp (682 Bytes)

Hi,

What you use is what I meant … there seems to be some issue with the forum and copy/pasting of code (I see in your code the following series of characters: [quote]for(int i=0; i<4>FindObject(name[i]); [/quote]

I can reproduce your problem when reloading the unnamed macro. I will investigate.

Philippe

Thanks for taking the time :slight_smile:

Hi,

Unfortunately this is a deficiency of the current CINT bytecode compiler. CINT is currently going through a serious overhaul, including a rewrite of the bytecode compiler. This problem should be solved once all these update are implemented.

In the meantime, you can either use a named macro (which is better anyway :slight_smile: ) or disable the bytecode compiler (issue the cint command .O 0 ).

Cheers,
Philippe

Thanks for taking the time to look into this :slight_smile: