Proper way to get a vector from a TChain with friends

Hi rooters,

I am trying to get some variables from a root file which has the structure you can see on the picture below. What I am using is:

TChain* fChain = new TChain(“l1EventTree/L1EventTree”);
TChain* fCaloTowerEmu = new TChain(“l1CaloTowerEmuTree/L1CaloTowerTree”);

After defining the TChains, I add the root files to the chains and then I am creating the friendship:

fChain -> AddFriend(fCaloTowerEmu);
fChain -> SetMakeClass(1);
TFriendElement friendTreeElement = (TFriendElement)fChain -> GetTree() -> GetListOfFriends() -> First();
friendTreeElement -> GetTree() -> SetMakeClass(1);

My problem is that when I am trying to get some variables using pointers, if it’s a single variable it works and if it’s a vector it doesn’t. The code I am using is:

unsigned int *lumi; lumi =0;
short *nECALTP; nECALTP=0;
vector < short > *hcalTP_iEta; hcalTP_iEta=0;
TBranch *b_lumi;
TBranch *b_nECALTP;
TBranch *b_hcalTP_iEta;
fChain->SetBranchAddress(“lumi” , &lumi , &b_lumi);
fChain->SetBranchAddress(“nECALTP”, &nECALTP, &b_nECALTP);
fChain->SetBranchAddress(“hcalTPieta”, &hcalTP_iEta, &b_hcalTP_iEta);

So the code works for “lumi” and “nECALTP” which are in two different chains/trees which have friendship, but it is does not for the “hcalTPieta”.

I load the tree for each entry and either it crashes without even accessing the vector and I get a segmentation fault when I am trying to get the entry:

nb = fChain->GetEntry(jentry);

either the code is running but when I ask for the vector’s size I get something enormous and if I loop over the vector I get the segmentation when trying to access an element of the vector.

std::cout << GenP_Pt -> size() << std::endl;
for (unsigned int i=0; i < GenP_Pt -> size(); i++){
float genP_Pt = GenP_Pt -> at(i);
std::cout << genP_Pt << std::endl;

What am I missing?


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

For branches that contains array of simple type, you need to allocate the array explicitly;

unsigned int *lumi = new unsigned int[ max_number_of_elements ];