MINT2
DalitzBoxSet.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:17:57 GMT
3 
4 #include "Mint/DalitzBoxSet.h"
5 #include <algorithm>
6 #include <ctime>
7 //#include <algo.h>
8 
9 using namespace std;
10 using namespace MINT;
11 
13 public:
14  bool operator()(const DalitzBox& a, const DalitzBox& b){
15  /*
16  if(a.area() == b.area()){
17  cout << " box a == box b "
18  << "\n " << a
19  << "\n " << b
20  << '\n'
21  << endl;
22  }
23  */
24  return a.area() == b.area();
25  }
26 };
27 
29 public:
30  bool operator()(const DalitzBox& a, const DalitzBox& b){
31  return a.guessedHeight() > b.guessedHeight();
32  }
33 };
35 public:
36  bool operator()(const DalitzBox& a, const DalitzBox& b){
37  return a.guessedHeight() < b.guessedHeight();
38  }
39 };
40 
42 public:
43  bool operator()(const DalitzBox& a, const DalitzBox& b){
44  return a.area() < b.area();
45  }
46 };
47 
49 public:
50  bool operator()(const DalitzBox& a, const DalitzBox& b){
51  return a.area().similar(b.area());
52  }
53 };
54 
56 public:
57  bool operator()(const DalitzBox& a, const DalitzBox& b){
58  return a.area().less(b.area());
59  }
60 };
61 
63  public:
64  bool operator()(const DalitzBox& a, const DalitzBox& b){
65  return a.area().size() < b.area().size();}
66 };
67 
69  : _ready(false)
70 {
71  _rnd = rnd;
72 }
73 
76  , MINT::PolymorphVector<DalitzBox>(other)
77  , _ready(other._ready)
78  , _volumeProbs(other._volumeProbs)
79  , _amps(other._amps)
80  , _rnd(other._rnd)
81 {
82  if(_ready) makeRelations();
83 }
84 
86 
87  // lessBoxBySimilarMappedArea lesser;
88  lesserBoxByMappedArea lesserArea;
90  moreBoxByGuessedHeight moreHeight;
91  //sameBoxBySimilarMappedArea same;
92 
93  sort(this->begin(), this->end(), moreHeight);
94  stable_sort(this->begin(), this->end(), lesserArea);
95 
96  std::vector<DalitzBox>::iterator
97  uniqueEnd = unique(this->begin(), this->end(), same);
98 
99 
100  cout << "removeDuplicates: after sorting: " << endl;
101  for(unsigned int i=0; i< this->size(); i++){
102  bool equalToPrevious = false;
103  if(i >= 1) equalToPrevious = same((*this)[i], (*this)[i-1]);
104  cout << " " << i << ") "
105  << (*this)[i]
106  << "\n is equal to previous? "
107  << equalToPrevious
108  << '\n' << endl;
109  }
110 
111  // sameBoxBySimilarMappedArea same;
112  /*
113  this->erase( (unique(this->begin(), this->end(), same))
114  , this->end());
115  */
116  this->erase(uniqueEnd, this->end());
117 
118  cout << "removeDuplicates: after removing: " << endl;
119  for(unsigned int i=0; i< this->size(); i++){
120  cout << " " << i << ") "
121  << (*this)[i]
122  << '\n' << endl;
123  }
124 }
126  cout << " DalitzBoxSet::sortYourself() " << endl;
127 
128  if(this->size() <= 1) return;
129  cout << " creating sba" << endl;
130  //sortBoxByAreaSize sba;
131  moreBoxByGuessedHeight moreHeight;
132 
133  /*
134  if(is_sorted(this->begin(), this->end(), sba)){
135  return;
136  }
137  */
138  stable_sort(this->begin(), this->end(), moreHeight);
139  // stable_sort(this->begin(), this->end(), sba);
140 
141  for(unsigned int i=0; i< this->size(); i++){
142  (*this)[i].setNumber(i);
143  }
144  cout << " done sorting " << endl;
145 }
146 
148  if(this->empty()) return;
149  (*this)[0].setDaddy(0);
150 
151  if(this->size() <=1) return;
152  // sortYourself();
153  for(unsigned int i=1; i< this->size(); i++){
154  (*this)[i].setDaddy(&((*this)[i-1]));
155  }
156 }
157 
158 double DalitzBoxSet::VolumeSum() const{
159  double sum = 0;
160  for(unsigned int i=0; i< this->size(); i++){
161  cout << " volume number " << i << ") "
162  << (*this)[i].volume()
163  << endl;
164  sum += (*this)[i].volume();
165  }
166  cout << " Volume sum: " << sum;
167  return sum;
168 }
169 
170 double DalitzBoxSet::AreaSum() const{
171  double sum = 0;
172  for(unsigned int i=0; i< this->size(); i++){
173  sum += (*this)[i].area().size();
174  }
175  return sum;
176 }
177 
179  bool dbThis=false;
180  if(this->empty()) return;
181  // intervals each with a length proportional
182  // to the volume of the corresponding box.
183  // All put together add up to one.
184  // For random number generation.
185 
187  _volumeProbs.resize(this->size());
188 
189  double totalVolume = VolumeSum();
190  double sum=0;
191  for(unsigned int i=0; i < this->size(); i++){
192  sum += (*this)[i].volume()/totalVolume;
193  if(dbThis) cout << " volume probs [" << i <<"] = " << sum << endl;
194  _volumeProbs[i] = sum;
195  }
196 }
197 
199  if(! _ready) getReady();
200 
201  if(_volumeProbs.size() != this->size()){
203  }
204 
205  double rndNumber = _rnd->Rndm();
206  for(unsigned int i=0; i< _volumeProbs.size(); i++){
207  if(_volumeProbs[i] > rndNumber) return i;
208  }
209  cout << "WARNING in DalitzBoxSet::pickRandomVolume():"
210  << " How odd - should never have gotten here."
211  << " rndNumber = " << rndNumber
212  << ", _volumeProbs[this->size()-1] = "
213  << _volumeProbs[this->size()-1]
214  << endl;
215  return this->size()-1;
216 }
217 
218 void DalitzBoxSet::add(const DalitzBox& box){
219  _ready = false;
220  cout << " DalitzBoxSet::addBox: adding box "
221  << " with " << box.eventList().size()
222  << " events in it."
223  << endl;
224  this->push_back(box);
225  cout << " done that." << endl;
226 }
227 
229  _ready = false;
230  for(unsigned int i=0; i<boxes.size(); i++){
231  this->push_back(boxes[i]);
232  }
233 }
234 
235 void DalitzBoxSet::add(const DalitzBoxSet& boxes){
236  _ready = false;
237  for(unsigned int i=0; i<boxes.size(); i++){
238  this->push_back(boxes[i]);
239  }
240 }
241 
243  _ready = false;
244  _amps = amps;
245 }
246 
248  for(unsigned int i=0; i < this->size(); i++){
249  (*this)[i].setAmps(_amps);
250  }
251 }
253  for(unsigned int i=0; i < this->size(); i++){
254  (*this)[i].setRnd(_rnd);
255  }
256 }
258  for(unsigned int i=0; i < this->size(); i++){
259  cout << " getting box " << i << " ready." << endl;
260  (*this)[i].getReady();
261  cout << " got it." << endl;
262  }
263 }
264 
266  sortYourself();
267 }
269  time_t startTime = time(0);
270 
271  cout << "DalitzBoxSet::getReady()" << endl;
272  cout << "before removing dupliates: "
273  << this->size() << " boxes." << endl;
275  cout << "after removing dupliates: "
276  << this->size() << " boxes." << endl;
277  sortYourself();
278  cout << " sorted" << endl;
279  makeRelations();
280  cout << " made relations" << endl;
281  cout << "I have the following " << this->size() << " boxes: " << endl;
282  cout << *this << endl;
283 
284  setBoxPDFs();
285  cout << " set the pdfs " << endl;
286  setBoxRnds();
287  cout << " set the rnds" << endl;
288  getBoxesReady();
289  cout << " got Boxes ready" << endl;
291  cout << " made the intervalse " << endl;
292 
293  double deltaT = difftime(time(0), startTime);
294  cout << "DalitzBoxSet: after only:" << deltaT/60 << " min" << endl;
295  cout << "DalitzBoxSet: I am ready." << endl;
296  _ready = true;
297 }
298 
299 
300 DalitzBoxSet DalitzBoxSet::split(unsigned int nWays) const{
301  if(nWays == 1) return (*this);
302 
303  DalitzBoxSet newSet;
304  if(nWays == 0) return newSet;
305 
306  cout << " DalitzBoxSet: before splitting I have "
307  << this->size() << " boxes"
308  << endl;
309  for(unsigned int i=0; i< this->size(); i++){
310  newSet.add((*this)[i].split(nWays));
311  }
312  cout << "after, it's " << newSet.size() << endl;
313  return newSet;
314 }
315 
317 
318  DalitzBoxSet newSet;
319 
320  cout << " DalitzBoxSet: before splitting I have "
321  << this->size() << " boxes"
322  << endl;
323  for(unsigned int i=0; i< this->size(); i++){
324  newSet.add((*this)[i].splitIfWiderThan(maxWidth));
325  }
326  cout << "after, it's " << newSet.size() << endl;
327  return newSet;
328 }
329 
330 
332  if(! _ready) getReady();
333  int boxIndex = pickRandomVolume();
334  //cout << "DalitzBoxSet::tryEvent() in volume " << boxIndex << endl;
335  return (*this)[boxIndex].tryEventForOwner();
336 }
337 
339  if(! _ready) getReady();
340  int boxIndex = pickRandomVolume();
341  //cout << "DalitzBoxSet::tryEvent() in volume " << boxIndex << endl;
342  return (*this)[boxIndex].tryWeightedEventForOwner();
343 }
344 
346  bool dbThis = false;
347  if(! _ready) getReady();
348  if(dbThis)cout << " DalitzBoxSet::newEvent "
349  << " getting event " << endl;
351  while(0 == ptr){
352  ptr = tryWeightedEvent();
353  }
354  if(dbThis) cout << "got event, returning " << ptr << endl;
355  return ptr;
356 }
357 
360  unsigned int counter=0;
361  unsigned int veryLargeNumber = 1000000000;
362  do{
363  newEvent = tryEvent();
364  counter++;
365  }while(0==newEvent && counter < veryLargeNumber);
366 
367  return newEvent;
368 }
371  if(0==evtPtr){
372  cout << "WARNING: DalitzBoxSet::generateEvent()"
373  << " failed to generate event despite LARGE"
374  << " number of tries." << endl;
375  DalitzEvent event;
376  }
377  DalitzEvent evt(*evtPtr);
378  // delete evtPtr;
379  return evt;
380 }
381 
382 bool DalitzBoxSet::setRnd(TRandom* rnd){
383  _ready = false;
384  if(0 == rnd) _rnd = gRandom;
385  else _rnd = rnd; // this *should* do it, but for safety:
386  for(unsigned int i=0; this->size(); i++){
387  (*this)[i].setRnd(_rnd);
388  }
389  return true;
390 }
392  _rnd->SetSeed(time(0)*4);
393  setRnd(_rnd);
394  return true;
395 }
396 void DalitzBoxSet::print(std::ostream& os) const{
397  os << " DalitzBoxSet with " << this->size() << " boxes:"
398  << "\n======================================";
399  for(unsigned int i=0; i< this->size(); i++){
400  os << "\n box " << i << "\n" << (*this)[i];
401  os << "\n--------------------------------------";
402  }
403 }
404 ostream& operator<<(ostream& os, const DalitzBoxSet& bs){
405  bs.print(os);
406  return os;
407 }
408 //
void makeRelations()
void setPDF(MINT::IReturnRealForEvent< IDalitzEvent > *amps)
MINT::IReturnRealForEvent< IDalitzEvent > * _amps
Definition: DalitzBoxSet.h:25
void getBoxesReady()
void makeVolumeProbIntervals()
std::vector< DalitzBox >::iterator end()
DalitzBoxSet split(unsigned int nWays) const
void resize(unsigned int N)
void push_back(const DalitzBox &c)
void sortYourself()
ostream & operator<<(ostream &os, const DalitzBoxSet &bs)
virtual MINT::counted_ptr< DalitzEvent > tryEvent()
bool operator()(const DalitzBox &a, const DalitzBox &b)
bool operator()(const DalitzBox &a, const DalitzBox &b)
virtual MINT::counted_ptr< IDalitzEvent > newEvent()
DalitzBoxSet splitIfWiderThan(double maxWidth) const
bool setRnd(TRandom *rnd)
virtual bool ensureFreshEvents()
virtual MINT::counted_ptr< DalitzEvent > generateEventForOwner()
double guessedHeight() const
Definition: DalitzBox.h:101
bool operator()(const DalitzBox &a, const DalitzBox &b)
void add(const DalitzBox &box)
bool operator()(const DalitzBox &a, const DalitzBox &b)
const MappedDalitzArea & area() const
Definition: DalitzBox.h:103
double VolumeSum() const
TRandom * _rnd
Definition: DalitzBoxSet.h:27
void erase(typename std::vector< DalitzBox >::iterator pos)
std::vector< DalitzBox >::iterator begin()
bool operator()(const DalitzBox &a, const DalitzBox &b)
bool similar(const MappedDalitzArea &ma) const
virtual void getReady()
void callSortYourselfForDebug()
virtual unsigned int size() const
Definition: EventList.h:59
double AreaSum() const
MINT::PolymorphVector< double > _volumeProbs
Definition: DalitzBoxSet.h:24
bool operator()(const DalitzBox &a, const DalitzBox &b)
int pickRandomVolume()
bool less(const MappedDalitzArea &ma) const
virtual MINT::counted_ptr< DalitzEvent > tryWeightedEvent()
void removeDuplicates()
double size() const
bool operator()(const DalitzBox &a, const DalitzBox &b)
DalitzBoxSet(TRandom *rnd=gRandom)
virtual DalitzEvent generateEvent()
DalitzEventList & eventList()
Definition: DalitzBox.h:112
virtual void print(std::ostream &os=std::cout) const