Rebin all the histogram in a Root File and save them to a new file

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?? :confused:

#include "TFile.h"
#include "TKey.h"
#include "TCollection.h"
#include "TIterator.h"
#include "TString.h"
#include "TH1.h"
#include "TH2.h"
#include "TCanvas.h"
#include <iostream>

void RebinHistos(){
    
  TFile* file = new TFile("file.root");
  TIter next(file->GetListOfKeys());
  TList* histogramList = new TList();
  TKey* key;
    
  while ((key = (TKey*)next())){
    if (key->InheritsFrom("TH2"))continue;
    TH2* h = (TH2*)key->ReadObj();
    histogramList->Add(h);
  }
  file->Close();
    
  TFile* newfile = new TFile("newfile.root","NEW");
  TIter liter(histogramList);
  TObject *obj = 0;
  while ((obj = liter.Next())!=0 ){
    TH2 *h = (TH2*)obj;
    h->Rebin2D(2,2);
    h->Write();  
  }
  newfile->Close();
}

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.

Thanks in advance,
Arturo

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)?

Hi, thanks a lot for your reply. I’m following this tutorial https://root.cern.ch/root/html/tutorials/io/loopdir.C.html there, they use the InheritsFrom.

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.

 TFile* file = TFile::Open("FitTemplate.root");
 TFile* fileout = TFile::Open("FitTemplate_rebinned.root","RECREATE");
 TIter next(file->GetListOfKeys());
 TKey* key0;
 while((key0=(TKey*)next())){
   TObject* obj = (TObject*)key0;
   if(!obj->InheritsFrom("TH2")) continue;
   TH2* h = (TH2*)obj;
   h->Rebin2D(2,2);
   h->Write();
}

 fileout->Write();
}

Any idea? Sorry, I know they could be simple-minded errors, I’m very new!
Thanks

You are still looking at the TKey itself instead of the object stored in the key. Call ReadObj as shown in my code!

Right!!:sweat_smile:
It’s working now!!:smile:

Thanks a looot!!

[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;

}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.