Error in <TBufferFile::WriteByteCount> while writing TH2F to file

Hello,

I need help with understanding why my program is failing with message Error in <TBufferFile::WriteByteCount>: bytecount too large (more than 1073741822) and terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped).

In brief, std::vector<TH2F*> contains 12 histograms, where every histogram is created by adding 20 histograms (h1->Add(h2)). Now when I am trying to write histogram (through loop over vector elements) to file, my program fails with:

Error in <TBufferFile::WriteByteCount>: bytecount too large (more than 1073741822) 
terminate called after throwing an instance of 'std::bad_alloc' 
what():  std::bad_alloc 
Aborted (core dumped).

Here is my code:

void TWCA(LEDplsSepByIntensity* selected_pulsesTWC){
  fOutTWC->cd();
  fOutTWC->mkdir(Form("%d",fSDC));
  fOutTWC->cd(Form("%d",fSDC));

  int Nch, IntensityID;
  float Charge, Time;
  TTree *tree = new TTree("T","Nch, time and charge of LED pulses");
  tree->Branch("IntensityID", &IntensityID, "IntensityID/I");
  tree->Branch("Nch", &Nch, "Nch/I");
  tree->Branch("Charge", &Charge, "Charge/F");
  tree->Branch("Time", &Time, "Time/F");



  for (UInt_t i=0; i < selected_pulsesTWC->size(); i++){
    for (UInt_t j=0; j < selected_pulsesTWC->at(i).size(); j++){
      TH2F* hist = new TH2F (Form("TvsChargeHistAtOM%d", j), Form("TvsChargeHistAtOM%d", j), 50000, 0.0, 1700.0, 8000, 0.0, 8000.0);
      if (selected_pulsesTWC->at(i).at(j).size() == 0) continue;
      for (UInt_t k = 0; k < selected_pulsesTWC->at(i).at(j).size(); k++ ){
        if (selected_pulsesTWC->at(i).at(j).at(k).Q != 0) {
          IntensityID = i;
          Nch = j;
          Charge = selected_pulsesTWC->at(i).at(j).at(k).Q;
          Time = selected_pulsesTWC->at(i).at(j).at(k).T;
          hist->Fill(Charge, Time);
          tree->Fill();
        }
      }
      if (i==0) {
        TvsChargeHist.push_back(hist);
      }
      if (i>0) {
        TvsChargeHist[j]->Add(hist);
        delete hist; hist = nullptr;
      }
    }
  }
  tree->Write();

  for (UInt_t i = 0; i < TvsChargeHist.size(); i++) {
    TvsChargeHist.at(i)->Write(); //Program terminates here!
  }
delete tree; tree = nullptr;
  fOutTWC->Close();
}

Maybe, adding histograms is not the best way in my situation, but can you guys help me to understand why the program terminates and fix? Thanks a lot in advance!

Try with: TH2F(..., 8500, 0., 1700., 8000, 0., 8000.)

Roughly speaking, for a TH2F, you need to make sure that: “(nbinsx + 2) * (nbinsy + 2) * (4 + 8) < 1073741822”
Note: “(4 + 8)” if the histogram has errors stored, otherwise just “(4)” (and for a TH2D it would be, respectively, “(8 + 8)” or “(8)”).

ROOT Forum → Search → 1073741822

Thank you very much! Now it works.

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