//______________________________________________________________________________ int SizeOfH(TObject *to) { TClass *kl = to->IsA(); if (!kl) return 0; if (!kl->InheritsFrom(TH1::Class())) return 0; int s0 = kl->Size(); TH1 *h = (TH1 *)to; int nbins = h->GetNbinsX()*h->GetNbinsY()*h->GetNbinsZ(); int szw = 0; if (kl->InheritsFrom("TArrayC")) szw=1; else if (kl->InheritsFrom("TArrayS")) szw=2; else if (kl->InheritsFrom("TArrayI")) szw=4; else if (kl->InheritsFrom("TArrayF")) szw=4; else if (kl->InheritsFrom("TArrayD")) szw=8; return s0 + nbins*szw; }
//______________________________________________________________________________ void TFractionFitter::CheckConsistency() { // Function used internally to check the consistency between the // various histograms. Checks are performed on nonexistent or empty // histograms, the precise histogram class, and the number of bins. // In addition, integrals over the "allowed" bin ranges are computed. // Any inconsistency results in a error. if (! fData) { Error("CheckConsistency","Nonexistent data histogram"); return; } Int_t minX, maxX, minY, maxY, minZ, maxZ; Int_t x,y,z,par; GetRanges(minX, maxX, minY, maxY, minZ, maxZ); fIntegralData = 0; fNpfits = 0; for (z = minZ; z <= maxZ; ++z) { for (y = minY; y <= maxY; ++y) { for (x = minX; x <= maxX; ++x) { if (IsExcluded(fData->GetBin(x, y, z))) continue; fNpfits++; fIntegralData += fData->GetBinContent(x, y, z); } } } if (fIntegralData <= 0) { Error("CheckConsistency","Empty data histogram"); return; } TClass* cl = fData->Class(); fNDF = fNpfits - fNpar; if (fNpar < 2) { Error("CheckConsistency","Need at least two MC histograms"); return; } for (par = 0; par < fNpar; ++par) { TH1 *h = (TH1*)fMCs.At(par); if (! h) { Error("CheckConsistency","Nonexistent MC histogram for source #%d",par); return; } if ((! h->Class()->InheritsFrom(cl)) || h->GetNbinsX() != fData->GetNbinsX() || (fData->GetDimension() > 1 && h->GetNbinsY() != fData->GetNbinsY()) || (fData->GetDimension() > 2 && h->GetNbinsZ() != fData->GetNbinsZ())) { Error("CheckConsistency","Histogram inconsistency for source #%d",par); return; } fIntegralMCs[par] = 0; for (z = minZ; z <= maxZ; ++z) { for (y = minY; y <= maxY; ++y) { for (x = minX; x <= maxX; ++x) { Int_t bin = fData->GetBin(x, y, z); if (IsExcluded(bin)) continue; Double_t MCEvents = h->GetBinContent(bin); if (MCEvents < 0) { Error("CheckConsistency", "Number of MC events (bin = %d, par = %d) cannot be negative: " " their distribution is binomial (see paper)", bin, par); } fIntegralMCs[par] += MCEvents; } } } if (fIntegralMCs[par] <= 0) { Error("CheckConsistency","Empty MC histogram #%d",par); } } }
void Template::makeResidualsControlPlot(const string& tag, unsigned int rebin) /*****************************************************************/ { if(numberOfDimensions()>0 && m_template->GetNbinsX()%rebin!=0) return; if(numberOfDimensions()>1 && m_template->GetNbinsY()%rebin!=0) return; if(numberOfDimensions()>2 && m_template->GetNbinsZ()%rebin!=0) return; stringstream cpName, cpRawName, resMapName, resDistName, relErrDistName; cpName << m_name << "_cp"; cpRawName << m_name << "_rawcp"; resMapName << m_name << "_resmap_" << tag << "_rebin" << rebin; resDistName << m_name << "_resdist_" << tag << "_rebin" << rebin; relErrDistName << m_name << "_relerrdist_" << tag << "_rebin" << rebin; TH1* cpTmp = NULL; TH1* cpRawTmp = NULL; if(rebin==1) { cpTmp = dynamic_cast<TH1*>(m_template->Clone(cpName.str().c_str())); cpRawTmp = dynamic_cast<TH1*>(m_rawTemplate->Clone(cpRawName.str().c_str())); } else { if(numberOfDimensions()==2) { cpTmp = dynamic_cast<TH2F*>(m_template)->Rebin2D(rebin, rebin, cpName.str().c_str()); cpRawTmp = dynamic_cast<TH2F*>(m_rawTemplate)->Rebin2D(rebin, rebin, cpName.str().c_str()); } else if(numberOfDimensions()==3) { cpTmp = dynamic_cast<TH3F*>(m_template)->Rebin3D(rebin, rebin, rebin, cpName.str().c_str()); cpRawTmp = dynamic_cast<TH3F*>(m_rawTemplate)->Rebin3D(rebin, rebin, rebin, cpName.str().c_str()); } } TH1* resMap = dynamic_cast<TH1*>(cpTmp->Clone(resMapName.str().c_str())); TH1D* resDist = new TH1D(resDistName.str().c_str(), resDistName.str().c_str(), 30, -3, 3); resDist->StatOverflows(); TH1D* relErrDist = new TH1D(relErrDistName.str().c_str(), relErrDistName.str().c_str(), 200, -1, 1); relErrDist->StatOverflows(); unsigned int nbins1 = cpTmp->GetNbinsX(); unsigned int nbins2 = cpTmp->GetNbinsY(); unsigned int nbins3 = cpTmp->GetNbinsZ(); for(unsigned int b1=1;b1<=nbins1;b1++) { for(unsigned int b2=1;b2<=nbins2;b2++) { for(unsigned int b3=1;b3<=nbins3;b3++) { double tmpValue = cpTmp->GetBinContent(b1,b2,b3); double tmpRawValue = cpRawTmp->GetBinContent(b1,b2,b3); double tmpRawError = cpRawTmp->GetBinError(b1,b2,b3); if(tmpValue>0. && tmpRawValue>0. && tmpRawError>0.) { double res = (tmpRawValue-tmpValue)/tmpRawError; double relErr = (tmpRawValue-tmpValue)/tmpRawValue; if(numberOfDimensions()==2) { resMap->SetBinContent(b1,b2,b3, res); resMap->SetBinError(b1,b2,b3, 0.); } resDist->Fill(res); relErrDist->Fill(relErr); } } } } stringstream plotMapName, plotDistName, plotErrDistName; plotMapName << "control_" << getName() << "_resMap" << "_" << tag << "_rebin" << rebin; plotDistName << "control_" << getName() << "_resDist" << "_" << tag << "_rebin" << rebin; plotErrDistName << "control_" << getName() << "_relErrDist" << "_" << tag << "_rebin" << rebin; if(numberOfDimensions()==2) { TCanvas* c = new TCanvas(plotMapName.str().c_str(),plotMapName.str().c_str(), 700,700); resMap->SetContour(99); resMap->SetAxisRange(-3., 3., "z"); resMap->Draw("color z"); resMap->SetXTitle(getVariable(0).c_str()); resMap->SetYTitle(getVariable(1).c_str()); addControlPlot(c); } TCanvas* c2 = new TCanvas(plotDistName.str().c_str(),plotDistName.str().c_str(), 700,700); resDist->SetLineColor(kBlack); resDist->SetLineWidth(2); resDist->SetMarkerColor(kBlack); resDist->SetMarkerStyle(20); resDist->SetXTitle("(raw-template)/error_{raw}"); resDist->Draw(); addControlPlot(c2); TCanvas* c3 = new TCanvas(plotErrDistName.str().c_str(),plotErrDistName.str().c_str(), 700,700); relErrDist->SetLineColor(kBlack); relErrDist->SetLineWidth(2); relErrDist->SetMarkerColor(kBlack); relErrDist->SetMarkerStyle(20); relErrDist->SetXTitle("(raw-template)/raw"); relErrDist->Draw(); addControlPlot(c3); if(cpTmp) delete cpTmp; if(cpRawTmp) delete cpRawTmp; }