Example #1
0
void	choix()
{
  char	choice;
  char  *str;

  choice = ' ';
  while (choice != 'c' && choice != 'h' && choice != 'q' && choice != 'z' && choice != 'b' && choice != 'n')
  {
    str = readLine();
    choice = str[0];
    if (choice  == 'c')
      casu();
    else if (choice == 'h')
      hardcore();
    else if (choice == 'b')
      blind();
    else if (choice == 'n')
      noreturn();
    else if (choice == 'z') {
      help();
      choix();
    }
    else if (choice == 'q')
      my_putstr("Merci d'avoir jouer ! Au revoir !\n");
    else
      my_putstr("Rentrez une valeur correcte s'il vous plait\n");
  }
}
/*
  CARRY, TAKE etc.
*/
void ivtake()
{
    int anobj, item;

    anobj = 0;
    for (item = 1; item < MAXOBJ; ++item)
	if (g.place[item] == g.loc)
	    if (anobj == 0)
		anobj = item;
	    else {
		needobj();
		return;
	    }

    if (anobj == 0 || (dcheck() && g.dflag >= 2) || blind())
	needobj();
    else {
	object = anobj;
	if (verb == YANK)
	    vyank();
	else if (verb == WEAR)
	    vwear();
	else
	    vtake();
    }
    return;
}
Example #3
0
Time time(const JSON& contest) {
  Time ans;
  ans.begin = begin(contest);
  ans.end = end(contest);
  ans.freeze = freeze(contest);
  ans.blind = blind(contest);
  return ans;
}
Example #4
0
void    rejouerblind()
{
  char  choix;
  char  *str;

  my_putstr("Voulez-vous rejouez ? (y/n) ");
  choix = ' ';
  while (choix != 'y' && choix != 'n')
    {
      str = readLine();
      choix = str[0];
      if (choix == 'y')
        blind();
      else if (choix == 'n')
        my_putstr("Merci d'avoir jouer ! A bientot !\n");
      else
        my_putstr("Vous devez taper y ou n\n");
    }
}
Example #5
0
/*
  Routine to take 1 turn
*/
void turn()
{
    int i, hint;
    static int waste = 0;

    if (newtravel) {
        /* If closing, then he can't leave except via the main office. */
        if (outside(g.newloc) && g.newloc != 0 && g.closing) {
            rspeak(130);
            g.newloc = g.loc;
            if (!g.panic)
                g.clock2 = 15;
            g.panic = TRUE;
        }
        /* See if a dwarf has seen him and has come from where he wants
           to go. */
        if (g.newloc != g.loc && !forced(g.loc) && g.loc_attrib[g.loc] & NOPIRAT == 0)
            for (i = 1; i < (DWARFMAX - 1); ++i)
                if (g.odloc[i] == g.newloc && g.dseen[i]) {
                    g.newloc = g.loc;
                    rspeak(2);
                    break;
                }

        g.loc = g.newloc;
        dwarves();			/* & special dwarf(pirate who
					   steals)	 */

        /* Check for death */
        if (g.loc == 0) {
            death();
            return;
        }
        /* Check for forced move */
        if (forced(g.loc)) {
            desclg(g.loc);
            ++g.visited[g.loc];
            domove();
            return;
        }
        /* Check for wandering in dark */
        if (g.wzdark && dark() && pct(35)) {
            rspeak(23);
            g.oldloc2 = g.loc;
            death();
            return;
        }
        /* see if he is wasting his batteies out in the open */
        if (outside(g.loc) && g.prop[LAMP]) {
            waste++;
            if (waste > 11) {
                rspeak(324);
                waste = 0;
            }
        } else
            waste = 0;

        /* If wumpus is chasing stooge, see if wumpus gets him */
        if (g.chase) {
            g.chase++;
            g.prop[WUMPUS] = g.chase / 2;
            move(WUMPUS, g.loc);
            if (g.chase >= 10) {
                if (dark())
                    rspeak(270);
                pspeak(WUMPUS, 5);
                death();
                return;
            }
        }
        /* check for radiation poisoning. */
        g.health += (outside(g.loc)) ? 3 : 1;
        if (g.health > 100)
            g.health = 100;
        if (here(RADIUM) && (g.place[RADIUM] != -SHIELD || ajar(SHIELD)))
            g.health -= 7;
        if (g.health < 60) {
            rspeak(391 + (60 - g.health) / 10);
            if (g.health < 0) {
                death();
                return;
            }
        }
        if ((g.oldloc == 188) && (g.loc != 188 && g.loc != 189)
                && (g.prop[BOOTH] == 1)) {
            move(GNOME, 0);
            g.prop[BOOTH] = 0;
        }
        /* Describe his situation */
        describe();
        if (!blind()) {
            ++g.visited[g.loc];
            descitem();
        }
    }					/* end of newtravel start for
					   second entry point */
    /* Check if this location is eligible for any hints.  If been here
       long enough, branch to help section. Ignore "hints" < HNTMIN
       (special stuff, see database notes. */
    for (hint = HNTMIN; hint <= HNTMAX; hint++) {
        if (g.hinted[hint])
            continue;
        if (g.loc_attrib[g.loc] / 256 != hint - 6)
            g.hintlc[hint] = -1;
        g.hintlc[hint]++;
        if (g.hintlc[hint] >= g.hints[hint][1])
            do_hint(hint);
    }

    if (g.closed) {
        if (g.prop[OYSTER] < 0 && toting(OYSTER))
            pspeak(OYSTER, 1);
        for (i = 1; i < MAXOBJ; ++i)
            if (toting(i) && g.prop[i] < 0)
                g.prop[i] = -1 - g.prop[i];
    }
    g.wzdark = dark();
    if (g.knfloc > 0 && g.knfloc != g.loc)
        g.knfloc = 0;
    ++g.turns;
    i = rand();

    if (stimer())			/* as the grains of sand slip
					   by */
        return;

    while (!english())			/* retrieve player instructions	 */
        ;

    vrbx = 1;
    objx = objs[1] ? 1 : 0;
    iobx = iobjs[1] ? 1 : 0;
    verb = VAL(verbs[vrbx]);
    do {
        object = objx ? objs[objx] : 0;
        iobj = iobx ? iobjs[iobx] : 0;
        if (object && (objs[2] || iobjs[2])) {
            pspeak(object, -1);
            printf("      ");
        }
        switch (CLASS(verbs[vrbx])) {
        case MOTION:
            motion = verb;
            domove();
            break;
        case NOUN:
            bug(22);
        case ACTION:
            if (object || iobj)
                trverb();
            else
                itverb();
            break;
        case MISC:
            rspeak(verb);
            if (verb == 51)
                g.hinted[1] = TRUE;
            break;
        default:
            bug(22);
        }
        if (objx) {
            objx++;
            if (objs[objx] == 0)
                objx = 0;
        }
        if ((!objx || !objs[objx]) && iobx) {
            iobx++;
            if (iobjs[iobx] == 0)
                iobx = 0;
            if (iobx && iobjs[1])
                objx = 1;
        }
    } while (objx || iobx);
    return;
}
Example #6
0
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;
}
Example #7
0
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;
}
Example #8
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()