Remove clone from file

Dear experts,

I want to write only the hrspSel histogram in my output, but I have may others, probably coming from the TH1::Clone() that I called in the loop but I do not see why. Do you know I can do that, so keep only hrspSel histogram?
Right now in my output, I have the hrspSel selected fit, but also all the other histogram fits from the loop Blue, Green, Magenta, Orange.

Regards

[*]

void code(){

TFile* ofile = new TFile(“file.root”,“RECREATE”);

  TH1D* hrspCloneB;
  TH1D* hrspCloneO;
  TH1D* hrspCloneM;
  TH1D* hrspCloneG;

  TF1* testfit{0}; 
  map<double, TH1D*> mapChi2Fit;
  map<double, TH1D*> mapChi2FitB;
  map<double, TH1D*> mapChi2FitO;
  map<double, TH1D*> mapChi2FitM;
  map<double, TH1D*> mapChi2FitG;

  for(int i=0; i<10; i++){

modified_fdscb = new TF1("modified_fdscb", modified_fnc_dscb, 0., 2., maxPar);

////////////////
// fit case 4
////////////////
modified_fdscb->SetLineColor(kBlue); 
modified_fdscb->SetParameter(0, gausLeft->GetParameter(0)); 
...
hrsp->Fit("modified_fdscb", "R"); 
testfit = (TF1*) hrsp->GetListOfFunctions()->Last();
testfit->SetNpx(hrsp->GetNbinsX());
ks=hrsp->KolmogorovTest(testfit->GetHistogram()); 
hrspCloneB = (TH1D*) hrsp->Clone(); 
mapKsFitB[ks]=hrspCloneB; 

////////////////
// fit case 3
////////////////
modified_fdscb->SetLineColor(kOrange); 
if(polLeft)for(int i=6; i<=12; i++) modified_fdscb->FixParameter(i, 0); 


hrsp->Fit(“modified_fdscb”, “R”);
hrspCloneO = (TH1D*) hrsp->Clone();
mapKsFitO[ks]=hrspCloneO;

////////////////
// fit case 2
////////////////
modified_fdscb->SetLineColor(kMagenta); 
for(int i=19; i<=20; i++) modified_fdscb->FixParameter(i, 0);
hrsp->Fit("modified_fdscb", "R"); 


hrspCloneM = (TH1D*) hrsp->Clone();
mapKsFitM[ks]=hrspCloneM;

////////////////
// fit case 1
////////////////
modified_fdscb->SetLineColor(kGreen); 
for(int i=16; i<=17; i++) modified_fdscb->FixParameter(i, 0); 
hrsp->Fit("modified_fdscb", "R"); 


hrspCloneG = (TH1D*) hrsp->Clone();
mapKsFitG[ks]=hrspCloneG;
mapChi2FitG[chi2]=hrspCloneG;

  } // loop

if(mapKsFitB.size()!=0)mapKsFit[mapKsFitB.rbegin()->first]=mapKsFitB.rbegin()->second;
if(mapKsFitO.size()!=0)mapKsFit[mapKsFitO.rbegin()->first]=mapKsFitO.rbegin()->second;
if(mapKsFitM.size()!=0)mapKsFit[mapKsFitM.rbegin()->first]=mapKsFitM.rbegin()->second;
if(mapKsFitG.size()!=0)mapKsFit[mapKsFitG.rbegin()->first]=mapKsFitG.rbegin()->second;

  //
  TH1D* hrspSel;
  bool goodFit{false};
  if(mapKsFit.size()!=0 && mapKsFit.rbegin()->first>1) { 
goodFit=true;
hrspSel= (TH1D*)  mapKsFit.rbegin()->second->Clone();
cout<<"selected Ks: "<<mapKsFit.rbegin()->first<<endl;
  }
  else {
goodFit=false;
if(mapKsFit.size()!=0) {
  cout<<"all fit failed keeping lowest Ks!!\n"; 
  hrspSel= (TH1D*) mapKsFit.rbegin()->second->Clone(); 
}
else { cout<<"no fit at all, saving original hist!!\n"; hrspSel= (TH1D*) hrsp->Clone(); }
  }
 hrspSel->Write(); // I want to have only that one write in the output root file

  delete hrspCloneB;
  delete hrspCloneO;
  delete hrspCloneM;
  delete hrspCloneG;
  delete hrspSel;
  delete hrsp;

}

Hi,

when an histogram is created, a reference to it is automatically added to the list of in-memory objects for the current file or directory. The rationale is also to automatically take care of writing the histograms without any action by the user. This default behaviour can be changed by:

       myHistogram->SetDirectory(nullptr);

All the details can be found here: root.cern.ch/doc/master/classTH1.html

Cheers,
Danilo

Dear Danilo,

than you very much! it solved my problem.

Regards