Hadd error (THnsparse)

I have 20 root files and I can just synthesize them and the synthesis comes out to 10G, but it is fine.

Now the problem is that when I scale each root file, the root file hadd after the scale doesn’t work, with the following error

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

The errors is in the merge of THnsparse, but since the files which not scaled can be merged, I don’t think it’s a memory or file size issue. The only thing I did was to traverse a list inside each root, scale a factor, and then write the list to the new root. here is the simple code

TObject * h = 0 ; 
  for(Int_t i = 0; i < list->GetEntries()-5; i++) { 
    h = list->At(i);
    THnSparse* hnout;
       TH1* hout;
       if(i==40)  hnout = dynamic_cast<THnSparse*> (h);
       else       hout = dynamic_cast<TH1*> (h);
       if(i==40){    hnout->Scale(scale);cout<<"thnsparse"<<endl; }
        else{
          hout->Sumw2();
          hout->Scale(scale) ;
          cout<<"============Scale(scale)=========="<<scale<<endl;
        }
}
list->Write(Form("%s",listName.Data()),1);
  list->Clear();

I have absolutely no idea about the cause and solution of the problem, looking forward to your help!

Try:

TObject * h = 0 ; 
  for(Int_t i = 0; i < list->GetEntries()-5; i++) { 
    h = list->At(i);
    if ( THnSparse* hnout = dynamic_cast<THnSparse*> (h) )
    {
       hnout->Scale(scale);
       cout<<"thnsparse"<<endl;
    } else if ( TH1*hout = dynamic_cast<TH1*> (h) )
    {
       hout->Sumw2();
       hout->Scale(scale) ;
       cout<<"============Scale(scale)=========="<<scale<<endl;
    } else if (h) {
       cout<< "Type " << h->IsA()->GetName() << " is not support\n";
    }
}
list->Write(Form("%s",listName.Data()),1);
list->Clear();

Hi Pcanal,

Thanks for your reply!

Your suggested changes are great, but they don’t solve my problem, I’m not making an error in the data type discrimination, my THnsparse data is too large and after scaling a factor of 10^-6 to 10^-12, the hadd method doesn’t work to merge successfully, do you have any other ideas or suggestions?

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

Hi @liuha yes I see what the problem is: Scale() iterates over all bins, scaling all of them, even if they were originally empty in the sparse histogram. I.e. this is indeed a bug! I have created `THnSparse::Scale()` unsparsifies · Issue #13962 · root-project/root · GitHub

Thanks for reporting this! I would suggest to either hand in a PR fixing this for ROOT :slight_smile: or iterating over the filled bins scaling them one by one, instead of relying on Scale(). Sorry about that!

Cheers, Axel

1 Like