MINT2
MinuitParameterSet.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:55 GMT
4 
5 #include "TFile.h"
6 #include "TNtupleD.h"
7 #include "Mint/Utils.h"
8 #include "Mint/FitParameter.h"
9 
10 #include <algorithm>
11 #include <iostream>
12 
13 #include "TDirectory.h"
14 
15 using namespace std;
16 using namespace MINT;
17 
18 //const char MinuitParameterSet::prtNameChars[] = { '+', '-', '*', '>', ',', '(', ')', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0' };
19 //const char MinuitParameterSet::ntpNameChars[] = { 'p', 'm', 's', '_', '_', '_', '_', 'a', 'b', 'c', 'c', 'e', 'f', 'g', 'h', 'i', 'j', '\0' };
20 
21 const char MinuitParameterSet::prtNameChars[] = { '+', '-', '*', '>', ',', '(', ')', '[', ']', '\0'};
22 const char MinuitParameterSet::ntpNameChars[] = { 'p', 'm', 's', '_', '_', '_', '_', '_', '_', '\0'};
23 
24 MinuitParameterSet* MinuitParameterSet::_defaultMinuitParameterSet=0;
25 MinuitParameterSet* MinuitParameterSet::getDefaultSet(){
26  if(0 == _defaultMinuitParameterSet){
27  _defaultMinuitParameterSet = new MinuitParameterSet();
28  }
29  return _defaultMinuitParameterSet;
30 }
31 
32 MinuitParameterSet::MinuitParameterSet(){}
33 MinuitParameterSet::MinuitParameterSet(const MinuitParameterSet& other)
34  : _parPtrList(other._parPtrList)
35 {}
36 
38  MinuitParameterSet floating;
39  for(unsigned int i=0; i< size(); i++){
40  if(0 != getParPtr(i)){
41  floating.add(getParPtr(i));
42  }
43  }
44  return floating;
45 }
46 
48  bool dbThis=false;
49  bool success=true;
50  if(0 == parPtr) return false;
51  if(dbThis){
52  cout << "MinuitParameterSet::add "
53  << " adding parPtr " << parPtr->name() << endl;
54  }
55  if(find(_parPtrList.begin(), _parPtrList.end(), parPtr) != _parPtrList.end()){
56  if(dbThis){
57  cout << "MinuitParameterSet::add "
58  << " already have parPtr " << parPtr->name() << endl;
59  }
60  return true ;
61  }
62  _parPtrList.push_back(parPtr);
63  success &= parPtr->setParSet(this);
64  success &= parPtr->setParSetIndex(_parPtrList.size()-1);
65  return success;
66 }
68  return addToEnd(parPtr);
69 }
70 
72 
73  if(_parPtrList.end() == find(_parPtrList.begin(), _parPtrList.end(), parPtr)){
74  cout << "WARNING in MinuitParameterSet::unregister"
75  << " parPtr you want to unregister is not part of this list!"
76  << endl;
77  return false;
78  }
79 
80  parPtr->setParSetIndex(-9999);
81  _parPtrList.erase(remove(_parPtrList.begin(), _parPtrList.end(), parPtr)
82  , _parPtrList.end()
83  );
84  setAllIndices();
85  return true;
86 }
87 
89  bool success=true;
90  for(unsigned int i=0; i < _parPtrList.size(); i++){
91  success &= _parPtrList[i]->setParSetIndex(i);
92  }
93  return success;
94 }
95 
96 unsigned int MinuitParameterSet::size()const{
97  return _parPtrList.size();
98 }
99 
101  if(i >= _parPtrList.size()) return 0;
102  return _parPtrList[i];
103 }
104 
105 const IMinuitParameter* MinuitParameterSet::getParPtr(unsigned int i) const{
106  if(i >= _parPtrList.size()) return 0;
107  return _parPtrList[i];
108 }
109 
111  for(std::vector<IMinuitParameter*>::iterator it = _parPtrList.begin();
112  it != _parPtrList.end(); it++){
113  delete (*it);
114  }
115  _parPtrList.clear();
116 }
117 
119  _parPtrList.clear();
120 }
121 
122 TNtupleD* MinuitParameterSet::makeNewNtpForOwner(TFile*& ntpFile) const{
123  bool dbThis=false;
124  if(dbThis){
125  cout << "Hello from MinuitParameterSet::makeNewNtpForOwner" << endl;
126  }
127  gDirectory->cd();
128  if(0 == ntpFile) ntpFile = new TFile("MinuitParameterSetFile.root"
129  , "RECREATE");
130  ntpFile->cd();
131  TNtupleD* ntp = new TNtupleD("MinuitParameterSetNtp"
132  , "MinuitParameterSetNtp"
133  , ntpNames().c_str());
134  return ntp;
135 }
136 
137 std::string MinuitParameterSet::prtToNtpName(const std::string& s_in){
138  std::string s(s_in);
139  for(unsigned int i=1; i < s.size(); i++){
140  // cout << "... " << s[i-1] << s[i];
141  if(s[i-1] == '-' && s[i] == '>'){
142  s[i-1] = 't'; s[i] = 'o';
143  }
144  }
145 
146  for(int i=0; prtNameChars[i] != '\0'; i++){
147  replace(s.begin(), s.end(), prtNameChars[i], ntpNameChars[i]);
148  }
149  return s;
150 }
151 
152 std::string MinuitParameterSet::ntpNames() const{
153  bool dbThis=false;
154  std::string str="";
155  int n=0;
156  for(unsigned int i=0; i < size(); i++){
157  if(0 == getParPtr(i)) continue;
158  if(0 != getParPtr(i)->iFixInit()) continue;
159  std::string name = //"p" + anythingToString(i) + "_" +
160  prtToNtpName(getParPtr(i)->name());
161  name = ((string) (TString(name).ReplaceAll("GS","")));
162  name = ((string) (TString(name).ReplaceAll("Lass","")));
163  name = ((string) (TString(name).ReplaceAll("RhoOmega","")));
164  if(str != "") str += ":";
165  str += (name + "_mean" + ":"); n++;
166  str += (name + "_init" + ":"); n++;
167  str += (name + "_err:"); n++;
168  str += (name + "_pull"); n++;
169  }
170  if(dbThis){
171  cout << "MinuitParameterSet::ntpNames():"
172  << " made " << n << " names " << str << endl;
173  cout << " this becomes: prtToNtpName(str) "
174  << prtToNtpName(str) << endl;
175  }
176  //return str;
177  return prtToNtpName(str);
178 }
179 void MinuitParameterSet::fillNtp(TFile*& ntpFile, TNtupleD*& ntp) const{
180  bool dbThis=false;
181  if(dbThis){
182  cout << "hello from MinuitParameterSet::fillNtp " << endl;
183  cout << " you called me with: " << ntp << endl;
184  }
185 
186  if(0 == ntp) ntp = makeNewNtpForOwner(ntpFile);
187  if(dbThis) cout << "got an ntuple ptr: " << ntp << endl;
188  Double_t* array = new Double_t[size()*4];
189  if(dbThis) cout << "made array" << endl;
190  int n=0;
191  for(unsigned int i=0; i < size(); i++){
192  if(0 == getParPtr(i)) continue;
193  if(0 != getParPtr(i)->iFixInit()) continue;
194  array[n++] = ((FitParameter*)getParPtr(i))->blindedMean();
195  array[n++] = getParPtr(i)->meanInit();
196  array[n++] = getParPtr(i)->err();
197  Double_t pull=-9999.0;
198  if(getParPtr(i)->err() > 0){
199  pull = (((FitParameter*)getParPtr(i))->blindedMean() - getParPtr(i)->meanInit())/getParPtr(i)->err();
200  }
201  array[n++] = pull;
202  }
203  if(dbThis){
204  cout << "checking how ntuple behaves with GetEntries()" << endl;
205  cout << " ntp->GetEntries() = " << ntp->GetEntries() << endl;
206  }
207  if(dbThis)cout << "filled the array, now putting this into the ntp" << endl;
208  ntp->Fill(array);
209  if(dbThis)cout << "filled into the ntuple, now deleting array" << endl;
210  delete[] array;
211  if(dbThis)cout << "done. returning" << endl;
212  return;
213 }
214 
215 
216 void MinuitParameterSet::print(std::ostream& os) const{
217  for(unsigned int i=0; i < size(); i++){
218  if(0 == getParPtr(i)) continue;
219  os << '\n';
220  getParPtr(i)->print(os);
221  }
222 }
223 void MinuitParameterSet::printVariable(std::ostream& os) const{
224  for(unsigned int i=0; i < size(); i++){
225  if(0 == getParPtr(i)) continue;
226  if(getParPtr(i)->iFixInit() == 1) continue;
227  os << '\n';
228  getParPtr(i)->print(os);
229  }
230 }
231 void MinuitParameterSet::printResultVsInput(std::ostream& os) const{
232  os << "\n ======== MinuitParameterSet::(FitResult - StartValue)/error ========\n";
233  for(unsigned int i=0; i < this->size(); i++){
234  if(0 == getParPtr(i)) continue;
235  if(getParPtr(i)->iFixInit() == 1) continue;
236  os << '\n';
238  }
239  os << "\n ===================================================================="
240  << endl;
241 }
242 
244  if(covMatrix.GetNcols() != size() || covMatrix.GetNrows() != size()){
245  cout << "WARNING: MiniutParameterSet::setCovMatrix: given matrix has the wrong size!" << endl ;
246  cout << "N. parameters: " << size() << ", matrix is " << covMatrix.GetNcols() << "x"
247  << covMatrix.GetNrows() << endl ;
248  return false ;
249  }
250  _covMatrix.ResizeTo(covMatrix) ;
252  return true ;
253 }
254 
256  return _covMatrix ;
257 }
258 
259 //
static const char prtNameChars[]
virtual double meanInit() const =0
virtual void printResultVsInput(std::ostream &os=std::cout) const =0
virtual bool setParSetIndex(int psetIndex)=0
std::vector< IMinuitParameter * > _parPtrList
void fillNtp(TFile *&ntpFile, TNtupleD *&ntp) const
bool addToEnd(IMinuitParameter *parPtr)
virtual int iFixInit() const =0
static const double s
bool setCovMatrix(const CovMatrix &)
Set the covariance matrix.
IMinuitParameter * getParPtr(unsigned int i)
unsigned int size() const
virtual double err() const =0
virtual const std::string & name() const =0
bool unregister(IMinuitParameter *patPtr)
TMatrixTSym< double > CovMatrix
void printResultVsInput(std::ostream &os=std::cout) const
TNtupleD * makeNewNtpForOwner(TFile *&ntpFile) const
bool add(IMinuitParameter *parPtr)
static std::string prtToNtpName(const std::string &s_in)
virtual bool setParSet(MinuitParameterSet *ps)=0
const CovMatrix & covMatrix() const
Get the covariance matrix.
virtual void print(std::ostream &os=std::cout) const =0
void print(std::ostream &os=std::cout) const
void printVariable(std::ostream &os=std::cout) const
static const char ntpNameChars[]
MinuitParameterSet getFloating()
std::string ntpNames() const