MINT2
DalitzPdfBaseFastInteg.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:01 GMT
4 #include "Mint/FitAmpSum.h"
5 #include "Mint/DalitzBWBoxSet.h"
6 #include "Mint/Minimiser.h"
7 
8 #include <iostream>
9 
10 using namespace std;
11 using namespace MINT;
12 
14  return (0 == _mps ? MinuitParameterSet::getDefaultSet() : _mps);
15 }
17  return (0 == _mps ? MinuitParameterSet::getDefaultSet() : _mps);
18 }
19 
20 bool DalitzPdfBaseFastInteg::saveIntegrator(const std::string& fname) const{
21  return _faint.save(fname);
22 }
23 
25  _integrating = true;
26  if( ! _faint.initialised()){
27  if(_pat.empty()){
28  cout << "DalitzPdfBaseFastInteg::getNorm: don't know event pattern!"
29  << endl;
30  _integrating = false;
31  return false;
32  }
33  if(0 == getAmps()){
34  cout << "DalitzPdfBaseFastInteg::getNorm: don't have amps!"
35  << endl;
36  _integrating = false;
37  return false;
38  }
39 
40  /*
41  if("" != _commaSepList_of_SavedIntegrators){
42  _faint.initialiseFromFile(evtPtr->eventPattern()
43  , _amps
44  , _commaSepList_of_SavedIntegrators);
45  }else{
46  */
47  _faint.initialise(_commaSepList_of_SavedIntegrators
48  , _pat
49  , getAmps()
50  //, _efficiency
51  , getEventGenerator()
52  , gRandom
53  , _precision
54  );
55  //}
56 
57 #ifdef CHECK_INTEGRATOR
58  cout << "WARNING in DalitzPdfBaseFastInteg::getNorm!!!"
59  << " using old, inefficient integrator."
60  << " Won't work for efficiencies!!"
61  << endl;
62  _mcint.initialise(evtPtr->eventPattern()
63  , getAmps()//this
64  , getEventGenerator()
65  , gRandom
66  , _precision
67  );
68 #endif
69  }
70 
71  _norm = _faint.getVal();
72 
73 
74 #ifdef CHECK_INTEGRATOR
75  double fastVal = _norm;
76  double oldVal = _mcint.getVal();
77  cout << "DalitzPdfBaseFastInteg::getNorm:"
78  << " compare mcint integral with fast: "
79  << "\n\t old: " << oldVal << " , fast: " << fastVal
80  << ", ratio " << oldVal/fastVal
81  << endl;
82  _norm = oldVal;
83 #endif
84 
85 
86  _integrating = false;
87  return _norm > 0;
88 }
90  if(_pat.empty()) return 0;
91  if(0 == getAmps()) return 0;
93  ptr(getAmps()->makeEventGenerator(_pat));
94  _defaultGenerator = ptr;
95  return _defaultGenerator.get();
96 }
97 
99  if(0 == _generator) makeDefaultGenerator();
100  return _generator;
101 }
103  if(0 == _generator){
104  cout << "WARNING in DalitzPdfBaseFastInteg::getEventGenerator() const"
105  << " returning 0 pointer." << endl;
106  }
107  return _generator;
108 }
109 
111  _precision = prec;
112 #ifdef CHECK_INTEGRATOR
113  _mcint.setPrecision(_precision);
114 #endif
115  _faint.setPrecision(_precision);
116 }
117 
119  getNorm();
120  _val= -1;
121  _redoGradNorm=true;
122 }
123 
126  generator
128  , double prec
130 
131  )
132  : PdfBase<IDalitzEvent>()
133  , _mps(mps)
134  , _pat(pat)
135  , _norm(-1)
136  , _precision(prec)
137  , _amps(amps)
138  , _countedAmps(0)
139  //, _efficiency(0)
140  , _generator(generator)
141  , _integrating(0)
142  , _defaultGenerator(0)
143  , _commaSepList_of_SavedIntegrators("")
144  , _val(-1)
145  , _gradNorm(1)
146  , _redoGradNorm(true)
147 {
148  setup();
149  _gradNorm.resize(this->getMPS()->size());
150  //_gradNorm= new Double_t[this->getMPS()->size()];
151  for(unsigned int i=0; i < _gradNorm.size(); i++) _gradNorm[i]= 0.;
152 }
153 
156  generator
157  //, counted_ptr<IGetDalitzEvent> eff
158  , MinuitParameterSet* mps
159  , double prec
160  )
161  : PdfBase<IDalitzEvent>()
162  , _mps(mps)
163  , _pat(pat)
164  , _norm(-1)
165  , _precision(prec)
166  , _amps(0)
167  , _countedAmps(0)
168  // , _efficiency(eff)
169  , _generator(generator)
170  , _integrating(0)
171  , _defaultGenerator(0)
172  , _commaSepList_of_SavedIntegrators("")
173  , _val(-1)
174  , _gradNorm(1)
175  , _redoGradNorm(true)
176 {
177  setup();
178  _gradNorm.resize(this->getMPS()->size());//= new Double_t[this->getMPS()->size()];
179  for(unsigned int i=0; i < _gradNorm.size(); i++) _gradNorm[i]= 0.;
180  // cout << "pset pointer in DalitzPdfBaseFastInteg " << _mps << " = " << getMPS() << endl;
181 }
182 
185  , IPdf<IDalitzEvent>()
186  , IDalitzPdf()
187  , PdfBase<IDalitzEvent>(other)
188  , _mps(other._mps)
189  , _pat(other._pat)
190  , _norm(other._norm)
191  , _precision(other._precision)
192  , _amps(other._amps)
193  , _countedAmps(other._countedAmps)
194  //, _efficiency(other._efficiency)
195  , _generator(other._generator)
196  , _integrating(other._integrating)
197  , _defaultGenerator(other._defaultGenerator)
198  , _commaSepList_of_SavedIntegrators(other._commaSepList_of_SavedIntegrators)
199  , _val(-1)
200  , _gradNorm(1)
201  , _redoGradNorm(true)
202 {
203  setup();
204  _gradNorm.resize(this->getMPS()->size());//= new Double_t[this->getMPS()->size()];
205  for(unsigned int i=0; i < _gradNorm.size(); i++) _gradNorm[i]= 0.;
206 }
207 
209  bool dbThis=false;
210  if(dbThis)cout << "DalitzPdfBaseFastInteg::~DalitzPdfBaseFastInteg() called" << endl;
211  //delete[] _gradNorm;
212  if(dbThis)cout << "DalitzPdfBaseFastInteg::~DalitzPdfBaseFastInteg() done" << endl;
213 }
214 
216  makeAmps(); // it's important to do this at construction time, otherwise the MinuitParameterSet will be empty.
217 }
219  cout << "DalitzPdfBaseFastInteg::makeAmps() with MPS pointer " << getMPS()
220  << " and _amps pointer " << _amps << endl;
221  if(0 == _amps && (! _pat.empty())){
222  cout << "DalitzPdfBaseFastInteg::getAmps() making _amps with pointer " << getMPS()
223  << endl;
224  _amps = new FitAmpSum(_pat, getMPS());
226  _countedAmps = cp;
227  }
228  return (bool) _amps;
229 }
230 
232  return _integrating;
233 }
234 
236  return evt.phaseSpace();
237 }
239  if(_pat.empty()) _pat = evt.eventPattern();
240  if(_integrating) return getVal_withPs(evt);
241  else return getVal_noPs(evt);
242 }
244  bool dbThis=false;
245  static double maxVal=0;
246  if(_pat.empty()) _pat = evt.eventPattern();
247 
248  if(_integrating){
249  // shouldn't really do that - use getVal or getVal_withPs
250  // when you integrate (automatically done in getVal()):
251  return un_normalised_noPs(evt);
252  }else{
253  if(_norm == -1) getNorm();
254  double num = un_normalised_noPs(evt);
255  if(dbThis){
256  double val=num/_norm;
257  if(fabs(val) > maxVal){
258  maxVal=fabs(val);
259  if(dbThis){
260  cout << "biggest un_normalised / norm, yet: "
261  << num << " / " <<_norm
262  << " = " << num/_norm
263  << ". ps = " << phaseSpace(evt)
264  << endl;
265  cout << "for event: "; evt.print(); cout << endl;
266  }
267  }
268  }
269 
270  /*
271  cout << "\n DalitzPdfBaseFastInteg::getVal_noPs():"
272  << " for event ptr " << getEvent()
273  << " returning "
274  << num << " / " << _norm << endl;
275  */
276 
277  _val= num/_norm;
278  if(dbThis) cout << "DalitzPdfBaseFastInteg::getVal_noPs: returning "
279  << _val << endl;
280  return num/_norm;
281  }
282 }
284  if(_pat.empty()) _pat = evt.eventPattern();
285 
286  if(_integrating){
287  return un_normalised_noPs(evt)*phaseSpace(evt);
288  }else{
289  if(_norm == -1) getNorm();
290  double num = un_normalised_noPs(evt)*phaseSpace(evt);
291  return num/_norm;
292  }
293 }
294 
296 
297  if(_val == -1)un_normalised_noPs(evt);
298  if(_norm == -1) getNorm();
299 
300  unsigned int N= mps->size();
301 
302  std::vector<double> gradAmpSum(N);
303  for(unsigned int j=0; j < gradAmpSum.size(); j++) gradAmpSum[j]= 0.;
304 
305  _amps->Gradient(evt,gradAmpSum,mps);
306 
307  if(_redoGradNorm){
308  for(unsigned int j=0; j < _gradNorm.size(); j++) _gradNorm[j]= 0.;
309  _faint.Gradient(mps, _gradNorm);
310  _redoGradNorm=false;
311  }
312 
313  if(_gradNorm.size() != grad.size() || gradAmpSum.size() !=grad.size()
314  || gradAmpSum.size() != _gradNorm.size()){
315  cout << "array size mismatch in DalitzPdfBaseFastInteg::Gradient" << endl;
316  throw "out";
317  }
318 
319  for (unsigned int i=0; i<N; i++) {
320  if(mps->getParPtr(i)->hidden() || mps->getParPtr(i)->iFixInit())continue;
321  grad.at(i)= gradAmpSum.at(i)/_norm- _val/_norm*_gradNorm.at(i);
322  }
323 
324 
325 }
326 
327 void DalitzPdfBaseFastInteg::GradientForLasso(vector<double>& grad){
328  _faint.GradientForLasso(this->getMPS(),grad);
329 }
330 
331 bool DalitzPdfBaseFastInteg::makePlots(const std::string& filename) const{
332  return histoSet().save(filename);
333 }
335  return (_faint.histoSet());
336 }
338  // non-const version to satisfy IDalitzPdf
339  return (_faint.histoSet());
340 }
341 
342 void DalitzPdfBaseFastInteg::saveEachAmpsHistograms(const std::string& prefix) const{
344  return;
345 }
346 
347 std::vector<DalitzHistoSet> DalitzPdfBaseFastInteg::GetEachAmpsHistograms(){
348  return _faint.GetEachAmpsHistograms();
349 }
350 
352  return (_faint.interferenceHistoSet());
353 }
355  // non-const version to satisfy IDalitzPdf
356  return (_faint.interferenceHistoSet());
357 }
358 void DalitzPdfBaseFastInteg::saveInterferenceHistograms(const std::string& prefix) const{
360  return;
361 }
364 }
365 
367 
368  //_faint.doFinalStats();
369 }
370 
372  _faint.doFinalStats(mini);
373 }
374 
375 void DalitzPdfBaseFastInteg::setIntegratorFileName(const std::string& commaSeparatedList){
376  _commaSepList_of_SavedIntegrators=commaSeparatedList;
377 }
378 
379 
380 //
std::vector< DalitzHistoSet > GetEachAmpsHistograms()
std::vector< DalitzHistoSet > GetInterferenceHistograms()
virtual double getVal(IDalitzEvent &evt)
void saveEachAmpsHistograms(const std::string &prefix) const
virtual double getVal_noPs(IDalitzEvent &evt)
MINT::IEventGenerator< IDalitzEvent > * makeDefaultGenerator()
virtual double phaseSpace() const =0
std::vector< DalitzHistoSet > GetInterferenceHistograms()
void setIntegratorFileName(const std::string &commaSeparatedList)
virtual int iFixInit() const =0
MINT::IEventGenerator< IDalitzEvent > * getEventGenerator()
void saveInterferenceHistograms(const std::string &prefix) const
IMinuitParameter * getParPtr(unsigned int i)
unsigned int size() const
virtual double getVal_withPs(IDalitzEvent &evt)
void doFinalStats(MINT::Minimiser *mini=0)
virtual void print(std::ostream &os=std::cout) const =0
bool saveIntegrator(const std::string &fname) const
void setIntegrationPrecision(double prec)
virtual void Gradient(IDalitzEvent &evt, std::vector< double > &grad, MINT::MinuitParameterSet *mps)
MINT::counted_ptr< IFastAmplitudeIntegrable > _countedAmps
virtual DalitzHistoSet interferenceHistoSet() const
void GradientForLasso(MINT::MinuitParameterSet *mps, std::vector< double > &grad)
DalitzPdfBaseFastInteg(const DalitzEventPattern &pat, MINT::IEventGenerator< IDalitzEvent > *generator, IFastAmplitudeIntegrable *amps, double precision=1.e-3, MINT::MinuitParameterSet *mps=0)
virtual DalitzHistoSet histoSet() const
DalitzHistoSet histoSet() const
virtual double un_normalised_noPs(IDalitzEvent &evt)=0
std::string _commaSepList_of_SavedIntegrators
bool makePlots(const std::string &filename) const
virtual void doFinalStats(MINT::Minimiser *mini=0)
virtual bool hidden() const =0
IFastAmplitudeIntegrable * _amps
virtual const DalitzEventPattern & eventPattern() const =0
virtual void GradientForLasso(std::vector< double > &grad)
std::vector< double > _gradNorm
MINT::MinuitParameterSet * getMPS()
std::vector< DalitzHistoSet > GetEachAmpsHistograms()
void saveEachAmpsHistograms(const std::string &prefix) const
virtual void Gradient(IDalitzEvent &evt, std::vector< double > &grad, MINT::MinuitParameterSet *mps)
FastAmplitudeIntegrator _faint
virtual double phaseSpace(IDalitzEvent &evt)
bool save(const std::string &filename="DalitzHistos.root") const
DalitzHistoSet interferenceHistoSet() const
X * get() const
Definition: counted_ptr.h:123
void Gradient(MINT::MinuitParameterSet *mps, std::vector< double > &grad)
void saveInterferenceHistograms(const std::string &prefix) const