Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:40

0001 /**
0002  * This class takes two histograms and a range. It extracts the specified range from
0003  * each histogram and scales them so that they have a consistent normalization.
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   // pair<TH1*, TProfile*> scale( TH1F * histo1, TProfile * histo2, const double & min, const double & max, const TString & index );
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   // Two overloaded inline methods because SetBinContent does not work with TProfile
0022   inline void fill(TH1F * histo, const int i, const double & value)
0023   {
0024     histo->SetBinContent( i, value );
0025     // histo->Fill( i, value );
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   // T * newHisto = (T*)histo->Clone();
0045   // newHisto->Reset();
0046   // newHisto->SetName(TString(histo->GetName())+"_"+index);
0047   // newHisto->SetTitle(TString(histo->GetTitle())+"_"+index);
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       // std::cout << "first("<<i<<") = " << histo->GetBinContent(i) << std::endl;
0055       std::cout << "first bin center("<<i<<") = " << histo->GetBinCenter(i) << " value = " << histo->GetBinContent(i) << std::endl;
0056     }
0057     // fill(newHisto, i, histo->GetBinContent(i));
0058     newHisto->SetBinContent( i, histo->GetBinContent(i) );
0059     newHisto->SetBinError( i, histo->GetBinError(i) );
0060   }
0061 
0062 //   newHisto->Multiply(maskHisto);
0063 
0064   // newHisto->Scale(1/newHisto->Integral("width"));
0065   // newHisto->Scale(1/newHisto->GetEntries());
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 // pair<TH1*, TProfile*> ScaleFraction::scale( TH1F * histo1, TProfile * histo2, const double & min, const double & max, const TString & index )
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   // TH1F * fakeHisto1 = 0;
0081   // return make_pair(fakeHisto1, scaleOne(histo2, min, max, index));
0082 }