Back to basics: object ownership

ROOTers
This is what happen what I run the following code:

gROOT->Reset(); 


#include "TCanvas.h"
#include "TMath.h"
#include "TH1.h"
#include "TF1.h"
#include "TRandom.h"

#define    NSR     5   //N-Sigma range
#define    MYSTRSIZE 1024
#define XMIN 0
#define XMAX 8000
#define BASICINT 10000

#define MPPEAK 1  //Main photo peak
#define FSPEAK 2  //First Escape peak
#define SSPEAK 4  //Second escape

#define CnMOLECULES    1
#define OnMOLECULES    0
#define NnMOLECULES    0
#define SinMOLECULES   0
#define AlnMOLECULES   0
#define FenMOLECULES   0

int myColour=1;


//Returns ratio of main photo peak over first escape peak. This value is energy dependent
//To be implemented...
double GetFSoverMPratio(double mean){
  return 1.;
}

//Returns ratio of main photo peak over second escape peak. This value is energy dependent
//To be implemented...
double GetSSoverMPratio(double mean){
  return 0.5;
}

//My sigma table, energy dependent extracted from data collected using API summer 2010
double GetSigma(double mean){
  double sigmaVal=20;

  return sigmaVal;
}


// sfm referes to second-first-main photo peak: enable proper bit: main=1, first=2,second=4
void superPeak( THStack *hs, TH1F *hTotal, char* name, double amp, double mean, double sigma, int nMolecules=1, int sfm=1){

  char hname[MYSTRSIZE];
  char fname[MYSTRSIZE];
  TH1F *h1;
  TF1 *f1;

  if(!nMolecules)
    return;

  if(sfm & 0x1){
    sprintf(hname,"mh%s_%4.0f",name,mean);
    sprintf(fname,"mf%s_%4.0f",name,mean);
    h1 = new TH1F(hname,name,2048,XMIN,XMAX);
    f1 = new TF1(fname,"gaus",mean-NSR*sigma,mean+NSR*sigma);
    f1->SetParameters(amp,mean,sigma);
    h1->FillRandom(fname,nMolecules*BASICINT);
    h1->SetFillColor(myColour);
    h1->SetMarkerStyle(21);
    h1->SetMarkerColor(myColour);
    hs->Add(h1);
    hTotal->Add(h1);
  }
  if(sfm & 0x2){
    mean-=511;
    sprintf(hname,"fh%s_%4.0f",name,mean);
    sprintf(fname,"ff%s_%4.0f",name,mean);
    h1 = new TH1F(hname,name,2048,XMIN,XMAX);
    f1 = new TF1(fname,"gaus",mean-NSR*sigma,mean+NSR*sigma);
    f1->SetParameters(amp*GetFSoverMPratio(mean),mean,sigma);
    h1->FillRandom(fname,nMolecules*BASICINT);
    h1->SetFillColor(myColour);
    h1->SetMarkerStyle(21);
    h1->SetMarkerColor(myColour);
    hs->Add(h1);
    hTotal->Add(h1);
  }
  if(sfm & 0x4){
    mean-=511;
    sprintf(hname,"sh%s_%4.0f",name,mean);
    sprintf(fname,"sf%s_%4.0f",name,mean);
    h1 = new TH1F(hname,name,2048,XMIN,XMAX);
    f1 = new TF1(fname,"gaus",mean-NSR*sigma,mean+NSR*sigma);
    f1->SetParameters(amp*GetSSoverMPratio(mean),mean,sigma);
    h1->FillRandom(fname,nMolecules*BASICINT);
    h1->SetFillColor(myColour);
    h1->SetMarkerStyle(21);
    h1->SetMarkerColor(myColour);
    hs->Add(h1);
    hTotal->Add(h1);
  }

  myColour++;
}

void npeaks() {

  

   TH1F *hTotal = new TH1F("hTotal","Total stack",2048,XMIN,XMAX);
   THStack *hs = new THStack("hs","Stacked 1D histograms");
   superPeak(hs,hTotal,"Carbon",   160,4439,30,CnMOLECULES,MPPEAK + FSPEAK + SSPEAK);
   superPeak(hs,hTotal,"Oxygen",   30, 2740,30,OnMOLECULES,7);
   superPeak(hs,hTotal,"Oxygen",   20, 3080,30,OnMOLECULES,7);
   superPeak(hs,hTotal,"Oxygen",   40, 3680,30,OnMOLECULES,7);
   superPeak(hs,hTotal,"Oxygen",   20, 3850,30,OnMOLECULES,7);
   superPeak(hs,hTotal,"Oxygen",   20, 4440,30,OnMOLECULES,7);
   superPeak(hs,hTotal,"Oxygen",   90, 6130,30,OnMOLECULES,7);
   superPeak(hs,hTotal,"Oxygen",   30, 6920,30,OnMOLECULES,7);
   superPeak(hs,hTotal,"Oxygen",   30, 7120,30,OnMOLECULES,7);

   superPeak(hs,hTotal,"Silica",   400,1780,30,SinMOLECULES,7);
   superPeak(hs,hTotal,"Silica",   60, 2840,30,SinMOLECULES,7);
   superPeak(hs,hTotal,"Silica",   12, 3200,30,SinMOLECULES,7);
   superPeak(hs,hTotal,"Silica",   20, 4500,30,SinMOLECULES,7);
   superPeak(hs,hTotal,"Silica",   40, 5100,30,SinMOLECULES,7);
   superPeak(hs,hTotal,"Silica",   60, 6880,30,SinMOLECULES,7);
   superPeak(hs,hTotal,"Silica",   30, 7400,30,SinMOLECULES,7);

   superPeak(hs,hTotal,"Nitrogen", 20, 1630,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 17, 2120,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 50, 2310,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 22, 3680,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 57, 4440,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 10, 5020,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 30, 5100,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 15, 6730,30,NnMOLECULES,7);
   superPeak(hs,hTotal,"Nitrogen", 40, 7030,30,NnMOLECULES,7);
         
   superPeak(hs,hTotal,"Iron",     500, 847,30,FenMOLECULES,7);
   superPeak(hs,hTotal,"Iron",     340,1240,30,FenMOLECULES,7);
   superPeak(hs,hTotal,"Iron",     60, 1810,30,FenMOLECULES,7);
   
   superPeak(hs,hTotal,"Aluminum", 30,  843,30,AlnMOLECULES,7);
   superPeak(hs,hTotal,"Aluminum", 80, 1010,30,AlnMOLECULES,7);
   superPeak(hs,hTotal,"Aluminum", 30, 1720,30,AlnMOLECULES,7);
   superPeak(hs,hTotal,"Aluminum", 120,1810,30,AlnMOLECULES,7);
   superPeak(hs,hTotal,"Aluminum", 120,2210,30,AlnMOLECULES,7);
   superPeak(hs,hTotal,"Aluminum", 80, 3000,30,AlnMOLECULES,7);


   TCanvas *cst = new TCanvas("cst","stacked hists",10,10,700,700);
   cst->SetFillColor(41);
   cst->Divide(2,2);
   // in top left pad, draw the stack with defaults
   cst->cd(1);
   hs->Draw();
   // in top right pad, draw the stack in non-stack mode 
   // and errors option
   cst->cd(2);
   gPad->SetGrid();
   hs->Draw("nostack,e1p");
   //in bottom left, draw in stack mode with "lego1" option
   cst->cd(3);
   gPad->SetFrameFillColor(17);
   gPad->SetTheta(3.77);
   gPad->SetPhi(2.9);
   hs->Draw("lego1");
   //DRAW total signal
   cst->cd(4);
   hTotal->Draw();

   //============================================================

 
              
}

[quote]bash-3.2$ root npeaks.C


  •                                     *
    
  •    W E L C O M E  to  R O O T       *
    
  •                                     *
    
  • Version 5.25/02 29 September 2009 *
  •                                     *
    
  • You are welcome to visit our Web site *
  •      [root.cern.ch](http://root.cern.ch)            *
    
  •                                     *
    

ROOT 5.25/02 (trunk@30530, Sep 29 2009, 15:28:19 on linuxx8664gcc)

CINT/ROOT C/C++ Interpreter version 5.17.00, Dec 21, 2008
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0]
Processing npeaks.C…
root [1] .x npeaks.C
Warning in TROOT::Append: Replacing existing TH1: hTotal (Potential memory leak).
Warning in TROOT::Append: Replacing existing TH1: mhCarbon_4439 (Potential memory leak).
Warning in TROOT::Append: Replacing existing TH1: fhCarbon_3928 (Potential memory leak).
Warning in TROOT::Append: Replacing existing TH1: shCarbon_3417 (Potential memory leak).
Warning in TCanvas::Constructor: Deleting canvas with same name: cst
root [2]
[/quote]

When I create a new histogram or any root object, they get attached to the current directory.
I can see when my session ends, I still have objects that were not deleted. I thought ROOT would delete them or maybe gSystem->Reset() would do the dirty job. My question is, to remove these error messages, and to do proper coding, should I be deleting these objects myself?
Thanks,

see TH1::AddDirectory(kFALSE) and read chapter about Object Ownership in the Users Guide

Rene