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