Skip to content

Instantly share code, notes, and snippets.

@jiafulow
Last active April 7, 2016 03:36
Show Gist options
  • Select an option

  • Save jiafulow/8877081e032158471578 to your computer and use it in GitHub Desktop.

Select an option

Save jiafulow/8877081e032158471578 to your computer and use it in GitHub Desktop.
Create a TTree filled with double, vector<double>, and vector<vector<double> > #askROOT
Download the C codes and run the following commands:
$ alias rot='root -l -b -q'
$ rot create_vector_vars.C+
$ rot read_vector_vars.C+
Tested in ROOT 6.06/00
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TSystem.h"
#include "TInterpreter.h"
#include <vector>
void create_vector_vars() {
gInterpreter->GenerateDictionary("vector<vector<double> >","vector");
TFile * f1 = TFile::Open("giallo.root", "RECREATE");
gRandom->SetSeed(1);
TTree * t1 = new TTree("tree1", "tree1");
double var1;
std::vector<double> var2;
std::vector<std::vector<double> > var3;
t1->Branch("var1", &var1);
t1->Branch("var2", &var2);
t1->Branch("var3", &var3);
for (int i=0; i<100; i++) {
var1 = gRandom->Rndm();
for (int j=0; j<10; j++) {
var2.push_back(gRandom->Rndm());
var3.push_back(std::vector<double>());
for (int k=0; k<10; k++) {
var3.back().push_back(gRandom->Rndm());
}
}
t1->Fill();
var2.clear();
var3.clear();
}
t1->Write();
f1->Close();
}
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TSystem.h"
#include <vector>
#include <iostream>
void read_vector_vars() {
TFile * f1 = TFile::Open("giallo.root", "READ");
TTree * t1 = (TTree *) f1->Get("tree1");
double var1;
std::vector<double> * var2 = 0;
std::vector<std::vector<double> > * var3 = 0;
t1->SetBranchAddress("var1", &var1);
t1->SetBranchAddress("var2", &var2);
t1->SetBranchAddress("var3", &var3);
for (int i=0; i<t1->GetEntries(); ++i) {
t1->GetEntry(i);
std::cout << "evt " << i << " var1: " << var1 << std::endl;
}
for (int i=0; i<t1->GetEntries(); ++i) {
t1->GetEntry(i);
for (int j=0; j<var2->size(); ++j) {
std::cout << "evt " << i << " var2[" << j << "]: " << var2->at(j) << std::endl;
}
}
for (int i=0; i<t1->GetEntries(); ++i) {
t1->GetEntry(i);
for (int j=0; j<var3->size(); ++j) {
for (int k=0; k<var3->at(j).size(); ++k) {
std::cout << "evt " << i << " var3[" << j << "][" << k << "]: " << var3->at(j).at(k) << std::endl;
}
}
}
f1->Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment