MINT2
FlexiFastAmplitudeIntegrator.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:18:02 GMT
4 #include "Mint/FitAmpSum.h"
5 #include "Mint/IDalitzEvent.h"
6 #include "Mint/Minimiser.h"
7 
8 #include "TRandom.h"
9 #include "TRandom3.h"
10 
11 #include <iostream>
12 #include <sstream>
13 
14 #include <ctime>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 using namespace std;
19 using namespace MINT;
20 
24  , IEventGenerator<IDalitzEvent> * generator
25  , TRandom* rnd
26  , double precision
27  , const std::string& fastFlexiEventFileName
28  )
29  : _Ncalls(0), _NReEvaluations(0), _sumT(0.0), _sumTRe(0.0), _sumTfast(0.0), _db(false)
30  // , _fastFlexiEventList(fastFlexiEventFileName, "RECREATE")
31 {
32  initialise(pattern, amps, generator, rnd, precision);
33  (void)fastFlexiEventFileName;
34 }
35 
36 FlexiFastAmplitudeIntegrator::FlexiFastAmplitudeIntegrator(const std::string& fastFlexiEventFileName)
37  : _sumT(0.0), _sumTRe(0.0), _sumTfast(0.0), _initialised(0), _db(false)//, _fastFlexiEventList(fastFlexiEventFileName, "RECREATE")
38 
39 {
40  (void)fastFlexiEventFileName;
41 }
42 
43 
47  , IEventGenerator<IDalitzEvent> * generator
48  , TRandom* rnd
49  , double precision
50  ){
51  bool sc=true;
52  sc &= setValues(pattern, amps, generator, rnd, precision);
53  sc &= generateEnoughEvents();
54  return sc;
55 }
56 
57 
59  int totalEvents = _numEvents + other._numEvents;
61 
62  if(totalEvents > 0){
63  _mean = (_numEvents * _mean + other._numEvents * other._mean)/totalEvents;
64  }
65  _numEvents = totalEvents;
66  _Ncalls += other._Ncalls;
68  _sumT += other._sumT;
69  _sumTRe += other._sumTRe;
70  _sumTfast += other._sumTfast;
71  if(0 != this->_integCalc){
72  _integCalc->add(other._integCalc);
73  }else{
74  if(0 != other._integCalc){
76  _integCalc = nL;
77  }
78  }
79  if(0 != this->_integCalc_copyForDebug){
81  }else{
82  if(0 != other._integCalc_copyForDebug){
84  nLc(new IntegCalculator(*(other._integCalc_copyForDebug)));
86  }
87  }
88  _initialised |= other._initialised;
89  _db |= other._db;
90 
91  // other quantities we take over if they aren't defined, here:
92  if(0 == _amps) _amps = other._amps;
93  if(0 == _rnd) _rnd = other._rnd;
94  if(0 == _localRnd) _localRnd = other._localRnd;
95  if(_precision <= 0.0) _precision = other._precision;
96 
97  return true;
98 }
99 
101  _rnd = makeNewRnd(time(0));
102  if(0 != _generator){
104  }
105  return true;
106 }
107 
109  if(seed < -9998) seed = time(0);
110  cout << "FlexiFastAmplitudeIntegrator::makeNewRnd with seed " << seed << endl;
111  counted_ptr<TRandom> lr(new TRandom3(seed));
112  _localRnd = lr;
113  _rnd = lr.get();
114  return _rnd;
115 }
119  //, counted_ptr<IGetDalitzEvent> weight
120  , IEventGenerator<IDalitzEvent> * generator
121  , TRandom* rnd
122  , double precision
123  ){
124  _pat = pattern;
125  _amps = amps;
126  //_weight = weight;
127  if(0 != rnd){
128  _rnd = rnd;
129  }else{
130  _rnd = makeNewRnd(time(0));
131  }
132  _precision = precision;
133 
134  _generator = generator;
135 
136  // _integCalc = amps->makeIntegCalculator();
137  _integCalc = amps->makeFitAmpPairList();
138  _integCalc->setFast();
139 
140  if(_db) _integCalc_copyForDebug = amps->makeFitAmpPairList();
141 
142  if(0 == _integCalc){
143  cout << "ERROR in FlexiFastAmplitudeIntegrator::initialise"
144  << " amps->makeIntegCalculator()"
145  << " return NULL pointer!" << endl;
146  cout << "\t This will go wrong." << endl;
147  }
148  _Ncalls=0;
149  _NReEvaluations=0;
150  _sumT = 0.0;
151  _sumTRe = 0.0;
152  _sumTfast = 0.0;
153 
154  _initialised = true;
155  return _initialised;
156 }
157 
159  if(! _initialised){
160  cout << " FlexiFastAmplitudeIntegrator::updateEventSet "
161  << " need to know pattern first." << endl;
162  return 0;
163  }
164 
166  int missingEvents = Nevents - _integCalc->numEvents();
167 
168  if(missingEvents > 0){
169  cout << "missing events: " << missingEvents
170  << " = " << Nevents
171  << " - " << _integCalc->numEvents()
172  << endl;
173  addEvents(missingEvents);
174  }else{
175  cout << "want " << Nevents
176  << ", have " << _integCalc->numEvents()
177  << ", so no more events missing." << endl;
178  }
180  return _integCalc->numEvents();
181 }
182 
185  for(unsigned int i=0; i < _fastFlexiEventList.size(); i++){
187  }
189 }
192  for(unsigned int i=0; i < _fastFlexiEventList.size(); i++){
194  }
196 }
197 
199  return 1;
200  // (not in use, but left as a hook should the need arise)
201  /*
202  if(0 == _weight) return 1;
203 
204  _weight->setEvent(evtPtr);
205  double wght = _weight->RealVal();
206  _weight->resetEventRecord();
207  return wght;
208  */
209 }
210 
214  _integCalc->addEvent(listEvent, weight(&listEvent));
215 }
217  _integCalc->reAddEvent(evt, weight(&evt));
218 }
219 
221  bool dbThis=false;
222  if(dbThis) cout << "FlexiFastAmplitudeIntegrator::addEvents "
223  << " got called " << endl;
224  if(Nevents <= 0) return _integCalc->numEvents();
225 
226  if(0 == _generator){
227  cout << "FlexiFastAmplitudeIntegrator::addEvents "
228  << " adding flat events. " << endl;
229  for(int i=0; i < Nevents; i++){
230  DalitzEvent evt(_pat, _rnd);
231  addEvent(evt);
232  }
233  }else{
234  int N_success = 0;
235  unsigned long int maxTries = (unsigned long int) Nevents * 1000;
236  // dealing with possible overflow:
237  if(maxTries < (unsigned long int) Nevents) maxTries = Nevents * 100;
238  if(maxTries < (unsigned long int) Nevents) maxTries = Nevents * 10;
239  if(maxTries < (unsigned long int) Nevents) maxTries = Nevents;
240 
241  time_t startTime = time(0);
242  for(unsigned long int i=0; i < maxTries && N_success < Nevents; i++){
244  if(dbThis) cout << "got event with ptr: " << ptr << endl;
245  if(0 == ptr){
246  if(_generator->exhausted()){
247  cout << "ERROR in FlexiFastAmplitudeIntegrator::addEvents"
248  << "\n\t Generator exhausted, cannot generate more events."
249  << endl;
250  break;
251  }
252  }else{
253  addEvent(*ptr);
254  N_success++;
255  int printEvery = 1000;
256  if(N_success > 1000) printEvery = 50000;
257  if(N_success > 100000) printEvery = 100000;
258  bool printout = (N_success%printEvery == 0);
259  if(dbThis) printout |= (N_success < 20);
260 
261  if(printout){
262  cout << " FlexiFastAmplitudeIntegrator::addEvents: added " << N_success
263  << " th event";
264  evaluateSum();
265  double v= variance();
266  double sigma = -9999;
267  if(v > 0) sigma = sqrt(v);
268  cout << "\t integ= " << _mean << " +/- " << sigma;
269  cout << "\t("
270  << MINT::stringtime(difftime(time(0), startTime))
271  << ")";
272  cout << endl;
273  }
274  }
275  }
276  }
277  if(_integCalc->numEvents() < Nevents){
278  cout << "ERROR in FlexiFastAmplitudeIntegrator::addEvents"
279  << "\n\t> I was supposed to add " << Nevents << " events,"
280  << "\n\t> but now the total list size is only "
281  << _integCalc->numEvents()
282  << endl;
283  }else{
284  cout << " added " << Nevents << " _integCalc->numEvents "
285  << _integCalc->numEvents() << endl;
286 
287  }
288  return _integCalc->numEvents();
289 }
290 
292  return _integCalc->variance();
293 }
295  time_t tstart = time(0);
296  // _Ncalls++;
297 
299  double v = variance();
300  double sigma = -9999;
301  if(v >= 0) sigma = sqrt(v);
302  double actualPrecision = -9999;
303  if(_mean != 0 && sigma > 0) actualPrecision = sigma/_mean;
304 
305  double delT = difftime(time(0), tstart);
306 
307  int printEvery = 100;
308  if(_Ncalls > 1000) printEvery = 1000;
309  if(_Ncalls > 10000) printEvery = 10000;
310  if(_Ncalls > 100000) printEvery = 100000;
311  bool printout = _Ncalls < 3 || (0 == _Ncalls%printEvery);
312 
313  if(printout || _db){
314  cout << " FlexiFastAmplitudeIntegrator::evaluateSum() for "
315  << _integCalc->numEvents() << " events, "
316  << _Ncalls << " th call:";
317  cout << "\n\t> getting: " << _mean << " +/- " << sigma
318  << "\n\t> precision: requested: " << _precision*100
319  << "%, actual: " << actualPrecision*100 << "%"
320  << "\n\t> This took " << delT << " s."
321  << endl;
322  if(_db){
323  cout << "checking save and read-back:" << endl;
324  _integCalc->save("checkSaveAndReadBack");
325  _integCalc_copyForDebug->retrieve("checkSaveAndReadBack");
326  cout << " copy values:"
327  << "\n\t mean: " << _integCalc_copyForDebug->integral()
328  << "\n\t variance: " << _integCalc_copyForDebug->variance()
329  << endl;
330  }
331  }
332  return _mean;
333 }
334 
337  evaluateSum();
338  double v = variance();
339  double safetyFactor = 1.15;
340  double maxVariance = _precision*_precision * _mean*_mean;
342  (v*safetyFactor/maxVariance);
343  // round to the nearest 100:
344  _numEvents = (_numEvents/100 + 1)*100;
345 
346  cout << " FlexiFastAmplitudeIntegrator::determineNumEvents():"
347  << "\n\t> mean: " << _mean << ", rms " << sqrt(v)
348  << "\n\t> currently, our relative precision is "
349  << sqrt(v)/_mean
350  << "\n\t> to achieve the requested pecision of " << _precision << ","
351  << "\n\t> (with a safety factor) I think we'll need "
352  << (v*safetyFactor/maxVariance)
353  << " times that, i.e. " << _numEvents << " events" << endl;
354 
355  return _numEvents;
356 }
357 
360  int counter(0);
361  while(determineNumEvents() > _integCalc->numEvents() &&
362  counter < 10 && _numEvents > 0){
364  counter++;
365  }
366  return _integCalc->numEvents();
367 }
368 
370  _Ncalls++;
371  time_t tstart = time(0);
372  double delT_slow, delT_fast, delT_total;
373  if( _integCalc->needToReIntegrate() ){
374  _NReEvaluations++;
375  reIntegrate();
376  delT_slow = difftime(time(0), tstart);
377  _sumTRe += delT_slow;
378  }
379  time_t tstart_2 = time(0);
380  evaluateSum();
381  delT_total = difftime(time(0), tstart);
382  delT_fast = difftime(time(0), tstart_2);
383  _sumTfast += delT_fast;
384 
385  int printEvery = 10;
386  if(_Ncalls > 100) printEvery = 100;
387  if(_Ncalls > 1000) printEvery = 1000;
388  if(_Ncalls > 10000) printEvery = 10000;
389  if(_Ncalls > 100000) printEvery = 100000;
390  bool printout = _Ncalls < 10 || (0 == _Ncalls%printEvery);
391 
392  if(_Ncalls > 0 && printout){
393  cout << "FlexiFastAmplitudeIntegrator::getVal, " << _Ncalls << "th call: Returning " << _mean
394  << ", re-evaluation fraction = " << ((double) _NReEvaluations)/((double) _Ncalls)
395  << "\n\t this call took " << delT_total << " s,"
396  << "\n\t average call " << _sumT / _Ncalls;
397  if(_NReEvaluations > 0) cout << ", average re-integration " << _sumTRe / _NReEvaluations;
398  int delN = _Ncalls - _NReEvaluations;
399  if(delN > 0){
400  cout << ", average call w/o re-integration " << _sumTfast / delN;
401  }
402  cout << " ." << endl;
403  }
404 
405  return _mean;
406 }
407 
409 
410  return _integCalc->histoSet();
411 }
412 void FlexiFastAmplitudeIntegrator::saveEachAmpsHistograms(const std::string& prefix) const{
413  return _integCalc->saveEachAmpsHistograms(prefix);
414 }
417 }
418 
421 }
422 void FlexiFastAmplitudeIntegrator::saveInterferenceHistograms(const std::string& prefix) const{
423  return _integCalc->saveInterferenceHistograms(prefix);
424 }
427 }
428 
430  bool dbThis=true;
431  if(dbThis) cout << "FlexiFastAmplitudeIntegrator::doFinalStats() called" << endl;
432  _integCalc->setSlow(); // means it makes histograms
434  _integCalc->doFinalStats(mini);
435 }
436 
437 void FlexiFastAmplitudeIntegrator::doFinalStatsAndSave(Minimiser* mini,const std::string& fname, const std::string& fnameROOT){
438  bool dbThis=true;
439  if(dbThis) cout << "FlexiFastAmplitudeIntegrator::doFinalStats() called" << endl;
440  _integCalc->setSlow(); // means it makes histograms
442  _integCalc->doFinalStatsAndSave(mini,fname,fnameROOT);
443 }
444 
446  return _integCalc->getFractionChi2();
447 }
448 
449 /*
450 bool FlexiFastAmplitudeIntegrator::save(const std::string& fname)const{
451  return _integCalc->save(fname);
452 }
453 */
454 
455 //
virtual bool Add(const EVENT_TYPE &evt)
Definition: EventList.h:63
MINT::counted_ptr< FitAmpPairList > _integCalc
virtual double getFractionChi2() const
virtual void doFinalStatsAndSave(MINT::Minimiser *min=0, const std::string &fname="FitAmpResults.txt", const std::string &fnameROOT="fitFractions.root")
virtual bool exhausted() const =0
void startForcedReIntegration()
virtual void reAddEvent(IDalitzEvent &evt, double weight=1)
std::vector< DalitzHistoSet > GetEachAmpsHistograms()
void saveEachAmpsHistograms(const std::string &prefix) const
virtual void doFinalStats(MINT::Minimiser *min=0)
FlexiFastAmplitudeIntegrator(const std::string &fastFlexiEventFileName="fastFlexiEventFileName.root")
virtual double variance() const
std::vector< DalitzHistoSet > GetInterferenceHistograms()
std::vector< DalitzHistoSet > GetInterferenceHistograms()
bool setValues(const DalitzEventPattern &pattern, IFastAmplitudeIntegrable *amps=0, MINT::IEventGenerator< IDalitzEvent > *eventGenerator=0, TRandom *rnd=0, double precision=1.e-2)
bool initialise(const DalitzEventPattern &pattern, IFastAmplitudeIntegrable *amps=0, MINT::IEventGenerator< IDalitzEvent > *eventGenerator=0, TRandom *rnd=0, double precision=1.e-2)
virtual bool add(const FitAmpPairList &otherList)
void doFinalStatsAndSave(MINT::Minimiser *min=0, const std::string &fname="FitAmpResults.txt", const std::string &fnameROOT="fitFractions.root")
virtual counted_ptr< RETURN_TYPE > newEvent()=0
bool needToReIntegrate() const
virtual double integral() const
virtual MINT::counted_ptr< FitAmpPairList > makeFitAmpPairList()=0
virtual bool ensureFreshEvents()=0
MINT::IEventGenerator< IDalitzEvent > * _generator
virtual bool save(const std::string &asSubdirOf=".") const
void saveEachAmpsHistograms(const std::string &prefix) const
virtual unsigned int size() const
Definition: EventList.h:59
virtual void doFinalStats(MINT::Minimiser *mini=0)
MINT::counted_ptr< TRandom > _localRnd
virtual bool add(const FlexiFastAmplitudeIntegrator &other)
void saveInterferenceHistograms(const std::string &prefix) const
void saveInterferenceHistograms(const std::string &prefix) const
MINT::counted_ptr< FitAmpPairList > _integCalc_copyForDebug
DalitzHistoSet interferenceHistoSet() const
virtual int numEvents() const
virtual void addEvent(IDalitzEvent *evtPtr, double weight=1)
virtual DalitzHistoSet histoSet() const
std::string stringtime(double dt)
Definition: Utils.cpp:14
virtual bool retrieve(const std::string &asSubdirOf=".")
X * get() const
Definition: counted_ptr.h:123
std::vector< DalitzHistoSet > GetEachAmpsHistograms()