MINT2
DalitzBox.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/DalitzBox.h"
5 
6 #include "Mint/Minimiser.h"
7 #include "Mint/Minimisable.h"
9 #include "Mint/FitParameter.h"
10 #include "Mint/GeneralisedPareto.h"
11 
12 #include <iostream>
13 #include <ctime>
14 
15 using namespace std;
16 using namespace MINT;
17 
18 bool bigDebug = false;
19 bool fastTest = false;
20 bool veryFastTest = false;
21 bool ultraFast = true;
22 
23 bool no_reuse = true;
24 
26  : public Minimisable
27 {
28 protected:
29 
35 
37 
38 public:
39  double getVal(){
40  bool db=false;
41  if(db) cout << " called getVal() " << endl;
42  counted_ptr<DalitzEvent> evtPtr(_box->area().makeEventForOwner(_s0, _s1, _s2, _s3, _s4));
43  if(db) cout << " got event Ptr " << evtPtr << endl;
44  if(0 == evtPtr) return 0;
45  if(_box->insideDaddysArea(*evtPtr)) return 0;
46 
47  if(db) cout << " evtPtr->print() ";
48  if(db)evtPtr->print();
49  if(db) cout << " that worked " << endl;
50  double val = _box->ampsWithPhaseSpace((*evtPtr));
51  if(db) cout << " got val " << val << endl;
52  if(db) cout << " returning " << -val << endl;
53  return -val;
54  }
55 
57  , FitParameter& s1
58  , FitParameter& s2
59  , FitParameter& s3
60  , FitParameter& s4
61  , DalitzBox* box
62  , MinuitParameterSet* mps
63  )
64  : Minimisable(mps)
65  , _s0(s0)
66  , _s1(s1)
67  , _s2(s2)
68  , _s3(s3)
69  , _s4(s4)
70  , _box(box)
71  {
72  }
73 
74 };
75 
77  : _max_s0(-1), _max_s1(-1), _max_s2(-1), _max_s3(-1), _max_s4(-1)
78  , _maxPhaseSpace(-9999)
79  , _number(-1)
80  , _name("noName")
81  , _ready(false)
82  , _area()
83  , _pat()
84  , _amps(0)
85  , _rnd(0)
86  , _height(-9999)
87  , _eventList()
88  , _nTries(0)
89  , _nEventsForTries(0)
90  , _daddy(0)
91  , _guessedHeight(-9999)
92  , _heightProblems(0)
93  , _flatPhaseSpace(0)
94 {}
95 
96 
99  , TRandom* rnd)
100  : _max_s0(-1), _max_s1(-1), _max_s2(-1), _max_s3(-1), _max_s4(-1)
101  , _maxPhaseSpace(-9999)
102  , _number(-1)
103  , _name("noName")
104  , _ready(false)
105  , _area(pat, rnd)
106  , _pat(pat)
107  , _amps(0)
108  , _rnd(rnd)
109  , _height(-9999)
110  , _eventList()
111  , _nTries(0)
112  , _nEventsForTries(0)
113  , _daddy(0)
114  , _guessedHeight(-9999)
115  , _heightProblems(0)
116  , _flatPhaseSpace(0)
117 {
118  setAmps(amps);
119 }
120 
122  , const DalitzCoordinate& limit
124  , TRandom* rnd)
125  : _max_s0(-1), _max_s1(-1), _max_s2(-1), _max_s3(-1), _max_s4(-1)
126  , _maxPhaseSpace(-9999)
127  , _number(-1)
128  , _name("noName")
129  , _ready(false)
130  , _area(pat, limit, rnd)
131  , _pat(pat)
132  , _amps(0)
133  , _rnd(rnd)
134  , _height(-9999)
135  , _eventList()
136  , _nTries(0)
137  , _nEventsForTries(0)
138  , _daddy(0)
139  , _guessedHeight(-9999)
140  , _heightProblems(0)
141  , _flatPhaseSpace(0)
142 {
143  setAmps(amps);
144 }
145 
147  , const std::vector<DalitzCoordinate>& limits
149  , TRandom* rnd)
150  : _max_s0(-1), _max_s1(-1), _max_s2(-1), _max_s3(-1), _max_s4(-1)
151  , _maxPhaseSpace(-9999)
152  , _number(-1)
153  , _name("noName")
154  , _ready(false)
155  , _area(pat, limits, rnd)
156  , _pat(pat)
157  , _amps(0)
158  , _rnd(rnd)
159  , _height(-9999)
160  , _eventList()
161  , _nTries(0)
162  , _nEventsForTries(0)
163  , _daddy(0)
164  , _guessedHeight(-9999)
165  , _heightProblems(0)
166  , _flatPhaseSpace(0)
167 {
168  setAmps(amps);
169 }
170 
172  : _max_s0(other._max_s0)
173  , _max_s1(other._max_s1)
174  , _max_s2(other._max_s2)
175  , _max_s3(other._max_s3)
176  , _max_s4(other._max_s4)
177  , _maxPhaseSpace(other._maxPhaseSpace)
178  , _number(other._number)
179  , _name(other._name)
180  , _ready(other._ready)
181  , _area(other._area)
182  , _pat(other._pat)
183  , _amps(other._amps)
184  , _rnd(other._rnd)
185  , _height(other._height)
186  , _eventList(other._eventList) // expensive!
187  , _nTries(other._nTries)
188  , _nEventsForTries(other._nEventsForTries)
189  , _daddy(other._daddy)
190  , _guessedHeight(other._guessedHeight)
191  , _heightProblems(other._heightProblems)
192  , _flatPhaseSpace(other._flatPhaseSpace)
193 {
194  // _ready = false;
195  // cout << "copying box" << endl;
196 }
197 
199  _ready = false;
200  _amps = amps;
201  _eventList.clear();
202  if(0 == _amps) return false;
203  return true;
204 }
205 
207 }
208 
209 void DalitzBox::setDaddy(const DalitzBox* daddy){
210  _ready = false;
211  _daddy = daddy;
212 }
213 
214 bool DalitzBox::insideArea(const DalitzEvent& evt) const{
215  /*
216  cout << "Hello from DalitzBox::insideArea()" << endl;
217  cout << " my area is " << _area << endl;
218  cout << ", or the same with (): " << area() << endl;
219  cout << " and the event: " << evt << endl;
220  cout << " Let's check if the event is inside the area: " << endl;
221  */
222  bool inside = area().isInside(evt);
223 
224  /*
225  cout << "Goodbye from DalitzBox::insideArea(); returning "
226  << inside << endl;
227  */
228 
229  return inside;
230 }
231 
233  if(0 == _daddy) return false;
234  return _daddy->insideMyOrDaddysArea(evt);
235 }
236 
238  return insideArea(evt) || insideDaddysArea(evt);
239 }
240 
242  cout << "making " << N << " flat event" << endl;
243  int counter = 0;
244  int guessedWasteFactor = 110*5;
245  if(fastTest) guessedWasteFactor = 150;
246 
247  for(int i=0; i< N*guessedWasteFactor; i++){
248  _nTries ++;
249  bool printdbg = (_nTries < 5 || 0 == _nTries%100000);
250  if(printdbg) cout << " nTries " << _nTries << endl;
251 
252  counted_ptr<DalitzEvent> evt(area().makeEventForOwner());
253  if(printdbg) cout << " made event" << endl;
254 
255  if(0 != evt && (! insideDaddysArea(*evt))){ // this should work
256  if(printdbg)cout << " area OK " << endl;
257  _eventList.Add(*evt);
258  if(printdbg)cout << " added event" << endl;
259 
260  if(++counter >= N) break;
261  }
262  }
263 
264  cout << " got " << _eventList.size() << endl;
265  return _nTries;
266 }
267 
269  bool dbThis=false;
270 
271  if(0 == _amps) return 1;
272  if(dbThis) cout << " the event: " << endl;
273  if(dbThis) evt.print();
274  if(dbThis) cout << " ... now getting RealVal() " << endl;
275  double val = _amps->RealVal(evt);
276  if(dbThis) cout << " all done, returning " << val << endl;
277  return val;
278 }
279 
281  if(_flatPhaseSpace) return 1;
282  return evt_in.phaseSpace();
283 }
284 
286  bool dbThis=false;
287 
288  double a = amps(evt_in);
289  if(dbThis) cout << "amps " << a << endl;
290  double ps = phaseSpace(evt_in);
291  if(dbThis) cout << "ps " << ps << endl;
292 
293  return a*ps;
294 }
295 
297  MinuitParameterSet parset;
298 
299  /*
300  // step 1: Find starting values:
301  int minEvents= 10000000;
302  int maxEvents= 10000001;
303  int counter = 0;
304  double max = -1;
305  double s0=0.5, s1=0.5, s2=0.5, s3=0.5, s4=0.5;
306  bool foundOne=false;
307  cout << " starting loop" << endl;
308  while((! (foundOne && counter > minEvents)) && counter < maxEvents){
309  counter++;
310  // cout << " counter " << counter << endl;
311  double tmp_s0 = _rnd->Rndm();
312  double tmp_s1 = _rnd->Rndm();
313  double tmp_s2 = _rnd->Rndm();
314  double tmp_s3 = _rnd->Rndm();
315  double tmp_s4 = _rnd->Rndm();
316  counted_ptr<DalitzEvent> tmpEvtPtr(area().makeEventForOwner(tmp_s0, tmp_s1, tmp_s2, tmp_s3, tmp_s4));
317  if(0 == tmpEvtPtr) continue;
318  //cout << " found a non-zero event!!!" << endl;
319  if(this->insideDaddysArea(*tmpEvtPtr)) return 0;
320 
321  foundOne=true;
322  _amps->setEvent(&(*tmpEvtPtr));
323  double val = _amps->RealVal();
324  _amps->resetEventRecord();
325  if(val > max){
326  max=val;
327  s0 = tmp_s0; s1=tmp_s1, s2=tmp_s2, s3=tmp_s3, s4=tmp_s4;
328  }
329  }
330 
331  if(max < 0){
332  cout << " didn't find starting point "
333  << " giving up; "
334  << endl;
335  _height = -1;
336  return true;
337  }
338 
339  cout << " max = " << max << " at "
340  << s0 << ", " << s1 << ", " << s2 << ", " << s3 << ", " << s4 << endl;
341 
342  */
343  FitParameter fit_scale0("scale0"
344  , 0
345  , _max_s0
346  , 0.2
347  , -0.05
348  , 1.05
349  , &parset
350  );
351  FitParameter fit_scale1("scale1"
352  , 0
353  , _max_s1
354  , 0.2
355  , -0.05
356  , 1.05
357  , &parset
358  );
359  FitParameter fit_scale2("scale2"
360  , 0
361  , _max_s2
362  , 0.2
363  , -0.05
364  , 1.05
365  , &parset
366  );
367  FitParameter fit_scale3("scale3"
368  , 0
369  , _max_s3
370  , 0.2
371  , -0.05
372  , 1.05
373  , &parset
374  );
375  FitParameter fit_scale4("scale4"
376  , 0
377  , _max_s4
378  , 0.2
379  , -0.05
380  , 1.05
381  , &parset
382  );
383 
384  FindMaxFCN fcn(fit_scale0
385  , fit_scale1
386  , fit_scale2
387  , fit_scale3
388  , fit_scale4
389  , this, &parset);
390 
391  Minimiser mini(&fcn);
392  // mini.Command("Set Strategy 2");
393  mini.prepFit();
394  mini.Command("Simplex 10000000");
395  mini.Command("MIGRAD");
396 
397  double safetyMargin = 0;
398 
399  _height = -(mini.getFCNVal() * (1.0 + safetyMargin));
400 
401  if(_height <= 0.0) _height = -1.0;
402  cout << " setting height to " << _height << endl;
403  return true;
404 }
405 
406 bool DalitzBox::estimateHeight(std::vector<double>& vals
407  ){
408  bool dbThis=false;
409  int maxEvents = 100000;
410  int maxTries = 10000000;
411  double meanPhaseSpaceSum = 0;
412  if(fastTest){
413  maxEvents = 10000;
414  maxTries = 1000000;
415  }
416  if(veryFastTest || ultraFast){
417  maxEvents = 1000;
418  maxTries = 1000000;
419  }
420 
421  if(guessedHeight() > 0){
423  return 0;
424  }
425 
426  // double saveFactor = 2.0;
427 
428  cout << "DalitzBox::estimateHeight" << endl;
429  //time_t startTime = time(0);
430 
431  double max = -9999;
432 
433  int numEvents=0, counter=0;
434  while(numEvents < maxEvents && counter < maxTries){
435  counter++;
436  double tmp_s0 = _rnd->Rndm();
437  double tmp_s1 = _rnd->Rndm();
438  double tmp_s2 = _rnd->Rndm();
439  double tmp_s3 = _rnd->Rndm();
440  double tmp_s4 = _rnd->Rndm();
441  counted_ptr<DalitzEvent> tmpEvtPtr(area().makeEventForOwner(tmp_s0, tmp_s1, tmp_s2, tmp_s3, tmp_s4));
442  if(0 == tmpEvtPtr) continue;
443  if(dbThis) cout << "estimate height got event "
444  << " printing it: " << endl;
445  if(dbThis) cout << *tmpEvtPtr << endl;
446 
447  //cout << " found a non-zero event!!!" << endl;
448  if(this->insideDaddysArea(*tmpEvtPtr)) continue;
449 
450  numEvents++;
451 
452 
453  DalitzEvent tmpEvt(*tmpEvtPtr);
454  double val = ampsWithPhaseSpace(tmpEvt);
455  vals.push_back(val);
456  if(val > max){
457  max=val;
458  _max_s0 = tmp_s0;
459  _max_s1 = tmp_s1;
460  _max_s2 = tmp_s2;
461  _max_s3 = tmp_s3;
462  _max_s4 = tmp_s4;
463  }
464  double p = phaseSpace(tmpEvt);
465 
466  meanPhaseSpaceSum += p;
467  if(p > _maxPhaseSpace){
468  _maxPhaseSpace = p;
469  }
470  };
471 
472  double meanPhaseSpace = meanPhaseSpaceSum/((double)numEvents);
473  cout << "mean phase space " << meanPhaseSpace
474  << ", max: " << _maxPhaseSpace << endl;
475 
476  //double epsilon = 0.25;
477 
478  double CL = 1.0 - 1.e-7;
479  std::cout << "actual max = " << max << std::endl;
480 
481  if(numEvents < 50){
482  cout << " no Paret of only " << _eventList.size()
483  << " events. Taking 5*max = 5*" << max
484  << " = " << 5*max
485  << " and return false." << endl;
486  _height = 5.0 * max;
487  return false;
488  }
489 
490  double actualMax=-1, paretoMax=-1;
491 
493  , CL
494  , actualMax
495  , paretoMax
496  );
497 
498  std::cout << " Pareto max " << paretoMax << std::endl;
499  /*
500  max *= 1.0 + epsilon;
501  std::cout << "Now added " << epsilon * 100 << "% for safety "
502  << max << std::endl;
503 
504  max *= saveFactor;
505 
506  std::cout << "and multiplied by safety factor "
507  << saveFactor
508  << " giving " << max
509  << endl;
510  */
511  cout << "compare to guessed height: "
512  << guessedHeight()*_maxPhaseSpace << endl;
513  _height = max;
514 
515  if(fabs( (paretoMax - actualMax)/actualMax) > 0.3 ){
516  return false;
517  }
518 
519  return true;
520 }
521 
522 bool DalitzBox::estimateHeight_old(std::vector<double>& vals
523  ){
524  double saveFactor = 2.0;
525 
526  cout << "DalitzBox::estimateHeight" << endl;
527  if(false && bigDebug){
528  vals.resize(_eventList.size(), 1);
529  _height=1;
530  return true;
531  }
532  if(_eventList.empty()){
533  _height = 0;
534  return false;
535  }
536  time_t startTime = time(0);
537 
538  unsigned int counter=0;
539 
540  vals.clear();
541 
542  vals.resize(_eventList.size());
543 
544  double max = -9999;
545 
546  double maxPhaseSpace = -9999;
547 
548  for(unsigned int i=0; i < _eventList.size(); i++){
549  IDalitzEvent& evt = _eventList[i];
550  double p = evt.phaseSpace();
551  if(p > maxPhaseSpace || 0 == counter){
552  maxPhaseSpace = p;
553  }
554  double d=ampsWithPhaseSpace(evt);
555 
556  if(d > max || 0 == counter){
557  max=d;
558  // _maxEvent = *(_eventList.getEvent());
559  }
560  vals[i] = d;
561  if(i < 5 || 0 == i%(_eventList.size()/20 + 1)){
562  std::cout << " calculated amps for event "
563  << i
564  << ", its value is " << d
565  << std::endl;
566  double deltaT = difftime(time(0), startTime);
567  std::cout << " this took " << deltaT << " s";
568  if(deltaT > 0.5){
569  std::cout << "; rate (before throwing away) = "
570  << i/deltaT
571  << " evts/s";
572  }
573  std::cout << std::endl;
574  }
575  };
576 
577  cout << " max phase space: " << maxPhaseSpace << endl;
578 
579  double epsilon = 0.25;
580 
581  double CL = 1.0 - 1./_eventList.size();
582  std::cout << "actual max = " << max << std::endl;
583 
584  if(_eventList.size() < 50){
585  cout << " no Paret of only " << _eventList.size()
586  << " events. Taking 5*max = 5*" << max
587  << " = " << 5*max
588  << " and return false." << endl;
589  _height = 5.0 * max;
590  return false;
591  }
592 
593  double actualMax=-1, paretoMax=-1;
594 
596  , CL
597  , actualMax
598  , paretoMax
599  );
600 
601  std::cout << " Pareto max " << paretoMax << std::endl;
602  max *= 1.0 + epsilon;
603  std::cout << "Now added " << epsilon * 100 << "% for safety "
604  << max << std::endl;
605 
606  max *= saveFactor;
607 
608  std::cout << "and multiplied by safety factor "
609  << saveFactor
610  << " giving " << max
611  << endl;
612  cout << "compare to guessed height: "
613  << guessedHeight()*maxPhaseSpace << endl;
614  _height = max;
615 
616  if(fabs( (paretoMax - actualMax)/actualMax) > 0.3 ){
617  return false;
618  }
619 
620  return true;
621 }
622 
624  double safetyFactor = 2.5;
625 
626  time_t startTime = time(0);
627  cout << "making starter set" << endl;
628  // unsigned int maxEvents = 400000;
629  int NperLoop = 20000;
630  // int maxTries = maxEvents * 110;
631  int maxLoop = 5;
632  if(fastTest){
633  NperLoop = 10000;
634  maxLoop = 3;
635  }
636  if(veryFastTest){
637  NperLoop = 1000;
638  maxLoop = 1;
639  }
640 
641  bool successfulHeightEstimate = false;
642  std::vector<double> vals;
643 
644  int counter = 0;
645  double prevHeight = -0.1;
646  double heightErrorEstimate = 0.1;
647 
648  bool doOldHeight=(guessedHeight() < 10);
649  bool doMinuitHeight=false;
650  if(doOldHeight){
651  do{
652  // makeFlatEventsKeepAll(NperLoop);
653  successfulHeightEstimate = estimateHeight(vals);
654  heightErrorEstimate
655  = fabs(prevHeight - _height)/( 0.5*(fabs(prevHeight) + fabs(_height) ));
656 
657  /*
658  successfulHeightEstimate |= heightErrorEstimate < 0.25;
659  // the above line means: I believe it if pareto and actual
660  // are close - or if at least pareto is consistent.
661  */
662 
663  successfulHeightEstimate &= heightErrorEstimate < 0.25;
664  // the above line means: I believe it if pareto and actual
665  // are close AND if Pareto is consistent.
666  counter++;
667  double deltaT = difftime(time(0), startTime);
668  cout << " making " << counter
669  << " " << NperLoop << "-events starter set(s)"
670  << " took " << deltaT/60 << " min" << endl;
671  cout << " current heightErrorEstimate: " << heightErrorEstimate << endl;
672  prevHeight = _height;
673  // no event inside box - box must have 0 volume
674  // (most likely overlap 100% with other volume)
675  }while(counter < maxLoop
676  && (! successfulHeightEstimate)
677  );
678 
679  cout << " DONE \"old\" height guess, now MINUIT " << endl;
680  }
681  double search_height = _height;
682  double minuit_height = -1;
683  if(doMinuitHeight){
684  successfulHeightEstimate |= estimateHeightMinuit();
685  minuit_height = _height;
686  }
687 
688 
689  _height = ( search_height > minuit_height ? search_height : minuit_height);
691 
692  cout << "Different Heights:"
693  << "\n\t search_height " << search_height
694  << "\n\t minuit_height " << minuit_height
695  << "\n\t guessed height " << guessedHeight()
696  << "\n\t chosen height " << _height
697  << endl;
698 
699  if(! successfulHeightEstimate){
700  cout << "DalitzBox::makeStarterSet "
701  << "WARNING - no reliable estimate of box-height"
702  << endl;
703  cout << " I'll multiply it by " << 1.0 + 2*heightErrorEstimate
704  << ", and continue..." << endl;
705  // _height *= 1.0 + 2.0*heightErrorEstimate;
706  }else{
707  cout << "DalitzBox::makeStarterSet "
708  << "I think I found a reliable Height Estimate of "
709  << _height
710  << endl;
711  /* _height *= 1.0 + 2.0*heightErrorEstimate;
712  cout << "For safety, I'll add 2* the Error estimate of "
713  << heightErrorEstimate << " which gives a final height of "
714  << _height
715  << endl;
716  */
717  }
718  cout << " appliying safety factor of " << safetyFactor << endl;
719  _height *= safetyFactor;
720  cout << "final height " << _height << endl;
721 
722  //throwAwayData(vals);
723 
724  return true;
725 }
726 
727 int DalitzBox::throwAwayData(const std::vector<double>& vals){
728  if(false && bigDebug) return 0;
729  time_t startTime = time(0);
730 
731  if(vals.size() != _eventList.size()){
732  cout << " ERROR in DalitzBox::throwAwayData: "
733  << " vals.size()=" << vals.size()
734  << ", but _eventList.size()="
735  << _eventList.size()
736  << endl;
737  throw "this is bad.";
738  }
739 
740  DalitzEventList newList;
741 
742  int rememberSize = _eventList.size();
743 
744  unsigned int counter=0;
745 
746  for(unsigned int i=0; i < _eventList.size(); i++){
747  IDalitzEvent& evt(_eventList[i]);
748 
749  double d=ampsWithPhaseSpace(evt);
750  // double d = vals[counter];
751 
752  if(_rnd->Rndm()*_height < d){
753  newList.Add( _eventList[i] );
754  }
755  if(counter < 10 || 0 == counter%(_eventList.size()/20 + 1)){
756  std::cout << " remembering amps for event "
757  << counter
758  << ", its value is " << d
759  << std::endl;
760  }
761  counter++;
762  }
763 
764  _eventList = newList;
765  std::cout << "now my size has changed to " << _eventList.size()
766  << std::endl;
767  std::cout << " So the waste factor for \n" << this->name() << " is ";
768  if(_eventList.size() > 0) std::cout << rememberSize/_eventList.size();
769  else std::cout << " infinity! - that's big!";
770  std::cout << std::endl;
771 
772 
773  double deltaTFinal = difftime(time(0), startTime);
774  std::cout << " this took " << deltaTFinal/60.0 << " min";
775  if(deltaTFinal > 0){
776  std::cout << " rate = " << (_eventList.size()/deltaTFinal) << " evts/s"
777  << " or " << (_eventList.size()/deltaTFinal) *60 << " evts/m"
778  << " or " << (_eventList.size()/deltaTFinal) *60.*60./1000.
779  << "k evts/h";
780  }
781  std::cout << std::endl;
782 
783  std::cout << " ---------------\n " << std::endl;
784 
785  return _eventList.size();
786 }
787 
788 double DalitzBox::volume() const{
789  double a = area().size();
790  cout << " volume: returning area " << a
791  << " times height " << _height
792  << " = " << a*_height
793  << endl;
794  return a * _height;
795 }
797  time_t startTime = time(0);
798 
799  cout << " DalitzBox::getReady() " << *this << endl;
800 
801  _nTries = 0;
803  _eventList.clear();
804 
805  makeStarterSet();
807 
808  double deltaT = difftime(time(0), startTime);
809  cout << " DalitzBox::getRead(): am ready. This took "
810  << deltaT/60 << " min" << endl;
811 
812  if(no_reuse) _eventList.clear();
813 
814  _ready = true;
815 }
816 
818  if(! _ready) getReady();
819 
821 
822  if(_eventList.empty()){
823  evt = tryNewEvent();
824  }else{
825  evt = tryEventFromList();
826  }
827  return evt;
828 }
829 
831 
832  if(0 == _amps || bigDebug) return evt.phaseSpace();
833 
834  double val = _amps->RealVal(evt) * evt.phaseSpace();
835 
836  return val;
837 }
838 
840 
841  counted_ptr<DalitzEvent> evt( area().makeEventForOwner() );
842 
843  if( 0 == evt){ // bad ptr
844  return counted_ptr<DalitzEvent>(0);
845  }
846  if(insideDaddysArea(*evt)){ // out of area
847  return counted_ptr<DalitzEvent>(0);
848  }
849 
850  double val = getEventsPdf(*evt);
851  if(val > _height){
852  _heightProblems++;
853  cout << "ERROR in DalitzBox::tryNewEvent()\n" << (*this)
854  << "\n val > _height: " << val << " > " << _height
855  << "\n (for phase space = " << evt->phaseSpace() << ")."
856  << "\n if _height had been correctly estimated,"
857  << " this should not be possible." << endl;
858  cout << " This has happened " << _heightProblems
859  << " times in this box " << endl;
860  }
861  if(_rnd->Rndm()*_height > val){ // then we don't take it.
862  evt = counted_ptr<DalitzEvent>(0);
863  }
864  return evt;
865 }
866 
868  bool dbThis=false;
869  counted_ptr<DalitzEvent> evt( area().makeEventForOwner() );
870 
871  if( 0 == evt){ // bad ptr
872  return counted_ptr<DalitzEvent>(0);
873  }
874  if(insideDaddysArea(*evt)){ // out of area
875  return counted_ptr<DalitzEvent>(0);
876  }
877 
878  if(dbThis)cout << " DalitzBox::tryWeightedEventForOwner() "
879  << " got event with ptr "
880  << evt
881  << ", setting val: " << endl;
882  if(dbThis)cout << " now printing event: "
883  << *evt
884  << endl;
885  double val = ampsWithPhaseSpace(*evt);
886  if(dbThis)cout << " val = " << val << ", now setting weight " << endl;
887  evt->setWeight(val/_height);
888  if(dbThis)cout << " done weight, now generator..." << endl;
890  if(dbThis)cout << " all OK, returning " << evt << endl;
891  return evt;
892 }
893 
895  // the list has already been subjected to
896  // data throwing away.
897  // _nTries were made, and _nEventsForTries are
898  // left over. So each event in the list represents
899  // on average _nTries/_nEventsForTries tries;
900 
901  double rndNumber = _rnd->Rndm()*_nTries;
902  if(rndNumber > (double) _nEventsForTries){
903  return counted_ptr<DalitzEvent>(0);
904  }
905  return popEventFromList();
906 }
907 
909  if(_eventList.empty()){
910  return counted_ptr<DalitzEvent>(0);
911  }
914  return evt;
915 }
916 
917 std::vector<DalitzBox> DalitzBox::split(unsigned int nWays
918  ) const{
919  std::vector<MappedDalitzArea> areaList(_area.split(nWays));
920 
921  std::vector<DalitzBox> newSet;
922  if(areaList.empty()) return newSet;
923  bool foundCtr=false;
924  for(unsigned int i=0; i < areaList.size(); i++){
925  DalitzBox newBox(*this);
926  newBox.area() = areaList[i];
927  if( ! (newBox.area().isInside((this->area().centre())))){
928  // newBox.setGuessedHeight(0.6*guessedHeight());
929  newBox.setGuessedHeight(-1);
930  }else{
931  foundCtr=true;
932  }
933  newSet.push_back(newBox);
934  }
935  if(false && (! foundCtr)){
936  cout << "ERROR split box " << *this << " " << nWays << " ways,"
937  << " but center: is gone." << endl;
938  cout << "newSet\n: " << newSet << endl;
939  }
940  return newSet;
941 }
942 
943 std::vector<DalitzBox> DalitzBox::splitIfWiderThan(double maxWidth
944  ) const{
945  std::vector<MappedDalitzArea> areaList(_area.splitIfWiderThan(maxWidth));
946 
947  std::vector<DalitzBox> newSet;
948  if(areaList.empty()) return newSet;
949  for(unsigned int i=0; i < areaList.size(); i++){
950  DalitzBox newBox(*this);
951  newBox.area() = areaList[i];
952  if( ! (newBox.area().isInside((this->area().centre())))){
953  // newBox.setGuessedHeight(0.6*guessedHeight());
954  newBox.setGuessedHeight(-1);
955  }
956  newSet.push_back(newBox);
957  }
958  return newSet;
959 }
960 
961 void DalitzBox::print(std::ostream& os) const{
962  os << "DalitzBox: " << name() << " number " << number()
963  << "\n area " << area()
964  << "\n guessed height " << guessedHeight()
965  << "\n height " << height()
966  << "\n daddy: ";
967  if(0 == _daddy){
968  os << " none";
969  }else{
970  os << " number " << _daddy->number();
971  }
972 }
973 
974 bool DalitzBox::setRnd(TRandom* rnd){
975  _rnd = rnd;
976  area().setRnd(rnd);
977  return true;
978 }
979 
980 ostream& operator<<(ostream& os, const DalitzBox& box){
981  box.print(os);
982  return os;
983 }
984 //
FitParameter & _s2
Definition: DalitzBox.cpp:32
bool veryFastTest
Definition: DalitzBox.cpp:20
double amps(IDalitzEvent &ep)
Definition: DalitzBox.cpp:268
double _max_s4
Definition: DalitzBox.h:23
virtual bool Add(const EVENT_TYPE &evt)
Definition: EventList.h:63
virtual void setWeight(double w)
FitParameter & _s4
Definition: DalitzBox.cpp:34
bool bigDebug
Definition: DalitzBox.cpp:18
int number() const
Definition: DalitzBox.h:95
MappedDalitzArea _area
Definition: DalitzBox.h:31
FitParameter & _s3
Definition: DalitzBox.cpp:33
FitParameter & _s1
Definition: DalitzBox.cpp:31
void resize(unsigned int N)
double ampsWithPhaseSpace(IDalitzEvent &ep)
Definition: DalitzBox.cpp:285
void print(std::ostream &os=std::cout) const
Definition: DalitzBox.cpp:961
virtual double phaseSpace() const =0
DalitzBox * _box
Definition: DalitzBox.cpp:36
virtual double phaseSpace() const
FitParameter & _s0
Definition: DalitzBox.cpp:30
double generalisedPareto_estimateMaximum(const std::vector< double > &input, double CL=0.001)
virtual double RealVal(EVENT_TYPE &evt)=0
bool _flatPhaseSpace
Definition: DalitzBox.h:47
virtual void print(std::ostream &os=std::cout) const =0
std::vector< MappedDalitzArea > split(unsigned int nWays) const
std::vector< MappedDalitzArea > splitIfWiderThan(double maxWidth) const
bool estimateHeight(std::vector< double > &vals)
Definition: DalitzBox.cpp:406
void setGuessedHeight(double h)
Definition: DalitzBox.h:100
MINT::counted_ptr< DalitzEvent > tryWeightedEventForOwner()
Definition: DalitzBox.cpp:867
const DalitzBox * _daddy
Definition: DalitzBox.h:42
DalitzEventList _eventList
Definition: DalitzBox.h:38
double phaseSpace(IDalitzEvent &ep)
Definition: DalitzBox.cpp:280
double getFCNVal()
Definition: Minimiser.cpp:272
double height() const
Definition: DalitzBox.h:106
bool estimateHeight_old(std::vector< double > &vals)
Definition: DalitzBox.cpp:522
bool insideArea(const DalitzEvent &evt) const
Definition: DalitzBox.cpp:214
bool setRnd(TRandom *rnd=gRandom)
Definition: DalitzBox.cpp:974
double volume() const
Definition: DalitzBox.cpp:788
double _max_s3
Definition: DalitzBox.h:23
bool makeStarterSet()
Definition: DalitzBox.cpp:623
double guessedHeight() const
Definition: DalitzBox.h:101
bool fastTest
Definition: DalitzBox.cpp:19
MINT::counted_ptr< DalitzEvent > makeEventForOwner() const
bool ultraFast
Definition: DalitzBox.cpp:21
const std::string & name() const
Definition: DalitzBox.h:98
double _max_s2
Definition: DalitzBox.h:23
double _maxPhaseSpace
Definition: DalitzBox.h:24
MINT::IReturnRealForEvent< IDalitzEvent > * _amps
Definition: DalitzBox.h:34
const MappedDalitzArea & area() const
Definition: DalitzBox.h:103
double getEventsPdf(DalitzEvent &evt)
Definition: DalitzBox.cpp:830
double _height
Definition: DalitzBox.h:37
int _nTries
Definition: DalitzBox.h:40
double _max_s0
Definition: DalitzBox.h:23
double _max_s1
Definition: DalitzBox.h:23
TRandom * _rnd
Definition: DalitzBox.h:36
std::vector< DalitzBox > splitIfWiderThan(double maxWidth) const
Definition: DalitzBox.cpp:943
int throwAwayData(const std::vector< double > &vals)
Definition: DalitzBox.cpp:727
bool setRnd(TRandom *rnd=gRandom)
virtual unsigned int size() const
Definition: EventList.h:59
FindMaxFCN(FitParameter &s0, FitParameter &s1, FitParameter &s2, FitParameter &s3, FitParameter &s4, DalitzBox *box, MinuitParameterSet *mps)
Definition: DalitzBox.cpp:56
ostream & operator<<(ostream &os, const DalitzBox &box)
Definition: DalitzBox.cpp:980
MINT::counted_ptr< DalitzEvent > tryNewEvent()
Definition: DalitzBox.cpp:839
void getReady()
Definition: DalitzBox.cpp:796
double getVal()
Definition: DalitzBox.cpp:39
int _nEventsForTries
Definition: DalitzBox.h:41
int makeFlatEventsKeepAll(int N)
Definition: DalitzBox.cpp:241
bool _ready
Definition: DalitzBox.h:29
MINT::counted_ptr< DalitzEvent > popEventFromList()
Definition: DalitzBox.cpp:908
virtual void setGeneratorPdfRelativeToPhaseSpace(double gpdf)
unsigned int _heightProblems
Definition: DalitzBox.h:45
bool estimateHeightMinuit()
Definition: DalitzBox.cpp:296
double size() const
bool isInside(const DalitzEvent &evt) const
MINT::counted_ptr< DalitzEvent > tryEventForOwner()
Definition: DalitzBox.cpp:817
bool insideMyOrDaddysArea(const DalitzEvent &evt) const
Definition: DalitzBox.cpp:237
std::vector< DalitzBox > split(unsigned int nWays) const
Definition: DalitzBox.cpp:917
MINT::counted_ptr< DalitzEvent > tryEventFromList()
Definition: DalitzBox.cpp:894
bool no_reuse
Definition: DalitzBox.cpp:23
bool setAmps(MINT::IReturnRealForEvent< IDalitzEvent > *amps)
Definition: DalitzBox.cpp:198
void setDaddy(const DalitzBox *daddy)
Definition: DalitzBox.cpp:209
bool insideDaddysArea(const DalitzEvent &evt) const
Definition: DalitzBox.cpp:232