Writing branches to different files

Rene Brun (Rene.Brun@cern.ch)
Fri, 04 Dec 1998 09:56:07 +0100


> Hi Rooters,
>
> has anybody a working samplecode for storing data in a TTree, where the
> branches are stored in different TFiles?
> I tried to do this, writing seemed to work, but when I tried to read I
> get an error.
>
> Sascha.f

Hi Sascha,
I have modified and simplified your program below.
See the two macros tofiles.C and fromfiles.C. You can execute them
in an interactive session with
Root > .x tofiles.C
Root > .x fromfiles.C

You do not need to open the branch files yourself.
Simply specify the branch name in TBranch::SetFile.
You do not need to close the files.

Rene Brun

//_________________macro tofiles.C

#include <iostream.h>
void tofiles () {
TFile *floatFile = new TFile("float.root","recreate");

Float_t f1,f2;
Int_t i1,i2;
Double_t d1,d2;

cout<<"Peter Wilmes"<<endl;
TTree* tree = new TTree("Tree","A Root Tree");
tree->Branch("Float1",&f1,"float1");
tree->Branch("Float2",&f2,"float2");
tree->Branch("Double1",&d1,"double1");
tree->Branch("Double2",&d2,"double2");
tree->Branch("Int1",&i1,"int1");
tree->Branch("Int2",&i2,"int2");
tree->GetBranch("Int1")->SetFile("int.root");
tree->GetBranch("Int2")->SetFile("int.root");
tree->GetBranch("Double1")->SetFile("double.root");
tree->GetBranch("Double2")->SetFile("double.root");
for (int i=0;i<=100000;++i) {
f1=i*.836;
f2=i*1.5836;
i1=i;
i1=i+7;
d1=(Double_t)f1;
d2=(Double_t)f2;
tree->Fill();
}
floatFile->Write();
delete floatFile;
cout<<"####### end job!!"<<endl;
}

//___________________macro fromfiles.C_______________________
#include <iostream.h>
void fromfiles() {
TFile *floatFile = new TFile("float.root","read");
Float_t f1,f2;

cout<<"####### start reading !!"<<endl;
TTree *ttree = (TTree*)floatFile->Get("Tree");
ttree->SetBranchAddress("Float1", &f1);
for (int i=0;i<=10;++i) {
ttree->GetEvent(i);
cout<<" der wert von f1: "<<f1<<endl;
}
delete floatFile;
cout<<"####### end job!!"<<endl;
}

>
> ###########################################################
> ########################################################
> Error message:
>
> Fatal in <operator new>: storage exhausted
> aborting
> Warning in <TClass::TClass>: no dictionary for class TWinNTSystem
> available
> Warning in <TWinNTSystem::StackTrace>: this method must be overridden!
>
> abnormal program termination
>
> #############################################################
> ########################################################
> Here is my Code of the Programm which fills the TTree:
>
> #include <iostream.h>
> #include "TROOT.h"
> #include "TFile.h"
> #include "TTag.h"
> #include "TTree.h"
> #include "TBranch.h"
>
> //initialize Root-System
> TROOT root("picotest","test of the Tag/DB");
> //instances
> TTag *tagdb;
> TTree* ttree;
> TBranch* branch;
>
> TFile *intFile = new TFile("int.root","recreate");
> TFile *doubleFile = new TFile("double.root","recreate");
> TFile *floatFile = new TFile("float.root","recreate");
>
> Float_t f1,f2;
> Int_t i1,i2;
> Double_t d1,d2;
>
> void main () {
>
> cout<<"Peter Wilmes"<<endl;
> //tagdb= new TTag();
> //cout<<*tagdb<<endl;
> TTree* tree = new TTree("Tree","A Root Tree");
> tree->Branch("Float1",&f1,"float1");
> tree->Branch("Float2",&f2,"float2");
> tree->Branch("Double1",&d1,"double1");
> tree->Branch("Double2",&d2,"double2");
> tree->Branch("Int1",&i1,"int1");
> tree->Branch("Int2",&i2,"int2");
> tree->GetBranch("Float1")->SetFile(floatFile);
> tree->GetBranch("Float2")->SetFile(floatFile);
> tree->GetBranch("Int1")->SetFile(intFile);
> tree->GetBranch("Int2")->SetFile(intFile);
> tree->GetBranch("Double1")->SetFile(doubleFile);
> tree->GetBranch("Double2")->SetFile(doubleFile);
> for (int i=0;i<=100000;++i) {
> f1=i*.836;
> f2=i*1.5836;
> i1=i;
> i1=i+7;
> d1=(Double_t)f1;
> d2=(Double_t)f2;
> tree->Fill();
> }
> //tree->Write();
> intFile->Write();
> floatFile->Write();
> doubleFile->Write();
> intFile->Close();
> floatFile->Close();
> doubleFile->Close();
> cout<<"####### end job!!"<<endl;
> cout<<endl;
> cout<<endl;
>
>
> }
>
> #########################################
> #########################################
> code for reading the tree:
> #include <iostream.h>
> #include "TROOT.h"
> #include "TFile.h"
> #include "TTree.h"
> #include "TBranch.h"
>
> //initialize Root-System
> TROOT root("picotest","test of the Tag/DB");
> //instances
> TTag *tagdb;
> TTree* ttree;
> TBranch* floatbranch;
> TBranch* doublebranch;
> TBranch* intbranch;
>
> TFile *intFile = new TFile("int.root","read");
> TFile *doubleFile = new TFile("double.root","read");
> TFile *floatFile = new TFile("float.root","read");
>
> Float_t f1,f2;
> Int_t i1,i2;
> Double_t d1,d2;
>
> void main () {
>
> cout<<"####### start reading !!"<<endl;
> ttree = (TTree*)floatFile->Get("Tree");
> //floatbranch->SetFile(floatFile);
> floatbranch = ttree->GetBranch("Float1");
>
> floatbranch->SetAddress(&f1);
> for (int i=0;i<=10;++i) {
> ttree->GetEvent(i);
> cout<<" der wert von f1: "<<f1<<endl;
> }
> intFile->Close();
> floatFile->Close();
> doubleFile->Close();
> cout<<"####### end job!!"<<endl;
> }