Example #1
0
  void actionOutcome(pair<State*, Action*> performed, pair<State*, double> outcome) {
    State *startState = performed.first;
    Action *actionPerformed = performed.second;

    State *resultState = outcome.first;
    double reward = outcome.second;

    assert(haveSeenState(startState));

    if (!haveSeenState(resultState)) {
      handleNewState(resultState);
    }

    ActionValue *curValue = findActionValue(startState, actionPerformed);
    assert(curValue != nullptr);

    double newQ = reward + futureDiscount * maxQ(resultState);
    curValue->value += learnRate * (newQ - curValue->value);
  }
Example #2
0
void writeFile(void)
{
	int i, j;
	FILE *fp;
	fp = fopen("output.txt", "w");
	for(i = 1; i <= Grid_Size; i++)
	{
		for(j = 1; j <= Grid_Size; j++)
		{
			if(Grid[i][j] == -1)
				fprintf(fp, "X ");
			else if(Grid[i][j] == 10)
				fprintf(fp, "G ");
			else
			{
				maxQ(i, j);	
				fprintf(fp, "%c ", Max_Direction);
			}
		}
		fprintf(fp, "\n");
	}
	fclose(fp);

}
Example #3
0
  void ActionOutcome(pair<State *, Action *> performed, pair<State *, double> outcome) {
    State *startState = performed.first;
    Action *actionPerformed = performed.second;

    State *resultState = outcome.first;
    double reward = outcome.second;

    if (!haveSeenState(startState)) {
      handleNewState(startState);
    }

    if (!haveSeenState(resultState)) {
      handleNewState(resultState);
    }

    ActionValue *curValue = findActionValue(startState, actionPerformed);
    assert(curValue != nullptr);

    double newQ = reward + futureDiscount * maxQ(resultState);
    // cout << "r: " << reward << " fd: " << futureDiscount << " newQ: " << newQ << endl;
    curValue->value += learnRate * (newQ - curValue->value);
    // cout << "v : " << curValue->value << endl;
    // getchar();
  }
Example #4
0
void calculate(float temperature, int tries)
{
	int i, start_x, start_y = 0, direction = 0, c, j, same;
	float max, botz_prob_max, cur;

	Random_Seed = time(NULL);
	for(i = 0; i < tries; i++)
	{
		/***** find an initial state *****/
		do
		{	
			start_x = eerand() % Extended_Size;
			start_y = eerand() % Extended_Size;
		}
		while(Grid[start_x][start_y]);

		/***** consider an arbitrary action *****/
		while(!(start_x == Goal_X && start_y == Goal_Y))
		{
			direction = 0;
			same = 0;
			botz_prob_max = pow(M_E, Q[0][start_x][start_y]/temperature);
			for(j = 1; j < 4; j++)	
			{
				cur = pow(M_E, Q[j][start_x][start_y]/temperature);
				if(cur > botz_prob_max)
				{
					botz_prob_max = cur;
					direction = j;
				}
				else if(cur == botz_prob_max)
					same++;
			}
			if(same > 0)
				direction = eerand() % 4;

			/*printf("%d %d %d %f\n", start_x, start_y, direction, botz_prob_max);*/

			switch(direction)		
			{
				case 0:
					if(start_y == 1 || Grid[start_x][start_y - 1] == -1)
						c = 1;
					else
						c = start_y - 1;

					max = maxQ(start_x, c);
					Q[0][start_x][start_y] = (1 - Alpha) * Q[0][start_x][start_y] + Alpha * (Grid[start_x][c] + max * Gama);
					start_y = c;
					break;

				case 1:	
					if(start_x == 1 || Grid[start_x - 1][start_y] == -1)
						c = 1;
					else 
						c = start_x - 1;

					max = maxQ(c, start_y);
					Q[1][start_x][start_y] = (1 - Alpha) * Q[1][start_x][start_y] + Alpha * (Grid[c][start_y] + max * Gama);
					start_x = c;
					break;

				case 2:	
					if(start_y == Grid_Size || Grid[start_x][start_y + 1] == -1)
						c = start_y;
					else
						c = start_y + 1;

					max = maxQ(start_x, c);
					Q[2][start_x][start_y] = (1 - Alpha) * Q[2][start_x][start_y] + Alpha * (Grid[start_x][c] + max * Gama);
					start_y = c;
					break;

				case 3:
					if(start_x == Grid_Size || Grid[start_x + 1][start_y] == -1)
						c = start_x;
					else
						c = start_x + 1;

					max = maxQ(c, start_y);
					Q[3][start_x][start_y] = (1 - Alpha) * Q[3][start_x][start_y] + Alpha * (Grid[c][start_y] + max * Gama);
					start_x = c;
					break;
			}
		}
	}
}
Example #5
0
void makeReducedTrees_P(Int_t bin) {

	TString binStr; binStr+=bin;
	Double_t minQ(0.), maxQ(0.);

	switch(bin) {
	case 0:
		minQ = TMath::Sqrt(0.1e6);
		maxQ = TMath::Sqrt(0.98e6);
		break;
	case 1:
		minQ = TMath::Sqrt(1.1e6);
		maxQ = TMath::Sqrt(2.e6);
		break;
	case 2:
		minQ = TMath::Sqrt(2.e6);
		maxQ = TMath::Sqrt(3.e6);
		break;
	case 3:
		minQ = TMath::Sqrt(3.e6);
		maxQ = TMath::Sqrt(4.e6);
		break;
	case 4:
		minQ = TMath::Sqrt(4.e6);
		maxQ = TMath::Sqrt(5.e6);
		break;
	case 5:
		minQ = TMath::Sqrt(5.e6);
		maxQ = TMath::Sqrt(6.e6);
		break;
	case 6:
		minQ = TMath::Sqrt(6.e6);
		maxQ = TMath::Sqrt(7.e6);
		break;
	case 7:
		minQ = TMath::Sqrt(7.e6);
		maxQ = TMath::Sqrt(8.e6);
		break;
	case 8:
		minQ = TMath::Sqrt(11.e6);
		maxQ = TMath::Sqrt(11.75e6);
		break;
	case 9:
		minQ = TMath::Sqrt(11.75e6);
		maxQ = TMath::Sqrt(12.5e6);
		break;
	case 10:
		minQ = TMath::Sqrt(15.e6);
		maxQ = TMath::Sqrt(16.e6);
		break;
	case 11:
		minQ = TMath::Sqrt(16.e6);
		maxQ = TMath::Sqrt(17.e6);
		break;
	case 12:
		minQ = TMath::Sqrt(17.e6);
		maxQ = TMath::Sqrt(18.e6);
		break;
	case 13:
		minQ = TMath::Sqrt(18.e6);
		maxQ = TMath::Sqrt(19.e6);
		break;
	case 14:
		minQ = TMath::Sqrt(19.e6);
		maxQ = TMath::Sqrt(20.e6);
		break;
	case 15:
		minQ = TMath::Sqrt(20.e6);
		maxQ = TMath::Sqrt(21.e6);
		break;
	case 16:
		minQ = TMath::Sqrt(21.e6);
		maxQ = TMath::Sqrt(22.e6);
		break;
	case 17:
		minQ = TMath::Sqrt(1.1e6);
		maxQ = TMath::Sqrt(6.e6);
		break;
	case 18:
		minQ = TMath::Sqrt(15.e6);
		maxQ = TMath::Sqrt(22.e6);
		break;
	case 19:
		minQ = TMath::Sqrt( 8.e6);
		maxQ = TMath::Sqrt(11.e6);
		break;
	case 20:
		minQ = TMath::Sqrt(12.5e6);
		maxQ = TMath::Sqrt(15.e6);
		break;
	default:
		return;
	}

	TFile * f = TFile::Open("/Home/dcraik/lhcb/rd/Kll/tuples/fromPatrick/Kmm.root");
	TTree * tree = dynamic_cast<TTree*>(f->Get("finalTree_KMuMu"));

	TFile* fs = TFile::Open("fromPatrick/Kmm_Q"+binStr+"_sWeights.root");
	TTree* stree = dynamic_cast<TTree*>(fs->Get("sWeights"));

	TFile* fn = new TFile("fromPatrick/Kmm_Q"+binStr+"_reduced.root","RECREATE");
	TTree* newtree = tree->CloneTree(0);

	TLorentzVector mup_4mom;
	TLorentzVector mum_4mom;
	TLorentzVector Psi_4mom;
	TLorentzVector K_4mom;
	TLorentzVector B_4mom;
	
	TVector3 B_boost;
	TVector3 Psi_boost;
	TVector3 mup_boost;
	TVector3 mum_boost;

	Double_t K_PE(0.),   K_PX(0.),   K_PY(0.),   K_PZ(0.);
	Double_t mup_PE(0.), mup_PX(0.), mup_PY(0.), mup_PZ(0.);
	Double_t mum_PE(0.), mum_PX(0.), mum_PY(0.), mum_PZ(0.);

	Double_t B_M(0.), Psi_M(0.);

	Double_t cosThetaL(0.);

	Double_t sWeight(0.);

	newtree->Branch("cosThetaL"  ,&cosThetaL);
	newtree->Branch("sWeight"     ,&sWeight);

	tree->SetBranchAddress("Bplus_M",  &B_M);
	tree->SetBranchAddress("Jpsi_M",  &Psi_M);

	tree->SetBranchAddress("Kplus_PE",   &K_PE);
	tree->SetBranchAddress("Kplus_PX",   &K_PX);
	tree->SetBranchAddress("Kplus_PY",   &K_PY);
	tree->SetBranchAddress("Kplus_PZ",   &K_PZ);
	tree->SetBranchAddress("muplus_PE",  &mup_PE);
	tree->SetBranchAddress("muplus_PX",  &mup_PX);
	tree->SetBranchAddress("muplus_PY",  &mup_PY);
	tree->SetBranchAddress("muplus_PZ",  &mup_PZ);
	tree->SetBranchAddress("muminus_PE", &mum_PE);
	tree->SetBranchAddress("muminus_PX", &mum_PX);
	tree->SetBranchAddress("muminus_PY", &mum_PY);
	tree->SetBranchAddress("muminus_PZ", &mum_PZ);



	stree->SetBranchAddress("Nsig_sw", &sWeight);

	Int_t i(0), is(0);

	Int_t n = tree->GetEntries();
	Int_t ns = stree->GetEntries();

	while( i<n && is<ns ) {
		if(is % 100 == 0) std::cout << "Entry " << is << " of " << ns << "..." << std::endl;
		do {
			++i;
			if(i==n) {//i cannot iterate past n because is would have already hit ns.
				std::cout << "This was supposed to be impossible. The two trees don't match!" << std::endl;
				std::cout << is << "\t" << ns << std::endl;
				return;
			}
			tree->GetEntry(i);
		} while(B_M<5170 || B_M>5970 || Psi_M<minQ || Psi_M>maxQ);

		++is;
		stree->GetEntry(is);

		//Get 4 momentum of each track
		K_4mom.SetPxPyPzE(   K_PX/1000,   K_PY/1000,   K_PZ/1000,   K_PE/1000);
		mup_4mom.SetPxPyPzE( mup_PX/1000, mup_PY/1000, mup_PZ/1000, mup_PE/1000);
		mum_4mom.SetPxPyPzE( mum_PX/1000, mum_PY/1000, mum_PZ/1000, mum_PE/1000);

		//Make 4 momenta of composites
		Psi_4mom = mup_4mom + mum_4mom;
		B_4mom   = Psi_4mom + K_4mom;

		//boost into B frame
		B_boost = B_4mom.BoostVector();
		Psi_4mom.Boost(-B_boost);
		mup_4mom.Boost(-B_boost);
		
		//Boost into Psi frame
		Psi_boost = Psi_4mom.BoostVector();
		mup_4mom.Boost(-Psi_boost);
		
		//cosThetaL is the angle between the Psi and the mu+ in the Psi rest frame
		mup_boost  = mup_4mom.BoostVector();
		cosThetaL  = Psi_boost.Dot(mup_boost)/(Psi_boost.Mag()*mup_boost.Mag());

		newtree->Fill();
	}
	std::cout << is << "\t" << ns << std::endl;


	newtree->AutoSave();
        fn->Close();
}
Example #6
0
File: fitKee.C Project: dcraik/lhcb
void fitKee(Int_t bin) {
	gSystem->Load("libRooFit");
	gROOT->SetStyle("Plain");
	gStyle->SetOptStat(1111);

	TFile * file = TFile::Open("/Disk/ecdf-nfs-ppe/lhcb/dcraik/BuKee_reduced.root");

	TTree * DecayTree = dynamic_cast<TTree*>(file->Get("DecayTree"));

	TString binStr; binStr+=bin;
	Double_t minQ(0.), maxQ(0.);

	switch(bin) {
	//case 0:
	//	minQ = TMath::Sqrt(0.1e6);
	//	maxQ = TMath::Sqrt(0.98e6);
	//	break;
	//case 1:
	//	minQ = TMath::Sqrt(1.1e6);
	//	maxQ = TMath::Sqrt(2.e6);
	//	break;
	//case 2:
	//	minQ = TMath::Sqrt(2.e6);
	//	maxQ = TMath::Sqrt(3.e6);
	//	break;
	//case 3:
	//	minQ = TMath::Sqrt(3.e6);
	//	maxQ = TMath::Sqrt(4.e6);
	//	break;
	//case 4:
	//	minQ = TMath::Sqrt(4.e6);
	//	maxQ = TMath::Sqrt(5.e6);
	//	break;
	//case 5:
	//	minQ = TMath::Sqrt(5.e6);
	//	maxQ = TMath::Sqrt(6.e6);
	//	break;
	//case 6:
	//	minQ = TMath::Sqrt(6.e6);
	//	maxQ = TMath::Sqrt(7.e6);
	//	break;
	//case 7:
	//	minQ = TMath::Sqrt(7.e6);
	//	maxQ = TMath::Sqrt(8.e6);
	//	break;
	//case 8:
	//	minQ = TMath::Sqrt(11.e6);
	//	maxQ = TMath::Sqrt(11.75e6);
	//	break;
	//case 9:
	//	minQ = TMath::Sqrt(11.75e6);
	//	maxQ = TMath::Sqrt(12.5e6);
	//	break;
	//case 10:
	//	minQ = TMath::Sqrt(15.e6);
	//	maxQ = TMath::Sqrt(16.e6);
	//	break;
	//case 11:
	//	minQ = TMath::Sqrt(16.e6);
	//	maxQ = TMath::Sqrt(17.e6);
	//	break;
	//case 12:
	//	minQ = TMath::Sqrt(17.e6);
	//	maxQ = TMath::Sqrt(18.e6);
	//	break;
	//case 13:
	//	minQ = TMath::Sqrt(18.e6);
	//	maxQ = TMath::Sqrt(19.e6);
	//	break;
	//case 14:
	//	minQ = TMath::Sqrt(19.e6);
	//	maxQ = TMath::Sqrt(20.e6);
	//	break;
	//case 15:
	//	minQ = TMath::Sqrt(20.e6);
	//	maxQ = TMath::Sqrt(21.e6);
	//	break;
	//case 16:
	//	minQ = TMath::Sqrt(21.e6);
	//	maxQ = TMath::Sqrt(22.e6);
	//	break;
	case 17:
		minQ = TMath::Sqrt(1.1e6);
		maxQ = TMath::Sqrt(6.e6);
		break;
	case 18:
		minQ = TMath::Sqrt(15.e6);
		maxQ = TMath::Sqrt(22.e6);
		break;
	case 19:
		minQ = TMath::Sqrt(1.1e6);
		maxQ = TMath::Sqrt(22.e6);
		break;
	default:
		return;
	}
	TString cutStr("B_ConstBNoPsiFit_J_psi_1S_M> "); cutStr += minQ; cutStr += " && B_ConstBNoPsiFit_J_psi_1S_M< "; cutStr += maxQ;

	//B_M 
	RooRealVar B_M("B_NoPsiFit_M","; m(Kmumu) (MeV/c^{2}); Candidates / 12 MeV/c^{2}",4900,5600);
	RooRealVar Psi_M("B_ConstBNoPsiFit_J_psi_1S_M","; m(mumu) (MeV/c^{2}); Candidates / 45 MeV/c^{2}",0,5000);
	RooRealVar B_PsiFit_M("B_FullFit_M","; m(Kmumu) (MeV/c^{2}); Candidates / 12 MeV/c^{2}",3000,7000);

	RooDataSet * data  = new RooDataSet("data", "dataset with B_REFITTED_M", DecayTree, RooArgSet(B_M,Psi_M,B_PsiFit_M));
	RooDataSet * data1 = dynamic_cast<RooDataSet*>(data->reduce(cutStr));
	RooDataSet * dataPR  = dynamic_cast<RooDataSet*>(data->reduce("B_FullFit_M>4800&&B_FullFit_M<5200"));
	RooDataSet * dataSig = dynamic_cast<RooDataSet*>(data->reduce("B_FullFit_M>5200&&B_FullFit_M<5350"));

    	RooKeysPdf pr( "pr" ,"pr" ,B_M,*dataPR);
    	RooKeysPdf sig("sig","sig",B_M,*dataSig);

	RooRealVar p0("p0","",0.);//-4.03708e-03,-0.1,0.1);
	RooExponential comb("comb","",B_M,p0);

	// Number of signal & background events
	RooRealVar nsig("nsig","#signal events",300,-1000,50000,"Events");
	RooRealVar nPR( "nPR" ,"#PR events"    ,300,-1000,50000,"Events");
	RooRealVar nbkg("nbkg","#signal events",300,-1000,50000,"Events");

	RooAddPdf full_RF_PDF("full_RF_PDF","RF PDF of everything",RooArgList(sig,pr,comb), RooArgList(nsig,nPR,nbkg));

	//# Do the fit on REFITTED Mass
	full_RF_PDF.fitTo(*data1,RooFit::Extended());

	TCanvas * can = new TCanvas("can","Mass fits Data",800,600);
	B_M_RF_Plot = B_M.frame(20);
	B_M_RF_Plot->SetTitle("");
	B_M_RF_Plot->GetYaxis()->SetTitle("Candidates / 16 MeV/c^{2}");
	B_M_RF_Plot->GetXaxis()->SetTitle("m(K#mu#mu) (MeV/c^{2})");

	data1->plotOn(B_M_RF_Plot);
	full_RF_PDF.plotOn(B_M_RF_Plot);
	full_RF_PDF.plotOn(B_M_RF_Plot, RooFit::Components("pr"),   RooFit::LineStyle(kDashed),RooFit::LineColor(kRed));
	full_RF_PDF.plotOn(B_M_RF_Plot, RooFit::Components("comb"), RooFit::LineStyle(kDashed),RooFit::LineColor(kMagenta));
        full_RF_PDF.plotOn(B_M_RF_Plot, RooFit::Components("sig"),  RooFit::LineStyle(kDashed));
	B_M_RF_Plot->Draw();

	can->SaveAs("plots/Kee_Q"+binStr+".pdf");

	can->SetLogy();
        B_M_RF_Plot->SetMinimum(1.e-1);
        B_M_RF_Plot->SetMaximum(5.e+2);
	B_M_RF_Plot->Draw();
	can->SaveAs("plots/Kee_Q"+binStr+"_log.pdf");

//	//Get integrals
//	double mBdm = sigmean.getVal() - 2.5*(sigsigma.getVal());
//	double mBdp = sigmean.getVal() + 2.5*(sigsigma.getVal());
//
//	B_M.setRange("signal",mBdm,mBdp);
//	B_M.setRange("sideband",5400,5970);
//	B_M.setRange("full",5170,5970);
//
//	double fsig1 = B0Sig.createIntegral(RooArgSet(B_M),RooFit::NormSet(B_M),RooFit::Range("signal"))->getVal();
//	double fsig2 = B0Sig.createIntegral(RooArgSet(B_M),RooFit::NormSet(B_M),RooFit::Range("sideband"))->getVal();
//	double fsig0 = B0Sig.createIntegral(RooArgSet(B_M),RooFit::NormSet(B_M),RooFit::Range("full"))->getVal();
//
//	double fbkg1 = comb_bkg.createIntegral(RooArgSet(B_M),RooFit::NormSet(B_M),RooFit::Range("signal"))->getVal();
//	double fbkg2 = comb_bkg.createIntegral(RooArgSet(B_M),RooFit::NormSet(B_M),RooFit::Range("sideband"))->getVal();
//	double fbkg0 = comb_bkg.createIntegral(RooArgSet(B_M),RooFit::NormSet(B_M),RooFit::Range("full"))->getVal();
//
//	std::cout << std::endl;
//	std::cout << sigmean.getVal() << "\t" << sigsigma.getVal() << std::endl << std::endl;
//	std::cout << "\t\tsig\tbkg" << std::endl;
//	std::cout << "window  \t" << nsig.getVal()*fsig1/fsig0 << "\t" << nbkg.getVal()*fbkg1/fbkg0 << std::endl;
//	std::cout << "sideband\t" << nsig.getVal()*fsig2/fsig0 << "\t" << nbkg.getVal()*fbkg2/fbkg0 << std::endl;
//	std::cout << std::endl;
//
//	std::ofstream fout;
//	fout.open("bkgParams/"+binStr+".dat");
//	fout << sigmean.getVal() - 2.5*sigsigma.getVal() << "\t" << sigmean.getVal() + 2.5*sigsigma.getVal() << "\t" << fbkg1/fbkg2 << std::endl;
//	fout.close();
//
//	//// Try splot stuff
//	//// First set all parameters to constant except for yields
//	sigmean.setConstant();
//	sigsigma.setConstant();
//	p0.setConstant();
//
//	RooStats::SPlot * sData = new RooStats::SPlot("sData","An SPlot",*data1, &full_RF_PDF, RooArgList(nsig,nbkg));
//	sData->GetSDataSet()->write("/Home/dcraik/Kll/tuples/fromPatrick/Kmm_Q"+binStr+"_sWeights.txt");

}
Example #7
0
void fitKmm_loQ(Int_t bin) {
	gSystem->Load("libRooFit");
	gROOT->SetStyle("Plain");
	gStyle->SetOptStat(1111);

	TFile * file = TFile::Open("/Disk/ecdf-nfs-ppe/lhcb/gcowan/B2Kll/data/fromAlex/BuKmm.root");
	TTree * DecayTree = dynamic_cast<TTree*>(file->Get("DecayTree"));

	TString binStr; binStr+=bin;
	Double_t minQ(0.), maxQ(0.);

	switch(bin) {
	case 0:
		minQ = TMath::Sqrt(1.1e6);
		maxQ = TMath::Sqrt(2.e6);
		break;
	case 1:
		minQ = TMath::Sqrt(2.e6);
		maxQ = TMath::Sqrt(3.e6);
		break;
	case 2:
		minQ = TMath::Sqrt(3.e6);
		maxQ = TMath::Sqrt(4.e6);
		break;
	case 3:
		minQ = TMath::Sqrt(4.e6);
		maxQ = TMath::Sqrt(5.e6);
		break;
	case 4:
		minQ = TMath::Sqrt(5.e6);
		maxQ = TMath::Sqrt(6.e6);
		break;
	default:
		return;
	}
	TString cutStr("Psi_M> "); cutStr += minQ; cutStr += " && Psi_M< "; cutStr += maxQ;

	//B_M 
	RooRealVar B_M("B_M","; m(Kmumu) (MeV/c^{2}); Candidates / 12 MeV/c^{2}",5150,6000);
	RooRealVar Psi_M("Psi_M","; m(mumu) (MeV/c^{2}); Candidates / 45 MeV/c^{2}",500,5000);

	RooDataSet * data  = new RooDataSet("data", "dataset with B_REFITTED_M", DecayTree, RooArgSet(B_M,Psi_M));
	RooDataSet * data1 = dynamic_cast<RooDataSet*>(data->reduce(cutStr));

// from J/Psi region
//   1  #sigma_{Lo}  1.59171e+01   9.61516e-02   1.80663e-03   6.11760e-02
//   2  M_{B}        5.28397e+03   3.00802e-02   1.66677e-03   3.66768e-01
//   3  a1           1.57752e+00   1.64484e-02   2.65338e-03  -7.53912e-01
//   4  a2          -2.64268e+00   2.11254e-02   2.51938e-03   4.90950e-01
//   5  frac         6.78672e-01   1.29969e-02   7.03329e-03   3.65422e-01
//   6  n1           4.79832e+00   2.84430e-01   2.61785e-02  -4.03463e-02
//   7  n2           1.08224e+00   2.68180e-02   5.47500e-03  -9.00362e-01
//   8  nbkg         5.56890e+03   1.31433e+02   7.62084e-03  -8.36640e-01
//   9  nsig         6.56230e+05   8.17224e+02   4.15943e-03   6.95832e-01
//  10  p0          -6.44379e-02   2.13769e-03   2.57927e-02   4.41139e-01
//  11  ratio        1.60407e+00   9.46569e-03   3.93086e-03  -7.72555e-01


	// B DCB 
	// start, range to from. plus names and titles.
	RooRealVar sigmean("M_{B}","B mass",5281.0,5250.0,5300.0,"MeV/c^{2}");
	RooRealVar sigsigma("#sigma_{Lo}","B sigma",15.9,0.0,30.0,"MeV/c^{2}");
	RooRealVar a1("a1","a1", 1.57752e+00);
	RooRealVar n1("n1","n1", 4.79832e+00);
	RooRealVar a2("a2","a2",-2.64268e+00);
	RooRealVar n2("n2","n2", 1.08224e+00);
	RooRealVar ratio("ratio","Ratio of widths",1.60407e+00);
	RooProduct sigsigma2("#sigma_{B}2","B sigma2",RooArgSet(sigsigma,ratio));
	RooRealVar frac("frac","fraction of events in each gaussian",6.78672e-01);
	RooCBShape BSig_RF( "Bsig_RF", "Signal CB B RF Mass", B_M, sigmean, sigsigma, a1, n1 );
	RooCBShape BSig_RF2( "Bsig_RF2", "Signal CB B RF Mass", B_M, sigmean, sigsigma2, a2, n2 );
	RooAddPdf B0Sig("B0signal","signal pdf",RooArgList(BSig_RF,BSig_RF2),RooArgList(frac));

	RooRealVar p0("p0","",-6.44379e-02,-0.1,0.1);
	RooExponential comb_bkg("comb_bkg","",B_M,p0);

	// Number of signal & background events
	RooRealVar nsig("nsig","#signal events",150,-1000,50000,"Events");
	RooRealVar nbkg("nbkg","#signal events",150,-1000,50000,"Events");

	RooAddPdf full_RF_PDF("full_RF_PDF","RF PDF of everything",RooArgList(B0Sig,comb_bkg), RooArgList(nsig,nbkg));

	//# Do the fit on REFITTED Mass
	full_RF_PDF.fitTo(*data1,RooFit::Extended());

	TCanvas * can = new TCanvas("can","Mass fits Data",800,600);
	B_M_RF_Plot = B_M.frame(100);
	B_M_RF_Plot->SetTitle("");
	B_M_RF_Plot->GetYaxis()->SetTitle("Candidates / 8.5 MeV/c^{2}");
	B_M_RF_Plot->GetXaxis()->SetTitle("m(K#mu#mu) (MeV/c^{2})");

	data1->plotOn(B_M_RF_Plot);
	full_RF_PDF.plotOn(B_M_RF_Plot);
	full_RF_PDF.plotOn(B_M_RF_Plot, RooFit::Components("comb_bkg"), RooFit::LineStyle(kDashed),RooFit::LineColor(kMagenta));
        full_RF_PDF.plotOn(B_M_RF_Plot, RooFit::Components("B0signal"), RooFit::LineStyle(kDashed));
	B_M_RF_Plot->Draw();

	can->SaveAs("plots/Kmm_loQ_"+binStr+".pdf");

	can->SetLogy();
        B_M_RF_Plot->SetMinimum(1.e-1);
        B_M_RF_Plot->SetMaximum(5.e+2);
	B_M_RF_Plot->Draw();
	can->SaveAs("plots/Kmm_loQ_"+binStr+"_log.pdf");

      //// Try splot stuff
      //// First set all parameters to constant except for yields
      sigmean.setConstant();
      sigsigma.setConstant();
      p0.setConstant();
      
      RooStats::SPlot * sData = new RooStats::SPlot("sData","An SPlot",*data1, &full_RF_PDF, RooArgList(nsig,nbkg));
      sData->GetSDataSet()->write("/Home/dcraik/Kll/tuples/Kmm_loQ_"+binStr+"_sWeights.txt");

}