# Memory issues with reading and writing histograms to file

**URL:** <https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925>\
**Category:** Newbie\
**Created:** [October 20, 2020, 12:30pm UTC](https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925 "2020-10-20T12:30:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@Vangi](https://root-forum.cern.ch/u/Vangi)\
**Post date:** [October 20, 2020, 12:30pm UTC](https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925/1 "2020-10-20T12:30:25Z")

</div>

Dear ROOTers,  
For my analysis, I have a piece of code that works fine using nominal systematics. The structure of the code looks something like this

```auto
std::map < std::string, TH1*> histogram_map {};
//declare and fill histogram map with some histograms
//open output file
//(loop over events in file){
   // calculate weight of event
   // calculate some reconstructed objects
   // fill histograms with results
}
//save histograms
//close file

```

Now, I am trying to include different systematics into my analysis

```auto
std::map < std::string, TH1*> histogram_map {};
std::map < std::string, Double_t > syst_map{};
//declare and fill histogram map with some histograms
//open output file
//create folders for the different systematics
//save empty histograms inside the folders (same histograms as before, just for each systematic now)
(loop over events in file){
    //calculate some reconstructed objects
    for( auto iter : syst_map ){
         // calculate event weight for the systematic
         OpenFile->cd(iter.first.c_str());
         TDirectory *dir = gDirectory();
          //loop over the keys and replace the histogram_map histograms with the histograms (with n events) saved in the folder by doing
          TKey *key;
          while ( ( key = ( TKey* ) next() ) ){
              std::string get_name( key->GetName() );
              if( std::strncmp( key->GetClassName(), "TH1F", 4 ) == 0 )
                   histogram_map[get_name] = (TH1F*)dir->Get ( key->GetName() ) );
               //same for 2D, but with TH2F
          }
         //fill histogram_map histograms with the new event
         //loop over the histogram map and save the histograms again (n + 1 entries):
        (loop over histogram map)
               hist_iter.second->Write( iter->first.c_str(), TObject::kOverwrite );
    }
}
close file

```

Unfortunately, jobs submitted using this method get killed quickly due to reaching their used memory limit. I have found a workaround, which is closing the file after saving the empty histograms, and then opening and closing it inside the systematics loop every time, but this results in the script being painfully slow. I am able to submit separate jobs for separate systematics and hadd the files later on, but I would like to do all of the systematics at once, since this way I am doing the “calculate some reconstructed objects” step only once per event, and the number of submitted jobs will be a lot fewer (although a lot slower). Any suggestions are welcome.  
Thank you,  
Vangi

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 20, 2020, 12:38pm UTC](https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925/2 "2020-10-20T12:38:50Z")

</div>

Well, if you keep all histograms in memory, you might well hit the limit. Not sure what would be the best solution here… Maybe @eguiraud has an idea?

---

<div class="post-metadata">

**Author:** ![Vangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@Vangi](https://root-forum.cern.ch/u/Vangi)\
**Post date:** [October 20, 2020, 12:43pm UTC](https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925/3 "2020-10-20T12:43:16Z")

</div>

hello, @bellenot, thank you for your reply. I am not a ROOT/C++ expert, but I was hoping that the only histograms staying in the memory are the ones in the histogram\_map, for example “electron\_pt”, and that gets changed to “electron\_pt” for the appropriate systematic. The number of events for each systematic should be the same as the number of events without them (first version of the script), so I was hoping that nothing in addition was being kept in the memory, just replaced by the appropriate histogram read off from the file - please, correct me if I am wrong.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 20, 2020, 12:50pm UTC](https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925/4 "2020-10-20T12:50:37Z")

</div>

> [@Vangi](#):
>
> ```auto
> while ( ( key = ( TKey* ) next() ) ){
> std::string get_name( key->GetName() );
> if( std::strncmp( key->GetClassName(), "TH1F", 4 ) == 0 )
> histogram_map[get_name] = (TH1F*)dir->Get ( key->GetName() ) );
> //same for 2D, but with TH2F
> }
> 
> ```

Looking at the code, it looks to me that all histogram (here `TH1F`) are being read and kept in the `std::map`. Or did I miss something?

---

<div class="post-metadata">

**Author:** ![Vangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/8baadc/32.png) [@Vangi](https://root-forum.cern.ch/u/Vangi)\
**Post date:** [October 20, 2020, 1:00pm UTC](https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925/5 "2020-10-20T13:00:54Z")

</div>

They are, but they were being stored in the first version as well. After that, aren’t they replaced by another histogram? E.g. if I have the histogram\_map[“electron\_pt”] for the sytematic “weight\_pileup\_UP”, I save that in the “weight\_pileup\_UP” folder, then on the next systematic, “weight\_pileup\_DOWN”, isn’t the map entry for histogram\_map[“electron\_pt”] replaced by that “electron\_pt”, filled with the event entry, then stored again inside the “weight\_pileup\_DOWN” folder. So the histogram pair to the “electron\_pt” string is different, but still the same size as before, and the corresponding histogram associated with “weight\_pileup\_UP” should hopefully no longer be stored in the memory - at least this is what I am trying to do. In the initial script, I wasn’t saving the histograms inside a loop, I was storing them in memory (same size histogram\_map - again, hopefully) and saving at the end of the script without any memory issues.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 20, 2020, 1:07pm UTC](https://root-forum.cern.ch/t/memory-issues-with-reading-and-writing-histograms-to-file/41925/6 "2020-10-20T13:07:50Z")

</div>

I might be wrong (not sure how `std::map` handle this), but if you replace a pointer value by another one, that doesn’t mean the object pointed by the first one gets deleted. You should call `delete` on the first pointer value before replacing it, so you first delete the old object and then replace it with the new one…
