A method of optimizing 4D cone beam computed tomography (4DCBCT) imaging, comprising: a. generating by a scanner projections of a target; b. forming by said scanner a cone beam computed tomography (CBCT) scan of said target from said projections, wherein said CBCT comprises a 3D image of said target; said method characterised by c. controlling by an appropriately programmed computer a rotation speed of a gantry and projection acquisition of a 4DCBCT in real-time according to a measured patient respiratory signal, wherein said 4DCBCT comprises multiple 3DCBCT images, wherein said control of said gantry rotation speed and projection acquisition of said 4DCBCT in real-time comprises: i. analyzing by sensors a patient's breathing pattern to determine a representative breathing trace; ii. separating the patients representative breathing trace into a number of phase or displacement based respiratory bins; iii. determining and applying by mathematical optimization techniques on said computer a gantry angle and a projection pulse rate schedule from said representative breathing cycle; iv. recomputing by said computer said gantry angle and a projection pulse rate schedule if said patient's breathing deviates from said representative breathing cycle; and v. continuing said projection acquisition according to said recomputed gantry angle and projection pulse rate schedule, wherein said real-time acquisition of said CBCT forms an optimized 4DCBCT. a. generating by a scanner projections of a target; b. forming by said scanner a cone beam computed tomography (CBCT) scan of said target from said projections, wherein said CBCT comprises a 3D image of said target; said method characterised by c. controlling by an appropriately programmed computer a rotation speed of a gantry and projection acquisition of a 4DCBCT in real-time according to a measured patient respiratory signal, wherein said 4DCBCT comprises multiple 3DCBCT images, wherein said control of said gantry rotation speed and projection acquisition of said 4DCBCT in real-time comprises: i. analyzing by sensors a patient's breathing pattern to determine a representative breathing trace; ii. separating the patients representative breathing trace into a number of phase or displacement based respiratory bins; iii. determining and applying by mathematical optimization techniques on said computer a gantry angle and a projection pulse rate schedule from said representative breathing cycle; iv. recomputing by said computer said gantry angle and a projection pulse rate schedule if said patient's breathing deviates from said representative breathing cycle; and v. continuing said projection acquisition according to said recomputed gantry angle and projection pulse rate schedule, i. analyzing by sensors a patient's breathing pattern to determine a representative breathing trace; ii. separating the patients representative breathing trace into a number of phase or displacement based respiratory bins; iii. determining and applying by mathematical optimization techniques on said computer a gantry angle and a projection pulse rate schedule from said representative breathing cycle; iv. recomputing by said computer said gantry angle and a projection pulse rate schedule if said patient's breathing deviates from said representative breathing cycle; and v. continuing said projection acquisition according to said recomputed gantry angle and projection pulse rate schedule,