1 | #include "Sample.h"
|
---|
2 |
|
---|
3 | Sample::Sample()
|
---|
4 | {
|
---|
5 | InitMembers();
|
---|
6 | mpRndNumList = new vector<int>;
|
---|
7 | }
|
---|
8 |
|
---|
9 | Sample::Sample(int size)
|
---|
10 | {
|
---|
11 | InitMembers();
|
---|
12 | mpRndNumList = new vector<int>;
|
---|
13 | mpRndNumList->resize(size);
|
---|
14 | }
|
---|
15 |
|
---|
16 | Sample::Sample(vector<int> *RndNumList)
|
---|
17 | {
|
---|
18 | InitMembers();
|
---|
19 | mpRndNumList = RndNumList;
|
---|
20 | }
|
---|
21 |
|
---|
22 | Sample::Sample(vector<int> *RndNumList, int size)
|
---|
23 | {
|
---|
24 | InitMembers();
|
---|
25 | mpRndNumList = RndNumList;
|
---|
26 | mpRndNumList->resize(size);
|
---|
27 | }
|
---|
28 |
|
---|
29 | Sample::~Sample(void)
|
---|
30 | {
|
---|
31 | if (mpRndNumList != NULL)
|
---|
32 | {
|
---|
33 | delete mpRndNumList;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | void Sample::InitMembers()
|
---|
38 | {
|
---|
39 | mpRndNumList = NULL;
|
---|
40 | mMinNumber = 0;
|
---|
41 | mMaxNumber = 0;
|
---|
42 | mSampleSize = 0;
|
---|
43 | mSeed = 4357;
|
---|
44 | }
|
---|
45 |
|
---|
46 | //============================================================================
|
---|
47 | //ACCESS
|
---|
48 | //============================================================================
|
---|
49 | void Sample::SetMinNumber(int min){ mMinNumber = min; return;}
|
---|
50 | void Sample::SetMaxNumber(int max){ mMaxNumber = max; return;}
|
---|
51 | void Sample::SetSampleSize(int size){mSampleSize = size; return;}
|
---|
52 | void Sample::SetSeed(int seed)
|
---|
53 | {
|
---|
54 | mSeed = seed;
|
---|
55 | mRandom.SetSeed(mSeed);
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | int Sample::GetMinNumber(){return mMinNumber;}
|
---|
59 | int Sample::GetMaxNumber(){return mMaxNumber;}
|
---|
60 | int Sample::GetSampleSize(){return mSampleSize;}
|
---|
61 | int Sample::GetSeed(){return mSeed;}
|
---|
62 |
|
---|
63 | void Sample::GetRndListValues(vector<int> *RndNumList)
|
---|
64 | {
|
---|
65 | vector<int>::iterator it;
|
---|
66 |
|
---|
67 | for ( it=mpRndNumList->begin() ; it < mpRndNumList->end(); it++ )
|
---|
68 | {
|
---|
69 | RndNumList->push_back(*it);
|
---|
70 | }
|
---|
71 |
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | vector<int>* Sample::GetRndListPtr()
|
---|
76 | {
|
---|
77 | return mpRndNumList;
|
---|
78 | }
|
---|
79 |
|
---|
80 | //============================================================================
|
---|
81 | //OPERATIONS
|
---|
82 | //============================================================================
|
---|
83 | int
|
---|
84 | Sample::GenerateRandomInt(
|
---|
85 | int min,
|
---|
86 | int max)
|
---|
87 | {
|
---|
88 | // int rndNum = min + mRandom.Integer(max - min);
|
---|
89 | int rndNum = (int) mRandom.Uniform(min, max);
|
---|
90 | // cout << rndNum << endl;
|
---|
91 | return rndNum;
|
---|
92 | }
|
---|
93 |
|
---|
94 | int
|
---|
95 | Sample::GenerateRandomInt()
|
---|
96 | {
|
---|
97 | return GenerateRandomInt( mMinNumber, mMaxNumber );
|
---|
98 | // return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void
|
---|
102 | Sample::GenerateSample()
|
---|
103 | {
|
---|
104 | GenerateSample( mpRndNumList, mMinNumber, mMaxNumber, mSampleSize);
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void
|
---|
109 | Sample::GenerateSample(
|
---|
110 | vector<int>* sampleVector,
|
---|
111 | int min,
|
---|
112 | int max,
|
---|
113 | int size)
|
---|
114 | {
|
---|
115 | if ( size > max - min)
|
---|
116 | {
|
---|
117 | cout << "sample size is larger than range of sample, will set size to range" << endl;
|
---|
118 | size = max - min + 1;
|
---|
119 | }
|
---|
120 | //resize destination vector for pulled numbers
|
---|
121 | sampleVector->resize(size);
|
---|
122 |
|
---|
123 | //calculate qunatity of numbers in range
|
---|
124 | int qunatityOfNumbers = max - min + 1;
|
---|
125 |
|
---|
126 | //crate a vector list of ordered numbers in defined range
|
---|
127 | vector<int> listOfNumbers;
|
---|
128 | listOfNumbers.resize(qunatityOfNumbers);
|
---|
129 |
|
---|
130 | //fill a list of ordered numbers in defined range
|
---|
131 | for (int i = min; i <= max; i++)
|
---|
132 | {
|
---|
133 | listOfNumbers.at(i)=i;
|
---|
134 | }
|
---|
135 |
|
---|
136 | //container to fill in numbers with ordering
|
---|
137 | set<int> sampleSet;
|
---|
138 |
|
---|
139 | //container for insert result
|
---|
140 | bool result;
|
---|
141 | for (int i = 0; i < size; i++)
|
---|
142 | {
|
---|
143 | int randomNumber = GenerateRandomInt( 0, qunatityOfNumbers-i);
|
---|
144 | result = sampleSet.insert(listOfNumbers.at(randomNumber)).second;
|
---|
145 | if (result)
|
---|
146 | {
|
---|
147 | // cout << "rndNR " << randomNumber << endl;
|
---|
148 | listOfNumbers.erase(listOfNumbers.begin()+randomNumber);
|
---|
149 | }
|
---|
150 | else if (!result)
|
---|
151 | {
|
---|
152 | cout << " pulled number exists, pulling again" << endl;
|
---|
153 | i--;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | set<int>::iterator it;
|
---|
158 |
|
---|
159 | int counter = 0;
|
---|
160 | for ( it=sampleSet.begin() ; it != sampleSet.end(); it++ )
|
---|
161 | {
|
---|
162 | sampleVector->at(counter)=*it;
|
---|
163 | counter++;
|
---|
164 | }
|
---|
165 |
|
---|
166 | return;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | int
|
---|
171 | Sample::BootstrapVector(vector<double> *inVector, vector<double> *outVector)
|
---|
172 | {
|
---|
173 | //get size of sample to be bootstrapped
|
---|
174 | int sampleSize = inVector->size();
|
---|
175 |
|
---|
176 | //Vector with positions in original sample
|
---|
177 | vector<int> entryID (sampleSize,0);
|
---|
178 |
|
---|
179 | //calculate wich entries from inVector will be put into outVector
|
---|
180 | BootstrapSample(&entryID, 0, sampleSize, sampleSize );
|
---|
181 |
|
---|
182 | vector<int>::iterator it;
|
---|
183 |
|
---|
184 | //Loop over entryID vector to fill content from inVector to outVector
|
---|
185 | int counter = 0;
|
---|
186 | for ( it=entryID.begin() ; it != entryID.end(); it++ )
|
---|
187 | {
|
---|
188 | outVector->at(counter) = inVector->at(*it);
|
---|
189 | counter++;
|
---|
190 | }
|
---|
191 |
|
---|
192 | return counter + 1;
|
---|
193 | }
|
---|
194 |
|
---|
195 | int
|
---|
196 | Sample::BootstrapTH1(TH1* inputHisto, TH1* outHisto)
|
---|
197 | {
|
---|
198 | //compute the median for 1-d histogram h1
|
---|
199 | int nbins = inputHisto->GetXaxis()->GetNbins();
|
---|
200 |
|
---|
201 | //we need to get the binning
|
---|
202 |
|
---|
203 | //entries of TH1
|
---|
204 | vector<float> entries;
|
---|
205 |
|
---|
206 | //quantity of entries in bin
|
---|
207 | int quantity = 0;
|
---|
208 | float value = 0;
|
---|
209 | for (int i=0;i<nbins;i++)
|
---|
210 | {
|
---|
211 | value = inputHisto->GetBinLowEdge(i);
|
---|
212 | quantity = inputHisto->GetBinContent(i);
|
---|
213 | // cout << value << "(" << quantity << ") ";
|
---|
214 | for (int j = 0; j < quantity; j++)
|
---|
215 | {
|
---|
216 | entries.push_back(value);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | //get size of sample to be bootstrapped
|
---|
221 | int sampleSize = entries.size();
|
---|
222 |
|
---|
223 | //Vector with positions in original sample
|
---|
224 | vector<int> entryID (sampleSize,0);
|
---|
225 |
|
---|
226 | //calculate a list with random EntryIDs and fill it into entryID
|
---|
227 | BootstrapSample(&entryID, 0, sampleSize, sampleSize );
|
---|
228 |
|
---|
229 | //Loop over entryID vector to bootstrap the histogram
|
---|
230 | int counter = 0;
|
---|
231 | for ( unsigned int i = 0 ; i < entryID.size(); i++ )
|
---|
232 | {
|
---|
233 | outHisto->Fill(
|
---|
234 | entries.at( entryID.at(i) )
|
---|
235 | );
|
---|
236 | counter++;
|
---|
237 | }
|
---|
238 |
|
---|
239 | return counter + 1;
|
---|
240 | }
|
---|
241 |
|
---|
242 | void
|
---|
243 | Sample::BootstrapSample()
|
---|
244 | {
|
---|
245 | BootstrapSample( mpRndNumList, mMinNumber, mMaxNumber, mSampleSize );
|
---|
246 |
|
---|
247 | return;
|
---|
248 | }
|
---|
249 |
|
---|
250 | void
|
---|
251 | Sample::BootstrapSample(
|
---|
252 | vector<int> *sampleVector,
|
---|
253 | int numMinEvent,
|
---|
254 | int numMaxEvent,
|
---|
255 | int size)
|
---|
256 | {
|
---|
257 | if (size == 0){
|
---|
258 | cout << "size = 0; nothing to do" << endl;
|
---|
259 | return;
|
---|
260 | }
|
---|
261 |
|
---|
262 | //resize the sample vector to size of boostrapped sample
|
---|
263 | sampleVector->resize(size);
|
---|
264 |
|
---|
265 | //list of rndnumbers pulled with putting back
|
---|
266 | multiset<int> sampleSet;
|
---|
267 |
|
---|
268 | //loop over samplesize to generate random numbers to fill into sampleset
|
---|
269 | for (int i = 0; i < size; i++)
|
---|
270 | {
|
---|
271 | int randomNumber = GenerateRandomInt( numMinEvent, numMaxEvent);
|
---|
272 | sampleSet.insert(randomNumber);
|
---|
273 | }
|
---|
274 |
|
---|
275 | multiset<int>::iterator it;
|
---|
276 |
|
---|
277 | // set<int>::iterator it;
|
---|
278 |
|
---|
279 | int counter = 0;
|
---|
280 | // loop over list of rndnumbers and fill their entries into sampleVector
|
---|
281 | // entries are vector positions
|
---|
282 | for ( it=sampleSet.begin() ; it != sampleSet.end(); it++ )
|
---|
283 | {
|
---|
284 | sampleVector->at(counter)=*it;
|
---|
285 | counter++;
|
---|
286 | }
|
---|
287 |
|
---|
288 | return;
|
---|
289 | }
|
---|
290 | //NOTE: crashes for some reason if size is smaller than max-min
|
---|