2D slice of 3D hist

Hello. A detailed explanation of what I am trying to do. I have a deposited Energy (dE) vs ADC histogram where dE is in x-axis and ADC is in y-axis. This is 1D histogram and created for mapping purpose.

Next, I have TH3D histogram where number of hits is in x axis, ADC is in y axis and RMS is in z axis.

Finally, I am using dE vs ADC histogram to get ADC, take 2D slice (z and x axis) of TH3D corresponding to that ADC and randomly generate hits and RMS from the distribution (2D slice). Here is my code on slicing part. The code seems to work but only partial as I am getting lots of those error message “h2slice is empty for adcBin: 0”. Could someone please confirm if there is any error in my code? Thank you.

  void useADC(double adc, double &hit, double &rms, TH3D* hist3D)
  {
    double adcBin = hist3D->GetYaxis()->FindBin(adc); //Bin that corresponds to ADC
    hist3D->GetYaxis()->SetRange(adcBin, adcBin); //Range for only this bin in Y-axis 
    TH2D *h2slice = (TH2D*)hist3D->Project3D("zx");
    
    if (h2slice->GetEntries() == 0) {
      std::cout << "ADC: " << adc << std::endl;
      std::cout << "h2slice is empty for adcBin: " << adcBin << std::endl;
      return;
    }   
    
    h2slice->GetRandom2(hit, rms);
    hist3D->GetYaxis()->SetRange(1, hist3D->GetNbinsY()); //resetting the y-axis range back to default
    delete h2slice;  
  }

Maybe @couet can take a look

According to what you say, some of the slices might be empty. Depending on your TH3D, that might be possible. At first glance, your code seems correct.

2 Likes