ATLAS Offline Software
Loading...
Searching...
No Matches
AnalysisPermutation.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef ANALYSISUTILS_ANALYSISPERMUTATION_H
6#define ANALYSISUTILS_ANALYSISPERMUTATION_H
7
13
14#include <algorithm>
15#include <vector>
16
18
19namespace AnalysisUtils {
20
23 template <class COLL> class Permutation
24 {
25 public:
26
30 Permutation(COLL *coll, const unsigned int nElement)
31 : m_coll(coll), m_first(true)
32 {
33 // init indices
34 for (unsigned int i=0; i<coll->size(); ++i)
35 m_index_for_comb.push_back(new unsigned int(i));
36
38 }
39
43 {
44 delete m_comb;
45
46 for (unsigned int i=0; i<m_index_for_comb.size(); ++i)
47 delete m_index_for_comb[i];
48 }
49
50 Permutation (const Permutation&) = delete;
52
58 template <class OUT>
59 bool get(OUT &perm)
60 {
61 // init returned vector
62 perm.clear();
63
64 // get combination if first
65 if (m_first)
66 {
67 m_first=false;
68 if (! m_comb->get(m_index)) return false;
69 std::sort(m_index.begin(), m_index.end());
70 }
71 else
72 {
73 // change sequence. if this is the last permutation. get next combination
74 if (! std::next_permutation (m_index.begin(), m_index.end()))
75 {
76 if (! m_comb->get(m_index)) return false;
77 std::sort(m_index.begin(), m_index.end());
78 }
79 }
80
81 // assign
82 std::vector<unsigned int *>::const_iterator it = m_index.begin();
83 std::vector<unsigned int *>::const_iterator itE = m_index.end();
84 for (; it!=itE; ++it)
85 perm.push_back((*m_coll)[**it]);
86
87 return true;
88 }
89
94 template <class CALLER, class OUT, class CRITERIA>
95 bool goodOnes(CALLER *caller, OUT &perm, CRITERIA criteria)
96 {
97 get(perm);
98
99 // check if this passes the criteria
100 if (criteria(caller,perm)) return true;
101
102 // if not, look for next combination
103 return goodOnes(caller, perm, criteria);
104 }
105
106
107 private:
108
111
113 COLL *m_coll;
114
116 std::vector<unsigned int *> m_index_for_comb;
117 std::vector<unsigned int *> m_index;
118
121
122 };
123} // end of AnalysisUtils
124
125#endif
#define OUT(dst, src)
Definition MD5.cxx:316
std::vector< unsigned int * > m_index
bool goodOnes(CALLER *caller, OUT &perm, CRITERIA criteria)
get a permutation which passes a selection criteria
std::vector< unsigned int * > m_index_for_comb
indices of elements
Permutation(const Permutation &)=delete
Combination< std::vector< unsigned int * > > * m_comb
combination
Permutation & operator=(const Permutation &)=delete
bool m_first
flag to check if first
bool get(OUT &perm)
get a permutation.
Permutation(COLL *coll, const unsigned int nElement)
constructor
utility class to select combination of elements in a collection
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.