コード例 #1
0
ファイル: bToDRawYield.C プロジェクト: Jelov/BtoD
void bToDRawYield()
{
	gStyle->SetTextSize(0.05);
	gStyle->SetTextFont(42);
	gStyle->SetPadRightMargin(0.04);
	gStyle->SetPadLeftMargin(0.14);
	gStyle->SetPadTopMargin(0.1);
	gStyle->SetPadBottomMargin(0.14);
	gStyle->SetTitleX(.0f);
	gStyle->SetOptFit(1111);
	gStyle->SetOptStat(0);
	gStyle->SetOptTitle(0);

	TCanvas* c4 = new TCanvas("c4","",800,600);
	c4->Divide(2,2);

	TCanvas* c2 = new TCanvas("c2","",400,600);
	c2->Divide(1,2);

	TCanvas* c1 = new TCanvas();

	TCanvas* c15 = new TCanvas("c15","",810,1000);
	c15->Divide(3,5);

	TFile* fPbPb = new TFile("bFeedDownPbPb.hist.root");
	TFile* fPbPbMB = new TFile("bFeedDownPbPbMB.hist.root");
	TFile* fPbPbMC = new TFile("bFeedDownPbPbMC.hist.root");
	TFile* fPbPbMBMC = new TFile("bFeedDownPbPbMBMC.hist.root");

	TH3D* hDataPbPb = (TH3D*)fPbPb->Get("hData");
	TH3D* hSidebandPbPb = (TH3D*)fPbPb->Get("hSideband");
	TH3D* hDataPbPbMB = (TH3D*)fPbPbMB->Get("hData");
	TH3D* hSidebandPbPbMB = (TH3D*)fPbPbMB->Get("hSideband");
	TH3D* hPtMD0DcaPbPb = (TH3D*)fPbPb->Get("hPtMD0Dca");
	TH3D* hPtMD0DcaPbPbMB = (TH3D*)fPbPbMB->Get("hPtMD0Dca");

	TH3D* hMCPSignalPbPb = (TH3D*)fPbPbMC->Get("hMCPSignal");
	TH3D* hMCNPSignalPbPb = (TH3D*)fPbPbMC->Get("hMCNPSignal");
	TH3D* hMCPSignalPbPbMB = (TH3D*)fPbPbMBMC->Get("hMCPSignal");
	TH3D* hMCNPSignalPbPbMB = (TH3D*)fPbPbMBMC->Get("hMCNPSignal");
	TH3D* hPtMD0DcaMCPSignalPbPb = (TH3D*)fPbPbMC->Get("hPtMD0DcaMCPSignal");
	TH3D* hPtMD0DcaMCPSwappedPbPb = (TH3D*)fPbPbMC->Get("hPtMD0DcaMCPSwapped");
	TH3D* hPtMD0DcaMCPSignalPbPbMB =(TH3D*)fPbPbMBMC->Get("hPtMD0DcaMCPSignal");
	TH3D* hPtMD0DcaMCPSwappedPbPbMB = (TH3D*)fPbPbMBMC->Get("hPtMD0DcaMCPSwapped");

	TH3D* hData = (TH3D*)hDataPbPb->Clone("hData");
	hData->Sumw2();
	hData->Add(hDataPbPbMB);

	TH3D* hSideband = (TH3D*)hSidebandPbPb->Clone("hSideband");
	hSideband->Sumw2();
	hSideband->Add(hSidebandPbPbMB);

	TH3D* hPtMD0Dca = (TH3D*)hPtMD0DcaPbPb->Clone("hPtMD0Dca");
	hPtMD0Dca->Sumw2();
	hPtMD0Dca->Add(hPtMD0DcaPbPbMB);

	TH3D* hMCPSignal = (TH3D*)hMCPSignalPbPb->Clone("hMCPSignal");
	hMCPSignal->Sumw2();
	hMCPSignal->Add(hMCPSignalPbPbMB);

	TH3D* hMCNPSignal = (TH3D*)hMCNPSignalPbPb->Clone("hMCNPSignal");
	hMCNPSignal->Sumw2();
	hMCNPSignal->Add(hMCNPSignalPbPbMB);

	TH3D* hPtMD0DcaMCPSignal = (TH3D*)hPtMD0DcaMCPSignalPbPb->Clone("hPtMD0DcaMCPSignal");
	hPtMD0DcaMCPSignal->Sumw2();
	hPtMD0DcaMCPSignal->Add(hPtMD0DcaMCPSignalPbPbMB);

	TH3D* hPtMD0DcaMCPSwapped =(TH3D*)hPtMD0DcaMCPSwappedPbPb->Clone("hPtMD0DcaMCPSwapped");
	hPtMD0DcaMCPSwapped->Sumw2();
	hPtMD0DcaMCPSwapped->Add(hPtMD0DcaMCPSwappedPbPbMB);

	TLatex* texCms = new TLatex(0.18,0.93, "#scale[1.25]{CMS} Preliminary");
	texCms->SetNDC();
	texCms->SetTextAlign(12);
	texCms->SetTextSize(0.06);
	texCms->SetTextFont(42);

	TLatex* texCol = new TLatex(0.96,0.93, "PbPb #sqrt{s_{NN}} = 5.02 TeV");
	texCol->SetNDC();
	texCol->SetTextAlign(32);
	texCol->SetTextSize(0.06);
	texCol->SetTextFont(42);

	const int nPtBins = 14;
	float ptBins[nPtBins+1] = {2.,3.,4.,5.,6.,8.,10.,12.5,15.0,20.,25.,30.,40.,60.,100};

	float pts[nPtBins];
	float ptErrors[nPtBins];
	float promptFraction[nPtBins];
	float totalYield[nPtBins];
	float totalYieldInvMassFit[nPtBins];
	float totalYieldInvMassFitError[nPtBins];
	float bToDYield[nPtBins];
	float bToDYieldError[nPtBins];
	float bToDYieldErrorDataOnly[nPtBins];
	float promptDYield[nPtBins];
	float promptDYieldError[nPtBins];
	float promptDYieldErrorDataOnly[nPtBins];

	const int nBinY = 14;
	Float_t binsY[nBinY+1];
	float firstBinYWidth = 0.001;
	float binYWidthRatio = 1.27;
	binsY[0]=0;
	for(int i=1; i<=nBinY; i++)
		binsY[i] = binsY[i-1]+firstBinYWidth*pow(binYWidthRatio,i-1);
	cout<<"last y bin: "<<binsY[nBinY]<<endl;

	//  for(int i=1; i<=nPtBins; i++)
	for(int i =7; i<=7; i++)
	{
		pts[i-1] = 0.5*(ptBins[i-1]+ptBins[i]);
		ptErrors[i-1] = 0.5*(ptBins[i]-ptBins[i-1]);
		float ptLow = ptBins[i-1];
		float ptHigh = ptBins[i];
		cout<<endl<<"======================================="<<endl;
		cout<<"pT range: "<<ptLow<<" "<<ptHigh<<endl;

		TLatex* texPtY = new TLatex(0.32,0.82,Form("%.1f < p_{T} < %.1f GeV/c      |y| < 1.0",ptLow,ptHigh));
		texPtY->SetNDC();
		texPtY->SetTextFont(42);
		texPtY->SetTextSize(0.06);
		texPtY->SetLineWidth(2);

		TLatex* texPt = new TLatex(0.18,0.82,Form("%.1f < p_{T} < %.1f GeV/c",ptLow,ptHigh));
		texPt->SetNDC();
		texPt->SetTextFont(42);
		texPt->SetTextSize(0.06);
		texPt->SetLineWidth(2);

		TLatex* texY = new TLatex(0.18,0.74,Form("|y| < 1.0"));
		texY->SetNDC();
		texY->SetTextFont(42);
		texY->SetTextSize(0.06);
		texY->SetLineWidth(2);

		c2->cd(1);

		hPtMD0Dca->GetZaxis()->SetRange(1,100);
		hPtMD0Dca->GetXaxis()->SetRangeUser(ptLow+0.001,ptHigh-0.001);
		hPtMD0DcaMCPSignal->GetXaxis()->SetRangeUser(ptLow+0.001,ptHigh-0.001);
		hPtMD0DcaMCPSwapped->GetXaxis()->SetRangeUser(ptLow+0.001,ptHigh-0.001);
		TH1D* hMData = (TH1D*)hPtMD0Dca->Project3D("y")->Clone(Form("hM_%1.1f_%1.1f", ptLow, ptHigh));
		TH1D* hMMCSignal = (TH1D*)hPtMD0DcaMCPSignal->Project3D("y");
		TH1D* hMMCSwapped = (TH1D*)hPtMD0DcaMCPSwapped->Project3D("y");

		setColorTitleLabel(hMData);
		setColorTitleLabel(hMMCSignal);
		setColorTitleLabel(hMMCSwapped);

		TF1* fMass = fitMass(hMData, hMMCSignal, hMMCSwapped);

		texCms->Draw();
		texCol->Draw();
		texPt->Draw();
		texY->Draw();

		TF1* fSignalAndSwapped = new TF1("fSignalAndSwapped","[0]*([3]*([5]*Gaus(x,[1],[2]*(1+[7]))/(sqrt(2*3.1415927)*[2]*(1+[7]))+(1-[5])*Gaus(x,[1],[6]*(1+[7]))/(sqrt(2*3.1415927)*[6]*(1+[7])))+(1-[3])*Gaus(x,[1],[4]*(1+[7]))/(sqrt(2*3.1415927)*[4]*(1+[7])))", 1.7, 2.0);      
		fSignalAndSwapped->SetParameter(0,fMass->GetParameter(0));
		fSignalAndSwapped->SetParameter(1,fMass->GetParameter(1));
		fSignalAndSwapped->SetParameter(2,fMass->GetParameter(2));
		fSignalAndSwapped->SetParameter(3,fMass->GetParameter(7));
		fSignalAndSwapped->SetParameter(4,fMass->GetParameter(8));
		fSignalAndSwapped->SetParameter(5,fMass->GetParameter(9));
		fSignalAndSwapped->SetParameter(6,fMass->GetParameter(10));
		fSignalAndSwapped->SetParameter(7,fMass->GetParameter(11));

		TF1* background = new TF1("fBackground","[0]+[1]*x+[2]*x*x+[3]*x*x*x");
		background->SetParameter(0,fMass->GetParameter(3));
		background->SetParameter(1,fMass->GetParameter(4));
		background->SetParameter(2,fMass->GetParameter(5));
		background->SetParameter(3,fMass->GetParameter(6));

		cout<<"MC signal width: "<<fMass->GetParameter(2)<<"   "<<fMass->GetParameter(10)<<endl;
		cout<<"MC swapped width: "<<fMass->GetParameter(8)<<endl;

		float massD = 1.8649;
		float massSignal1 = massD-0.025;
		float massSignal2 = massD+0.025;
		float massSideBand1 = massD-0.1;
		float massSideBand2 = massD-0.075;
		float massSideBand3 = massD+0.075;
		float massSideBand4 = massD+0.1;

		float scaleSideBandBackground = background->Integral(massSignal1, massSignal2)/(background->Integral(massSideBand1, massSideBand2)+background->Integral(massSideBand3, massSideBand4));
		cout<<"scaleSideBandBackground: "<<scaleSideBandBackground<<endl;
		totalYieldInvMassFit[i-1] = fMass->GetParameter(0)*fMass->GetParameter(7)/hMData->GetBinWidth(1);
		totalYieldInvMassFitError[i-1] = fMass->GetParError(0)*fMass->GetParameter(7)/hMData->GetBinWidth(1);
		cout<<"totalYieldInvMassFit: "<<totalYieldInvMassFit[i-1]<<" +- "<<totalYieldInvMassFitError[i-1]<<endl;
		float scaleSideBandMethodSignal = fSignalAndSwapped->GetParameter(0)*fSignalAndSwapped->GetParameter(3) / (fSignalAndSwapped->Integral(massSignal1, massSignal2)-fSignalAndSwapped->Integral(massSideBand1, massSideBand2)-fSignalAndSwapped->Integral(massSideBand3, massSideBand4));
		cout<<"scaleSideBandMethodSignal: "<<scaleSideBandMethodSignal<<endl;

		TLatex* texScale = new TLatex(0.18,0.66,Form("side band bg scale: %1.3f", scaleSideBandBackground));
		texScale->SetNDC();
		texScale->SetTextFont(42);
		texScale->SetTextSize(0.06);
		texScale->SetLineWidth(2);
		texScale->Draw();

		TLine* lineSignal1 = new TLine(massSignal1, 0, massSignal1, hMData->GetMaximum()*0.5);
		TLine* lineSignal2 = new TLine(massSignal2, 0, massSignal2, hMData->GetMaximum()*0.5);
		TLine* lineSideBand1 = new TLine(massSideBand1, 0, massSideBand1, hMData->GetMaximum()*0.5);
		TLine* lineSideBand2 = new TLine(massSideBand2, 0, massSideBand2, hMData->GetMaximum()*0.5);
		TLine* lineSideBand3 = new TLine(massSideBand3, 0, massSideBand3, hMData->GetMaximum()*0.5);
		TLine* lineSideBand4 = new TLine(massSideBand4, 0, massSideBand4, hMData->GetMaximum()*0.5);
		lineSignal1->Draw();
		lineSignal2->Draw();
		lineSideBand1->Draw();
		lineSideBand2->Draw();
		lineSideBand3->Draw();
		lineSideBand4->Draw();

		c2->cd(2);
		gPad->SetLogy();

		hData->GetXaxis()->SetRangeUser(ptLow+0.001,ptHigh-0.001);
		hSideband->GetXaxis()->SetRangeUser(ptLow+0.001,ptHigh-0.001);
		hMCPSignal->GetXaxis()->SetRangeUser(ptLow+0.001,ptHigh-0.001);
		hMCNPSignal->GetXaxis()->SetRangeUser(ptLow+0.001,ptHigh-0.001);

		TH1D* hD0DcaData0 = (TH1D*)hData->Project3D("y")->Clone("hD0DcaData0");
		TH1D* hD0DcaSideband = (TH1D*)hSideband->Project3D("y")->Clone("hD0DcaSideband");
		TH1D* hD0DcaMCPSignal0 = (TH1D*)hMCPSignal->Project3D("y")->Clone("hD0DcaMCPSignal0");
		TH1D* hD0DcaMCNPSignal0 = (TH1D*)hMCNPSignal->Project3D("y")->Clone("hD0DcaMCNPSignal0");

		float integralRawYieldMCP = hD0DcaMCPSignal0->Integral();
		float integralRawYieldMCNP = hD0DcaMCNPSignal0->Integral();
		cout<<"integralRawYieldMCP: "<<integralRawYieldMCP<<endl;
		cout<<"integralRawYieldMCNP: "<<integralRawYieldMCNP<<endl;

		hD0DcaMCPSignal = hD0DcaMCPSignal0;
		hD0DcaMCNPSignal = hD0DcaMCNPSignal0;

		divideBinWidth(hD0DcaData0);
		divideBinWidth(hD0DcaSideband);
		setColorTitleLabel(hD0DcaData0, 1);
		hD0DcaData0->GetXaxis()->SetRangeUser(0,0.07);
		hD0DcaData0->GetYaxis()->SetTitle("counts per cm");

		TH1D* hD0DcaSideband0 = (TH1D*)hD0DcaSideband->Clone("hD0DcaSideband0");
		hD0DcaSideband->Scale(scaleSideBandBackground);

		TH1D* hD0DcaDataSubSideBand = (TH1D*)hD0DcaData0->Clone("hD0DcaDataSubSideBand");
		hD0DcaDataSubSideBand->Add(hD0DcaSideband,-1);
		hD0DcaDataSubSideBand->Scale(scaleSideBandMethodSignal);

		hD0DcaData0->SetMarkerSize(0.6);
		hD0DcaData0->Draw();
		hD0DcaSideband->Draw("hsame");
		hD0DcaSideband0->SetLineStyle(2);
		hD0DcaSideband0->Draw("hsame");

		TLegend* leg1 = new TLegend(0.44,0.6,0.90,0.76,NULL,"brNDC");
		leg1->SetBorderSize(0);
		leg1->SetTextSize(0.06);
		leg1->SetTextFont(42);
		leg1->SetFillStyle(0);
		leg1->AddEntry(hD0DcaData0,"D^{0} candidate","pl");
		leg1->AddEntry(hD0DcaSideband,"side band","l");
		leg1->AddEntry(hD0DcaSideband0,"side band unscaled","l");
		leg1->Draw("same");

		texCms->Draw();
		texCol->Draw();
		texPtY->Draw();

		c2->SaveAs(Form("plots/PbPb_%.0f_%.0f_sideBand.pdf",ptLow,ptHigh));

		c2->cd(1);
		hMMCSignal->Draw();
		texCms->Draw();
		texCol->Draw();
		texPt->Draw();
		texY->Draw();

		c2->cd(2);
		gPad->SetLogy(0);
		hMMCSwapped->Draw();
		texCms->Draw();
		texCol->Draw();
		texPt->Draw();
		texY->Draw();

		c2->SaveAs(Form("plots/PbPb_%.0f_%.0f_McInvMassFit.pdf",ptLow,ptHigh));

		c15->cd(1);

		fitMass(hMData, hMMCSignal, hMMCSwapped);

		texPt->Draw();
		texY->Draw();

		TH1D* hD0DcaDataFit = new TH1D("hD0DcaDataFit", ";D^{0} DCA (cm);dN / d(D^{0} DCA) (cm^{-1})", nBinY, binsY);

		for(int j=1; j<=14; j++)
		{
			c15->cd(j+1);
			hPtMD0Dca->GetZaxis()->SetRange(j,j);
			float D0DcaLow = hPtMD0Dca->GetZaxis()->GetBinLowEdge(j);
			float D0DcaHigh = hPtMD0Dca->GetZaxis()->GetBinUpEdge(j);
			TH1D* hMData_D0Dca = (TH1D*)hPtMD0Dca->Project3D("y")->Clone(Form("hM_pt_%1.1f_%1.1f_D0Dca_%1.4f_%1.4f", ptLow, ptHigh, D0DcaLow, D0DcaHigh));
			setColorTitleLabel(hMData_D0Dca);
			fMass = fitMass(hMData_D0Dca, hMMCSignal, hMMCSwapped);

			float yield = fMass->GetParameter(0)*fMass->GetParameter(7)/hMData_D0Dca->GetBinWidth(1);
			float yieldError = fMass->GetParError(0)*fMass->GetParameter(7)/hMData_D0Dca->GetBinWidth(1);

			hD0DcaDataFit->SetBinContent(j, yield);
			hD0DcaDataFit->SetBinError(j, yieldError);

			TLatex* texD0Dca = new TLatex(0.18,0.82,Form("D^{0} DCA: %1.4f - %1.4f",D0DcaLow,D0DcaHigh));
			texD0Dca->SetNDC();
			texD0Dca->SetTextFont(42);
			texD0Dca->SetTextSize(0.06);
			texD0Dca->SetLineWidth(2);
			texD0Dca->Draw();

			TLatex* texYield = new TLatex(0.18,0.74,Form("D^{0} yield: %1.0f #pm %1.0f",yield,yieldError));
			texYield->SetNDC();
			texYield->SetTextFont(42);
			texYield->SetTextSize(0.06);
			texYield->SetLineWidth(2);
			texYield->Draw();
		}

		c15->SaveAs(Form("plots/PbPb_%.0f_%.0f_invMassFit.pdf",ptLow,ptHigh));

		divideBinWidth(hD0DcaDataFit);

		c4->cd(1);
		gPad->SetLogy();

		normalize(hD0DcaMCPSignal);
		setColorTitleLabel(hD0DcaMCPSignal, 2);
		hD0DcaMCPSignal->GetXaxis()->SetRangeUser(0,0.07);

		normalize(hD0DcaMCNPSignal);
		setColorTitleLabel(hD0DcaMCNPSignal, 4);
		hD0DcaMCNPSignal->GetXaxis()->SetRangeUser(0,0.07);
		hD0DcaMCNPSignal->GetYaxis()->SetTitle("dN / d(D^{0} DCA) (cm^{-1})");
		hD0DcaMCNPSignal->GetXaxis()->SetTitle("D^{0} DCA (cm)");
		hD0DcaMCNPSignal->SetMaximum(hD0DcaMCPSignal->GetMaximum()*3.);

		hD0DcaMCNPSignal->Draw("");
		hD0DcaMCPSignal->Draw("same");

		TLegend* leg2 = new TLegend(0.54,0.72,0.90,0.88,NULL,"brNDC");
		leg2->SetBorderSize(0);
		leg2->SetTextSize(0.06);
		leg2->SetTextFont(42);
		leg2->SetFillStyle(0);
		leg2->AddEntry(hD0DcaMCPSignal,"MC Prompt D^{0}","pl");
		leg2->AddEntry(hD0DcaMCNPSignal,"MC Non-prompt D^{0}","pl");
		leg2->Draw("same");

		c4->cd(2);
		gPad->SetLogy();

		TH1D* hD0DcaData = hD0DcaDataFit;
		if(pts[i-1]>20) hD0DcaData = hD0DcaDataSubSideBand;

		setColorTitleLabel(hD0DcaData, 1);

		double integralTotalYield = hD0DcaData->Integral(1,hD0DcaData->GetXaxis()->GetNbins(),"width");
		cout<<"integralTotalYield: "<<integralTotalYield<<endl;

		TF1* fMix = new TF1("fMix",&funMix, 0., 0.5, 2);
		fMix->SetParameters(0.5*integralTotalYield,0.5*integralTotalYield);
		fMix->SetParLimits(0,0,2*integralTotalYield);
		fMix->SetParLimits(1,0,2*integralTotalYield);

		fMix->SetLineColor(2);
		fMix->SetFillColor(kRed-9);
		fMix->SetFillStyle(1001);

		float fitRangeL = 0;
		float fitRangeH = 0.08;

		hD0DcaData->GetXaxis()->SetRangeUser(0,0.07);
		hD0DcaData->Draw();
		int fitStatus = 1;
		TFitResultPtr fitResult;
		double fitPrecision = 1.e-6;
		while(fitStatus)
		{
			TFitter::SetPrecision(fitPrecision);
			fMix->SetParameters(0.5*integralTotalYield,0.5*integralTotalYield);
			fMix->SetParError(0,0.1*integralTotalYield);
			fMix->SetParError(1,0.1*integralTotalYield);
			fitResult = hD0DcaData->Fit("fMix","E SNQ0", "", fitRangeL, fitRangeH);
			fitStatus = fitResult->Status();
			cout<<"fit precision: "<<TFitter::GetPrecision()<<"   status: "<<fitStatus<<endl;
			if(fitStatus)
				fitPrecision *= 10;
		}
		cout<<"============== do main fit ============"<<endl;
		fMix->SetParameters(integralTotalYield,0.9);
		fMix->SetParError(0,0.1*integralTotalYield);
		fMix->SetParError(1,0.1);
		fMix->SetNpx(10000);
		fitResult = hD0DcaData->Fit("fMix","E S0", "", fitRangeL, fitRangeH);
		hD0DcaData->GetFunction("fMix")->Draw("flsame");
		fitStatus = fitResult->Status();
		cout<<"fit precision: "<<TFitter::GetPrecision()<<"   status: "<<fitStatus<<endl;

		TF1* fNP = new TF1("fNP",&funNonPrompt, 0., 0.5, 2);
		fNP->SetParameters(fMix->GetParameter(0),fMix->GetParameter(1));
		fNP->SetRange(fitRangeL,fitRangeH);
		fNP->SetLineColor(4);
		fNP->SetFillStyle(1001);
		fNP->SetFillColor(kBlue-9);
		fNP->SetNpx(10000);
		fNP->Draw("same");  

		hD0DcaData->Draw("same");

		promptDYield[i-1] = fMix->GetParameter(0);
		promptDYieldErrorDataOnly[i-1] = fMix->GetParError(0);
		bToDYield[i-1] = fMix->GetParameter(1);
		bToDYieldErrorDataOnly[i-1] = fMix->GetParError(1);
		totalYield[i-1] = promptDYield[i-1]+bToDYield[i-1];
		promptFraction[i-1] = promptDYield[i-1]/totalYield[i-1];

		cout<<"chi2 / NDF: "<<fitResult->Chi2()<<" / "<<fitResult->Ndf()<<endl;

		texCms->Draw();
		texCol->Draw();
		texPtY->Draw();

		TLatex* texPrompt = new TLatex(0.4,0.73,Form("Prompt D^{0} yield : %.0f #pm %.0f",fMix->GetParameter(0),fMix->GetParError(0)));
		texPrompt->SetNDC();
		texPrompt->SetTextFont(42);
		texPrompt->SetTextSize(0.06);
		texPrompt->SetLineWidth(2);
		texPrompt->Draw();

		TLatex* texNonPrompt = new TLatex(0.4,0.65,Form("B to D^{0} yield : %.0f #pm %.0f",fMix->GetParameter(1),fMix->GetParError(1)));
		texNonPrompt->SetNDC();
		texNonPrompt->SetTextFont(42);
		texNonPrompt->SetTextSize(0.06);
		texNonPrompt->SetLineWidth(2);
		texNonPrompt->Draw();

		TLegend* leg4 = new TLegend(0.56,0.38,0.90,0.62);
		leg4->SetBorderSize(0);
		leg4->SetTextSize(0.06);
		leg4->SetTextFont(42);
		leg4->SetFillStyle(0);
		leg4->AddEntry(hD0DcaData,"Data","pl");
		leg4->AddEntry(fMix,"Prompt D^{0}","f");
		leg4->AddEntry(fNP,"B to D^{0}","f");
		leg4->Draw("same");

		//smear MC smaple with the error, to simulate the MC statistic error effect.
		c4->cd(3);

		hD0DcaMCPSignal = (TH1D*)hD0DcaMCPSignal0->Clone("hMCPSignal");
		hD0DcaMCNPSignal = (TH1D*)hD0DcaMCNPSignal0->Clone("hMCNPSignal");

		TH1D* hNPYield = new TH1D("hNPYield", ";hNPYield", 100, 0., 1.1*(fMix->GetParameter(0)+fMix->GetParameter(1)));
		TH1D* hPYield = new TH1D("hPYield", ";hPYield", 100, 0., 1.1*(fMix->GetParameter(0)+fMix->GetParameter(1)));
		setColorTitleLabel(hNPYield, 1);
		setColorTitleLabel(hPYield, 1);

		int nSmear = 1000;

		for(int j=0; j<nSmear; j++)
		{
			RandomSmear(hD0DcaMCPSignal0, hD0DcaMCPSignal);
			RandomSmear(hD0DcaMCNPSignal0, hD0DcaMCNPSignal);
			fMix->SetParameters(0.5*integralTotalYield,0.5*integralTotalYield);
			fMix->SetParError(0,0.1*integralTotalYield);
			fMix->SetParError(1,0.1*integralTotalYield);

			hD0DcaData->Fit("fMix","E QN0");

			hPYield->Fill(fMix->GetParameter(0));
			hNPYield->Fill(fMix->GetParameter(1));
		}

		hPYield->GetXaxis()->SetTitle("prompt D^{0} yield");
		hPYield->GetYaxis()->SetTitle("counts");
		hPYield->GetYaxis()->SetRangeUser(0.5, 1.4*hPYield->GetMaximum());
		hPYield->SetMarkerStyle(20);
		hPYield->SetStats(0);
		hPYield->Draw("e");
		hPYield->Fit("gaus");

		TLatex* texGaussMeanSigmaP = new TLatex(0.27,0.83,Form("#mu: %.0f              #sigma: %.0f",hPYield->GetFunction("gaus")->GetParameter(1),hPYield->GetFunction("gaus")->GetParameter(2)));
		texGaussMeanSigmaP->SetNDC();
		texGaussMeanSigmaP->SetTextFont(42);
		texGaussMeanSigmaP->SetTextSize(0.06);
		texGaussMeanSigmaP->SetLineWidth(2);
		texGaussMeanSigmaP->Draw();

		float promptYieldErrorMc = hPYield->GetFunction("gaus")->GetParameter(2);
		promptDYieldError[i-1] = sqrt(pow(promptDYieldErrorDataOnly[i-1],2)+pow(promptYieldErrorMc,2));

		c4->cd(4);

		hNPYield->GetXaxis()->SetTitle("B to D^{0} yield");
		hNPYield->GetYaxis()->SetTitle("counts");
		hNPYield->GetYaxis()->SetRangeUser(0.5, 1.4*hNPYield->GetMaximum());
		hNPYield->SetMarkerStyle(20);
		hNPYield->SetStats(0);
		hNPYield->Draw("e");
		hNPYield->Fit("gaus");

		TLatex* texGaussMeanSigmaNP = new TLatex(0.27,0.83,Form("#mu: %.0f              #sigma: %.0f",hNPYield->GetFunction("gaus")->GetParameter(1),hNPYield->GetFunction("gaus")->GetParameter(2)));
		texGaussMeanSigmaNP->SetNDC();
		texGaussMeanSigmaNP->SetTextFont(42);
		texGaussMeanSigmaNP->SetTextSize(0.06);
		texGaussMeanSigmaNP->SetLineWidth(2);
		texGaussMeanSigmaNP->Draw();

		float bToDYieldErrorMc = hNPYield->GetFunction("gaus")->GetParameter(2);
		bToDYieldError[i-1] = sqrt(pow(bToDYieldErrorDataOnly[i-1],2)+pow(bToDYieldErrorMc,2));

		cout<<"prompt D yield: "<<promptDYield[i-1]<<" +- "<<promptDYieldError[i-1]<<" (+- "<<promptDYieldErrorDataOnly[i-1]<<" +- "<<promptYieldErrorMc<<" )"<<endl;
		cout<<"B to D yield: "<<bToDYield[i-1]<<" +- "<<bToDYieldError[i-1]<<" (+- "<<bToDYieldErrorDataOnly[i-1]<<" +- "<<bToDYieldErrorMc<<" )"<<endl;
		cout<<"total yield: "<<totalYield[i-1]<<endl;
		cout<<"prompt fraction: "<<promptFraction[i-1]<<endl;

		float promptMCScale = promptDYield[i-1]/integralRawYieldMCP;
		float nonPromptMCScale = bToDYield[i-1]/integralRawYieldMCNP;

		cout<<"promptMCScale: "<<promptMCScale<<endl;
		cout<<"nonPromptMCScale: "<<nonPromptMCScale<<endl;

		//restore original unsmeared histograms before saving plots
		delete hD0DcaMCPSignal;
		delete hD0DcaMCNPSignal;
		hD0DcaMCPSignal = hD0DcaMCPSignal0;
		hD0DcaMCNPSignal = hD0DcaMCNPSignal0;
		hD0DcaData->Fit("fMix","E QN0");

		c4->SaveAs(Form("plots/PbPb_%.0f_%.0f_fit.pdf",ptLow,ptHigh));

		c1->cd();

		TH1D* hD0DcaDataOverFit = (TH1D*)hD0DcaData->Clone("hD0DcaDataOverFit");
		hD0DcaDataOverFit->Divide(fMix);
		hD0DcaDataOverFit->GetYaxis()->SetTitle("data / fit");
		hD0DcaDataOverFit->GetYaxis()->SetRangeUser(0,5);
		hD0DcaDataOverFit->GetXaxis()->SetRangeUser(0,0.07);
		setColorTitleLabel(hD0DcaDataOverFit, 1);
		hD0DcaDataOverFit->Draw("e");

		TF1* fLine1 = new TF1("fLine1", "1", 0,1);
		fLine1->Draw("same");
		hD0DcaDataOverFit->Draw("esame");

		c1->SaveAs(Form("plots/dataOverFit_%.0f_%.0f_fit.pdf",ptLow,ptHigh));

		delete hD0DcaMCPSignal;
		delete hD0DcaMCNPSignal;

	} // end for i ptbins
	c1->cd();

	TH1D* hStupidJie = new TH1D("hStupidJie", "", 100, 0, 100);
	hStupidJie->GetYaxis()->SetRangeUser(0,1);
	hStupidJie->GetXaxis()->SetTitle("p_{T} (GeV/c)");
	hStupidJie->GetYaxis()->SetTitle("prompt fraction");
	hStupidJie->SetStats(0);
	hStupidJie->Draw();
	TGraph* grFraction = new TGraph(nPtBins, pts, promptFraction);
	grFraction->SetName("grPromptFraction");
	grFraction->SetMarkerStyle(20);
	grFraction->Draw("psame");

	c1->SaveAs("promptFraction.pdf");

	c1->SetLogy();

	TH1D* hBtoDRawYield = new TH1D("hBtoDRawYield", ";p_{T} (GeV/c);dN/dp_{T} ((GeV/c)^{-1})", nPtBins, ptBins);
	for(int i=1; i<=nPtBins; i++)
	{
		if(bToDYield[i-1] <= 0) continue;
		hBtoDRawYield->SetBinContent(i, bToDYield[i-1]);
		hBtoDRawYield->SetBinError(i, bToDYieldError[i-1]);
	}
	divideBinWidth(hBtoDRawYield);
	setColorTitleLabel(hBtoDRawYield, 1);
	c1->SetBottomMargin(0.14);
	hBtoDRawYield->Draw("p");
	c1->SaveAs("BtoD.pdf");

	TH1D* hPromptDRawYield = new TH1D("hPromptDRawYield", ";p_{T} (GeV/c);dN/dp_{T} ((GeV/c)^{-1})", nPtBins, ptBins);
	for(int i=1; i<=nPtBins; i++)
	{
		if(promptDYield[i-1] <= 0) continue;
		hPromptDRawYield->SetBinContent(i, promptDYield[i-1]);
		hPromptDRawYield->SetBinError(i, promptDYieldError[i-1]);
	}
	divideBinWidth(hPromptDRawYield);
	setColorTitleLabel(hPromptDRawYield, 1);
	c1->SetBottomMargin(0.14);
	hPromptDRawYield->Draw("p");
	c1->SaveAs("promptD.pdf");

	TH1D* hTotalDYieldInvMassFit = new TH1D("hTotalDYieldInvMassFit", ";p_{T} (GeV/c);dN/dp_{T} ((GeV/c)^{-1})", nPtBins, ptBins);
	for(int i=1; i<=nPtBins; i++)
	{
		if(totalYieldInvMassFit[i-1] <= 0) continue;
		hTotalDYieldInvMassFit->SetBinContent(i, totalYieldInvMassFit[i-1]);
		hTotalDYieldInvMassFit->SetBinError(i, totalYieldInvMassFitError[i-1]);
	}
	divideBinWidth(hTotalDYieldInvMassFit);
	setColorTitleLabel(hTotalDYieldInvMassFit, 1);
	hTotalDYieldInvMassFit->Draw("p");
	c1->SaveAs("totalDInvMassFit.pdf");

	TFile* fOut = new TFile("bFeedDownResult.root", "recreate");
	fOut->WriteTObject(grFraction);
	fOut->WriteTObject(hBtoDRawYield);
	fOut->WriteTObject(hPromptDRawYield);
	fOut->WriteTObject(hTotalDYieldInvMassFit);
	fOut->Write();
	fOut->Close();
}
コード例 #2
0
ファイル: fakerate.C プロジェクト: radziej/findsusyb3
void tight_loose_ratioplot()
{
  DEBUG("tight_loose_ratioplot() start");
  MakeCanvas(1,1);

  DEBUG("reading histograms");
  // "plot" histograms, i.e. read into memory
  cd(1);
  top();
  plot3("TightMuons");
  cd(2);
  top();
  plot3("LooseMuons");
  cd(1);

  TLegend * leg = new TLegend(0.53, 0.52, 0.77, 0.84);
  setopt(leg);
  Bool_t first = kTRUE;

  // get summed histograms
  vector<Int_t> entries;
  TH3D * hTightSum = 0;
  TH3D * hLooseSum = 0;
  for (Int_t i = 0; i < gMaxProcess; i++) {
    Int_t process = gOrder[0][i];
    // not existing
    TH3D * hTight3 = gHisto3[0][process];
    TH3D * hLoose3 = gHisto3[1][process];
    if (hTight3 == 0 || hLoose3 == 0) {
      ERROR("could not get histogram # " << i << " from memory");
      continue;
    }
    // if joined, it was already considered in previous iteration
    if (gProcess[process].join)
      continue;
    if (strncmp(gProcess[process].fname, "qcd", 3) &&
	strncmp(gProcess[process].fname, "dyll", 4) &&
	strncmp(gProcess[process].fname, "ttjets", 6) &&
	strncmp(gProcess[process].fname, "wjetstolnu", 10) &&
	strncmp(gProcess[process].fname, "data", 3))
      continue;
    DEBUG("Creating histos for process " << gProcess[process].fname);
    hTightSum = new TH3D(*hTight3);
    hLooseSum = new TH3D(*hLoose3);
    // check if histograms are joined -> we need to add statistics
    for (Int_t j = i+1; j < gMaxProcess; j++) {
      Int_t proc = gOrder[0][j];
      if (gOrder[1][j] != proc) {
	ERROR("wrong order - need to restart!");
	return;
      }
      // only add joined histograms
      if (!gProcess[proc].join)
	break;
      // not existing
      if (gHisto3[0][proc] == 0 || gHisto3[1][proc] == 0)
	continue;
      DEBUG("Adding histos for process " << gProcess[proc].fname);
      hTightSum->Add(gHisto3[0][proc], 1.);
      hLooseSum->Add(gHisto3[1][proc], 1.);
    }
    DEBUG("Getting ratio");
    TH1D * histo = get_1d_ratio(hTightSum, hLooseSum);
    if (histo == 0) {
      ERROR("Division failed");
      return;
    }
    check_1d_ratio_histogram(histo);
    setopt(histo);
    histo->SetLineColor(gProcess[process].lcolor);
    histo->SetLineStyle(gProcess[process].lstyle);
    DEBUG("Adding to legend");
    if (process != gMaxProcess-1) {
      leg->AddEntry(histo, gProcess[process].tname, "l");
    }
    else {
      leg->AddEntry(histo, gProcess[process].tname);
    }
    DEBUG("Draw");
    if (first) {
      histo->SetMaximum(1.);
      histo->SetMinimum(-0.1);
      histo->SetXTitle("p_{T}(#mu) [GeV]");
      histo->SetYTitle("T/L ratio");
      histo->Draw("ehisto");
      first = kFALSE;
    }
    else {
      if (process == gMaxProcess-1) {
	histo->SetMarkerStyle(gProcess[gMaxProcess-1].marker);
	histo->Draw("epsame");
      }
      else {
	histo->Draw("ehistosame");
      }
    }
  }
  // add subtracted data histogram
  TH1D * hdata_subtracted = get_subtracted_tight_loose_ratio(false, false);
  if (hdata_subtracted == 0)
    return;
  Double_t linemax = hdata_subtracted->GetXaxis()->GetXmax();
  Double_t linemin = hdata_subtracted->GetXaxis()->GetXmin();
  TLine * l = new TLine(linemin, 0, linemax, 0);
  l->SetLineStyle(kDotted);
  l->SetLineColor(kBlack);
  l->SetLineWidth(2);
  l->Draw();
  hdata_subtracted->SetMarkerColor(kBlue);
  hdata_subtracted->SetMarkerStyle(8);
  hdata_subtracted->Draw("epsame");
  leg->AddEntry(hdata_subtracted, "data subtr.", "ep");
  leg->Draw();
  gPad->Print("tlratio.pdf");
}
コード例 #3
0
int ScanChain( TChain* chain, bool fast = true, int nEvents = -1, string skimFilePrefix = "test") {

  //load PUweights
  TFile *fPU = new TFile("puWeights.root","READ");
  TH1D *puWeight     = (TH1D*)fPU->Get("puWeight");
  TH1D *puWeightUp   = (TH1D*)fPU->Get("puWeightUp");
  TH1D *puWeightDown = (TH1D*)fPU->Get("puWeightDown");

  TFile *fxsec = new TFile("xsec_stop_13TeV.root","READ");
  TH1D *hxsec     = (TH1D*)fxsec->Get("stop");

  TFile *f_el_SF       = new TFile("lepsf/kinematicBinSFele.root", "read");
  TFile *f_mu_SF_id    = new TFile("lepsf/TnP_MuonID_NUM_MediumID_DENOM_generalTracks_VAR_map_pt_eta.root", "read");
  TFile *f_mu_SF_iso   = new TFile("lepsf/TnP_MuonID_NUM_MiniIsoTight_DENOM_LooseID_VAR_map_pt_eta.root");
  TFile *f_mu_SF_veto_id  = new TFile("lepsf/TnP_MuonID_NUM_LooseID_DENOM_generalTracks_VAR_map_pt_eta.root", "read");
  TFile *f_mu_SF_veto_iso = new TFile("lepsf/TnP_MuonID_NUM_MiniIsoTight_DENOM_LooseID_VAR_map_pt_eta.root");
  //TFile *f_mu_SF_veto_iso = new TFile("lepsf/TnP_MuonID_NUM_MiniIsoLoose_DENOM_LooseID_VAR_map_pt_eta.root");
  //TFile *f_vetoLep_eff = new TFile("lepsf/lepeff__ttbar_powheg_pythia8_25ns.root", "read");
  TFile *f_vetoLep_eff = new TFile("lepsf/lepeff__ttbar_powheg_pythia8_25ns__SRcuts.root", "read");  
  TH2D *h_el_SF_id_temp      = (TH2D*)f_el_SF->Get("CutBasedMedium");
  TH2D *h_el_SF_iso_temp     = (TH2D*)f_el_SF->Get("MiniIso0p1_vs_AbsEta");
  TH2D *h_el_SF_veto_id_temp  = (TH2D*)f_el_SF->Get("CutBasedVeto");
  TH2D *h_el_SF_veto_iso_temp = (TH2D*)f_el_SF->Get("MiniIso0p4_vs_AbsEta");
  TH2D *h_mu_SF_id_temp      = (TH2D*)f_mu_SF_id->Get("pt_abseta_PLOT_pair_probeMultiplicity_bin0_&_tag_combRelIsoPF04dBeta_bin0_&_tag_pt_bin0_&_tag_IsoMu20_pass");
  TH2D *h_mu_SF_iso_temp     = (TH2D*)f_mu_SF_iso->Get("pt_abseta_PLOT_pair_probeMultiplicity_bin0_&_tag_combRelIsoPF04dBeta_bin0_&_tag_pt_bin0_&_PF_pass_&_tag_IsoMu20_pass");
  TH2D *h_mu_SF_veto_id_temp  = (TH2D*)f_mu_SF_veto_id->Get("pt_abseta_PLOT_pair_probeMultiplicity_bin0_&_tag_combRelIsoPF04dBeta_bin0_&_tag_pt_bin0_&_tag_IsoMu20_pass");
  TH2D *h_mu_SF_veto_iso_temp = (TH2D*)f_mu_SF_veto_iso->Get("pt_abseta_PLOT_pair_probeMultiplicity_bin0_&_tag_combRelIsoPF04dBeta_bin0_&_tag_pt_bin0_&_PF_pass_&_tag_IsoMu20_pass");
  //TH2D *h_el_vetoLepEff_temp = (TH2D*)f_vetoLep_eff->Get("h2_lepEff_vetoSel_Eff_el");
  //TH2D *h_mu_vetoLepEff_temp = (TH2D*)f_vetoLep_eff->Get("h2_lepEff_vetoSel_Eff_mu");
  TH2D *h_el_vetoLepEff_temp = (TH2D*)f_vetoLep_eff->Get("h2_lepEff_vetoSel_rebin_Eff_el");
  TH2D *h_mu_vetoLepEff_temp = (TH2D*)f_vetoLep_eff->Get("h2_lepEff_vetoSel_rebin_Eff_mu");
  TH2D *h_el_SF_id  = (TH2D*)h_el_SF_id_temp->Clone("h_el_SF_id");
  TH2D *h_el_SF_iso = (TH2D*)h_el_SF_iso_temp->Clone("h_el_SF_iso");
  TH2D *h_mu_SF_id  = (TH2D*)h_mu_SF_id_temp->Clone("h_mu_SF_id");
  TH2D *h_mu_SF_iso = (TH2D*)h_mu_SF_iso_temp->Clone("h_mu_SF_iso");
  TH2D *h_el_SF_veto_id  = (TH2D*)h_el_SF_veto_id_temp->Clone("h_el_SF_veto_id");
  TH2D *h_el_SF_veto_iso = (TH2D*)h_el_SF_veto_iso_temp->Clone("h_el_SF_veto_iso");
  TH2D *h_mu_SF_veto_id  = (TH2D*)h_mu_SF_veto_id_temp->Clone("h_mu_SF_veto_id");
  TH2D *h_mu_SF_veto_iso = (TH2D*)h_mu_SF_veto_iso_temp->Clone("h_mu_SF_veto_iso");
  //This is are the important ones
  TH2D *h_el_vetoLepEff = (TH2D*)h_el_vetoLepEff_temp->Clone("h_el_vetoLepEff");
  TH2D *h_mu_vetoLepEff = (TH2D*)h_mu_vetoLepEff_temp->Clone("h_mu_vetoLepEff");
  TH2D *h_el_SF = (TH2D*)h_el_SF_id->Clone("h_el_SF");
  h_el_SF->Multiply(h_el_SF_iso);
  TH2D *h_el_SF_veto = (TH2D*)h_el_SF_veto_id->Clone("h_el_SF_veto");
  TH2D *h_mu_SF = (TH2D*)h_mu_SF_id->Clone("h_mu_SF");
  h_mu_SF->Multiply(h_mu_SF_iso);
  TH2D *h_mu_SF_veto = (TH2D*)h_mu_SF_veto_id->Clone("h_mu_SF_veto");
  h_mu_SF_veto->Multiply(h_mu_SF_veto_iso);
  TFile *f_el_FS_ID       = new TFile("lepsf/sf_el_mediumCB.root", "read");
  TFile *f_el_FS_Iso      = new TFile("lepsf/sf_el_mini01.root", "read");
  TFile *f_mu_FS_ID       = new TFile("lepsf/sf_mu_mediumID.root", "read");
  TFile *f_mu_FS_Iso      = new TFile("lepsf/sf_mu_mini02.root", "read");
  TH2D *h_el_FS_ID_temp  = (TH2D*)f_el_FS_ID ->Get("histo2D");
  //TH2D *h_el_FS_ID       = (TH2D*)h_el_FS_ID_temp ->Clone("h_el_FS_ID");
  TH2D *h_el_FS          = (TH2D*)h_el_FS_ID_temp ->Clone("h_el_FS");
  TH2D *h_el_FS_Iso_temp = (TH2D*)f_el_FS_Iso->Get("histo2D");
  //TH2D *h_el_FS_Iso      = (TH2D*)h_el_FS_Iso_temp->Clone("h_el_FS_Iso");
  h_el_FS->Multiply(h_el_FS_Iso_temp);
  TH2D *h_mu_FS_ID_temp  = (TH2D*)f_mu_FS_ID ->Get("histo2D");
  //TH2D *h_mu_FS_ID       = (TH2D*)h_mu_FS_ID_temp ->Clone("h_mu_FS_ID");
  TH2D *h_mu_FS          = (TH2D*)h_mu_FS_ID_temp ->Clone("h_mu_FS");
  TH2D *h_mu_FS_Iso_temp = (TH2D*)f_mu_FS_Iso->Get("histo2D");
  //TH2D *h_mu_FS_Iso      = (TH2D*)h_mu_FS_Iso_temp->Clone("h_mu_FS_ID");
  h_mu_FS->Multiply(h_mu_FS_Iso_temp);

  
  // Benchmark
  TBenchmark *bmark = new TBenchmark();
  bmark->Start("benchmark");

  // Example Histograms
  TDirectory *rootdir = gDirectory->GetDirectory("Rint:");


  map<string, TH3D*> histos;//use D histos as weights can vary a lot among the signal
  vector<string> histonames; histonames.clear();
  //  vector<int> hbins; hbins.clear();
  //  vector<float> hlow; hlow.clear();
  //  vector<float> hup; hup.clear();

  //lumi, trigger, stats done
  histonames.push_back("SRyield");
  histonames.push_back("SR_Bup_HF");//done
  histonames.push_back("SR_Bdown_HF");
  histonames.push_back("SR_Bup_LF");//done
  histonames.push_back("SR_Bdown_LF");
  histonames.push_back("SR_JESup");
  histonames.push_back("SR_JESdown");
  histonames.push_back("SR_muRFup");
  histonames.push_back("SR_muRFdown");
  //histonames.push_back("SR_PDFup");
  //histonames.push_back("SR_PDFdown");
  histonames.push_back("SR_ISRup");//done preliminary
  histonames.push_back("SR_ISRdown");
  histonames.push_back("SR_PUup");//done preliminary
  histonames.push_back("SR_PUdown");
  histonames.push_back("SR_LepEffup");//done - I guess we need no renormalization - no fastsim in, no vetoSF
  histonames.push_back("SR_LepEffdown");
  histonames.push_back("SR_LepEffFSup");//done - I guess we need no renormalization - no fastsim in, no vetoSF
  histonames.push_back("SR_LepEffFSdown");
  histonames.push_back("SR_Xsecup");//done
  histonames.push_back("SR_Xsecdown");
  histonames.push_back("CR1l_sigcontamination");//scaled to signalreg yield
  histonames.push_back("CR2l_sigcontamination");//scaled to signalreg yield
  /*
  histonames.push_back("eventsum");
  histonames.push_back("rawweightsum");
  histonames.push_back("totweightsum");
  histonames.push_back("ISRsum");
  histonames.push_back("BSFsum");
  histonames.push_back("PUweightsum");
  histonames.push_back("xsecsum");
  histonames.push_back("nevtsum");
  histonames.push_back("lepsum");
  histonames.push_back("lepSFsum");
  */
  for(unsigned int i = 0; i<histonames.size(); ++i){
    string mapname = histonames[i];
    if(histos.count(mapname) == 0 ) histos[mapname] = new TH3D(mapname.c_str(), "", 37,99,1024, 19,-1,474, 13, -0.5,12.5);
    //mStop 100-1000, mLSP 0-450, SR 1-12, 9200 bins, SR 0 is non-SR - in case it it needed!!
      histos[mapname]->Sumw2(); histos[mapname]->SetDirectory(rootdir);
    }

  
  
  // Loop over events to Analyze
  unsigned int nEventsTotal = 0;
  unsigned int nEventsChain = chain->GetEntries();
  if( nEvents >= 0 ) nEventsChain = nEvents;
  TObjArray *listOfFiles = chain->GetListOfFiles();
  TIter fileIter(listOfFiles);
  TFile *currentFile = 0;

  //get the reweighting histograms
  TIter fileIterFirst(listOfFiles);
  TFile *currentFileFirst = 0;
  TH3D* counterhistSig;
  TH2F* histNEvts;
  bool thisisfirst = true;
  // File Loop for adding correct histograms
  while ( (currentFileFirst = (TFile*)fileIterFirst.Next()) ) {
    TFile *file = new TFile( currentFileFirst->GetTitle() );
    file->cd();
    if(thisisfirst){
      counterhistSig = (TH3D*)file->Get("h_counterSMS");
      counterhistSig->SetDirectory(0); 
      histNEvts = (TH2F*)file->Get("histNEvts");
      histNEvts->SetDirectory(0);
      thisisfirst = false;
    } else {
      TH3D *tempcounterhistSig = (TH3D*)file->Get("h_counterSMS");
      tempcounterhistSig->SetDirectory(0); 
      TH2F *temphistNEvts = (TH2F*)file->Get("histNEvts");
      temphistNEvts->SetDirectory(0);
      counterhistSig->Add(tempcounterhistSig);
      histNEvts->Add(temphistNEvts);
      tempcounterhistSig->Delete();
      temphistNEvts->Delete();
    }
    file->Close();
    delete file;
  }

  // File Loop
  while ( (currentFile = (TFile*)fileIter.Next()) ) {

    // Get File Content
    TFile *file = new TFile( currentFile->GetTitle() );
    TTree *tree = (TTree*)file->Get("t");
    if(fast) TTreeCache::SetLearnEntries(10);
    if(fast) tree->SetCacheSize(128*1024*1024);
    cms3.Init(tree);
    
    // Loop over Events in current file
    if( nEventsTotal >= nEventsChain ) continue;
    unsigned int nEventsTree = tree->GetEntriesFast();
    for( unsigned int event = 0; event < nEventsTree; ++event) {
 
      // Get Event Content
      if( nEventsTotal >= nEventsChain ) continue;
      if(fast) tree->LoadTree(event);
      cms3.GetEntry(event);
      ++nEventsTotal;
    
      // Progress
      CMS3::progress( nEventsTotal, nEventsChain );

      // Analysis Code

      float mStop = mass_stop();
      float mLSP = mass_lsp();
      float mCharg = mass_chargino();
      //float xVal = mass_lsp();
      int Nevts = histNEvts->GetBinContent(histNEvts->FindBin(mStop,mLSP));
      double nevts = double(Nevts);
      //float weight = cms3.scale1fb()*2.11;
      double PUweight     = puWeight    ->GetBinContent(puWeight    ->FindBin(pu_ntrue() ) );
      double PUweightUp   = puWeightUp  ->GetBinContent(puWeightUp  ->FindBin(pu_ntrue() ) );
      double PUweightDown = puWeightDown->GetBinContent(puWeightDown->FindBin(pu_ntrue() ) );
      PUweightUp = 1; PUweightDown = PUweight; PUweight = 1; //now PU syst is applying vs not applying
     double ISRnorm = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,19));
      double ISRnormup = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,20));
      double ISRnormdown = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,21));
      double ISRweight = weight_ISR();
      double BSFnorm = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,14));
      double BSFnormHup = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,15));
      double BSFnormLup = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,16));
      double BSFnormHdown = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,17));
      double BSFnormLdown = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,18));
      double BSFweight = weight_btagsf();
      double muRFnorm = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,1));
      double muRFnormup = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,5));
      double muRFnormdown = counterhistSig->GetBinContent(counterhistSig->FindBin(mStop,mLSP,9));
      if(ISRnorm>0) ISRweight*=nevts/ISRnorm;
      if(ISRnorm<=0||ISRnormup<=0||ISRnormdown<=0){ ISRnormdown=1.; ISRnormup=1.; ISRnorm=1.;}
      if(ISRweight!=weight_ISR()) cout << "ISRw " << ISRweight << " wISR " << weight_ISR() << " nevts " << nevts << " ISRn " << ISRnorm << endl;
      if(BSFnorm>0) BSFweight *=nevts/BSFnorm;
      if(BSFnorm<=0||BSFnormHup<=0||BSFnormLup<=0||BSFnormHdown<=0||BSFnormLdown<=0){
	BSFnorm=1; BSFnormHup=1; BSFnormLup=1; BSFnormHdown=1; BSFnormLdown=1;
      }
      if(muRFnorm<=0||muRFnormup<=0||muRFnormdown<=0){ muRFnormdown=1; muRFnormup=1; muRFnorm=1; }
      //lepSF is done below
      double xsection = hxsec->GetBinContent(hxsec->FindBin(mStop));
      double xsectionerr = hxsec->GetBinError(hxsec->FindBin(mStop));
      //double rawweight = xsec()*2260./nevts;
      //double weight = xsec()*2260./nevts*PUweight*ISRweight*BSFweight;//xsec given in pb
      double rawweight = xsection*2260./nevts;
      double weight = xsection*2260./nevts*PUweight*ISRweight*BSFweight;//xsec given in pb
      //did put ISRweight which should be ==1
      if(ISRweight!=1) cout << "ISRw " << ISRweight << endl;
      if(event==0) cout << "weight " << weight << " nEvents " << nEventsTree << " filename " << currentFile->GetTitle() << endl;

      int NSLeps = 0;
      int NAddVetoLeps = 0;
      if(lep1_is_mu()){
	if(lep1_pt()>20&&fabs(lep1_eta())<2.4) {++NSLeps;}
      } else if (lep1_is_el()){
	if(lep1_pt()>20&&fabs(lep1_eta())<1.4442) {++NSLeps; }
      } if(lep2_is_mu()){
	if(lep2_pt()>20&&fabs(lep2_eta())<2.4) {++NSLeps;}
      } else if (lep2_is_el()){
	if(lep2_pt()>20&&fabs(lep2_eta())<1.4442) {++NSLeps; }
      }
      if(lep2_is_mu()){
	if(lep2_pt()>10&&fabs(lep2_eta())<2.4) {++NAddVetoLeps;}
      } else if (lep2_is_el()){
	if(lep2_pt()>10&&fabs(lep2_eta())<2.4) {++NAddVetoLeps; }
      }
      if(NSLeps<1) continue;//temp
      float lepSF_pt_cutoff = 100.0;
      float lepSF_pt_min    = 10.0;
      double lepSF    = 1.0;
      double lepSF_Up = 1.0;
      double lepSF_Dn = 1.0;
      float lepSF_FS_pt_cutoff = 200.0;
      double lepSF_FS    = 1.0;
      double lepSF_FS_Up = 1.0;
      double lepSF_FS_Dn = 1.0;	
      if(lep1_is_el()){
	int binX = h_el_SF->GetXaxis()->FindBin( std::min(lepSF_pt_cutoff, (float)lep1_p4().Pt()) );
	int binY = h_el_SF->GetYaxis()->FindBin( fabs(lep1_p4().Eta()) );
	lepSF    = h_el_SF->GetBinContent( binX, binY );
	lepSF_Up = lepSF + h_el_SF->GetBinError( binX, binY );
	lepSF_Dn = lepSF - h_el_SF->GetBinError( binX, binY );
	int bin = h_el_FS->FindBin(  std::min(lepSF_FS_pt_cutoff, (float)lep1_p4().Pt()), fabs(lep1_p4().Eta()) );
	lepSF_FS = h_el_FS->GetBinContent(bin);
	lepSF_FS_Up = lepSF_FS + h_el_FS->GetBinError(bin);
	lepSF_FS_Dn = lepSF_FS + h_el_FS->GetBinError(bin);
      }
      if(lep1_is_mu()){
	int binX = h_mu_SF->GetXaxis()->FindBin( std::min(lepSF_pt_cutoff, (float)lep1_p4().Pt()) );
	int binY = h_mu_SF->GetYaxis()->FindBin( fabs(lep1_p4().Eta()) );
	lepSF    = h_mu_SF->GetBinContent( binX, binY );
	lepSF_Up = lepSF + h_mu_SF->GetBinError( binX, binY );
	lepSF_Dn = lepSF - h_mu_SF->GetBinError( binX, binY );
	int bin = h_mu_FS->FindBin(  std::min(lepSF_FS_pt_cutoff, (float)lep1_p4().Pt()), fabs(lep1_p4().Eta()) );
	lepSF_FS = h_mu_FS->GetBinContent(bin);
	lepSF_FS_Up = lepSF_FS + h_mu_FS->GetBinError(bin);
	lepSF_FS_Dn = lepSF_FS + h_mu_FS->GetBinError(bin);
      }
      weight *= (lepSF*lepSF_FS);

      
      if(nvtxs()<0)               continue;
      if(ngoodleps()<1)           continue;//accomodate 2l-CR
      if(nvetoleps()<1)           continue;//accomodate 2l-CR
    //if(!PassTrackVeto_v3())     continue;//accomodate 2l-CR
    //if(!PassTauVeto())          continue;//accomodate 2l-CR
      if(ngoodjets()<2)           continue;
      if(ngoodbtags()<0)          continue;//accomodate 1l-CR
      if(pfmet()<250)             continue;
      if(mt_met_lep()<150)        continue;
      if(mindphi_met_j1_j2()<0.8) continue;


      int SR = -1;
      int compressedSR = -1;
      if(ngoodleps()==1&&nvetoleps()==1&&PassTrackVeto_v3()&&PassTauVeto()&&ngoodbtags()>=1){//basis for SR 1l, >=1b
	if(ngoodjets()>=4){
	  if(MT2W()<=200){
	    if(pfmet()>325) SR = 2;
	    else SR = 1;
	  } else { //high MT2W
	    if(pfmet()>450) SR = 5;
	    else if(pfmet()>350) SR = 4;
	    else SR = 3;
	  }
	} else if(ngoodjets()==3 && MT2W()>200 && pfmet()>350) {
	  SR = 6;
	} else if(MT2W()>200&&topnessMod()>(-3)) { //2 or 3 jets
	  if(ngoodbtags()==1){
	    if(pfmet()>400) SR = 8;
	    else SR = 7;
	  } else {//ge2 jets
	    if(pfmet()>400) SR = 10;
	    else SR = 9;
	  }
	}
	//compressed region (jets are sorted by pt
	if(ngoodjets()>=5&&ak4pfjets_passMEDbtag()[0]==false&&ak4pfjets_pt()[0]>200.){
	  if(MT2W()<=200) compressedSR = 11;
	  else compressedSR = 12;
	}
      }

      //CR-1l
      int CR1l = -1;
      if(ngoodleps()==1&&nvetoleps()==1&&PassTrackVeto_v3()&&PassTauVeto()&&ngoodbtags()==0&&ngoodjets()>=3&&MT2W()>200){
	if(ngoodjets()==3){
	  CR1l = 1;
	} else {
	  CR1l = 2;
	}
      }
      //CR1l  1 --> SR  6
      //CR1l  2 --> SR  3-5
      float CR1l_1_6 = 0.37*0.18;
      float CR1l_2_3 = 0.55*0.15;
      float CR1l_2_4 = 0.25*0.29;
      float CR1l_2_5 = 0.20*0.40;

      //CR2l = -1;
      int lepind = -1;
      if(ngoodleps()>=2&&NSLeps==2) lepind = 5;
      else if(ngoodleps()==2&&NSLeps==2) lepind = 4;//exactly two leptons,CR4
      else if(ngoodleps()==1&&NSLeps==1&&NAddVetoLeps>=1) lepind = 3;//one lepton, but more than 1 add. loose,1l,>2l
      //else if(ngoodleps()==1&&NSLeps==1&&nvetoleps()==2) lepind = 2;//one lepton + 1 add. loose,CR5
      else if(ngoodleps()==1&&NSLeps==1&&nvetoleps()==0&&(!PassTrackVeto_v3()||!PassTauVeto())) lepind = 1;//exactly one lepton, but do not pass track/tau veto - i.e. one additional track or tau, CR6
      int CR2l = -1;
      if((lepind==4||lepind==3||lepind==1)&&ngoodjets()>=3&&ngoodbtags()>=1){
	if(MT2W()<=200) CR2l = 1;
	else CR2l = 2;
      }
      float CR2l_1_1 = 0.61*0.48;
      float CR2l_1_2 = 0.61*0.19;
      float CR2l_2_3 = 0.44*0.39;
      float CR2l_2_4 = 0.44*0.11;
      float CR2l_2_5 = 0.44*0.07;
      float CR2l_2_6 = 0.44*0.11;

      if(SR==(-1)&&CR1l==(-1)&&CR2l==(-1)&&compressedSR==(-1)) continue;
      //implement some sanity checks
      if(CR1l!=(-1)&&CR2l!=(-1)) cout << "WTF CR1l " << CR1l << " CR2l " << CR2l << endl;
      if(SR!=(-1)&&CR1l!=(-1)) cout << "WTF SR " << SR << " CR1l " << CR1l << endl;
      if(SR!=(-1)&&CR2l!=(-1)) cout << "WTF SR " << SR << " CR2l " << CR2l << endl;

      //ISR reweighting, get stop pair using last copy:
      double ISRup = weight_ISRup()/weight_ISR()*ISRnorm/ISRnormup;
      double ISRdown = weight_ISRdown()/weight_ISR()*ISRnorm/ISRnormdown;
      //double XSup = (xsec()+xsec_uncert())/xsec();
      //double XSdown = (xsec()-xsec_uncert())/xsec();
      double XSup = (xsection+xsectionerr)/xsection;
      double XSdown = (xsection-xsectionerr)/xsection;
      double PUup = PUweightUp/PUweight;
      double PUdown = PUweightDown/PUweight;
      double lEffup = lepSF_Up/lepSF;
      double lEffdown = lepSF_Dn/lepSF;
      double lEffFSup = lepSF_FS_Up/lepSF_FS;
      double lEffFSdown = lepSF_FS_Dn/lepSF_FS;
      double BSFHup = weight_btagsf_heavy_UP()/weight_btagsf()*BSFnorm/BSFnormHup;
      double BSFLup = weight_btagsf_light_UP()/weight_btagsf()*BSFnorm/BSFnormHup;
      double BSFHdown = weight_btagsf_heavy_DN()/weight_btagsf()*BSFnorm/BSFnormHup;
      double BSFLdown = weight_btagsf_light_DN()/weight_btagsf()*BSFnorm/BSFnormHup;
      double muRFup = genweights().at(4)/genweights().at(0)*muRFnorm/muRFnormup;
      double muRFdown = genweights().at(8)/genweights().at(0)*muRFnorm/muRFnormdown;
      //cout << genweights().at(0) << " " << genweights().at(4) << " " << genweights().at(8) << " " << mStop << " " << mLSP << endl;

      if(CR1l>0){
	if(ngoodleps()!=1) cout << __LINE__ << " " << ngoodleps() << endl;
	if(NSLeps!=1) cout << __LINE__ << " " << NSLeps << endl;
	if(nvetoleps()!=1) cout << __LINE__ << " " << nvetoleps() << endl;
	if(ngoodbtags()>=1) cout << __LINE__ << " " << ngoodbtags() << endl;
	//signal contamination in 0b control region, do correlations later during datacard making
	if(CR1l==1){
	  histos["CR1l_sigcontamination"]->Fill(mStop,mLSP,6,weight*CR1l_1_6);
	} else if(CR1l==2){
	  histos["CR1l_sigcontamination"]->Fill(mStop,mLSP,3,weight*CR1l_2_3);
	  histos["CR1l_sigcontamination"]->Fill(mStop,mLSP,4,weight*CR1l_2_4);
	  histos["CR1l_sigcontamination"]->Fill(mStop,mLSP,5,weight*CR1l_2_5);
	}
      } else if(CR2l>0){
	if(nvetoleps()<=1||(nvetoleps()==1&&(!PassTrackVeto_v3()||!PassTauVeto()))) cout << __LINE__ << " " << nvetoleps() << " " << PassTrackVeto_v3() << " " << PassTauVeto() << endl;
	if(ngoodbtags()<1) cout << __LINE__ << " " << ngoodbtags() << endl;
	//signal contamination in 2l control region, do correlations later during datacard making
	if(CR2l==1){
	  histos["CR2l_sigcontamination"]->Fill(mStop,mLSP,1,weight*CR2l_1_1);
	  histos["CR2l_sigcontamination"]->Fill(mStop,mLSP,2,weight*CR2l_1_2);
	} else if(CR2l==2){
	  histos["CR2l_sigcontamination"]->Fill(mStop,mLSP,3,weight*CR2l_2_3);
	  histos["CR2l_sigcontamination"]->Fill(mStop,mLSP,4,weight*CR2l_2_4);
	  histos["CR2l_sigcontamination"]->Fill(mStop,mLSP,5,weight*CR2l_2_5);
	  histos["CR2l_sigcontamination"]->Fill(mStop,mLSP,6,weight*CR2l_2_6);

	}
      } else if(SR>0){
	if(ngoodleps()!=1) cout << __LINE__ << " " << ngoodleps() << endl;
	if(NSLeps!=1) cout << __LINE__ << " " << NSLeps << endl;
	if(nvetoleps()!=1) cout << __LINE__ << " " << nvetoleps() << endl;
	if(!PassTrackVeto_v3())  cout << __LINE__ << endl;
	if(!PassTauVeto())  cout << __LINE__ << endl;
	if(SR<=6&&ngoodjets()<3) cout << __LINE__ << " " << ngoodjets() << endl;
	if(ngoodbtags()<1) cout << __LINE__ << " " << ngoodbtags() << endl;
	/*
	histos["eventsum"]->Fill(mStop,mLSP,SR,1.);
	histos["rawweightsum"]->Fill(mStop,mLSP,SR,rawweight);
	histos["totweightsum"]->Fill(mStop,mLSP,SR,weight);
	histos["ISRsum"]->Fill(mStop,mLSP,SR,ISRweight);
	histos["BSFsum"]->Fill(mStop,mLSP,SR,BSFweight);
	histos["PUweightsum"]->Fill(mStop,mLSP,SR,PUweight);
	histos["xsecsum"]->Fill(mStop,mLSP,SR,xsection);
	histos["nevtsum"]->Fill(mStop,mLSP,SR,nevts);
	histos["lepsum"]->Fill(mStop,mLSP,SR,lepSF);
	histos["lepSFsum"]->Fill(mStop,mLSP,SR,lepSF_FS);
	*/
	//finally - do signal regions!
	histos["SRyield"]->Fill(mStop,mLSP,SR,weight);
	histos["SR_ISRup"]->Fill(mStop,mLSP,SR,weight*ISRup);
	histos["SR_ISRdown"]->Fill(mStop,mLSP,SR,weight*ISRdown);
	histos["SR_Xsecup"]->Fill(mStop,mLSP,SR,weight*XSup);
	histos["SR_Xsecdown"]->Fill(mStop,mLSP,SR,weight*XSdown);
	histos["SR_PUup"]->Fill(mStop,mLSP,SR,weight*PUup);
	histos["SR_PUdown"]->Fill(mStop,mLSP,SR,weight*PUdown);
	histos["SR_Bup_HF"]->Fill(mStop,mLSP,SR,weight*BSFHup);
	histos["SR_Bup_LF"]->Fill(mStop,mLSP,SR,weight*BSFLup);
	histos["SR_Bdown_HF"]->Fill(mStop,mLSP,SR,weight*BSFHdown);
	histos["SR_Bdown_LF"]->Fill(mStop,mLSP,SR,weight*BSFLdown);
	histos["SR_LepEffup"]->Fill(mStop,mLSP,SR,weight*lEffup);
	histos["SR_LepEffdown"]->Fill(mStop,mLSP,SR,weight*lEffdown);
	histos["SR_LepEffFSup"]->Fill(mStop,mLSP,SR,weight*lEffFSup);
	histos["SR_LepEffFSdown"]->Fill(mStop,mLSP,SR,weight*lEffFSdown);
	histos["SR_muRFup"]->Fill(mStop,mLSP,SR,weight*muRFup);
	histos["SR_muRFdown"]->Fill(mStop,mLSP,SR,weight*muRFdown);
      }
      if(compressedSR>0){
	if(compressedSR<=6) cout << __LINE__ << " " << compressedSR << endl;
	//compressedSR is defined to not overlap with SR - can use same histogram!
	histos["SRyield"]->Fill(mStop,mLSP,compressedSR,weight);
	histos["SR_ISRup"]->Fill(mStop,mLSP,compressedSR,weight*ISRup);
	histos["SR_ISRdown"]->Fill(mStop,mLSP,compressedSR,weight*ISRdown);
	histos["SR_Xsecup"]->Fill(mStop,mLSP,compressedSR,weight*XSup);
	histos["SR_Xsecdown"]->Fill(mStop,mLSP,compressedSR,weight*XSdown);
	histos["SR_PUup"]->Fill(mStop,mLSP,compressedSR,weight*PUup);
	histos["SR_PUdown"]->Fill(mStop,mLSP,compressedSR,weight*PUdown);
	histos["SR_Bup_HF"]->Fill(mStop,mLSP,compressedSR,weight*BSFHup);
	histos["SR_Bup_LF"]->Fill(mStop,mLSP,compressedSR,weight*BSFLup);
	histos["SR_Bdown_HF"]->Fill(mStop,mLSP,compressedSR,weight*BSFHdown);
	histos["SR_Bdown_LF"]->Fill(mStop,mLSP,compressedSR,weight*BSFLdown);
	histos["SR_LepEffup"]->Fill(mStop,mLSP,compressedSR,weight*lEffup);
	histos["SR_LepEffdown"]->Fill(mStop,mLSP,compressedSR,weight*lEffdown);
	histos["SR_LepEffFSup"]->Fill(mStop,mLSP,compressedSR,weight*lEffFSup);
	histos["SR_LepEffFSdown"]->Fill(mStop,mLSP,compressedSR,weight*lEffFSdown);
	histos["SR_muRFup"]->Fill(mStop,mLSP,compressedSR,weight*muRFup);
	histos["SR_muRFdown"]->Fill(mStop,mLSP,compressedSR,weight*muRFdown);
      }

    }//event loop
  
    // Clean Up
    delete tree;
    file->Close();
    delete file;
  }//file loop
  if ( nEventsChain != nEventsTotal ) {
    cout << Form( "ERROR: number of events from files (%d) is not equal to total number of events (%d)", nEventsChain, nEventsTotal ) << endl;
  }
  
  // Example Histograms
  // samplehisto->Draw();

  /*
  for(map<string,TH3D*>::iterator h=histos.begin(); h!=histos.end();++h){
    //add overflow
    //h->second->SetBinContent(h->second->GetNbinsX(), h->second->GetBinContent(h->second->GetNbinsX() )+ h->second->GetBinContent(h->second->GetNbinsX()+1) );
    //h->second->SetBinError(h->second->GetNbinsX(), sqrt(pow(h->second->GetBinError(h->second->GetNbinsX() ),2)+pow(h->second->GetBinError(h->second->GetNbinsX()+1),2) ) );
    //add underfloe
    //h->second->SetBinContent(1, h->second->GetBinContent(1)+ h->second->GetBinContent(0) );
    //h->second->SetBinError(1, sqrt(pow(h->second->GetBinError(1),2)+pow(h->second->GetBinError(0),2) ) );
  }
  */
  string filename = "rootfiles/signalyields/Histos_"+skimFilePrefix+".root";
  TFile *f = new TFile(filename.c_str(),"RECREATE");
  f->cd();
  for(map<string,TH3D*>::iterator h=    histos.begin(); h!=    histos.end();++h) h->second->Write();
  f->Close();
  cout << "Saved histos in " << f->GetName() << endl;

  f_el_SF->Close();
  f_mu_SF_id->Close();
  f_mu_SF_iso->Close();
  f_mu_SF_veto_id->Close();
  f_mu_SF_veto_iso->Close();
  f_vetoLep_eff->Close();
  f_el_FS_ID->Close();
  f_el_FS_Iso->Close();
  f_mu_FS_ID->Close();
  f_mu_FS_Iso->Close();
  // return
  bmark->Stop("benchmark");
  cout << endl;
  cout << nEventsTotal << " Events Processed" << endl;
  cout << "------------------------------" << endl;
  cout << "CPU  Time:	" << Form( "%.01f", bmark->GetCpuTime("benchmark")  ) << endl;
  cout << "Real Time:	" << Form( "%.01f", bmark->GetRealTime("benchmark") ) << endl;
  cout << endl;
  delete bmark;
  delete fPU;//delete PU file
  delete f_el_SF;
  delete f_mu_SF_id;
  delete f_mu_SF_iso;
  delete f_mu_SF_veto_id;
  delete f_mu_SF_veto_iso;
  delete f_vetoLep_eff;
  delete f_el_FS_ID;
  delete f_el_FS_Iso;
  delete f_mu_FS_ID;
  delete f_mu_FS_Iso;
  return 0;
}
コード例 #4
0
ファイル: function_fit.C プロジェクト: Jelov/BtoD
void function_fit(){

	gStyle->SetTextSize(0.05);
	gStyle->SetTextFont(42);
	gStyle->SetPadRightMargin(0.04);
	gStyle->SetPadLeftMargin(0.14);
	gStyle->SetPadTopMargin(0.1);
	gStyle->SetPadBottomMargin(0.14);
	gStyle->SetTitleX(.0f);
	gStyle->SetOptFit(1111);
	gStyle->SetOptStat(0);
	gStyle->SetOptTitle(0);

	TFile* fPbPb = new TFile("bFeedDownPbPb.hist.root");
	TFile* fPbPbMB = new TFile("bFeedDownPbPbMB.hist.root");
//	TFile* fPbPbMC = new TFile("bFeedDownPbPbMC_funfit.hist.root");
//	TFile* fPbPbMBMC = new TFile("bFeedDownPbPbMBMC_funfit.hist.root");

	  TFile* fPbPbMC = new TFile("bFeedDownPbPbMC.hist.root");
	  TFile* fPbPbMBMC = new TFile("bFeedDownPbPbMBMC.hist.root");

	TH3D* hDataPbPb = (TH3D*)fPbPb->Get("hData");
	TH3D* hSidebandPbPb = (TH3D*)fPbPb->Get("hSideband");
	TH3D* hDataPbPbMB = (TH3D*)fPbPbMB->Get("hData");
	TH3D* hSidebandPbPbMB = (TH3D*)fPbPbMB->Get("hSideband");
	TH3D* hPtMD0DcaPbPb = (TH3D*)fPbPb->Get("hPtMD0Dca");
	TH3D* hPtMD0DcaPbPbMB = (TH3D*)fPbPbMB->Get("hPtMD0Dca");

	TH3D* hMCPSignalPbPb = (TH3D*)fPbPbMC->Get("hMCPSignal");
	TH3D* hMCNPSignalPbPb = (TH3D*)fPbPbMC->Get("hMCNPSignal");
	TH3D* hMCPSignalPbPbMB = (TH3D*)fPbPbMBMC->Get("hMCPSignal");
	TH3D* hMCNPSignalPbPbMB = (TH3D*)fPbPbMBMC->Get("hMCNPSignal");
	TH3D* hPtMD0DcaMCPSignalPbPb = (TH3D*)fPbPbMC->Get("hPtMD0DcaMCPSignal");
	TH3D* hPtMD0DcaMCPSwappedPbPb = (TH3D*)fPbPbMC->Get("hPtMD0DcaMCPSwapped");
	TH3D* hPtMD0DcaMCPSignalPbPbMB =(TH3D*)fPbPbMBMC->Get("hPtMD0DcaMCPSignal");
	TH3D* hPtMD0DcaMCPSwappedPbPbMB = (TH3D*)fPbPbMBMC->Get("hPtMD0DcaMCPSwapped");

	TH3D* hData = (TH3D*)hDataPbPb->Clone("hData");
	hData->Sumw2();
	hData->Add(hDataPbPbMB);

	TH3D* hSideband = (TH3D*)hSidebandPbPb->Clone("hSideband");
	hSideband->Sumw2();
	hSideband->Add(hSidebandPbPbMB);

	TH3D* hPtMD0Dca = (TH3D*)hPtMD0DcaPbPb->Clone("hPtMD0Dca");
	hPtMD0Dca->Sumw2();
	hPtMD0Dca->Add(hPtMD0DcaPbPbMB);

	TH3D* hMCPSignal = (TH3D*)hMCPSignalPbPb->Clone("hMCPSignal");
	hMCPSignal->Sumw2();
	hMCPSignal->Add(hMCPSignalPbPbMB);

	TH3D* hMCNPSignal = (TH3D*)hMCNPSignalPbPb->Clone("hMCNPSignal");
	hMCNPSignal->Sumw2();
	hMCNPSignal->Add(hMCNPSignalPbPbMB);

	TH3D* hPtMD0DcaMCPSignal = (TH3D*)hPtMD0DcaMCPSignalPbPb->Clone("hPtMD0DcaMCPSignal");
	hPtMD0DcaMCPSignal->Sumw2();
	hPtMD0DcaMCPSignal->Add(hPtMD0DcaMCPSignalPbPbMB);

	TH3D* hPtMD0DcaMCPSwapped =(TH3D*)hPtMD0DcaMCPSwappedPbPb->Clone("hPtMD0DcaMCPSwapped");
	hPtMD0DcaMCPSwapped->Sumw2();
	hPtMD0DcaMCPSwapped->Add(hPtMD0DcaMCPSwappedPbPbMB);


//	TH1D *h_DcaData = (TH1D*)fDcaData->Get("D0DcaDatafitOut_pt5");

//	const int nPtBins = 14;
//	float ptBins[nPtBins+1] = {2.,3.,4.,5.,6.,8.,10.,12.5,15.0,20.,25.,30.,40.,60.,100};

  const int nPtBins = 9;
  float ptBins[nPtBins+1] = {2.,4.,6.,8.,10.,12.5,20.,40.,60.,100};

  const int nBinY = 20;
  const Double_t binsY[nBinY+1] = {-0.0734,-0.0562,-0.0428,-0.0320,-0.0236,-0.0170,-0.0118,-0.0078,-0.0046,-0.002,0.0,0.002,0.0046,0.0078,0.0118,0.0170,0.0236,0.0320,0.0428,0.0562,0.0734};

  TFile *fDcaData = new TFile("bFeedDownResult.root");
  TH1D *h_DcaData[nPtBins];
//  TH1D *h_DcaData = (TH1D*)fDcaData->Get("D0DcaDatafitOut_pt5");


	RooBinning bin_dcaxy(nBinY,binsY);

	//	TH1D* h_MCP_DCAxy = new TH1D("h_MCP_DCAxy", "h_MCP_DCAxy", nBinY, binsY);
	//  TH1D* h_MCNP_DCAxy = new TH1D("h_MCNP_DCAxy", "h_MCNP_DCAxy", nBinY, binsY);

	TFile *fout= new TFile("function_fit_result.root","RECREATE");
	TH1D *h_PromptYield_fix = new TH1D("h_PromptYield_fix","h_PromptYield_fix",nPtBins,ptBins);
  TH1D *h_NonPromptYield_fix = new TH1D("h_NonPromptYield_fix","h_NonPromptYield_fix",nPtBins,ptBins);
	TH1D *h_NonPromptFraction_fix= new TH1D("h_NonPromptFraction_fix","h_NonPromptFraction_fix",nPtBins,ptBins);

  TH1D *h_PromptYield_float = new TH1D("h_PromptYield_float","h_PromptYield_float",nPtBins,ptBins);
  TH1D *h_NonPromptYield_float = new TH1D("h_NonPromptYield_float","h_NonPromptYield_float",nPtBins,ptBins);
  TH1D *h_NonPromptFraction_float= new TH1D("h_NonPromptFraction_float","h_NonPromptFraction_float",nPtBins,ptBins);

	


	hMCPSignal->GetXaxis()->SetRangeUser(8,10);
	hMCNPSignal->GetXaxis()->SetRangeUser(8,10);

	TH1D* h_MCP_DCAxy= (TH1D*)hMCPSignal->Project3D("y")->Clone("h_MCP_DCAxy");
	TH1D* h_MCNP_DCAxy= (TH1D*)hMCNPSignal->Project3D("y")->Clone("h_MCNP_DCAxy");


	TH1D* h_MCP_DCAxy_ptArr[nPtBins];
	TH1D* h_MCNP_DCAxy_ptArr[nPtBins];

	TCanvas *c_MCP_ptArr[nPtBins];
	TCanvas *c_MCNP_ptArr[nPtBins];

	double max,min;
	double MCPsigmaVal;

	TCanvas *c_MCP_gauss = new TCanvas("c_MCP_gauss","c_MCP_gauss",1000,600);
	c_MCP_gauss->Divide(5,3);

	TCanvas *c_MCP_gauss2 = new TCanvas("c_MCP_gauss2","c_MCP_gauss2",1000,600);
	c_MCP_gauss2->Divide(5,3);

	TCanvas *c_MCNP_gauss = new TCanvas("c_MCNP_gauss","c_MCNP_gauss",1000,600);
	c_MCNP_gauss->Divide(5,3);

	TCanvas *c_data[nPtBins];// = new TCanvas("c_data","c_data",600,600);


		for(int iPtBins=0; iPtBins<nPtBins; iPtBins++)
//	for(int iPtBins=1; iPtBins<2; iPtBins++)
	{
		// Fill the TH1D

		Float_t ptLow=ptBins[iPtBins];
		Float_t ptHigh=ptBins[iPtBins+1];

		hMCPSignal->GetXaxis()->SetRangeUser(ptLow,ptHigh);
		hMCNPSignal->GetXaxis()->SetRangeUser(ptLow,ptHigh);

		h_MCP_DCAxy_ptArr[iPtBins]= (TH1D*)hMCPSignal->Project3D("y")->Clone(Form("h_MCP_DCAxy_Dpt%.1fto%.1f",ptLow,ptHigh));
		h_MCNP_DCAxy_ptArr[iPtBins]= (TH1D*)hMCNPSignal->Project3D("y")->Clone(Form("h_MCNP_DCAxy_Dpt%.1fto%.1f",ptLow,ptHigh));


		// MCP fit and plot

		c_MCP_ptArr[iPtBins] = new TCanvas(Form("c_MCP_%i",iPtBins),Form("c_MCP_%i",iPtBins), 800,400 ); 	
		c_MCP_ptArr[iPtBins]->Divide(2,1);
		c_MCP_ptArr[iPtBins]->cd(1);		


		RooRealVar x("x","Dca_xy",-0.07,0.07);
		//  RooRealVar mean("mean","mean",0,-0.02,0.02);
		RooRealVar mean("mean","mean",0);

		RooRealVar sigma("sigma","sigma",0.003,0.0000001,0.02);
		RooGaussian MCP_pdf("gauss","gaussian PDF",x,mean,sigma);
		RooPlot* MCP_frame = x.frame();

		RooDataHist h_MCP("h_MCP","h_MCP",RooArgList(x),h_MCP_DCAxy_ptArr[iPtBins]);

		MCP_pdf.fitTo(h_MCP);
		MCPsigmaVal=sigma.getVal();

		h_MCP.plotOn(MCP_frame,Binning(bin_dcaxy));


		MCP_pdf.plotOn(MCP_frame,LineColor(2));
		MCP_pdf.paramOn(MCP_frame);
		//	MCP_frame->SetTitle("test Title");
		max = MCP_frame->GetMaximum();
		min = MCP_frame->GetMinimum();
		MCP_frame->SetMaximum(max+0.2*(max-min));
		MCP_frame->Draw();

		TLatex *tex_MCP= new TLatex(0.18,0.82,Form("Prompt D"));
		tex_MCP->SetNDC();
		tex_MCP->SetTextFont(42);
		tex_MCP->SetTextSize(0.035);
		tex_MCP->SetLineWidth(2);
		tex_MCP->Draw();
		TLatex *tex_MCPGauss= new TLatex(0.18,0.75,"Gaussian Fit");
		tex_MCPGauss->SetNDC();
		tex_MCPGauss->SetTextFont(42);
		tex_MCPGauss->SetTextSize(0.035);
		tex_MCPGauss->SetLineWidth(2);
		tex_MCPGauss->Draw();


		c_MCP_gauss->cd(iPtBins+1);
		MCP_frame->Draw();
		if(iPtBins==0){
			TLatex *tex_MCP_Gauss = new TLatex(0.18,0.75,Form("Prompt D, Gauss"));
			tex_MCP_Gauss->SetNDC();
			tex_MCP_Gauss->SetTextFont(42);
			tex_MCP_Gauss->SetTextSize(0.035);
			tex_MCP_Gauss->SetLineWidth(2);
			tex_MCP_Gauss->Draw();
		}
		TLatex *tex_MCP_Gausspt = new TLatex(0.18,0.82,Form("%.1f<pt<%.1f",ptLow,ptHigh));
		tex_MCP_Gausspt->SetNDC();
		tex_MCP_Gausspt->SetTextFont(42);
		tex_MCP_Gausspt->SetTextSize(0.035);
		tex_MCP_Gausspt->SetLineWidth(2);
		tex_MCP_Gausspt->Draw();



/*
		c_MCP_ptArr[iPtBins]->cd(2);

		RooRealVar ARatio("ARatio","ARatio",0.5,0,1);
		RooRealVar sigmaRatio("sigmaRatio","sigmaRatio",1.24,1.000001,100000);
		RooGenericPdf dgauss("dgauss","Double Gaussian","ARatio*exp(-pow((x-mean)/sigma,2)/2)+(1-ARatio)*exp(-pow((x-mean)/(sigma*sigmaRatio),2)/2)",RooArgSet(x,mean,sigma,ARatio,sigmaRatio));

		dgauss.fitTo(h_MCP);
		RooPlot* MCP_frame2 = x.frame();
		h_MCP.plotOn(MCP_frame2);
		dgauss.plotOn(MCP_frame2);
		dgauss.paramOn(MCP_frame2);
		max = MCP_frame2->GetMaximum();
		min = MCP_frame2->GetMinimum();
		MCP_frame2->SetMaximum(max+0.2*(max-min));

		MCP_frame2->Draw();

		TLatex *tex_MCPpt= new TLatex(0.18,0.82,Form("%.1f<D P_{T}<%.1f",ptLow,ptHigh));
		tex_MCPpt->SetNDC();
		tex_MCPpt->SetTextFont(42);
		tex_MCPpt->SetTextSize(0.035);
		tex_MCPpt->SetLineWidth(2);
		tex_MCPpt->Draw();
		TLatex *tex_MCPGauss2= new TLatex(0.18,0.75,"Double Gaussian Fit");
		tex_MCPGauss2->SetNDC();
		tex_MCPGauss2->SetTextFont(42);
		tex_MCPGauss2->SetTextSize(0.035);
		tex_MCPGauss2->SetLineWidth(2);
		tex_MCPGauss2->Draw();

		c_MCP_ptArr[iPtBins]->SaveAs(Form("plots_functionfit/MCP_FunCom_pt%dto%d.pdf",(int)ptLow,(int)ptHigh));

		c_MCP_gauss2->cd(iPtBins+1);
		MCP_frame2->Draw();
		if(iPtBins==0){
			TLatex *tex_MCP_Gauss2 = new TLatex(0.18,0.75,Form("Prompt D,Double Gauss"));
			tex_MCP_Gauss2->SetNDC();
			tex_MCP_Gauss2->SetTextFont(42);
			tex_MCP_Gauss2->SetTextSize(0.035);
			tex_MCP_Gauss2->SetLineWidth(2);
			tex_MCP_Gauss2->Draw();
		}
		TLatex *tex_MCP_Gauss2pt = new TLatex(0.18,0.82,Form("%.1f<pt<%.1f",ptLow,ptHigh));
		tex_MCP_Gauss2pt->SetNDC();
		tex_MCP_Gauss2pt->SetTextFont(42);
		tex_MCP_Gauss2pt->SetTextSize(0.035);
		tex_MCP_Gauss2pt->SetLineWidth(2);
		tex_MCP_Gauss2pt->Draw();
*/


		/////////////////////
		// MCNP fit and plot 
		/////////////////////

		c_MCNP_ptArr[iPtBins] = new TCanvas(Form("c_MCNP_%i",iPtBins),Form("c_MCNP_%i",iPtBins), 800,800 ); 
		c_MCNP_ptArr[iPtBins]->Divide(2,2);
		c_MCNP_ptArr[iPtBins]->cd(1);


    gPad->SetLogy();

		//  RooRealVar dca("dca","dca_xy",-0.05,0.05);
		RooRealVar alpha("alpha","alpha",-100,-20000,-0.00001);
		RooGenericPdf gp("gp","Generic PDF","exp(abs(x)*alpha)",RooArgSet(x,alpha));

		RooRealVar mg("mg","mg",0);
		RooRealVar sg("sg","sg",MCPsigmaVal,0.0000001,0.1);
		RooGaussian gauss2("gauss2","gauss2",x,mg,sg);
		x.setBins(10000,"cache");
		//  RooFFTConvPdf gpxg("gpxg","exp (x) gauss",x,gp,gauss2);
		RooFFTConvPdf *MCNP_pdf = new RooFFTConvPdf("MCNP_pdf","exp (x) gauss",x,gp,gauss2);

//		sg.setConstant(kTRUE);

		RooPlot * MCNP_frame = x.frame();
		//  RooDataHist h_MCNP("h_MCNP","h_MCNP",RooArgSet(dca),h_MCNP_DCAxy);
		RooDataHist h_MCNP("h_MCNP","h_MCNP",RooArgSet(x),h_MCNP_DCAxy_ptArr[iPtBins]);
		RooFitResult * fitres = MCNP_pdf->fitTo(h_MCNP);
		h_MCNP.plotOn(MCNP_frame,Binning(bin_dcaxy));
		MCNP_pdf->plotOn(MCNP_frame);
		MCNP_pdf->paramOn(MCNP_frame);
		//  MCNP_frame2->GetXaxis()->SetTitle("set x title");
		max = MCNP_frame->GetMaximum();
		min = MCNP_frame->GetMinimum();
//		MCNP_frame->SetMaximum(max+0.2*(max-min));
		MCNP_frame->SetMaximum(max*10);
		MCNP_frame->SetMinimum(0.5);
		MCNP_frame->Draw();
		cout<<"sg = "<<sg.getVal();

		TLatex *tex_MCNP= new TLatex(0.18,0.82,Form("Non Prompt D"));
		tex_MCNP->SetNDC();
		tex_MCNP->SetTextFont(42);
		tex_MCNP->SetTextSize(0.035);
		tex_MCNP->SetLineWidth(2);
		tex_MCNP->Draw();
		TLatex *tex_MCNPGauss= new TLatex(0.18,0.75,"Exp (x) Gaussian");
		tex_MCNPGauss->SetNDC();
		tex_MCNPGauss->SetTextFont(42);
		tex_MCNPGauss->SetTextSize(0.035);
		tex_MCNPGauss->SetLineWidth(2);
		tex_MCNPGauss->Draw();

    TLatex *tex_MCNP_Gausspt = new TLatex(0.18,0.68,Form("%.1f<pt<%.1f",ptLow,ptHigh));
    tex_MCNP_Gausspt->SetNDC();
    tex_MCNP_Gausspt->SetTextFont(42);
    tex_MCNP_Gausspt->SetTextSize(0.035);
    tex_MCNP_Gausspt->SetLineWidth(2);
    tex_MCNP_Gausspt->Draw();



		// test other function
		c_MCNP_ptArr[iPtBins]->cd(2);
    gPad->SetLogy();
	
		RooRealVar power_a("power_a","power_a",10,0.000001,100000);
		RooRealVar power_n("power_n","power_n",3,0.001,100000);	
		RooGenericPdf power_pdf("power_pdf","power_odf","1/pow((1+power_a*abs(x)),power_n)",RooArgSet(x,power_a,power_n)); 
	
		RooPlot * MCNP_frame2 = x.frame();
		power_pdf.fitTo(h_MCNP);
		h_MCNP.plotOn(MCNP_frame2,Binning(bin_dcaxy));
		power_pdf.plotOn(MCNP_frame2);
		power_pdf.paramOn(MCNP_frame2);
		MCNP_frame2->SetMaximum(max*10);	
		MCNP_frame2->SetMinimum(0.5);
		MCNP_frame2->Draw();

    TLatex *tex_MCNPpower= new TLatex(0.18,0.75,"1/(1+a*x)^{n}");
    tex_MCNPpower->SetNDC();
    tex_MCNPpower->SetTextFont(42);
    tex_MCNPpower->SetTextSize(0.035);
    tex_MCNPpower->SetLineWidth(2);
    tex_MCNPpower->Draw();

	

		c_MCNP_ptArr[iPtBins]->cd(3);
		    gPad->SetLogy();
		RooRealVar twoExpA("twoExpA","twoExpA",-100,-100000,-10);
		RooRealVar twoExpB("twoExpB","twoExpB",-10,-300,-0.01);
		RooRealVar twoExpAfrac("twoExpAfrac","twoExpAfrac",0.5,0.0,1.0);
		RooGenericPdf twoExp_pdf("twoExp_pdf","twoExp_pdf","twoExpAfrac*exp(twoExpA*abs(x))+(1-twoExpAfrac)*exp(twoExpB*abs(x))",RooArgSet(x,twoExpA,twoExpB,twoExpAfrac));

		RooPlot *MCNP_frame3 = x.frame();
		twoExp_pdf.fitTo(h_MCNP);
		h_MCNP.plotOn(MCNP_frame3,Binning(bin_dcaxy));
		twoExp_pdf.plotOn(MCNP_frame3);
		twoExp_pdf.paramOn(MCNP_frame3);
		MCNP_frame3->SetMaximum(max*10);
		MCNP_frame3->SetMinimum(0.5);
		MCNP_frame3->Draw();

    TLatex *tex_MCNPtwoExp= new TLatex(0.18,0.75,"A*exp1+(1-A)exp2 ");
    tex_MCNPtwoExp->SetNDC();
    tex_MCNPtwoExp->SetTextFont(42);
    tex_MCNPtwoExp->SetTextSize(0.035);
    tex_MCNPtwoExp->SetLineWidth(2);
    tex_MCNPtwoExp->Draw();


/*
		c_MCNP_ptArr[iPtBins]->cd(4);
		gPad->SetLogy();

		RooRealVar doubleExpA("doubleExpA","doubleExpA",-100,-10000,-0.00001);
		RooRealVar doubleExpB("doubleExpB","doubleExpB",-1,-5000,-0.000001);
		RooGenericPdf doubleExp_pdf("doubleExp_pdf","doubleExp_pdf","exp(doubleExpA*exp(doubleExpB*abs(x)))",RooArgSet(x,doubleExpA,doubleExpB));

    RooPlot *MCNP_frame4 = x.frame();
    doubleExp_pdf.fitTo(h_MCNP);
    h_MCNP.plotOn(MCNP_frame4,Binning(bin_dcaxy));
    doubleExp_pdf.plotOn(MCNP_frame4);
    doubleExp_pdf.paramOn(MCNP_frame4);
    MCNP_frame4->SetMaximum(max*10);
    MCNP_frame4->SetMinimum(0.5);
    MCNP_frame4->Draw();

*/
    c_MCNP_ptArr[iPtBins]->SaveAs(Form("plots_functionfit/MCNP_FunCom_pt%dto%d.pdf",(int)ptLow,(int)ptHigh));
		

		//	fitres->Print(); not work for unknown reason.

/*
		c_MCNP_ptArr[iPtBins]->cd(3);
		RooPlot *MCNP_frame3= x.frame();
		sg.setConstant(kFALSE);
		gpxg->fitTo(h_MCNP);
		h_MCNP.plotOn(MCNP_frame3);
		gpxg->plotOn(MCNP_frame3);
		gpxg->paramOn(MCNP_frame3);
		MCNP_frame3->SetMaximum(max+0.2*(max-min));
		MCNP_frame3->Draw();

		TLatex *tex_MCNPGaussF= new TLatex(0.18,0.75,"Exp.(x)Gaus.");
		tex_MCNPGaussF->SetNDC();
		tex_MCNPGaussF->SetTextFont(42);
		tex_MCNPGaussF->SetTextSize(0.035);
		tex_MCNPGaussF->SetLineWidth(2);
		tex_MCNPGaussF->Draw();


		c_MCNP_gauss->cd(iPtBins+1);
		MCNP_frame3->Draw();
		if(iPtBins==0){
			TLatex *tex_MCNP_Gauss = new TLatex(0.18,0.75,Form("Non Prompt D, Gauss"));
			tex_MCNP_Gauss->SetNDC();
			tex_MCNP_Gauss->SetTextFont(42);
			tex_MCNP_Gauss->SetTextSize(0.035);
			tex_MCNP_Gauss->SetLineWidth(2);
			tex_MCNP_Gauss->Draw();
		}
		TLatex *tex_MCNP_Gausspt = new TLatex(0.18,0.82,Form("%.1f<pt<%.1f",ptLow,ptHigh));
		tex_MCNP_Gausspt->SetNDC();
		tex_MCNP_Gausspt->SetTextFont(42);
		tex_MCNP_Gausspt->SetTextSize(0.035);
		tex_MCNP_Gausspt->SetLineWidth(2);
		tex_MCNP_Gausspt->Draw();



		c_MCNP_ptArr[iPtBins]->cd(2);

		//  RooFFTConvPdf gpxg2("gpxg2","exp (x) gauss2",x,gp,dgauss); dgauss can not perform FFT
		RooNumConvPdf gpxg2("gpxg2","exp (x) gauss2",x,gp,dgauss);

		sigma.setConstant(kTRUE);
		ARatio.setConstant(kTRUE);
		sigmaRatio.setConstant(kTRUE);


		RooPlot *MCNP_frame2= x.frame();
		//	sg.setRange(0.0000001,0.1);
		gpxg2.fitTo(h_MCNP);
		h_MCNP.plotOn(MCNP_frame2);
		gpxg2.plotOn(MCNP_frame2);
		gpxg2.paramOn(MCNP_frame2);

		MCNP_frame2->SetMaximum(max+0.2*(max-min));
		MCNP_frame2->Draw();

		TLatex *tex_MCNPpt= new TLatex(0.18,0.82,Form("%.1f<D P_{T}<%.1f",ptLow,ptHigh));
		tex_MCNPpt->SetNDC();
		tex_MCNPpt->SetTextFont(42);
		tex_MCNPpt->SetTextSize(0.035);
		tex_MCNPpt->SetLineWidth(2);
		tex_MCNPpt->Draw();
		TLatex *tex_MCNPGauss2= new TLatex(0.18,0.75,"Exp (x) Double Gass.");
		tex_MCNPGauss2->SetNDC();
		tex_MCNPGauss2->SetTextFont(42);
		tex_MCNPGauss2->SetTextSize(0.035);
		tex_MCNPGauss2->SetLineWidth(2);
		tex_MCNPGauss2->Draw();



		// double exponential fit
		c_MCNP_ptArr[iPtBins]->cd(4);

*/
/*
		c_MCNP_ptArr[iPtBins]->cd(4);
		sigma.setConstant(kFALSE);
		ARatio.setConstant(kFALSE);
		sigmaRatio.setConstant(kFALSE);

		RooPlot *MCNP_frame4= x.frame();
		//  sg.setRange(0.0000001,0.1);
		gpxg2.fitTo(h_MCNP);
		gpxg2.fitTo(h_MCNP);
		h_MCNP.plotOn(MCNP_frame4);
		gpxg2.plotOn(MCNP_frame4);
		gpxg2.paramOn(MCNP_frame4);

		MCNP_frame4->SetMaximum(max+0.2*(max-min));
		MCNP_frame4->Draw();

		TLatex *tex_MCNP2GaussF= new TLatex(0.18,0.75,"(x)Double Gauss;float");
		tex_MCNP2GaussF->SetNDC();
		tex_MCNP2GaussF->SetTextFont(42);
		tex_MCNP2GaussF->SetTextSize(0.035);
		tex_MCNP2GaussF->SetLineWidth(2);
		tex_MCNP2GaussF->Draw();

*/

//		c_MCNP_ptArr[iPtBins]->SaveAs(Form("plots_functionfit/MCNP_FunCom_pt%dto%d.pdf",(int)ptLow,(int)ptHigh));


		////////////////////
		///// fit to data //
		////////////////////

		c_data[iPtBins] = new TCanvas(Form("c_data_pt%i",iPtBins),Form("c_data_pt%i",iPtBins),1200,800);
		c_data[iPtBins]->Divide(3,2);

		c_data[iPtBins]->cd(1); //plot MC prompt fit
		gPad->SetLogy();
		MCP_frame->SetMaximum(MCP_frame->GetMaximum()*10);
		MCP_frame->SetMinimum(1);
//    gauss.plotOn(MCP_frame,LineColor(2));
    MCP_frame->Draw();
    tex_MCP->Draw();
    tex_MCPGauss->Draw();

    TLatex *tex_Datapt= new TLatex(0.18,0.68,Form("%.1f<D P_{T}<%.1f",ptLow,ptHigh));
    tex_Datapt->SetNDC();
    tex_Datapt->SetTextFont(42);
    tex_Datapt->SetTextSize(0.035);
    tex_Datapt->SetLineWidth(2);
    tex_Datapt->Draw();



		c_data[iPtBins]->cd(4);	//plot MC Non prompt fit
		gPad->SetLogy();

		MCNP_frame2->Draw();
		tex_MCNPpower->Draw();
		tex_MCNP->Draw();
/*
		MCNP_frame->SetMaximum(MCNP_frame->GetMaximum()*10);
		MCNP_frame->SetMinimum(1);
    MCNP_frame->Draw();
    tex_MCNP->Draw();
    TLatex *tex_MCNPGaussF= new TLatex(0.18,0.75,"Exp.(x)Gaus.");
    tex_MCNPGaussF->SetNDC();
    tex_MCNPGaussF->SetTextFont(42);
    tex_MCNPGaussF->SetTextSize(0.035);
    tex_MCNPGaussF->SetLineWidth(2);
    tex_MCNPGaussF->Draw();
*/
		c_data[iPtBins]->cd(2); //plot data fit , parameter fixed

		h_DcaData[iPtBins]=(TH1D*)fDcaData->Get(Form("D0DcaDataOut_pt%i",iPtBins));
/*
		TCanvas *c_dcadata = new TCanvas("c_dcadta");
		c_dcadata->cd();
		h_DcaData->

		c_data[iPtBins]->cd(3);
*/
//		gPad->SetLogy();	// not work .... <THistPainter::PaintInit>: log scale requested with a negative argument
//		fixZeroBin(h_DcaData[iPtBins]);
//		double maxdatah = h_DcaData[iPtBins]->GetMaximum();
//		h_DcaData[iPtBins]->GetYaxis()->SetRangeUser(0.00001,maxdatah);
//		h_DcaData[iPtBins]->SetMinimum(0.001);

		RooDataHist h_Data("h_Data","h_Data",RooArgSet(x),h_DcaData[iPtBins]);

/* fraction fit
		RooRealVar pfrac("pfrac","pfrac",0.5,0,1);
//		RooRealVar npfrac("npfrac","npfrac",0.1);
		RooAddPdf MCDCAmix_pdf("MCDCAmix_pdf","P+NP",RooArgList(gauss,gp),RooArgList(pfrac));
*/

//		RooArgSet * comps=gpxp.getComponents();

		RooRealVar pNum("Prompt","Prompt",20000,0,100000);
		RooRealVar npNum("NonPrompt","NonPrompt",9000,0,100000);

//		RooAddPdf MCDCAmix_pdf("MCDCAmix_pdf","P+NP exML",RooArgList(MCP_pdf,*MCNP_pdf),RooArgList(pNum,npNum));
		RooAddPdf MCDCAmix_pdf("MCDCAmix_pdf","P+NP exML",RooArgList(MCP_pdf,power_pdf),RooArgList(pNum,npNum));

		sigma.setConstant(kTRUE);
		power_a.setConstant(kTRUE);
		power_n.setConstant(kTRUE);

//		alpha.setConstant(kTRUE);
//		sg.setConstant(kTRUE);

//		MCDCAmix_pdf.fitTo(h_Data);
		MCDCAmix_pdf.fitTo(h_Data,Extended(kTRUE));	

		RooPlot* data_frame = x.frame();
		h_Data.plotOn(data_frame,Binning(bin_dcaxy));

		MCDCAmix_pdf.plotOn(data_frame,Components(RooArgSet(MCP_pdf,power_pdf)),LineColor(3));
		MCDCAmix_pdf.plotOn(data_frame,Components(power_pdf),LineStyle(kDashed),LineColor(4));
    MCDCAmix_pdf.plotOn(data_frame,Components(MCP_pdf),LineStyle(kDashed),LineColor(2));
		MCDCAmix_pdf.paramOn(data_frame);

    max = data_frame->GetMaximum();
    min = data_frame->GetMinimum();
    data_frame->SetMaximum(max+0.2*(max-min));
		// data_frame->SetMinimum(0.1); // must after the plotOn
		data_frame->Draw();	

    TLatex *tex_datafix= new TLatex(0.18,0.82,"Data, param. fix");
    tex_datafix->SetNDC();
    tex_datafix->SetTextFont(42);
    tex_datafix->SetTextSize(0.035);
    tex_datafix->SetLineWidth(2);
    tex_datafix->Draw();

		// fill in output
		h_PromptYield_fix->SetBinContent(iPtBins+1,pNum.getVal());
		h_PromptYield_fix->SetBinError(iPtBins+1,pNum.getError());
    h_NonPromptYield_fix->SetBinContent(iPtBins+1,npNum.getVal());
    h_NonPromptYield_fix->SetBinError(iPtBins+1,npNum.getError());

    h_NonPromptFraction_fix->SetBinContent(iPtBins+1,npNum.getVal()/(pNum.getVal()+npNum.getVal()));
    h_NonPromptFraction_fix->SetBinError(iPtBins+1,npNum.getError()/(pNum.getVal()+npNum.getVal()));

	
		c_data[iPtBins]->cd(5);
		gPad->SetLogy();
		RooPlot* data_framelog = x.frame();
    h_Data.plotOn(data_framelog,Binning(bin_dcaxy));
    MCDCAmix_pdf.plotOn(data_framelog,Components(RooArgSet(MCP_pdf,power_pdf)),LineColor(3));
    MCDCAmix_pdf.plotOn(data_framelog,Components(power_pdf),LineStyle(kDashed),LineColor(4));
    MCDCAmix_pdf.plotOn(data_framelog,Components(MCP_pdf),LineStyle(kDashed),LineColor(2));
//    MCDCAmix_pdf.paramOn(data_framelog);
    max = data_framelog->GetMaximum();
    min = data_frame->GetMinimum();
    data_framelog->SetMaximum(10*max);
    data_framelog->SetMinimum(0.5); // must after the plotOn
    data_framelog->Draw();
    tex_datafix->Draw();
			
	  			

		c_data[iPtBins]->cd(3);

		sigma.setConstant(kFALSE);
		power_a.setConstant(kFALSE);
		power_n.setConstant(kFALSE);

		MCDCAmix_pdf.fitTo(h_Data,Extended(kTRUE));		
    RooPlot* data_frame2 = x.frame();
    h_Data.plotOn(data_frame2,Binning(bin_dcaxy));
    MCDCAmix_pdf.plotOn(data_frame2,Components(RooArgSet(MCP_pdf,power_pdf)),LineColor(3));
    MCDCAmix_pdf.plotOn(data_frame2,Components(power_pdf),LineStyle(kDashed),LineColor(4));
    MCDCAmix_pdf.plotOn(data_frame2,Components(MCP_pdf),LineStyle(kDashed),LineColor(2));
    MCDCAmix_pdf.paramOn(data_frame2);

	  max = data_frame2->GetMaximum();
    min = data_frame2->GetMinimum();
    data_frame2->SetMaximum(max+0.2*(max-min));
    data_frame2->Draw();

    TLatex *tex_datafloat= new TLatex(0.18,0.82,"Data, param. float");
    tex_datafloat->SetNDC();
    tex_datafloat->SetTextFont(42);
    tex_datafloat->SetTextSize(0.035);
    tex_datafloat->SetLineWidth(2);
    tex_datafloat->Draw();


    // fill in output
    h_PromptYield_float->SetBinContent(iPtBins+1,pNum.getVal());
    h_PromptYield_float->SetBinError(iPtBins+1,pNum.getError());
    h_NonPromptYield_float->SetBinContent(iPtBins+1,npNum.getVal());
    h_NonPromptYield_float->SetBinError(iPtBins+1,npNum.getError());

    h_NonPromptFraction_float->SetBinContent(iPtBins+1,npNum.getVal()/(pNum.getVal()+npNum.getVal()));
    h_NonPromptFraction_float->SetBinError(iPtBins+1,npNum.getError()/(pNum.getVal()+npNum.getVal()));

		c_data[iPtBins]->cd(6);
		gPad->SetLogy();
		RooPlot *data_frame2log = x.frame();
    h_Data.plotOn(data_frame2log,Binning(bin_dcaxy));
    MCDCAmix_pdf.plotOn(data_frame2log,Components(RooArgSet(MCP_pdf,power_pdf)),LineColor(3));
    MCDCAmix_pdf.plotOn(data_frame2log,Components(power_pdf),LineStyle(kDashed),LineColor(4));
    MCDCAmix_pdf.plotOn(data_frame2log,Components(MCP_pdf),LineStyle(kDashed),LineColor(2));
//    MCDCAmix_pdf.paramOn(data_frame2);
    max = data_frame2log->GetMaximum();
//    min = data_frame2->GetMinimum();
    data_frame2log->SetMaximum(10*max);
		data_frame2log->SetMinimum(0.5);
    data_frame2log->Draw();
    tex_datafloat->Draw();
	



		c_data[iPtBins]->SaveAs(Form("plots_functionfit/fitdata_pt%dto%d.pdf",(int)ptLow,(int)ptHigh));


	} // end for iPtBins
/*
	c_MCP_gauss->SaveAs("plots_functionfit/MCP_Gauss_Ptall.pdf");
	c_MCP_gauss2->SaveAs("plots_functionfit/MCP_Gauss2_Ptall.pdf");
	c_MCNP_gauss->SaveAs("plots_functionfit/MCNP_Gauss_Ptall.pdf");
*/

	divideBinWidth(h_PromptYield_fix);
	divideBinWidth(h_NonPromptYield_fix);
	divideBinWidth(h_PromptYield_float);
	divideBinWidth(h_NonPromptYield_float);

	TCanvas *c_dataPtall = new TCanvas("c_dataPtall","c_dataPtall",1000,600);
	c_dataPtall->Divide(3,2);
	c_dataPtall->cd(1);
	gPad->SetLogy();
	h_PromptYield_fix->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
  h_PromptYield_fix->GetYaxis()->SetTitle("Prompt Yield (fix)");
	h_PromptYield_fix->Draw();
	c_dataPtall->cd(2);
  gPad->SetLogy();
  h_NonPromptYield_fix->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
  h_NonPromptYield_fix->GetYaxis()->SetTitle("NonPrompt Yield (fix)");
	h_NonPromptYield_fix->Draw();
	c_dataPtall->cd(3);
  h_NonPromptFraction_fix->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
  h_NonPromptFraction_fix->GetYaxis()->SetTitle("NonPrompt Fraction (fix)");
	h_NonPromptFraction_fix->Draw();
  c_dataPtall->cd(4);
  gPad->SetLogy();
  h_PromptYield_float->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
  h_PromptYield_float->GetYaxis()->SetTitle("Prompt Yield (float)");
  h_PromptYield_float->Draw();
  c_dataPtall->cd(5);
  gPad->SetLogy();
  h_NonPromptYield_float->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
  h_NonPromptYield_float->GetYaxis()->SetTitle("NonPrompt Yield (float)");
  h_NonPromptYield_float->Draw();
  c_dataPtall->cd(6);
  h_NonPromptFraction_float->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
  h_NonPromptFraction_float->GetYaxis()->SetTitle("NonPrompt Fraction (float)");
//	h_NonPromptFraction_float->SetMaximum(1);;
  h_NonPromptFraction_float->Draw();

	c_dataPtall->SaveAs("plots_functionfit/fitPt_spectrum.pdf");

	
	TH1D *h_PromptYield_tmp = (TH1D*)fDcaData->Get("hPromptDRawYield");
	TH1D *h_NonPromptYield_tmp = (TH1D*)fDcaData->Get("hBtoDRawYield");

	TH1D *h_PromptYield_diff = new TH1D("h_PromptYield_diff","h_PromptYield_diff",nPtBins,ptBins);
  TH1D *h_NonPromptYield_diff = new TH1D("h_PromptYield_diff","h_PromptYield_diff",nPtBins,ptBins);

	TH1D *h_PromptYield_RelErr = new TH1D("h_PromptYield_RelErr","h_PromptYield_RelErr",nPtBins,ptBins);
  TH1D *h_NonPromptYield_RelErr = new TH1D("h_NonPromptYield_RelErr","h_NonPromptYield_RelErr",nPtBins,ptBins);

	for(int iPtBins =0;iPtBins<nPtBins; iPtBins++){
	
		h_PromptYield_diff->SetBinContent(iPtBins+1,h_PromptYield_float->GetBinContent(iPtBins+1)-h_PromptYield_tmp->GetBinContent(iPtBins+1));
    h_NonPromptYield_diff->SetBinContent(iPtBins+1,h_NonPromptYield_float->GetBinContent(iPtBins+1)-h_NonPromptYield_tmp->GetBinContent(iPtBins+1));
	
    h_PromptYield_RelErr->SetBinContent(iPtBins+1,abs((h_PromptYield_float->GetBinContent(iPtBins+1)-h_PromptYield_tmp->GetBinContent(iPtBins+1))/h_PromptYield_tmp->GetBinContent(iPtBins+1)));
    h_NonPromptYield_RelErr->SetBinContent(iPtBins+1,abs((h_NonPromptYield_float->GetBinContent(iPtBins+1)-h_NonPromptYield_tmp->GetBinContent(iPtBins+1))/h_NonPromptYield_tmp->GetBinContent(iPtBins+1)));

	}

	TCanvas *c_yield_sys = new TCanvas("c_yield_sys","c_yield_sys",800,800);
	c_yield_sys->Divide(2,2);

	c_yield_sys->cd(1);
	gPad->SetLogy();
//	h_PromptYield_tmp->SetMarkerStyle(24);
//	h_PromptYield_tmp->SetMarkerColor(4);
//	h_PromptYield_tmp->SetLinceColor(4);
	SetHistStyle(h_PromptYield_tmp,4);
	h_PromptYield_tmp->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
	h_PromptYield_tmp->Draw();
//	h_PromptYield_float->SetMarkerStyle(24);
//  h_PromptYield_float->SetMarkerColor(2);
//	h_PromptYield_float->SetLineColor(2);
	SetHistStyle(h_PromptYield_float,2);
	h_PromptYield_float->Draw("same");

	TLegend *le_PromptYield = new TLegend(0.65,0.65,0.88,0.88);
	le_PromptYield->SetBorderSize(0);
	le_PromptYield->AddEntry((TObject*)0,"Prompt Yield","");
	le_PromptYield->AddEntry(h_PromptYield_tmp,"template fit","l");
  le_PromptYield->AddEntry(h_PromptYield_float,"function fit","l");
	le_PromptYield->Draw();

  c_yield_sys->cd(2);
	gPad->SetLogy();
//  h_NonPromptYield_tmp->SetMarkerStyle(24);
//  h_NonPromptYield_tmp->SetMarkerColor(4);
//  h_NonPromptYield_tmp->SetLinceColor(4);
  SetHistStyle(h_NonPromptYield_tmp,4);
  h_NonPromptYield_tmp->GetXaxis()->SetTitle("D P_{T} (GeV/c)");
  h_NonPromptYield_tmp->Draw();
//  h_NonPromptYield_float->SetMarkerStyle(24);
//  h_NonPromptYield_float->SetMarkerColor(2);
//  h_NonPromptYield_float->SetLineColor(2);
  SetHistStyle(h_NonPromptYield_float,2);
  h_NonPromptYield_float->Draw("same");

  TLegend *le_NonPromptYield = new TLegend(0.65,0.65,0.88,0.88);
  le_NonPromptYield->SetBorderSize(0);
  le_NonPromptYield->AddEntry((TObject*)0,"NonPrompt Yield","");
  le_NonPromptYield->AddEntry(h_NonPromptYield_tmp,"template fit","l");
  le_NonPromptYield->AddEntry(h_NonPromptYield_float,"function fit","l");
  le_NonPromptYield->Draw();

	c_yield_sys->cd(3);
	h_PromptYield_RelErr->Draw();
  TLatex *tex_promptY_RelErr= new TLatex(0.18,0.82,"Prompt Yield rel.Err");
  tex_promptY_RelErr->SetNDC();
  tex_promptY_RelErr->SetTextFont(42);
  tex_promptY_RelErr->SetTextSize(0.035);
  tex_promptY_RelErr->SetLineWidth(2);
  tex_promptY_RelErr->Draw();



	c_yield_sys->cd(4);
//	h_NonPromptYield_RelErr->SetMaximum(1);
	h_NonPromptYield_RelErr->Draw();
  TLatex *tex_NonpromptY_RelErr= new TLatex(0.18,0.82,"NonPrompt Yield rel.Err");
  tex_NonpromptY_RelErr->SetNDC();
  tex_NonpromptY_RelErr->SetTextFont(42);
  tex_NonpromptY_RelErr->SetTextSize(0.035);
  tex_NonpromptY_RelErr->SetLineWidth(2);
  tex_NonpromptY_RelErr->Draw();




	c_yield_sys->SaveAs("plots_functionfit/Yield_sys.pdf");




	fout->cd();
  h_PromptYield_fix->Write();
  h_NonPromptYield_fix->Write();
  h_NonPromptFraction_fix->Write();

	h_PromptYield_float->Write();
	h_NonPromptYield_float->Write();
	h_NonPromptFraction_float->Write();


	h_PromptYield_diff->Write();
	h_NonPromptYield_diff->Write();
	h_PromptYield_RelErr->Write();
	h_NonPromptYield_RelErr->Write();	



	fout->Write();
	fout->Close(); // this will delete all object on plot


	cout<<"end of main"<<endl;
	

	/*


		 TCanvas *c_DCA_xy = new TCanvas("c_DCA_xy","c_DCA_xy",600,600);
		 c_DCA_xy->cd();
		 h_MCP_DCAxy->SetLineColor(1);
		 h_MCP_DCAxy->Draw();
		 h_MCNP_DCAxy->SetLineColor(4);
		 h_MCNP_DCAxy->Draw("SAME");



		 TCanvas *c_MCP = new TCanvas("c_MCP","c_MCP",600,600);
		 c_MCP->cd();

		 TF1 *f_MCP = new TF1("f_MCP",&fun_MCP,-0.05,0.0,5);
	//	f_MCP->SetParameters(h_MCP_DCAxy->Integral(), 0, h_MCP_DCAxy->GetRMS());


	f_MCP->SetParameters(77770, 0.7, 0,0.002726,2);
	f_MCP->FixParameter(2,0);
	f_MCP->SetParLimits(1,0,1);
	f_MCP->SetParLimits(4,1,1000);
	cout<<"integral = "<<h_MCP_DCAxy->Integral()<<" , RMS = "<<h_MCP_DCAxy->GetRMS()<<endl;

	TFitResultPtr fitResult;
	fitResult = h_MCP_DCAxy->Fit("f_MCP","MR","",-0.05,0.05);
	int fitStatus=1;
	fitStatus= fitResult->Status();
	cout<<"fit precision: "<<TFitter::GetPrecision()<<"   status: "<<fitStatus<<endl;

	h_MCP_DCAxy->Draw();
	f_MCP->Draw("flsame");



	TCanvas *c_RoofitMCP = new TCanvas("c_RoofitMCP","c_RoofitMCP",800,800);
	c_RoofitMCP->Divide(2,2);
	c_RoofitMCP->cd(1);

	RooRealVar x("x","dca_xy",-0.05,0.05);
	//	RooRealVar mean("mean","mean",0,-0.02,0.02);
	RooRealVar mean("mean","mean",0);
	RooRealVar sigma("sigma","sigma",0.003,0.00001,0.02);
	RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma);
	RooPlot* MCP_frame = x.frame();

	RooDataHist h_MCP("h_MCP","h_MCP",RooArgList(x),h_MCP_DCAxy);

	gauss.fitTo(h_MCP);
	h_MCP.plotOn(MCP_frame);
	gauss.plotOn(MCP_frame);
	gauss.paramOn(MCP_frame);
	MCP_frame->Draw();

	c_RoofitMCP->cd(2);
	gPad->SetLogy();
	MCP_frame->Draw();


	c_RoofitMCP->cd(3);
	RooRealVar ARatio("ARatio","ARatio",0.5,0,1);
	RooRealVar sigmaRatio("sigmaRatio","sigmaRatio",1.24,1.000001,100000);
	RooGenericPdf dgauss("dgauss","Double Gaussian","ARatio*exp(-pow((x-mean)/sigma,2)/2)+(1-ARatio)*exp(-pow((x-mean)/(sigma*sigmaRatio),2)/2)",RooArgSet(x,mean,sigma,ARatio,sigmaRatio));
	//	RooGenericPdf dgauss("dgauss","Double Gaussian", "exp(-pow((x-mean)/sigma,2)/2)" ,RooArgSet(x,mean,sigma));

	dgauss.fitTo(h_MCP);
	RooPlot* MCP_frame2 = x.frame();
	h_MCP.plotOn(MCP_frame2);
	dgauss.plotOn(MCP_frame2);
	dgauss.paramOn(MCP_frame2);
	MCP_frame2->Draw();

	c_RoofitMCP->cd(4);
	gPad->SetLogy();
	MCP_frame2->Draw();




	TCanvas *c_RoofitMCNP = new TCanvas("c_RoofitMCNP","c_RoofitMCNP",800,800);
	c_RoofitMCNP->Divide(2,2);
	c_RoofitMCNP->cd(1);

	RooRealVar dca("dca","dca_xy",-0.05,0.05);
	RooRealVar alpha("alpha","alpha",-100,-20000,-0.00001);
	RooGenericPdf gp("gp","Generic PDF","exp(abs(dca)*alpha)",RooArgSet(dca,alpha));
	RooPlot * MCNP_frame = dca.frame();

	RooDataHist h_MCNP("h_MCNP","h_MCNP",RooArgSet(dca),h_MCNP_DCAxy);
	gp.fitTo(h_MCNP);
	h_MCNP.plotOn(MCNP_frame);
	gp.plotOn(MCNP_frame);
	MCNP_frame->Draw();

	c_RoofitMCNP->cd(2);
	gPad->SetLogy();
	MCNP_frame->Draw();


	//	TCanvas *c_RoofitMCNP_Conv = new TCanvas("c_RoofitMCNP_Conv","c_RoofitMCNP_Conv",600,600);
	//  RooRealVar dca("dca","dca",-0.05,0.05);
	//  RooRealVar alpha("alpha","alpha",-100,-20000,-0.00001);
	//  RooGenericPdf gp("gp","Generic PDF","exp(abs(dca)*alpha)",RooArgSet(dca,alpha));

	c_RoofitMCNP->cd(3);	
	RooRealVar mg("mg","mg",0);
	RooRealVar sg("sg","sg",0.0001,0.0000001,0.1);
	RooGaussian gauss2("gauss2","gauss2",dca,mg,sg);
	dca.setBins(10000,"cache");
	RooFFTConvPdf gpxg("gpxg","exp (x) gauss",dca,gp,gauss2);


	RooPlot * MCNP_frame2 = dca.frame();
	//  RooDataHist h_MCNP("h_MCNP","h_MCNP",RooArgSet(dca),h_MCNP_DCAxy);
	gpxg.fitTo(h_MCNP);
	h_MCNP.plotOn(MCNP_frame2);
	gpxg.plotOn(MCNP_frame2);
	gpxg.paramOn(MCNP_frame2);
	//	MCNP_frame2->GetXaxis()->SetTitle("set x title");
	MCNP_frame2->Draw();

	c_RoofitMCNP->cd(4);
	gPad->SetLogy();
	MCNP_frame2->Draw();





	TCanvas *c_MCNP = new TCanvas("c_MCNP","c_MCNP",600,600);
	c_MCNP->cd();
	TF1 *f_MCNP = new TF1("f_MCNP",&fun_MCNP,-0.05,0.05,2);
	f_MCNP->SetParameters(1000,-3);

	TFitResultPtr fitResult_MCNP;
	fitResult_MCNP = h_MCNP_DCAxy->Fit("f_MCNP","MR","",-0.05,0.05);
	int fitStatus_MCNP=1;
	fitStatus_MCNP = fitResult_MCNP->Status();
	h_MCNP_DCAxy->Draw();
	f_MCNP->Draw("flsame");

	*/
		////
		cout<<"MCPsigmaVal ="<<MCPsigmaVal<<endl;

}