laura is hosted by Hepforge, IPPP Durham
Laura++  v2r0
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauFitNtuple.cc
Go to the documentation of this file.
1 
2 // Copyright University of Warwick 2004 - 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 // Authors:
7 // Thomas Latham
8 // John Back
9 // Paul Harrison
10 
15 #include <iostream>
16 using std::cout;
17 using std::cerr;
18 using std::endl;
19 
20 #include "TFile.h"
21 #include "TMath.h"
22 #include "TMatrixD.h"
23 #include "TSystem.h"
24 #include "TTree.h"
25 
26 #include "LauAbsFitter.hh"
27 #include "LauFitNtuple.hh"
28 #include "LauFitter.hh"
29 #include "LauParamFixed.hh"
30 
31 ClassImp(LauFitNtuple)
32 
33 
34 LauFitNtuple::LauFitNtuple(const TString& fileName, Bool_t storeAsymErrors) :
35  rootFileName_(fileName),
36  rootFile_(0),
37  fitResults_(0),
38  definedFitTree_(kFALSE),
39  storeAsymErrors_(storeAsymErrors),
40  fitStatus_(0),
41  nFitPars_(0),
42  nFreePars_(0),
43  nExtraPars_(0),
44  NLL_(0.0),
45  iExpt_(0)
46 {
47  rootFile_ = TFile::Open(rootFileName_, "recreate");
48  rootFile_->cd();
49  fitResults_ = new TTree("fitResults", "fitResults");
50  fitResults_->SetDirectory(rootFile_);
51 
52  fitVars_.clear(); extraVars_.clear();
53 }
54 
56 {
57  if (rootFile_ && rootFile_->IsOpen()) {
58  delete fitResults_; fitResults_ = 0;
59  }
60  delete rootFile_; rootFile_ = 0;
61 }
62 
63 void LauFitNtuple::storeCorrMatrix( UInt_t iExpt, Double_t NLL, Int_t fitStatus, const TMatrixD& covMatrix )
64 {
65  // store the minimised NLL value, correlation matrix status and experiment number
66  iExpt_ = iExpt;
67  NLL_ = NLL;
68  fitStatus_ = fitStatus;
69 
70  // make the correlation matrix the correct dimensions
71  if (definedFitTree_ == kFALSE) {
72  corrMatrix_.clear();
73  if ( covMatrix.GetNoElements() != 0 ) {
74  corrMatrix_.resize(nFitPars_);
75  for (UInt_t i = 0; i < nFitPars_; ++i) {corrMatrix_[i].resize(nFitPars_);}
76  }
77  }
78 
79  if ( covMatrix.GetNoElements() == 0 ) {
80  return;
81  }
82 
83  // calculate the correlation matrix information from the fitter covariance matrix
84  Bool_t iFixed(kFALSE);
85  Bool_t jFixed(kFALSE);
86  UInt_t iFree(0);
87  UInt_t jFree(0);
88 
89  for (UInt_t i = 0; i < nFitPars_; ++i) {
90 
91  iFixed = fitVars_[i]->fixed();
92 
93  // reset the "j" free parameter counter
94  jFree = 0;
95 
96  // NB the supplied covariance matrix is of dimension nFreePars_ x nFreePars_
97  for (UInt_t j = 0; j < nFitPars_; ++j) {
98 
99  jFixed = fitVars_[j]->fixed();
100 
101  if (i == j) {
102  corrMatrix_[i][j] = 1.0;
103  } else if (iFixed == kTRUE || jFixed == kTRUE) {
104  corrMatrix_[i][j] = 0.0;
105  } else {
106  Double_t r_ij = covMatrix(iFree,jFree);
107  Double_t r_ii = covMatrix(iFree,iFree);
108  Double_t r_jj = covMatrix(jFree,jFree);
109  Double_t denom = r_ii * r_jj;
110  if (denom < 0.0) {
111  r_ij = 0.0;
112  denom = 1.0;
113  }
114  denom = TMath::Sqrt(denom);
115  if (denom < 1e-30) {
116  r_ij = 0.0;
117  denom = 1.0;
118  }
119  corrMatrix_[i][j] = r_ij / denom;
120  }
121 
122  if (!jFixed) {
123  ++jFree;
124  }
125  }
126 
127  if (!iFixed) {
128  ++iFree;
129  }
130  }
131 }
132 
133 void LauFitNtuple::storeParsAndErrors(const std::vector<LauParameter*>& fitVars, const std::vector<LauParameter>& extraVars)
134 {
135  fitVars_ = fitVars;
136  UInt_t nFitPars = fitVars_.size();
137 
138  // the number of parameters being given to us should be the same as the number from the last fit
139  // OR it's the first time so the "last" number is zero
140  if (nFitPars_ != 0 && nFitPars_ != nFitPars) {
141  cerr << "ERROR in LauFitNtuple::storeParsAndErrors : expected total number of parameters (" << nFitPars_
142  << ") not the same as the number provided (" << nFitPars << ")." << endl;
143  gSystem->Exit(EXIT_FAILURE);
144  }
145 
146  LauParamFixed pred;
147  UInt_t nFreePars = nFitPars - std::count_if(fitVars_.begin(),fitVars_.end(),pred);
148 
149  // the number of free parameters being given to us should be the same as the number from the last fit
150  // OR it's the first time so the "last" number is zero
151  // (NB we check whether nFitPars_ is zero for this since it is possible to have zero free parameters, albeit rather daft)
152  if (nFitPars_ != 0 && nFreePars_ != nFreePars) {
153  cerr << "ERROR in LauFitNtuple::storeParsAndErrors : expected number of free parameters (" << nFreePars_
154  << ") not the same as the number provided (" << nFreePars << ")." << endl;
155  gSystem->Exit(EXIT_FAILURE);
156  }
157 
158  nFitPars_ = nFitPars;
159  nFreePars_ = nFreePars;
160 
161  extraVars_ = extraVars;
162  nExtraPars_ = extraVars_.size();
163 }
164 
166 {
167  // Now create and fill the stored fit results into an ntuple (TTree)
168  if (definedFitTree_ == kFALSE) {
169 
170  cout << "INFO in LauFitNtuple::updateFitNtuple : totNoPars = " << nFitPars_ << endl;
171 
172  // Add experiment number as a branch
173  fitResults_->Branch("iExpt", &iExpt_, "iExpt/I");
174  fitResults_->Branch("fitStatus", &fitStatus_, "fitStatus/I");
175 
176  // Add NLL (negative log-likelihood) value from fit
177  fitResults_->Branch("NLL", &NLL_, "NLL/D");
178 
179  for (UInt_t i = 0; i < nFitPars_; i++) {
180 
181  TString parName = fitVars_[i]->name();
182  TString parNameD(parName); parNameD += "/D";
183  fitResults_->Branch(parName.Data(), &fitVars_[i]->value_, parNameD.Data());
184 
185  TString parInitName(parName); parInitName += "_True";
186  TString parInitNameD(parInitName); parInitNameD += "/D";
187  fitResults_->Branch(parInitName.Data(), &fitVars_[i]->genValue_, parInitNameD.Data());
188 
189  if (!fitVars_[i]->fixed()) {
190  TString parErrName(parName); parErrName += "_Error";
191  TString parErrNameD(parErrName); parErrNameD += "/D";
192  fitResults_->Branch(parErrName.Data(), &fitVars_[i]->error_, parErrNameD.Data());
193 
194  if ( storeAsymErrors_ ) {
195  TString parNegErrName(parName); parNegErrName += "_NegError";
196  TString parNegErrNameD(parNegErrName); parNegErrNameD += "/D";
197  fitResults_->Branch(parNegErrName.Data(), &fitVars_[i]->negError_, parNegErrNameD.Data());
198 
199  TString parPosErrName(parName); parPosErrName += "_PosError";
200  TString parPosErrNameD(parPosErrName); parPosErrNameD += "/D";
201  fitResults_->Branch(parPosErrName.Data(), &fitVars_[i]->posError_, parPosErrNameD.Data());
202  }
203 
204  TString parPullName(parName); parPullName += "_Pull";
205  TString parPullNameD(parPullName); parPullNameD += "/D";
206  fitResults_->Branch(parPullName.Data(), &fitVars_[i]->pull_, parPullNameD.Data());
207  }
208 
209  // Now add in the correlation matrix values (only for floating parameters)
210  if (!fitVars_[i]->fixed()) {
211  // First the global correlation coeffs
212  TString parGCCName(parName); parGCCName += "_GCC";
213  TString parGCCNameD(parGCCName); parGCCNameD += "/D";
214  fitResults_->Branch(parGCCName.Data(), &fitVars_[i]->gcc_, parGCCNameD.Data());
215 
216  if ( ! corrMatrix_.empty() ) {
217  // Then the rest
218  for (UInt_t j = 0; j < nFitPars_; j++) {
219  if (!fitVars_[j]->fixed() && i!=j) {
220  TString parName2 = fitVars_[j]->name();
221  TString corrName("corr__");
222  corrName += parName; corrName += "__"; corrName += parName2;
223 
224  TString corrNameD(corrName); corrNameD += "/D";
225  fitResults_->Branch(corrName.Data(), &corrMatrix_[i][j], corrNameD.Data());
226  }
227  }
228  }
229  }
230  }
231 
232  // Update extra parameter values...
233  for (UInt_t i = 0; i < nExtraPars_; i++) {
234 
235  TString parName = extraVars_[i].name();
236  TString parNameD(parName); parNameD += "/D";
237  fitResults_->Branch(parName.Data(), &extraVars_[i].value_, parNameD.Data());
238 
239  TString parInitName(parName); parInitName += "_True";
240  TString parInitNameD(parInitName); parInitNameD += "/D";
241  fitResults_->Branch(parInitName.Data(), &extraVars_[i].genValue_, parInitNameD.Data());
242 
243  //TString parErrName(parName); parErrName += "_Error";
244  //TString parErrNameD(parErrName); parErrNameD += "/D";
245  //fitResults_->Branch(parErrName.Data(), &extraVars_[i].error_, parErrNameD.Data());
246 
247  // Also find the fit fraction pull and store it
248  //TString pullName(parName); pullName += "_Pull";
249  //TString pullNameD(pullName); pullNameD += "/D";
250  //fitResults_->Branch(pullName.Data(), &extraVars_[i].pull_, pullNameD.Data());
251 
252  }
253 
254  definedFitTree_ = kTRUE;
255 
256  }
257 
258  cout << "NLL = " << NLL_ << endl;
259 
260  fitResults_->Fill();
261 }
262 
264 {
265  // Write out the fit ntuple to the appropriate root file
266  rootFile_->cd();
267  rootFile_->Write();
268  rootFile_->Close();
269  delete rootFile_; rootFile_ = 0;
270 }
271 
Bool_t definedFitTree_
Flags whether the fit tree has been defined.
Definition: LauFitNtuple.hh:94
std::vector< LauParameter * > fitVars_
Fit variables.
Definition: LauFitNtuple.hh:86
Bool_t fixed() const
Check whether the parameter is fixed or floated.
File containing declaration of LauParamFixed class.
Int_t iExpt_
Experiment number.
UInt_t nExtraPars_
Number of extra parameters.
File containing declaration of LauAbsFitter class.
std::vector< std::vector< Double_t > > corrMatrix_
Correlation matrix.
Definition: LauFitNtuple.hh:91
std::vector< LauParameter > extraVars_
Extra variables.
Definition: LauFitNtuple.hh:88
void updateFitNtuple()
Update the fit ntuple.
TFile * rootFile_
Root file.
Definition: LauFitNtuple.hh:81
File containing LauFitter namespace.
UInt_t nFitPars_
Number of fit parameters.
void writeOutFitResults()
Write out fit results.
Predicate to allow counting of the number of fixed parameters.
void storeCorrMatrix(UInt_t iExpt, Double_t NLL, Int_t fitStatus, const TMatrixD &covMatrix)
Store the correlation matrix and other fit information.
Definition: LauFitNtuple.cc:63
Double_t NLL_
Minimised negative log likelihood.
virtual ~LauFitNtuple()
Destructor.
Definition: LauFitNtuple.cc:55
void storeParsAndErrors(const std::vector< LauParameter * > &fitVars, const std::vector< LauParameter > &extraVars)
Store parameters and their errors.
TTree * fitResults_
Fit results.
Definition: LauFitNtuple.hh:83
Class to store the results from the fit into an ntuple.
Definition: LauFitNtuple.hh:42
Int_t fitStatus_
Status of fit.
Definition: LauFitNtuple.hh:99
UInt_t nFreePars_
Number of free parameters.
File containing declaration of LauFitNtuple class.
Bool_t storeAsymErrors_
Flags whether or not to store the asymmetric error information.
Definition: LauFitNtuple.hh:96