Hi, I have 2 different root files, with different trees but same leaves. I want to draw the same variable for each of the root files in one histogram so that I can compare. The code for “extracting” one histogram out of the root file is:
void energy_top_susysignal(){
TFile * f = new TFile(“susysignal_total.root”);
TH1F * h1 = (TH1F*) f->Get(“signal_Tree”);
h1->Draw(“energytop1”);
}
I know about Draw(“same”), but here the argument of Draw is the leaf. How do I do it?
Ok Thank you very much. It worked. But since you plot them like that, how do I get to fix the axis range? I tried “t1->GetYaxis()->SetRangeUser(0,700);” but it doesn’t work.
This is looking terribly wrong and is one of the things I hate about ROOT/many people using ROOT.
A tree is NOT a histogram. Thus you are casting two things that don’t fit. Even if the C-style cast (i.e. (type)something) is used in many ROOT examples, I can only urge everyone not to use it. In this case that should be a dynamic_cast<TTree*> plus an additional check if the result is nullptr - and if it is, something fatal should be happening.
Wile’s solution using GetObject doesn’t have this problem but I dislike having separate declare + get steps.
So my solution (and IMHO something like this should be provided by ROOT) is something like this
template <typename T>
T* checkedGet(TFile *f, const char *name) {
auto obj = f->Get(name);
if (!obj) throw std::runtime_error((string("No object named ") + name + " in the file " + f->GetName()).c_str());
auto result = dynamic_cast<T*>(obj);
if (!result) throw std::runtime_error((string("Object named ") + name + " in the file " + f->GetName() + " is of a different type").c_str());
return result;
}
// use it:
auto tree = checkedGet<TTree>(f, "nameOfTree");