Conversion from 'std::vector<short>' to 'Double_t'

Hi,
How could I fix the next error?
note: candidate function not viable: no known conversion from ‘std::vector’ to ‘Double_t’ (aka ‘double’)

Code:

	std::vector<short> *v1 		= {0}; 
	std::vector<short> *v2 		= {0};
	DTTree->SetBranchAddress("v1", 			&v1);
	DTTree->SetBranchAddress("v2", 			&v2);

TH2S *hvvsv = new TH2S("h_vvsv", "TH2Svvsv", 1, 0, 5, 1, 5, 9); 

for(int i=0; i<v1->size(); i++)
	{
		DTTree->GetEntry(i);
		
		hvvsv         ->Fill(v1[i], v2[i]);

}

Could you be more exact, i.e. in which line does that error appear (i.e. post the complete error message).

The loop seems strange. v1 is connected to the tree, but you didn’t call GetEntry, yet you are calling the size() function. Do you want to loop over the tree instead?

treeEntries = DTTree->GetEntries(); 
for (Long64_t i = 0; i < treeEntries; ++i) { ... }

It seems to me that you should have two loops, one over the tree entries and one over the vector.

Other than that:

  • Note that your histogram has 1 bin in x and 1 bin in y. Seems pointless!
  • I’d suggest to write std::vector<short> *v1 = {0}; as std::vector<short> *v1 = nullptr; (makes it clearer that the pointer is initialized).

Edit:
v1 and v2 are pointers to vectors. Use (*v1)[i] to access the i-th element.

for(int i=0; iGetEntries(); i++)
{
DTTree->GetEntry(i);

for(int i=0; i<v1->size(); i++)
{	
	hvvsv       ->Fill(v1[i], v2[i]);	
}
}

Actually I am not sure about how to organize the loops. The error appear because of the Fill function: hvvsv ->Fill(v1[i], v2[i]);

Ok. When using two nested loops, use different names for the loop variables to avoid confusion.

v1 is a pointer to vector<short>
v1[0] is *(v1 + 0) - that’s the vector itself. v1[1] would be another vector<short>, not the first element!
*v1 is your vector
(*v1)[0] is the first element of the vector. Alternative: v1->operator[](0)

1 Like

Dear behrenhoff,
I don’t get you last sentence
(*v1)[0] is the first element of the vector. Alternative: v1->operator[](0)

if I change “hvvsv ->Fill(v1[i], v2[i])” to hvvsv ->Fill(*v1[i], *v2[i]);

the new error that I get is : “error: indirection requires pointer operand (‘std::vector’ invalid)”

Look up https://en.cppreference.com/w/cpp/language/operator_precedence
Then ask yourself whether you can can omit parentheses at will.

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