musrfit  1.9.2
PFunctionHandler.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  PFunctionHandler.cpp
4 
5  Author: Andreas Suter
6  e-mail: andreas.suter@psi.ch
7 
8 ***************************************************************************/
9 
10 /***************************************************************************
11  * Copyright (C) 2007-2023 by Andreas Suter *
12  * andreas.suter@psi.ch *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the *
26  * Free Software Foundation, Inc., *
27  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28  ***************************************************************************/
29 
30 #include <string>
31 #include <cassert>
32 
33 #include "PFunctionHandler.h"
34 
35 //-------------------------------------------------------------
36 // Constructor
37 //-------------------------------------------------------------
44 {
45  fValid = true;
46 }
47 
48 //-------------------------------------------------------------
49 // Destructor
50 //-------------------------------------------------------------
55 {
56  fLines.clear();
57  fFuncs.clear();
58  fFuncComment.clear();
59 }
60 
61 //-------------------------------------------------------------
62 // DoParse (public)
63 //-------------------------------------------------------------
68 {
69  Bool_t success = true;
70  PFunctionGrammar function;
71  TString line;
72 
73  // feed the function block into the parser. Start with i=1, since i=0 is FUNCTIONS
74  for (UInt_t i=1; i<fLines.size(); i++) {
75 
76  // function line to upper case after cutting out prepended comment
77  line = fLines[i].fLine;
78  Ssiz_t pos = line.First('#'); // find prepended comment
79  TString Comment("");
80  if (pos != kNPOS) { // comment present
81  for (Int_t i=pos; i<line.Length(); i++) {
82  Comment += line[i];
83  }
84  }
85  fFuncComment.push_back(Comment);
86  if (pos != kNPOS) { // comment present, hence remove it from the string to be parsed
87  line.Remove(pos);
88  line.Remove(TString::kTrailing, ' ');
89  }
90  line.ToUpper();
91 
92  // do parsing
93  tree_parse_info<> info = ast_parse(line.Data(), function, space_p);
94 
95  if (info.full) { // parsing successful
96  PFunction func(info); // generate an evaluation function object based on the AST tree
97  fFuncs.push_back(func); // feeds it to the functions vector
98  } else {
99  std::cerr << std::endl << "**ERROR**: FUNCTIONS parse failed in line " << fLines[i].fLineNo << std::endl;
100  success = false;
101  break;
102  }
103  }
104 
105  // check that the function numbers are unique
106  if (success) {
107  for (UInt_t i=0; i<fFuncs.size(); i++) {
108  for (UInt_t j=i+1; j<fFuncs.size(); j++) {
109  if (fFuncs[i].GetFuncNo() == fFuncs[j].GetFuncNo()) {
110  std::cerr << std::endl << "**ERROR**: function number " << fFuncs[i].GetFuncNo();
111  std::cerr << " is at least twice present! Fix this first.";
112  std::cerr << std::endl;
113  success = false;
114  }
115  }
116  }
117  }
118 
119  return success;
120 }
121 
122 //-------------------------------------------------------------
123 // CheckMapAndParamRange (public)
124 //-------------------------------------------------------------
133 Bool_t PFunctionHandler::CheckMapAndParamRange(UInt_t mapSize, UInt_t paramSize)
134 {
135  Bool_t success = true;
136 
137  for (UInt_t i=0; i<fFuncs.size(); i++) {
138  success = fFuncs[i].CheckMapAndParamRange(mapSize, paramSize);
139  if (!success)
140  break;
141  }
142 
143  return success;
144 }
145 
146 //-------------------------------------------------------------
147 // Eval (public)
148 //-------------------------------------------------------------
158 Double_t PFunctionHandler::Eval(Int_t funNo, std::vector<Int_t> map, std::vector<double> param, PMetaData metaData)
159 {
160  if (GetFuncIndex(funNo) == -1) {
161  std::cerr << std::endl << "**ERROR**: Couldn't find FUN" << funNo << " for evaluation";
162  std::cerr << std::endl;
163  return 0.0;
164  }
165 
166  // set correct map
167  fFuncs[GetFuncIndex(funNo)].SetMap(map);
168 
169  // return evaluated function
170  return fFuncs[GetFuncIndex(funNo)].Eval(param, metaData);
171 }
172 
173 //-------------------------------------------------------------
174 // GetFuncNo (public)
175 //-------------------------------------------------------------
182 {
183  if (idx > fFuncs.size())
184  return -1;
185 
186  return fFuncs[idx].GetFuncNo();
187 }
188 
189 //-------------------------------------------------------------
190 // GetFuncIndex (public)
191 //-------------------------------------------------------------
198 {
199  Int_t index = -1;
200 
201  for (UInt_t i=0; i<fFuncs.size(); i++) {
202  if (fFuncs[i].GetFuncNo() == funcNo) {
203  index = i;
204  break;
205  }
206  }
207 
208  return index;
209 }
210 
211 //-------------------------------------------------------------
212 // GetFuncString (public)
213 //-------------------------------------------------------------
220 {
221  TString funStr("");
222 
223  if ((idx > fFuncs.size()) || (idx > fFuncComment.size()))
224  return funStr;
225 
226  if (fFuncComment[idx].Length() > 0)
227  funStr = *fFuncs[idx].GetFuncString() + " " + fFuncComment[idx];
228  else
229  funStr = *fFuncs[idx].GetFuncString();
230 
231  return funStr;
232 }
Bool_t fValid
true = function handler has valid functions
virtual Bool_t CheckMapAndParamRange(UInt_t mapSize, UInt_t paramSize)
virtual TString GetFuncString(UInt_t idx)
virtual Int_t GetFuncNo(UInt_t idx)
virtual double Eval(Int_t funNo, std::vector< Int_t > map, std::vector< double > param, PMetaData metaData)
PMsrLines fLines
stores the msr-file FUNCTIONS block as clear text.
virtual ~PFunctionHandler()
std::vector< PFunction > fFuncs
vector of all evaluatable functions
std::vector< PMsrLineStructure > PMsrLines
Definition: PMusr.h:539
PFunctionHandler(PMsrLines lines)
virtual Int_t GetFuncIndex(Int_t funcNo)
virtual Bool_t DoParse()
std::vector< TString > fFuncComment
vector of prepended function comments