// -------- ROOT baseDistributions ------------------ // Description: plot basic statistical distributions // // Author: G. Quast, Dec. 2013 // last modified: // // dependencies: ROOT //--------------------------------------------------- #ifndef __CINT__ // These include-statements are needed if the program is #include // run as a "stand-alone application", i.e. if it is not #include // called from an interactive ROOT session #include "TStyle.h" #include "TFile.h" #include "TH1.h" #include "TH2.h" #include "TF1.h" #include "TRandom.h" #include "TMath.h" #include "TCanvas.h" #include "TPad.h" #include "TText.h" //-----end headers ------------------------------------------------- #endif // from here on, the code can also be used as a macro Double_t NormBinomial(Double_t *x, Double_t *par) // Binomial distribution { int n,k; double p,norm; n=par[1]; k=x[0]; p=par[0]; norm=par[2]; return norm*TMath::Binomial(n,k)*TMath::Power(p,k)*TMath::Power(1-p,n-k); } Double_t NormPoisson(Double_t *x, Double_t *par) // Poisson distribution { double norm=par[1], mu=par[0], x0=int(x[0]); return norm*TMath::Poisson(x0,mu); } Double_t NormGauss(Double_t *x, Double_t *par) // Gaussdistribution { double norm=par[2], mu=par[0], sigma=par[1]; return norm*TMath::Gaus(x[0],mu,sigma,kTRUE); } void baseDistributions(double n=1000.,double p=0.05) { // plot basic statistical distribuions //double n=1000., p=0.05; // input // double mu=n*p, sigma=sqrt(n*p*(1.-p)) ; // create canvas and pads TCanvas *c1=new TCanvas("c1","Basic distributions" ,10, 10, 710, 710); TPad *p1=new TPad("Pad3","Pad3",0.0,0.5,0.5,1.); p1->Draw(); TPad *p2=new TPad("Pad4","Pad4",0.5,0.5,1.,1.); p2->Draw(); TPad *p3=new TPad("Pad1","Pad1",0.,0.,0.5,0.5); p3->Draw(); TPad *p4=new TPad("Pad2","Pad2",0.5,0.0,1.,0.5); p4->Draw(); double xmin=mu-3.5*sigma; double xmax=mu+3.5*sigma; //binomial TF1 *binom = new TF1("binom",NormBinomial,xmin,xmax,400); binom->SetTitle("Binomial distribution"); binom->SetParameter(0,p); binom->SetParameter(1,n); binom->SetParameter(2,1.); // normalisation // poisson TF1 *poiss = new TF1("binom",NormPoisson,xmin,xmax,400); poiss->SetTitle("Poisson distribution"); poiss->SetParameter(0,mu); poiss->SetParameter(1,1.); // normalisation //gauss TF1 *gauss = new TF1("NormGauss",NormGauss,xmin,xmax,400); gauss->SetTitle("Normal distribution"); gauss->SetParameter(0,mu); gauss->SetParameter(1,sigma); gauss->SetParameter(2,1.); // normalisation p1->cd(); binom->DrawCopy(); p2->cd(); poiss->DrawCopy(); p3->cd(); gauss->DrawCopy(); p4->cd(); // draw all in same pad p4->SetLogy(); TLegend* legend = new TLegend(0.35, 0.15, 0.65, 0.3); TObject* copied; char txt[50]; sprintf(txt,"p=%1.3g , n=%3g",p,n); binom->SetTitle(txt); binom->SetLineColor(2); copied = binom->DrawCopy(); legend->AddEntry(copied, "Binomial"); poiss->SetLineColor(3); copied = poiss->DrawCopy("SAME"); legend->AddEntry(copied, "Poisson"); gauss->SetLineColor(4); copied = gauss->DrawCopy("SAME"); legend->AddEntry(copied, "Gauss"); legend->Draw(); } #ifndef __CINT__ // needed if the program is compiled stand-alone //_________________________________________________________________ int main() { baseDistributions(); return 0; } #endif