# memory leak in TTree-\>fill() (multitree)

**URL:** <https://root-forum.cern.ch/t/memory-leak-in-ttree-fill-multitree/48078>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [December 16, 2021, 7:53am UTC](https://root-forum.cern.ch/t/memory-leak-in-ttree-fill-multitree/48078 "2021-12-16T07:53:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mingzhe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@mingzhe](https://root-forum.cern.ch/u/mingzhe)\
**Post date:** [December 16, 2021, 7:53am UTC](https://root-forum.cern.ch/t/memory-leak-in-ttree-fill-multitree/48078/1 "2021-12-16T07:53:11Z")

</div>

Hello, I’m working on the analysis code to read a xAOD file and generate my own root file. However I have the problems with memory leak like this

```auto
Package.EventLoop INFO Memory increase/change during the job:
Package.EventLoop INFO - resident: 29.4749 kB/event (14708 kB total)
Package.EventLoop INFO - virtual : 1.32265 kB/event (660 kB total)
Package.EventLoop WARNING *
Package.EventLoop WARNING * A significant memory leak was detected
Package.EventLoop WARNING *

```

after some debugging, the memory leak is located at tree-\>fill(). but I can’t figure out why this happens, the branches in my trees are all float or bool or int, there should be no pointer related problem. But the case is if I comment out some “\*\*\*tree-\>Fill()”, there is no leak. And if I only fill the commented trees, the leak don’t exist either. I’m very confused.

The code is attached below:

```auto
StatusCode MyxAODAnalysis::initialize()
{
****
 TFile *outputFile = wk()->getOutputFile(m_outputName);
    ANA_CHECK(EventSave_TreeInitialize(outputFile));
    ANA_CHECK(EventInfo_TreeInitialize(outputFile));
    ANA_CHECK(xAODInfo_TreeInitialize(outputFile));

 ANA_CHECK(Electron_TreeInitialize(outputFile));
    ANA_CHECK(Electron_Calibration_TreeInitialize(outputFile));
    ANA_CHECK(Electron_Useless_TreeInitialize(outputFile));
    ANA_CHECK(Electron_Track_TreeInitialize(outputFile));
    ANA_CHECK(Electron_Isolation_TreeInitialize(outputFile));
    ANA_CHECK(Electron_Cluster_TreeInitialize(outputFile));
    ANA_CHECK(Electron_ShowerShape_TreeInitialize(outputFile));
    if (isMC)
    {
        ANA_CHECK(Electron_SF_TreeInitialize(outputFile));
        ANA_CHECK(Electron_Pt_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_ChargeMisIDSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumIDSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_TightID_ChargeMisIDSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_TightIDSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCLooseIso_ChargeMisIDSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCLooseIsoSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCLooseIso_TriggerEff_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCLooseIso_TriggerSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_TightID_FCLooseIso_ChargeMisIDSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_TightID_FCLooseIsoSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_TightID_FCLooseIso_TriggerEff_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_TightID_FCLooseIso_TriggerSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCTightIso_ChargeMisIDSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCTightIsoSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCTightIso_TriggerEff_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCTightIso_TriggerSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCTightIso_ECIDSLooseSF_Syst_TreeInitialize(outputFile));
        ANA_CHECK(Electron_MediumID_FCTightIso_ECIDSLoose_ChargeMisIDSF_Syst_TreeInitialize(outputFile));
}
****
}

one example for tree_initialize()

```

StatusCode MyxAODAnalysis::Electron\_TreeInitialize(TFile \*&outputfile)  
{  
ANA\_MSG\_INFO(“Electron\_Tree-INITIALIZE”);  
m\_Electron\_Tree = new TTree(“Electron\_Tree”, “Electron\_Tree”);  
m\_Electron\_Tree-\>SetDirectory(outputfile);

```
m_Electron_Tree->Branch("Electron_Calibrated_Pt", &Electron_Calibrated_Pt, "Electron_Calibrated_Pt/F");
m_Electron_Tree->Branch("Electron_Eta", &Electron_Eta, "Electron_Eta/F");
m_Electron_Tree->Branch("Electron_Phi", &Electron_Phi, "Electron_Phi/F");
m_Electron_Tree->Branch("Electron_Charge", &Electron_Charge, "Electron_Charge/F");
m_Electron_Tree->Branch("Electron_PassIDMedium", &Electron_PassIDMedium, "Electron_PassIDMedium/O");
m_Electron_Tree->Branch("Electron_PassIDLoose", &Electron_PassIDLoose, "Electron_PassIDLoose/O");
m_Electron_Tree->Branch("Electron_PassIDTight", &Electron_PassIDTight, "Electron_PassIDTight/O");
m_Electron_Tree->Branch("Electron_Track_dz0", &Electron_Track_dz0, "Electron_Track_dz0/F");
m_Electron_Tree->Branch("Electron_Track_d0sig", &Electron_Track_d0sig, "Electron_Track_d0sig/F");
m_Electron_Tree->Branch("Electron_MatchedTrigger", &Electron_MatchedTrigger, "Electron_MatchedTrigger/O");
m_Electron_Tree->Branch("Electron_PassECIDSLoose", &Electron_PassECIDSLoose, "Electron_PassECIDSLoose/O");
m_Electron_Tree->Branch("Electron_PassIso_FCLoose", &Electron_PassIso_FCLoose, "Electron_PassIso_FCLoose/O");
m_Electron_Tree->Branch("Electron_PassIso_FCTight", &Electron_PassIso_FCTight, "Electron_PassIso_FCTight/O");

return StatusCode::SUCCESS;

```

}

```auto

then for each event I have:

```

StatusCode MyxAODAnalysis::execute()  
{  
// ----------------  
// Containers  
// ----------------  
// retrieve the eventInfo object from the event store  
m\_store-\>clear();  
eventInfo = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(eventInfo, “EventInfo”));  
vertex = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(vertex, “PrimaryVertices”));  
const xAOD::JetContainer \*jets = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(jets, “AntiKt4EMPFlowJets”));  
const xAOD::JetContainer \*btag\_jets = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(btag\_jets, “AntiKt4EMPFlowJets\_BTagging201903”));  
const xAOD::MuonContainer \*muons = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(muons, “Muons”));  
const xAOD::ElectronContainer \*electrons = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(electrons, “Electrons”));  
const xAOD::ElectronContainer \*electronsfwd = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(electronsfwd, “ForwardElectrons”));  
ANA\_CHECK(evtStore()-\>retrieve(photons, “Photons”));  
ANA\_CHECK(evtStore()-\>retrieve(taus, “TauJets”));  
// retrieve the MET core  
const xAOD::MissingETContainer \*coreMet = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(coreMet, “MET\_Core\_AntiKt4EMPFlow”));  
// retrieve the MET association map  
const xAOD::MissingETAssociationMap \*metMap = nullptr;  
ANA\_CHECK(evtStore()-\>retrieve(metMap, “METAssoc\_AntiKt4EMPFlow”));  
if (isMC)  
{  
ANA\_CHECK(evtStore()-\>retrieve(truths, “TruthParticles”));  
ANA\_CHECK(evtStore()-\>retrieve(event\_truth, “TruthEvents”));  
ANA\_CHECK(evtStore()-\>retrieve(jet\_truth, “AntiKt4TruthJets”));  
}  
auto electrons\_shallowCopy = xAOD::shallowCopyContainer(\*electrons);  
std::unique\_ptrxAOD::ElectronContainer electronsSC(electrons\_shallowCopy.first);  
std::unique\_ptrxAOD::ShallowAuxContainer electronsAuxSC(electrons\_shallowCopy.second);  
xAOD::Electron \*electron\_Calib = nullptr;  
xAOD::Electron \*electron\_Calib\_S12Off = nullptr;

```
for (auto electronSC : *electronsSC)
{
    m_Electron_Tree->Fill();
    m_Electron_Calibration_Tree->Fill();
    m_Electron_Useless_Tree->Fill();
    m_Electron_Track_Tree->Fill();
    m_Electron_Isolation_Tree->Fill();
    m_Electron_Cluster_Tree->Fill();
    m_Electron_ShowerShape_Tree->Fill();
    m_Electron_SF_Tree->Fill();
    m_Electron_MediumID_ChargeMisIDSF_Syst_Tree->Fill();

    m_Electron_MediumIDSF_Syst_Tree->Fill();          
    m_Electron_TightID_ChargeMisIDSF_Syst_Tree->Fill();
    m_Electron_TightIDSF_Syst_Tree->Fill();            
    m_Electron_MediumID_FCLooseIso_ChargeMisIDSF_Syst_Tree->Fill();
    m_Electron_MediumID_FCLooseIsoSF_Syst_Tree->Fill();
    m_Electron_MediumID_FCLooseIso_TriggerEff_Syst_Tree->Fill();
    m_Electron_MediumID_FCLooseIso_TriggerSF_Syst_Tree->Fill();
    m_Electron_TightID_FCLooseIso_ChargeMisIDSF_Syst_Tree->Fill();//
    m_Electron_TightID_FCLooseIsoSF_Syst_Tree->Fill();
    m_Electron_TightID_FCLooseIso_TriggerEff_Syst_Tree->Fill();
    m_Electron_TightID_FCLooseIso_TriggerSF_Syst_Tree->Fill();
    m_Electron_MediumID_FCTightIso_ChargeMisIDSF_Syst_Tree->Fill();
    m_Electron_MediumID_FCTightIsoSF_Syst_Tree->Fill();
    m_Electron_MediumID_FCTightIso_TriggerEff_Syst_Tree->Fill();
    m_Electron_MediumID_FCTightIso_TriggerSF_Syst_Tree->Fill();
if (electron_Calib != nullptr)
        delete electron_Calib;
    electron_Calib = nullptr;
}

```

}  
//if (NElectrons \> 0)  
//ANA\_CHECK(LoopElectron(electrons));

```auto
currently for debugging I don't give the branches any value which are given in LoopElectrons(electrons)
feedbacks are realy welcomed !!!
```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [December 16, 2021, 8:48am UTC](https://root-forum.cern.ch/t/memory-leak-in-ttree-fill-multitree/48078/2 "2021-12-16T08:48:29Z")

</div>

Why do you fill tens of trees? Do you delete the trees in the end? Can you [run valgrind](https://root-forum.cern.ch/t/valgrind-and-root/28506) on your code and post the leak checker output?
