Error in SetBranchAddress

Hi,

The error is : Error in <TTree::SetBranchAddress>: The pointer type given "Double_t" (8) does not correspond to the type needed "vector<double>" by the branch: m_nl_j

Part of the code is:

vector<double> nm_nl_j;
datatree->SetBranchAddress("m_nl_j",&(nm_nl_j[0]));

Thanks!

vector<double>* nm_nl_j;
datatree->SetBranchAddress("m_nl_j", &nm_nl_j);

Hi,
What is the difference between vector<double>* nm_nl_j and vector<double> nm_nl_j ?
Thanks!

Ehm… The former is pointer, the later is not.

Hi,
Isn’t the first element of a vector a pointer as well?
Thanks!

Here the first element of a vector is a double, because it is vector<double>.

Ahh… sorry… the array name is a pointer to its first element… I got confused.
Anyways, will (*nm_nl_j)[i] be able to get the ith element of the vector?
Just wanted to clarify this… there is no error with this command…
Thanks!

No, it is not.

Yes.

But it will be easier to work with references, I think:

vector<double>* nm_nl_j_;
datatree->SetBranchAddress("m_nl_j", &nm_nl_j_);
for (Long64_t e = 0; e < datatree->GetEntries(); e++) {
	datatree->GetEntry(e);
	vector<double>& nm_nl_j = *nm_nl_j_;
	// use nm_nl_j 
}

Any reason why this causes segmentation violation?

   vector<double>* nm_nl_j;
   double* nnjet;

   datatree->SetBranchAddress("m_nl_j",&nm_nl_j);
   datatree->SetBranchAddress("njet",&nnjet);
    Long64_t ientry=0;

    for (ientry=0; ientry < 100; ientry++)
    {
         datatree->GetEntry(ientry);

         cout<<*nnjet<<endl;

         for (Int_t i=0; i<*nnjet;i++)
         {
              cout<<(*nm_nl_j)[i]<<endl;        
         }
     }

Thanks!

Try double nnjet; instead.
(And don’t change datatree->SetBranchAddress("njet",&nnjet);)

Hi,

It does give an output, but the moment output ends, a new root promt opens with error of segmentation violation and also hangs the current session due to which I have to close the terminal and reopen again… (Also, I changed the cout<<*nnjet to cout<<nnjet )

`…2
85.2737
58.1586
3
148.254
75.6262
99.6121
1
83.4105

root [2]
*** Break *** segmentation violation
`

Thanks!

You don’t need a nnjet branch, std::vector stores its own size.

vector<double>* nm_nl_j_;

datatree->SetBranchAddress("m_nl_j", &nm_nl_j_);
Long64_t ientry = 0;

for (ientry=0; ientry < 100; ientry++)
{
    datatree->GetEntry(ientry);
    vector<double>& nm_nl_j = *nm_nl_j_;

    cout << nm_nl_j.size() << endl;

    for (Int_t i = 0; i < nm_nl_j.size(); i++)
    {
        cout << nm_nl_j[i] << endl;        
    }
}

I agree, but I am getting segmentation violation even with this command…

  
    double nnjet;


    datatree->SetBranchAddress("njet",&nnjet);
    Long64_t ientry=0;

    for (ientry=0; ientry < 100; ientry++)
    {
         datatree->GetEntry(ientry);
         cout<<nnjet<<endl;
     }

Thanks!

double nnjet;

Could you upload .root-file and minimal complete script?

Hi,

Here are the files…

code.C (897 Bytes)
nfile_81.root (823.8 KB)

Thanks!

Ah. Completely forgot. The pointer shall be initialized:
vector<double>* nm_nl_j = 0;

Ahh… now it works…
Thanks a lot!

You may point it to an existing object:

vector<double> nm_nl_j;
vector<double>* nm_nl_j_ = &nm_nl_j;

datatree->SetBranchAddress("m_nl_j", &nm_nl_j_);

And then use nm_nl_j[i] instead of (*nm_nl_j)[i].

1 Like

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