MINT2
BinFlipChi2.cpp
Go to the documentation of this file.
1 #include <Mint/BinFlipChi2.h>
3 
4 using namespace std ;
5 using MINT::FitParameter ;
7 
8 vector<double> _blindingPars(unsigned long seed, double range, unsigned long offset){
9  if(seed == 0)
10  return vector<double>();
11  vector<double> pars(3, seed + offset);
12  pars[1] = -range;
13  pars[2] = range;
14  return pars;
15 }
16 
17 BinFlipParSet::BinFlipParSet(double _zcp_Re, double zcp_Re_step, double _zcp_Im, double zcp_Im_step,
18  double _deltaz_Re, double deltaz_Re_step, double _deltaz_Im, double deltaz_Im_step,
19  unsigned long blindingSeed, double zBlindRange, double deltazBlindRange) :
20  zcp_Re("zcp_Re", FitParameter::FIT, _zcp_Re, zcp_Re_step, 0., 0., this,
21  NamedParameterBase::VERBOSE, nullptr, _blindingPars(blindingSeed, zBlindRange, 0)),
22  zcp_Im("zcp_Im", FitParameter::FIT, _zcp_Im, zcp_Im_step, 0., 0., this,
23  NamedParameterBase::VERBOSE, nullptr, _blindingPars(blindingSeed, zBlindRange, 1)),
24  deltaz_Re("deltaz_Re", FitParameter::FIT, _deltaz_Re, deltaz_Re_step, 0., 0., this,
25  NamedParameterBase::VERBOSE, nullptr, _blindingPars(blindingSeed, deltazBlindRange, 2)),
26  deltaz_Im("deltaz_Im", FitParameter::FIT, _deltaz_Im, deltaz_Im_step, 0., 0., this,
27  NamedParameterBase::VERBOSE, nullptr, _blindingPars(blindingSeed, deltazBlindRange, 3))
28 {
29 }
30 
31 BinFlipParSet::BinFlipParSet(const string& fname) :
32  zcp_Re("zcp_Re", fname.c_str(), this, FitParameter::FIT),
33  zcp_Im("zcp_Im", fname.c_str(), this, FitParameter::FIT),
34  deltaz_Re("deltaz_Re", fname.c_str(), this, FitParameter::FIT),
35  deltaz_Im("deltaz_Im", fname.c_str(), this, FitParameter::FIT)
36 {}
37 
43 }
44 
45 complex<double> BinFlipParSet::zcp() const {
46  return complex<double>(zcp_Re.blindedMean(), zcp_Im.blindedMean()) ;
47 }
48 
49 complex<double> BinFlipParSet::deltaz() const {
50  return complex<double>(deltaz_Re.blindedMean(), deltaz_Im.blindedMean()) ;
51 }
52 
53 complex<double> BinFlipParSet::unblindZcp() const {
54  return complex<double>(zcp_Re.getCurrentFitVal(), zcp_Im.getCurrentFitVal()) ;
55 }
56 
57 complex<double> BinFlipParSet::unblindDeltaz() const {
58  return complex<double>(deltaz_Re.getCurrentFitVal(), deltaz_Im.getCurrentFitVal()) ;
59 }
60 
61 pair<complex<double>, complex<double> > BinFlipParSet::fromXY(double x, double y, double magqoverp, double phi) {
62  complex<double> z(-y, -x) ;
63  complex<double> qoverp = polar(magqoverp, phi) ;
64  complex<double> poverq = pow(qoverp, -1) ;
65  complex<double> zcp = z * (qoverp + poverq) / 2. ;
66  complex<double> deltaz = z * (qoverp - poverq) / 2. ;
67  return make_pair(zcp, deltaz) ;
68 }
69 
71  Minimisable(fitPars),
72  m_fitPars(fitPars),
73  m_zcp(fitPars->unblindZcp()),
74  m_dz(fitPars->unblindDeltaz())
75 {}
76 
80 }
81 
82 TH2D BinFlipChi2Base::scan2D(unsigned ip1, unsigned ip2, float nSigmaRange, unsigned nBins, bool zeroCentre,
83  float scale) {
86  const double v1 = p1->getCurrentFitVal() ;
87  const double v2 = p2->getCurrentFitVal() ;
88  const double s1 = p1->err() ;
89  const double s2 = p2->err() ;
90  double v1min = v1 - s1 * nSigmaRange ;
91  double v1max = v1 + s1 * nSigmaRange ;
92  double v2min = v2 - s2 * nSigmaRange ;
93  double v2max = v2 + s2 * nSigmaRange ;
94  if(zeroCentre){
95  v1min -= v1 ;
96  v1max -= v1 ;
97  v2min -= v2 ;
98  v2max -= v2 ;
99  }
100  TH2D hscan(("deltachi2_" + p2->name() + "_vs_" + p1->name()).c_str(), "",
101  nBins, v1min*scale, v1max*scale, nBins, v2min*scale, v2max*scale) ;
102  const double chi2 = getVal() ;
103  for(unsigned i1 = 1 ; i1 < nBins + 1 ; ++i1){
104  double iv1 = hscan.GetXaxis()->GetBinCenter(i1) ;
105  for(unsigned i2 = 1 ; i2 < nBins + 1 ; ++i2){
106  double iv2 = hscan.GetYaxis()->GetBinCenter(i2) ;
107  p1->setCurrentFitVal(iv1) ;
108  p2->setCurrentFitVal(iv2) ;
110  double ichi2 = getVal() ;
111  double dsigma = sqrt(ichi2 - chi2) ;
112  if(dsigma <= nSigmaRange)
113  hscan.SetBinContent(i1, i2, dsigma) ;
114  else
115  hscan.SetBinContent(i1, i2, 0.) ;
116  }
117  }
118 
119  vector<string> titles(1, "Re(z_{CP})") ;
120  titles.push_back("Im(z_{CP})") ;
121  titles.push_back("Re(#Delta z)") ;
122  titles.push_back("Im(#Delta z)") ;
123  string xtitle = titles.at(ip1) ;
124  string ytitle = titles.at(ip2) ;
125  if(zeroCentre){
126  xtitle = "#delta " + xtitle ;
127  ytitle = "#delta " + ytitle ;
128  }
129  hscan.SetXTitle(xtitle.c_str()) ;
130  hscan.SetYTitle(ytitle.c_str()) ;
131  hscan.SetZTitle("N. #sigma") ;
132  hscan.SetContour(nSigmaRange) ;
133  hscan.SetStats(false) ;
134 
135  p1->setCurrentFitVal(v1) ;
136  p2->setCurrentFitVal(v2) ;
138  return hscan ;
139 }
140 
142  return m_fitPars ;
143 }
144 
146  const TimeBinning& timeBins) :
147  BinFlipChi2Base(fitPars),
148  m_hadronicPars(hadronicPars),
149  m_timeBinning(timeBins)
150 {
151  init() ;
152 }
153 
155  const string& hadronicParsName, const string& timeBinsName,
156  const string& fname) :
157  BinFlipChi2Base(fitPars),
158  m_hadronicPars(hadronicParsName, fname),
159  m_timeBinning(timeBinsName, fname)
160 {
161  init() ;
162 }
163 
166 }
167 
170  throw invalid_argument("The phase binning of the hadronic parameters and binned data don't match!") ;
171 }
172 
174  double chi2 = 0. ;
175  for(unsigned iTimeBin = 0 ; iTimeBin < m_timeBinning.nBinsTime() ; ++iTimeBin){
176  for(unsigned iPhaseBin = 1 ; iPhaseBin < m_timeBinning.nBinsPhase() + 1; ++iPhaseBin){
177  // Needs the unmixed time.
178  double Rplus = m_hadronicPars.bin(iPhaseBin).R(m_timeBinning.meanUnmixedTime(iTimeBin),
181  double Rminus = m_hadronicPars.bin(iPhaseBin).Rbar(m_timeBinning.meanUnmixedTime(iTimeBin),
184  chi2 += m_timeBinning.chiSquared(iTimeBin, iPhaseBin, Rplus, Rminus) ;
185  }
186  }
187  return chi2 ;
188 }
189 
190 deque<deque<TH1F> > BinFlipChi2::plotsVsTime(const string& name) const {
191  deque<deque<TH1F> > histos = m_timeBinning.plotsVsTime(name) ;
192  unsigned iPhaseBin = 1 ;
193  for(auto& binhistos : histos) {
194  TH1F& rminus = binhistos[2] ;
195  TH1F& rplus = binhistos[5] ;
196  TH1F& rdiff = binhistos[6] ;
197  TH1F rminusfit = TH1F(rminus) ;
198  rminusfit.SetName((string(rminus.GetName()) + "_fit").c_str()) ;
199  TH1F rplusfit = TH1F(rplus) ;
200  rplusfit.SetName((string(rplus.GetName()) + "_fit").c_str()) ;
201  TH1F rdifffit = TH1F(rdiff) ;
202  rdifffit.SetName((string(rdiff.GetName()) + "_fit").c_str()) ;
203  for(unsigned iTimeBin = 0 ; iTimeBin < m_timeBinning.nBinsTime() ; ++iTimeBin){
204  double t = m_timeBinning.meanUnmixedTime(iTimeBin) ;
205  double t2 = m_timeBinning.meanUnmixedTime2(iTimeBin) ;
206  double rplus = m_hadronicPars.bin(iPhaseBin).R(t, t2, m_timeBinning.getLifetime(), m_zcp, m_dz) ;
207  double rminus = m_hadronicPars.bin(iPhaseBin).Rbar(t, t2, m_timeBinning.getLifetime(), m_zcp, m_dz) ;
208  double rdiff = rplus-rminus ;
209  rminusfit.SetBinContent(iTimeBin+1, rminus) ;
210  rminusfit.SetBinError(iTimeBin+1, 0.) ;
211  rplusfit.SetBinContent(iTimeBin+1, rplus) ;
212  rplusfit.SetBinError(iTimeBin+1, 0.) ;
213  rdifffit.SetBinContent(iTimeBin+1, rdiff) ;
214  rdifffit.SetBinError(iTimeBin+1, 0.) ;
215  }
216  binhistos.push_back(rminusfit) ;
217  binhistos.push_back(rplusfit) ;
218  binhistos.push_back(rdifffit) ;
219  ++iPhaseBin ;
220  }
221  return histos ;
222 }
223 
224 void BinFlipChi2::savePlotsVsTime(const string& name, TFile& outputfile) const {
225  outputfile.cd() ;
226  deque<deque<TH1F> > histos = plotsVsTime(name) ;
227  for(auto& binhistos : histos)
228  for(auto& histo : binhistos)
229  histo.Write() ;
230 }
231 
232 
234  BinFlipChi2Base((BinFlipParSet*)flip1->getParSet()),
235  m_flippers(1, flip1)
236 {
237  m_flippers.push_back(flip2) ;
238 }
239 
241  BinFlipChi2Base((BinFlipParSet*)flippers.at(0)->getParSet()),
242  m_flippers(flippers)
243 {}
244 
246  double chi2 = 0. ;
247  for(auto* flipper : m_flippers)
248  chi2 += flipper->getVal() ;
249  return chi2 ;
250 }
251 
254  for(auto* flipper : m_flippers)
255  flipper->parametersChanged() ;
256 }
void fixZeroCPV()
Definition: BinFlipChi2.cpp:38
std::deque< std::deque< TH1F > > plotsVsTime(const std::string &) const
unsigned nBinsPhase() const
virtual void parametersChanged() override
Definition: BinFlipChi2.cpp:77
BinFlipParSet(double, double, double, double, double, double, double, double, unsigned long blindingSeed=0, double zBlindRange=0., double deltazBlindRange=0.)
Definition: BinFlipChi2.cpp:17
BinFlipParSet * m_fitPars
Definition: BinFlipChi2.h:45
TH2D scan2D(unsigned, unsigned, float nSigmaRange=5., unsigned nBins=300, bool zeroCentre=true, float scale=1.)
Definition: BinFlipChi2.cpp:82
std::complex< double > unblindDeltaz() const
Definition: BinFlipChi2.cpp:57
TimeBinning m_timeBinning
Definition: BinFlipChi2.h:61
BinFlippers m_flippers
Definition: BinFlipChi2.h:77
std::complex< double > m_zcp
Definition: BinFlipChi2.h:46
double blindedMean() const
const PhaseBinningBase & binning() const
Get the binning scheme.
virtual double getVal() override
IMinuitParameter * getParPtr(unsigned int i)
void savePlotsVsTime(const std::string &, TFile &) const
BinFlipChi2Base(BinFlipParSet *)
Definition: BinFlipChi2.cpp:70
std::complex< double > unblindZcp() const
Definition: BinFlipChi2.cpp:53
BinFlipChi2(BinFlipParSet *, const HadronicParameters &, const TimeBinning &)
vector< double > _blindingPars(unsigned long seed, double range, unsigned long offset)
Definition: BinFlipChi2.cpp:8
MINT::FitParameter zcp_Re
Definition: BinFlipChi2.h:24
virtual const std::string & name() const
Definition: FitParameter.h:144
static std::pair< std::complex< double >, std::complex< double > > fromXY(double, double, double, double)
Definition: BinFlipChi2.cpp:61
virtual void parametersChanged() override
MINT::FitParameter deltaz_Re
Definition: BinFlipChi2.h:26
BinFlipChi2Simul(BinFlipChi2 *, BinFlipChi2 *)
std::complex< double > deltaz() const
Definition: BinFlipChi2.cpp:49
MINT::FitParameter zcp_Im
Definition: BinFlipChi2.h:25
virtual void setCurrentFitVal(double fv)
std::complex< double > zcp() const
Definition: BinFlipChi2.cpp:45
double meanUnmixedTime(unsigned) const
virtual double getCurrentFitVal() const
double meanUnmixedTime2(unsigned) const
const Bin & bin(IDalitzEvent &) const
Get the bin for a DalitzEvent.
double err() const
void checkPhaseBinning() const
BinFlipParSet * getBinFlipParSet()
HadronicParameters::BinningPtr phaseBinning() const
double chiSquared(unsigned, unsigned, double, double) const
std::complex< double > m_dz
Definition: BinFlipChi2.h:47
double getVal()=0
double getLifetime() const
std::deque< std::deque< TH1F > > plotsVsTime(const std::string &) const
double Rbar(double, double, double, const std::complex< double > &, const std::complex< double > &) const
virtual double getVal() override
MINT::FitParameter deltaz_Im
Definition: BinFlipChi2.h:27
HadronicParameters m_hadronicPars
Definition: BinFlipChi2.h:60
std::deque< BinFlipChi2 * > BinFlippers
Definition: BinFlipChi2.h:70
unsigned nBinsTime() const
double R(double, double, double, const std::complex< double > &, const std::complex< double > &) const
Get expected ratio of events (suppressed)/(favoured) at the given time for the given mixing parameter...