// // This tutorials demonstrate how to store and restore simple vectors // in a TTree // #include #include #include "TFile.h" #include "TTree.h" #include "TH1F.h" #include "TPad.h" #include "TRandom.h" #ifdef __MAKECINT__ #pragma link C++ class vector+; #endif void write() { TFile *f = TFile::Open("hvector3.root","RECREATE"); if (!f) { return; } TH1F hf("hf","This is the px distribution",100,-4,4); vector v; TTree *t = new TTree("tvec","Tree with vectors"); t->Branch("hf",&hf); t->Branch("v",&v); gRandom->SetSeed(); for (Int_t i = 0; i < 500; i++) { hf.Reset(); v.clear(); Int_t npx = (Int_t)(gRandom->Rndm(1)*150); for (Int_t j = 0; j < npx; ++j){ Float_t px,py; gRandom->Rannor(px,py); hf.Fill(px); v.push_back(py); } t->Fill(); } f->Write(); delete f; } void read() { TFile *f = TFile::Open("hvector3.root","READ"); if (!f) { return; } TTree *t; f->GetObject("tvec",t); TH1F *hf = 0; TBranch *bhf = 0; t->SetBranchAddress("hf",&hf,&bhf); vector *v = 0; TBranch *bv = 0; t->SetBranchAddress("v",&v,&bv); TH1F *h = new TH1F("h","This is the py distribution",100,-4,4); const Int_t kUPDATE = 100; for (Int_t i = 0; i < 500; i++){ h->Reset(); Long64_t tentry = t->LoadTree(i); bhf->GetEntry(tentry); bv->GetEntry(tentry); for (UInt_t j = 0; j < v->size(); ++j){ h->Fill(v->at(j)); } if (i && (i%kUPDATE) == 0){ hf->Draw(); gPad->Update(); h->Draw(); gPad->Update(); } } t->ResetBranchAddresses(); } void hvector3() { write(); read(); }