Warning in <TH1::Build>: Replacing

Hello Rooters.

One simple question: is there ANY way to avoid the printing of this message…

Warning in TH1::Build: Replacing existing histogram:

Independienty of it is not important or not?

This is a part of the main code for may program :wink:

TH1D temp = NULL;

if (!temp){
for (int i =1; i<4; i++){
bins = 40 + (i

temp = new TH1D(“name”,“label”,bins,xlow,xup);

for (int j = 0; j< value; j++){



That is, I want to DEFINE the TH’s characteristics each time I do the loop, and delete it at the end, and I want tho have “my screen clear”.


I know that I can eather define the TH inside my loop or define an array of TH’s, but these solutions are not elegant, so to say.

I you want more details, I can post the full code…

Thanks in advance !

instead of

temp->Reset(); temp->Delete(); temp->Clear();
do “delete temp;” This has nothing to do with elegance but with avoiding a memory leak. So be thankful that ROOT prints that message - actually it should have made you wonder whether you’re doing something wrong. That’s what it’s for.

Cheers, Axel.

Hello Alex!

Thanks for your quick reply, but I tried that before ! (Yes, I know that if you create a pointer it is necesary to delete it after)

This is my code:

   TH1D *VdfitC1        = NULL; 
       for (int i = 0; i<4; i++){
	   bins = 30+ (i*20);
	   //the objets--------------------------------------------
	   VdfitC1   = new TH1D("Vdfit"  ,";Vd [cm/#mus];packs",bins,vlow,vup);
	   //Filling the histo -------------------
	   for(Int_t i= 0 ;i< t ;i++){
	       if (i >0)
	       if (tree->GetEvent(i+BPP)<0){
		   cout << "error reading event !" << endl;
	       Vdc[i]      = entry->getVdc();
            // only to indicate which things I tried ;)
	     delete VdfitC1;

That is, I’m reading a member of an object (t times) stored in a ROOT tree, and filling the histogram in different ways and different ranges with this info.

It works but as you said I think that if I create and destroy let’s say… 1000 histos… I will have “memory” problems. :wink:

That’s why I want to declare only one histo that will be filled 1000 times. After my tests using a " TStopwatch" object and 20 histos, this option (declare one object and use it 1000 times) seems to be a 10% faster, but it prints this message.
This " Reset", “Delete” , “Clear” and so on were to indicate what I tried.

It was my fault. Next time I will try to explain my problem better :blush:

Thanx anyway!


uh, OK, but then you still should not use Reset with new. Reset goes well with GetXaxis()->SetNbins() etc; new needs a delete. You could create a new TH1 once outside your loops, and then within your loops just re-initialize it using VdfitC1->Reset(), VdfitC1->SetName(), VdfitC1->GetXaxis()->SetNbins() etc.

Cheers, Axel.