MINT2
DalitzBWBoxSet.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:58 GMT
3 #include "Mint/DalitzBWBoxSet.h"
4 
6 #include "Mint/ReturnWeight.h"
8 
9 #include <ctime>
10 #include <cmath>
11 #include <iostream>
12 
13 using namespace std;
14 using namespace MINT;
15 
17 // phaseSpaceFrac is the fraction of events generated according to
18 // phase space (in addition to any flat phase-space component the
19 // model might have). This phase-space component makes sure we don't
20 // miss out any part of phase space where the (approximate) amplitude
21 // model might have some artificial "holes" etc.
22 
25  , _maxWeightEstimate(-9999.0)
26  , _maxWeightInSample(-9999.0)
27  , _ampSum(amps)
28  , _ready(false)
29  , _volumeProbs()
30  , _rnd(r)
31  , _phaseSpaceFrac(DalitzBWBoxSet::__phaseSpaceFracDefaultValue)
32  , _phaseSpaceIntegral(-9999)
33  , _psbox()
34  , _pick_ps_prob(-9999)
35 {}
38  , _maxWeightEstimate(-9999.0)
39  , _maxWeightInSample(-9999.0)
40  , _ampSum(0)
41  , _ready(false)
42  , _volumeProbs()
43  , _rnd(r)
44  , _phaseSpaceFrac(DalitzBWBoxSet::__phaseSpaceFracDefaultValue)
45  , _phaseSpaceIntegral(-9999)
46  , _psbox()
47  , _pick_ps_prob(-9999)
48 {}
49 
54  , _eventPtrList(other._eventPtrList)
55  , _maxWeightEstimate(other._maxWeightEstimate)
56  , _maxWeightInSample(other._maxWeightInSample)
57  , _ampSum(other._ampSum)
58  , _ready(other._ready)
59  , _volumeProbs(other._volumeProbs)
60  , _rnd(other._rnd)
61  , _phaseSpaceFrac(other._phaseSpaceFrac)
62  , _phaseSpaceIntegral(other._phaseSpaceIntegral)
63  , _psbox(other._psbox)
64  , _pick_ps_prob(other._pick_ps_prob)
65 {}
66 
67 double DalitzBWBoxSet::fullPdf(DalitzEvent& evt){ // (but w/o phase space factor)
68  bool dbThis=false;
69  if(dbThis) cout << "DalitzBWBoxSet::fullPdf called" << endl;
70  if(0 == _ampSum) return 0;
71  double returnVal = _ampSum->RealVal(evt);
72  if(dbThis) cout << "DalitzBWBoxSet::fullPdf result: "
73  << returnVal << endl;
74  return returnVal;
75 }
76 
78  _ready = false;
79  box.setRnd(_rnd);
80  this->push_back(box);
81 }
82 
84  _ready = false;
85  for(unsigned int i=0; i<boxes.size(); i++){
86  this->add(boxes[i]);
87  }
88 }
89 bool DalitzBWBoxSet::setRnd(TRandom* rnd){
90  if(0 == rnd) _rnd = gRandom;
91  else _rnd = rnd; // this *should* do it, but for safety:
92  for(unsigned int i=0; i< this->size(); i++){
93  (*this)[i].setRnd(_rnd);
94  }
95  return true;
96 }
97 
99  setRnd(_rnd); // safety paranoia - all boxes should be using this already
100  _rnd->SetSeed(time(0)*2);
101  return true;
102 }
104  bool s=true;
105 
106  double prevV=0;
107  for(unsigned int i=0; i< this->size(); i++){
108  if(prevV == _volumeProbs[i]) continue;
109  prevV=_volumeProbs[i];
110  cout << "checking integration for box "
111  << (*this)[i].name() << endl;
112  s &= (*this)[i].checkIntegration();
113  cout << "============================="
114  << endl;
115  }
116  return s;
117 }
118 
120  cout << "Hello from DalitzBWBoxSet::am_I_generating_what_I_think_I_am_generating"
121  << " with " << Nevents << " events to generate for each case" << endl;
122 
123  DiskResidentEventList generated_approxPDF_events("generated_approxPDF_events.root"
124  , "RECREATE");
125  DiskResidentEventList generatedFlat_weighted_approxPDF_events("generatedFlat_weighted_approxPDF_events.root"
126  , "RECREATE");
127 
128  int printEvery = min(Nevents/10, 10000);
129 
130  for(int i=0; i < Nevents; i++){
131  generated_approxPDF_events.Add(*makeWeightedApproxEventForOwner());
132  if(0 == i%printEvery) cout << "done event " << i << endl;
133  }
134  cout << "generated approxPDF_events"<< endl;
135 
136  generated_approxPDF_events.save();
137  DalitzHistoSet datGen= generated_approxPDF_events.weightedHistoSet();
138  datGen.save("generated_approxPDF_histos.root");
139  datGen.draw("generated_approxPDF_histos");
140 
141  int rwfactor=1;
142  for(int i=0; i < Nevents*rwfactor; i++){
144  evtPtr->setWeight(evtPtr->getWeight()*genValueNoPs(*evtPtr));
145  generatedFlat_weighted_approxPDF_events.Add(*evtPtr);
146  if(0 == i%(printEvery*rwfactor)) cout << "done event " << i << endl;
147  }
148  cout << "generated generatedFlat_weighted_approxPDF_events" << endl;
149  generatedFlat_weighted_approxPDF_events.save();;
150  DalitzHistoSet datWeight = generatedFlat_weighted_approxPDF_events.weightedHistoSet();
151  datWeight.save("generatedFlat_weighted_approxPDF_histos.root");
152  datWeight.draw("generatedFlat_weighted_approxPDF_histos");
153  datGen.drawWithFitNorm(datWeight, "genDotsWeightLine_", "eps", "E1 SAME");
154 
155  DalitzHistoSet ratio(datGen);
156  ratio /= datWeight;
157  ratio.save("ratio_full_by_weight.root");
158  ratio.draw("ratio_full_by_weight");
159 
160 
161  cout << "DalitzBWBoxSet::am_I_generating_what_I_think_I_am_generating:"
162  << " all done" << endl;
163 
164  return true;
165 }
167  cout << "Hello from DalitzBWBoxSet::compareGenerationMethodsForFullPDF"
168  << " (with " << this->size() << " boxes)"
169  << " with " << Nevents << " events to generate for each case" << endl;
170 
171  DiskResidentEventList generated_fullPDF_efficient_weighted_events("generated_fullPDF_efficient_weighted_events.root"
172  , "RECREATE");
173  DiskResidentEventList generatedFlat_fullPDF_flat_weighted_events("generated_fullPDF_flat_weighted_events.root"
174  , "RECREATE");
175 
176  bool unweightFull = false;
177 
178  int printEvery = min(Nevents/10, 10000);
179 
180  for(int i=0; i < Nevents; i++){
181  if(unweightFull) {
182  generated_fullPDF_efficient_weighted_events.Add(*makeEventForOwner());
183  }else{
184  generated_fullPDF_efficient_weighted_events.Add(*makeWeightedEventForOwner());
185  }
186  if(0 == i%printEvery) cout << "done event " << i << endl;
187  }
188  cout << "generated approxPDF_events"<< endl;
189 
190  generated_fullPDF_efficient_weighted_events.save();
191  DalitzHistoSet datGen= generated_fullPDF_efficient_weighted_events.weightedHistoSet();
192  datGen.save("generated_fullPDF_efficient_weighted_histos.root");
193  datGen.draw("generated_fullPDF_efficient_weighted");
194 
195  int rwfactor=1;
196  for(int i=0; i < Nevents*rwfactor; i++){
198  evtPtr->setWeight(evtPtr->getWeight()*fullPdf(*evtPtr));
199  generatedFlat_fullPDF_flat_weighted_events.Add(*evtPtr);
200  if(0 == i%(printEvery*rwfactor)) cout << "done event " << i << endl;
201  }
202  cout << "generated generatedFlat_fullPDF_flat_weighted_events" << endl;
203  generatedFlat_fullPDF_flat_weighted_events.save();;
204  DalitzHistoSet datWeight = generatedFlat_fullPDF_flat_weighted_events.weightedHistoSet();
205  datWeight.save("generated_fullPDF_efficient_weighted_histos.root");
206  datWeight.draw("generated_fullPDF_efficient_weighted_events");
207  datGen.drawWithFitNorm(datWeight, "efficientBlue_FlatRed_", "eps", "E1 SAME");
208 
209  DalitzHistoSet ratio(datGen);
210  ratio /= datWeight;
211  ratio.save("ratio_full_by_weight.root");
212  ratio.draw("ratio_full_by_weight");
213 
214  cout << "DalitzBWBoxSet::compareGenerationMethodsForFullPDF:"
215  << " all done" << endl;
216 
217  return true;
218 }
219 
221  bool dbThis=false;
222  if(phaseSpaceFrac() <= 0) return;
223 
224  double rat = calc_pick_ps_prob()/phaseSpaceFrac();
225  double w=1;
226  if(rat > 0) w=rat;
227  _psbox.weight() *= w;
228  _psbox.height() /= w;
229 
230  if(dbThis){
231  cout << "h " << _psbox.height()
232  << ", w " << _psbox.weight()
233  << endl;
234  }
235 }
236 
238  bool dbThis=false;
239  if(_phaseSpaceFrac <= 0) return 0;
240  double returnVal= _psbox.volume()/(_psbox.volume() + VolumeSum());
241  if(dbThis){
242  cout << "calc_pick_ps_prob() = "
243  << _psbox.volume() << " / " << "("
244  << _psbox.volume() << " + " << VolumeSum() << ")"
245  << " = " << returnVal << endl;
246  }
247  return returnVal;
248 }
250  bool dbThis=false;
251  if(_phaseSpaceFrac <= 0) return 0;
252  if(_pick_ps_prob < 0){
254  if(dbThis){
255  cout << " DalitzBWBoxSet::pick_ps_prob() = " << _pick_ps_prob << endl;
256  }
257  }
258  return _pick_ps_prob;
259 }
260 
262  _psbox.setPattern(this->begin()->pattern());
263 
265  _psbox.weight()=1.0;
266  for(int i=0; i < 100; i++){
268  }
269 }
270 
272  // checkIntegration();
273  bool dbThis=false;
274  if(this->empty()) return;
275  setup_psbox();
276 
279 
280  // checkIntegration(); // for debug only
281 
282  if(dbThis){
283  cout << "DalitzBWBoxSet::getReady() - got ready, result is this: "
284  << (*this) << "\n ======= OK, no? ====== " << endl;
285  }
286  _ready=true;
287  // am_I_generating_what_I_think_I_am_generating(1000000); // for debug only
288 
289 }
290 
292  bool dbThis=false;
293 
294  time_t startTime = time(0);
295  // int maxEvents = 100000;
296  int maxEvents = 60000; // memory
297  int maxTries = 9;
298 
299  bool speedupabit=true;
300  if(speedupabit){
301  maxEvents=50000;
302  maxTries = 3;
303  }
304  bool speedupalot=false;
305  if(speedupalot){
306  maxEvents=30000;
307  maxTries = 2;
308  }
309  bool instant=false; // set true for very fast & very dirty testing jobs etc;
310  if(instant){
311  maxEvents=5000;
312  maxTries = 1;
313  }
314  //int maxEvents = 100000;
315  // maxEvents = 10000;
316 
317  cout << "DalitzBWBoxSet::findMax making starter set with "
318  << maxEvents << " events." << endl;
319 
320  int numTries=0;
321  _maxWeightEstimate=-9999; // estimated max from pareto
322 
323  while(_maxWeightEstimate < 0 ||
325  && numTries < maxTries
326  && _eventPtrList.size() < 1000000)
327  ){
328  numTries++;
329  cout << " DalitzBWBoxSet::findMax(): iteration number " << numTries
330  << " for estimated max of parent " << _maxWeightEstimate
331  << " and max of current sample: " << _maxWeightInSample
332  << endl;
333  for(int i=0; i < maxEvents; i++){
335 
336  unsigned int printEvery = 100000;
337  if(i <= 10) printEvery=10;
338  else if(i <= 100) printEvery=100;
339  else if(i <= 1000) printEvery=1000;
340  else if(i <= 100000) printEvery=10000;
341  else if(i <= 1000000) printEvery=500000;
342 
343  if(i < 1 || i%printEvery == 0){
344  cout << "DalitzBWBoxSet::findMax() "
345  << "made event number " << i
346  << "\t (" << difftime(time(0), startTime) << " sec)"
347  << endl;
348  }
349  if(dbThis && (0 != evt)) {
350  cout << "DalitzBWBoxSet::findMax(): made event with weight "
351  << evt->getWeight() << endl;
352  }
353  if(0 != evt) _eventPtrList.Add(evt);
354  }
355 
356  cout << "DalitzBWBoxSet::findMax() starter set took "
357  << difftime(time(0), startTime) << " s." << endl;
358 
360  // _maxWeightEstimate = 60; // debug only june 2011
361  // break; // debug only june 2011
362  }
363 
367  }
368  // _maxWeightEstimate = 60; // debug only june 2011
369 
370  if(dbThis){
371  cout << "DalitzBWBoxSet::findMax(): I have now "
372  << _eventPtrList.size()
373  << " events in the eventPtrList." << endl;
374  if( _eventPtrList.size() > 0){
375  cout << "The first one is: " << _eventPtrList.getEventRef(0) << endl;
376  }
377  }
378 
379  //if(dbThis)_eventPtrList.save("eventListBeforeUnweighting.root");
380 
381  ReturnWeight w;
383 
384  if(dbThis){
385  cout << "DalitzBWBoxSet::findMax(): AFTER unweighting I have "
386  << _eventPtrList.size()
387  << " events in the eventPtrList." << endl;
388  if( _eventPtrList.size() > 0){
389  cout << "The first unweighted event is: " << _eventPtrList.getEventRef(0) << endl;
390  }
391  }
392 
393 
394 
395  /*
396  _eventPtrList.findMaxAndThrowAwayData(_maxWeightEstimate, &w);
397  */
398 
399  // we've un-weighted them, need to set weight correctly...
400  for(unsigned int i=0; i < _eventPtrList.size(); i++){
402  }
403 
404  cout << "DalitzBWBoxSet::findMax() after throw away I have "
405  << _eventPtrList.size() << " events left. Maximum weight estimate is: "
406  << _maxWeightEstimate << endl;
407 
408  cout << "DalitzBWBoxSet::findMax() done after "
409  << difftime(time(0), startTime) << " s." << endl;
410  return;
411 }
412 
413 double DalitzBWBoxSet::findMaxInList(double& sampleMax){
414  bool dbThis=false;
415 
416 
417  time_t startTime = time(0);
418  if(0 == _eventPtrList.size()) return -9999;
419 
420  std::vector<double> vals;
421  vals.resize(_eventPtrList.size());
422  sampleMax=-9999;
423  for(unsigned int i=0; i < _eventPtrList.size(); i++){
425 
426  double w = evt.getWeight();
427  double d=w;
428 
429  if(dbThis) cout << "w = " << w << endl;
430 
431  if(d > sampleMax || 0 == i) sampleMax=d;
432  vals[i] = d;
433 
434 
435  unsigned int printEvery = _eventPtrList.size()/10;
436  if(printEvery <=0) printEvery = 5;
437  if(i <= 2) printEvery=1;
438  else if(i <= 200) printEvery=100;
439 
440  if(i < 5 || 0 == i%(printEvery)){
441  std::cout << "DalitzBWBoxSet::findMaxInList() calculated "
442  << "_ampSum for event "
443  << i
444  << ", its value is " << d
445  << std::endl;
446  double deltaT = difftime(time(0), startTime);
447  std::cout << " DalitzBWBoxSet::findMaxInList()this took "
448  << deltaT << " s";
449  if(deltaT > 0){
450  std::cout << "; rate (before throwing away) = "
451  << i/deltaT
452  << " evts/s";
453  }
454  std::cout << std::endl;
455  }
456  };
457 
458 
459  double epsilon = 0.2;
460  // sampleMax = sampleMax + fabs(sampleMax * epsilon);
461 
462  double CL = 1.0 - 1./(_eventPtrList.size()*10000);
463  std::cout << "sampleMax = " << sampleMax << std::endl;
464 
465  double maxValue = generalisedPareto_estimateMaximum(vals, CL);
466  std::cout << " maxValue after " << maxValue << std::endl;
467  maxValue *= 1.0 + epsilon;
468  std::cout << "DalitzBWBoxSet::findMaxInList():: Now added "
469  << epsilon * 100 << "% for safety. Returning "
470  << maxValue << std::endl;
471 
472  return maxValue;
473 }
474 
477  ){
478  // bool dbThis=true;
479  std::cout << "EventPtrList::justThrowAwayData:"
480  << " before throwing away data, my size is "
481  << _eventPtrList.size() << std::endl;
482 
483  time_t startTime = time(0);
484 
485  int rememberSize = _eventPtrList.size();
486  unsigned int counter=0;
487 
488  DalitzEventPtrList newList;
489 
490  for(unsigned int i=0; i < _eventPtrList.size(); i++){
491  double d=amps->RealVal(_eventPtrList.getEventRef(i));
492  if(_rnd->Rndm()*maxValue < d){
493  newList.Add( _eventPtrList.getEventRef(counter) );
494  }
495 
496  unsigned int printEvery = 10000; //size()/10;
497  //if(printEvery <=0) printEvery = 500;
498  // if(counter <= 2) printEvery=1;
499  //else if(counter <= 200) printEvery=10000;
500 
501  if(counter < 1 || 0 == counter%(printEvery)){
502  std::cout << " amps for event "
503  << counter
504  << ": " << d
505  << ". " << newList.size()
506  << " events passed ";
507  double deltaT = difftime(time(0), startTime);
508  std::cout << ". Took " << deltaT << " s";
509 
510  if(deltaT > 0){
511  std::cout << "; rate (before/after throwing away) = "
512  << counter/deltaT
513  << " / " << newList.size()/deltaT
514  << " evts/s";
515  }
516  std::cout << std::endl;
517  }
518  counter++;
519  };
520 
521  _eventPtrList = newList;
522  std::cout << "now my size has changed to " << _eventPtrList.size()
523  << std::endl;
524  std::cout << " So the waste factor is ";
525  if(size() > 0) std::cout << rememberSize/_eventPtrList.size();
526  else std::cout << " infinity! - that's big!";
527  std::cout << std::endl;
528 
529 
530  double deltaTFinal = difftime(time(0), startTime);
531  std::cout << " this took " << deltaTFinal/60.0 << " min" << std::endl;
532 
533  return _eventPtrList.size();
534 }
535 
536 
537 
539  bool dbThis=false;
540 
541  double sum = 0;
542  for(unsigned int i=0; i< this->size(); i++){
543  if(dbThis)cout << " height number " << i << ") "
544  << (*this)[i].height()
545  << endl;
546  sum += (*this)[i].height();
547  }
548 
549  if(dbThis)cout << " Volume sum: " << sum;
550  return sum;
551 }
553  bool dbThis=false;
554 
555  double sum = 0;
556  for(unsigned int i=0; i< this->size(); i++){
557  if(dbThis)cout << " volume number " << i << ") "
558  << (*this)[i].volume()
559  << endl;
560  sum += (*this)[i].volume();
561  }
562 
563  if(dbThis)cout << " Volume sum: " << sum;
564  return sum;
565 }
566 
568  for(unsigned int i=0; i< this->size(); i++){
569  (*this)[i].setUnWeightPs(doSo);
570  }
571 }
572 
574  bool dbThis=false;
575 
576  if(this->empty()) return;
577  // intervals each with a length proportional
578  // to the volume of the corresponding box.
579  // All put together add up to one.
580  // For random number generation.
581 
582  _volumeProbs.clear();
583  _volumeProbs.resize(this->size());
584 
585  double totalVolume = VolumeSum();
586  double sum=0;
587  for(unsigned int i=0; i < this->size(); i++){
588  // (*this)[i].height() *= 1./totalVolume;
589  sum += (*this)[i].volume()/totalVolume;
590  if(dbThis){
591  // cout << " with height " << (*this)[i].height();
592  cout << " + " << (*this)[i].volume()/totalVolume;
593  cout << " = \t volume probs [" << i <<"] = " << sum << endl;
594  }
595  _volumeProbs[i] = sum;
596  }
597 }
598 
600  if(! ready()) getReady();
601 
602  if(_volumeProbs.size() != this->size()){
604  }
605 
606  double rndNumber = _rnd->Rndm();
607  for(unsigned int i=0; i< _volumeProbs.size(); i++){
608  if(_volumeProbs[i] > rndNumber) return i;
609  }
610  cout << "WARNING in DalitzBWBoxSet::pickRandomVolume():"
611  << " How odd - should never have gotten here."
612  << " rndNumber = " << rndNumber
613  << ", _volumeProbs[this->size()-1] = "
614  << _volumeProbs[this->size()-1]
615  << endl;
616  return this->size()-1;
617 }
618 
619 
621  if(this->empty()) return counted_ptr<DalitzEvent>(0);
622  if(! ready()) getReady();
623  return _psbox.makeEventForOwner();
624 }
625 
627 // bool dbThis=false;
628  if(this->empty()){
629  cout << "DalitzBWBoxSet::tryEventForOwner ERROR: "
630  << " you called me, but there are no boxes"
631  << "\n\t returning 0-ptr" << endl;
632  return counted_ptr<DalitzEvent>(0);
633  }
634  if(! ready()) getReady();
635 
636  counted_ptr<DalitzEvent> evtPtr(0);
637  if(_rnd->Rndm() < pick_ps_prob()){
638  evtPtr = phaseSpaceEvent();
639 /* if(dbThis){
640  cout << "picked phaseSpace " << endl;
641  }*/
642  }else{
643  int vol = pickRandomVolume();
644  evtPtr = ((*this)[vol].tryEventForOwner());
645 /* if(dbThis){
646  cout << "picked volume number: " << vol << endl;
647  }*/
648  }
649 /* if(dbThis && 0 != evtPtr){
650  cout << "weight in DalitzBWBoxSet::tryEventForOwner() "
651  << evtPtr->getWeight() << endl;
652  }*/
653  if(0 != evtPtr){
654 /* if(dbThis) {
655  cout << "DalitzBWBoxSet::tryEventForOwner() calling:"
656  << " setGeneratorPdfRelativeToPhaseSpace(genValueNoPs(*evtPtr))"
657  << endl;
658  }*/
660 // if(dbThis) cout << " .. done that." << endl;
661  }
662 /* if(dbThis){
663  cout << " DalitzBWBoxSet::tryEventForOwner(): Returning " << evtPtr << endl;
664  if(0 != evtPtr){
665  cout << " ... with weight " << evtPtr->getWeight() << endl;
666  }
667  }*/
668  return evtPtr;
669 }
671  int NTries = 0;
672  return makeEventForOwner(NTries);
673 }
675 // bool dbThis=false;
676  static unsigned int Ncalls=0;
677  static unsigned int printEveryNthCall = 10000;
678 
679  Ncalls++;
680  bool printThis = Ncalls < 3 || (0 == Ncalls%printEveryNthCall);
681 
682  NTries = 0;
683  if(! _ready) getReady();
684  if(_maxWeightEstimate < 0) findMax();
685 // if(dbThis) cout << "makeEventForOwner found max" << endl;
686  if(_eventPtrList.size() > 0){
687 // if(dbThis) cout << "popping Event" << endl;
688  return popEventFromList();
689  }
690 // if(dbThis) cout << "making new event" << endl;
691  counted_ptr<DalitzEvent> evtPtr(0);
692 
693  int counter=0;
694  int maxCount = 1000000;
695  int countProblems = 0;
696  do{
697  int tempTries=0;
698  counter++;
699  if(counter > maxCount){
700  cout << "DalitzBWBoxSet::makeEventForOwner tried "
701  << counter << " times unsuccessfully. Giving up"
702  << endl;
703  return evtPtr;
704  }
705  evtPtr = makeWeightedEventForOwner(tempTries);
706  NTries += tempTries;
707  double w = -9999.0;
708  if(0 != evtPtr) w = evtPtr->getWeight();
710  if(w > _maxWeightEstimate){
711  countProblems++;
712  cout << "DalitzBWBoxSet::makeEventForOwner ERROR!!! "
713  << " for the " << countProblems << " th time. "
714  << "\n\t This event's weight > max Weight: "
715  << evtPtr->getWeight() << " > " << _maxWeightEstimate
716  << endl;
717  _maxWeightEstimate = evtPtr->getWeight()*1.3;
718  cout << "\n\t...increased _maxWeightEstimate to: "
720  << endl;
721  }
722  }while(0 == evtPtr || _rnd->Rndm()*_maxWeightEstimate > evtPtr->getWeight());
723 
724  if(printThis){
725  cout << "DalitzBWBoxSet::makeEventForOwner INFO after "
726  << Ncalls << " calls."
727  << " Estimate of max weight " << _maxWeightEstimate
728  << " max weight in sample so far " << _maxWeightInSample
729  << "." << endl;
730  }
731 
732  if(0 != evtPtr) evtPtr->setWeight(1);
733  return evtPtr;
734 }
735 
737  bool dbThis=false;
738  if(! dbThis){
740  return returnThis;
741  } else {
743  return returnThis;
744  }
745 }
748  return returnThis;
749 }
750 
752  if(_eventPtrList.empty()){
753  return counted_ptr<DalitzEvent>(0);
754  }
755  // counted_ptr<DalitzEvent> evt(new DalitzEvent(_eventPtrList.getEventRef(_eventPtrList.size()-1]));
756  //_eventPtrList.resize(_eventPtrList.size()-1);
758 }
759 
761  int NTries = 0;
762  return makeWeightedEventForOwner(NTries);
763 }
765 // bool dbThis=false;
767  if(! ptr) return ptr;
768  double w = ptr->getWeight();
769 // if(dbThis) cout << "phase space weight " << w << endl;
770 
771  double full = fullPdf(*ptr);
772  double gen = genValueNoPs(*ptr);
773 // if(dbThis) cout << ", full weight " << w
774 // << " * " << full << " / " << gen << " = ";
775 
776  w *= full/gen;
777 // if(dbThis) cout << w << endl;
778 
779  ptr->setWeight(w);
781 
782  return ptr;
783 }
784 
786  int NTries = 0;
787  return makeWeightedApproxEventForOwner(NTries);
788 }
790  //bool dbThis=false;
792  int maxTries = 10000000;
793  NTries = 0;
794  while((! ptr) && NTries < maxTries){
795  ptr=tryEventForOwner();
796  NTries++;
797  }
798  if(! ptr){
799  cout << "ERROR in DalitzBWBoxSet::makeWeightedEventForOwner() "
800  << " failed to make event after " << maxTries
801  << " tries!"
802  << endl;
803  return ptr;
804  }
805  return ptr;
806 }
807 
808 
810  // mainly for debug.
811  bool dbThis=false;
812 
814  if(this->empty())return -9999;
815 
816  if(dbThis) cout << " calculating phaseSpaceIntegral " << endl;
817  DalitzEventPattern pat = (*this)[0].pattern();
818  DalitzBWBox box(pat);
819 
821  cout << "compare to " << p4.getVal(pat) << endl;
822 
823  int maxEvents = 10000;
824  int maxTries = 10000 * maxEvents;
825  int nTries=0;
826  double sum=0, sumsq=0;
827  for(int i=0; i < maxEvents && nTries < maxTries; i++){
828  int triesForThisEvent=0;
829  counted_ptr<DalitzEvent> ptr(box.makeEventForOwner(triesForThisEvent));
830  nTries += triesForThisEvent;
831  if(! ptr) continue;
832  double val = ptr->phaseSpace();
833  sum += val;
834  sumsq += val*val;
835  if(dbThis && (0 == i%1000 || i < 100)){
836  cout << " i = " << i << ", nTries = " << nTries
837  << " val = " << val << " sum " << sum << endl;
838  }
839  }
840  double mean = sum/((double)nTries);
841  double meanOfSq = sumsq/((double)nTries);
842  double variance = meanOfSq - mean*mean;
843  double error = sqrt(variance)/sqrt((double)nTries);
844 
845  _phaseSpaceIntegral = sum/((double)nTries) * box.area().size();
846  // _phaseSpaceIntegral = sum/((double)maxEvents);// * box.volume();
847  // double psVolume = box.volume() * ((double)maxEvents)/((double)nTries);
848  //double psVolume = box.volume();
849  //_phaseSpaceIntegral = sum/((double)maxEvents);// * psVolume;
850  cout << "phaseSpaceIntegral: returning " << _phaseSpaceIntegral
851  << " relativeError: " << error/mean * 100 << "%" << endl;
852 
853  return _phaseSpaceIntegral;
854 }
855 
857  bool dbThis=false;
858  double sum=0;
859  if(dbThis){
860  cout << "DalitzBWBoxSet::genValueNoPs called with " << evt
861  << endl;
862  }
863 
864  if(this->empty()) return 0;
865 
866  for(unsigned int i=0; i<this->size(); i++){
867  double val = (*this)[i].genValue(evt);
868  if(dbThis){
869  cout << "DalitzBWBoxSet::genValueNoPs adding " << (*this)[i].name()
870  << " with " << val << endl;
871  }
872  sum += val;
873  }
874  if(phaseSpaceFrac() > 0)sum += _psbox.genValue();
875 
876  if(dbThis){
877  cout << "DalitzBWBoxSet::genValueNoPs returning " << sum << endl;
878  }
879  return sum;
880 }
881 double DalitzBWBoxSet::genValuePs(const DalitzEvent& evt)const{
882  //bool dbThis=false;
883  return evt.phaseSpace()/phaseSpaceIntegral();
884 }
885 
886 
888  // for debug only
889  //bool dbThis=false;
890  double sum=0;
891  for(int i=0; i < evt.numPermutations(); i++){
892  evt.setPermutationIndex(i);
893  sum += genValue(evt);
894  }
895  evt.setPermutationIndex(0);
896  sum /= ((double) evt.numPermutations());
897  return sum;
898 }
899 
900 double DalitzBWBoxSet::genValue(const DalitzEvent& evt)const{
901  bool dbThis=false;
902  double sum=0;
903  sum = genValueNoPs(evt) * (1.0 - phaseSpaceFrac());
904 
905  if(dbThis) cout << "DalitzBWBoxSet::genValue sum before phase space "
906  << sum << endl;
907  double ps = phaseSpaceFrac()*genValuePs(evt);
908  if(dbThis) cout << "DalitzBWBoxSet::genValue adding ps = "
909  << ps << endl;
910  sum += ps;
911  if(dbThis) cout << "DalitzBWBoxSet::genValue sum after phase space "
912  << sum << endl;
913  return sum;
914 }
915 void DalitzBWBoxSet::print(ostream& os) const{
916  for(unsigned int i=0; i < this->size(); i++){
917  os << " box " << i << " )" << (*this)[i] << "\n";
918  }
919  return;
920 }
921 
922 ostream& operator<<(ostream& os, const DalitzBWBoxSet& box){
923  box.print(os);
924  return os;
925 }
926 //
virtual const EVENT_TYPE & getEventRef(unsigned int i) const
Definition: EventPtrList.h:49
virtual void setWeight(double w)
double VolumeSum() const
void setPattern(const DalitzEventPattern &pat)
void makeVolumeProbIntervals()
std::vector< double > _volumeProbs
bool am_I_generating_what_I_think_I_am_generating(int Nevents=1000000)
double calc_pick_ps_prob() const
DalitzHistoSet weightedHistoSet() const
int numPermutations() const
double fullPdf(DalitzEvent &evt)
bool ready() const
void push_back(const DalitzBWBox &c)
bool ensureFreshEvents()
virtual bool Add(const EVENT_TYPE &evt)
Definition: EventPtrList.h:88
virtual MINT::counted_ptr< DalitzEvent > makeEventForOwner()
bool drawWithFitNorm(const DalitzHistoSet &fit, const std::string &baseName="", const std::string &format="eps", const std::string &fitDrawOpt="HIST C SAME") const
DalitzEventPtrList _eventPtrList
double genValue(const DalitzEvent &evt) const
virtual double phaseSpace() const
static const double s
void set_psbox_height_and_weight()
virtual MINT::counted_ptr< DalitzEvent > makeWeightedEventForOwner()
virtual MINT::counted_ptr< IDalitzEvent > newEvent()
double generalisedPareto_estimateMaximum(const std::vector< double > &input, double CL=0.001)
virtual double RealVal(EVENT_TYPE &evt)=0
double genValuePs(const DalitzEvent &evt) const
double _phaseSpaceIntegral
TRandom * _rnd
double findMaxInList(double &sampleMax)
virtual bool Add(const DalitzEvent &evt)
double _maxWeightInSample
bool compareGenerationMethodsForFullPDF(int Nevents=1000000)
void setUnWeightPs(bool doSo=true)
MINT::counted_ptr< DalitzEvent > makeEventForOwner()
void setPermutationIndex(int i)
virtual MINT::counted_ptr< DalitzEvent > phaseSpaceEvent()
bool setRnd(TRandom *rnd=gRandom)
double heightSum() const
virtual unsigned int size() const
Definition: EventPtrList.h:76
ostream & operator<<(ostream &os, const DalitzBWBoxSet &box)
virtual double getWeight() const
bool setRnd(TRandom *rnd=gRandom)
bool draw(const std::string &baseName="", const std::string &drawOpt="", const std::string &format="eps") const
std::vector< DalitzBWBox >::iterator begin()
virtual MINT::counted_ptr< IDalitzEvent > newUnweightedEvent()
double _pick_ps_prob
DalitzBWBoxSet(MINT::IReturnRealForEvent< IDalitzEvent > *amps=0, TRandom *r=gRandom)
DalitzPhaseSpaceBox _psbox
MINT::counted_ptr< DalitzEvent > popEventFromList()
double _phaseSpaceFrac
bool checkIntegration() const
static double __phaseSpaceFracDefaultValue
unsigned int size() const
const MappedDalitzBWArea & area() const
Definition: DalitzBWBox.h:65
double genValueWithLoop(DalitzEvent &evt) const
MINT::counted_ptr< DalitzEvent > makeEventForOwner()
virtual MINT::counted_ptr< DalitzEvent > tryEventForOwner()
int justThrowAwayData(double maxValue, MINT::IReturnRealForEvent< IDalitzEvent > *amps)
double _maxWeightEstimate
double getVal(const DalitzEventPattern &_pat)
virtual void setGeneratorPdfRelativeToPhaseSpace(double gpdf)
virtual MINT::counted_ptr< EVENT_TYPE > popLastEventPtr()
Definition: EventPtrList.h:80
double phaseSpaceFrac() const
virtual MINT::counted_ptr< DalitzEvent > makeWeightedApproxEventForOwner()
void print(std::ostream &os) const
double phaseSpaceIntegral() const
double genValueNoPs(const DalitzEvent &evt) const
bool save(const std::string &filename="DalitzHistos.root") const
MINT::IReturnRealForEvent< IDalitzEvent > * _ampSum
void add(DalitzBWBox &box)