ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/PandiTools.C
Revision: 1.1
Committed: Mon Mar 18 16:06:07 2013 UTC (12 years, 1 month ago) by buchmann
Content type: text/plain
Branch: MAIN
Log Message:
Added tools for fitting uncertainty band provided by francesco (pandolfi)

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <math.h>
3     #include <TH1.h>
4     #include <TF1.h>
5     #include <TMatrixD.h>
6     #include <TMinuit.h>
7    
8    
9    
10     // Create uncertainty band (histogram) for a given function and error matrix
11     // in the range of the function.
12     TH1D* getBand(TF1 *f, TMatrixD const& m, std::string name, bool getRelativeBand, int npx) {
13    
14     Bool_t islog = false;
15     double xmin = f->GetXmin();
16     double xmax = f->GetXmax()*1.1; //fixes problem in drawing with c option
17     int npar = f->GetNpar();
18    
19     // Create binning (linear or log)
20     Double_t xvec[npx];
21     xvec[0] = xmin;
22     double dx = (islog ? pow(xmax/xmin, 1./npx) : (xmax-xmin)/npx);
23     for (int i = 0; i != npx; ++i) {
24     xvec[i+1] = (islog ? xvec[i]*dx : xvec[i]+dx);
25     }
26    
27    
28     //
29     // Compute partial derivatives numerically
30     // can be used with any fit function
31     //
32     Double_t sigmaf[npx];
33     TH1D* h1_band = new TH1D(name.c_str(), "", npx, xvec);
34    
35     for( unsigned ipx=0; ipx<=npx; ++ipx ) {
36    
37     sigmaf[ipx] = 0.;
38     Double_t partDeriv[npar];
39    
40     //compute partial derivatives of f wrt its parameters:
41     for( unsigned ipar=0; ipar<npar; ++ipar ) {
42    
43     Float_t pi = f->GetParameter(ipar);
44     Float_t dpi = sqrt(m[ipar][ipar])*0.01; //small compared to the par sigma
45     f->SetParameter(ipar, pi+dpi);
46     Float_t fplus = f->Eval(xvec[ipx]);
47     f->SetParameter(ipar, pi-dpi);
48     Float_t fminus = f->Eval(xvec[ipx]);
49     f->SetParameter(ipar, pi); //put it back as it was
50    
51     partDeriv[ipar] = (fplus-fminus)/(2.*dpi);
52    
53     } //for params
54    
55     //compute sigma(f) at x:
56     for( unsigned ipar=0; ipar<npar; ++ipar ) {
57     for( unsigned jpar=0; jpar<npar; ++jpar ) {
58     sigmaf[ipx] += partDeriv[ipar]*partDeriv[jpar]*m[ipar][jpar];
59     }
60     }
61     sigmaf[ipx] = sqrt(sigmaf[ipx]); //absolute band
62    
63     h1_band->SetBinContent( ipx, f->Eval(xvec[ipx]) );
64     if( getRelativeBand )
65     h1_band->SetBinError( ipx, sigmaf[ipx]/f->Eval(xvec[ipx]) );
66     else
67     h1_band->SetBinError( ipx, sigmaf[ipx] );
68    
69     } //for points
70    
71    
72     return h1_band;
73    
74     } //getband
75    
76     TH1D* getBand( TF1* f, const std::string& name ) {
77     const int ndim_resp_q = f->GetNpar();
78     TMatrixD emat_resp_q(ndim_resp_q, ndim_resp_q);
79     gMinuit->mnemat(&emat_resp_q[0][0], ndim_resp_q);
80    
81     return getBand(f, emat_resp_q, name,false,50);
82     }
83