void erfFit(pair<int, double> bump, vector<pair<int, double> > shower, 
	    TH1D* energyResoX, TH1D* energyResoY, TH1D* posResoX,
	    TH1D* posResoY)
 {
   TF1* erfX = new TF1("erfX", erfPulse, -5, 5, 3);
   erfX->SetParameters(IDtoX(bump.first), 1., bump.second);

   TF1* erfY = new TF1("erfY", erfPulse, -5, 5, 3); 
   erfY->SetParameters(IDtoY(bump.first), 1., bump.second);
 
   TH2D* histo = new TH2D("histo", "ShowerFit",
			  11, -5, 5,
			  11, -5, 5);


   vector<pair<int, double> >::iterator b; 
   for (b=shower.begin(); b!=shower.end(); b++)
	 {
	   histo->Fill(IDtoX(b->first), IDtoY(b->first), b->second);
	 }
   TH1D* histoX = histo->ProjectionX();
   TH1D* histoY = histo->ProjectionY();
   histoX->Fit(erfX, "Q");
   energyResoX->Fill(erfX->Integral(-5, 5)-energyGamma);
   posResoX->Fill(erfX->GetParameter(0));
   histoY->Fit(erfY, "Q");
   energyResoY->Fill(erfY->Integral(-5, 5)-energyGamma);
   posResoY->Fill(erfY->GetParameter(0));
 `
			  
 }
Exemple #2
0
void fitBarea(TString infname="",bool doweight = 1)
{
  if (doweight==0) weight="1";
  if (infname=="") infname=inputdata.Data();
  TFile *inf = new TFile(infname.Data());
  TTree *nt = (TTree*) inf->Get("ntKp");

  TFile *infMC = new TFile(inputmc.Data());
  TTree *ntGen = (TTree*)infMC->Get("ntGen");
  TTree *ntGen2 = (TTree*)inf->Get("ntGen");
  TTree *ntMC = (TTree*)infMC->Get("ntKp");
    
  ntGen->AddFriend(ntMC);
  ntGen2->AddFriend(ntMC);
    
  const int nBins = 1;
  double ptBins[nBins+1] = {10,60};
//  const int nBins = 1;
//  double ptBins[nBins+1] = {10,60};
  TH1D *hPt = new TH1D("hPt","",nBins,ptBins);

  for (int i=0;i<nBins;i++)
    {
      TF1 *f = fit(nt,ntMC,ptBins[i],ptBins[i+1]);
      double yield = f->Integral(5,6)/0.02;
      double yieldErr = f->Integral(5,6)/0.02*f->GetParError(0)/f->GetParameter(0);
      hPt->SetBinContent(i+1,yield/(ptBins[i+1]-ptBins[i]));
      hPt->SetBinError(i+1,yieldErr/(ptBins[i+1]-ptBins[i]));
    }
  
}
void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
{

  Double_t Lsum=0.;
  Double_t Nevt=dataColl.size();
  Double_t fs = par[0];
  Double_t fb = par[1];

  double tmppar[12];
  for(int i=0; i<12; i++){
    tmppar[i] = par[i+2];
  }
  TF1 *funcS = new TF1("funcS", exp_conv, -1., 20., 12);
  funcS->SetParameters(tmppar);
  Double_t Signorm = funcS->Integral(-1., 20.);
  TF1 *funcB = new TF1("funcB", expinv_power, -1., 20., 12);
  funcB->SetParameters(tmppar);
  Double_t Bkgnorm = funcB->Integral(-1., 20.);

  SigPDFnorm = Signorm;
  BkgPDFnorm = Bkgnorm;

  for ( int i=0; i<dataColl.size(); i++ ) {
    //PDF for signal and background
    Double_t x = dataColl[i];
    Double_t Ls = exp_conv(&x,tmppar)/Signorm;
    Double_t Lb = expinv_power(&x,tmppar)/Bkgnorm;
    //Get Log Likelihood		  
    Lsum += TMath::Log( (fs*Ls + fb*Lb) / (fs+fb) );
  }
  //f=2*( -1*Lsum + (fs+fb) - Nevt*TMath::Log(fs+fb) );
  f=2*( -1*Lsum + (fs+fb) - Nevt*TMath::Log(fs+fb) );
}
Exemple #4
0
void plot_diff_xsec()
{
  TF1* fXsec = new TF1("fXsec", "diff_xsec(x)", 1, 0);
  fXsec->SetLineWidth(3);
  fXsec->SetTitle("#nu_{#mu}-e differential cross section (E_{#nu} = 2 GeV)");
  fXsec->Draw();
  fXsec->GetXaxis()->SetTitle("cos#theta");
  fXsec->GetXaxis()->CenterTitle();
  fXsec->GetYaxis()->SetTitle("d#sigma/dcos#theta (cm^{-2})");
  fXsec->GetYaxis()->CenterTitle();
  fXsec->GetYaxis()->SetTitleOffset(1.4);
  gPad->SetLogy(1);
  
  double totXsec = fXsec->Integral(0,1);
  for(int i = 0; i < 100; i++)
  {
    cout << fXsec->Integral(0.01*i,1)/totXsec << endl;
  }
  cout << totXsec << " " << fXsec->Integral(.99,1) << endl;
  
  ROOT::Math::GSLIntegrator ig(1.E-6,1.E-6,1000); 
  ROOT::Math::WrappedTF1 wf(*fXsec);
  ig.SetFunction(wf);
  cout << ig.Integral(.9,1) << endl;
  
  //~ TCanvas* c2 = new TCanvas("c2");
  //~ TF1* fCXsec = new TF1("fCXsec", "cumul_xsec(x)", 0, 90);
  //~ fCXsec->Draw();
  //~ cout << cumul_xsec(45) << endl;
}
Exemple #5
0
Double_t calc_dNdY(const char *data_file = "pt_RFE", Bool_t show = kFALSE)
{
  TGraphErrors *g = new TGraphErrors(data_file);
  if (g->IsZombie()) return;

  TF1 *flt = new TF1("flt", LevyTsallis, 0., 5., 4);
  flt->SetParameters(2.93483e-02, 2.80382e-01, 8.10224e+00, 1.01944e+00);
  flt->FixParameter(3, 1.019445);

  Double_t fitmin = 0.25, fitmax = 5.25; // !!!
  g->Fit(flt, "Q", "", fitmin, fitmax);
  g->Fit(flt, "Q", "", fitmin, fitmax);
  g->Fit(flt, "Q", "", fitmin, fitmax);

  Double_t first, last;
  // !!! bining sensibility !!!
  Int_t graph_range[2] = {2, 23}; // [2] = 0.05 - 0.01, [23] = 4.70 + 0.01

  // interval 1
  first = 0.0;   // !!!
  last  = g->GetX()[graph_range[0]] - g->GetErrorX(graph_range[0]);
  Double_t fi1 = flt->Integral(first, last);

  // interval 2
  first = last;
  last  = g->GetX()[graph_range[1]] + g->GetErrorX(graph_range[1]);
  Double_t fi2 = flt->Integral(first, last);
  Double_t gi2 = 0.0;
  for (Int_t ip = graph_range[0]; ip <= graph_range[1]; ip++)
    gi2 += g->GetY()[ip]*g->GetErrorX(ip)*2.0;

  // interval 3
  first = last;
  last  = 30.0;   // !!!
  Double_t fi3 = flt->Integral(first, last);

  Double_t result = fi1 + gi2 + fi3;

  if (!show) {
    delete g;
    delete flt;
    return result;
  }

  Printf("function: %f \t %f \t %f", fi1, fi2, fi3);
  Printf("graph: \t \t \t %f", gi2);
  Printf("result => %f", result);
  g->Draw("AP");
  gPad->SetLogy();
  return result;
}
Exemple #6
0
void fitDdecreasewid(TString collsyst="PbPb", Float_t centMin=0, Float_t centMax=100, TString outputfile="outfMasshisto/mass")
{
  collisionsystem = collsyst;
  infname = outputfile;
  centmin = centMin;
  centmax = centMax;

  gStyle->SetTextSize(0.05);
  gStyle->SetTextFont(42);
  gStyle->SetPadRightMargin(0.043);
  gStyle->SetPadLeftMargin(0.18);
  gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadBottomMargin(0.145);
  gStyle->SetTitleX(.0f);

  TF1* fit (Float_t ptmin, Float_t ptmax);
  ofstream fout(Form("outYield/decreasewid_%s_cent_%.0f_%.0f.dat",collsyst.Data(),centmin,centmax));
  for(int i=0;i<nBins;i++)
    {
      TF1* f = fit(ptBins[i],ptBins[i+1]);
      Float_t yield = f->Integral(minhisto,maxhisto)/binwidthmass;
      fout<<ptBins[i]<<" "<<ptBins[i+1]<<" "<<yield<<endl;
    }
  fout.close();
}
Exemple #7
0
void myfit()
{
   TString dir = gSystem->UnixPathName(__FILE__);
   dir.ReplaceAll("myfit.C","../hsimple.C");
   dir.ReplaceAll("/./","/");
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *hsimple = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
   if (!hsimple) return;

   TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);

   TH1F *hpx = (TH1F*)hsimple->Get("hpx");

// Creates a Root function based on function fitf above
   TF1 *func = new TF1("fitf",fitf,-2,2,3);

// Sets initial values and parameter names
   func->SetParameters(100,0,1);
   func->SetParNames("Constant","Mean_value","Sigma");

// Fit histogram in range defined by function
   hpx->Fit(func,"r");

// Gets integral of function between fit limits
   printf("Integral of function = %g\n",func->Integral(-2,2));
}
double intXS(double *ecm, double *par)
{
    double res=par[3];
    double hs =par[5];

    TF1 *fXS = new TF1("fXS", rescs, 3.05, 3.12, 8);
    fXS->SetParameter(0, par[0]); //mass of Jpsi
    fXS->SetParameter(1, par[1]); //total width of Jpsi
    fXS->SetParameter(2, par[2]); //Gamma_ee
    fXS->SetParameter(3, par[3]); //Gauss resolution
    fXS->SetParameter(4, ecm[0]); //initial energy
    fXS->SetParameter(5, par[4]); // BfKsX
    fXS->SetParameter(6, par[5]); // h(s) for sigma_mumu 
    fXS->SetParameter(7, par[6]); // phase
    double my_intXS=fXS->Integral(TMath::Min(ecm[0],par[0])-20*res, TMath::Max(ecm[0],par[0])+20*res);


    double beta = 2.0*alpha/pi*(2.0*log(ecm[0]/me)-1.0);
    double delta= 0.75*beta+beta*beta/24.0*(1.0/3*log(ecm[0]/me/me)+2.0*pi*pi-37.0/4);
    double epsilon = 1.0e-8;
    double emin = 1.4;
    double UPL = 1.0-4.0*emin*emin/(ecm[0]*ecm[0]);
    TF1 *fXS_funs = new TF1("fXS_funs", funs, 0.0, 1.0, 1);
    fXS_funs->SetParameter(0, ecm[0]); //initial energy
    double my_intXS_qed1 = fXS_funs->Integral(epsilon, UPL);
    double my_intXS_qed2 = TMath::Power(epsilon,beta)*qedxs(ecm[0]*ecm[0]);//(86.8/(ecm[0]*ecm[0])), born cross section of mu+mu-
    TF1 *fXS_funh = new TF1("fXS_funh", funh, 0.0, 1.0, 1);
    fXS_funh->SetParameter(0, ecm[0]); //initial energy
    double my_intXS_qed3 = fXS_funh->Integral(0.0, UPL);
    double my_intXS_qed = hs*( my_intXS_qed3); //

    return my_intXS+my_intXS_qed;

}
Exemple #9
0
void fitMassHisto(TString inputfile="outfiles/MassHisto", TString outputfile="outfiles/YieldHisto",
                  Float_t centmin=0., Float_t centmax=100.)
{
  infname = inputfile;
  centMin = centmin;
  centMax = centmax;

  gStyle->SetTextSize(0.05);
  gStyle->SetTextFont(42);
  gStyle->SetPadRightMargin(0.043);
  gStyle->SetPadLeftMargin(0.18);
  gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadBottomMargin(0.145);
  gStyle->SetTitleX(.0f);

  TF1* fit(TH1D* h, TH1D* hMCSignal, TH1D* hMCSwapped, Float_t ptmin, Float_t ptmax, Int_t j);
  TH1D** hYield = new TH1D*[4];
  for(int i=0;i<4;i++) hYield[i] = new TH1D(Form("hYield_%s",tfend[i].Data()),"",nPtBins,ptBins);
  for(int i=0;i<nPtBins;i++)
    {
      TFile* infile = new TFile(Form("%s_cent_%.0f_%.0f_pt_%.0f_%.0f.root",infname.Data(),centMin,centMax,ptBins[i],ptBins[i+1]));
      TH1D* hMCSignal = (TH1D*)infile->Get("hMCSignal");
      TH1D* hMCSwapped = (TH1D*)infile->Get("hMCSwapped");
      for(int j=0;j<4;j++)
        {
          TH1D* h = (TH1D*)infile->Get(Form("h_%s",tfend[j].Data()));
          TF1* f = fit(h,hMCSignal,hMCSwapped,ptBins[i],ptBins[i+1],j);
          Double_t yield = f->Integral(minhisto,maxhisto)/binwidthmass;
          Double_t yieldErr = f->Integral(minhisto,maxhisto)/binwidthmass*f->GetParError(0)/f->GetParameter(0);
          hYield[j]->SetBinContent(i+1,yield);
          hYield[j]->SetBinError(i+1,yieldErr);
          delete h;
        }
      delete hMCSwapped;
      delete hMCSignal;
      delete infile;
    }

  TFile* outf = new TFile(Form("%s_cent_%.0f_%.0f.root",outputfile.Data(),centmin,centmax),"recreate");
  outf->cd();
  for(int i=0;i<4;i++) hYield[i]->Write();
  outf->Close();
  delete []hYield;
  delete outf;

  cout<<endl;
}
Exemple #10
0
Double_t Likelihood(Double_t *x, Double_t *p)
{
   TF1 *out = new TF1("Outer",Outer,0.,B0 + 5.*sB,1);   
   Double_t low = B0 > 5.*sB ? B0 - 5.*sB : 0.;
   Double_t retval = out->Integral(low,B0+5.*sB,x,epsilon);
   delete out;
   return retval;
}
Exemple #11
0
Double_t NtrueOverNmeas (Double_t ptMin, Double_t ptMax) {

  //Double_t n = 6;
  //Double_t n = 5.78121e+00; // from ref level
  Double_t n = 5.95950e+00; // from reco level
  Double_t c1 = 5.23359e-01;
  Double_t c2 = 1.15226e-01;
  Double_t c3 = -7.96949e-03;

  TF1 *fTrue = new TF1("fMeas","([1]+[2]*log(x)+[3]*log(x)*log(x)+[2]+2*[3]*log(x))/exp([0]*log(x*([1]+[2]*log(x)+[3]*log(x)*log(x))))",0,500); 
  fTrue->SetParameters(n,c1,c2,c3);
  TF1 *fMeas = new TF1("fTrue","1/exp([0]*log(x))",0,500);
  fMeas->SetParameter(0,n);

  return fTrue->Integral(ptMin,ptMax)/fMeas->Integral(ptMin,ptMax);

}
Exemple #12
0
void GetRandomTest(){
	double k0=1.39;
	double k1 = 0.425;
	double theta0 = 3.41;
	double theta1 = 1.30;
	int iNpart=2;
	double k_=k0+k1*(iNpart-2);
        double theta_=theta0+theta1*TMath::Log(iNpart-1);
	TF1 *f = new TF1("f","TMath::GammaDist(x,[0],0,[1])",0,200);
	f->SetParameters(k1,theta_);
	cout<<"Value at 0 = "<<f->Eval(0)<<endl;
	cout<<"Value at 1e-11 = "<<f->Eval(1e-11)<<endl;
	cout<<"Integral 1= "<<f->Integral(f->GetXmin(),f->GetXmax())<<endl;
	f->SetRange(1e-12,200);
	cout<<"Integral 2= "<<f->Integral(f->GetXmin(),f->GetXmax())<<endl;
	cout<<"fXmin = "<<f->GetXmin()<<"\tfXmax = "<<f->GetXmax()<<"\tfNpx = "<<f->GetNpx()<<endl;
	f->SetNpx(1e5);
	TCanvas *c1 = new TCanvas();
	c1->SetLogy();
	cout<<"f mean = "<<f->Mean(0,200)<<endl;
	cout<<"math mean = "<<f->GetParameter(0)*f->GetParameter(1)<<endl;
	TH1D* h = new TH1D("h","h",1000,0,200);
	for(int i=0;i<1e6;i++){
		double para = f->GetRandom();
		h->Fill(para);
	}
	h->Scale(1.0/h->Integral()*1000/200);
	h->GetYaxis()->SetRangeUser(1e-10,1);
	h->SetMarkerStyle(24);
	h->SetMarkerColor(4);
	h->SetMarkerSize(1.1);
	TLegend *leg = new TLegend(0.6,0.7,0.8,0.9);
        leg->SetFillColor(0);
        leg->SetFillStyle(0);
        leg->SetBorderSize(0);
        leg->SetTextFont(42);
        leg->SetTextSize(0.03);
        leg->AddEntry(f,"function","lp");
        leg->AddEntry(h,"filled histogram","lp");
	h->Draw("P");
	f->Draw("same");
        leg->Draw("same");
	cout<<"h mean = "<<h->GetMean(1)<<endl;
	c1->Print("TestGetRandom.png");
	}
Exemple #13
0
double GetChiSquareProb(double ndf, double chi2)
{
  TF1* func = new TF1("func","1/(pow(2,[0]/2.0)*TMath::Gamma([0]/2.0))*pow(x,[0]/2.0-1.0)*TMath::Exp(-x/2.0)",0,1000);//pdf of chi-squared distribution
  func->SetParameter(0,ndf);
  double integral = func->Integral(0,chi2);
  double prob = 1-integral;
  std::cout << "chi2/df = " << chi2/ndf << " with p-value = " << prob << std::endl;
  return prob;
}
Exemple #14
0
void plotV2vsflow(){
gStyle->SetOptFit(kFALSE);
gStyle->SetOptStat(kFALSE);
TCanvas *c1 = new TCanvas("c1","c1",600,600);
TLegend *leg = new TLegend(0.2,0.1,0.4,0.4);
leg->SetTextSize(0.05);
leg->SetBorderSize(0);
leg->SetFillColor(0);
const int ninflow = 6;
int xbin=0;
TString dir;
double V2_Prod[ninflow],V2err_Prod[ninflow];
const int mult = 150;

TF1 *PtDistr  = new TF1("PtDistr","0.03*(exp (-(x/0.594540))+0.00499506*exp (-(x/1.89391)))", 0.3,6.0);       //Real Data
TF1 *V2vsPt = new TF1("V2vsPt","((x/3.31699)^2.35142/(1+(x/3.49188)^3.54429))*(.00005+(1/x)^1.50600)",0.3,6.0);
TF1 *V2 = new TF1("V2","0.03*(exp (-(x/0.594540))+0.00499506*exp (-(x/1.89391)))*((x/3.31699)^2.35142/(1+(x/3.49188)^3.54429))*(.00005+(1/x)^1.50600)",0.3,6.0);
double InV2 = V2->Integral(0.3,6.0)/PtDistr->Integral(0.3,6.0);
double inV2[ninflow]={};
for(int i=0;i<ninflow;i++){
        inV2[i] = InV2 * i / 5 ; 
	dir=Form("pPbDataV20%dm%d",i,mult);
	TFile *mergedV_Prod = TFile::Open(Form("%s/mergedV_Prod.root",dir.Data()));
        if(i==)
	TFile *mergedV_Prod = TFile::Open(Form("%s/mergedV_Sum.root",dir.Data()));
	TVectorD *vecMult = (TVectorD*)mergedV_Prod->Get("totmultall");
	TVectorD *vecNevent = (TVectorD*)mergedV_Prod->Get("Nevent");
	TVectorD *vecV2_Prod=(TVectorD*)mergedV_Prod->Get(Form("Vmean",xbin));
	TVectorD *vecV2err_Prod=(TVectorD*)mergedV_Prod->Get(Form("deltaVmean",xbin));
	V2_Prod[i]=(*vecV2_Prod)[xbin];
	V2err_Prod[i]=(*vecV2err_Prod)[xbin];
        cout<<inV2[i]<<endl;
//	V2sp[i]=InV2/V2_Prod[i];
//	V2spError[i]=InV2/V2_Prod[i]/V2_Prod[i]*(*vecV2err_Prod)[xbin];
//	cout<<V2sp[i]<<"pm"<<V2spError[i]<<"\t";
	}
        V2_Prod[1]=0;
cout<<endl<<InV2<<endl;
TH1D* hFrame = new TH1D("","",2000,-1,1);
hFrame->GetXaxis()->SetRangeUser(0., 0.08);
hFrame->GetYaxis()->SetRangeUser(0., 0.08);
hFrame->Draw();
TGraphErrors *grProd=new TGraphErrors(ninflow,inV2,V2_Prod,0,V2err_Prod);
grProd->SetMarkerColor(1);
grProd->SetMarkerSize(1.6);
grProd->SetLineColor(1);
grProd->SetMarkerStyle(20);
hFrame->GetXaxis()->SetTitle("Input V2");
hFrame->GetYaxis()->SetTitle("Calc V2");
hFrame->Draw();
grProd->Draw("Psame");
TLine *l = new TLine(0,0,0.06,0.06);
l->SetLineStyle(2);
l->Draw("same");
c1->SaveAs("V2vsflow.gif");
}
void MyEff(double n, double N, double& eff, double& errp, double& errn)
{

  TF1* gaus = new TF1("gaus","TMath::Gaus(x,0,1,1)");
  double lowerB = gaus->Integral(-1,0);
  double upperB = 1- lowerB;

  eff = n/N;
  if(n > N)
    {
      errp = errn = -999;
      return;
    }

  double a = n+1;
  double b = N-n+1;
  
  double integralPeak = incompletebeta(a, b, eff);

  if(integralPeak >= lowerB && integralPeak <= upperB)
    {
      double xmin = inverseBeta(integralPeak - lowerB, a, b);
      double xmax = inverseBeta(integralPeak + lowerB, a, b);
      errp = xmax-eff;
      errn = eff-xmin;
      return;

    }
  else if(integralPeak < lowerB)
    {
      double xmax = inverseBeta(2*lowerB, a, b);      
      errp = xmax-eff;
      errn = eff;
      return;

    }

  else if(integralPeak > upperB)
    {
      double xmin = inverseBeta(1-2*lowerB, a, b);
      errp = 1-eff;
      errn = eff-xmin;
      return;      
    }

  else{

    cout << "There is a bug !!!" << endl;
    errp = errn = -999;
    return;

  }


}
void estimateEfficiencyAndPurity(TH1* fraction,double cut,double& efficiency, double& purity, double& efferror, double& purerror, TFitResultPtr fitRes) {
  // efficiency loss is defined as the estimated signal below the cut over the estimated total signal
  double rangeLow, rangeHigh;
  TF1* expo = fraction->GetFunction("expo");
  expo->GetRange(rangeLow, rangeHigh);

  double signalLoss         = expo->Integral(0,cut)/fraction->GetBinWidth(1);
  double signalLossError = expo->IntegralError(0,cut,fitRes->GetParams(),fitRes->GetCovarianceMatrix().GetMatrixArray())/fraction->GetBinWidth(1);
  
  double signal_expopart         = expo->Integral(cut,fraction->GetBinLowEdge(fraction->FindBin(rangeHigh)+1))/fraction->GetBinWidth(1);
  double signal_expopartError = expo->IntegralError(cut, fraction->GetBinLowEdge(fraction->FindBin(rangeHigh)+1),fitRes->GetParams(),fitRes->GetCovarianceMatrix().GetMatrixArray())/fraction->GetBinWidth(1);

  double signal_toppoart = fraction->Integral(fraction->FindBin(rangeHigh)+1,fraction->FindBin(1.)+1);
  
  efficiency = (signal_expopart+signal_toppoart)/(signal_expopart+signal_toppoart+signalLoss);
  efferror = TMath::Sqrt( pow(signalLoss,2)*pow(signal_expopartError,2) + pow(signal_toppoart+signal_expopart,2)*pow(signalLossError,2) )/pow(signal_expopart+signal_toppoart+signalLoss,2);
  
  // purity is defined as the signal above the cut (signal_expopart+signal_toppoart) 
  // over the total data above that cut
  
  double data_kept = fraction->Integral(fraction->FindBin(cut),fraction->FindBin(1.)+1);
  double allBeforeRangeHigh = fraction->Integral(fraction->FindBin(cut),fraction->FindBin(rangeHigh));
  purity = (signal_expopart+signal_toppoart)/data_kept;
  purerror = signal_expopartError/data_kept; 
 
  if(purity > 1.){
    std::cout << "#################################################"<<std::endl;
    std::cout << "cut: "<<cut<<" purity:" << purity <<std::endl;
    std::cout << "signalLoss: " << signalLoss << " signalExtrapolated: " << signal_expopart << " signalTop: " << signal_toppoart << std::endl;
    std::cout << "all b.r.h.: " << allBeforeRangeHigh << std::endl;
    std::cout << "total signal with expo: "<< (signal_expopart+signal_toppoart)<< std::endl;
    std::cout << "total signal integrating: "<< (allBeforeRangeHigh+signal_toppoart)<< std::endl;
    std::cout << "data kept:  " << data_kept  << std::endl;
    std::cout << "ratio:      " << allBeforeRangeHigh/signal_expopart << std::endl;
    std::cout << "#################################################"<<std::endl;
  }
 
  //   std::cout << "estimateEfficiencyAndPurity for cut=" << cut << std::endl;
  //   std::cout << "signal: " << signalLoss << " " << signal_expopart << " " << signal_toppoart << std::endl;
  //   std::cout << "data kept: " << data_kept << std::endl;
  //   std::cout << "eff: " << efficiency << "+/- " << efferror << " pur: " << purity << " +/- " << purerror << std::endl;
}
Exemple #17
0
Double_t Outer(Double_t *x, Double_t *p)
{
   Double_t sigma, par[2];
   TF1 *in = new TF1("Inner",Inner,0.,A0 + 5.*sA,2);
   par[0]=x[0];  // background value
   par[1]=p[0];  // signal cross section value
   Double_t low = A0 > 5.*sA ? A0 - 5.*sA : 0.;
   Double_t retval = 1./sqrt(2.*TMath::Pi())/sB*exp(-(x[0]-B0)*(x[0]-B0)/2./sB/sB)*in->Integral(low,A0+5.*sA,par,epsilon);
   delete in;
   return retval;
}
void ResonanceSimulator::resonanceIntegrals(){
	std::cout<<"eresosize = "<<E_reso.size()<<std::endl;
for (size_t i = 0; i < E_reso.size(); ++i)
	{
	TF1 *Resonance = new TF1("Resonance","[0]*TMath::Gaus(x,[1],[2])",E_reso[i]-5*W_reso[i],E_reso[i]+5*W_reso[i]);
	Resonance->SetParameters(I_reso[i],E_reso[i],W_reso[i]);
	Inte.push_back(Resonance->Integral(E_reso[i]-5*W_reso[i],E_reso[i]+5*W_reso[i])*1.0e-24);
	delete Resonance;
	}
return;
}
void PoissonianMagic()
{
  TFile file_PUnum("/nfs/dust/cms/user/rathjd/VBF-LS-tau/PU/DataPUFile_22Jan2013ReReco_Run2012.root", "read");
  TFile file_PUden("/nfs/dust/cms/user/rathjd/VBF-LS-tau/PU/S10MC_PUFile.root", "read");
  
  TH1F *data = (TH1F*)file_PUnum.Get("analyzeHiMassTau/NVertices_0");
  data->Scale(1/data->Integral(0,-1));
  TH1F *MC   = (TH1F*)file_PUden.Get("analyzeHiMassTau/NVertices_0");
  MC->Scale(1/MC->Integral(0,-1));
  
  //define empty histograms for the smeared versions
  TH1F *num = (TH1F*)data->Clone("ratio");
  for(int i=0; i<num->GetNbinsX(); i++) num->SetBinContent(i+1,0);
  TH1F *den = (TH1F*)data->Clone("MC");
  for(int i=0; i<den->GetNbinsX(); i++) den->SetBinContent(i+1,0);
  
  //generate the poissonian distributions
  for(unsigned int i=0; i<data->GetNbinsX(); i++){
    TF1 *Pd = new TF1("Pd", "TMath::PoissonI(x,[0])",0,100);
    Pd->SetParameter(0,data->GetBinLowEdge(i+1));
    std::cout<<data->GetBinLowEdge(i+1)<<std::endl;
    TF1 *Pm = new TF1("Pm", "TMath::PoissonI(x,[0])",0,100);
    Pm->SetParameter(0,MC->GetBinLowEdge(i+1));
    //add up the poissonians
    for(unsigned int j=0; j<data->GetNbinsX(); j++){
      num->SetBinContent(j+1,num->GetBinContent(j+1)+Pd->Eval(j)/Pd->Integral(0,100)*data->GetBinContent(i+1));
      den->SetBinContent(j+1,den->GetBinContent(j+1)+Pm->Eval(j)/Pm->Integral(0,100)*MC->GetBinContent(i+1));
    }
  }
  
  //make the ratio and save the result
  num->Divide(den);
  
  TFile *f=new TFile("PUreweightHistogram.root","RECREATE");
  num->Write();
  den->Write();
  
  f->Close();
  file_PUnum.Close();
  file_PUden.Close();
}
Exemple #20
0
double Calc_dsigma_dgamma(double gamma){
	
  C = 4.*gamma*gamma*M*M/(rootS*rootS);
  TF1 *func = new TF1("func","PDF_TOT(x)",0,10);
	
  double ret;
	
  //ret = (1./pow(gamma,5.))*sqrt(1.-1./(gamma*gamma))*func->Integral(C,1.);
  ret = (1./gamma)*sqrt(1.-1./(gamma*gamma))*func->Integral(C,1.);
	
  return ret;
}
Exemple #21
0
void shTest(){
  Double_t par[] = {0.3164, 3., 1.5};   //  guesses
  Double_t points(24);
  TF1 * sT = new TF1("sT","0.3164*pow(x,4)*exp(-x*1.5)",0., points);
  sT->Draw();
  Double_t shapeInt = sT->Integral(0., points);
  Double_t shapeMax = sT->GetMaximum(0.,points);
  Double_t shapePeak= sT->GetMaximumX(0.,points);
  Double_t shapeRMS = sqrt(sT->Moment(2,0.,points)-sT->Moment(1,0.,points)*sT->Moment(1,0.,points));
  cout<<"SHAPE  Peak = "<<shapePeak<<"  Max = "<<shapeMax<<"  RMS = "<<shapeRMS<<"  Integral = "<<shapeInt<<endl;
  // return par[0]*pow(x1,4.)*(exp(-x1*16./fN));    //  fN is set to (float)NSAMPLES
}
Exemple #22
0
double computePValue(TH1F * histo, double s) {
  // Get the x axis
  TAxis * ax = histo->GetXaxis();

  double integ = histo->Integral();
  double nAboveS = 0;

  // Loop over all the bins
  for (int bin=ax->GetFirst(); bin<=ax->GetLast(); bin++){
    if(histo->GetBinCenter(bin) > s) {
      nAboveS += histo->GetBinContent(bin);
    }
  }
  double old = nAboveS/integ;
  TString gName = "gaus_"+TString(histo->GetName());

  TF1 *gaus = new TF1(gName.Data(),"gaus",0.2,100);
  histo->Fit(gaus,"Q","",0.2,100);
  double pvalue = gaus->Integral(s,10)/gaus->Integral(-10,10);
  cout << "        p-value computation for S: " << s << " with histo: " << old
       << " with fit: " << pvalue << endl;
  return  pvalue;
}
void Fithistograms(TH1D * histo[], TH1D * mc_matched_signal[], TH1D * mc_matched_kpiswapped[], TString MBorDtrig, int iptstart, int iptend, bool isPbPb, int centlow, int centhigh, TH1D * dNdpt, TString fitoption)
{
	for(int ipt = iptstart; ipt < iptend; ipt++)
	{       
		TF1* signalfittedfunc = NULL;

		int iptmc = ipt;
//		if( ipt > 2 ) 
//			iptmc = ipt;
//		else    
//			iptmc = 3;

        if( fitoption == "poly3bkg_floatwidth")
            signalfittedfunc = fit_histo_poly3bkg_floatwidth( isPbPb, centlow, centhigh, histo[ipt], mc_matched_signal[iptmc], mc_matched_kpiswapped[iptmc], ipt, MBorDtrig);

        if( fitoption == "expobkg_2nd_floatwidth")
            signalfittedfunc = fit_histo_expobkg_2nd_floatwidth( isPbPb, centlow, centhigh, histo[ipt], mc_matched_signal[iptmc], mc_matched_kpiswapped[iptmc], ipt, MBorDtrig);

        if( fitoption == "poly2bkg_floatwidth")
            signalfittedfunc = fit_histo_poly2bkg_floatwidth( isPbPb, centlow, centhigh, histo[ipt], mc_matched_signal[iptmc], mc_matched_kpiswapped[iptmc], ipt, MBorDtrig);

        if( fitoption == "poly3bkg")
            signalfittedfunc = fit_histo_poly3bkg( isPbPb, centlow, centhigh, histo[ipt], mc_matched_signal[iptmc], mc_matched_kpiswapped[iptmc], ipt, MBorDtrig);

        if( fitoption == "expobkg_2nd")
            signalfittedfunc = fit_histo_expobkg_2nd( isPbPb, centlow, centhigh, histo[ipt], mc_matched_signal[iptmc], mc_matched_kpiswapped[iptmc], ipt, MBorDtrig);

        if( fitoption == "poly2bkg")
            signalfittedfunc = fit_histo_poly2bkg( isPbPb, centlow, centhigh, histo[ipt], mc_matched_signal[iptmc], mc_matched_kpiswapped[iptmc], ipt, MBorDtrig);

		double histomassbinsize = histo[ipt]->GetBinWidth(10);
		double yield = signalfittedfunc->Integral(massmin,massmax)/histomassbinsize;
		double yieldErr = signalfittedfunc->Integral(massmin,massmax)/histomassbinsize * signalfittedfunc->GetParError(0)/signalfittedfunc->GetParameter(0);
		dNdpt->SetBinContent(ipt+1,yield/(ptbins[ipt+1]-ptbins[ipt]));
		dNdpt->SetBinError(ipt+1,yieldErr/(ptbins[ipt+1]-ptbins[ipt]));
	}   
}
Exemple #24
0
void DoTtest(double n1, double n2, double mean1, double mean2, double var1, double var2, double &t, double& prob)
{
  double sig1 = var1*var1/n1;
  double sig2 = var2*var2/n2;
  t = (mean1-mean2)/sqrt(sig1 + sig2);
  std::cout << "t-stat = " << t << std::endl;

  TF1* gaus = new TF1("gaus","TMath::Gaus(x,[0],[1],1)",-20,20);
  gaus->SetParameter(0,0);
  gaus->SetParameter(1,1);
  double integral = gaus->Integral(-1*fabs(t),fabs(t));
  prob = 1-integral;
  //prob = prob/(1-1/pow(df,2));
  std::cout << "prob = " << prob << std::endl;
}
Exemple #25
0
void  testIntegPerf(double x1, double x2, int n = 100000){


   std::cout << "\n\n***************************************************************\n";
   std::cout << "Test integration performances in interval [ " << x1 << " , " << x2 << " ]\n\n";

  TStopwatch timer;

  double dx = (x2-x1)/double(n);

  //ROOT::Math::Functor1D<ROOT::Math::IGenFunction> f1(& TMath::BreitWigner);
  ROOT::Math::WrappedFunction<> f1(func);

  timer.Start();
  ROOT::Math::Integrator ig(f1 );
  double s1 = 0.0;
  nc = 0;
  for (int i = 0; i < n; ++i) {
     double x = x1 + dx*i;
     s1+= ig.Integral(x1,x);
  }
  timer.Stop();
  std::cout << "Time using ROOT::Math::Integrator        :\t" << timer.RealTime() << std::endl;
  std::cout << "Number of function calls = " << nc/n << std::endl;
  int pr = std::cout.precision(18);  std::cout << s1 << std::endl;  std::cout.precision(pr);



  //TF1 *fBW = new TF1("fBW","TMath::BreitWigner(x)",x1, x2);  //  this is faster but cannot measure number of function calls
  TF1 *fBW = new TF1("fBW",func2,x1, x2,0);

  timer.Start();
  nc = 0;
  double s2 = 0;
  for (int i = 0; i < n; ++i) {
     double x = x1 + dx*i;
     s2+= fBW->Integral(x1,x );
  }
  timer.Stop();
  std::cout << "Time using TF1::Integral :\t\t\t" << timer.RealTime() << std::endl;
  std::cout << "Number of function calls = " << nc/n << std::endl;
  pr = std::cout.precision(18);  std::cout << s1 << std::endl;  std::cout.precision(pr);


}
Exemple #26
0
void CheckStatErr() {

    TFile*finput=new TFile("fileFitfunctionData.root");
    TF1*fbackground=(TF1*)finput->Get("fbackground1");//
    TF1*fsignal=(TF1*)finput->Get("fmass1");//

    fbackground->SetName("fbackground");
    fsignal->SetName("fsignal");

    TFile *infMC = new TFile(inputmc.Data());
    TTree *ntMC = (TTree*)infMC->Get("ntKstar1");
    TTree *ntMC2 = (TTree*)infMC->Get("ntKstar2");



    int i,j;
    int nexp=100;

    const int nBins = 3;
    double ptBins[nBins+1] = {10,15,20,60};

    TH1D *hyield = new TH1D("hyield","",30,450,550);

    for(j=0; j<nexp; j++)
    {
        if(j%10==0) cout<<j<<" / "<<nexp<<endl;
        TH1D *hMass = new TH1D("hMass","",50,5,6);
        for (i=0; i<500; i++)
        {
            double sgn=fsignal->GetRandom();
            double bkg=fbackground->GetRandom();

            hMass->Fill(sgn);
            hMass->Fill(bkg);
        }
        i=0;//first pt bin
        //hMass->Draw();
        TF1 *f = fit(hMass,ntMC,ntMC2,ptBins[i],ptBins[i+1]);
        double yield = f->Integral(5,6)/0.02;
        hyield->Fill(yield);
    }
    TCanvas *c2= new TCanvas("c2","",600,600);
    hyield->Draw();
    c2->SaveAs("ResultsCheckStatErr/YieldDistribution.gif");
}
void myfit()
{
   TFile* hsimple = TFile::Open("hsimple.root");
   if (!hsimple) return;

   TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);

   TH1F *hpx = (TH1F*)hsimple->Get("hpx");

// Creates a Root function based on function fitf above
   TF1 *func = new TF1("fitf",fitf,-2,2,3);

// Sets initial values and parameter names
   func->SetParameters(100,0,1);
   func->SetParNames("Constant","Mean_value","Sigma");

// Fit histogram in range defined by function
   hpx->Fit(func,"r");

// Gets integral of function between fit limits
   printf("Integral of function = %g\n",func->Integral(-2,2));
}
void test_mc_rejection_method_lin()
{
  const int    N     = 500000;
  const int    nbins = 300;
  const double ymax  = 1.1;
  const double xmin  = 0;
  const double xmax  = 10;
  const double dx    = xmax-xmin;

  TRandom rg;

  TF1 *  func = new TF1  ("func","1/(x+1)",0,10);
  TH1D * hgen = new TH1D ("hgen","generated",nbins,xmin,xmax);

  for(int i=0; i<N; i++) {
     cout << "..................." << i << endl;
     bool selected=false;
     while(1) {
       double xg = xmin + rg.Uniform() * dx;
       double yg = ymax * rg.Uniform();
       double yc = func->Eval(xg);
       selected = (yg<yc);
       if(selected) {
           hgen->Fill(xg);
           break;
       }
     }
  }

  double IF = func->Integral(xmin,xmax);
  double IH = hgen->Integral("width");
  double sc = IF/IH;
  hgen->Scale(sc);

  hgen->Draw();
  func->Draw("same");
}
Exemple #29
0
void fitDexpo(TString collsyst="PbPb", TString outputfile="outfMasshisto/mass")
{
  collisionsystem = collsyst;
  infname = outputfile;

  gStyle->SetTextSize(0.05);
  gStyle->SetTextFont(42);
  gStyle->SetPadRightMargin(0.043);
  gStyle->SetPadLeftMargin(0.18);
  gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadBottomMargin(0.145);
  gStyle->SetTitleX(.0f);

  TF1* fit (double ptmin, double ptmax);

  ofstream fout(Form("outYield/expo_%s.dat",collsyst.Data()));
  for(int i=0;i<nBins;i++)
    {
      TF1* f = fit(ptBins[i],ptBins[i+1]);
      Double_t yield = f->Integral(minhisto,maxhisto)/binwidthmass;
      fout<<ptBins[i]<<" "<<ptBins[i+1]<<" "<<yield<<endl;
    }
  fout.close();
}
Exemple #30
0
void  DrawCumulative(double x1, double x2, int n = 100){

   std::cout << "\n\n***************************************************************\n";
   std::cout << "Drawing cumulatives of BreitWigner in interval [ " << x1 << " , " << x2 << " ]\n\n";


   double dx = (x2-x1)/double(n);

   TH1D *cum0 = new TH1D("cum0", "", n, x1, x2); //exact cumulative
   for (int i = 1; i <= n; ++i) {
      double x = x1 + dx*i;
      cum0->SetBinContent(i, exactIntegral(x1, x));

   }

   // alternative method using ROOT::Math::Functor class
   ROOT::Math::Functor1D f1(& func);


   ROOT::Math::Integrator ig(f1, ROOT::Math::IntegrationOneDim::kADAPTIVE,1.E-12,1.E-12);

   TH1D *cum1 = new TH1D("cum1", "", n, x1, x2);
   for (int i = 1; i <= n; ++i) {
      double x = x1 + dx*i;
      cum1->SetBinContent(i, ig.Integral(x1,x));
   }


   TF1 *fBW = new TF1("fBW","TMath::BreitWigner(x, 0, 1)",x1, x2);


   TH1D *cum2 = new TH1D("cum2", "", n, x1, x2);
   for (int i = 1; i <= n; ++i) {
      double x = x1 + dx*i;
      cum2->SetBinContent(i, fBW->Integral(x1,x));
   }

   TH1D *cum10 = new TH1D("cum10", "", n, x1, x2); //difference between  1 and exact
   TH1D *cum20 = new TH1D("cum23", "", n, x1, x2); //difference between 2 and excact
   for (int i = 1; i <= n; ++i) {
      double delta  =  cum1->GetBinContent(i) - cum0->GetBinContent(i);
      double delta2 =  cum2->GetBinContent(i) - cum0->GetBinContent(i);
      //std::cout << " diff for " << x << " is " << delta << "  " << cum1->GetBinContent(i) << std::endl;
      cum10->SetBinContent(i, delta );
      cum10->SetBinError(i, std::numeric_limits<double>::epsilon() * cum1->GetBinContent(i) );
      cum20->SetBinContent(i, delta2 );
   }


   TCanvas *c1 = new TCanvas("c1","Integration example",20,10,800,500);
   c1->Divide(2,1);
   c1->Draw();

   cum0->SetLineColor(kBlack);
   cum0->SetTitle("BreitWigner - the cumulative");
   cum0->SetStats(0);
   cum1->SetLineStyle(2);
   cum2->SetLineStyle(3);
   cum1->SetLineColor(kBlue);
   cum2->SetLineColor(kRed);
   c1->cd(1);
   cum0->DrawCopy("h");
   cum1->DrawCopy("same");
   //cum2->DrawCopy("same");
   cum2->DrawCopy("same");

   c1->cd(2);
   cum10->SetTitle("Difference");
   cum10->SetStats(0);
   cum10->SetLineColor(kBlue);
   cum10->Draw("e0");
   cum20->SetLineColor(kRed);
   cum20->Draw("hsame");

   TLegend * l = new TLegend(0.11, 0.8, 0.7 ,0.89);
   l->AddEntry(cum10, "GSL integration - analytical ");
   l->AddEntry(cum20, "TF1::Integral  - analytical ");
   l->Draw();


   c1->Update();
   std::cout << "\n***************************************************************\n";


}