示例#1
0
void potion_heal(int extra)
{
	int ratio;
	short add = 0 ;

	if( rogue.hp_current < rogue.hp_max )
	{
		int i ;

		for( i = 0 ; i < get_rogue_level(1) ; i++ ) // (zerogue 0.4.1)
			add += get_rand(1,6) ;

		add += get_rogue_level(1) ; // (zerogue 0.4.1)

		if( ( rogue.hp_current + add > rogue.hp_max ) && ! extra )
			rogue.hp_current = rogue.hp_max ;
		else
			rogue.hp_current += add ;
	}

	if( blind ) unblind() ;
	
	if( confused && extra )
		unconfuse() ;
	else if (confused)
		confused = (confused / 2) + 1 ;

	if( halluc && extra )
		unhallucinate() ;
	else if( halluc )
		halluc = (halluc / 2) + 1 ;
}
示例#2
0
static void
potion_heal(boolean extra)
{
	float ratio;
	short add;

	rogue.hp_current += rogue.exp;

	ratio = ((float)rogue.hp_current) / rogue.hp_max;

	if (ratio >= 1.00) {
		rogue.hp_max += (extra ? 2 : 1);
		extra_hp += (extra ? 2 : 1);
		rogue.hp_current = rogue.hp_max;
	} else if (ratio >= 0.90) {
		rogue.hp_max += (extra ? 1 : 0);
		extra_hp += (extra ? 1 : 0);
		rogue.hp_current = rogue.hp_max;
	} else {
		if (ratio < 0.33) {
			ratio = 0.33;
		}
		if (extra) {
			ratio += ratio;
		}
		add = (short)(ratio * ((float)rogue.hp_max - rogue.hp_current));
		rogue.hp_current += add;
		if (rogue.hp_current > rogue.hp_max) {
			rogue.hp_current = rogue.hp_max;
		}
	}
	if (blind) {
		unblind();
	}
	if (confused && extra) {
		unconfuse();
	} else if (confused) {
		confused = (confused / 2) + 1;
	}
	if (halluc && extra) {
		unhallucinate();
	} else if (halluc) {
		halluc = (halluc / 2) + 1;
	}
}
示例#3
0
文件: use.c 项目: masoo/rogueclone2s
void
potion_heal(int extra)
{
    long ratio;
    short add;

    rogue.hp_current += rogue.exp;

    ratio = rogue.hp_current * 100L / rogue.hp_max;

    if (ratio >= 100L) {
	rogue.hp_max += (extra ? 2 : 1);
	extra_hp += (extra ? 2 : 1);
	rogue.hp_current = rogue.hp_max;
    } else if (ratio >= 90L) {
	rogue.hp_max += (extra ? 1 : 0);
	extra_hp += (extra ? 1 : 0);
	rogue.hp_current = rogue.hp_max;
    } else {
	if (ratio < 33L) {
	    ratio = 33L;
	}
	if (extra) {
	    ratio += ratio;
	}
	add = (short) (ratio * (rogue.hp_max - rogue.hp_current) / 100L);
	rogue.hp_current += add;
	if (rogue.hp_current > rogue.hp_max) {
	    rogue.hp_current = rogue.hp_max;
	}
    }
    if (blind) {
	unblind();
    }
    if (confused && extra) {
	unconfuse();
    } else if (confused) {
	confused = (confused / 2) + 1;
    }
    if (halluc && extra) {
	unhallucinate();
    } else if (halluc) {
	halluc = (halluc / 2) + 1;
    }
}
示例#4
0
void
quaff(void)
{
	short ch;
	char buf[80];
	object *obj;

	ch = pack_letter("quaff what?", POTION);

	if (ch == CANCEL) {
		return;
	}
	if (!(obj = get_letter_object(ch))) {
		message("no such item.", 0);
		return;
	}
	if (obj->what_is != POTION) {
		message("you can't drink that", 0);
		return;
	}
	switch(obj->which_kind) {
		case INCREASE_STRENGTH:
			message("you feel stronger now, what bulging muscles!",
			0);
			rogue.str_current++;
			if (rogue.str_current > rogue.str_max) {
				rogue.str_max = rogue.str_current;
			}
			break;
		case RESTORE_STRENGTH:
			rogue.str_current = rogue.str_max;
			message("this tastes great, you feel warm all over", 0);
			break;
		case HEALING:
			message("you begin to feel better", 0);
			potion_heal(0);
			break;
		case EXTRA_HEALING:
			message("you begin to feel much better", 0);
			potion_heal(1);
			break;
		case POISON:
			if (!sustain_strength) {
				rogue.str_current -= get_rand(1, 3);
				if (rogue.str_current < 1) {
					rogue.str_current = 1;
				}
			}
			message("you feel very sick now", 0);
			if (halluc) {
				unhallucinate();
			}
			break;
		case RAISE_LEVEL:
			rogue.exp_points = level_points[rogue.exp - 1];
			message("you suddenly feel much more skillful", 0);
			add_exp(1, 1);
			break;
		case BLINDNESS:
			go_blind();
			break;
		case HALLUCINATION:
			message("oh wow, everything seems so cosmic", 0);
			halluc += get_rand(500, 800);
			break;
		case DETECT_MONSTER:
			show_monsters();
			if (!(level_monsters.next_monster)) {
				message(strange_feeling, 0);
			}
			break;
		case DETECT_OBJECTS:
			if (level_objects.next_object) {
				if (!blind) {
					show_objects();
				}
			} else {
				message(strange_feeling, 0);
			}
			break;
		case CONFUSION:
			message((halluc ? "what a trippy feeling" :
			"you feel confused"), 0);
			cnfs();
			break;
		case LEVITATION:
			message("you start to float in the air", 0);
			levitate += get_rand(15, 30);
			bear_trap = being_held = 0;
			break;
		case HASTE_SELF:
			message("you feel yourself moving much faster", 0);
			haste_self += get_rand(11, 21);
			if (!(haste_self % 2)) {
				haste_self++;
			}
			break;
		case SEE_INVISIBLE:
			sprintf(buf, "hmm, this potion tastes like %sjuice", fruit);
			message(buf, 0);
			if (blind) {
				unblind();
			}
			see_invisible = 1;
			relight();
			break;
	}
	print_stats((STAT_STRENGTH | STAT_HP));
	if (id_potions[obj->which_kind].id_status != CALLED) {
		id_potions[obj->which_kind].id_status = IDENTIFIED;
	}
	vanish(obj, 1, &rogue.pack);
}
示例#5
0
文件: rsa-ltm.c 项目: AIdrifter/samba
static int
ltm_rsa_private_decrypt(int flen, const unsigned char* from,
			unsigned char* to, RSA* rsa, int padding)
{
    unsigned char *ptr;
    int res, size;
    mp_int in, out, n, e, b, bi;
    int blinding = (rsa->flags & RSA_FLAG_NO_BLINDING) == 0;
    int do_unblind = 0;

    if (padding != RSA_PKCS1_PADDING)
	return -1;

    size = RSA_size(rsa);
    if (flen > size)
	return -2;

    mp_init_multi(&in, &n, &e, &out, &b, &bi, NULL);

    BN2mpz(&n, rsa->n);
    BN2mpz(&e, rsa->e);

    mp_read_unsigned_bin(&in, rk_UNCONST(from), flen);

    if(mp_isneg(&in) || mp_cmp(&in, &n) >= 0) {
	size = -2;
	goto out;
    }

    if (blinding) {
	setup_blind(&n, &b, &bi);
	blind(&in, &b, &e, &n);
	do_unblind = 1;
    }

    if (rsa->p && rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp) {
	mp_int p, q, dmp1, dmq1, iqmp;

	mp_init_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);

	BN2mpz(&p, rsa->p);
	BN2mpz(&q, rsa->q);
	BN2mpz(&dmp1, rsa->dmp1);
	BN2mpz(&dmq1, rsa->dmq1);
	BN2mpz(&iqmp, rsa->iqmp);

	res = ltm_rsa_private_calculate(&in, &p, &q, &dmp1, &dmq1, &iqmp, &out);

	mp_clear_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);

	if (res != 0) {
	    size = -3;
	    goto out;
	}

    } else {
	mp_int d;

	if(mp_isneg(&in) || mp_cmp(&in, &n) >= 0)
	    return -4;

	BN2mpz(&d, rsa->d);
	res = mp_exptmod(&in, &d, &n, &out);
	mp_clear(&d);
	if (res != 0) {
	    size = -5;
	    goto out;
	}
    }

    if (do_unblind)
	unblind(&out, &bi, &n);

    ptr = to;
    {
	size_t ssize;
	ssize = mp_unsigned_bin_size(&out);
	assert(size >= ssize);
	mp_to_unsigned_bin(&out, ptr);
	size = ssize;
    }

    /* head zero was skipped by mp_int_to_unsigned */
    if (*ptr != 2) {
	size = -6;
	goto out;
    }
    size--; ptr++;
    while (size && *ptr != 0) {
	size--; ptr++;
    }
    if (size == 0)
	return -7;
    size--; ptr++;

    memmove(to, ptr, size);

 out:
    mp_clear_multi(&e, &n, &in, &out, &b, &bi, NULL);

    return size;
}
示例#6
0
文件: rsa-ltm.c 项目: AIdrifter/samba
static int
ltm_rsa_private_encrypt(int flen, const unsigned char* from,
			unsigned char* to, RSA* rsa, int padding)
{
    unsigned char *p, *p0;
    int res;
    int size;
    mp_int in, out, n, e;
    mp_int bi, b;
    int blinding = (rsa->flags & RSA_FLAG_NO_BLINDING) == 0;
    int do_unblind = 0;

    if (padding != RSA_PKCS1_PADDING)
	return -1;

    mp_init_multi(&e, &n, &in, &out, &b, &bi, NULL);

    size = RSA_size(rsa);

    if (size < RSA_PKCS1_PADDING_SIZE || size - RSA_PKCS1_PADDING_SIZE < flen)
	return -2;

    p0 = p = malloc(size);
    *p++ = 0;
    *p++ = 1;
    memset(p, 0xff, size - flen - 3);
    p += size - flen - 3;
    *p++ = 0;
    memcpy(p, from, flen);
    p += flen;
    assert((p - p0) == size);

    BN2mpz(&n, rsa->n);
    BN2mpz(&e, rsa->e);

    mp_read_unsigned_bin(&in, p0, size);
    free(p0);

    if(mp_isneg(&in) || mp_cmp(&in, &n) >= 0) {
	size = -3;
	goto out;
    }

    if (blinding) {
	setup_blind(&n, &b, &bi);
	blind(&in, &b, &e, &n);
	do_unblind = 1;
    }

    if (rsa->p && rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp) {
	mp_int p, q, dmp1, dmq1, iqmp;

	mp_init_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);

	BN2mpz(&p, rsa->p);
	BN2mpz(&q, rsa->q);
	BN2mpz(&dmp1, rsa->dmp1);
	BN2mpz(&dmq1, rsa->dmq1);
	BN2mpz(&iqmp, rsa->iqmp);

	res = ltm_rsa_private_calculate(&in, &p, &q, &dmp1, &dmq1, &iqmp, &out);

	mp_clear_multi(&p, &q, &dmp1, &dmq1, &iqmp, NULL);

	if (res != 0) {
	    size = -4;
	    goto out;
	}
    } else {
	mp_int d;

	BN2mpz(&d, rsa->d);
	res = mp_exptmod(&in, &d, &n, &out);
	mp_clear(&d);
	if (res != 0) {
	    size = -5;
	    goto out;
	}
    }

    if (do_unblind)
	unblind(&out, &bi, &n);

    if (size > 0) {
	size_t ssize;
	ssize = mp_unsigned_bin_size(&out);
	assert(size >= ssize);
	mp_to_unsigned_bin(&out, to);
	size = ssize;
    }

 out:
    mp_clear_multi(&e, &n, &in, &out, &b, &bi, NULL);

    return size;
}
void BackgroundPrediction_Kinematic_GaussExp_ttbarSubtracted()
{

  gROOT->SetStyle("Plain");
  gStyle->SetOptStat(000000000);
  
  if (bReg) tags=tags+"_bReg";
  
  const unsigned int nPoints=4;
  double mass[nPoints]={90., 107.5, 142.5, 160.};
  double n_SB[nPoints], n_SR[nPoints];
  double ratio[nPoints];
  double errorsY[nPoints], errorsX[nPoints];
  double ratioAt125=-1, errorAt125=-1;
  
  // TFile *f_MMMM_a=new TFile((tags+"/a/Histograms_8TeVData2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_a=new TFile((tags+"/a/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TFile *f_MMMM_a=new TFile((tags+"/a_KinFit/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_a=new TFile((tags+"/a_KinFit/Histograms_BJetPlusX_Run2012BCD_Skim_selected_bTagged_.root").c_str());
  TH1F *h_mX_CR2_a=(TH1F*)f_MMMM_a->Get("h_mX_CR2");
  TH1F *h_mX_CR4_a=(TH1F*)f_MMMM_a->Get("h_mX_CR4");
  TH1F *h_mX_SR_a=(TH1F*)f_MMMM_a->Get("h_mX_SR");
  ratioAt125=h_mX_SR_a->GetSumOfWeights()/(h_mX_CR2_a->GetSumOfWeights()+h_mX_CR4_a->GetSumOfWeights());
  
  // === MMMM/b ===
  // TFile *f_MMMM_b=new TFile((tags+"/b/Histograms_8TeVData2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_b=new TFile((tags+"/b/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TFile *f_MMMM_b=new TFile((tags+"/b_KinFit/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_b=new TFile((tags+"/b_KinFit/Histograms_BJetPlusX_Run2012BCD_Skim_selected_bTagged_.root").c_str());
  TH1F *h_mX_CR2_b=(TH1F*)f_MMMM_b->Get("h_mX_CR2");
  TH1F *h_mX_CR4_b=(TH1F*)f_MMMM_b->Get("h_mX_CR4");
  TH1F *h_mX_SR_b=(TH1F*)f_MMMM_b->Get("h_mX_SR");
  n_SB[0]=(h_mX_CR2_b->GetSumOfWeights()+h_mX_CR4_b->GetSumOfWeights());
  n_SR[0]=h_mX_SR_b->GetSumOfWeights();
  
  if (useRatioFit)
  {
    // === MMMM/d ===
    // TFile *f_MMMM_d=new TFile((tags+"/d/Histograms_8TeVData2012BCD_Skim.root").c_str());
    TFile *f_MMMM_d=new TFile((tags+"/d/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
    // TFile *f_MMMM_d=new TFile((tags+"/d/Histograms_BJetPlusX_Run2012BCD_Skim_selected_bTagged_.root").c_str());
    TH1F *h_mX_CR2_d=(TH1F*)f_MMMM_d->Get("h_mX_CR2");
    TH1F *h_mX_CR4_d=(TH1F*)f_MMMM_d->Get("h_mX_CR4");
    TH1F *h_mX_SR_d=(TH1F*)f_MMMM_d->Get("h_mX_SR");
    n_SB[1]=(h_mX_CR2_d->GetSumOfWeights()+h_mX_CR4_d->GetSumOfWeights());
    n_SR[1]=h_mX_SR_d->GetSumOfWeights();
  
    // === MMMM/e ===
    // TFile *f_MMMM_e=new TFile((tags+"/e/Histograms_8TeVData2012BCD_Skim.root").c_str());
    TFile *f_MMMM_e=new TFile((tags+"/e/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
    // TFile *f_MMMM_e=new TFile((tags+"/e/Histograms_BJetPlusX_Run2012BCD_Skim_selected_bTagged_.root").c_str());
    TH1F *h_mX_CR2_e=(TH1F*)f_MMMM_e->Get("h_mX_CR2");
    TH1F *h_mX_CR4_e=(TH1F*)f_MMMM_e->Get("h_mX_CR4");
    TH1F *h_mX_SR_e=(TH1F*)f_MMMM_e->Get("h_mX_SR");
    n_SB[2]=(h_mX_CR2_e->GetSumOfWeights()+h_mX_CR4_e->GetSumOfWeights());
    n_SR[2]=h_mX_SR_e->GetSumOfWeights();
  
    // === MMMM/c ===
    // TFile *f_MMMM_c=new TFile((tags+"/c/Histograms_8TeVData2012BCD_Skim.root").c_str());
    TFile *f_MMMM_c=new TFile((tags+"/c/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
    // TFile *f_MMMM_c=new TFile((tags+"/c/Histograms_BJetPlusX_Run2012BCD_Skim_selected_bTagged_.root").c_str());
    TH1F *h_mX_CR2_c=(TH1F*)f_MMMM_c->Get("h_mX_CR2");
    TH1F *h_mX_CR4_c=(TH1F*)f_MMMM_c->Get("h_mX_CR4");
    TH1F *h_mX_SR_c=(TH1F*)f_MMMM_c->Get("h_mX_SR");
    n_SB[3]=(h_mX_CR2_c->GetSumOfWeights()+h_mX_CR4_c->GetSumOfWeights());
    n_SR[3]=h_mX_SR_c->GetSumOfWeights();
  
    for (unsigned int i=0; i<nPoints; ++i)
    {
      ratio[i]=n_SR[i]/n_SB[i];
      errorsY[i]=ratio[i]*pow(1./n_SR[i]+1./n_SB[i], 0.5);
      errorsX[i]=0.;
    }
  
    TGraphErrors *g_ratio=new TGraphErrors(nPoints, mass, ratio, errorsX, errorsY);
    g_ratio->SetTitle("SR/SB ratio");
    TCanvas *c_ratio=new TCanvas("c_ratio", "c_ratio", 700, 700);
    g_ratio->SetMinimum(0.); g_ratio->SetMaximum(2.);
    g_ratio->Draw("A*");
    TF1 *f_ratio=new TF1("f_ratio", "pol1");
    g_ratio->Fit(f_ratio);
    c_ratio->SaveAs(("c_ratio_"+tags+".png").c_str());
  
    ratioAt125=f_ratio->Eval(125.);
    errorAt125=(errorsY[0]+errorsY[1]+errorsY[2]+errorsY[3])/4.;
  }
  
  std::cout<<"ratioAt125 = "<<ratioAt125<<" +- "<<errorAt125<<std::endl;
  std::cout<<"bgFloat   lnN     -   "<<1.+errorAt125/ratioAt125<<std::endl;
  
  // Get the ttbar
  double totalLuminosity=17928; // /pb
  double xsec_ttbar_fulllept=24.56;
  double xsec_ttbar_semilept=103.12;
  double xsec_ttbar_hadronic=106.32;
  
  TFile *ttbar_fulllept_b=new TFile("MMMM_nominal/b_KinFit/Histograms_TTJets_FullLeptMGDecays_8TeV-madgraph_Skim.root");
  TFile *ttbar_semilept_b=new TFile("MMMM_nominal/b_KinFit/Histograms_TTJets_SemiLeptMGDecays_8TeV-madgraph_Skim.root");
  TFile *ttbar_hadronic_b=new TFile("MMMM_nominal/b_KinFit/Histograms_TTJets_HadronicMGDecays_8TeV-madgraph_Skim.root");
  
  TFile *ttbar_fulllept_a=new TFile("MMMM_nominal/a_KinFit/Histograms_TTJets_FullLeptMGDecays_8TeV-madgraph_Skim.root");
  TFile *ttbar_semilept_a=new TFile("MMMM_nominal/a_KinFit/Histograms_TTJets_SemiLeptMGDecays_8TeV-madgraph_Skim.root");
  TFile *ttbar_hadronic_a=new TFile("MMMM_nominal/a_KinFit/Histograms_TTJets_HadronicMGDecays_8TeV-madgraph_Skim.root");
  
  double init_ttbar_fulllept=((TH1F*)ttbar_fulllept_a->Get("CountWithPU"))->GetBinContent(1);
  double init_ttbar_semilept=((TH1F*)ttbar_semilept_a->Get("CountWithPU"))->GetBinContent(1);
  double init_ttbar_hadronic=((TH1F*)ttbar_hadronic_a->Get("CountWithPU"))->GetBinContent(1);
  
  double scale_ttbar_fulllept=totalLuminosity*xsec_ttbar_fulllept/init_ttbar_fulllept;
  double scale_ttbar_semilept=totalLuminosity*xsec_ttbar_semilept/init_ttbar_semilept;
  double scale_ttbar_hadronic=totalLuminosity*xsec_ttbar_hadronic/init_ttbar_hadronic;
  
  TH1F *h_mX_VR_ttbar_fulllept=(TH1F*)ttbar_fulllept_b->Get("h_mX_SR");
  TH1F *h_mX_VR_ttbar_semilept=(TH1F*)ttbar_semilept_b->Get("h_mX_SR");
  TH1F *h_mX_VR_ttbar_hadronic=(TH1F*)ttbar_hadronic_b->Get("h_mX_SR");
  
  TH1F *h_mX_VB_ttbar_fulllept=(TH1F*)ttbar_fulllept_b->Get("h_mX_CR2"); h_mX_VB_ttbar_fulllept->Add((TH1F*)ttbar_fulllept_b->Get("h_mX_CR4"));
  TH1F *h_mX_VB_ttbar_semilept=(TH1F*)ttbar_semilept_b->Get("h_mX_CR2"); h_mX_VB_ttbar_semilept->Add((TH1F*)ttbar_semilept_b->Get("h_mX_CR4"));
  TH1F *h_mX_VB_ttbar_hadronic=(TH1F*)ttbar_hadronic_b->Get("h_mX_CR2"); h_mX_VB_ttbar_hadronic->Add((TH1F*)ttbar_hadronic_b->Get("h_mX_CR4"));
  
  TH1F *h_mX_SR_ttbar_fulllept=(TH1F*)ttbar_fulllept_a->Get("h_mX_SR");
  TH1F *h_mX_SR_ttbar_semilept=(TH1F*)ttbar_semilept_a->Get("h_mX_SR");
  TH1F *h_mX_SR_ttbar_hadronic=(TH1F*)ttbar_hadronic_a->Get("h_mX_SR");
  
  TH1F *h_mX_SB_ttbar_fulllept=(TH1F*)ttbar_fulllept_a->Get("h_mX_CR2"); h_mX_SB_ttbar_fulllept->Add((TH1F*)ttbar_fulllept_a->Get("h_mX_CR4"));
  TH1F *h_mX_SB_ttbar_semilept=(TH1F*)ttbar_semilept_a->Get("h_mX_CR2"); h_mX_SB_ttbar_semilept->Add((TH1F*)ttbar_semilept_a->Get("h_mX_CR4"));
  TH1F *h_mX_SB_ttbar_hadronic=(TH1F*)ttbar_hadronic_a->Get("h_mX_CR2"); h_mX_SB_ttbar_hadronic->Add((TH1F*)ttbar_hadronic_a->Get("h_mX_CR4"));
  
  h_mX_VR_ttbar_fulllept->Scale(scale_ttbar_fulllept);
  h_mX_VR_ttbar_semilept->Scale(scale_ttbar_semilept);
  h_mX_VR_ttbar_hadronic->Scale(scale_ttbar_hadronic);
  TH1F *h_mX_VR_ttbar=h_mX_VR_ttbar_fulllept->Clone("h_mX_VR_ttbar");
  h_mX_VR_ttbar->Add(h_mX_VR_ttbar_semilept);
  h_mX_VR_ttbar->Add(h_mX_VR_ttbar_hadronic);
  h_mX_VR_ttbar->Rebin(rebin);
  
  h_mX_VB_ttbar_fulllept->Scale(scale_ttbar_fulllept);
  h_mX_VB_ttbar_semilept->Scale(scale_ttbar_semilept);
  h_mX_VB_ttbar_hadronic->Scale(scale_ttbar_hadronic);
  TH1F *h_mX_VB_ttbar=h_mX_VB_ttbar_fulllept->Clone("h_mX_VB_ttbar");
  h_mX_VB_ttbar->Add(h_mX_VB_ttbar_semilept);
  h_mX_VB_ttbar->Add(h_mX_VB_ttbar_hadronic);
  h_mX_VB_ttbar->Rebin(rebin);
  
  h_mX_SR_ttbar_fulllept->Scale(scale_ttbar_fulllept);
  h_mX_SR_ttbar_semilept->Scale(scale_ttbar_semilept);
  h_mX_SR_ttbar_hadronic->Scale(scale_ttbar_hadronic);
  TH1F *h_mX_SR_ttbar=h_mX_SR_ttbar_fulllept->Clone("h_mX_SR_ttbar");
  h_mX_SR_ttbar->Add(h_mX_SR_ttbar_semilept);
  h_mX_SR_ttbar->Add(h_mX_SR_ttbar_hadronic);
  h_mX_SR_ttbar->Rebin(rebin);
  
  h_mX_SB_ttbar_fulllept->Scale(scale_ttbar_fulllept);
  h_mX_SB_ttbar_semilept->Scale(scale_ttbar_semilept);
  h_mX_SB_ttbar_hadronic->Scale(scale_ttbar_hadronic);
  TH1F *h_mX_SB_ttbar=h_mX_SB_ttbar_fulllept->Clone("h_mX_SB_ttbar");
  h_mX_SB_ttbar->Add(h_mX_SB_ttbar_semilept);
  h_mX_SB_ttbar->Add(h_mX_SB_ttbar_hadronic);
  h_mX_SB_ttbar->Rebin(rebin);
  // //
  
  
  std::cout<<" = MMMM b ======================================== "<<std::endl;
  TH1F *h_mMMMMb_3Tag_CR2=(TH1F*)f_MMMM_b->Get("h_mX_CR2");
  TH1F *h_mMMMMb_3Tag_CR4=(TH1F*)f_MMMM_b->Get("h_mX_CR4");
  TH1F *h_mMMMMb_3Tag_SR=(TH1F*)f_MMMM_b->Get("h_mX_SR");
  h_mMMMMb_3Tag_SR->Rebin(rebin);
  h_mMMMMb_3Tag_SR->Add(h_mX_VR_ttbar, -1);
  double bS=h_mMMMMb_3Tag_SR->GetSumOfWeights();
  std::cout<<"Number of events in MMMM b signal region = "<<bS<<std::endl;
  TH1F *h_mMMMMb_3Tag_CR24=(TH1F*)h_mMMMMb_3Tag_CR2->Clone("h_mX_CR24");
  h_mMMMMb_3Tag_CR24->Add(h_mMMMMb_3Tag_CR4);
  h_mMMMMb_3Tag_CR24->Rebin(rebin);
  h_mMMMMb_3Tag_CR24->SetLineColor(kRed);
  h_mMMMMb_3Tag_SR->SetLineColor(kBlue);
  h_mMMMMb_3Tag_CR24->Add(h_mX_VB_ttbar, -1);
  double bC=h_mMMMMb_3Tag_CR24->GetSumOfWeights();
  std::cout<<"bC = "<<bC<<", bS = "<<bS<<std::endl;
  h_mMMMMb_3Tag_CR24->SetMaximum(h_mMMMMb_3Tag_CR24->GetMaximum()*1.3);
  h_mMMMMb_3Tag_CR24->SetTitle(("Kinematic Extrapolation in "+tags+" Validation Region; m_{X} GeV").c_str());
  h_mMMMMb_3Tag_SR->Scale(bC/bS);
  // Do the fits using RooFit
  gSystem->Load("../PDFs/GaussExp_cxx.so");
  RooRealVar x("x", "m_{X} (GeV)", VR_lo-100., VR_hi+100.);
  // bC
  RooRealVar bC_p0("bC_p0", "bC_p0", 300., 500.);
  RooRealVar bC_p1("bC_p1", "bC_p1", 40., 100.1);
  RooRealVar bC_p2("bC_p2", "bC_p2", 0.1, 10.1);
  GaussExp bC_fit("bC_fit", "bC Fit", x, bC_p0, bC_p1, bC_p2);
  h_mMMMMb_3Tag_CR24->GetXaxis()->SetRangeUser(VR_lo-100., VR_hi+100.);
  RooDataHist bC_data("bC_data", "bC Data", RooArgList(x), h_mMMMMb_3Tag_CR24);
  RooFitResult *r_bC_fit=bC_fit.fitTo(bC_data, RooFit::Range(VR_lo, VR_hi), RooFit::Save());
  RooPlot *bC_plot=x.frame();
  bC_data.plotOn(bC_plot);
  bC_fit.plotOn(bC_plot, RooFit::VisualizeError(*r_bC_fit, sigmaVisual), RooFit::FillColor(kOrange));
  bC_fit.plotOn(bC_plot, RooFit::LineColor(kRed));
  bC_data.plotOn(bC_plot, RooFit::LineColor(kRed), RooFit::MarkerColor(kRed));
  // bS
  RooRealVar bS_p0("bS_p0", "bS_p0", 300., 500.);
  RooRealVar bS_p1("bS_p1", "bS_p1", 40., 100.1);
  RooRealVar bS_p2("bS_p2", "bS_p2", 0.1, 10.1);
  GaussExp bS_fit("bS_fit", "bS Fit", x, bS_p0, bS_p1, bS_p2);
  h_mMMMMb_3Tag_SR->GetXaxis()->SetRangeUser(VR_lo-100., VR_hi+100.);
  RooDataHist bS_data("bS_data", "bS Data", RooArgList(x), h_mMMMMb_3Tag_SR);
  RooFitResult *r_bS_fit=bS_fit.fitTo(bS_data, RooFit::Range(VR_lo, VR_hi), RooFit::Save()); // RooFit::SumW2Error(kTRUE), 
  RooPlot *bS_plot=x.frame();
  bS_data.plotOn(bS_plot);
  bS_fit.plotOn(bS_plot,RooFit::VisualizeError(*r_bS_fit, sigmaVisual), RooFit::FillColor(kCyan));
  bS_fit.plotOn(bS_plot, RooFit::LineColor(kBlue));
  bS_data.plotOn(bS_plot, RooFit::LineColor(kBlue), RooFit::MarkerColor(kBlue));
  std::cout<<" === === "<<std::endl;
  // std::cout<<"chi^2/ndof of bC = "<<bC_plot->chiSquare()<<std::endl;
  // std::cout<<"chi^2/ndof of bS = "<<bS_plot->chiSquare()<<std::endl;
  std::cout<<" === === "<<std::endl;
  TCanvas *c_bC=comparePlots2(bC_plot, bS_plot, h_mMMMMb_3Tag_SR, h_mMMMMb_3Tag_CR24, "Kinematic Extrapolation in "+tags+" Validation Region of Data; m_{X} GeV");
  double x_mean_bC=bC_p0.getVal();
  double x_k_bC=bC_p0.getVal()+bC_p2.getVal()*bC_p1.getVal();
  TLine *l_mean_bC=new TLine(x_mean_bC, 0, x_mean_bC, h_mMMMMb_3Tag_CR24->GetMaximum()*0.8); l_mean_bC->SetLineColor(kRed); l_mean_bC->Draw();
  TLine *l_k_bC=new TLine(x_k_bC, 0, x_k_bC, h_mMMMMb_3Tag_CR24->GetMaximum()*0.8); l_k_bC->SetLineColor(kRed); l_k_bC->SetLineStyle(9); l_k_bC->Draw();
  double x_mean_bS=bS_p0.getVal();
  double x_k_bS=bS_p0.getVal()+bS_p2.getVal()*bS_p1.getVal();
  TLine *l_mean_bS=new TLine(x_mean_bS, 0, x_mean_bS, h_mMMMMb_3Tag_SR->GetMaximum()); l_mean_bS->SetLineColor(kBlue); l_mean_bS->Draw();
  TLine *l_k_bS=new TLine(x_k_bS, 0, x_k_bS, h_mMMMMb_3Tag_SR->GetMaximum()); l_k_bS->SetLineColor(kBlue); l_k_bS->SetLineStyle(9); l_k_bS->Draw();
  if (logPlot) c_bC->SaveAs(("c_compareData_"+tags+"_VR_RooFit_GaussExp_LOG.png").c_str());
  else c_bC->SaveAs(("c_compareData_"+tags+"_VR_RooFit_GaussExp.png").c_str());
  
  // Calculate Pi and DPi and dPi -- for shape systematics
  double PbC_0=bC_p0.getVal();
  double PbC_1=bC_p1.getVal();
  double PbC_2=bC_p2.getVal();
  double dPbC_0=bC_p0.getError();
  double dPbC_1=bC_p1.getError();
  double dPbC_2=bC_p2.getError();
  double PbS_0=bS_p0.getVal();
  double PbS_1=bS_p1.getVal();
  double PbS_2=bS_p2.getVal();
  double dPbS_0=bS_p0.getError();
  double dPbS_1=bS_p1.getError();
  double dPbS_2=bS_p2.getError();
  /*double DPb_0=PbS_0-PbC_0;
  double DPb_1=PbS_1-PbC_1;
  double DPb_2=PbS_2-PbC_2;
  double dPb_0=quad(dPbC_0, dPbS_0);
  double dPb_1=quad(dPbC_1, dPbS_1);
  double dPb_2=quad(dPbC_2, dPbS_2);*/
  
  std::cout<<" = MMMM Background Prediction ==== "<<std::endl;
  TH1F *h_mMMMMa_3Tag_CR2=(TH1F*)f_MMMM_a->Get("h_mX_CR2");
  TH1F *h_mMMMMa_3Tag_CR4=(TH1F*)f_MMMM_a->Get("h_mX_CR4");
  TH1F *h_mMMMMa_3Tag_SR;
  if (tags!="MMMM_nominal") h_mMMMMa_3Tag_SR=(TH1F*)f_MMMM_a->Get("h_mX_SR");
  TH1F *h_mMMMMa_3Tag_CR24=(TH1F*)h_mMMMMa_3Tag_CR2->Clone("h_mX_CR24");
  h_mMMMMa_3Tag_CR24->Add(h_mMMMMa_3Tag_CR4);
  h_mMMMMa_3Tag_CR24->Rebin(rebin);
  h_mMMMMa_3Tag_CR24->SetLineColor(kBlack);
  h_mMMMMa_3Tag_CR24->Add(h_mX_SB_ttbar, -1);
  if (tags!="MMMM_nominal") h_mMMMMa_3Tag_SR->Rebin(rebin);
  if (tags!="MMMM_nominal") h_mMMMMa_3Tag_SR->SetLineColor(kBlue);
  if (tags!="MMMM_nominal") h_mMMMMa_3Tag_SR->Add(h_mX_SR_ttbar, -1);
  TH1F *h_mMMMMa_3Tag_SR_Prediction=(TH1F*)h_mMMMMa_3Tag_CR24->Clone("h_mMMMMa_3Tag_SR_Prediction");
  if (logPlot) h_mMMMMa_3Tag_SR_Prediction->SetMinimum(1);
  // double aC=h_mMMMMa_3Tag_CR24->GetSumOfWeights();
  // Get the scale of the prediction right
  std::cout<<"bS/bC = "<<bS/bC<<std::endl;
  std::cout<<"(bC) bgFloat   lnN     -    "<<1.+sqrt(1./bC)<<std::endl;
  std::cout<<"(bC + bS) bgFloat   lnN     -    "<<1.+sqrt(1./bC+1./bS)<<std::endl;
  // std::cout<<"ratioAt125 = "<<ratioAt125<<", +- "<<errorAt125<<" (fract unc.) = "<<1.+errorAt125/ratioAt125<<std::endl;
  // h_mMMMMa_3Tag_SR_Prediction->Scale(ratioAt125);
  std::cout<<"Number of predicted events in 17.928 /fb = "<<h_mMMMMa_3Tag_SR_Prediction->GetSumOfWeights()*ratioAt125<<std::endl;
  std::cout<<"Number of predicted events in 17.928 /fb around mX=509(+-26) GeV = "<<(h_mMMMMa_3Tag_SR_Prediction->Integral(h_mMMMMa_3Tag_SR_Prediction->FindBin(509.-26.), h_mMMMMa_3Tag_SR_Prediction->FindBin(509.+26.)))*249./h_mMMMMa_3Tag_SR_Prediction->GetSumOfWeights()<<std::endl;
  std::cout<<"Number of predicted events in 17.928 /fb around mX=714(+-40) GeV = "<<(h_mMMMMa_3Tag_SR_Prediction->Integral(h_mMMMMa_3Tag_SR_Prediction->FindBin(714.-40.), h_mMMMMa_3Tag_SR_Prediction->FindBin(714.+40.)))*249./h_mMMMMa_3Tag_SR_Prediction->GetSumOfWeights()<<std::endl;
  std::cout<<"Number of predicted events in 17.928 /fb around mX=450-550 GeV = "<<(h_mMMMMa_3Tag_SR_Prediction->Integral(h_mMMMMa_3Tag_SR_Prediction->FindBin(450.), h_mMMMMa_3Tag_SR_Prediction->FindBin(550.)))*249./h_mMMMMa_3Tag_SR_Prediction->GetSumOfWeights()<<std::endl;
  std::cout<<"Number of predicted events in 17.928 /fb around mX=600-800 GeV = "<<(h_mMMMMa_3Tag_SR_Prediction->Integral(h_mMMMMa_3Tag_SR_Prediction->FindBin(600.), h_mMMMMa_3Tag_SR_Prediction->FindBin(800.)))*249./h_mMMMMa_3Tag_SR_Prediction->GetSumOfWeights()<<std::endl;
  // RooFit fit to background prediction
  // RooRealVar bg_p0("bg_p0", "bg_p0", 400., 600.);
  // RooRealVar bg_p1("bg_p1", "bg_p1", 50., 100.1);
  // RooRealVar bg_p2("bg_p2", "bg_p2", 0.1, 10.1);
  // For mX300
  RooRealVar bg_p0("bg_p0", "bg_p0", 350., 600.);
  RooRealVar bg_p1("bg_p1", "bg_p1", 30., 100.1);
  RooRealVar bg_p2("bg_p2", "bg_p2", 0.01, 10.1);
  GaussExp bg("bg", "Background Prediction PDF", x, bg_p0, bg_p1, bg_p2);
  RooDataHist pred("pred", "Prediction from SB", RooArgList(x), h_mMMMMa_3Tag_SR_Prediction);
  RooFitResult *r_bg=bg.fitTo(pred, RooFit::Range(SR_lo, SR_hi), RooFit::Save());
  // ---------------------
  // Envelope of functions
  // RooRealVar bg_p0_p("bg_p0_p", "bg_p0_p", bg_p0.getVal()+bg_p0.getError()/2.);
  // RooRealVar bg_p0_m("bg_p0_m", "bg_p0_m", bg_p0.getVal()-bg_p0.getError()/2.);
  // RooRealVar bg_p1_p("bg_p1_p", "bg_p1_p", bg_p1.getVal()+bg_p1.getError()/2.);
  // RooRealVar bg_p1_m("bg_p1_m", "bg_p1_m", bg_p1.getVal()-bg_p1.getError()/2.);
  // RooRealVar bg_p2_p("bg_p2_p", "bg_p2_p", bg_p2.getVal()+bg_p2.getError()/2.);
  // RooRealVar bg_p2_m("bg_p2_m", "bg_p2_m", bg_p2.getVal()-bg_p2.getError()/2.);
  // GaussExp bgEnv_p0_p("bgEnv_p0_p", "bgEnv_p0_p", x, bg_p0_p, bg_p1, bg_p2);
  // GaussExp bgEnv_p0_m("bgEnv_p0_m", "bgEnv_p0_m", x, bg_p0_m, bg_p1, bg_p2);
  // GaussExp bgEnv_p1_p("bgEnv_p1_p", "bgEnv_p1_p", x, bg_p0, bg_p1_p, bg_p2);
  // GaussExp bgEnv_p1_m("bgEnv_p1_m", "bgEnv_p1_m", x, bg_p0, bg_p1_m, bg_p2);
  // GaussExp bgEnv_p2_p("bgEnv_p2_p", "bgEnv_p2_p", x, bg_p0, bg_p1, bg_p2_p);
  // GaussExp bgEnv_p2_m("bgEnv_p2_m", "bgEnv_p2_m", x, bg_p0, bg_p1, bg_p2_m);
  // ---------------------
  // Multiplicative Polynomials
  RooRealVar bg_p3("bg_p3", "bg_p3", -10, 10);
  RooRealVar bg_p4("bg_p4", "bg_p4", -10, 10);
  RooRealVar bg_p5("bg_p5", "bg_p5", -10, 10);
  RooPlot *aC_plot=x.frame();
  pred.plotOn(aC_plot, RooFit::MarkerColor(kRed));
  bg.plotOn(aC_plot, RooFit::VisualizeError(*r_bg, sigmaVisual), RooFit::FillColor(kOrange));
  bg.plotOn(aC_plot, RooFit::LineColor(kRed));
  pred.plotOn(aC_plot, RooFit::LineColor(kRed), RooFit::MarkerColor(kRed));
  
  TCanvas *c_rooFit=new TCanvas("c_rooFit", "c_rooFit", 700, 700);
  TPad *p_2=new TPad("p_2", "p_2", 0, 0, 1, 0.35);
  TPad *p_1=new TPad("p_1", "p_1", 0, 0.35, 1, 1);
  p_1->SetBottomMargin(0.05);
  p_1->SetFillStyle(4000);
  p_1->SetFrameFillColor(0);
  p_2->SetFillStyle(4000);
  p_2->SetFrameFillColor(0);
  p_1->Draw();
  p_2->Draw();
  p_1->cd();
  if (tags!="MMMM_nominal") h_mMMMMa_3Tag_SR->Draw("Ep9 SAME");
  aC_plot->SetTitle("; m_{X} (GeV); Events / (10 GeV)");
  aC_plot->Draw();
  double x_mean_aC=bg_p0.getVal();
  double x_k_aC=bg_p0.getVal()+bg_p2.getVal()*bg_p1.getVal();
  TLine *l_mean_aC=new TLine(x_mean_aC, 0, x_mean_aC, h_mMMMMa_3Tag_SR_Prediction->GetMaximum()); l_mean_aC->SetLineColor(kRed); l_mean_aC->Draw();
  TLine *l_k_aC=new TLine(x_k_aC, 0, x_k_aC, h_mMMMMa_3Tag_SR_Prediction->GetMaximum()); l_k_aC->SetLineColor(kRed); l_k_aC->SetLineStyle(9); l_k_aC->Draw();
  
  // Prediction Curve with Shape Systematics
  double PaC_0=bg_p0.getVal();
  double PaC_1=bg_p1.getVal();
  double PaC_2=bg_p2.getVal();
  double dPaC_0=bg_p0.getError();
  double dPaC_1=bg_p1.getError();
  double dPaC_2=bg_p2.getError();
  double PaS_0, PaS_1, PaS_2;
  double dPaS_0, dPaS_1, dPaS_2;
  if (externalParameterPrediction)
  {
    PaS_0=externalPrediction_p0(PaC_0, dPaC_0, dPaS_0);
    PaS_1=externalPrediction_p1(PaC_1, dPaC_1, dPaS_1);
    PaS_2=externalPrediction_p2(PaC_2, dPaC_2, dPaS_2);
  }
  else
  {
    PaS_0=PaC_0*PbS_0/PbC_0;
    PaS_1=PaC_1*PbS_1/PbC_1;
    PaS_2=PaC_2*PbS_2/PbC_2;
    dPaS_0=PaS_0*quad((dPaC_0/PaC_0), (dPbS_0/PbS_0), (dPbC_0/PbC_0));
    dPaS_1=PaS_1*quad((dPaC_1/PaC_1), (dPbS_1/PbS_1), (dPbC_1/PbC_1));
    dPaS_2=PaS_2*quad((dPaC_2/PaC_2), (dPbS_2/PbS_2), (dPbC_2/PbC_2));
  }
  std::cout<<"(dPaC_0/PaC_0) = ("<<dPaC_0<<"/"<<PaC_0<<") = "<<(dPaC_0/PaC_0)<<"; (dPbS_0/PbS_0) = ("<<dPbS_0<<"/"<<PbS_0<<") = "<<(dPbS_0/PbS_0)<<"; (dPbC_0/PbC_0) = ("<<dPbC_0<<"/"<<PbC_0<<") = "<<(dPbC_0/PbC_0)<<std::endl;
  std::cout<<"(dPaC_1/PaC_1) = ("<<dPaC_1<<"/"<<PaC_1<<") = "<<(dPaC_1/PaC_1)<<"; (dPbS_1/PbS_1) = ("<<dPbS_1<<"/"<<PbS_1<<") = "<<(dPbS_1/PbS_1)<<"; (dPbC_1/PbC_1) = ("<<dPbC_1<<"/"<<PbC_1<<") = "<<(dPbC_1/PbC_1)<<std::endl; 
  std::cout<<"(dPaC_2/PaC_2) = ("<<dPaC_2<<"/"<<PaC_2<<") = "<<(dPaC_2/PaC_2)<<"; (dPbS_2/PbS_2) = ("<<dPbS_2<<"/"<<PbS_2<<") = "<<(dPbS_2/PbS_2)<<"; (dPbC_2/PbC_2) = ("<<dPbC_2<<"/"<<PbC_2<<") = "<<(dPbC_2/PbC_2)<<std::endl; 
  std::cout<<" Predicted PaS_0 = "<<PaS_0<<" +- "<<dPaS_0<<std::endl;
  std::cout<<" Predicted PaS_1 = "<<PaS_1<<" +- "<<dPaS_1<<std::endl;
  std::cout<<" Predicted PaS_2 = "<<PaS_2<<" +- "<<dPaS_2<<std::endl;
  RooRealVar *bg_pred0;
  RooRealVar *bg_pred1;
  RooRealVar *bg_pred2;
  // Let the mean and std dev float a bit
  // dPaS_0=dPaS_0*5.;
  // dPaS_1=dPaS_1*5.;
  // dPaS_2=dPaS_2*5.;
  std::cout<<"Parameter Ranges:"<<std::endl;
  std::cout<<" Predicted PaS_0 = "<<PaS_0<<" +- "<<dPaS_0<<std::endl;
  std::cout<<" Predicted PaS_1 = "<<PaS_1<<" +- "<<dPaS_1<<std::endl;
  std::cout<<" Predicted PaS_2 = "<<PaS_2<<" +- "<<dPaS_2<<std::endl;
  if (tags!="MMMM_nominal")
  { 
    bg_pred0=(new RooRealVar("bg_pred0", "bg_pred0", PaS_0-dPaS_0/2., PaS_0+dPaS_0/2.));
    bg_pred1=(new RooRealVar("bg_pred1", "bg_pred1", PaS_1-dPaS_1/2., PaS_1+dPaS_1/2.));
    bg_pred2=(new RooRealVar("bg_pred2", "bg_pred2", PaS_2-dPaS_2/2., PaS_2+dPaS_2/2.));
  }
  else
  {
    bg_pred0=(new RooRealVar("bg_pred0", "bg_pred0", PaS_0));  bg_pred0->setError(dPaS_0);
    bg_pred1=(new RooRealVar("bg_pred1", "bg_pred1", PaS_1));  bg_pred1->setError(dPaS_1);
    bg_pred2=(new RooRealVar("bg_pred2", "bg_pred2", PaS_2));  bg_pred2->setError(dPaS_2);
  }
  GaussExp bg_pred_init("background_init", "Background Predicted for Signal Region", x, *bg_pred0, *bg_pred1, *bg_pred2);
  GaussExp bg_pred("background", "Background Predicted for Signal Region", x, *bg_pred0, *bg_pred1, *bg_pred2);
  RooPlot *aS_plot=x.frame();
  if (tags!="MMMM_nominal")
  {
    RooDataHist unblind("unblind", "Signal Region", RooArgList(x), h_mMMMMa_3Tag_SR);
    unblind.plotOn(aS_plot, RooFit::LineColor(kBlue), RooFit::MarkerColor(kBlue));
    // bg_pred_init.plotOn(aS_plot, RooFit::LineColor(kGreen), RooFit::Range(SR_lo, SR_hi));
    RooFitResult *r_bg_pred=bg_pred.fitTo(unblind, RooFit::Range(SR_lo, SR_hi), RooFit::Save());
    bg_pred.plotOn(aS_plot, RooFit::VisualizeError(*r_bg_pred, sigmaVisual), RooFit::FillColor(kCyan));
    bg_pred.plotOn(aS_plot, RooFit::LineColor(kBlue));
    bg_pred.plotOn(aS_plot, RooFit::Name("r_bg_prediction"));
    unblind.plotOn(aS_plot, RooFit::LineColor(kBlue), RooFit::MarkerColor(kBlue));
    aS_plot->Draw("same");
  }
  else
  {
    bg_pred.plotOn(aC_plot, RooFit::LineColor(kGreen), RooFit::Range(SR_lo, SR_hi));
    aC_plot->Draw("same");
  }
  double x_mean_aS=bg_pred0->getVal();
  double x_k_aS=bg_pred0->getVal()+bg_pred2->getVal()*bg_pred1->getVal();
  TLine *l_mean_aS=new TLine(x_mean_aS, 0, x_mean_aS, h_mMMMMa_3Tag_SR_Prediction->GetMaximum()); l_mean_aS->SetLineColor(kBlue); l_mean_aS->Draw();
  TLine *l_k_aS=new TLine(x_k_aS, 0, x_k_aS, h_mMMMMa_3Tag_SR_Prediction->GetMaximum()); l_k_aS->SetLineColor(kBlue); l_k_aS->SetLineStyle(9); l_k_aS->Draw();
  
  std::cout<<" === === "<<std::endl;
  // std::cout<<"chi^2/ndof of bC = "<<bC_plot->chiSquare()<<std::endl;
  // std::cout<<"chi^2/ndof of bS = "<<bS_plot->chiSquare()<<std::endl;
  // std::cout<<"chi^2/ndof of aC = "<<aC_plot->chiSquare()<<std::endl;
  // std::cout<<"chi^2/ndof of aS = "<<aS_plot->chiSquare()<<std::endl;
  std::cout<<" === === "<<std::endl;
  // if (logPlot) p_1->SetLogy();
  
  TLatex * tPrel = new TLatex();
  tPrel->SetNDC();
  tPrel->SetTextColor(kBlack);
  tPrel->SetTextSize(0.04);
  tPrel->DrawLatex(0.1,0.95,"CMS Preliminary; #sqrt{s} =  8 TeV, L=17.928 fb^{-1}");
  
  p_2->cd();
  p_2->SetGridy();
  RooHist *hpull;
  hpull=aC_plot->pullHist();
  hpull->SetMinimum(-4); hpull->SetMaximum(6);
  RooPlot *frameP=x->frame();
  frameP->SetTitle("; m_{X} (GeV); (data-fit)/fit");
  frameP->addPlotable(hpull, "P");
  frameP->Draw();
  TLine *m_one_line=new TLine(SR_lo, 0, SR_hi, 0); m_one_line->Draw();
  
  if (logPlot) c_rooFit->SaveAs(("c_compareData_"+tags+"_SR_RooFit_GaussExp_LOG.png").c_str());
  else c_rooFit->SaveAs(("c_compareData_"+tags+"_SR_RooFit_GaussExp.png").c_str());
  // --- Ratio of function to data points ---
  /*
  RooCurve *f_bg_pred=(RooCurve*)aS_plot->findObject("r_bg_prediction");
  TH1F *h_ratio=(TH1F*)h_mMMMMa_3Tag_SR->Clone("h_ratio");
  for (unsigned int i=0; i<h_ratio->GetNbinsX(); ++i)
  {
    double fEval=f_bg_pred->Eval(h_mMMMMa_3Tag_SR->GetBinCenter(i));
    double data=h_mMMMMa_3Tag_SR->GetBinContent(i);
    // std::cout<<"i = "<<i<<", fEval = "<<fEval<<", data = "<<data<<std::endl;
    double binContent=(h_mMMMMa_3Tag_SR->GetBinContent(i))/(f_bg_pred->Eval(h_mMMMMa_3Tag_SR->GetBinCenter(i)));
    double binError=(h_mMMMMa_3Tag_SR->GetBinError(i))/(f_bg_pred->Eval(h_mMMMMa_3Tag_SR->GetBinCenter(i)));
    h_ratio->SetBinContent(i, binContent);
    h_ratio->SetBinError(i, binError);
  }
  h_ratio->GetXaxis()->SetRangeUser(SR_lo, SR_hi);
  h_ratio->SetMaximum(2.5); h_ratio->SetMinimum(-0.5);
  h_ratio->SetTitle("Data/Fit in SR; m_{X} (GeV); Data/Fit");
  h_ratio->Fit("pol1", "", "", SR_lo, SR_hi);
  TCanvas *c_DataFit=new TCanvas("c_DataFit", "c_DataFit", 1000, 700);
  h_ratio->Draw();
  c_DataFit->SaveAs(("c_DataFit_"+tags+"SR.png").c_str());
  */
  // ------------------------------------------
  
  RooWorkspace *w=new RooWorkspace("HbbHbb");
  w->import(bg_pred);
  w->SaveAs("w_background_GaussExp.root");
  
}
void BackgroundPrediction_Kinematic_SigmoidGaussExp()
{

  gROOT->SetStyle("Plain");
  gStyle->SetOptStat(000000000);
  
  if (bReg) tags=tags+"_bReg";
  
  const unsigned int nPoints=4;
  double mass[nPoints]={90., 107.5, 142.5, 160.};
  double n_SB[nPoints], n_SR[nPoints];
  double ratio[nPoints];
  double errorsY[nPoints], errorsX[nPoints];
  
  // === MMMM/b ===
  // TFile *f_MMMM_b=new TFile((tags+"/b/Histograms_8TeVData2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_b=new TFile((tags+"/b/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TFile *f_MMMM_b=new TFile((tags+"/b_KinFit/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_b=new TFile((tags+"/b/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TH1F *h_mX_CR2_b=(TH1F*)f_MMMM_b->Get("h_mX_CR2");
  TH1F *h_mX_CR4_b=(TH1F*)f_MMMM_b->Get("h_mX_CR4");
  TH1F *h_mX_SR_b=(TH1F*)f_MMMM_b->Get("h_mX_SR");
  n_SB[0]=(h_mX_CR2_b->GetSumOfWeights()+h_mX_CR4_b->GetSumOfWeights());
  n_SR[0]=h_mX_SR_b->GetSumOfWeights();
  
  // === MMMM/d ===
  // TFile *f_MMMM_d=new TFile((tags+"/d/Histograms_8TeVData2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_d=new TFile((tags+"/d/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TFile *f_MMMM_d=new TFile((tags+"/d/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TH1F *h_mX_CR2_d=(TH1F*)f_MMMM_d->Get("h_mX_CR2");
  TH1F *h_mX_CR4_d=(TH1F*)f_MMMM_d->Get("h_mX_CR4");
  TH1F *h_mX_SR_d=(TH1F*)f_MMMM_d->Get("h_mX_SR");
  n_SB[1]=(h_mX_CR2_d->GetSumOfWeights()+h_mX_CR4_d->GetSumOfWeights());
  n_SR[1]=h_mX_SR_d->GetSumOfWeights();
  
  // === MMMM/e ===
  // TFile *f_MMMM_e=new TFile((tags+"/e/Histograms_8TeVData2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_e=new TFile((tags+"/e/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TFile *f_MMMM_e=new TFile((tags+"/e/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TH1F *h_mX_CR2_e=(TH1F*)f_MMMM_e->Get("h_mX_CR2");
  TH1F *h_mX_CR4_e=(TH1F*)f_MMMM_e->Get("h_mX_CR4");
  TH1F *h_mX_SR_e=(TH1F*)f_MMMM_e->Get("h_mX_SR");
  n_SB[2]=(h_mX_CR2_e->GetSumOfWeights()+h_mX_CR4_e->GetSumOfWeights());
  n_SR[2]=h_mX_SR_e->GetSumOfWeights();
  
  // === MMMM/c ===
  // TFile *f_MMMM_c=new TFile((tags+"/c/Histograms_8TeVData2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_c=new TFile((tags+"/c/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TFile *f_MMMM_c=new TFile((tags+"/c/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TH1F *h_mX_CR2_c=(TH1F*)f_MMMM_c->Get("h_mX_CR2");
  TH1F *h_mX_CR4_c=(TH1F*)f_MMMM_c->Get("h_mX_CR4");
  TH1F *h_mX_SR_c=(TH1F*)f_MMMM_c->Get("h_mX_SR");
  n_SB[3]=(h_mX_CR2_c->GetSumOfWeights()+h_mX_CR4_c->GetSumOfWeights());
  n_SR[3]=h_mX_SR_c->GetSumOfWeights();
  
  // TFile *f_MMMM_a=new TFile((tags+"/a/Histograms_8TeVData2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_a=new TFile((tags+"/a/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  TFile *f_MMMM_a=new TFile((tags+"/a_KinFit/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  // TFile *f_MMMM_a=new TFile((tags+"/a/Histograms_BJetPlusX_Run2012BCD_Skim.root").c_str());
  
  for (unsigned int i=0; i<nPoints; ++i)
  {
    if (n_SB[i]>0)
    {
      ratio[i]=n_SR[i]/n_SB[i];
      errorsY[i]=ratio[i]*pow(1./n_SR[i]+1./n_SB[i], 0.5);
      errorsX[i]=0.;
    } else 
    {
      ratio[i]=1;
      errorsX[i]=0;
      errorsY[i]=0;
    }
  }
  
  std::cout<<"n_SR[2] = "<<n_SR[2]<<std::endl;
  std::cout<<"n_SB[2] = "<<n_SB[2]<<std::endl;
  
  TGraphErrors *g_ratio=new TGraphErrors(nPoints, mass, ratio, errorsX, errorsY);
  g_ratio->SetTitle("SR/SB ratio");
  TCanvas *c_ratio=new TCanvas("c_ratio", "c_ratio", 700, 700);
  g_ratio->SetMinimum(-1.); g_ratio->SetMaximum(2.);
  g_ratio->Draw("A*");
  TF1 *f_ratio=new TF1("f_ratio", "pol1");
  g_ratio->Fit(f_ratio);
  c_ratio->SaveAs(("c_ratio_"+tags+".png").c_str());
  
  double ratioAt125=f_ratio->Eval(125.);
  double errorAt125=(errorsY[0]+errorsY[1]+errorsY[2]+errorsY[3])/4.;
  
  // Just a hack for the current box selections
  ratioAt125=n_SR[0]/n_SB[0];
  
  std::cout<<"ratioAt125 = "<<ratioAt125<<" +- "<<errorAt125<<std::endl;
  
  std::cout<<" = MMMM b ======================================== "<<std::endl;
  TH1F *h_mMMMMb_3Tag_CR2=(TH1F*)f_MMMM_b->Get("h_mX_CR2");
  TH1F *h_mMMMMb_3Tag_CR4=(TH1F*)f_MMMM_b->Get("h_mX_CR4");
  TH1F *h_mMMMMb_3Tag_SR=(TH1F*)f_MMMM_b->Get("h_mX_SR");
  TH1F *h_mMMMMb_3Tag_CR24=(TH1F*)h_mMMMMb_3Tag_CR2->Clone("h_mX_CR24");
  h_mMMMMb_3Tag_CR24->Add(h_mMMMMb_3Tag_CR4);
  h_mMMMMb_3Tag_CR24->GetXaxis()->SetRangeUser(VR_lo, VR_hi);
  h_mMMMMb_3Tag_SR->GetXaxis()->SetRangeUser(VR_lo, VR_hi);
  h_mMMMMb_3Tag_CR24->Rebin(rebin);
  h_mMMMMb_3Tag_SR->Rebin(rebin);
  h_mMMMMb_3Tag_CR24->SetLineColor(kRed);
  h_mMMMMb_3Tag_SR->SetLineColor(kBlue);
  double bS=h_mMMMMb_3Tag_SR->GetSumOfWeights();
  double bC=h_mMMMMb_3Tag_CR24->GetSumOfWeights();
  std::cout<<"Number of events in MMMM b signal region = "<<bS<<std::endl;
  std::cout<<"bC = "<<bC<<", bS = "<<bS<<std::endl;
  h_mMMMMb_3Tag_SR->Scale(bC/bS);
  // Do the fits using RooFit
  gSystem->Load("../../PDFs/LowMassBackgroundPDF_cxx.so");
  RooRealVar x("x", "m_{X} (GeV)", VR_lo, VR_hi);
  // bC
  RooRealVar bC_p0("bC_p0", "bC_p0", 300., 500.);
  RooRealVar bC_p1("bC_p1", "bC_p1", 40., 100.1);
  RooRealVar bC_p2("bC_p2", "bC_p2", 0.1, 5.1);
  RooRealVar bC_p3("bC_p3", "bC_p3", 0., 1.);
  RooRealVar bC_p4("bC_p4", "bC_p4", 180., 350.);
  LowMassBackgroundPDF bC_fit("bC_fit", "bC_fit", x, bC_p0, bC_p1, bC_p2, bC_p3, bC_p4);
  RooDataHist bC_data("bC_data", "bC Data", RooArgList(x), h_mMMMMb_3Tag_CR24);
  bC_fit.fitTo(bC_data, RooFit::Range(VR_lo, VR_hi));
  RooPlot *bC_plot=x.frame();
  bC_data.plotOn(bC_plot, RooFit::MarkerColor(kRed));
  bC_fit.plotOn(bC_plot, RooFit::LineColor(kRed));
  
  // bC_plot->Draw();
  // return;
  
  // bS
  RooRealVar bS_p0("bS_p0", "bS_p0", 300., 500.);
  RooRealVar bS_p1("bS_p1", "bS_p1", 40., 100.1);
  RooRealVar bS_p2("bS_p2", "bS_p2", 0.1, 5.1);
  RooRealVar bS_p3("bS_p3", "bS_p3", 0., 1.);
  RooRealVar bS_p4("bS_p4", "bS_p4", 180., 350.);
  LowMassBackgroundPDF bS_fit("bS_fit", "bS_fit", x, bS_p0, bS_p1, bS_p2, bS_p3, bS_p4);
  RooDataHist bS_data("bS_data", "bS Data", RooArgList(x), h_mMMMMb_3Tag_SR);
  bS_fit.fitTo(bS_data, RooFit::Range(VR_lo, VR_hi));
  RooPlot *bS_plot=x.frame();
  bS_data.plotOn(bS_plot, RooFit::MarkerColor(kBlue));
  bS_fit.plotOn(bS_plot, RooFit::LineColor(kBlue));
  std::cout<<" === === "<<std::endl;
  std::cout<<"chi^2/ndof of bC = "<<bC_plot->chiSquare()<<std::endl;
  std::cout<<"chi^2/ndof of bS = "<<bS_plot->chiSquare()<<std::endl;
  std::cout<<" === === "<<std::endl;
  TCanvas *c_bC=comparePlots2(bC_plot, bS_plot, h_mMMMMb_3Tag_SR, h_mMMMMb_3Tag_CR24, "Kinematic Extrapolation in "+tags+" Validation Region of Data; m_{X} GeV");
  double x_mean_bC=bC_p0.getVal();
  double x_kHi_bC=bC_p0.getVal()+bC_p2.getVal()*bC_p1.getVal();
  double x_kLo_bC=bC_p4.getVal();
  TLine *l_mean_bC=new TLine(x_mean_bC, 0, x_mean_bC, h_mMMMMb_3Tag_CR24->GetMaximum()*0.8); l_mean_bC->SetLineColor(kRed); l_mean_bC->Draw();
  TLine *l_kHi_bC=new TLine(x_kHi_bC, 0, x_kHi_bC, h_mMMMMb_3Tag_CR24->GetMaximum()); l_kHi_bC->SetLineColor(kRed); l_kHi_bC->SetLineStyle(9); l_kHi_bC->Draw();
  TLine *l_kLo_bC=new TLine(x_kLo_bC, 0, x_kLo_bC, h_mMMMMb_3Tag_CR24->GetMaximum()); l_kLo_bC->SetLineColor(kRed); l_kLo_bC->SetLineStyle(9); l_kLo_bC->Draw();
  double x_mean_bS=bS_p0.getVal();
  double x_kHi_bS=bS_p0.getVal()+bS_p2.getVal()*bS_p1.getVal();
  double x_kLo_bS=bS_p4.getVal();
  TLine *l_mean_bS=new TLine(x_mean_bS, 0, x_mean_bS, h_mMMMMb_3Tag_SR->GetMaximum()); l_mean_bS->SetLineColor(kBlue); l_mean_bS->Draw();
  TLine *l_kHi_bS=new TLine(x_kHi_bS, 0, x_kHi_bS, h_mMMMMb_3Tag_SR->GetMaximum()); l_kHi_bS->SetLineColor(kBlue); l_kHi_bS->SetLineStyle(9); l_kHi_bS->Draw();
  TLine *l_kLo_bS=new TLine(x_kLo_bS, 0, x_kLo_bS, h_mMMMMb_3Tag_SR->GetMaximum()); l_kLo_bS->SetLineColor(kBlue); l_kLo_bS->SetLineStyle(9); l_kLo_bS->Draw();
  c_bC->SaveAs(("c_compareData_"+tags+"_VR_RooFit_GaussExp.png").c_str());
  
  return; 
  
  // Calculate Pi and DPi and dPi -- for shape systematics
  double PbC_0=bC_p0.getVal();
  double PbC_1=bC_p1.getVal();
  double PbC_2=bC_p2.getVal();
  double PbC_3=bC_p3.getVal();
  double PbC_4=bC_p4.getVal();
  double dPbC_0=bC_p0.getError();
  double dPbC_1=bC_p1.getError();
  double dPbC_2=bC_p2.getError();
  double dPbC_3=bC_p3.getError();
  double dPbC_4=bC_p4.getError();
  double PbS_0=bS_p0.getVal();
  double PbS_1=bS_p1.getVal();
  double PbS_2=bS_p2.getVal();
  double PbS_3=bS_p3.getVal();
  double PbS_4=bS_p4.getVal();
  double dPbS_0=bS_p0.getError();
  double dPbS_1=bS_p1.getError();
  double dPbS_2=bS_p2.getError();
  double dPbS_3=bS_p3.getError();
  double dPbS_4=bS_p4.getError();
  
  std::cout<<" = MMMM Background Prediction ==== "<<std::endl;
  TH1F *h_mMMMMa_3Tag_CR2=(TH1F*)f_MMMM_a->Get("h_mX_CR2");
  TH1F *h_mMMMMa_3Tag_CR4=(TH1F*)f_MMMM_a->Get("h_mX_CR4");
  TH1F *h_mMMMMa_3Tag_SR;
  if (tags!="MMMM") h_mMMMMa_3Tag_SR=(TH1F*)f_MMMM_a->Get("h_mX_SR");
  TH1F *h_mMMMMa_3Tag_CR24=(TH1F*)h_mMMMMa_3Tag_CR2->Clone("h_mX_CR24");
  h_mMMMMa_3Tag_CR24->Add(h_mMMMMa_3Tag_CR4);
  h_mMMMMa_3Tag_CR24->Rebin(rebin);
  h_mMMMMa_3Tag_CR24->SetLineColor(kBlack);
  if (tags!="MMMM") h_mMMMMa_3Tag_SR->Rebin(rebin);
  if (tags!="MMMM") h_mMMMMa_3Tag_SR->SetLineColor(kBlue);
  TH1F *h_mMMMMa_3Tag_SR_Prediction=(TH1F*)h_mMMMMa_3Tag_CR24->Clone("h_mMMMMa_3Tag_SR_Prediction");
  double aC=h_mMMMMa_3Tag_CR24->Integral(h_mMMMMa_3Tag_CR24->FindBin(VR_lo), h_mMMMMa_3Tag_CR24->FindBin(VR_hi));
  // Get the scale of the prediction right
  // std::cout<<"bS/bC = "<<bS/bC<<std::endl;
  std::cout<<"ratioAt125 = "<<ratioAt125<<", +- "<<errorAt125<<" (fract unc.) = "<<1.+errorAt125/ratioAt125<<std::endl;
  std::cout<<"Number of aC events in 18.6 /fb within the mass window = "<<h_mMMMMa_3Tag_SR_Prediction->Integral(h_mMMMMa_3Tag_SR_Prediction->FindBin(SR_lo), h_mMMMMa_3Tag_SR_Prediction->FindBin(SR_hi))<<std::endl;
  std::cout<<"Number of predicted events in 18.6 /fb within the mass window = "<<h_mMMMMa_3Tag_SR_Prediction->Integral(h_mMMMMa_3Tag_SR_Prediction->FindBin(SR_lo), h_mMMMMa_3Tag_SR_Prediction->FindBin(SR_hi))*ratioAt125<<std::endl;
  RooRealVar x("x", "m_{X} (GeV)", SR_lo, SR_hi);
  RooRealVar bg_p0("bg_p0", "bg_p0", 300., 450.);
  RooRealVar bg_p1("bg_p1", "bg_p1", 20., 90.);
  RooRealVar bg_p2("bg_p2", "bg_p2", 0.0, 10.1);
  RooRealVar bg_p3("bg_p3", "bg_p3", 0., 1.);
  RooRealVar bg_p4("bg_p4", "bg_p4", 200., 400.);
  LowMassBackgroundPDF bg("bg", "bg", x, bg_p0, bg_p1, bg_p2, bg_p3, bg_p4);
  RooDataHist pred("pred", "Prediction from SB", RooArgList(x), h_mMMMMa_3Tag_SR_Prediction);
  bg.fitTo(pred, RooFit::Range(SR_lo, SR_hi));
  RooPlot *aC_plot=x.frame();
  pred.plotOn(aC_plot, RooFit::LineColor(kRed), RooFit::MarkerColor(kRed));
  bg.plotOn(aC_plot, RooFit::LineColor(kRed));
  TCanvas *c_rooFit=new TCanvas("c_rooFit", "c_rooFit", 1000, 700);
  if (tags!="MMMM") h_mMMMMa_3Tag_SR->Draw("Ep9 SAME");
  aC_plot->Draw();
  double x_mean_aC=bg_p0.getVal();
  double x_kHi_aC=bg_p0.getVal()+bg_p2.getVal()*bg_p1.getVal();
  double x_kLo_aC=bg_p4.getVal();
  TLine *l_mean_aC=new TLine(x_mean_aC, 0, x_mean_aC, h_mMMMMa_3Tag_SR_Prediction->GetMaximum()); l_mean_aC->SetLineColor(kRed); l_mean_aC->Draw();
  TLine *l_kHi_aC=new TLine(x_kHi_aC, 0, x_kHi_aC, h_mMMMMb_3Tag_SR->GetMaximum()); l_kHi_aC->SetLineColor(kRed); l_kHi_aC->SetLineStyle(9); l_kHi_aC->Draw();
  TLine *l_kLo_aC=new TLine(x_kLo_aC, 0, x_kLo_aC, h_mMMMMb_3Tag_SR->GetMaximum()); l_kLo_aC->SetLineColor(kRed); l_kLo_aC->SetLineStyle(9); l_kLo_aC->Draw();
  
  std::cout<<"chi^2/ndof of aC = "<<aC_plot->chiSquare()<<std::endl;
  c_rooFit->SaveAs(("c_compareData_"+tags+"_SR_RooFit_GaussExp.png").c_str());
  
  // Prediction Curve with Shape Systematics
  double PaC_0=bg_p0.getVal();
  double PaC_1=bg_p1.getVal();
  double PaC_2=bg_p2.getVal();
  double PaC_3=bg_p3.getVal();
  double PaC_4=bg_p4.getVal();
  double dPaC_0=bg_p0.getError();
  double dPaC_1=bg_p1.getError();
  double dPaC_2=bg_p2.getError();
  double dPaC_3=bg_p3.getError();
  double dPaC_4=bg_p4.getError();
  double PaS_0=PaC_0*PbS_0/PbC_0;
  double PaS_1=PaC_1*PbS_1/PbC_1;
  double PaS_2=PaC_2*PbS_2/PbC_2;
  // double PaS_3=PaC_3*PbS_3/PbC_3;
  double PaS_3=PaC_3;
  // double PaS_4=PaC_4*PbS_4/PbC_4;
  double PaS_4=PaC_4;
  double dPaS_0=PaS_0*quad((dPaC_0/PaC_0), (dPbS_0/PbS_0), (dPbC_0/PbC_0));
  double dPaS_1=PaS_1*quad((dPaC_1/PaC_1), (dPbS_1/PbS_1), (dPbC_1/PbC_1));
  double dPaS_2=PaS_2*quad((dPaC_2/PaC_2), (dPbS_2/PbS_2), (dPbC_2/PbC_2));
  // double dPaS_3=PaS_3*quad((dPaC_3/PaC_3), (dPbS_3/PbS_3), (dPbC_3/PbC_3));
  double dPaS_3=dPaC_3;
  double dPaS_4=PaS_4*quad((dPaC_4/PaC_4), (dPbS_4/PbS_4), (dPbC_4/PbC_4));
  std::cout<<"(dPaC_0/PaC_0) = ("<<dPaC_0<<"/"<<PaC_0<<") = "<<(dPaC_0/PaC_0)<<"; (dPbS_0/PbS_0) = ("<<dPbS_0<<"/"<<PbS_0<<") = "<<(dPbS_0/PbS_0)<<"; (dPbC_0/PbC_0) = ("<<dPbC_0<<"/"<<PbC_0<<") = "<<(dPbC_0/PbC_0)<<std::endl;
  std::cout<<"(dPaC_1/PaC_1) = ("<<dPaC_1<<"/"<<PaC_1<<") = "<<(dPaC_1/PaC_1)<<"; (dPbS_1/PbS_1) = ("<<dPbS_1<<"/"<<PbS_1<<") = "<<(dPbS_1/PbS_1)<<"; (dPbC_1/PbC_1) = ("<<dPbC_1<<"/"<<PbC_1<<") = "<<(dPbC_1/PbC_1)<<std::endl; 
  std::cout<<"(dPaC_2/PaC_2) = ("<<dPaC_2<<"/"<<PaC_2<<") = "<<(dPaC_2/PaC_2)<<"; (dPbS_2/PbS_2) = ("<<dPbS_2<<"/"<<PbS_2<<") = "<<(dPbS_2/PbS_2)<<"; (dPbC_2/PbC_2) = ("<<dPbC_2<<"/"<<PbC_2<<") = "<<(dPbC_2/PbC_2)<<std::endl; 
  std::cout<<"(dPaC_3/PaC_3) = ("<<dPaC_3<<"/"<<PaC_3<<") = "<<(dPaC_3/PaC_3)<<"; (dPbS_3/PbS_3) = ("<<dPbS_3<<"/"<<PbS_3<<") = "<<(dPbS_3/PbS_3)<<"; (dPbC_3/PbC_3) = ("<<dPbC_3<<"/"<<PbC_3<<") = "<<(dPbC_3/PbC_3)<<std::endl;
  std::cout<<"(dPaC_4/PaC_4) = ("<<dPaC_4<<"/"<<PaC_4<<") = "<<(dPaC_4/PaC_4)<<"; (dPbS_4/PbS_4) = ("<<dPbS_4<<"/"<<PbS_4<<") = "<<(dPbS_4/PbS_4)<<"; (dPbC_4/PbC_4) = ("<<dPbC_4<<"/"<<PbC_4<<") = "<<(dPbC_4/PbC_4)<<std::endl;
  std::cout<<" Predicted PaS_0 = "<<PaS_0<<" +- "<<dPaS_0<<std::endl;
  std::cout<<" Predicted PaS_1 = "<<PaS_1<<" +- "<<dPaS_1<<std::endl;
  std::cout<<" Predicted PaS_2 = "<<PaS_2<<" +- "<<dPaS_2<<std::endl;
  std::cout<<" Predicted PaS_3 = "<<PaS_3<<" +- "<<dPaS_3<<std::endl;
  std::cout<<" Predicted PaS_4 = "<<PaS_4<<" +- "<<dPaS_4<<std::endl;
  RooRealVar bg_pred0;
  RooRealVar bg_pred1;
  RooRealVar bg_pred2;
  RooRealVar bg_pred3;
  RooRealVar bg_pred4;
  if (tags!="MMMM_nominal")
  {
    bg_pred0=new RooRealVar("bg_pred0", "bg_pred0", PaS_0-dPaS_0/2., PaS_0+dPaS_0/2.);
    bg_pred1=new RooRealVar("bg_pred1", "bg_pred1", PaS_1-dPaS_1/2., PaS_1+dPaS_1/2.);
    bg_pred2=new RooRealVar("bg_pred2", "bg_pred2", PaS_2-dPaS_2/2., PaS_2+dPaS_2/2.);
    bg_pred3=new RooRealVar("bg_pred3", "bg_pred3", PaS_3-dPaS_3/2., PaS_3+dPaS_3/2.);
    bg_pred4=new RooRealVar("bg_pred4", "bg_pred4", PaS_4-dPaS_4/2., PaS_4+dPaS_4/2.);
  }
  else
  {
    bg_pred0=new RooRealVar("bg_pred0", "bg_pred0", PaS_0);  bg_pred0.setError(dPaS_0);
    bg_pred1=new RooRealVar("bg_pred1", "bg_pred1", PaS_1);  bg_pred1.setError(dPaS_1);
    bg_pred2=new RooRealVar("bg_pred2", "bg_pred2", PaS_2);  bg_pred2.setError(dPaS_2);
    bg_pred3=new RooRealVar("bg_pred3", "bg_pred3", PaS_3);  bg_pred3.setError(dPaS_3);
    bg_pred4=new RooRealVar("bg_pred4", "bg_pred4", PaS_4);  bg_pred4.setError(dPaS_4);
  }
  LowMassBackgroundPDF bg_pred("background", "Background Predicted for Signal Region", x, bg_pred0, bg_pred1, bg_pred2, bg_pred3, bg_pred4);
  RooPlot *aS_plot=x.frame();
  if (tags!="MMMM_nominal")
  {
    RooDataHist unblind("unblind", "Signal Region", RooArgList(x), h_mMMMMa_3Tag_SR);
    unblind.plotOn(aS_plot, RooFit::LineColor(kBlue), RooFit::MarkerColor(kBlue));
    bg_pred.fitTo(unblind, RooFit::Range(SR_lo, SR_hi));
    bg_pred.plotOn(aS_plot, RooFit::LineColor(kBlue));
    aS_plot->Draw("same");
  }
  else
  {
    bg_pred.plotOn(aC_plot, RooFit::LineColor(kBlue), RooFit::Range(SR_lo, SR_hi));
    aC_plot->Draw("same");
  }
  double x_mean_aS=bg_pred0.getVal();
  double x_k_aS=bg_pred0.getVal()+bg_pred2.getVal()*bg_pred1.getVal();
  TLine *l_mean_aS=new TLine(x_mean_aS, 0, x_mean_aS, h_mMMMMa_3Tag_SR_Prediction->GetMaximum()); l_mean_aS->SetLineColor(kBlue); l_mean_aS->Draw();
  TLine *l_k_aS=new TLine(x_k_aS, 0, x_k_aS, h_mMMMMa_3Tag_SR_Prediction->GetMaximum()); l_k_aS->SetLineColor(kBlue); l_k_aS->SetLineStyle(9); l_k_aS->Draw();
  
  std::cout<<" === === "<<std::endl;
  std::cout<<"chi^2/ndof of bC = "<<bC_plot->chiSquare()<<std::endl;
  std::cout<<"chi^2/ndof of bS = "<<bS_plot->chiSquare()<<std::endl;
  std::cout<<"chi^2/ndof of aC = "<<aC_plot->chiSquare()<<std::endl;
  std::cout<<"chi^2/ndof of aS = "<<aS_plot->chiSquare()<<std::endl;
  std::cout<<" === === "<<std::endl;
   
  c_rooFit->SaveAs(("c_compareData_"+tags+"_SR_RooFit_GaussExp.png").c_str());

  RooWorkspace *w=new RooWorkspace("HbbHbb");
  w->import(bg_pred);
  w->SaveAs("w_background_LowMass.root"); 

 
     
}
示例#9
0
文件: use.c 项目: masoo/rogueclone2s
void
quaff(void)
{
    short ch;
    char buf[80];
    object *obj;

    ch = pack_letter(mesg[231], POTION);
    if (ch == CANCEL) {
	return;
    }
    if (!(obj = get_letter_object(ch))) {
	message(mesg[232], 0);
	return;
    }
    if (obj->what_is != POTION) {
	message(mesg[233], 0);
	return;
    }
    switch (obj->which_kind) {
    case INCREASE_STRENGTH:
	message(mesg[234], 0);
	rogue.str_current++;
	if (rogue.str_current > rogue.str_max) {
	    rogue.str_max = rogue.str_current;
	}
	break;
    case RESTORE_STRENGTH:
	rogue.str_current = rogue.str_max;
	message(mesg[235], 0);
	break;
    case HEALING:
	message(mesg[236], 0);
	potion_heal(0);
	break;
    case EXTRA_HEALING:
	message(mesg[237], 0);
	potion_heal(1);
	break;
    case POISON:
	if (!sustain_strength) {
	    rogue.str_current -= get_rand(1, 3);
	    if (rogue.str_current < 1) {
		rogue.str_current = 1;
	    }
	}
	message(mesg[238], 0);
	if (halluc) {
	    unhallucinate();
	}
	break;
    case RAISE_LEVEL:
	rogue.exp_points = level_points[rogue.exp - 1];
	add_exp(1, 1);
	break;
    case BLINDNESS:
	go_blind();
	break;
    case HALLUCINATION:
	message(mesg[239], 0);
	halluc += get_rand(500, 800);
	break;
    case DETECT_MONSTER:
	show_monsters();
	if (!(level_monsters.next_monster)) {
	    message(strange_feeling, 0);
	}
	break;
    case DETECT_OBJECTS:
	if (level_objects.next_object) {
	    if (!blind) {
		show_objects();
	    }
	} else {
	    message(strange_feeling, 0);
	}
	break;
    case CONFUSION:
	message((halluc ? mesg[240]
		 : mesg[241]), 0);
	confuse();
	break;
    case LEVITATION:
	message(mesg[242], 0);
	levitate += get_rand(15, 30);
	being_held = bear_trap = 0;
	break;
    case HASTE_SELF:
	message(mesg[243], 0);
	haste_self += get_rand(11, 21);
	if (!(haste_self % 2)) {
	    haste_self++;
	}
	break;
    case SEE_INVISIBLE:
	sprintf(buf, mesg[244], fruit);
	message(buf, 0);
	if (blind) {
	    unblind();
	}
	see_invisible = true;
	relight();
	break;
    }
    print_stats((STAT_STRENGTH | STAT_HP));
#if !defined( ORIGINAL )
    id_potions[obj->which_kind].id_status = IDENTIFIED;
#else /* ORIGINAL */
    if (id_potions[obj->which_kind].id_status != CALLED) {
	id_potions[obj->which_kind].id_status = IDENTIFIED;
    }
#endif /* ORIGINAL */
    vanish(obj, 1, &rogue.pack);
}
示例#10
0
/*
 * (zerogue 0.4.3) Applies the effect of a potion to the rogue.
 */
void apply_potion( unsigned short pkind )
{
	char buf[80] ;

	switch( pkind )
	{
		case INCREASE_STRENGTH:
			message( "You feel stronger now, what bulging muscles!", 0 ) ;
			rogue.str_current++ ;
			rogue.str_max++ ; // (zerogue 0.4.1)
			break ;
		case RESTORE_STRENGTH:
			rogue.str_current = rogue.str_max;
			message( "This tastes great, you feel warm all over.", 0 ) ;
			break ;
		case HEALING:
			message( "You begin to feel better.", 0 ) ;
			potion_heal(0) ;
			break ;
		case EXTRA_HEALING:
			message( "You begin to feel much better.", 0 ) ;
			potion_heal(1) ;
			break ;
		case POISON:
			if( !sustain_strength )
			{
				rogue.str_current -= get_rand( 1, 3 ) ;
				if( rogue.str_current < 1 )
					rogue.str_current = 1 ;
			}
			message( "You feel very sick now.", 0 ) ;
			if( halluc ) unhallucinate() ;
			break ;
		case RAISE_LEVEL:
			rogue.exp_points = level_points[rogue.exp - 1] ;
			add_exp( 1, 1 ) ;
			break ;
		case BLINDNESS:
			go_blind() ;
			break ;
		case HALLUCINATION:
			message( "Oh wow, everything seems so cosmic.", 0 ) ;
			halluc += get_rand( 500, 800 ) ;
			break ;
		case DETECT_MONSTER:
			show_monsters() ;
			if( !(level_monsters.next_monster) )
				message( strange_feeling, 0 ) ;
			break;
		case DETECT_OBJECTS:
			if( level_objects.next_object )
				if( !blind ) show_objects() ;
			else
				message( strange_feeling, 0 ) ;
			break;
		case CONFUSION:
			message( ( halluc ? "What a trippy feeling." :
			                    "You feel confused." ), 0 ) ;
			confuse() ;
			break;
		case LEVITATION:
			message( "You start to float in the air.", 0 ) ;
			levitate += get_rand( 15, 30 ) ;
			being_held = 0 ;
		   	bear_trap = 0 ;
			break ;
		case HASTE_SELF:
			message( "You feel yourself moving much faster.", 0 ) ;
			haste_self += get_rand( 11, 21 ) ;
			if( !(haste_self % 2) ) haste_self++ ;
			break ;
		case SEE_INVISIBLE:
			sprintf( buf, "Hmm ... this potion tastes like %sjuice.", fruit ) ;
			message( buf, 0 ) ;
			if( blind ) unblind() ;
			see_invisible = 1 ;
			relight() ;
			break ;
		case RESTORE_LEVEL:
			if( expmod < 0 )
			{
				// Restore lost hit points.
				short i ;
				for( i = 0 ; i > expmod ; i-- )
					rogue.hp_max += hp_raise() ;
				expmod = 0 ;  // Clear negative levels.
				message( "You feel your lost vitality returning.", 0 ) ;
			}
			else
				message( "Your thirst is thoroughly quenched.", 0 ) ;
			break ;
		default:
			message( "Nothing happens.", 0 ) ;
	}

	return ;
}
示例#11
0
	~blindfold() {
		unblind();
	}
示例#12
0
void PhiModel::DefineModel()
{
  if(par->debug) std::cout<<"Running: PhiModel::DefineModel()"<<std::endl;

  std::map<std::string,std::string> mod;
  std::map<std::string,std::string> Bmod;
  mod[Ds2PhiPi]="#phi#pi"; mod[Ds2KKPi]="KK#pi"; mod[Ds2KPiPi]="K#pi#pi";    mod[Ds2KKPi]="#pi#pi#pi";
  Bmod[DsD0]="D_{s}D^{0}"; Bmod[DsPhi]="D_{s}#phi";

  // --------- Yields for DsD0, in an array (as line 447 of Model.C) ---------------
  if(par->debug) std::cout<<"Running: PhiModel::DefineModel() --> Making RooRealVars"<<std::endl;
  
  for(std::vector<std::string>::iterator h=HelBinList.begin();h!=HelBinList.end();h++){ 
    for(std::vector<std::string>::iterator ds=DsBDTBinList.begin();ds!=DsBDTBinList.end();ds++){ 
      for(std::vector<std::string>::iterator ph=PhiBDTBinList.begin();ph!=PhiBDTBinList.end();ph++){ 
        for(std::vector<std::string>::iterator a=magnetList.begin();a!=magnetList.end();a++){
          for(std::vector<std::string>::iterator c=chargeList.begin();c!=chargeList.end();c++){
            yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2PhiPi][*c][*a]  = new RooRealVar(Form("yield_%s_peak_DsD0_Ds2PhiPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Peak D_{s}D^{0} (#phi#pi)",  1660, 0, 830000);
            yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2KKPi][*c][*a]   = new RooRealVar(Form("yield_%s_peak_DsD0_Ds2KKPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),   "Yield #phi Peak D_{s}D^{0} (KK#pi)",    1660, 0, 830000);
            yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2PiPiPi][*c][*a] = new RooRealVar(Form("yield_%s_peak_DsD0_Ds2PiPiPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()), "Yield #phi Peak D_{s}D^{0} (#pi#pi#pi)", 350, 0, 175000);
            yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2KPiPi][*c][*a]  = new RooRealVar(Form("yield_%s_peak_DsD0_Ds2KPiPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Peak D_{s}D^{0} (K#pi#pi)",   200, 0, 100000);

            yield_Phi_comb[*h][*ds][*ph][DsD0][Ds2PhiPi][*c][*a]  = new RooRealVar(Form("yield_%s_comb_DsD0_Ds2PhiPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Comb. D_{s}D^{0} (#phi#pi)",   710, 0, 600000);
            yield_Phi_comb[*h][*ds][*ph][DsD0][Ds2KKPi][*c][*a]   = new RooRealVar(Form("yield_%s_comb_DsD0_Ds2KKPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),   "Yield #phi Comb. D_{s}D^{0} (KK#pi)",     710, 0, 600000);
            yield_Phi_comb[*h][*ds][*ph][DsD0][Ds2PiPiPi][*c][*a] = new RooRealVar(Form("yield_%s_comb_DsD0_Ds2PiPiPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()), "Yield #phi Comb. D_{s}D^{0} (#pi#pi#pi)", 390, 0, 600000);
            yield_Phi_comb[*h][*ds][*ph][DsD0][Ds2KPiPi][*c][*a]  = new RooRealVar(Form("yield_%s_comb_DsD0_Ds2KPiPi_%s_%s_%s_%s_%s",particle_name[DsD0].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Comb. D_{s}D^{0} (K#pi#pi)",   200, 0, 600000);

            // ---------------
            // DsPhi variables
            // ---------------
            yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2PhiPi][*c][*a]  = new RooRealVar(Form("yield_%s_peak_DsPhi_Ds2PhiPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Peak D_{s}#phi (#phi#pi)",  1660, 0, 83000);
            yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2KKPi][*c][*a]   = new RooRealVar(Form("yield_%s_peak_DsPhi_Ds2KKPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),   "Yield #phi Peak D_{s}#phi (KK#pi)",    1660, 0, 83000);
            yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2PiPiPi][*c][*a] = new RooRealVar(Form("yield_%s_peak_DsPhi_Ds2PiPiPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()), "Yield #phi Peak D_{s}#phi (#pi#pi#pi)", 350, 0, 17500);
            yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2KPiPi][*c][*a]  = new RooRealVar(Form("yield_%s_peak_DsPhi_Ds2KPiPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Peak D_{s}#phi (K#pi#pi)",   200, 0, 10000);

            yield_Phi_comb[*h][*ds][*ph][DsPhi][Ds2PhiPi][*c][*a]  = new RooRealVar(Form("yield_%s_comb_DsPhi_Ds2PhiPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Comb. D_{s}#phi (#phi#pi)",   710, 0, 600000);
            yield_Phi_comb[*h][*ds][*ph][DsPhi][Ds2KKPi][*c][*a]   = new RooRealVar(Form("yield_%s_comb_DsPhi_Ds2KKPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),   "Yield #phi Comb. D_{s}#phi (KK#pi)",     710, 0, 600000);
            yield_Phi_comb[*h][*ds][*ph][DsPhi][Ds2PiPiPi][*c][*a] = new RooRealVar(Form("yield_%s_comb_DsPhi_Ds2PiPiPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()), "Yield #phi Comb. D_{s}#phi (#pi#pi#pi)", 390, 0, 600000);
            yield_Phi_comb[*h][*ds][*ph][DsPhi][Ds2KPiPi][*c][*a]  = new RooRealVar(Form("yield_%s_comb_DsPhi_Ds2KPiPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "Yield #phi Comb. D_{s}#phi (K#pi#pi)",   200, 0, 600000);


            // -------------------------------------------------------------------------

            if(par->debug) std::cout<<"Running: PhiModel::DefineModel() --> Setting up blinding"<<std::endl;
            // RooCategory used for blind/unblind switching.
            TString blind("blind"), unblind("unblind");
            RooCategory blindCatBu("blindCatBu","Bu blind state Category");
            blindCatBu.defineType(unblind, 0);
            blindCatBu.defineType(blind, 1);

            if (needsBlinding)
                blindCatBu.setLabel(blind);
            else
                blindCatBu.setLabel(unblind);

            
            if(needsBlinding){  
              Phi_yield[*h][*ds][*ph][DsD0][Ds2PhiPi][*c][*a]  = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2PhiPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsD0][Ds2KKPi][*c][*a]   = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2KKPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsD0][Ds2KPiPi][*c][*a]  = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2KPiPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsD0][Ds2PiPiPi][*c][*a] = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2PiPiPi][*c][*a];

              //Phi_yield[DsD0][Ds2PhiPi][*c][*a]  = new RooUnblindUniform(Form("B_nsig_DsD0_Ds2PhiPi_%s_%s",(*c).c_str(),(*a).c_str()),  "nsig Bu blind (#phi#pi)",  "nsigBuDsD0blindedPhiPi",  1000., *yield_Phi_peak[DsD0][Ds2PhiPi][*c][*a] );
              //Phi_yield[DsD0][Ds2KKPi][*c][*a]   = new RooUnblindUniform(Form("B_nsig_DsD0_Ds2KKPi_%s_%s",(*c).c_str(),(*a).c_str()),   "nsig Bu blind (KK#pi)",    "nsigBuDsD0blindedKKPi",   1000., *yield_Phi_peak[DsD0][Ds2KKPi][*c][*a]  );
              //Phi_yield[DsD0][Ds2PiPiPi][*c][*a] = new RooUnblindUniform(Form("B_nsig_DsD0_Ds2PiPiPi_%s_%s",(*c).c_str(),(*a).c_str()), "nsig Bu blind (#pi#pi#pi)","nsigBuDsD0blindedPiPiPi", 100., *yield_Phi_peak[DsD0][Ds2PiPiPi][*c][*a] );
              //Phi_yield[DsD0][Ds2KPiPi][*c][*a]  = new RooUnblindUniform(Form("B_nsig_DsD0_Ds2KPiPi_%s_%s",(*c).c_str(),(*a).c_str()),  "nsig Bu blind (K#pi#pi)",  "nsigBuDsD0blindedKPiPi",  100., *yield_Phi_peak[DsD0][Ds2KPiPi][*c][*a]  );
              
              // Only Blind DsPhi numbers...
              Phi_yield[*h][*ds][*ph][DsPhi][Ds2PhiPi][*c][*a]  = new RooUnblindUniform(Form("%s_nsig_DsPhi_Ds2PhiPi_%s_%s_%s_%s_%s", particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "nsig Bu blind (#phi#pi)",  "nsigBuDsPhiblindePhiPi",   40., *yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2PhiPi][*c][*a] );
              Phi_yield[*h][*ds][*ph][DsPhi][Ds2KKPi][*c][*a]   = new RooUnblindUniform(Form("%s_nsig_DsPhi_Ds2KKPi_%s_%s_%s_%s_%s",  particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "nsig Bu blind (KK#pi)",    "nsigBuDsPhiblindedKKPi",   40., *yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2KKPi][*c][*a]  );
              Phi_yield[*h][*ds][*ph][DsPhi][Ds2PiPiPi][*c][*a] = new RooUnblindUniform(Form("%s_nsig_DsPhi_Ds2PiPiPi_%s_%s_%s_%s_%s",particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "nsig Bu blind (#pi#pi#pi)","nsigBuDsPhiblindedPiPiPi", 20., *yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2PiPiPi][*c][*a]);
              Phi_yield[*h][*ds][*ph][DsPhi][Ds2KPiPi][*c][*a]  = new RooUnblindUniform(Form("%s_nsig_DsPhi_Ds2KPiPi_%s_%s_%s_%s_%s", particle_name[DsPhi].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*c).c_str(),(*a).c_str()),  "nsig Bu blind (K#pi#pi)",  "nsigBuDsPhiblindedKPiPi",  10., *yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2KPiPi][*c][*a] );
            } else {
              Phi_yield[*h][*ds][*ph][DsD0][Ds2PhiPi][*c][*a]  = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2PhiPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsD0][Ds2KKPi][*c][*a]   = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2KKPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsD0][Ds2KPiPi][*c][*a]  = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2KPiPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsD0][Ds2PiPiPi][*c][*a] = yield_Phi_peak[*h][*ds][*ph][DsD0][Ds2PiPiPi][*c][*a];

              Phi_yield[*h][*ds][*ph][DsPhi][Ds2PhiPi][*c][*a]  = yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2PhiPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsPhi][Ds2KKPi][*c][*a]   = yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2KKPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsPhi][Ds2KPiPi][*c][*a]  = yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2KPiPi][*c][*a];
              Phi_yield[*h][*ds][*ph][DsPhi][Ds2PiPiPi][*c][*a] = yield_Phi_peak[*h][*ds][*ph][DsPhi][Ds2PiPiPi][*c][*a];
            }
          } //end of loop over chargeList
        }  //end of loop over magnetList
      } 
    } 
  }



  // --------- Mean B mass ---------------

  double default_Phi=1019.461;
  double Phi_low    = 994.461;
  double Phi_high   = 1044.461;
  
  double default_D0 =1864.84;
  double D0_low     = 1839.84;
  double D0_high    = 1889.84;
  
  for(std::vector<std::string>::iterator h=allHelbinList.begin();h!=allHelbinList.end();h++){ 
    for(std::vector<std::string>::iterator ds=allDsBDTbinList.begin();ds!=allDsBDTbinList.end();ds++){ 
      for(std::vector<std::string>::iterator ph=allPhiBDTbinList.begin();ph!=allPhiBDTbinList.end();ph++){
        for(std::vector<std::string>::iterator b=allBmodeList.begin();b!=allBmodeList.end();b++){  
          for(std::vector<std::string>::iterator m=allmodeList.begin();m!=allmodeList.end();m++){
            bool isPhi = false;
            if((*b).c_str()==DsPhi) isPhi = true; 
            mean_Phi[*h][*ds][*ph][*b][*m][both][both] = new RooRealVar(Form("mean_%s_%s_%s_%s_%s_%s"           ,particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()), "Mean Phi mass", (isPhi?default_Phi:default_D0), (isPhi?Phi_low:D0_low), (isPhi?Phi_high:D0_high));
            mean_Phi[*h][*ds][*ph][*b][*m][plus][up]   = new RooRealVar(Form("mean_%s_%s_%s_%s_%s_plus_up_%s"   ,particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()), "Mean Phi mass", (isPhi?default_Phi:default_D0), (isPhi?Phi_low:D0_low), (isPhi?Phi_high:D0_high));
            mean_Phi[*h][*ds][*ph][*b][*m][minus][dn]  = new RooRealVar(Form("mean_%s_%s_%s_%s_%s_minus_dn_%s"  ,particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()), "Mean Phi mass", (isPhi?default_Phi:default_D0), (isPhi?Phi_low:D0_low), (isPhi?Phi_high:D0_high));
            mean_Phi[*h][*ds][*ph][*b][*m][plus][dn]   = new RooRealVar(Form("mean_%s_%s_%s_%s_%s_plus_dn_%s"   ,particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()), "Mean Phi mass", (isPhi?default_Phi:default_D0), (isPhi?Phi_low:D0_low), (isPhi?Phi_high:D0_high));
            mean_Phi[*h][*ds][*ph][*b][*m][minus][up]  = new RooRealVar(Form("mean_%s_%s_%s_%s_%s_minus_up_%s"  ,particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()), "Mean Phi mass", (isPhi?default_Phi:default_D0), (isPhi?Phi_low:D0_low), (isPhi?Phi_high:D0_high));
            mean_Phi[*h][*ds][*ph][*b][*m][plus][both] = new RooRealVar(Form("mean_%s_%s_%s_%s_%s_plus_both_%s" ,particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()), "Mean Phi mass", (isPhi?default_Phi:default_D0), (isPhi?Phi_low:D0_low), (isPhi?Phi_high:D0_high));
            mean_Phi[*h][*ds][*ph][*b][*m][minus][both]= new RooRealVar(Form("mean_%s_%s_%s_%s_%s_minus_both_%s",particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()), "Mean Phi mass", (isPhi?default_Phi:default_D0), (isPhi?Phi_low:D0_low), (isPhi?Phi_high:D0_high));
            mean_Phi[*h][*ds][*ph][*b][*m][both][up]		= mean_Phi[*h][*ds][*ph][*b][*m][both][both];
            mean_Phi[*h][*ds][*ph][*b][*m][both][dn]		= mean_Phi[*h][*ds][*ph][*b][*m][both][both];
            mean_Phi[*h][*ds][*ph][*b][Ds2KPiPi][both][both]	= mean_Phi[*h][*ds][*ph][*b][Ds2KKPi][both][both];
            mean_Phi[*h][*ds][*ph][*b][Ds2PiPiPi][both][both] = mean_Phi[*h][*ds][*ph][*b][Ds2KKPi][both][both];
            mean_Phi[*h][*ds][*ph][*b][Ds2PhiPi][both][both]  = mean_Phi[*h][*ds][*ph][*b][Ds2KKPi][both][both];
          }
        }
      }
    }
  }

  if(par->debug) std::cout<<"Running: PhiModel::DefineModel() --> Making more RooRealVars"<<std::endl;
  // --------- Signal width ---------------
  for(std::vector<std::string>::iterator h=allHelbinList.begin();h!=allHelbinList.end();h++){ 
    for(std::vector<std::string>::iterator ds=allDsBDTbinList.begin();ds!=allDsBDTbinList.end();ds++){ 
      for(std::vector<std::string>::iterator ph=allPhiBDTbinList.begin();ph!=allPhiBDTbinList.end();ph++){
        for(std::vector<std::string>::iterator b=allBmodeList.begin();b!=allBmodeList.end();b++){
          for(std::vector<std::string>::iterator m=allmodeList.begin();m!=allmodeList.end();m++){
            sigma_Phi[*h][*ds][*ph][*b][*m][both][both]  = new RooRealVar(Form("sigma_%s_%s_%s_%s_%s_%s",particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str())          ,Form("%s sigma",mod[*b].c_str()),10,  1,  20); //change D0 to D^0
            sigma_Phi[*h][*ds][*ph][*b][*m][plus][both]  = sigma_Phi[*h][*ds][*ph][*b][*m][both][both];
            sigma_Phi[*h][*ds][*ph][*b][*m][minus][both] = sigma_Phi[*h][*ds][*ph][*b][*m][both][both];
            sigma_Phi[*h][*ds][*ph][*b][*m][plus][up]    = new RooRealVar(Form("sigma_%s_%s_%s_%s_%s_%s_plus_up",particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str())  ,Form("%s sigma",mod[*b].c_str()),10,  1,  20);
            sigma_Phi[*h][*ds][*ph][*b][*m][plus][dn]    = new RooRealVar(Form("sigma_%s_%s_%s_%s_%s_%s_plus_dn",particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str())  ,Form("%s sigma",mod[*b].c_str()),10,  1,  20);
            sigma_Phi[*h][*ds][*ph][*b][*m][minus][up]   = new RooRealVar(Form("sigma_%s_%s_%s_%s_%s_%s_minus_up",particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()) ,Form("%s sigma",mod[*b].c_str()),10,  1,  20);//sigma_Phi[*m][minus][up] = sigma_Phi[*m][plus][dn];
            sigma_Phi[*h][*ds][*ph][*b][*m][minus][dn]   = new RooRealVar(Form("sigma_%s_%s_%s_%s_%s_%s_minus_dn",particle_name[*b].c_str(),(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str()) ,Form("%s sigma",mod[*b].c_str()),10,  1,  20);//sigma_Phi[*m][minus][dn] = sigma_Phi[*m][plus][up];
            sigma_Phi[*h][*ds][*ph][*b][*m][both][up]    = sigma_Phi[*h][*ds][*ph][*b][*m][both][both]; 
            sigma_Phi[*h][*ds][*ph][*b][*m][both][dn]    = sigma_Phi[*h][*ds][*ph][*b][*m][both][both];            
            sigma_Phi[*h][*ds][*ph][DsPhi][*m][both][dn] = sigma_Phi[*h][*ds][*ph][DsD0][*m][both][both];
            sigma_Phi[*h][*ds][*ph][DsPhi][*m][both][up] = sigma_Phi[*h][*ds][*ph][DsD0][*m][both][both];
          }
        }
      }
    }
  }

  // --------- Comb. background slope ---------------
  for(std::vector<std::string>::iterator h=allHelbinList.begin();h!=allHelbinList.end();h++){ 
    for(std::vector<std::string>::iterator ds=allDsBDTbinList.begin();ds!=allDsBDTbinList.end();ds++){ 
      for(std::vector<std::string>::iterator ph=allPhiBDTbinList.begin();ph!=allPhiBDTbinList.end();ph++){
        for(std::vector<std::string>::iterator b=allBmodeList.begin();b!=allBmodeList.end();b++){ 
          for(std::vector<std::string>::iterator m=allmodeList.begin();m!=allmodeList.end();m++){
            comb_a1[*h][*ds][*ph][*b][*m] = new RooRealVar(Form("comb_a1_%s_%s_%s_%s_%s",(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str())   ,Form("%s comb. a1",mod[*b].c_str()),  0.4,   -1.0, 1.0);
            comb_a2[*h][*ds][*ph][*b][*m] = new RooRealVar(Form("comb_a2_%s_%s_%s_%s_%s",(*h).c_str(),(*ds).c_str(),(*ph).c_str(),(*b).c_str(),(*m).c_str())   ,Form("%s comb. a2",mod[*b].c_str()), -0.004, -1.0, 1.0);
          }
        }
      }
    }
  }
  
  // --------- Fix RooRealVars to each other  -------------
  // ------------------------------------------------------
  // ------------------------------------------------------
   
  for(std::vector<std::string>::iterator h=allHelbinList.begin();h!=allHelbinList.end();h++){ 
    for(std::vector<std::string>::iterator ds=allDsBDTbinList.begin();ds!=allDsBDTbinList.end();ds++){ 
      for(std::vector<std::string>::iterator ph=allPhiBDTbinList.begin();ph!=allPhiBDTbinList.end();ph++){
        for(std::vector<std::string>::iterator b=allBmodeList.begin();b!=allBmodeList.end();b++){  
          for(std::vector<std::string>::iterator m=allmodeList.begin();m!=allmodeList.end();m++){           
 
            // Fix comb slope to be the same 
            comb_a1[*h][*ds][*ph][*b][*m] = comb_a1[*HelBinList.begin()][*DsBDTBinList.begin()][*PhiBDTBinList.begin()][*BmodeList.begin()][*modeList.begin()];
            comb_a2[*h][*ds][*ph][*b][*m] = comb_a2[*HelBinList.begin()][*DsBDTBinList.begin()][*PhiBDTBinList.begin()][*BmodeList.begin()][*modeList.begin()];

            for(std::vector<std::string>::iterator a=magnetList.begin();a!=magnetList.end();a++){
              for(std::vector<std::string>::iterator c=chargeList.begin();c!=chargeList.end();c++){
                
                // Use same mean value for all plots 
                mean_Phi[*h][*ds][*ph][*b][*m][*a][*c] = mean_Phi[*HelBinList.begin()][*DsBDTBinList.begin()][*PhiBDTBinList.begin()][*BmodeList.begin()][*modeList.begin()][both][both]; 

                // Allow sigma_Phi to vary between Ds decay modes
                sigma_Phi[*h][*ds][*ph][*b][*m][*a][*c] = sigma_Phi[*HelBinList.begin()][*DsBDTBinList.begin()][*PhiBDTBinList.begin()][*BmodeList.begin()][*m][*magnetList.begin()][*chargeList.begin()]; 

              }
            }
          }
        }
      }
    }
  }



  // --------- Define the simultaneous PDFs ---------------
  // ------------------------------------------------------
  // ------------------------------------------------------
  
  if(par->debug) std::cout<<"Running: PhiModel::DefineModel() --> Setting up simultaneous PDF"<<std::endl;
  sim = new RooSimultaneous("model","Simultaneous model",*cat);

  RooAbsPdf *pdf_peak = 0;
  RooAbsPdf *pdf_comb = 0;
  for(std::vector<std::string>::iterator h=HelBinList.begin();h!=HelBinList.end();h++){ 
    for(std::vector<std::string>::iterator ds=DsBDTBinList.begin();ds!=DsBDTBinList.end();ds++){ 
      for(std::vector<std::string>::iterator ph=PhiBDTBinList.begin();ph!=PhiBDTBinList.end();ph++){
        for(std::vector<std::string>::iterator b=BmodeList.begin();b!=BmodeList.end();b++){
          for(std::vector<std::string>::iterator m=modeList.begin();m!=modeList.end();m++){
            for(std::vector<std::string>::iterator a=magnetList.begin();a!=magnetList.end();a++){
              for(std::vector<std::string>::iterator c=chargeList.begin();c!=chargeList.end();c++){
                std::string tag=(*h)+underscore+(*ds)+underscore+(*ph)+underscore+(*b)+underscore+(*m)+underscore+(*c)+underscore+(*a);
                if(par->debug) std::cout<<"Running: PhiModel::DefineModel() --> Adding to sim pdf: "<< tag <<std::endl;
                
                //DsD0
                RooAbsReal* mub     = mean_Phi[*h][*ds][*ph][*b][*m][*c][*a];
                RooAbsReal* sig     = sigma_Phi[*h][*ds][*ph][*b][*m][*c][*a];
                pdf_peak  = new RooGaussian( Form("pdf_Phi_peak_%s",tag.c_str()), "", (((*b).c_str()==DsPhi)?*mPhi:*mD0), *mub, *sig );

                //comb bkg
                RooRealVar* comba1 = comb_a1[*h][*ds][*ph][*b][*m];
                RooRealVar* comba2 = comb_a2[*h][*ds][*ph][*b][*m];
                pdf_comb = new RooChebychev(Form("pdf_Phi_comb_%s",tag.c_str()),"",(((*b).c_str()==DsPhi)?*mPhi:*mD0),RooArgSet(*comba1,*comba2)) ;
              
                //List of PDFs and yields
                RooArgSet pdflist( *pdf_peak, *pdf_comb);
                RooArgSet nevents( *Phi_yield[*h][*ds][*ph][*b][*m][*c][*a], *yield_Phi_comb[*h][*ds][*ph][*b][*m][*c][*a]); 
                
                //Add to master PDF
                RooAddPdf* masterPdf       = new RooAddPdf(Form("masterPdf_Phi_%s",tag.c_str())       ,"",pdflist, nevents);
        	      
                std::stringstream str;
                str<<(*h)<<underscore<<(*ds)<<underscore<<(*ph)<<underscore<<(*b)<<underscore<<(*m)<<underscore<<(*c)<<underscore<<(*a);
                
                sim->addPdf(*masterPdf,str.str().c_str());

              }
            }
          } 
        }//closing of for loops over b, m, a and c
      }
    }
  } // closing loop over h
} //end of funcn DefineModel()