Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef Legend_h
0002 #define Legend_h
0003 
0004 #include "TString.h"
0005 #include "TPaveText.h"
0006 #include "TF1.h"
0007 
0008 #include <iomanip>
0009 #include <sstream>
0010 
0011 /**
0012  * Small function to simplify the creation of text in the TPaveText. <br>
0013  * It automatically writes the corret number of figures.
0014  */
0015 TString setText(const char * text, const double & num1, const char * divider = "", const double & num2 = 0)
0016 {
0017 
0018   // Counter gives the precision
0019   int precision = 1;
0020   int k=1;
0021   while( int(num2*k) == 0 ) {
0022     k*=10;
0023     ++precision;
0024   }
0025 
0026   std::stringstream numString;
0027   TString textString(text);
0028   numString << std::setprecision(precision) << std::fixed << num1;
0029   textString += numString.str();
0030   if( num2 != 0 ) {
0031     textString += divider;
0032     numString.str("");
0033     if( std::string(text).find("ndf") != std::string::npos ) precision = 0;
0034     numString << std::setprecision(precision) << std::fixed << num2;
0035     textString += numString.str();
0036   }
0037   return textString;
0038 }
0039 
0040 /// This function sets up the TPaveText with chi2/ndf and parameters values +- errors
0041 void setTPaveText(const TF1 * fit, TPaveText * paveText) {
0042   Double_t chi2 = fit->GetChisquare();
0043   Int_t ndf = fit->GetNDF();
0044   paveText->AddText(setText("#chi^2 / ndf = ", chi2,  " / ", ndf));
0045 
0046   for( int iPar=0; iPar<fit->GetNpar(); ++iPar ) {
0047     TString parName(fit->GetParName(iPar));
0048     Double_t parValue = fit->GetParameter(iPar); // value of Nth parameter
0049     Double_t parError = fit->GetParError(iPar);  // error on Nth parameter
0050     paveText->AddText(setText(parName + " = ", parValue, " #pm ", parError));
0051   }
0052 }
0053 
0054 /**
0055  * This class can be used to create two legends which are then filled with the results of the fit
0056  * for two functions.
0057  */
0058 class TwinLegend
0059 {
0060  public:
0061   TwinLegend() : drawSecondLegend_(true)
0062   {
0063     paveText1_ = new TPaveText(0.20,0.15,0.49,0.28,"NDC");
0064     paveText1_->SetFillColor(0);
0065     paveText1_->SetTextColor(1);
0066     paveText1_->SetTextSize(0.04);
0067     paveText1_->SetBorderSize(1);
0068 
0069     paveText2_ = new TPaveText(0.59,0.15,0.88,0.28,"NDC");
0070     paveText2_->SetFillColor(0);
0071     paveText2_->SetTextColor(2);
0072     paveText2_->SetTextSize(0.04);
0073     paveText2_->SetBorderSize(1);
0074   }
0075 
0076   void setText(TF1 * fit1, TF1 * fit2)
0077   {
0078     setTPaveText(fit1, paveText1_);
0079     if( fit2 != 0 ) {
0080       setTPaveText(fit2, paveText2_);
0081     }
0082     else {
0083       drawSecondLegend_ = false;
0084     }
0085   }
0086 
0087   void Draw(const TString & option)
0088   {
0089     paveText1_->Draw(option);
0090     if( drawSecondLegend_ ) {
0091       paveText2_->Draw(option);
0092     }
0093   }
0094 
0095   TPaveText * paveText1_;
0096   TPaveText * paveText2_;
0097   bool drawSecondLegend_;
0098 };
0099 
0100 #endif