TBranch and std::vector

Dear all,

I want to reduce the size of a Ntuple, using TBranch with a std::vector as a type, is it possible something like?

vector MyType;
t2.Branch(“MyType”, gstep.MyType, “MyType/??”);

long is not a portable data type.
Use int or Int_t for a portable 32 bits integer
Use Long64_t for a portable 64 bits integer.
eg, with the file longs.C below, do

root > .L longs.C+ root > longs();

#include "TFile.h" #include "TTree.h" #include <vector> #if defined (__MAKECINT__) #pragma link C++ class vector<Long64_t>+; #endif void longs() { vector<Long64_t> *MyType = new vector<Long64_t>; TTree *T = new TTree("T","test"); T->Branch("longs",&MyType); T->Print(); }

Rene

Thanks Rene,

I tried it as follows,

[code]#include “wp_v12000605.hh”
#include <vector.h>

#if defined (MAKECINT)
#pragma link C++ class vector<Long64_t>+;
#endif
using namespace std;

#define efficiency wp_v12000605
efficiency *d;

int main(int argc, char **argv)
{
vector<Float_t> *Myvar = new vector<Float_t>;
TFile f(“tree2.root”,“recreate”);

TTree *T = new TTree(“T”,“test”);
//T->Branch(“MyEventNumber”, “MyEventNumber/I”);
T->Branch(“MyPtGen”,&Myvar);

TChain *tre = new TChain(“CollectionTree”);
tre->Add("/misc/xdata03/mpedraza/central/trig1_misal1_csc11.005611.Wprime2000emutau.recon.NTUP.v12000605/trig1_misal1_csc11.005611.Wprime2000emutau.recon.$
d= new efficiency(tre);

int nentries = tre->GetEntries();

for (Long64_t jentry=0; jentry<nentries;jentry++) {
tre->GetEvent(jentry);
//T.MyEventNumber= d->EventNumber;
copy(((d->PtGen)).begin(),((d->PtGen)).end(), Myvar->begin());
}
T->Write();
f.Close();

return 0;
}
[/code]

and I got

Error in <TTree::Branch>: The pointer specified for MyPtGen is not of a class known to ROOT

Could you help me with that, thank you.

[/code]

Hi,

You use both vector<Float_t> and vector<Long64_t>, which one did you really mean to use? You are also missing a call to T->Fill();

Cheers,
Philippe

Thanks Phillipe,
Sorry I used a Float_t variable for simplicity, in fact I need to use both and also Int_t, UInt_t, and others. Well I changed the variable to be consistent with my first question, sorry.

I made the changes that you mentioned,

#include "wp_v12000605.hh"
...
#include <vector.h>

#if defined (__MAKECINT__)
#pragma link C++ class vector<Long64_t>+;
#endif



using namespace std;

#define efficiency wp_v12000605
 
efficiency *d;




int main(int argc, char **argv)
{
  vector<Long64_t> *Myvar = new vector<Long64_t>; 
  TFile f("tree2.root","recreate");
     
  TTree *T = new TTree("T","test");
  //T->Branch("MyEventNumber", "MyEventNumber/I");
  T->Branch("MyKMothNt",&Myvar);
   

  TChain *tre = new TChain("CollectionTree");
  tre->Add("/misc/xdata03/mpedraza/central/trig1_misal1_csc11.005611.Wprime2000emutau.recon.NTUP.v12000605/trig1_misal1_csc11.005611.Wprime2000emutau.recon.NTUP.v12000605.root");
  d= new efficiency(tre);
  
  int nentries = tre->GetEntries();
  
  for (Long64_t jentry=0; jentry<nentries;jentry++) {
    tre->GetEvent(jentry);
    //T.MyEventNumber= d->EventNumber;
    copy((*(d->KMothNt)).begin(),(*(d->KMothNt)).end(), Myvar->begin());
    T->Fill();
  }
  T->Write();
  f.Close();
  
  return 0;
}

And I am still getting the same Error

[code] (gdb) run
[Thread debugging using libthread_db enabled]
[New Thread -1239169328 (LWP 21002)]
Error in TTree::Branch: The pointer specified for MyKMothNt is not of a class known to ROOT

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1239169328 (LWP 21002)]
0x0804b5d5 in std::__copy<false, std::random_access_iterator_tag>::copy<long*, long long*> (__first=0xe072a50, __last=0xe072fb0, __result=0x0) at /usr/lib/gcc/i486-linux-gnu/4.1.2/…/…/…/…/include/c++/4.1.2/bits/stl_algobase.h:285
(gdb) [\code]

Thank you,

You changed/added your new types in one place only. You should add them too to the following block

#if defined (__MAKECINT__) #pragma link C++ class vector<Long64_t>+; #endif

Rene

Thank you Rene.

I have been trying to generate an NTuple using the tips you have given me, and I could not add information to the vectors, hence I came back to your code (that works) and I have not found the way to add the information either,

[code]#include “TFile.h”
#include “TTree.h”
#include
#if defined (MAKECINT)
#pragma link C++ class vector<Long64_t>+;
#endif
void longs() {
vector<Long64_t> longs;
TFile f(“tree2.root”,“recreate”);
vector<Long64_t> *MyType = new vector<Long64_t>;
TTree *T = new TTree(“T”,“test”);
T->Branch(“longs”,&MyType);
//longs.push_back(5.);
//(*MyType).push_back(5.);
(*MyType)[0]=5;
T->Print();
T->Write();

f.Close();
//delete f;

}
[/code]

What should I use?

code.push_back(5.); [/code]should have worked, how did it fail?

humm … also you are missing the all important T->Fill() to actually upload the data for entry in the TTree.

Philippe.

Sorry for the late answer… It worked, with CINT. But since I was trying to compile it took me a little bit more to construct the dictionary for the definition of the new types,

thank you very much.