CMS 3D CMS Logo

RootMinuitCommands.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_RootMinuitCommands_h
2 #define PhysicsTools_Utilities_RootMinuitCommands_h
7 #include <map>
8 #include <string>
9 #include <cstdlib>
10 #include <fstream>
11 #include <sstream>
12 #include <iostream>
13 #include<boost/tokenizer.hpp>
14 #include <boost/algorithm/string/join.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  }
75  void add(RootMinuit<Function>& minuit, funct::Parameter& p) const {
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  vector<string> directories;
130  directories.reserve(3);
131  directories.emplace_back(".");
132  if(!cmssw_release_base.empty()) {
133  directories.emplace_back(cmssw_release_base + "/src");
134  }
135  if(!cmssw_base.empty()) {
136  directories.emplace_back(cmssw_base + "/src");
137  }
138  ifstream file;
139  for(auto const& d: directories) {
140  std::ifstream f{ d+"/"+fileName };
141  if(f.good()) {
142  file = std::move(f);
143  break;
144  }
145  }
146  if(!file.is_open()) {
148  << "RootMinuitCommands: can't open file: " << fileName
149  << " in path: " << boost::algorithm::join(directories,":") << "\n";
150  }
151  if (verbose_)
152  cout << ">>> configuration file: " << fileName << endl;
153  string line;
154  lineNumber_ = 0;
155  bool commands = false;
156  while(getline(file, line)) {
157  ++lineNumber_;
158  if(line.size()==0) continue;
159  char last = *line.rbegin();
160  if(!(last >= '0' && last <= 'z')) line.erase(line.end() - 1);
161  boost::char_separator<char> sep(" ");
162  tokenizer tokens(line, sep);
163  tokenizer::iterator i = tokens.begin(), e = tokens.end();
164  if(tokens.begin()==tokens.end()) continue;
165  if(*(i->begin()) != '#') {
166  if(*i == kParameter) {
167  if(commands)
169  << errorHeader()
170  << "please, declare all parameter before all other minuit commands.\n";
171  string name = nextToken(i, e);
172  parameter_t par;
173  par.val = string2double(nextToken(i, e));
174  par.err = string2double(nextToken(i, e));
175  par.min = string2double(nextToken(i, e));
176  par.max = string2double(nextToken(i, e));
177  tokenizer::iterator j = i; ++j;
178  if(j != e) {
179  string fixed = nextToken(i, e);
180  if(fixed == "fixed")
181  par.fixed = true;
182  else if(fixed == "free")
183  par.fixed = false;
184  else
186  << errorHeader()
187  << "fix parameter option unknown: " << *i << "\n"
188  << "valid options are: fixed, free.\n";
189  } else {
190  par.fixed = false;
191  }
192  pars_.push_back(std::make_pair(name, par));
193  size_t s = parIndices_.size();
194  parIndices_[name] = s;
195  if(verbose_)
196  cout << ">>> " << kParameter << " " << name
197  << " " << par.val
198  << " [" << par.min << ", " << par.max << "],"
199  << " err: " << par.err
200  << endl;
201  } else if(*i == kFix || *i == kRelease) {
202  commands = true;
203  command com;
204  com.name = *i;
205  string arg = nextToken(i, e);
206  com.stringArgs.push_back(arg);
207  commands_.push_back(com);
208  if(verbose_) {
209  cout << ">>> "; com.print(cout); cout << endl;
210  }
211  } else if(*i == kSet) {
212  commands = true;
213  command com;
214  com.name = *i;
215  string arg = nextToken(i, e);
216  com.stringArgs.push_back(arg);
217  com.doubleArgs.push_back(string2double(nextToken(i, e)));
218  commands_.push_back(com);
219  if(verbose_) {
220  cout << ">>> "; com.print(cout); cout << endl;
221  }
222  } else if(*i == kMinimize || *i == kMigrad || *i == kPrintAll) {
223  commands = true;
224  command com;
225  com.name = *i;
226  commands_.push_back(com);
227  if(verbose_) {
228  cout << ">>> "; com.print(cout); cout << endl;
229  }
230  } else {
232  << errorHeader()
233  << "unkonwn command:: " << *i << "\n";
234 
235  }
236  }
237  }
238  if (verbose_)
239  cout << ">>> end configuration" << endl;
240  }
241 
242  template<typename Function>
244  using namespace std;
245  typename vector<command>::const_iterator c = commands_.begin(), end = commands_.end();
246  for(; c != end; ++c) {
247  if(verbose_) {
248  cout << ">>> minuit command: ";
249  c->print(cout);
250  cout << endl;
251  }
252  if(c->name == kMinimize)
253  minuit.minimize();
254  else if(c->name == kMigrad)
255  minuit.migrad();
256  else if(c->name == kPrintAll)
257  minuit.printFitResults();
258  else if(c->name == kFix)
259  minuit.fixParameter(c->stringArgs[0]);
260  else if(c->name == kRelease)
261  minuit.releaseParameter(c->stringArgs[0]);
262  else if(c->name == kSet)
263  minuit.setParameter(c->stringArgs[0], c->doubleArgs[0]);
264  }
265  }
266 
267 }
268 
269 #endif
double min(const std::string &name)
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
int init
Definition: HydjetWrapper.h:67
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:37
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
double minimize()
Definition: RootMinuit.h:132
commands
min number of hits for refit layers to remove
void print(std::ostream &cout) const
double f[11][100]
const char * kPrintAll
#define end
Definition: vmac.h:39
double string2double(const std::string &str) const
std::map< std::string, size_t > parIndices_
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
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:32
const char * kRelease
RootMinuitCommand command
RootMinuit< Function > minuit
const char * kMinimize
void releaseParameter(const std::string &name)
Definition: RootMinuit.h:94
#define str(s)
RootMinuitCommands(const char *fileName, bool verbose=true)
RootMinuitCommands(bool verbose=true)
def move(src, dest)
Definition: eostools.py:511