CMS Wiki Pages

users:clange

Code

Sorting a vector based on another vector: http://stackoverflow.com/questions/236172/how-do-i-sort-a-stdvector-by-the-values-of-a-different-stdvector

```struct tlvordering {
bool operator ()(std::pair<size_t, std::vector<TLorentzVector>::const_iterator> const& a, std::pair<size_t, std::vector<TLorentzVector>::const_iterator> const& b) {
return (*(a.second)).Pt() > (b.second->Pt());
}
};

template <typename T>
std::vector<T> sort_from_tlvref(
std::vector<T> const& in,
std::vector<std::pair<size_t, std::vector<TLorentzVector>::const_iterator> > const& reference
) {
std::vector<T> ret(in.size());

size_t const size = in.size();
for (size_t i = 0; i < size; ++i)
ret[i] = in[reference[i].first];

return ret;
}

void sortJetsByPt(std::vector<unsigned int>& selJets, // index vector
std::vector<TLorentzVector>& selJetsP4,
std::vector<int>& selJetsSVMassBins,
std::vector<double>& selJetsSVMassSum,
std::vector< std::vector<bool> >& selJetsBTags,
std::vector<JetFlavor::Code>& selJetsFlav) {

// make a pair of index order and TLorentzVector
std::vector<std::pair<size_t, std::vector<TLorentzVector>::const_iterator> > order(selJetsP4.size());
size_t n = 0;
for (std::vector<TLorentzVector>::const_iterator it = selJetsP4.begin(); it != selJetsP4.end(); ++it, ++n) {
order[n] = std::make_pair(n, it);
}
// sort order pair by jet Pt
sort(order.begin(), order.end(), tlvordering());
// user order pair as reference for sorting all vectors
selJets = sort_from_tlvref(selJets, order);
selJetsP4 = sort_from_tlvref(selJetsP4, order);
selJetsSVMassBins = sort_from_tlvref(selJetsSVMassBins, order);
selJetsSVMassSum = sort_from_tlvref(selJetsSVMassSum, order);
selJetsBTags = sort_from_tlvref(selJetsBTags, order);
selJetsFlav = sort_from_tlvref(selJetsFlav, order);

// loop over jets and change order
//   for (size_t iJ = 0; iJ < selJets.size()-1; ++iJ) {
//
//     if (selJetsP4[iJ].Pt() < selJetsP4[iJ+1].Pt()) {
//       jetOrder[iJ] = iJ+1
//     }
//   }

}```