source: fact/tools/rootmacros/PulseTemplates/Sample.C@ 14807

Last change on this file since 14807 was 14754, checked in by Jens Buss, 12 years ago
addiditonal cout
  • Property svn:executable set to *
File size: 6.8 KB
Line 
1#include "Sample.h"
2
3Sample::Sample()
4{
5 InitMembers();
6 mpRndNumList = new vector<int>;
7}
8
9Sample::Sample(int size)
10{
11 InitMembers();
12 mpRndNumList = new vector<int>;
13 mpRndNumList->resize(size);
14}
15
16Sample::Sample(vector<int> *RndNumList)
17{
18 InitMembers();
19 mpRndNumList = RndNumList;
20}
21
22Sample::Sample(vector<int> *RndNumList, int size)
23{
24 InitMembers();
25 mpRndNumList = RndNumList;
26 mpRndNumList->resize(size);
27}
28
29Sample::~Sample(void)
30{
31 if (mpRndNumList != NULL)
32 {
33 delete mpRndNumList;
34 }
35}
36
37void Sample::InitMembers()
38{
39 mpRndNumList = NULL;
40 mMinNumber = 0;
41 mMaxNumber = 0;
42 mSampleSize = 0;
43 mSeed = 4357;
44}
45
46//============================================================================
47//ACCESS
48//============================================================================
49void Sample::SetMinNumber(int min){ mMinNumber = min; return;}
50void Sample::SetMaxNumber(int max){ mMaxNumber = max; return;}
51void Sample::SetSampleSize(int size){mSampleSize = size; return;}
52void Sample::SetSeed(int seed)
53{
54 mSeed = seed;
55 mRandom.SetSeed(mSeed);
56 return;
57}
58int Sample::GetMinNumber(){return mMinNumber;}
59int Sample::GetMaxNumber(){return mMaxNumber;}
60int Sample::GetSampleSize(){return mSampleSize;}
61int Sample::GetSeed(){return mSeed;}
62
63void 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
75vector<int>* Sample::GetRndListPtr()
76{
77 return mpRndNumList;
78}
79
80//============================================================================
81//OPERATIONS
82//============================================================================
83int
84Sample::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
94int
95Sample::GenerateRandomInt()
96{
97 return GenerateRandomInt( mMinNumber, mMaxNumber );
98// return 0;
99}
100
101void
102Sample::GenerateSample()
103{
104 GenerateSample( mpRndNumList, mMinNumber, mMaxNumber, mSampleSize);
105 return;
106}
107
108void
109Sample::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
170int
171Sample::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
195int
196Sample::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
242void
243Sample::BootstrapSample()
244{
245 BootstrapSample( mpRndNumList, mMinNumber, mMaxNumber, mSampleSize );
246
247 return;
248}
249
250void
251Sample::BootstrapSample(
252 vector<int> *sampleVector,
253 int numMinEvent,
254 int numMaxEvent,
255 int size)
256{
257 if (size == 0){
258 cout << "Bootstrapping: size of vector = 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
Note: See TracBrowser for help on using the repository browser.