CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RootMinuitCommands.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_RootMinuitCommands_h
2 #define PhysicsTools_Utilities_RootMinuitCommands_h
8 #include <map>
9 #include <string>
10 #include <cstdlib>
11 #include <fstream>
12 #include <sstream>
13 #include <iostream>
14 #include<boost/tokenizer.hpp>
15 
16 const char * kParameter = "par";
17 const char * kFix = "fix";
18 const char * kRelease = "release";
19 const char * kSet = "set";
20 const char * kMinimize = "minimize";
21 const char * kMigrad = "migrad";
22 const char * kPrintAll = "print_all";
23 
24 namespace fit {
25 
28  std::vector<std::string> stringArgs;
29  std::vector<double> doubleArgs;
30  void print(std::ostream& cout) const {
31  cout << name;
32  if(stringArgs.size() > 0) {
33  for(size_t i = 0; i != stringArgs.size(); ++i) {
34  if(i != 0) cout << ",";
35  cout << " \"" << stringArgs[i] << "\"";
36  }
37  }
38  if(doubleArgs.size() > 0) {
39  for(size_t i = 0; i != doubleArgs.size(); ++i) {
40  if(i != 0) cout << ",";
41  cout << " " << doubleArgs[i];
42  }
43  }
44  }
45  };
46 
47  template<class Function>
49  public:
52  RootMinuitCommands(bool verbose = true) :
53  verbose_(verbose) {
54  }
55  RootMinuitCommands(const char * fileName, bool verbose = true) :
56  verbose_(verbose) {
57  init(fileName);
58  }
59  void init(const char * fileName);
60  double par(const std::string& name) {
61  return parameter(name).val;
62  }
63  double err(const std::string& name) {
64  return parameter(name).err;
65  }
66  double min(const std::string& name) {
67  return parameter(name).min;
68  }
69  double max(const std::string& name) {
70  return parameter(name).max;
71  }
72  bool fixed(const std::string& name) {
73  return parameter(name).fixed;
74  }
76  const std::string & name = p.name();
77  const parameter_t & par = parameter(name);
78  minuit.addParameter(p, par.err, par.min, par.max);
79  if(par.fixed) minuit.fixParameter(name);
80  }
81  void run(RootMinuit<Function>& minuit) const;
82 
83  private:
84  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
85  bool verbose_;
86  unsigned int lineNumber_;
88  std::map<std::string, size_t> parIndices_;
89  std::vector<command> commands_;
90  double string2double(const std::string & str) const {
91  const char * begin = str.c_str();
92  char * end;
93  double val = strtod(begin, &end);
94  size_t s = end - begin;
95  if(s < str.size()) {
97  << "RootMinuitCommands: invalid double value: "
98  << str << "\n";
99  }
100  return val;
101  }
102  const parameter_t & parameter(const std::string& name) const {
103  typename std::map<std::string, size_t>::const_iterator p = parIndices_.find(name);
104  if(p == parIndices_.end())
106  << "RootMinuit: can't find parameter " << name << "\n";
107  return pars_[p->second].second;
108  }
110  std::ostringstream out;
111  out << "RootMinuitCommands config. error, line " << lineNumber_<< ": ";
112  return out.str();
113  }
114  std::string nextToken(typename tokenizer::iterator & i,
115  const typename tokenizer::iterator & end) const {
116  ++i;
117  if(i == end)
119  << errorHeader() << "missing parameter\n";
120  return *i;
121  }
122  };
123 
124  template<typename Function>
126  using namespace std;
127  string cmssw_release_base = getenv("CMSSW_RELEASE_BASE");
128  string cmssw_base = getenv("CMSSW_BASE");
129  string path = ".";
130  if(!cmssw_release_base.empty()) {
131  path += ':';
132  path += (cmssw_release_base + "/src");
133  }
134  if(!cmssw_base.empty()) {
135  path += ':';
136  path += (cmssw_base + "/src");
137  }
138  FileInPath fileInPath(path, fileName);
139  std::ifstream * file = fileInPath();
140  if(file==0 || !file->is_open())
142  << "RootMinuitCommands: can't open file: " << fileName
143  << " in path: " << path << "\n";
144  if (verbose_)
145  cout << ">>> configuration file: " << fileName << endl;
146  string line;
147  lineNumber_ = 0;
148  bool commands = false;
149  while(getline(*file, line)) {
150  ++lineNumber_;
151  if(line.size()==0) continue;
152  char last = *line.rbegin();
153  if(!(last >= '0' && last <= 'z')) line.erase(line.end() - 1);
154  boost::char_separator<char> sep(" ");
155  tokenizer tokens(line, sep);
156  tokenizer::iterator i = tokens.begin(), e = tokens.end();
157  if(tokens.begin()==tokens.end()) continue;
158  if(*(i->begin()) != '#') {
159  if(*i == kParameter) {
160  if(commands)
162  << errorHeader()
163  << "please, declare all parameter before all other minuit commands.\n";
164  string name = nextToken(i, e);
165  parameter_t par;
166  par.val = string2double(nextToken(i, e));
167  par.err = string2double(nextToken(i, e));
168  par.min = string2double(nextToken(i, e));
169  par.max = string2double(nextToken(i, e));
170  tokenizer::iterator j = i; ++j;
171  if(j != e) {
172  string fixed = nextToken(i, e);
173  if(fixed == "fixed")
174  par.fixed = true;
175  else if(fixed == "free")
176  par.fixed = false;
177  else
179  << errorHeader()
180  << "fix parameter option unknown: " << *i << "\n"
181  << "valid options are: fixed, free.\n";
182  } else {
183  par.fixed = false;
184  }
185  pars_.push_back(std::make_pair(name, par));
186  size_t s = parIndices_.size();
187  parIndices_[name] = s;
188  if(verbose_)
189  cout << ">>> " << kParameter << " " << name
190  << " " << par.val
191  << " [" << par.min << ", " << par.max << "],"
192  << " err: " << par.err
193  << endl;
194  } else if(*i == kFix || *i == kRelease) {
195  commands = true;
196  command com;
197  com.name = *i;
198  string arg = nextToken(i, e);
199  com.stringArgs.push_back(arg);
200  commands_.push_back(com);
201  if(verbose_) {
202  cout << ">>> "; com.print(cout); cout << endl;
203  }
204  } else if(*i == kSet) {
205  commands = true;
206  command com;
207  com.name = *i;
208  string arg = nextToken(i, e);
209  com.stringArgs.push_back(arg);
210  com.doubleArgs.push_back(string2double(nextToken(i, e)));
211  commands_.push_back(com);
212  if(verbose_) {
213  cout << ">>> "; com.print(cout); cout << endl;
214  }
215  } else if(*i == kMinimize || *i == kMigrad || *i == kPrintAll) {
216  commands = true;
217  command com;
218  com.name = *i;
219  commands_.push_back(com);
220  if(verbose_) {
221  cout << ">>> "; com.print(cout); cout << endl;
222  }
223  } else {
225  << errorHeader()
226  << "unkonwn command:: " << *i << "\n";
227 
228  }
229  }
230  }
231  if (verbose_)
232  cout << ">>> end configuration" << endl;
233  }
234 
235  template<typename Function>
237  using namespace std;
238  typename vector<command>::const_iterator c = commands_.begin(), end = commands_.end();
239  for(; c != end; ++c) {
240  if(verbose_) {
241  cout << ">>> minuit command: ";
242  c->print(cout);
243  cout << endl;
244  }
245  if(c->name == kMinimize)
246  minuit.minimize();
247  else if(c->name == kMigrad)
248  minuit.migrad();
249  else if(c->name == kPrintAll)
250  minuit.printFitResults();
251  else if(c->name == kFix)
252  minuit.fixParameter(c->stringArgs[0]);
253  else if(c->name == kRelease)
254  minuit.releaseParameter(c->stringArgs[0]);
255  else if(c->name == kSet)
256  minuit.setParameter(c->stringArgs[0], c->doubleArgs[0]);
257  }
258  }
259 
260 }
261 
262 #endif
double min(const std::string &name)
int i
Definition: DBlmapReader.cc:9
void init(const char *fileName)
std::string errorHeader() const
const std::string & name() const
Definition: Parameter.h:13
boost::tokenizer< boost::char_separator< char > > tokenizer
std::vector< std::string > stringArgs
void printFitResults(std::ostream &cout=std::cout)
Definition: RootMinuit.h:175
void setParameter(const std::string &name, double val)
Definition: RootMinuit.h:101
double migrad()
Definition: RootMinuit.h:146
void fixParameter(const std::string &name)
Definition: RootMinuit.h:87
double par(const std::string &name)
void run(RootMinuit< Function > &minuit) const
std::vector< command > commands_
std::vector< double > doubleArgs
A arg
Definition: Factorize.h:36
std::string nextToken(typename tokenizer::iterator &i, const typename tokenizer::iterator &end) const
bool fixed(const std::string &name)
void addParameter(const std::string &name, boost::shared_ptr< double > val, double err, double min, double max)
Definition: RootMinuit.h:27
const char * kSet
const char * kParameter
std::vector< std::pair< std::string, parameter_t > > parameterVector_t
Definition: ParameterMap.h:14
const char * kFix
tuple path
else: Piece not in the list, fine.
double minimize()
Definition: RootMinuit.h:132
void print(std::ostream &cout) const
int j
Definition: DBlmapReader.cc:9
const char * kPrintAll
#define end
Definition: vmac.h:37
double string2double(const std::string &str) const
tuple out
Definition: dbtoconf.py:99
std::map< std::string, size_t > parIndices_
double max(const std::string &name)
void add(RootMinuit< Function > &minuit, funct::Parameter &p) const
const parameter_t & parameter(const std::string &name) const
double err(const std::string &name)
const char * kMigrad
#define begin
Definition: vmac.h:30
list cmssw_release_base
Definition: cmsBenchmark.py:61
const char * kRelease
RootMinuitCommand command
tuple cout
Definition: gather_cfg.py:121
RootMinuit< Function > minuit
const char * kMinimize
void releaseParameter(const std::string &name)
Definition: RootMinuit.h:94
RootMinuitCommands(const char *fileName, bool verbose=true)
RootMinuitCommands(bool verbose=true)