TH1* subtractHistograms(const std::string& newHistogramName, const TH1* histogram_central, const TH1* histogram_shift, int mode) { const TH1* histogramMinuend = 0; const TH1* histogramSubtrahend = 0; if ( compIntegral(histogram_central) >= compIntegral(histogram_shift) ) { histogramMinuend = histogram_central; histogramSubtrahend = histogram_shift; } else { histogramMinuend = histogram_shift; histogramSubtrahend = histogram_central; } if ( !(histogramMinuend && histogramSubtrahend) ) return 0; TH1* newHistogram = (TH1*)histogramMinuend->Clone(newHistogramName.data()); newHistogram->Reset(); if ( !newHistogram->GetSumw2N() ) newHistogram->Sumw2(); int numBins = newHistogram->GetNbinsX(); for ( int iBin = 0; iBin <= (numBins + 1); ++iBin ) { double newBinContent = histogramMinuend->GetBinContent(iBin) - histogramSubtrahend->GetBinContent(iBin); double newBinError2 = square(histogramMinuend->GetBinError(iBin)) + square(histogramSubtrahend->GetBinError(iBin)); if ( mode == kRelative ) { if ( histogram_central->GetBinContent(iBin) > 0. ) { newBinContent /= histogram_central->GetBinContent(iBin); newBinError2 /= square(histogram_central->GetBinContent(iBin)); } else { newBinContent = 0.; newBinError2 = 0.; } } newHistogram->SetBinContent(iBin, newBinContent); assert(newBinError2 >= 0.); newHistogram->SetBinError(iBin, TMath::Sqrt(newBinError2)); } return newHistogram; }
void makeBinContentsPositive(TH1* histogram, int verbosity = 0) { if ( verbosity ) { std::cout << "<makeBinContentsPositive>:" << std::endl; std::cout << " integral(" << histogram->GetName() << ") = " << histogram->Integral() << std::endl; } double integral_original = compIntegral(histogram, true, true); if ( integral_original < 0. ) integral_original = 0.; if ( verbosity ) { std::cout << " integral_original = " << integral_original << std::endl; } int numBins = histogram->GetNbinsX(); for ( int iBin = 0; iBin <= (numBins + 1); ++iBin ) { double binContent_original = histogram->GetBinContent(iBin); double binError2_original = square(histogram->GetBinError(iBin)); if ( binContent_original < 0. ) { double binContent_modified = 0.; double binError2_modified = binError2_original + square(binContent_original - binContent_modified); assert(binError2_modified >= 0.); if ( verbosity ) { std::cout << "bin #" << iBin << " (x = " << histogram->GetBinCenter(iBin) << "): binContent = " << binContent_original << " +/- " << TMath::Sqrt(binError2_original) << " --> setting it to binContent = " << binContent_modified << " +/- " << TMath::Sqrt(binError2_modified) << std::endl; } histogram->SetBinContent(iBin, binContent_modified); histogram->SetBinError(iBin, TMath::Sqrt(binError2_modified)); } } double integral_modified = compIntegral(histogram, true, true); if ( integral_modified < 0. ) integral_modified = 0.; if ( verbosity ) { std::cout << " integral_modified = " << integral_modified << std::endl; } if ( integral_modified > 0. ) { double sf = integral_original/integral_modified; if ( verbosity ) { std::cout << "--> scaling histogram by factor = " << sf << std::endl; } histogram->Scale(sf); } else { for ( int iBin = 0; iBin <= (numBins + 1); ++iBin ) { histogram->SetBinContent(iBin, 0.); } } if ( verbosity ) { std::cout << " integral(" << histogram->GetName() << ") = " << histogram->Integral() << std::endl; } }
void dumpHistogram(TH1* histogram) { std::cout << "<dumpHistogram>:" << std::endl; std::cout << " histogram: name = " << histogram->GetName() << ", title = " << histogram->GetTitle() << std::endl; std::cout << " fillColor = " << histogram->GetFillColor() << ", fillStyle = " << histogram->GetFillStyle() << "," << " lineColor = " << histogram->GetLineColor() << ", lineStyle = " << histogram->GetLineStyle() << ", lineWidth = " << histogram->GetLineWidth() << "," << " markerColor = " << histogram->GetMarkerColor() << ", markerStyle = " << histogram->GetMarkerStyle() << ", markerSize = " << histogram->GetMarkerSize() << std::endl; TAxis* xAxis = histogram->GetXaxis(); int numBins = xAxis->GetNbins(); for ( int iBin = 1; iBin <= numBins; ++iBin ) { std::cout << "bin #" << iBin << " (x = " << xAxis->GetBinCenter(iBin) << "): " << histogram->GetBinContent(iBin) << " +/- " << histogram->GetBinError(iBin) << std::endl; } std::cout << "integral = " << compIntegral(histogram, true, true) << std::endl; }
void makePlot_shift_minus_central( const std::string& inputFilePath_ref, const std::string& inputFileName_ref, const std::string& histogramName_ref_shift, const std::string& histogramName_ref_central, const std::string& legendEntry_ref, const std::string& inputFilePath_test, const std::string& inputFileName_test, const std::string& histogramName_test_shift, const std::string& histogramName_test_central, const std::string& legendEntry_test, const std::string& inputFilePath_test2, const std::string& inputFileName_test2, const std::string& histogramName_test2_shift, const std::string& histogramName_test2_central, const std::string& legendEntry_test2, const std::string& outputFileName) { std::cout << "<makePlot_shift_minus_central>:" << std::endl; TString inputFileName_ref_full = inputFilePath_ref; if ( !inputFileName_ref_full.EndsWith("/") ) inputFileName_ref_full.Append("/"); inputFileName_ref_full.Append(inputFileName_ref.data()); TFile* inputFile_ref = new TFile(inputFileName_ref_full.Data()); TH1* histogram_ref_shift = loadHistogram(inputFile_ref, histogramName_ref_shift); TH1* histogram_ref_central = loadHistogram(inputFile_ref, histogramName_ref_central); TH1* histogram_ref_shift_minus_centralAbs = 0; TH1* histogram_ref_shift_minus_centralRel = 0; double integral_ref = -1.; if ( histogram_ref_shift && histogram_ref_central ) { std::string histogramName_ref_shift_minus_centralAbs = Form("%s_minus_%sAbs", histogram_ref_shift->GetName(), histogram_ref_central->GetName()); histogram_ref_shift_minus_centralAbs = subtractHistograms(histogramName_ref_shift_minus_centralAbs, histogram_ref_shift, histogram_ref_central, kAbsolute); std::string histogramName_ref_shift_minus_centralRel = Form("%s_minus_%sRel", histogram_ref_shift->GetName(), histogram_ref_central->GetName()); histogram_ref_shift_minus_centralRel = subtractHistograms(histogramName_ref_shift_minus_centralRel, histogram_ref_shift, histogram_ref_central, kRelative); integral_ref = compIntegral(histogram_ref_shift_minus_centralAbs); std::cout << " integral_ref = " << integral_ref << std::endl; divideByBinWidth(histogram_ref_shift_minus_centralAbs); } TString inputFileName_test_full = inputFilePath_test; if ( !inputFileName_test_full.EndsWith("/") ) inputFileName_test_full.Append("/"); inputFileName_test_full.Append(inputFileName_test.data()); TFile* inputFile_test = ( inputFileName_test_full != inputFileName_ref_full ) ? new TFile(inputFileName_test_full.Data()) : inputFile_ref; TH1* histogram_test_shift = loadHistogram(inputFile_test, histogramName_test_shift); TH1* histogram_test_central = loadHistogram(inputFile_test, histogramName_test_central); TH1* histogram_test_shift_minus_centralAbs = 0; TH1* histogram_test_shift_minus_centralRel = 0; double integral_test = -1.; if ( histogram_test_shift && histogram_test_central ) { std::string histogramName_test_shift_minus_centralAbs = Form("%s_minus_%sAbs", histogram_test_shift->GetName(), histogram_test_central->GetName()); histogram_test_shift_minus_centralAbs = subtractHistograms(histogramName_test_shift_minus_centralAbs, histogram_test_shift, histogram_test_central, kAbsolute); std::string histogramName_test_shift_minus_centralRel = Form("%s_minus_%sRel", histogram_test_shift->GetName(), histogram_test_central->GetName()); histogram_test_shift_minus_centralRel = subtractHistograms(histogramName_test_shift_minus_centralRel, histogram_test_shift, histogram_test_central, kRelative); integral_test = compIntegral(histogram_test_shift_minus_centralAbs); std::cout << " integral_test = " << integral_test << std::endl; divideByBinWidth(histogram_test_shift_minus_centralAbs); } TFile* inputFile_test2 = 0; TH1* histogram_test2_shift_minus_centralAbs = 0; TH1* histogram_test2_shift_minus_centralRel = 0; double integral_test2 = -1.; if ( inputFilePath_test2 != "" && inputFileName_test2 != "" && histogramName_test2_shift != "" && histogramName_test2_central != "" ) { TString inputFileName_test2_full = inputFilePath_test2; if ( !inputFileName_test2_full.EndsWith("/") ) inputFileName_test2_full.Append("/"); inputFileName_test2_full.Append(inputFileName_test2.data()); inputFile_test2 = ( inputFileName_test2_full != inputFileName_ref_full ) ? new TFile(inputFileName_test2_full.Data()) : inputFile_ref; TH1* histogram_test2_shift = loadHistogram(inputFile_test2, histogramName_test2_shift); TH1* histogram_test2_central = loadHistogram(inputFile_test2, histogramName_test2_central); if ( histogram_test2_shift && histogram_test2_central ) { std::string histogramName_test2_shift_minus_centralAbs = Form("%s_minus_%sAbs", histogram_test2_shift->GetName(), histogram_test2_central->GetName()); histogram_test2_shift_minus_centralAbs = subtractHistograms(histogramName_test2_shift_minus_centralAbs, histogram_test2_shift, histogram_test2_central, kAbsolute); std::string histogramName_test2_shift_minus_centralRel = Form("%s_minus_%sRel", histogram_test2_shift->GetName(), histogram_test2_central->GetName()); histogram_test2_shift_minus_centralRel = subtractHistograms(histogramName_test2_shift_minus_centralRel, histogram_test2_shift, histogram_test2_central, kRelative); integral_test2 = compIntegral(histogram_test2_shift_minus_centralAbs); std::cout << " integral_test2 = " << integral_test2 << std::endl; divideByBinWidth(histogram_test2_shift_minus_centralAbs); } } size_t idx = outputFileName.find_last_of('.'); std::string outputFileName_abs = std::string(outputFileName, 0, idx); outputFileName_abs.append("_absolute"); if ( idx != std::string::npos ) outputFileName_abs.append(std::string(outputFileName, idx)); showHistograms(800, 900, histogram_ref_shift_minus_centralAbs, legendEntry_ref, integral_ref, histogram_test_shift_minus_centralAbs, legendEntry_test, integral_test, histogram_test2_shift_minus_centralAbs, legendEntry_test2, integral_test2, //NULL, "", 0., NULL, "", 0., NULL, "", 0., NULL, "", 0., "m_{#tau#tau} [GeV]", 1.10, true, 1.e-4, 1.e+4, "1/dm_{#tau#tau} [1/GeV]", 1.30, 0.34, 0.72, outputFileName_abs.data()); std::string outputFileName_rel = std::string(outputFileName, 0, idx); outputFileName_rel.append("_relative"); if ( idx != std::string::npos ) outputFileName_rel.append(std::string(outputFileName, idx)); showHistograms(800, 900, histogram_ref_shift_minus_centralRel, legendEntry_ref, integral_ref, histogram_test_shift_minus_centralRel, legendEntry_test, integral_test, histogram_test2_shift_minus_centralRel, legendEntry_test2, integral_test2, //NULL, "", 0., NULL, "", 0., NULL, "", 0., NULL, "", 0., "m_{#tau#tau} [GeV]", 1.10, true, 1.e-4, 1.e+4, "#Delta 1/dm_{#tau#tau}", 1.30, 0.34, 0.72, outputFileName_rel.data()); delete inputFile_ref; if ( inputFile_test != inputFile_ref ) delete inputFile_test; if ( inputFile_test2 != inputFile_ref && inputFile_test2 != inputFile_test ) delete inputFile_test2; }
void makePlot( const std::string& inputFilePath_ref, const std::string& inputFileName_ref, const std::string& histogramName_ref, const std::string& legendEntry_ref, const std::string& inputFilePath_test, const std::string& inputFileName_test, const std::string& histogramName_test, const std::string& legendEntry_test, const std::string& inputFilePath_test2, const std::string& inputFileName_test2, const std::string& histogramName_test2, const std::string& legendEntry_test2, const std::string& outputFileName) { std::cout << "<makePlot_shift_minus_central>:" << std::endl; TString inputFileName_ref_full = inputFilePath_ref; if ( !inputFileName_ref_full.EndsWith("/") ) inputFileName_ref_full.Append("/"); inputFileName_ref_full.Append(inputFileName_ref.data()); TFile* inputFile_ref = new TFile(inputFileName_ref_full.Data()); TH1* histogram_ref = loadHistogram(inputFile_ref, histogramName_ref); double integral_ref = compIntegral(histogram_ref); divideByBinWidth(histogram_ref); TString inputFileName_test_full = inputFilePath_test; if ( !inputFileName_test_full.EndsWith("/") ) inputFileName_test_full.Append("/"); inputFileName_test_full.Append(inputFileName_test.data()); TFile* inputFile_test = ( inputFileName_test_full != inputFileName_ref_full ) ? new TFile(inputFileName_test_full.Data()) : inputFile_ref; TH1* histogram_test = loadHistogram(inputFile_test, histogramName_test); double integral_test = compIntegral(histogram_test); divideByBinWidth(histogram_test); TFile* inputFile_test2 = 0; TH1* histogram_test2 = 0; double integral_test2 = -1.; if ( inputFilePath_test2 != "" && inputFileName_test2 != "" && histogramName_test2 != "" ) { TString inputFileName_test2_full = inputFilePath_test2; if ( !inputFileName_test2_full.EndsWith("/") ) inputFileName_test2_full.Append("/"); inputFileName_test2_full.Append(inputFileName_test2.data()); inputFile_test2 = ( inputFileName_test2_full != inputFileName_ref_full ) ? new TFile(inputFileName_test2_full.Data()) : inputFile_ref; histogram_test2 = loadHistogram(inputFile_test2, histogramName_test2); integral_test2 = compIntegral(histogram_test2); divideByBinWidth(histogram_test2); } showHistograms(800, 900, histogram_ref, legendEntry_ref, integral_ref, histogram_test, legendEntry_test, integral_test, histogram_test2, legendEntry_test2, integral_test2, //NULL, "", 0., NULL, "", 0., NULL, "", 0., NULL, "", 0., "m_{#tau#tau} [GeV]", 1.10, true, 1.e-4, 1.e+4, "1/dm_{#tau#tau} [1/GeV]", 1.30, 0.34, 0.72, outputFileName.data()); TH1* histogramErr_ref = compHistogramErr(histogram_ref); TH1* histogramErr_test = compHistogramErr(histogram_test); TH1* histogramErr_test2 = ( histogram_test2 ) ? compHistogramErr(histogram_test2) : 0; std::string outputFileNameErr = TString(outputFileName.data()).ReplaceAll(".png", "_Err.png").Data(); showHistograms(800, 900, histogramErr_ref, legendEntry_ref, -1., histogramErr_test, legendEntry_test, -1., histogramErr_test2, legendEntry_test2, -1., //NULL, "", 0., NULL, "", 0., NULL, "", 0., NULL, "", 0., "m_{#tau#tau} [GeV]", 1.10, true, 1.e-4, 1.e+4, "#sigma(1/dm_{#tau#tau}) [1/GeV]", 1.30, 0.34, 0.72, outputFileNameErr.data()); delete histogramErr_ref; delete histogramErr_test; delete histogramErr_test2; delete inputFile_ref; if ( inputFile_test != inputFile_ref ) delete inputFile_test; if ( inputFile_test2 != inputFile_ref && inputFile_test2 != inputFile_test ) delete inputFile_test2; }