Coughing up blood can be alarming. The blood can be coming from some part of your respiratory tract. Blood coming from elsewhere, such as your stomach, can mimic coughing up blood. It's important for your doctor to determine the site of the bleeding, and then determine why you're coughing up blood.
I am concerned that you are had upper mid-back pain that lasted for three weeks. Please give your doctor a call and report the back pain, even though it is gone, and the coughing up of blood. It is better to have this checked out.