File indexing completed on 2024-04-06 12:22:45
0001
0002
0003
0004
0005
0006 #include "TH1F.h"
0007 #include "TProfile.h"
0008 #include <utility>
0009 #include <iostream>
0010
0011
0012 class ScaleFraction
0013 {
0014 public:
0015 ScaleFraction() {};
0016
0017 pair<TH1*, TH1*> scale( TH1F * histo1, TProfile * histo2, const double & min, const double & max, const TString & index );
0018 protected:
0019 template <class T>
0020 TH1 * scaleOne( T * histo, const double & min, const double & max, const TString & index );
0021
0022 inline void fill(TH1F * histo, const int i, const double & value)
0023 {
0024 histo->SetBinContent( i, value );
0025
0026 }
0027 inline void fill(TProfile * histo, const double & x, const double & y)
0028 {
0029 std::cout << "inside fill: x = " << x << ", y = " << y << std::endl;
0030 histo->Fill( x, y );
0031 }
0032 };
0033
0034 template <class T>
0035 TH1 * ScaleFraction::scaleOne( T * histo, const double & min, const double & max, const TString & index )
0036 {
0037 int minBin = histo->FindBin(min);
0038 int maxBin = histo->FindBin(max);
0039
0040 std::cout << "For " << histo->GetName() << std::endl;
0041 std::cout << "minBin = " << minBin << std::endl;
0042 std::cout << "maxBin = " << maxBin << std::endl;
0043
0044
0045
0046
0047
0048
0049 TH1F * newHisto = new TH1F(TString(histo->GetName())+"_"+index, TString(histo->GetTitle())+"_"+index,
0050 histo->GetNbinsX(), histo->GetXaxis()->GetXmin(), histo->GetXaxis()->GetXmax());
0051
0052 for( int i=minBin; i<=maxBin; ++i ) {
0053 if( histo->GetBinContent(i) != 0 ) {
0054
0055 std::cout << "first bin center("<<i<<") = " << histo->GetBinCenter(i) << " value = " << histo->GetBinContent(i) << std::endl;
0056 }
0057
0058 newHisto->SetBinContent( i, histo->GetBinContent(i) );
0059 newHisto->SetBinError( i, histo->GetBinError(i) );
0060 }
0061
0062
0063
0064
0065
0066
0067 for( int i=minBin; i<=maxBin; ++i ) {
0068 if( newHisto->GetBinContent(i) != 0 ) {
0069 std::cout << "first("<<i<<") = " << newHisto->GetBinContent(i) << std::endl;
0070 }
0071 }
0072
0073 return newHisto;
0074 }
0075
0076
0077 pair<TH1*, TH1*> ScaleFraction::scale( TH1F * histo1, TProfile * histo2, const double & min, const double & max, const TString & index )
0078 {
0079 return make_pair(scaleOne(histo1, min, max, index), scaleOne(histo2, min, max, index));
0080
0081
0082 }