MINT2
EventList.h
Go to the documentation of this file.
1 #ifndef EVENTLIST_HH
2 #define EVENTLIST_HH
3 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
4 // status: Mon 9 Feb 2009 19:17:54 GMT
5 
6 #include <iostream>
7 //#include <vector>
8 #include <ctime>
9 #include <cmath>
10 
11 #include "TRandom.h"
12 
13 #include "Mint/IEventList.h"
14 #include "Mint/PolymorphVector.h"
15 
16 namespace MINT{
17 
18  template<typename EVENT_TYPE>
19  class EventList
20  : virtual public IEventList<EVENT_TYPE >
21  , public MINT::PolymorphVector<EVENT_TYPE> // std::vector<EVENT_TYPE>
22  {
23  public:
25  : IEventList<EVENT_TYPE>()
26  , MINT::PolymorphVector<EVENT_TYPE>()
27  {
28  }
30  : IEventList<EVENT_TYPE>()
31  , MINT::PolymorphVector<EVENT_TYPE>(other)
32  {
33  }
34  virtual ~EventList(){}
35 
38  if(this == &other) return *this;
39  this->clear();
40  for(unsigned int i=0; i<other.size(); i++){
41  this->push_back(other[i]);
42  }
43  return *this;
44  }
45 
46  virtual EVENT_TYPE& operator[](unsigned int i){
48  //return std::vector<EVENT_TYPE>::operator[](i);
49  }
50  virtual const EVENT_TYPE& operator[] (unsigned int i) const{
52  //return std::vector<EVENT_TYPE>::operator[](i);
53  }
54 
55  virtual EVENT_TYPE getEvent(unsigned int i)const{
57  }
58 
59  virtual unsigned int size() const{
61  }
62 
63  virtual bool Add(const EVENT_TYPE & evt){
64  this->push_back(evt);
65  return true;
66  }
67  virtual bool Add(const EventList<EVENT_TYPE> & addList){
68  for(unsigned int i = 0; i < addList.size(); i++){
69  this->push_back(addList[i]);
70  }
71  return true;
72  }
73 
74  /*
75  double getMax(IReturnReal<EVENT_TYPE>* amps){
76  double max=-1;
77  int counter=0;
78  for(int i=0; i < this->size(); i++){
79  double d=amps->RealVal((*this)[i]);
80  if(d > max || 0 == counter) max=d;
81  counter++;
82  };
83  return max;
84  }
85 
86  double getMin(IReturnReal* amps){
87  double min = +9999;
88  int counter=0;
89  this->Start();
90  while(this->Next()){// 1st call to next gives 1st event
91  double d=amps->RealVal();
92  if(d < min || 0 == counter) min=d;
93  counter++;
94  }
95  return min;
96  }
97 
98  int findMaxAndThrowAwayData(double& maxValue
99  , IGetRealEvent<EVENT_TYPE>* amps
100  , int Nevents = -1
101  , TRandom* rnd = gRandom)
102  {
103  // bool dbThis=true;
104  std::cout << "EventList::findMaxAndThrowAwayData:"
105  << " before throwing away data, my size is "
106  << this->size() << std::endl;
107 
108  time_t startTime = time(0);
109 
110  int rememberSize = this->size();
111  unsigned int counter=0;
112 
113  std::vector<double> vals;
114  vals.resize(this->size());
115  this->Start();
116  amps->setEventRecord(this);
117  while(this->Next()){// 1st call to next gives 1st event
118  double d=amps->RealVal();
119  if(d > maxValue || 0 == counter) maxValue=d;
120  vals[counter] = d;
121 
122  unsigned int printEvery = size()/10;
123  if(printEvery <=0) printEvery = 5;
124  if(counter <= 2) printEvery=1;
125  else if(counter <= 200) printEvery=100;
126 
127  if(counter < 5 || 0 == counter%(printEvery)){
128  std::cout << " calculated amps for event "
129  << counter
130  << ", its value is " << d
131  << std::endl;
132  double deltaT = difftime(time(0), startTime);
133  std::cout << " this took " << deltaT << " s";
134  if(deltaT > 0){
135  std::cout << "; rate (before throwing away) = "
136  << counter/deltaT
137  << " evts/s";
138  }
139  std::cout << std::endl;
140  }
141  counter++;
142  };
143  amps->resetEventRecord();
144 
145 
146  double epsilon = 0.2;
147  // maxValue = maxValue + fabs(maxValue * epsilon);
148 
149  double CL = 1.0 - 1./(this->size()*10000);
150  std::cout << "maxValue before = " << maxValue << std::endl;
151  maxValue = generalisedPareto_estimateMaximum(vals, CL);
152  std::cout << " maxValue after " << maxValue << std::endl;
153  maxValue *= 1.0 + epsilon;
154  std::cout << "Now added " << epsilon * 100 << "% for safety "
155  << maxValue << std::endl;
156 
157  EventList<EVENT_TYPE, EVENT_TYPE> newList;
158 
159  for(counter=0; counter<this->size(); counter++){
160  double d=vals[counter];
161  if(rnd->Rndm()*maxValue < d){
162  newList.Add( (*this)[counter] );
163  }
164  unsigned int printEvery = size()/2;
165  if(printEvery <=0) printEvery = 2;
166 
167  if(counter < 2 || 0 == counter%(printEvery)){
168  std::cout << " remembering amps for event "
169  << counter
170  << ", its value is " << d
171  << std::endl;
172  }
173  if(Nevents > 0 && (int)newList.size() >= Nevents) break;
174  }
175  (*this) = newList;
176  std::cout << "now my size has changed to " << this->size()
177  << std::endl;
178  std::cout << " So the waste factor is ";
179  if(size() > 0) std::cout << rememberSize/this->size();
180  else std::cout << " infinity! - that's big!";
181  std::cout << std::endl;
182 
183 
184  double deltaTFinal = difftime(time(0), startTime);
185  std::cout << " this took " << deltaTFinal/60.0 << " min";
186  if(deltaTFinal > 0){
187  std::cout << " rate = " << (this->size()/deltaTFinal) << " evts/s"
188  << " or " << (this->size()/deltaTFinal) *60 << " evts/m"
189  << " or " << (this->size()/deltaTFinal) *60.*60./1000.
190  << "k evts/h";
191  }
192  std::cout << std::endl;
193 
194  std::cout << " ---------------\n " << std::endl;
195  this->Start();
196  return this->size();
197  }
198  int justThrowAwayData(double maxValue
199  , IGetRealEvent<EVENT_TYPE>* amps
200  , TRandom* rnd = gRandom)
201  {
202  // bool dbThis=true;
203  std::cout << "EventList::justThrowAwayData:"
204  << " before throwing away data, my size is "
205  << this->size() << std::endl;
206 
207  time_t startTime = time(0);
208 
209  int rememberSize = this->size();
210  unsigned int counter=0;
211 
212  EventList<EVENT_TYPE, EVENT_TYPE> newList;
213 
214  this->Start();
215  amps->setEventRecord(this);
216  while(this->Next()){// 1st call to next gives 1st event
217  double d=amps->RealVal();
218  if(rnd->Rndm()*maxValue < d){
219  newList.Add( (*this)[counter] );
220  }
221 
222  unsigned int printEvery = size()/10;
223  if(printEvery <=0) printEvery = 5;
224  if(counter <= 2) printEvery=1;
225  else if(counter <= 200) printEvery=100;
226 
227  if(counter < 5 || 0 == counter%(printEvery)){
228  std::cout << " amps for event "
229  << counter
230  << ": " << d
231  << ". " << newList.size()
232  << " events passed ";
233  double deltaT = difftime(time(0), startTime);
234  std::cout << ". Took " << deltaT << " s";
235 
236  if(deltaT > 0){
237  std::cout << "; rate (before/after throwing away) = "
238  << counter/deltaT
239  << " / " << newList.size()/deltaT
240  << " evts/s";
241  }
242  std::cout << std::endl;
243  }
244  counter++;
245  };
246  amps->resetEventRecord();
247 
248  (*this) = newList;
249  std::cout << "now my size has changed to " << this->size()
250  << std::endl;
251  std::cout << " So the waste factor is ";
252  if(size() > 0) std::cout << rememberSize/this->size();
253  else std::cout << " infinity! - that's big!";
254  std::cout << std::endl;
255 
256 
257  double deltaTFinal = difftime(time(0), startTime);
258  std::cout << " this took " << deltaTFinal/60.0 << " min" << std::endl;
259 
260  this->Start();
261  return this->size();
262  }
263  double findMax(double& maxInThisSample
264  , IGetRealEvent<EVENT_TYPE>* amps
265  )
266  {
267  // bool dbThis=true;
268  std::cout << "EventList::findMax:" << std::endl;
269 
270  time_t startTime = time(0);
271 
272  //int rememberSize = this->size();
273  unsigned int counter=0;
274  std::vector<double> vals;
275  vals.resize(this->size());
276 
277  this->Start();
278  amps->setEventRecord(this);
279  while(this->Next()){// 1st call to next gives 1st event
280  double d=amps->RealVal();
281  if(d > maxInThisSample || 0 == counter) maxInThisSample=d;
282  vals[counter] = d;
283 
284  unsigned int printEvery = size()/10;
285  if(printEvery <=0) printEvery = 5;
286  if(counter <= 2) printEvery=1;
287  else if(counter <= 200) printEvery=100;
288 
289  if(counter < 5 || 0 == counter%(printEvery)){
290  std::cout << " calculated amps for event "
291  << counter
292  << ", its value is " << d
293  << std::endl;
294  double deltaT = difftime(time(0), startTime);
295  std::cout << " this took " << deltaT << " s";
296  if(deltaT > 0){
297  std::cout << "; rate (before throwing away) = "
298  << counter/deltaT
299  << " evts/s";
300  }
301  std::cout << std::endl;
302  }
303  counter++;
304  };
305  amps->resetEventRecord();
306 
307 
308  double epsilon = 0.2;
309  // maxInThisSample = maxInThisSample + fabs(maxInThisSample * epsilon);
310 
311  double CL = 1.0 - 1./(this->size()*10000);
312  std::cout << "maxValue in this sample = " << maxInThisSample << std::endl;
313  double estimatedMaxOfParentSample = generalisedPareto_estimateMaximum(vals, CL);
314  std::cout << " maxValue after " << estimatedMaxOfParentSample << std::endl;
315  estimatedMaxOfParentSample *= 1.0 + epsilon;
316  std::cout << "Now added " << epsilon * 100 << "% for safety "
317  << estimatedMaxOfParentSample << std::endl;
318 
319  double deltaTFinal = difftime(time(0), startTime);
320  std::cout << " this took " << deltaTFinal/60.0 << " min";
321  if(deltaTFinal > 0){
322  std::cout << " rate = " << (this->size()/deltaTFinal) << " evts/s"
323  << " or " << (this->size()/deltaTFinal) *60 << " evts/m"
324  << " or " << (this->size()/deltaTFinal) *60.*60./1000.
325  << "k evts/h";
326  }
327  std::cout << std::endl;
328 
329  return estimatedMaxOfParentSample;
330  }
331 
332  double findMax(IGetRealEvent<EVENT_TYPE>* amps
333  ){
334  double maxInThisSample;
335  return findMax(maxInThisSample, amps);
336  }
337 
338  // for bw compatibility
339  int throwAwayData(IGetRealEvent<EVENT_TYPE>* amps
340  , int Nevents=-1, TRandom* rnd=gRandom)
341  {
342  double maxValue = -9999;
343  return findMaxAndThrowAwayData(maxValue, amps, Nevents, rnd);
344  }
345 
346  */
347 };
348 
349 }//namespace MINT
350 
351 #endif
352 //
virtual bool Add(const EVENT_TYPE &evt)
Definition: EventList.h:63
virtual EVENT_TYPE & operator[](unsigned int i)
Definition: EventList.h:46
void push_back(const EVENT_TYPE &c)
virtual ~EventList()
Definition: EventList.h:34
EventList(const EventList< EVENT_TYPE > &other)
Definition: EventList.h:29
virtual bool Add(const EventList< EVENT_TYPE > &addList)
Definition: EventList.h:67
T & at(unsigned int i)
virtual EVENT_TYPE getEvent(unsigned int i) const
Definition: EventList.h:55
EventList< EVENT_TYPE > & operator=(const EventList< EVENT_TYPE > &other)
Definition: EventList.h:37
unsigned int size() const
virtual unsigned int size() const
Definition: EventList.h:59