Why i can't fill this tree?

Hi,
i can’t fill the tree named phi0tree. why?

#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 <TH2.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <iostream>
#include <sstream>

using namespace std;

void prendi(string title,string cosa,TTree *atree);

void shooter(bool zpe2)
{
string Energy ="3GeV";

const char *Direction[6];
Direction[0] ="theta0";
Direction[1] ="theta15";
Direction[2] ="theta30";
Direction[3] ="theta45";
Direction[4] ="theta60";

const char *Phi[3];
Phi[0] ="phi0";
Phi[1] ="phi30";
Phi[2] ="phi90";

TFile *newfile0 =new TFile("datishooter.root","recreate");
delete newfile0;

double meanPE =0;
double sigma_of_the_mean =0;
double sigma =0;
double sigma_on_sqr_mean =0;

TTree *pretree = new TTree("phi","phi");
pretree->Branch("meanPE",&meanPE,"meanPE/D");
pretree->Branch("sigma",&sigma,"sigma/D");
pretree->Branch("sigma_of_the_mean",&sigma_of_the_mean,"sigma_of_the_mean/D");
pretree->Branch("sigma_on_sqr_mean",&sigma_on_sqr_mean,"sigma_on_sqr_mean/D");

string cosa;

if (zpe2==0)
	{
	cosa="PE_totali";
	}
else
	{
	cosa="PE_totali_random";
	}

for(int e=0;e<3;e++)
    {
     if(e==0)
     	{
        TTree *phi0tree =pretree->CloneTree(); 
        ostringstream filetitle;
        filetitle<<"Shootermu"<<Energy<<Direction[0]<<Phi[e]<<"TotalData.root";
        cout<<filetitle.str()<<endl;
	    phi0tree->SetName(Phi[0]);	 
        prendi(filetitle.str().c_str(),cosa,phi0tree);
	
		TFile *fatto =TFile::Open("datishooter.root","update");
		phi0tree->Write();
		delete phi0tree;
		delete fatto;
		}
	}
}
/********************************************************************************************
*********************************************************************************************/   
void prendi(string title,string cosa,TTree *atree)
{

int PE=0;

TFile *myfile =TFile::Open(title.c_str());
cout<<"title "<<title<<endl;
double meanPE =0;
double sigma_of_the_mean =0;
double sigma =0;
double sigma_on_sqr_mean =0;

TTree *oldtree =(TTree*)myfile->Get(cosa.c_str());
oldtree->SetBranchAddress("PEtot3",&PE);

cout<<"cosa "<<cosa<<endl;
int nent =oldtree->GetEntries();
cout<<"nent "<<nent<<endl;
TH1F *primo =new TH1F("tot3","tot3",nent,0,nent);
for(int g=0; g<nent;g++)
{
oldtree->GetEntry(g);
//cout<<"PE "<<PE<<endl;
primo->Fill(PE);
}

meanPE =(double)primo->GetMean();
cout<<"mean "<<meanPE<<endl;
sigma =(double)primo->GetRMS();
cout<<"sigma "<<sigma<<endl;
sigma_of_the_mean =sigma/(pow(nent-2,0.5));    
cout<<"sigma_of_the_mean "<<sigma_of_the_mean<<endl;//<==i know from these couts that the values exist and are correct. 
sigma_on_sqr_mean =sigma/pow(meanPE,0.5);
cout<<"sigma_on_sqr_mean "<<sigma_on_sqr_mean<<endl;

atree->Fill();
delete myfile;
}

Thank you
Amir[/code]

Please give more details
-you did not give the input files
-you do not specify how to call your function

Rene

sorry, here are the files.

start root
.L prendishooter.cc+
shooter(0) or shooter(1)

it will create a file called datishooter.root containing a tree named phi0.

i’m trying to fill this tree with the values of mean, sigma… of the graph PEtot3 of Shootermu3GeVtheta0phi0TotalData.root.

phi0 is filled but with entries =0 not the proper values.

Maybe it’s beacause tree->Fill() method is in a function.

The main idea is to collect mean,sigma,… from various files (that’s the use of the commented cicle) and to store them in 3 trees (i’ll use them to create graphs)

Thank you
Amir
filesAmir.tar.gz (741 KB)

i solved the problem.

the tree was filling with values =0 because the variables of the tree were declared and initialized in the function.
So they were cleared after the end of the function.

I changed the code this way:

using namespace std;

double meanPE;
double sigma_of_the_mean;
double sigma;
double sigma_on_sqr_mean;

void prendi(string title,string cosa,TTree *atree);
void shooter(bool zpe2)
{
string Energy ="3GeV";

const char *Direction[6];
Direction[0] ="theta0";
Direction[1] ="theta15";
Direction[2] ="theta30";
Direction[3] ="theta45";
Direction[4] ="theta60";

const char *Phi[3];
Phi[0] ="phi0";
Phi[1] ="phi30";
Phi[2] ="phi90";

TFile *newfile0 =new TFile("datishooter.root","recreate");
delete newfile0;

TTree *pretree = new TTree("phi","phi");
pretree->Branch("meanPE",&meanPE,"meanPE/D");
pretree->Branch("sigma",&sigma,"sigma/D");
pretree->Branch("sigma_of_the_mean",&sigma_of_the_mean,"sigma_of_the_mean/D");
pretree->Branch("sigma_on_sqr_mean",&sigma_on_sqr_mean,"sigma_on_sqr_mean/D");

string cosa;

if (zpe2==0)
	{
	cosa="PE_totali";
	}
else
	{
	cosa="PE_totali_random";
	}

for(int e=0;e<3;e++)
    {
     if(e==0)
     	{
        TTree *phi0tree =pretree->CloneTree(); 
        ostringstream filetitle;
        filetitle<<"Shootermu"<<Energy<<Direction[0]<<Phi[e]<<"TotalData.root";
        cout<<filetitle.str()<<endl;
	    phi0tree->SetName(Phi[0]);	 
        prendi(filetitle.str().c_str(),cosa,phi0tree);
		TFile *fatto =TFile::Open("datishooter.root","update");
		phi0tree->Write();
		delete phi0tree;
		delete fatto;
		}
	}
}
/********************************************************************************************
*********************************************************************************************/   
void prendi(string title,string cosa,TTree *atree)
{

int PE=0;

TFile *myfile =TFile::Open(title.c_str());
cout<<"title "<<title<<endl;
meanPE =0;
sigma_of_the_mean =0;
sigma =0;
sigma_on_sqr_mean =0;

TTree *oldtree =(TTree*)myfile->Get(cosa.c_str());
.
.
.
.

It is in these things that my poor knowledge on C++ shows up…sorry:(
Amir