Hello everyone, I am very new with ROOT. I need to get all the histogram from a file.root and the rebin them and save them to a different ROOT file, something like newfile.root
Here is my code, however, I’m getting the famous “Break segmentation violation” Can anyone help me??
And additionally, I would prefer to have a function instead of this, I mean, something that I can pass a file.root and a rebining paratmeter and it gives the newfile.root with the rebinned histograms.
This logic is flawed: if the key is a TH2 (which is never the case), then skip this key. If it is not a TH2, then cast the TKey to a TH2?! You need to check if the object read inherits from TH2, not the TKey.
Do something along the lines:
auto file = TFile::Open("file.root");
auto fileOut = TFile::Open("file.rebinned.root", "recreate");
for (auto keyO : *file->GetListOfKeys()) {
auto key = static_cast<TKey*>(keyO);
auto obj = key->ReadObj();
if (!obj->InheritsFrom("TH2")) continue;
auto h2 = static_cast<TH2*>(obj);
RebinYourTH2AndSaveIt(h2);
}
fileOut->Write();
By the way: what is the InheritsFrom good for, in particular does it differ from testing dynamic_cast<T*>(obj)?
I have coded everything again, see below, and I don’t get compilation problem but when I looked in the new file, there aren’t histograms there, it’s empty.
[quote=“Arturo, post:1, topic:25720”]
And additionally, I would prefer to have a function instead of this, I mean, something that I can pass a file.root and a rebining parameter and it would return the newfile.root with the rebinned histograms.
Thanks in advance,
Arturo
[/quote]Can you give some hints to me of how I can do this?
I guess something like
TFile* RebinHistos (TFile* file, int rebin_number){
the previous code ...
return file.rebinned.root;
}