1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
//
// This tutorials demonstrate how to store and restore simple vectors
// in a TTree
//
#include <vector>
#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TFrame.h"
#include "TH1F.h"
#include "TBenchmark.h"
#include "TRandom.h"
#include "TSystem.h"
#ifdef __MAKECINT__
#pragma link C++ class vector<float>+;
#endif
void write()
{
TFile *f = TFile::Open("hvector.root","RECREATE");
if (!f) { return; }
// Create one histograms
TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
hpx->SetFillColor(48);
std::vector<float> vpx;
std::vector<float> vpy;
std::vector<float> vpz;
std::vector<float> vrand;
// Create a TTree
TTree *t = new TTree("tvec","Tree with vectors");
t->Branch("vpx",&vpx);
t->Branch("vpy",&vpy);
t->Branch("vpz",&vpz);
t->Branch("vrand",&vrand);
// Create a new canvas.
TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
c1->SetFillColor(42);
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(6);
c1->GetFrame()->SetBorderMode(-1);
gRandom->SetSeed();
const Int_t kUPDATE = 1000;
for (Int_t i = 0; i < 25000; i++) {
Int_t npx = (Int_t)(gRandom->Rndm(1)*15);
vpx.clear();
vpy.clear();
vpz.clear();
vrand.clear();
for (Int_t j = 0; j < npx; ++j) {
Float_t px,py,pz;
gRandom->Rannor(px,py);
pz = px*px + py*py;
Float_t random = gRandom->Rndm(1);
hpx->Fill(px);
vpx.push_back(px);
vpy.push_back(py);
vpz.push_back(pz);
vrand.push_back(random);
}
if (i && (i%kUPDATE) == 0) {
if (i == kUPDATE) hpx->Draw();
c1->Modified();
c1->Update();
if (gSystem->ProcessEvents())
break;
}
t->Fill();
}
f->Write();
delete f;
}
void read()
{
TFile *f = TFile::Open("hvector.root","READ");
if (!f) { return; }
TTree *t; f->GetObject("tvec",t);
std::vector<float> *vpx = 0;
// Create a new canvas.
TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
c1->SetFillColor(42);
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(6);
c1->GetFrame()->SetBorderMode(-1);
const Int_t kUPDATE = 1000;
TBranch *bvpx = 0;
t->SetBranchAddress("vpx",&vpx,&bvpx);
// Create one histograms
TH1F *h = new TH1F("h","This is the px distribution",100,-4,4);
h->SetFillColor(48);
for (Int_t i = 0; i < 25000; i++) {
Long64_t tentry = t->LoadTree(i);
bvpx->GetEntry(tentry);
for (UInt_t j = 0; j < vpx->size(); ++j) {
h->Fill(vpx->at(j));
}
if (i && (i%kUPDATE) == 0) {
if (i == kUPDATE) h->Draw();
c1->Modified();
c1->Update();
if (gSystem->ProcessEvents())
break;
}
}
// Since we passed the address of a local variable we need
// to remove it.
t->ResetBranchAddresses();
}
void hvector()
{
gBenchmark->Start("hvector");
write();
read();
gBenchmark->Show("hvector");
}
|