Upvote:4
Yes, you'll go through US customs and immigration in Vancouver (known as "preclearance"; source: US Customs and Border Patrol). That means that changing planes in San Francisco will be just like changing between two domestic flights. You don't mention the length of your layover in Vancouver; I hope it's long enough for you to get through preclearance.
50Β minutes is shorter than I'd like for a connection but, as long as your flight from Vancouver isn't more than a few minutes late, you'll make your connection. It'll be stressful but it should be OK. Your baggage will most likely make the connection, too. If your incoming flight is late, then Lufthansa will book you onto yet another alternative flight; if your baggage doesn't make the connection, it will be forwarded to you, probably the next day.