Troubles with TFile::Open()

Hello ROOTers,
I have some troubles when I try to open a root file into a loop.
Consider the following programm:

[code] vector < TH1D > TChannel_V, TbarChannel_V, TTBar_V, WJets_V, Data_V;
vector < TString > samples, observables, observableName, channels;
map < TString, double > obsMax, obsMin;
map < TString, TH1D > CHANNELS;
map <TString, vector > VECTORS;

channels.push_back(“TChannel”);
channels.push_back(“TTBar”);
channels.push_back(“TbarChannel”);
channels.push_back(“WJets”);

samples.push_back(“3J_1T_noSyst”);
samples.push_back(“3J_2T_noSyst”);
samples.push_back(“2J_0T_noSyst”);
samples.push_back(“2J_1T_noSyst”);

observables.push_back(“costhetalj”);
observables.push_back(“eta”);
observables.push_back(“topPt”);

observableName.push_back(“cos#theta^{*}”);
observableName.push_back("#eta_{lq}");
observableName.push_back(“P_{T,lb#nu}”);

obsMin[“eta”] = 0.;
obsMax[“eta”] = 5.;

obsMin[“costhetalj”] = -1.;
obsMax[“costhetalj”] = 1.;

obsMin[“topPt”] = 30.;
obsMax[“topPt”] = 180.;

for(size_t c=0;c<channels.size();++c){
TString channel = channels.at©;

TString folder = "8TeV/";
filename = (folder+channel+".root");
//    TFile *f = new TFile (filename,"Open");
//    TFile f (filename,"OPEN");
f = TFile::Open(filename);
if( !f->IsOpen() ){
  cout<< " WARNING FILE " << filename << endl;
  continue;
}  
for (size_t s=0;s<samples.size();++s){
  TString sample = samples.at(s);
  
  for (size_t o=0;o<observables.size();++o){
TString observable = observables.at(o);

TString lepton == "Mu";
TString histoname = observable+TString("_")+channel+TString("_")+sample+lepton;

TH1D Histo (histoname,histoname,nBins,obsMin[observable],obsMax[observable]);
CHANNELS[channel]=Histo;	

TString mupath = "TreesMu/"+channel+"_"+sample+"";
TString elepath = "TreesEle/"+channel+"_"+sample+"";
TH1D * tmp  = new TH1D("t","t",nBins,obsMin[observable],obsMax[observable]);
((TTree*)f->Get(mupath))->Project("t",observable,cut);
Histo.Add(tmp,1.);
VECTORS[channel].push_back(Histo);
delete tmp;
  }
}
f->Close("R");
delete f;

}
[/code]

Root crashes while entering for the second time in the first loop:

[quote]Root.exe(227,0x7fff70064cc0) malloc: *** error for object 0x101b3e9d0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug[/quote]

Is this problem related to the way I open the root file?
I’m using ROOT v5.34.

Thanks in advance,
Francesco

Hi,

The problem is likely the memory management of the histogram. In addition the code provide does a lot of (unnecessary) copy of histograms leading to significant waste of time, avoid using a vector of histogram objects (prefer a vector of pointers).

In the code you provided I can not understand the semantic. There is neither any ‘Write’ of the histograms nor any ‘clear’ of the vectors. Are the histogram supposed to be written to the file and then deleted or are they supposed to be written then kept (for some purpose not described here) or a little bit of both?

Philippe.

PS. The direct cause of the crash is that the histogram ‘belong’ to the TFile and are deleted during the TFile close.