Problem with RAM

Sorry to bother again… :frowning:

[code]#include <TFile.h>
#include <TObjArray.h>
#include <TGraph.h>
#include <TObject.h>
#include <TCint.h>
#include <TGraphErrors.h>
#include <TMultiGraph.h>
#include <TMarker.h>
#include <TTree.h>
#include <TBranch.h>
#include <TFolder.h>
#include <TString.h>
#include <TH1.h>
#include <TCanvas.h>
#include
#include
#include

using namespace std;

int main(){
const Char_t *Energy[5];
Energy[0]=“3MeV”;
Energy[1]=“10MeV”;
Energy[2]=“30MeV”;
Energy[3]=“100MeV”;
Energy[4]=“RanEn”;

const Char_t *Direction[4];
Direction[0]=“theta0”;
Direction[1]=“theta30”;
Direction[2]=“theta60”;
Direction[3]=“RanPosRanDir”;

for(Int_t c=0; c<4; c++)
{

for(Int_t a=0; a<5; a++)
{
ostringstream title;
ostringstream endTitle;
ostringstream otherTitle;

title<<“el”<<Energy[a]<<Direction[c];

endTitle<<title.str()<<".root";
cout<<“Processing “<<endTitle.str()<<”…”;
otherTitle<<title.str()<<“sigma.root”;
TTree *sigmaTree =new TTree(“Sigma”,“Sigma”);
TFile *myfile =TFile::Open(endTitle.str().c_str());

Double_t PETotpmt1;
Double_t PETotpmt2;
Double_t PETotpmt3;
Double_t PETotb;
Double_t sigma;

sigmaTree->Branch(“sigma”,&sigma,“sigma/D”);

for(Int_t b=1;b<2501;b++)
{
PETotpmt1 =0;
PETotpmt2 =0;
PETotpmt3 =0;
PETotb=0;
const Char_t* dir;
ostringstream pippo;
pippo<<"Run "<<b;
dir =pippo.str().c_str();
myfile->cd(dir);
gDirectory->cd(“Station 119”);
//cout<<“Run: “<<b<<”\t”;

TTree *pmt1Tot =(TTree *)gDirectory->Get(“pmt1Tot”);
TTree *pmt2Tot =(TTree *)gDirectory->Get(“pmt2Tot”);
TTree *pmt3Tot =(TTree *)gDirectory->Get(“pmt3Tot”);

pmt1Tot->SetBranchAddress(“PETot”,&PETotpmt1);
Int_t nentriesPmt1 =(Int_t)pmt1Tot->GetEntries();
pmt1Tot->GetEntry(nentriesPmt1-1);

pmt2Tot->SetBranchAddress(“PETot”,&PETotpmt2);
Int_t nentriesPmt2 =(Int_t)pmt2Tot->GetEntries();
pmt2Tot->GetEntry(nentriesPmt2-1);

pmt3Tot->SetBranchAddress(“PETot”,&PETotpmt3);
Int_t nentriesPmt3 =(Int_t)pmt3Tot->GetEntries();
pmt3Tot->GetEntry(nentriesPmt3-1);

PETotb =PETotpmt1 +PETotpmt2 +PETotpmt3;

sigma =pow(0.5*(pow(PETotpmt1-(PETotb/3.0),2) +pow(PETotpmt2-(PETotb/3.0),2) +pow(PETotpmt3-(PETotb/3.0),2)),0.5)/(PETotb/3.0);
//cout<<"sigma: "<<sigma<<endl;
sigmaTree->Fill();
delete pmt1Tot;
delete pmt2Tot;
delete pmt3Tot;
myfile->cd();

}

TFile *sigmafile =new TFile(otherTitle.str().c_str(),“recreate”);
TTree *sigmaTreeNew =sigmaTree->CloneTree();
sigmaTreeNew->Write();
cout<<“All Green!”<<endl;
delete sigmaTree;

delete sigmaTreeNew;

}
}
return 0;
}[/code]

i’m using this script to process a lot of files named “el10MeVtheta0.root” and to create new files with a tree and a branch called “sigma”.

The problem is that this process is very slow, while it is very fast if i process the files one at time (replacing the “for” structure with a single name ).

I had to delete trees because, if not, the program runs much faster, but quickly finishes the RAM.

what i need to change?
maybe there is a way to clean the memory between the cycles?
I tryed also to use ~TTree() but with no effect.

Thank you
Amir

I suggest to modify your program as shown below.
If this does not fix the problem, I will need access to your files.

Rene

[code]int main(){
const Char_t *Energy[5];
Energy[0]=“3MeV”;
Energy[1]=“10MeV”;
Energy[2]=“30MeV”;
Energy[3]=“100MeV”;
Energy[4]=“RanEn”;

const Char_t *Direction[4];
Direction[0]=“theta0”;
Direction[1]=“theta30”;
Direction[2]=“theta60”;
Direction[3]=“RanPosRanDir”;

for(Int_t c=0; c<4; c++) {

for(Int_t a=0; a<5; a++) {
ostringstream title;
ostringstream endTitle;
ostringstream otherTitle;

 title<<"el"<<Energy[a]<<Direction[c]; 

 endTitle<<title.str()<<".root"; 
 cout<<"Processing "<<endTitle.str()<<"...."; 
 otherTitle<<title.str()<<"sigma.root"; 
 TTree *sigmaTree =new TTree("Sigma","Sigma"); 
 TFile *myfile =TFile::Open(endTitle.str().c_str()); 

 Double_t PETotpmt1; 
 Double_t PETotpmt2; 
 Double_t PETotpmt3; 
 Double_t PETotb; 
 Double_t sigma; 


 sigmaTree->Branch("sigma",&sigma,"sigma/D"); 

 for(Int_t b=1;b<2501;b++) { 
    TFile *myfile =TFile::Open(endTitle.str().c_str()); //<=======
    PETotpmt1 =0; 
    PETotpmt2 =0; 
    PETotpmt3 =0; 
    PETotb=0; 
    const Char_t* dir; 
    ostringstream pippo; 
    pippo<<"Run "<<b; 
    dir =pippo.str().c_str(); 
    myfile->cd(dir); 
    gDirectory->cd("Station 119"); 
    //cout<<"Run: "<<b<<"\t"; 

    TTree *pmt1Tot =(TTree *)gDirectory->Get("pmt1Tot"); 
    TTree *pmt2Tot =(TTree *)gDirectory->Get("pmt2Tot"); 
    TTree *pmt3Tot =(TTree *)gDirectory->Get("pmt3Tot"); 

    pmt1Tot->SetBranchAddress("PETot",&PETotpmt1); 
    Int_t nentriesPmt1 =(Int_t)pmt1Tot->GetEntries(); 
    pmt1Tot->GetEntry(nentriesPmt1-1); 



    pmt2Tot->SetBranchAddress("PETot",&PETotpmt2); 
    Int_t nentriesPmt2 =(Int_t)pmt2Tot->GetEntries(); 
    pmt2Tot->GetEntry(nentriesPmt2-1); 



    pmt3Tot->SetBranchAddress("PETot",&PETotpmt3); 
    Int_t nentriesPmt3 =(Int_t)pmt3Tot->GetEntries(); 
    pmt3Tot->GetEntry(nentriesPmt3-1); 

    PETotb =PETotpmt1 +PETotpmt2 +PETotpmt3; 

    sigma =pow(0.5*(pow(PETotpmt1-(PETotb/3.0),2) +pow(PETotpmt2-(PETotb/3.0),2) +pow(PETotpmt3-(PETotb/3.0),2)),0.5)/(PETotb/3.0); 
    //cout<<"sigma: "<<sigma<<endl; 
    sigmaTree->Fill(); 
    delete pmt1Tot; 
    delete pmt2Tot; 
    delete pmt3Tot; 
    delete myfile; //<==========
 } 

 TFile *sigmafile =new TFile(otherTitle.str().c_str(),"recreate"); 
 TTree *sigmaTreeNew =sigmaTree->CloneTree(); 
 sigmaTreeNew->Write(); 
 cout<<"All Green!"<<endl; 
 delete sigmaTree;        
 delete sigmaTreeNew; 

}
}
return 0;
}[/code]

sorry for the delay :frowning:
thank you!:smiley: