Projecting a 2D histogram down WITH Sumw2

Hi all,

I have a TH2 that has some binning in the X and Y dimensions. I want to be able to project this histogram down into one dimension with arbitrary binning. I can do this just fine, except for I do not understand how to get the bin error added in properly.

I have put an example below that assumes a TH2 that has log bins from 0.01 to 100 in both X and Y and 100 bins per energy decade…so a 400x400 TH2…In the following example the naming convention goes like:

h : The original 2D histo
hcopy: A cloned version of h
h1d : The rebinned 1D projection of h


////////Define Some variables/////////////////
  double MinEinc = 0.7;
  double MaxEinc = 5.0;

  double MinEout =0.01;
  double MaxEout = 2.5;

  int NbinsPerDecade= 100;
//////////////END OF DEFINING VARIABLES///////////

///FIGURE OUT BINNING USING LOG MAGIC/////////////
  double nDecades = log10(MaxEout) - log10(MinEout);
  int nBins = NbinsPerDecade*nDecades+1;
  Double_t x[nBins+1] = {0};                                     
  for(int i=0;i<=nBins;i++)
    {
      double id = static_cast<double>(i);
      x[i] = pow(10.,log10(MinEout)+id/NbinsPerDecade);
    }

  //Create the empty 1D histos                                                                                                       
  TH1D *h1d = new TH1D("h1d","h1d",nBins,x);
////////////////////////DONE FIGURING OUT BINS//////////////////////////


  /////////////////////////////                                                                                                      
  //Project the Data down///////                                                                                                       
  ////////////////////////////                                                                                                       
  TH2* hcopy =(TH2*)h->Clone();
  projectiony = hcopy->ProjectionY("py",
                                    hcopy->GetXaxis()->FindBin(MinEinc),
                                    hcopy->GetXaxis()->FindBin(MaxEinc));


  for (int biniter=projectiony->FindBin(MinEout);
           biniter<=projectiony->FindBin(MaxEout);
           biniter++)
         {
           //h1d->AddBinContent(h1d->GetXaxis()->FindBin(projectiony->GetBinCenter(biniter)),                                      
           //                  projectiony->GetBinContent(biniter));                                                                 

           h1d->Fill(projectiony->GetBinCenter(biniter),
                      projectiony->GetBinContent(biniter));
         }//end of looping over 1D bins   

So again, how do I make it so ‘h1d’ has the correct Sumw2 information from ‘h’

thanks for any help!

TArrayD *Errors = projectiony->GetSumw2();
Double_t *fgErrors = Errors->GetArray();

  for (int biniter=projectiony->FindBin(MinEout);
           biniter<=projectiony->FindBin(MaxEout);
           biniter++)
         {
          
           h1d->Fill(projectiony->GetBinCenter(biniter),
                      projectiony->GetBinContent(biniter));
         
         
         h1d->SetBinError(projectiony->GetBinCenter(biniter),
                                   h1d->GetBinError(h1d->FindBin(projectiony->GetBinCenter(biniter)))
         	                   +(fgErrors[biniter]));
         
         }//end of looping over 1D bins