Assign vector of pointers of type vector<double> for branch adressing in selector

Dear rooters,

I am trying to write my analysis that inherits from a TSelector (among other classes). In this application I am using root files that have multiple times the same branch for each event, one branch per readout channel. The number of readout channels is not fixed and may varie up to 64.

In the header of the selector I need to declare and initialize all the pointers to be associated with my root file. The issue is that I would like this selector to usable in any case, regardless of the number of channels the file I am trying to read contains. In order to do that I declared all the pointers for the maximum number of channels (64) one by one by hand and have conditional addressing if the branch exists. This makes it quite inconvenient to use in my code.
Instead of decreeing the pointers one by one, my idea was to declare a vector of pointers, which I would then assign the branches of the file, something along the following lines:

   // Vector of pointers
   std::vector<std::vector<double>*> t;
   std::vector<std::vector<double>*> w;
   // Branches
   std::vector<TBranch*> b_t;   //!
   std::vector<TBranch*> b_w;   //!
   // Initialization
   for (int i = 0; i < 64; i++)
         t.push_back(new std::vector<double>(i));
         w.push_back(new std::vector<double>(i)); = 0; = 0;

    // Addressing
   for (unsigned int ich = 0; ich < 64; ich++)
         if (fChain->GetBranch(Form("t%02u", ich)))
             fChain->SetBranchAddress(Form("t%02u", ich), &, &;
             fChain->SetBranchAddress(Form("w%02u", ich), &, &;

Obviously this is failing, with no indication of what is going wrong. I have already included the header file and also the following preprocessor directive:

#ifdef __CINT__
#pragma link C++ class vector< vector<double> >+; 

but it does not seem to help!!
Please let me know if there are any ideas or other solutions.

Ok I found the solution after a lot of trial and error and quite some digging through root and STL.
First off I removed the preprocessor directives, if anything they cause linker errors at runtime compilation.
I added the #include which although I would assume it is already in the standard included libraries form root it is necessary here.
Then the last part is initializing the TBranch pointers within the loop with their default constructor:

	 b_t.push_back(new TBranch());
	 b_w.push_back(new TBranch());

After that it worked!

Yes, the vector of branch pointers needed to be sized properly and doing the push_back had that effect. However it also lead here to a (small) memory leak. The following two options should also work:





Thanks Philippe, you are right, I guess this is because the branch pointers do not need to be initialized. I corrected it!
Thanks again!

They do. ‘resize’ will initialize them using the ‘default construction method’ (i.e. assigning nullptr in this case).