Re: replacing histogram in a loop

From: Axel Naumann <axel-naumann_at_gmx.de>
Date: Fri, 18 Feb 2005 14:49:02 -0600


Hi Gunasingha,
there are some problems with your macro. The attached version should work. Axel.

Rat Gunasingha wrote:
> Hi,
> I am using following script(see attached file) to
> draw some histos and
> get
> the mean values
> after doing some fitting.
> In this file I have a while loop, that takes two
> variables tubn, dist from
> a input file and run through it. Before I start this
> in root, I run
> another script to merger several root files.
> and also I do TH1F *h1=new TH1F("h1","
> ",100,0.,1000.);
>
> My question is this.
> Just after the while loop I have a h1->Delete(); line
> to delete the
> histogram from previous one. When this line is, I get
> the following
> error(attached file).
> saying segmentation violation. Without that line (
> h1->Delete() ), Program
> runs and get a warning (see attached file) at each
> histogram. It says potential memory leak when it
> replaces the previous
> one.
> I have copied the warning, ERROR and the script to the
> attached file.
> I am using ROOT 3.10/01 and RedHat Linux 9.0.
> How can I get this fixed.
>
> Thanks,
>
> Gunasingha
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Meet the all-new My Yahoo! - Try it today!
> http://my.yahoo.com
>

{
        ifstream input;
        ofstream output;
        Double_t average,gausave,dist;
        Double_t mini,maxi;
        Int_t tubn;
        gStyle->SetOptFit();
        gStyle->SetStatX(0.9);
        gStyle->SetStatY(0.9);
        input.open("c_dist.dat");
        output.open("39005.dat");
        // move outside the loop, no need to do this every iteration
         cout.setf(ios::fixed);
         cout.setf(ios::showpoint);
         cout.precision(2);
         output.setf(ios::fixed);
         output.setf(ios::showpoint);
         output.precision(2);


        if(input.fail()){
         cout<<" Can not open input file. "<<endl;
         exit(1);
        }
        // create outside the loop, so it is known in all iterations
        // set to 0, so the first delete won't do anything.
         TH1F* h1=0; 
         

         while(input>>tubn>>dist){
         cout<<"tubn : "<<tubn<<endl;
         delete h1; // delete the provious iteration's histogram
         h1=new TH1F("h1"," ",100,0.,1000.);
         chain->Draw("cpul>>h1",Form("ctub==39005&&btub==%d",tubn));
         average = h1->GetMean();
         mini = average - 120;
         maxi = average + 120;
         h1->GetYaxis()->SetTitle("Counts");
         h1->Fit("gaus","R","",mini,maxi);
         gausave = h1->GetMean();
         h1->GetXaxis()->SetTitle("ADC");
         c1->SetFillColor(0);
         c1->SaveAs(Form("39005_%d.eps",tubn));

         if(gausave!=0.0){
         cout<<tubn<<" "<<dist<<" "<<gausave<<endl;
         output<<tubn<<" "<<dist<<" "<<gausave<<endl;
        }
        }

} Received on Fri Feb 18 2005 - 21:49:08 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:05 MET