ATLAS Offline Software
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 
14 #include <algorithm>
15 #include <vector>
16 
18 
19 namespace 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;
51  Permutation& operator= (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 
120  bool m_first;
121 
122  };
123 } // end of AnalysisUtils
124 
125 #endif
AnalysisUtils::Combination::get
bool get(OUT &comb)
get a combination.
Definition: AnalysisCombination.h:43
AnalysisUtils::Combination
combination
Definition: AnalysisCombination.h:20
AnalysisUtils::Permutation::m_index
std::vector< unsigned int * > m_index
Definition: AnalysisPermutation.h:117
skel.it
it
Definition: skel.GENtoEVGEN.py:423
AnalysisUtils
utility class to select combination of elements in a collection
Definition: AnalysisCombination.h:16
TruthTest.itE
itE
Definition: TruthTest.py:25
AnalysisUtils::Permutation::m_first
bool m_first
flag to check if first
Definition: AnalysisPermutation.h:120
OUT
#define OUT(dst, src)
Definition: MD5.cxx:316
AnalysisUtils::Permutation::~Permutation
~Permutation()
destructor
Definition: AnalysisPermutation.h:42
AnalysisUtils::Permutation
permutation
Definition: AnalysisPermutation.h:24
lumiFormat.i
int i
Definition: lumiFormat.py:92
AnalysisCombination.h
AnalysisUtils::Permutation::get
bool get(OUT &perm)
get a permutation.
Definition: AnalysisPermutation.h:59
AnalysisUtils::Permutation::m_index_for_comb
std::vector< unsigned int * > m_index_for_comb
indices of elements
Definition: AnalysisPermutation.h:116
AnalysisUtils::Permutation::m_comb
Combination< std::vector< unsigned int * > > * m_comb
combination
Definition: AnalysisPermutation.h:110
AnalysisUtils::Permutation::Permutation
Permutation(COLL *coll, const unsigned int nElement)
constructor
Definition: AnalysisPermutation.h:30
AnalysisUtils::Permutation::goodOnes
bool goodOnes(CALLER *caller, OUT &perm, CRITERIA criteria)
get a permutation which passes a selection criteria
Definition: AnalysisPermutation.h:95
AnalysisUtils::Permutation::m_coll
COLL * m_coll
collection
Definition: AnalysisPermutation.h:113
AnalysisUtils::Permutation::Permutation
Permutation(const Permutation &)=delete
AnalysisUtils::Permutation::operator=
Permutation & operator=(const Permutation &)=delete