ROOT logo

From $ROOTSYS/tutorials/tree/hvector.C

//
// 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");   
}
 hvector.C:1
 hvector.C:2
 hvector.C:3
 hvector.C:4
 hvector.C:5
 hvector.C:6
 hvector.C:7
 hvector.C:8
 hvector.C:9
 hvector.C:10
 hvector.C:11
 hvector.C:12
 hvector.C:13
 hvector.C:14
 hvector.C:15
 hvector.C:16
 hvector.C:17
 hvector.C:18
 hvector.C:19
 hvector.C:20
 hvector.C:21
 hvector.C:22
 hvector.C:23
 hvector.C:24
 hvector.C:25
 hvector.C:26
 hvector.C:27
 hvector.C:28
 hvector.C:29
 hvector.C:30
 hvector.C:31
 hvector.C:32
 hvector.C:33
 hvector.C:34
 hvector.C:35
 hvector.C:36
 hvector.C:37
 hvector.C:38
 hvector.C:39
 hvector.C:40
 hvector.C:41
 hvector.C:42
 hvector.C:43
 hvector.C:44
 hvector.C:45
 hvector.C:46
 hvector.C:47
 hvector.C:48
 hvector.C:49
 hvector.C:50
 hvector.C:51
 hvector.C:52
 hvector.C:53
 hvector.C:54
 hvector.C:55
 hvector.C:56
 hvector.C:57
 hvector.C:58
 hvector.C:59
 hvector.C:60
 hvector.C:61
 hvector.C:62
 hvector.C:63
 hvector.C:64
 hvector.C:65
 hvector.C:66
 hvector.C:67
 hvector.C:68
 hvector.C:69
 hvector.C:70
 hvector.C:71
 hvector.C:72
 hvector.C:73
 hvector.C:74
 hvector.C:75
 hvector.C:76
 hvector.C:77
 hvector.C:78
 hvector.C:79
 hvector.C:80
 hvector.C:81
 hvector.C:82
 hvector.C:83
 hvector.C:84
 hvector.C:85
 hvector.C:86
 hvector.C:87
 hvector.C:88
 hvector.C:89
 hvector.C:90
 hvector.C:91
 hvector.C:92
 hvector.C:93
 hvector.C:94
 hvector.C:95
 hvector.C:96
 hvector.C:97
 hvector.C:98
 hvector.C:99
 hvector.C:100
 hvector.C:101
 hvector.C:102
 hvector.C:103
 hvector.C:104
 hvector.C:105
 hvector.C:106
 hvector.C:107
 hvector.C:108
 hvector.C:109
 hvector.C:110
 hvector.C:111
 hvector.C:112
 hvector.C:113
 hvector.C:114
 hvector.C:115
 hvector.C:116
 hvector.C:117
 hvector.C:118
 hvector.C:119
 hvector.C:120
 hvector.C:121
 hvector.C:122
 hvector.C:123
 hvector.C:124
 hvector.C:125
 hvector.C:126
 hvector.C:127
 hvector.C:128
 hvector.C:129
 hvector.C:130
 hvector.C:131
 hvector.C:132
 hvector.C:133
 hvector.C:134
 hvector.C:135
 hvector.C:136
 hvector.C:137
 hvector.C:138
 hvector.C:139
 hvector.C:140
 hvector.C:141
 hvector.C:142
 hvector.C:143
 hvector.C:144
 hvector.C:145
 hvector.C:146
 hvector.C:147
 hvector.C:148
 hvector.C:149
 hvector.C:150
 hvector.C:151
 hvector.C:152
 hvector.C:153
 hvector.C:154
thumb