Is it a good way to read files?

Hi,
i have many root files each with 30000 directories containing various trees.
i need to read them, retrieve the data i need and put i on a single new root file.

since the files are very big the process is very slow in real life (more than a week!)
so i want to save as much machine time i can, optimizing the process.

do you think the structure of this “sample” is well written or there is a way to do things faster and better?

Thank you
Amir

[code]#include <TFile.h>
#include <TObject.h>
#include <TCint.h>
#include <TTree.h>
#include <TBranch.h>
#include <TString.h>
#include
#include

using namespace std;

void fprova(int e,string titolo);

TTree *petotal0;
TTree *clonetree;

int energytree;

int main(){

const Char_t *Direction;
Direction=“theta15”;

const Char_t *Phi[2];
Phi[0]=“phi30”;
Phi[1]=“phi90”;

ostringstream otherTitle;
otherTitle<<“prova2.root”;

petotal0 =new TTree(“name”,“name”);
petotal0->Branch(“energy”,&energytree,“energytree/I”);

for(int e=0; e<2; e++)
{
ostringstream titolo;
titolo<<Direction<<Phi[e];

clonetree =petotal0->CloneTree();
clonetree->SetName(titolo.str().c_str());

fprova(e,titolo.str());

TFile *newfile =new TFile(otherTitle.str().c_str(),“update”);
clonetree ->Write();
newfile->Close();
}//fine phi

cout<<“All Green!”<<endl;
return 0;
}

/*********************************************************************************************************
*********************************************************************************************************/
void fprova(int e,string titolo)
{
int newEnergy =0;

TFile *myfile =TFile::Open(“prova.root”);

TTree *direction =(TTree *)myfile->Get(titolo.c_str());
direction->SetBranchAddress(“energy”,&newEnergy);
int entries =(int)direction->GetEntries();
direction->GetEntry(entries-1);

energytree =newEnergy +5;

clonetree->Fill();

myfile->Close();
}

[/code]

The “real” code is this:

[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 <TH2.h>
#include <TCanvas.h>
#include
#include
#include

using namespace std;

void fTotRunHistos(string endTitle,bool zpe);

TTree *petotal0;
TTree *clonetree;

Double_t mean_totpmt;
Double_t energytree;
Double_t sigma;
Double_t sigma_on_sqr_mean;
Double_t sigma_of_the_mean;

int main(int argc,char *argv[]){

int ZPE;
bool zpe=0;//for suppression of events with 0 PE

if (argc!=2)
{
cout<<“viene accettato solo 1 parametro: 1=Zpe,0=Normale”<<endl;
exit(1);
}
ZPE = atoi(argv[1]);
if (ZPE !=0 && ZPE !=1)
{
cout<<“Parametro errato, vengono accettati: 1=Zpe,0=Normale”<<endl;
exit(1);
}
else
{
zpe=ZPE;
}

const Char_t *Energy[6];
Energy[0]=“0_1GeV”;
Energy[1]=“0_3GeV”;
Energy[2]=“1GeV”;
Energy[3]=“3GeV”;
Energy[4]=“10GeV”;
Energy[5]=“RanEn”;

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

const Char_t *Phi[3];
Phi[0]=“phi0”;
Phi[1]=“phi30”;
Phi[2]=“phi90”;

ostringstream otherTitle;
if (zpe !=0)
{
otherTitle<<“TotRunHistosZpe.root”;
}
else
{
otherTitle<<“TotRunHistos.root”;
}

petotal0 =new TTree(“theta0”,“theta0”);
petotal0->Branch(“energy”,&energytree,“energytree/D”);
petotal0->Branch(“sigma”,&sigma,“sigma/D”);
petotal0->Branch(“sigma_on_sqr_mean”,&sigma_on_sqr_mean,“sigma_on_sqr_mean/D”);
petotal0->Branch(“meantotpmt”,&mean_totpmt,“mean_totpmt/D”);
petotal0->Branch(“sigmaofmean”,&sigma_of_the_mean,“sigma_of_the_mean/D”);

for(Int_t c=0; c<5; c++)
{
//int c=1;
if (c==0)
{
for(int a=0; a<6; a++)
{

ostringstream title;
ostringstream endTitle;
title<<“Shootermu”<<Energy[a]<<Direction[c]<<Phi[c];
endTitle<<title.str()<<".root";
cout<<“Processing “<<endTitle.str()<<”…”<<endl;

ostringstream titolo;
titolo<<Direction[c]<<Phi[c];

clonetree->SetName(titolo.str().c_str());
fTotRunHistos(endTitle.str(),zpe);

TFile *newfile =new TFile(otherTitle.str().c_str(),“update”);
clonetree ->Write();
newfile->Close();

}//fine energy
}//fine if

else
{
for(int e=1; e<3; e++)
{
for(int a=0; a<6; a++)
{

ostringstream title;
ostringstream endTitle;
title<<“Shootermu”<<Energy[a]<<Direction[c]<<Phi[e];
endTitle<<title.str()<<".root";
cout<<“Processing “<<endTitle.str()<<”…”<<endl;

ostringstream titolo;
titolo<<Direction[c]<<Phi[e];

clonetree =petotal0->CloneTree();
clonetree->SetName(titolo.str().c_str());
fTotRunHistos(endTitle.str(),zpe);

TFile *newfile =new TFile(otherTitle.str().c_str(),“update”);
clonetree ->Write();
newfile->Close();

}// fine energy
}//fine phi
}//fine else

}//fine direction

cout<<“All Green!”<<endl;
return 0;
}

/*********************************************************************************************************
*********************************************************************************************************/
void fTotRunHistos(string endTitle,bool zpe)
{
int count =0;
double totpmt =0;
double sigma_part=0;

for(Int_t b=1;b<30001;b++)
{

TFile *myfile =TFile::Open(endTitle.c_str());

double pmt1 =0;
double pmt2 =0;
double pmt3 =0;

const Char_t* dir;
ostringstream pippo;
pippo<<"Run "<<b;
dir =pippo.str().c_str();
myfile->cd(dir);
gDirectory->cd(“Station 119”);

//cout<<b<<endl;
TTree *pmt1Tot =(TTree *)gDirectory->Get(“pmt1Tot”);
TTree *pmt2Tot =(TTree *)gDirectory->Get(“pmt2Tot”);
TTree *pmt3Tot =(TTree *)gDirectory->Get(“pmt3Tot”);
TTree *particle =(TTree *)gDirectory->Get(“Particle”);

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

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

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

particle->SetBranchAddress(“Energy”,&energytree);
Int_t nenParticle =(Int_t)particle->GetEntries();
particle->GetEntry(nenParticle-1);

if (zpe !=0)
{
if (pmt1 !=0 || pmt2 !=0 || pmt3 !=0)
{
count++;
totpmt += pmt1+pmt2+pmt3;
}
}
else
{
totpmt += pmt1+pmt2+pmt3;
}

delete myfile;

}//fine ciclo sulle stazioni

if (zpe !=0)
{
mean_totpmt =totpmt/count; //<===============================================mean_totpmtZpe
}
else
{
mean_totpmt =totpmt/30000; //<===============================================mean_totpmt
}
//myfile->cd();

for(Int_t b=1;b<30001;b++)
{
TFile *myfile =TFile::Open(endTitle.c_str());
double pmt1 =0;
double pmt2 =0;
double pmt3 =0;
double totpmtperrun =0;

const Char_t* dir;
ostringstream pippo;
pippo<<"Run "<<b;
dir =pippo.str().c_str();
myfile->cd(dir);
gDirectory->cd(“Station 119”);

//cout<<b<<endl;
TTree *pmt1Tot =(TTree *)gDirectory->Get(“pmt1Tot”);
TTree *pmt2Tot =(TTree *)gDirectory->Get(“pmt2Tot”);
TTree *pmt3Tot =(TTree *)gDirectory->Get(“pmt3Tot”);
TTree *particle =(TTree *)gDirectory->Get(“Particle”);

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

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

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

particle->SetBranchAddress(“Energy”,&energytree);
Int_t nenParticle =(Int_t)particle->GetEntries();
particle->GetEntry(nenParticle-1);

if (zpe !=0)
{
if (pmt1 !=0 || pmt2 !=0 || pmt3 !=0)
{
totpmtperrun = pmt1+pmt2+pmt3;
}
}
else
{
totpmtperrun = pmt1+pmt2+pmt3;
}
//cout<<totpmtperrun<<endl;
sigma_part += pow((totpmtperrun-mean_totpmt),2);

delete myfile;

}//fine ciclo per sigma

sigma =pow((1/29999.0)*(sigma_part),0.5);//<===============================================sigma
sigma_of_the_mean =sigma/(pow(30000.0,0.5));//<===============================================sigma_of_the_mean
sigma_on_sqr_mean =sigma/pow(mean_totpmt,0.5);//<===============================================sigma_on_sqr_mean

clonetree->Fill();
}

[/code]

use $ROOTSYS/bin/hadd to merge root files

hadd -f result.root f1.root f2.root fn.root

Rene

Thank you Rene

Amir