I am not sure how to phrase this question correctly, but this is what I am trying to do.
A single histogram can be plotted using cern ROOT with a following command,
(TH1F*)electron->Draw();
But I have tens of histograms named in a sequence, such as electron1, elecron2, electron3, etc, and I want to write a simple loop to plot them all. I tried using sprintf and a simple for loop, but ROOT doesn’t like it.
char name[20];
(TH1F*)electron->Draw();
for(int j=0;j<5;j++){
sprintf(name, "%s%d","electron",j);
(TH1F*)name->Draw("same");
}
What am I doing wrong?
Thanks in advance.
You need one additional step. As @twalberg says, you have a string, not an object pointer. For root what you can do is just change your code such that I add one additional line.
The extra line gets the referenced object by name from the local TDirectory. The cast is necessary such that the gDirectory is cast to the right kind of object.
When you use root interactively this happens magically behind the scenes.