#ifndef __CINT__ # include # include # include # include # include # include using namespace std; #endif void CreateTree(Int_t i, Int_t m=3) { TClonesArray* array = new TClonesArray("TVector3"); TFile* file = TFile::Open(Form("%03d.root", i), "RECREATE"); TTree* tree = new TTree("T", "T"); tree->Branch("vector", &array); cout << " Writing tree " << tree->GetName() << " to file " << file->GetName() << endl; for (Int_t j = 0; j < m; j++) { cout << " Entry # " << j << endl; array->Clear(); for (Int_t k = 0; k < j+1; k++) { Double_t x = 100 * i + 10 * j + k + .1; Double_t y = 100 * i + 10 * j + k + .1; Double_t z = 100 * i + 10 * j + k + .1; new ((*array)[k]) TVector3(x, y, z); } tree->Fill(); } file->Write(); file->Close(); } void CreateChain(Int_t n=3, Int_t m=3) { cout << "Creating " << n << " files" << endl; for (Int_t i = 0; i < n; i++) CreateTree(i, m); } void ReadArray(TClonesArray* array=0) { Int_t o = array->GetEntries(); cout << " Reading " << o << " vectors from array" << endl; for (Int_t k = 0; k < o; k++) { TVector3* v = static_cast(array->At(k)); // cout << " (" << v->X() << "," << v->Y() // << "," << v->Z() << ")" << endl; } } void ReadTree(TTree* tree=0) { cout << " Reading from the tree " << tree->GetName() << endl; TClonesArray* array = new TClonesArray("TVector3"); TClonesArray* other = new TClonesArray("TVector3"); tree->SetBranchAddress("vector", &array); TTree* clone = tree->CloneTree(); clone->SetBranchAddress("vector", &other); Int_t entry = 0; Int_t read; while ((read = tree->GetEntry(entry)) > 0) { cout << " Entry # " << entry << " (" << read << " bytes)" << endl; ReadArray(array); if (entry > 0) { read = clone->GetEntry(entry-1); cout << " Clone Entry # " << entry - 1 << " (" << read << " bytes)\n " << std::flush; ReadArray(other); } entry++; } } void ReadFile(const Char_t* name="000.root") { cout << "Reading from the a single tree from file " << name << endl; TFile* file = TFile::Open(name, "READ"); TTree* tree = static_cast(file->Get("T")); ReadTree(tree); file->Close(); } void ReadChain() { cout << "Reading from the a chain of trees from" << endl; TChain* tree = new TChain("T"); tree->Add("0*.root"); TFile* file = TFile::Open("foo.root", "RECREATE"); ReadTree(tree); } void Mixed() { CreateChain(); ReadFile(); ReadChain(); }