l.MBH                = 5.307; !28/11/2017 F. Savary
Kmax_MBH             = 11.23;
Kmin_MBH             = 0.2460;!..to be reviewed
Imax_MBH             = 11850;
l.tcld              = 1;



!mbh_rm_mcs=0; ! =1 remove mcs
!mbh_rm_mcdo=0; ! =1 remove mco,mcd

MBH: RBEND, L := l.MBH, Kmax := Kmax_MBH, Kmin := Kmin_MBH, 
    Calib := Kmax_MBH / Imax_MBH;

tcld: RCOLLIMATOR, L=l.tcld;

if (mylhcbeam>2){
  bv_aux=-1;
} else {
  bv_aux=1;
};



install_mbh(mbm,mbn,irn,ira,irb): macro={
print, text="installing MBH in Qmbn  IRirn in the ARCirairb";

abh.airnirb=ab.airnirb/2;   kbh.airnirb=abh.airnirb/l.mbh;
abh.airairn=ab.airairn/2;   kbh.airairn=abh.airairn/l.mbh;

mbh.ambnrirn.b1:mbh, angle:= abh.airnirb, k0= kbh.airnirb,  polarity=+1;
mbh.bmbnrirn.b1:mbh, angle:= abh.airnirb, k0= kbh.airnirb,  polarity=+1;
mbh.ambnlirn.b2:mbh, angle:=-abh.airairn, k0=-kbh.airairn,  polarity=+1;
mbh.bmbnlirn.b2:mbh, angle:=-abh.airairn, k0=-kbh.airairn,  polarity=+1;
mbh.ambnrirn.b2:mbh, angle:=-abh.airnirb, k0=-kbh.airnirb,  polarity=+1;
mbh.bmbnrirn.b2:mbh, angle:=-abh.airnirb, k0=-kbh.airnirb,  polarity=+1;
mbh.ambnlirn.b1:mbh, angle:= abh.airairn, k0= kbh.airairn,  polarity=+1;
mbh.bmbnlirn.b1:mbh, angle:= abh.airairn, k0= kbh.airairn,  polarity=+1;


!tolerances from DS MB Q13-Q7
mbh_hsag=l.MBH/ab.a78*2*(1/cos(ab.a78/4)-1)/2; !half sagitta
g_MBH=0.017150;
r_MBH:=0.022-MBH_hsag;
r_tol_MBH=0.0009;
h_tol_MBH=0.0008;
v_tol_MBH=0.0005;


value,mbh_hsag,g_mbh,r_mbh;

mbh.ambnrirn.b1,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};
mbh.bmbnrirn.b1,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};
mbh.ambnlirn.b2,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};
mbh.bmbnlirn.b2,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};
mbh.ambnrirn.b2,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};
mbh.bmbnrirn.b2,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};
mbh.ambnlirn.b1,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};
mbh.bmbnlirn.b1,APERTYPE=RECTELLIPSE,APERTURE:={r_MBH,g_MBH,r_MBH,r_MBH},APER_TOL={r_tol_MBH,h_tol_MBH,v_tol_MBH};



tcld.mbnrirn.b1: tcld;
tcld.mbnlirn.b2: tcld;

s.tcld.mbnrirn.b1: marker;
s.tcld.mbnlirn.b2: marker;
e.tcld.mbnrirn.b1: marker;
e.tcld.mbnlirn.b2: marker;

pos_mbr=mb.mbmmbnrirn.b1->at;
pos_mbl=mb.mbmmbnlirn.b1->at;

value, pos_mbr, pos_mbl;

if(bv_aux == 1){
Option, -echo,warn,info;
seqedit,sequence=lhcb1;
remove,element=mb.mbmmbnrirn.b1;
remove,element=mb.mbmmbnlirn.b1;
if (mbh_rm_mcs == 1){
remove,element=mcs.mbmmbnrirn.b1;
remove,element=mcs.mbmmbnlirn.b1;
};
if (mbh_rm_mcdo == 1){
remove,element=mco.mbnlirn.b1;
remove,element=mco.mbnrirn.b1;
remove,element=mco.mbnlirn.b1;
remove,element=mco.mbnrirn.b1;
remove,element=mcd.mbnlirn.b1;
remove,element=mcd.mbnrirn.b1;
remove,element=mcd.mbnlirn.b1;
remove,element=mcd.mbnrirn.b1;
};
remove,element=mbh.ambnrirn.b1;
remove,element=mbh.ambnlirn.b1;
remove,element=s.tcld.mbnrirn.b1;
remove,element=tcld.mbnrirn.b1  ;
remove,element=e.tcld.mbnrirn.b1;
remove,element=mbh.bmbnrirn.b1   ;
remove,element=mbh.bmbnlirn.b1   ;
install,element=mbh.bmbnlirn.b1  ,at=pos_mbl-4.4,from=ipirn;
install,element=mbh.ambnlirn.b1  ,at=pos_mbl+4.4,from=ipirn;
install,element=mbh.ambnrirn.b1  ,at=pos_mbr-4.4,from=ipirn;
install,element=s.tcld.mbnrirn.b1,at=pos_mbr-0.5,from=ipirn;
install,element=tcld.mbnrirn.b1  ,at=pos_mbr    ,from=ipirn;
install,element=e.tcld.mbnrirn.b1,at=pos_mbr+0.5,from=ipirn;
install,element=mbh.bmbnrirn.b1  ,at=pos_mbr+4.4,from=ipirn;
endedit;
};

pos_mbl=mb.mbmmbnlirn.b2->at;
pos_mbr=mb.mbmmbnrirn.b2->at;

seqedit,sequence=lhcb2;
remove,element=mb.mbmmbnlirn.b2;
remove,element=mb.mbmmbnrirn.b2;
if (__mbmbm>0){
remove,element=mcs.mbmmbnrirn.b2;
remove,element=mcs.mbmmbnlirn.b2;
};
if (__mbmbm==1){
remove,element=mco.mbnlirn.b2;
remove,element=mco.mbnrirn.b2;
remove,element=mco.mbnlirn.b2;
remove,element=mco.mbnrirn.b2;
remove,element=mcd.mbnlirn.b2;
remove,element=mcd.mbnrirn.b2;
remove,element=mcd.mbnlirn.b2;
remove,element=mcd.mbnrirn.b2;
};
remove,element=mbh.ambnlirn.b2;
remove,element=mbh.ambnrirn.b2;
remove,element=s.tcld.mbnlirn.b2;
remove,element=s.tcld.mbnrirn.b2;
remove,element=tcld.mbnlirn.b2  ;
remove,element=tcld.mbnrirn.b2  ;
remove,element=e.tcld.mbnlirn.b2;
remove,element=e.tcld.mbnrirn.b2;
remove,element=mbh.bmbnlirn.b2   ;
remove,element=mbh.bmbnrirn.b2   ;
install,element=mbh.bmbnlirn.b2,  at=pos_mbl-bv_aux*4.4,from=ipirn;
install,element=s.tcld.mbnlirn.b2,at=pos_mbl-bv_aux*0.5,from=ipirn;
install,element=tcld.mbnlirn.b2  ,at=pos_mbl           ,from=ipirn;
install,element=e.tcld.mbnlirn.b2,at=pos_mbl+bv_aux*0.5,from=ipirn;
install,element=mbh.ambnlirn.b2,  at=pos_mbl+bv_aux*4.4,from=ipirn;
install,element=mbh.ambnrirn.b2,  at=pos_mbr-bv_aux*4.4,from=ipirn;
install,element=mbh.bmbnrirn.b2,  at=pos_mbr+bv_aux*4.4,from=ipirn;
endedit;
Option, -echo,-warn,-info;

!show,mbh.ambnrirn.b1;
!show,mbh.ambnlirn.b2;

};

seqedit,sequence=lhcb1;flatten;cycle,start=IP3;flatten;endedit;
seqedit,sequence=lhcb2;flatten;cycle,start=IP3;flatten;endedit;


if (mbh_ir1q8 >0){ exec,install_mbh(b,8 ,1,8,2); };
if (mbh_ir1q9 >0){ exec,install_mbh(b,9 ,1,8,2); };
if (mbh_ir1q10>0){ exec,install_mbh(b,10,1,8,2); };

if (mbh_ir2q8 >0){ exec,install_mbh(a,8 ,2,1,3); };
if (mbh_ir2q10>0){ exec,install_mbh(a,10,2,1,3); };
if (mbh_ir2q11>0){ exec,install_mbh(a,11,2,1,3); };

if (mbh_ir5q8 >0){ exec,install_mbh(b,8 ,5,4,6); };
if (mbh_ir5q9 >0){ exec,install_mbh(b,9 ,5,4,6); };
if (mbh_ir5q10>0){ exec,install_mbh(b,10,5,4,6); };

if (mbh_ir7q8 >0){ exec,install_mbh(b,8 ,7,6,8); };
if (mbh_ir7q10>0){ exec,install_mbh(b,10,7,6,8); };

if (mbh_ir7q8a >0){ exec,install_mbh(a,8 ,7,6,8); };
if (mbh_ir7q8b >0){ exec,install_mbh(b,8 ,7,6,8); };
if (mbh_ir7q9a >0){ exec,install_mbh(a,9 ,7,6,8); };
if (mbh_ir7q9b >0){ exec,install_mbh(b,9 ,7,6,8); };
if (mbh_ir7q10a >0){ exec,install_mbh(a,10 ,7,6,8); };
if (mbh_ir7q10b >0){ exec,install_mbh(b,10 ,7,6,8); };
