Cloning histogram in a loop

Hello rooters,

I am having problem in cloning histogram in a loop. I have two arrays of histograms drawn on separate pads and now I want to Clone one of the histogram array but I could figure out the way it should be done.

Here is the sample code-

 C1->cd(1);
	    TH1D *hh1[35] ;
	    char *histname1 = new char[35];
	    
	     for(Int_t j=0;j<5;j++)
	     {                                               
	       sprintf(histname1, "%s%d","hh1",j);
	       trStepIn->Draw(Form("tstor*1e-8>>%s(%d,%d,%d)",histname1,tbin,tmin,tmax),Form("(lambda == %d)",500 + j*10000)*tmeas,(j==0) ? "" : "same");  
	         
	    TH1D *h1 =  (TH1D*)gDirectory->Get(histname1);
            for (Int_t i = 1; i<=h1->GetNbinsX();i++){
		h1->SetBinError(i,0);
            }
              h1->SetLineColor(kRed-j);
	   
           h1->SetDirectory(0);            
	   }
	          
             C1->cd(2);  
             TH1D *hh2[35] ;
	     char *histname2 = new char[35];
	    
	     for(Int_t k=0;k<5;k++)
	     {  
                                             
	       sprintf(histname2, "%s%d","hh2",k);
 trEv->Draw(Form("tstor*1e-8>>%s(%d,%d,%d)",histname2,tbin,tmin,tmax),Form("(lambda == %d)",500 + k*10000)*coincidence,(k==0) ? "E1" : "same*E1");  
	         
	    TH1D *h2 =  (TH1D*)gDirectory->Get(histname2);            
              h2->SetLineColor(kRed-k);	    
           h2->SetDirectory(0);                     
	   }        
             
            C2->cd(1);  
         // i want to clone hh2[ ] into hh3[ ] so that I could divide these array of histogram
                  
            TH1D *hr_Co[35] ;
            char *histname3 = new char[35];
             for(Int_t j =0;j<5;j++)
            {
            sprintf(histname3,"%s%d","hr_Co",j);
            
            //TH1D *Form("%s",histname3) = (TH1D*)Form("%s",histname2)->Clone(Form("%s",histname3)); 
                 
          }

Thanks

Hi,

you need to clone them one by one.

for (int i=0;i<35;++i)
   hh3[i] = (TH1D*)hh2[i]->Clone();

Cheers,
Danilo

Hi,
I tried your suggestion -

           TH1D *hr_Co[35] ;
            char *histname3 = new char[35];
             for(Int_t j =0;j<35;j++)
            {
            sprintf(histname3,"%s%d","hr_Co",j);
              hr_Co[j] = (TH1D*)hh2[j]->Clone();
               TH1D *h3 = (TH1D*)gDirectory->Get(histname3);
               h3->Divide(h1);    // from previous code           
             if (j==0) h3->Draw();
             else h3->Draw("same");                 
             h3->SetLineColor(kRed-j);	 
             h3->SetDirectory(0);
           }

but error - illegal pointer to class object hh2[j] 0x0 1125 tree_2nov.cxx:278:

and if I replace hr_Co[j] = (TH1D*)hh2[j]->Clone(); by
hr_Co[j] = (TH1D*)h2->Clone(Form("%s",histname3));
it works fine but the only problem it is just drawing one histogram. I just wonder is there any other way of drawing all 35 histograms in one canvas.

Thanks

Hi,

it looks like you are dealing at some point with an invalid pointer (your error). Not sure where it comes from.
The portion of the code you use for the draw looks correct. Can you check the list of primitives in the canvas?

D

Hello ,

Thanks again. I tried C2->GetListOfPrimitives()->Print();
It is giving hell lot of errors. I guess I may be doing something mistakes here. Moreover, drawn histogram is the 35th i.e last one from loop.

It looks like is not under control in your macro.
Do you have a reproducer?

Hi ,

I have no idea about what a reproducer does or how to use it. I am relatively new to root.

Thank you,
S

Hi,

for reproducer one usually means a standalone, minimal program which reproduces a particular issue for others to replay the steps you perform to encounter that issue on your system.
The concept is not related to ROOT.

Cheers,
D

Hello again,

Thank you for answering. I think the problem lies in the way histograms are cloned because the
hr_Co[j] = (TH1D*)h2->Clone(Form("%s",histname3));
is copying only the last histogram from the pointer.

I tried to check this 3 histograms hh2[3]
h2 =0x19a9c10
h2 =0x198ebd0 t
h2 =0x19a7c40

h2 =0x19a7c40
h3 =0x19a8a60
h2 =0x19a7c40 while cloning only last histogram is being cloned.
h3 =0x1995aa0
h2 =0x19a7c40
h3 =0x1995ea0

Thanks again.

This is fairly clear. Can you provide the reproducer?