1 #ifndef PhysicsTools_Utilities_RootMinuitCommands_h 2 #define PhysicsTools_Utilities_RootMinuitCommands_h 13 #include<boost/tokenizer.hpp> 14 #include <boost/algorithm/string/join.hpp> 32 if(stringArgs.size() > 0) {
33 for(
size_t i = 0;
i != stringArgs.size(); ++
i) {
34 if(
i != 0) cout <<
",";
35 cout <<
" \"" << stringArgs[
i] <<
"\"";
38 if(doubleArgs.size() > 0) {
39 for(
size_t i = 0;
i != doubleArgs.size(); ++
i) {
40 if(
i != 0) cout <<
",";
41 cout <<
" " << doubleArgs[
i];
47 template<
class Function>
84 typedef boost::tokenizer<boost::char_separator<char> >
tokenizer;
91 const char *
begin = str.c_str();
93 double val = strtod(begin, &end);
97 <<
"RootMinuitCommands: invalid double value: " 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;
110 std::ostringstream
out;
111 out <<
"RootMinuitCommands config. error, line " << lineNumber_<<
": ";
115 const typename tokenizer::iterator &
end)
const {
119 << errorHeader() <<
"missing parameter\n";
124 template<
typename Function>
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");
135 if(!cmssw_base.empty()) {
136 directories.emplace_back(cmssw_base +
"/src");
139 for(
auto const&
d: directories) {
140 std::ifstream
f{
d+
"/"+fileName };
146 if(!file.is_open()) {
148 <<
"RootMinuitCommands: can't open file: " << fileName
152 cout <<
">>> configuration file: " << fileName << endl;
156 while(getline(file, line)) {
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(
" ");
163 tokenizer::iterator
i = tokens.begin(),
e = tokens.end();
164 if(tokens.begin()==tokens.end())
continue;
165 if(*(i->begin()) !=
'#') {
170 <<
"please, declare all parameter before all other minuit commands.\n";
171 string name = nextToken(i,
e);
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;
179 string fixed = nextToken(i,
e);
182 else if(fixed ==
"free")
187 <<
"fix parameter option unknown: " << *i <<
"\n" 188 <<
"valid options are: fixed, free.\n";
192 pars_.push_back(std::make_pair(name, par));
193 size_t s = parIndices_.size();
194 parIndices_[
name] =
s;
198 <<
" [" << par.
min <<
", " << par.
max <<
"]," 199 <<
" err: " << par.
err 205 string arg = nextToken(i,
e);
207 commands_.push_back(com);
211 }
else if(*i ==
kSet) {
215 string arg = nextToken(i,
e);
217 com.
doubleArgs.push_back(string2double(nextToken(i,
e)));
218 commands_.push_back(com);
226 commands_.push_back(com);
233 <<
"unkonwn command:: " << *i <<
"\n";
239 cout <<
">>> end configuration" << endl;
242 template<
typename Function>
245 typename vector<command>::const_iterator
c = commands_.begin(),
end = commands_.end();
246 for(; c !=
end; ++
c) {
248 cout <<
">>> minuit command: ";
258 else if(c->name ==
kFix)
262 else if(c->name ==
kSet)
263 minuit.
setParameter(c->stringArgs[0], c->doubleArgs[0]);
double min(const std::string &name)
void init(const char *fileName)
std::string errorHeader() const
const std::string & name() const
boost::tokenizer< boost::char_separator< char > > tokenizer
std::vector< std::string > stringArgs
void printFitResults(std::ostream &cout=std::cout)
void setParameter(const std::string &name, double val)
void fixParameter(const std::string &name)
double par(const std::string &name)
void run(RootMinuit< Function > &minuit) const
std::vector< command > commands_
std::vector< double > doubleArgs
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)
std::vector< std::pair< std::string, parameter_t > > parameterVector_t
commands
min number of hits for refit layers to remove
void print(std::ostream &cout) const
double string2double(const std::string &str) const
std::map< std::string, size_t > parIndices_
static std::string join(char **cmd)
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)
RootMinuitCommand command
RootMinuit< Function > minuit
void releaseParameter(const std::string &name)
RootMinuitCommands(const char *fileName, bool verbose=true)
RootMinuitCommands(bool verbose=true)