Filling a tree with branches of vector<float>

Hello kind Root helpers,
I am having a great deal of problem with filling a branch of a tree with a vector of floats. In a header file i have initialised some variables from a tree a which to read from (this was actually generated by MakeClass in root), such as:
vector *el_E;
TBranch *b_el_E;
el_E = 0;
fChain->SetBranchAddress(“el_E”, &el_E, &b_el_E);
(With a whole host of other variables there as well).

So with that all set up, i want to read from one Tree (fChain) some of these varialbes, check they meet some criteria and store the entries from those which do in another smaller Tree (Loose), so:

           TTree* Loose = new TTree("Loose","Loose");

            Loose->Branch("el_n", &el_n);
            Loose->Branch("el_E",&el_E,"el_E/F");
            Loose->Branch("el_eta", &el_eta,"el_eta/F");
            e.t.c.....

So i loop through the Tree i am reading from gettin each entry and checking which entries meet my criteria:

if (fChain == 0) return;
cout << "Entries in the tree: " << fChain->GetEntries() << endl;
Long64_t nentries = 0;
if (m_events<0 ) nentries = fChain->GetEntries();
else nentries = m_events;

      for (Long64_t i_event = 0; i_event < nentries; ++i_event) {
            Long64_t event_tree = LoadTree(i_event);
            if (event_tree < 0) break;
            fChain->GetEntry(i_event);
            ResetCounters();

Then i fill my Loose tree based on this:

if(lomu>0 && loel>0){
cout << “Filling Tree\n”;
Loose->Fill();
}

MY PROBLEM
So my problem, is that by using the above method, the tree is created in the output ROOT file with all the correct branches correct, but the branches of vector are filled with 0’s even though oddly the branches of type Int or vector are all filled correctly with non zeros.

WHAT I HAVE TRIED PREVIOUSLY
So i have run into various problems along the way, the first is that if i define a branch like this:
Loose->Branch(“el_pt”, &el_pt); (as instructed by ROOT tutorials)
I will get an error whereby ROOT says it does not recognise:
Error in TTree::Branch: The pointer specified for el_pt is not of a class known to ROOT

However the odd thing is it does recgonise vector and vector just not floats…? So i can create a branch with vectors of integers of doubles without explicitly passing the “I” or “D” e.t.c So i cunningly decided i can recast my variables as a vectors of doubles instead of floats, this results in no problems of ROOT not recognising the pointer but causes segmentation faults when trying to use these varaibles in checking my criteria. Also, it results in the branches storing only 0’s or stupendously high values (i guess the size in bytes of a double or something).

Thanks for taking the time to help me with this problem.

Perhaps i had not been so clear in my explaination, here is a test script i made to try and simulate what i want to do:

void test(){
  // Declare here for histos and so on to keep info
                TFile * Output          = new TFile("testfile.root","RECREATE");

                cout << "Define Tree and setup branches\n";
                //Store skimmed variables after a few of the cuts but not all
                TTree* Loose = new TTree("Loose","Loose");


                std::vector<float>* vpx;
                std::vector<float>* vpy;
                std::vector<float>* vpz;

                //SetBranches for Electrons
                Loose->Branch("vpx", &vpx);
                Loose->Branch("vpy", &vpy);
                Loose->Branch("vpz", &vpz);

                for(int i =0; i<100; i++){
                        vpx->clear();
                        vpy->clear();
                        vpz->clear();

                        for(int j =0; j<10; j++){
                                vpx->push_back(j);
                                vpy->push_back(1);
                                vpz->push_back(3);
                        }

                        Loose->Fill();
                }

                Output->Write();

}//END TEST

It compiles fine, but when run gives this error:
Error in TTree::Branch: The pointer specified for vpx is not of a class known to ROOT
Error in TTree::Branch: The pointer specified for vpy is not of a class known to ROOT
Error in TTree::Branch: The pointer specified for vpz is not of a class known to ROOT

The only difference from what i am doing in my real code is rather than filling the vectors vpx,vpy,vpz they are read from another tree. Hopefully this is a little clearer, as this is causing me considerable grief.

Sorry, for so many posts, but i think i have understood why the above methods do not work. It seems that you need to explitly link to the c++ vector class in order to run correctly (even if a program can compile correctly).

#ifdef MAKECINT
#pragma link C++ class vector+;
#endif

My problem now is that, even after including this, the compiler seems to ignore this link, i took out the ifdef part so just use:

#pragma link C++ class vector+;

But whehn compiling i get this warning: src/analysis.cpp:39: warning: ignoring #pragma link C

So how can i get the compiler not to ignore this flag to link the class?

Hi,

You need to generate a dictionary (see root.cern.ch/drupal/faq#n676).
A simple way is to usegInterpreter->GenerateDictionary("vector<float>","vector");However the dictionary for vector is provide by default and can be loaded via gROOT->ProcessLine("#include <vector>");

Cheers,
Philippe.