Sorting a vector of TLorentzVector

Hi, I have a vector of TLorentzVectors that I want to sort in descending order of Pt().

I tried to sort the particles as they were being added by scanning through the list and adding it at the appropriate point with the insert() function. That code is below.

(input to code are the int “tag” and the TLorentzVector “particle”)

	vector<TLorentzVector> v_gam;
	bool added;
	added = false;
	if ( tag == 0 ) {
		for ( int i=0; i<v_gam.size(); i++) {
			if ( v_gam.at(i).Pt() < particle.Pt() ) {
				v_gam.insert(v_gam.begin()+i,particle);
				added = true;
			}
		}
		if ( !added ) {
			v_gam.push_back(particle);
		}
	}

That code just hangs. I don’t get errors, the code runs and it just never stops running. It correctly adds the first particle to the vector, but then it hangs on the second particle.

So then I tried creating a subroutine to sort between two particles based on the Pt()

bool event::reorder(const TLorentzVector &a, const TLorentzVector &b)
{
	return a.Pt() > b.Pt();
}

and called it using

	std::sort(v_gam.begin(), v_gam.end(), reorder);

But this gave me the following error:

calculations.cpp:699: error: argument of type ‘bool (event::)(const TLorentzVector&, const TLorentzVector&)’ does not match ‘bool (event::*)(const TLorentzVector&, const TLorentzVector&)’

I found that solution online where someone had a vector of a struct that they were trying to sort. They claimed the code worked, and I don’t really understand why mine doesn’t. (stackoverflow.com/questions/4892 … of-structs)

Can anyone offer advice? Thanks.

Never mind. I found out what was wrong with my original “insert” method.

I just kept adding a particle to the list if it had a higher PT, and so the loop just kept running. v_gam.size() kept increasing! Silly me.