ATLAS Offline Software
MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <iostream>
6 
7 #include "TMath.h" // for TMath::Binomial()
8 #include <TString.h> // for Form
9 
11 
12 using namespace MuonCalib;
13 
14 //*****************************************************************************
15 
16 //:::::::::::::::::::::::::
17 //:: DEFAULT CONSTRUCTOR ::
18 //:::::::::::::::::::::::::
19 
21 
22  init(1,1);
23 
24 }
25 
26 //*****************************************************************************
27 
28 //:::::::::::::::::
29 //:: CONSTRUCTOR ::
30 //:::::::::::::::::
31 
32 Combination::Combination(const unsigned int & n, const unsigned int & k) {
33 
34  init(n, k);
35 
36 }
37 
38 //*****************************************************************************
39 
40 //:::::::::::::::::
41 //:: METHOD init ::
42 //:::::::::::::::::
43 
44 void Combination::init(const unsigned int & n, const unsigned int & k) {
45 
47 // CHECK IF k<=n //
49 
50  if (k>n) {
51  throw std::runtime_error(Form("File: %s, Line: %d\nCombination::init() - error, class number greater than number of elements!", __FILE__, __LINE__));
52  }
53 
55 // ALLOCATE MEMORY //
57 
58  m_k = k;
59  m_n = n;
60  m_index = std::vector<unsigned int>(m_k);
61  m_flag = std::vector<unsigned int>(m_k);
62 
64 // SETUP START CONFIGURATION //
66 
67  reset();
68 
69  return;
70 
71 }
72 
73 //*****************************************************************************
74 
75 //:::::::::::::::::::::::::::::
76 //:: METHOD numberOfElements ::
77 //:::::::::::::::::::::::::::::
78 
79 unsigned int Combination::numberOfElements(void) const {
80 
81  return m_n;
82 
83 }
84 
85 //*****************************************************************************
86 
87 //:::::::::::::::::::::::
88 //:: METHOD whichClass ::
89 //:::::::::::::::::::::::
90 
91 unsigned int Combination::whichClass(void) const {
92 
93  return m_k;
94 
95 }
96 
97 //*****************************************************************************
98 
99 //:::::::::::::::::::::::::::::::::
100 //:: METHOD numberOfCombinations ::
101 //:::::::::::::::::::::::::::::::::
102 
103 unsigned int Combination::numberOfCombinations(void) const {
104 
106 // VARIABLES //
108 
109  int ncomb = TMath::Binomial(m_n, m_k);
110 
112 // RETURN THE NUMBER OF COMBINATIONS //
114 
115  return ncomb;
116 
117 }
118 
119 //*****************************************************************************
120 
121 //:::::::::::::::::::::::::::::::
122 //:: METHOD currentCombination ::
123 //:::::::::::::::::::::::::::::::
124 
126  std::vector<unsigned int> & index_array) const {
127 
128  if (index_array.size()<m_k) {
129  index_array = std::vector<unsigned int>(m_k);
130  }
131  for (unsigned int j=0; j<m_k; j++) {
132  index_array[j] = m_index[j];
133  }
134 
135  return;
136 
137 }
138 
139 //*****************************************************************************
140 
141 //::::::::::::::::::::::::::::
142 //:: METHOD nextCombination ::
143 //::::::::::::::::::::::::::::
144 
145 void Combination::nextCombination(std::vector<unsigned int> & index_array) {
146 
148 // VARIABLES //
150 
151  int loc_flag; // local flag
152 
154 // CHECK SIZE OF index_array//
156 
157  if (index_array.size()<m_k) {
158  index_array = std::vector<unsigned int>(m_k);
159  }
160 
162 // CHECK FLAGS //
164 
165  for (unsigned int j=0; j<m_k; j++) {
166  if (m_index[j] == (m_n-(m_k-j-1))) {
167  m_flag[j] = 1;
168  }
169  }
170  if (m_flag[0] == 1) {
171  reset();
172  currentCombination(index_array);
173  return;
174  }
175  loc_flag = 0;
176  for (unsigned int j=1; j<m_k; j++) {
177  if (m_flag[j] == 1) {
178  m_index[j-1] = m_index[j-1]+1;
179  for (unsigned int l=j; l<m_k; l++) {
180  m_index[l] = m_index[l-1]+1;
181  }
182  loc_flag = 1;
183  break;
184  }
185  }
186  if (loc_flag == 0) {
187  m_index[m_k-1] = m_index[m_k-1]+1;
188  }
189  for (unsigned int j=0; j<m_k; j++) {
190  if (m_index[j] == (m_n-(m_k-j-1))) {
191  m_flag[j] = 1;
192  }
193  else {
194  m_flag[j] = 0;
195  }
196  index_array[j] = m_index[j];
197  }
198 
200 // RETURN THE DESIRED COMBINATION //
202 
203  return;
204 
205 }
206 
207 //*****************************************************************************
208 
209 //::::::::::::::::::
210 //:: METHOD reset ::
211 //::::::::::::::::::
212 
213 void Combination::reset(void) {
214 
215  for (unsigned int j=0; j<m_k; j++) {
216  m_index[j] = j+1;
217  m_flag[j] = 0;
218  }
219 
220  return;
221 
222 }
223 
224 //*****************************************************************************
225 
226 //::::::::::::::::::::::::::::::::
227 //:: METHOD setNewParameters ::
228 //::::::::::::::::::::::::::::::::
229 
230 void Combination::setNewParameters(const unsigned int & nb_elements,
231  const unsigned int & wh_class) {
232 
233  init(nb_elements, wh_class);
234 
235  return;
236 
237 }
MuonCalib::Combination::whichClass
unsigned int whichClass(void) const
get the class of which the combination is
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:91
MuonCalib::Combination::numberOfCombinations
unsigned int numberOfCombinations(void) const
get the number of combinations
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:103
MuonCalib::Combination::Combination
Combination(void)
Default construcor. A combination of 1 out of 1 is created.
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:20
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
MuonCalib::Combination::currentCombination
void currentCombination(std::vector< unsigned int > &index_array) const
get the current combination; the result is stored in the vector index_array
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:125
MuonCalib::Combination::m_k
unsigned int m_k
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/MuonCalibMath/Combination.h:71
beamspotman.n
n
Definition: beamspotman.py:731
MuonCalib::Combination::setNewParameters
void setNewParameters(const unsigned int &nb_elements, const unsigned int &wh_class)
set the number of elements = nb_elements; set the class of which the combination is = wh_class
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:230
MuonCalib
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
Definition: CscCalcPed.cxx:22
Combination.h
MuonCalib::Combination::reset
void reset(void)
go back to the first combination
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:213
MuonCalib::Combination::m_index
std::vector< unsigned int > m_index
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/MuonCalibMath/Combination.h:72
MuonCalib::Combination::m_flag
std::vector< unsigned int > m_flag
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/MuonCalibMath/Combination.h:74
MuonCalib::Combination::nextCombination
void nextCombination(std::vector< unsigned int > &index_array)
get the next combination; the results is stored in the array index_array
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:145
MuonCalib::Combination::init
void init(const unsigned int &n, const unsigned int &k)
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:44
fitman.k
k
Definition: fitman.py:528
MuonCalib::Combination::numberOfElements
unsigned int numberOfElements(void) const
get the number of elements
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/src/Combination.cxx:79
MuonCalib::Combination::m_n
unsigned int m_n
Definition: MuonSpectrometer/MuonCalib/MuonCalibUtils/MuonCalibMath/MuonCalibMath/Combination.h:70