Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-04 22:54:39

0001 #include <cmath>
0002 #include "TH1D.h"
0003 #include "TProfile2D.h"
0004 
0005 TH1D* projectProfile2DAlongX(TProfile2D* prof2d) {
0006   TH1D* res = nullptr;
0007 
0008   if (prof2d) {
0009     char name[200];
0010     sprintf(name, "%s_proj", prof2d->GetName());
0011     res = new TH1D(
0012         name, prof2d->GetTitle(), prof2d->GetNbinsY(), prof2d->GetYaxis()->GetXmin(), prof2d->GetYaxis()->GetXmax());
0013     res->SetDirectory(nullptr);
0014     res->Sumw2();
0015     for (int iy = 1; iy < prof2d->GetNbinsY() + 1; ++iy) {
0016       double sum = 0.;
0017       double sumsq = 0.;
0018       double nevt = 0.;
0019       for (int ix = 1; ix < prof2d->GetNbinsX() + 1; ++ix) {
0020         const int ibin = prof2d->GetBin(ix, iy);
0021         sum += prof2d->GetBinContent(ibin) * prof2d->GetBinEntries(ibin);
0022         sumsq += prof2d->GetBinError(ibin) * prof2d->GetBinError(ibin) * prof2d->GetBinEntries(ibin) *
0023                      prof2d->GetBinEntries(ibin) +
0024                  prof2d->GetBinContent(ibin) * prof2d->GetBinContent(ibin) * prof2d->GetBinEntries(ibin);
0025         nevt += prof2d->GetBinEntries(ibin);
0026       }
0027       double mean = nevt == 0 ? 0 : sum / nevt;
0028       double meansq = nevt == 0 ? 0 : sumsq / nevt;
0029       double err = meansq >= mean * mean ? sqrt(meansq - mean * mean) : 0;
0030       err = nevt == 0 ? 0 : err / sqrt(nevt);
0031       res->SetBinContent(iy, mean);
0032       res->SetBinError(iy, err);
0033     }
0034   }
0035 
0036   return res;
0037 }
0038 
0039 TH1D* projectProfile2DAlongY(TProfile2D* prof2d) {
0040   TH1D* res = nullptr;
0041 
0042   if (prof2d) {
0043     char name[200];
0044     sprintf(name, "%s_proj", prof2d->GetName());
0045     res = new TH1D(
0046         name, prof2d->GetTitle(), prof2d->GetNbinsX(), prof2d->GetXaxis()->GetXmin(), prof2d->GetXaxis()->GetXmax());
0047     res->SetDirectory(nullptr);
0048     res->Sumw2();
0049     for (int ix = 1; ix < prof2d->GetNbinsX() + 1; ++ix) {
0050       double sum = 0.;
0051       double sumsq = 0.;
0052       double nevt = 0.;
0053       for (int iy = 1; iy < prof2d->GetNbinsY() + 1; ++iy) {
0054         const int ibin = prof2d->GetBin(ix, iy);
0055         sum += prof2d->GetBinContent(ibin) * prof2d->GetBinEntries(ibin);
0056         sumsq += prof2d->GetBinError(ibin) * prof2d->GetBinError(ibin) * prof2d->GetBinEntries(ibin) *
0057                      prof2d->GetBinEntries(ibin) +
0058                  prof2d->GetBinContent(ibin) * prof2d->GetBinContent(ibin) * prof2d->GetBinEntries(ibin);
0059         nevt += prof2d->GetBinEntries(ibin);
0060       }
0061       double mean = nevt == 0 ? 0 : sum / nevt;
0062       double meansq = nevt == 0 ? 0 : sumsq / nevt;
0063       double err = meansq >= mean * mean ? sqrt(meansq - mean * mean) : 0;
0064       err = nevt == 0 ? 0 : err / sqrt(nevt);
0065       res->SetBinContent(ix, mean);
0066       res->SetBinError(ix, err);
0067     }
0068   }
0069 
0070   return res;
0071 }