Error in Histogram Variable

Dear Colleagues

I am getting error: use of undeclared identifier ‘i’ float binEnergy = hist3[i]->GetBinContent(ix,iy,iz);

How can I use hist3[i] declared for histogram downstairs in the code. I don’t understand how to do it

  
    TH3F* hist3[1000];
    for(int i=0;i<1000;i++){
    hist3[i] = new TH3F(Form("energy_%d",i), "Total Energy Deposit;x;y;z", nx, xmin, xmax, ny, ymin, ymax, nz, 0, zmax - zmin);
}
 
    // *************************************************************************
    // Event Loop
    // The first for loop to fill 3D histogram
    int nev_max = 1000;
    int nev = 0;
    while (myReader.Next())
    {
        if (nev >= nev_max) break;
        nev++;
        if (nev % 100 == 0) cout << "Event: " << nev << endl;

        float tot_e = 0;
        for (int i = 0; i < energy.GetSize(); i++)
        {
            tot_e += energy[i];
            float z_shifted = abs(z[i]) - zmin;
            h3["env"]->Fill(x[i],y[i],z_shifted,energy[i]);         
        }
          
      // Identify Max Energy Bin
 	   float e_max = 0;
 	   int ix_max = -999;
 	   int iy_max = -999;
 	   int iz_max = -999;
 	   for (int ix = 1; ix <= h3["env"]->GetNbinsX(); ix++)
 	   {
 	       for (int iy = 1; iy <= h3["env"]->GetNbinsY(); iy++)
 	       {
  	          for (int iz = 1; iz <= h3["env"]->GetNbinsZ(); iz++)
   	         {
   	             if (h3["env"]->GetBinContent(ix,iy,iz) > e_max)
   	             {
                     e_max = h3["env"]->GetBinContent(ix,iy,iz);
                    ix_max = ix;
                    iy_max = iy;
                    iz_max = iz;
                }
            }
        }
    }
     
    //identify neighbouring bins
     
  	  float nenergy = 0;
  	  for (int ix = ix_max-1; ix <= ix_max+1; ix++)
   	 {
   	     for (int iy = iy_max-1; iy_max <= iy_max+1; iy++)
    	    {
            for (int iz = iz_max-1; iz_max <= iz_max+1; iz++)
            {
            float binEnergy = hist3[i]->GetBinContent(ix,iy,iz);
            nenergy += binEnergy;
            }
        }
    }

In your code, the “i” variable is defined/known only inside of your “for (int i = ...) { ... }” loops.

I am trying to access the histogram from upstairs

On this line i is undeclared

            float binEnergy = hist3[i]->GetBinContent(ix,iy,iz);

Look at you code. i is the index of the loop … it is known inside the loops only …

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.