Write and read strings into/outof Tree

[size=150]
Hallo,
I’ve a problem with a vector I want to store in a tree and read out again afterwards. I fill the tree with the following code:
[/size]

vector *tidentivec = new vector;

TFile fi(“identi.root”,“recreate”);
TTree *Treeidenti = new TTree(“Treeidenti”,“Vectortree”);
Treeidenti->Branch(“tidentivec”,“vector”,&tidentivec);

for(Int_t i=0; ipush_back(Identivec[idselectarr->At(i)]);

Treeidenti->Fill();    

}
fi.cd();
Treeidenti->Write();
}

[size=150]and afterwards I read it out with the following code[/size]

//identifiertree
TFile *ifile = new TFile(“identi.root”);
TTree Treeidenti = (TTree)ifile->Get(“Treeidenti”);

//Declaration of leaves types
vector *tidentivec = new vector;

//Set branch address
Treeidenti->SetBranchAddress(“tidentivec”,&tidentivec);

Int_t id = Treeidenti->GetEntries();

//create canvas and histogram with time on xaxis
c1 = new TCanvas(“c1”,“Time on Axis”,10,10,600,800);
int i=0;
int c=0;
Int_t p = tfg->GetEntries();
c = ceil(sqrt(4));
c1->Divide(c,c);
TDatime da(2006,05,11,00,00,00);
gStyle->SetTimeOffset(da.Convert());
gStyle->SetOptStat(11);

TH2F *hist = new TH2F(“hist”, “Temperature vs. time”,900,40000,86400,100,-30,30);

TArrayD *tstoparr = new TArrayD§;
for(Int_t i=0;i

GetEntry(i);
tstoparr -> AddAt(tstop,i);
}

//read all entries and fill histogram
Int_t nentries = Envronetree->GetEntries();
for (Int_t z=1;z<4>cd(z);
for(Int_t i=tstoparr->At(z-1); iAt(z); i++){
Envronetree->GetEntry(i);
hist->Fill(ttsec,tval);
}
[size=150] Treeidenti->GetEntry(i); [/size]

hist -> SetTitle(“Temperature vs.time”);
hist -> GetYaxis() -> SetTitle(“temperature [#circC]”);
hist -> GetXaxis() -> SetTitle(“time [sec]”);
hist -> GetXaxis() -> SetLabelSize(0.03);
hist -> GetXaxis() -> SetTimeDisplay(1);
hist -> GetXaxis() -> SetTimeFormat("%H:%M");

hist -> DrawCopy();
hist -> Reset();
[size=150]
Now I want to know how to display the string. It’s not possible with cout<< tidentivec <<endl; … because I get a number then. Or is there a mistake in filling and reading the tree?
I would like to use the strings stored in the vector as title of the Histograms . How is this possible ?

Many thanks for any help
[/size]
Readdat.C (5.96 KB)
Drawdat.C (3.26 KB)

cout<< tidentivec <<endl; print the address of your vector of strings. If suspect you want to do:

for (int i=0; i< tidentivec->size(); ++i) { cout << (*tidentivec)[i] << end; }
Also you code may have problem with the loop indices:

for (Int_t z=1;z<=4;z++) { c1->cd(z); for(Int_t i=tstoparr->At(z-1); i<tstoparr->At(z); i++){ Envronetree->GetEntry(i); hist->Fill(ttsec,tval); } Treeidenti->GetEntry(i); Why to use use ‘i’ in Treeidenti->GetEntry(i); while it is only defined within the scope of the previous loop (your code would compile because you have another (unrelated?) declaration of ‘i’ earlier in the code.

Cheers,
Philippe.

[size=150]Hi,
thanks for the reply . But I could not get the strings printed to the screen with the following code: [/size]

[code] //identifiertree
TFile *ifile = new TFile(“identi.root”);
TTree Treeidenti = (TTree)ifile->Get(“Treeidenti”);

//Declaration of leaves types
vector *tidentivec = new vector;

//Set branch address
Treeidenti->SetBranchAddress(“tidentivec”,&tidentivec);

Int_t id = Treeidenti->GetEntries();
for(int i=0; isize(); i++){
Treeidenti->GetEntry(i);
cout << (*tidentivec)[i]<< endl;
}[/code]
[size=150]
What’s wrong here ? The macro compiles without an error message. The filling of the tree seems to bee alright bcause I can display the strings with the following code:[/size]

[code] vector *tidentivec = new vector;

TFile fi(“identi.root”,“recreate”);
TTree *Treeidenti = new TTree(“Treeidenti”,“Vectortree”);
Treeidenti->Branch(“tidentivec”,“vector”,&tidentivec);

for(Int_t i=0; ipush_back(Identivec[idselectarr->At(i)]);

Treeidenti->Fill();    

}
fi.cd();
Treeidenti->Write();

for(int k=0;ksize();k++){
cout<< (*tidentivec)[k] <<endl;
} [/code]

[size=150]Is there a mistake wit the daclaration of the tree ???
Thank you very much for any hint [/size] :confused:

Unfortunately your code was ‘eaten’ by the html processor of the forum.
Can you please repost (and disable HTML in this post) or add it as an attachment.

Thanks,
Philippe