AmpTools
ConfigFileParser.cc
Go to the documentation of this file.
1 //******************************************************************************
2 // This file is part of AmpTools, a package for performing Amplitude Analysis
3 //
4 // Copyright Trustees of Indiana University 2010, all rights reserved
5 //
6 // This software written by Matthew Shepherd, Ryan Mitchell, and
7 // Hrayr Matevosyan at Indiana University, Bloomington
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 // 1. Redistributions of source code must retain the above copyright
13 // notice and author attribution, this list of conditions and the
14 // following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright
16 // notice and author attribution, this list of conditions and the
17 // following disclaimer in the documentation and/or other materials
18 // provided with the distribution.
19 // 3. Neither the name of the University nor the names of its contributors
20 // may be used to endorse or promote products derived from this software
21 // without specific prior written permission.
22 //
23 // Creation of derivative forms of this software for commercial
24 // utilization may be subject to restriction; written permission may be
25 // obtained from the Trustees of Indiana University.
26 //
27 // INDIANA UNIVERSITY AND THE AUTHORS MAKE NO REPRESENTATIONS OR WARRANTIES,
28 // EXPRESS OR IMPLIED. By way of example, but not limitation, INDIANA
29 // UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCANTABILITY OR
30 // FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THIS SOFTWARE OR
31 // DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS,
32 // OR OTHER RIGHTS. Neither Indiana University nor the authors shall be
33 // held liable for any liability with respect to any claim by the user or
34 // any other party arising from use of the program.
35 //******************************************************************************
36 
37 
38 #include <iostream>
39 #include <fstream>
40 #include <cctype>
41 #include <utility>
42 #include <string>
43 #include <vector>
44 #include <map>
45 #include <complex>
46 #include <stdlib.h>
49 
50 
51 bool ConfigFileParser::m_verboseParsing = false;
52 
53 
55  : m_configurationInfo( NULL ), m_fitName(""), m_configFile(""){
56 }
57 
58 
59 
60 ConfigFileParser::ConfigFileParser(const string& configFile)
61  : m_configurationInfo( NULL ), m_fitName(""){
62 
63  readConfigFile(configFile);
64 
65 }
66 
67 
68 
70  : m_configurationInfo( NULL ), m_fitName(""){
71 
72  readConfigFile(input);
73 
74 }
75 
76 
77 void
78 ConfigFileParser::readConfigFile(const string& configFile){
79  if (m_configurationInfo) delete m_configurationInfo;
80  m_configFile = configFile;
81  m_fitName = "";
82 
83  m_configFileLines = expandConfigFileLines(readConfigFileLines(configFile));
84  setupConfigurationInfo();
85 
86 }
87 
88 
89 void
91  if (m_configurationInfo) delete m_configurationInfo;
92  m_configFile = "stream";
93  m_fitName = "";
94 
95  m_configFileLines = expandConfigFileLines(readConfigFileLines(input));
96  setupConfigurationInfo();
97 
98 }
99 
100 
101 vector<ConfigFileLine>
102 ConfigFileParser::readConfigFileLines(const string& configfile) const{
103 
104  if (m_verboseParsing)
105  cout << "ConfigFileParser INFO: Reading from the file... " << configfile << endl;
106 
107  vector<ConfigFileLine> configFileLines;
108  int lineNumber = 0;
109 
110  ifstream in(configfile.c_str());
111  if (!in.is_open()){
112  cout << "ConfigFileParser ERROR: Could not open file: " << configfile << endl;
113  exit(1);
114  }
115  while (!in.eof()){
116  string line;
117  getline(in,line);
118  configFileLines.push_back(ConfigFileLine(configfile,++lineNumber, line));
119  if (m_verboseParsing)
120  configFileLines[configFileLines.size()-1].printLine();
121  }
122  in.close();
123 
124  if (m_verboseParsing)
125  cout << "ConfigFileParser INFO: Finished reading from the file... " << configfile << endl;
126 
127  return configFileLines;
128 
129 }
130 
131 
132 vector<ConfigFileLine>
133 ConfigFileParser::readConfigFileLines(istream& input) const{
134 
135  if (m_verboseParsing)
136  cout << "ConfigFileParser INFO: Reading from a stream... " << endl;
137 
138  vector<ConfigFileLine> configFileLines;
139  int lineNumber = 0;
140 
141  while (!input.eof()){
142  string line;
143  getline(input,line);
144  configFileLines.push_back(ConfigFileLine("stream",++lineNumber, line));
145  if (m_verboseParsing)
146  configFileLines[configFileLines.size()-1].printLine();
147  }
148 
149  if (m_verboseParsing)
150  cout << "ConfigFileParser INFO: Finished reading from a stream " << endl;
151 
152  return configFileLines;
153 
154 }
155 
156 
157 
158 vector<ConfigFileLine>
159 ConfigFileParser::expandConfigFileLines(vector<ConfigFileLine> configFileLines) const{
160 
161  if (m_verboseParsing)
162  cout << "ConfigFileParser INFO: Starting to expand config file lines..." << endl;
163 
164 
165  // flush all "include" lines
166 
167  for (vector<ConfigFileLine>::iterator lineItr = configFileLines.begin();
168  lineItr != configFileLines.end(); ++lineItr){
169 
170  if (lineItr->keyword() == "include"){
171 
172  if (lineItr->arguments().size() != 1){
173  cout << "ConfigFileParser ERROR: Wrong number of arguments for the include statement:" << endl;
174  lineItr->printLine();
175  exit(1);
176  }
177 
178  // read in the input config file lines
179 
180  string inputFile = lineItr->arguments()[0];
181  vector<ConfigFileLine> inputFileLines = readConfigFileLines(inputFile);
182 
183  // replace the input line
184 
185  vector<ConfigFileLine>::iterator inputBegin = inputFileLines.begin();
186  vector<ConfigFileLine>::iterator inputEnd = inputFileLines.end();
187  *lineItr = ConfigFileLine("",0,"########## INCLUDE FILE " + inputFile + " ########");
188  configFileLines.insert(++lineItr, inputBegin, inputEnd);
189  lineItr = configFileLines.begin();
190 
191  }
192 
193  }
194 
195 
196  // flush all definitions given by "define" lines
197 
198  for (vector<ConfigFileLine>::iterator lineItr = configFileLines.begin();
199  lineItr != configFileLines.end(); ++lineItr){
200 
201  if (lineItr->keyword() == "define"){
202 
203  if (lineItr->arguments().size() == 0){
204  cout << "ConfigFileParser ERROR: Wrong number of arguments for the define statement:" << endl;
205  lineItr->printLine();
206  exit(1);
207  }
208 
209  vector<string> arguments = lineItr->arguments();
210  string key = arguments[0];
211  vector<string> value (arguments.begin()+1,arguments.end());
212 
213  for (unsigned int j = 0; j < configFileLines.size(); j++){
214  configFileLines[j].flushDefinition(key,value);
215  }
216 
217  }
218  }
219 
220 
221  if (m_verboseParsing)
222  cout << "ConfigFileParser INFO: Finished expanding config file lines..." << endl;
223 
224  if (m_verboseParsing){
225  for (unsigned int i = 0; i < configFileLines.size(); i++){
226  configFileLines[i].printLine();
227  }
228  }
229 
230  return configFileLines;
231 
232 }
233 
234 
235 
236 
237 
238 void
239 ConfigFileParser::setupConfigurationInfo(){
240 
241 
242  // ZEROTH PASS ("fit")
243 
244  if (m_verboseParsing)
245  cout << "ConfigFileParser INFO: Starting ZEROTH PASS (finding the fit name)" << endl;
246 
247  for (vector<ConfigFileLine>::iterator lineItr = m_configFileLines.begin();
248  lineItr != m_configFileLines.end(); ++lineItr){
249 
250  if ((*lineItr).keyword() == "fit") doFit(*lineItr);
251 
252  if ((*lineItr).keyword() == "keyword") m_userKeywords.insert((*lineItr).arguments()[0]);
253 
254  }
255 
256  if (m_verboseParsing)
257  cout << "ConfigFileParser INFO: Finished ZEROTH PASS" << endl;
258 
259 
260  // Create a new ConfigurationInfo object
261 
262 
263  if (m_fitName == "")
264  cout << "ConfigFileParser WARNING: use the keyword \"fit\" to define a fit name" << endl;
265 
266  m_configurationInfo = new ConfigurationInfo(m_fitName);
267 
268 
269 
270  // FIRST PASS ("reaction")
271 
272  if (m_verboseParsing)
273  cout << "ConfigFileParser INFO: Starting FIRST PASS (creating reactions and parameters)" << endl;
274 
275  for (vector<ConfigFileLine>::iterator lineItr = m_configFileLines.begin();
276  lineItr != m_configFileLines.end(); ++lineItr){
277 
278  if ((*lineItr).keyword() == "reaction") doReaction(*lineItr);
279  if ((*lineItr).keyword() == "parameter") doParameter(*lineItr);
280 
281  if (m_userKeywords.count((*lineItr).keyword())) doKeyword(*lineItr);
282 
283  }
284 
285  if (m_verboseParsing)
286  cout << "ConfigFileParser INFO: Finished FIRST PASS" << endl;
287 
288 
289 
290  // SECOND PASS ("sum" and reaction info)
291 
292  if (m_verboseParsing)
293  cout << "ConfigFileParser INFO: Starting SECOND PASS (creating sums and filling reactions)" << endl;
294 
295  for (vector<ConfigFileLine>::iterator lineItr = m_configFileLines.begin();
296  lineItr != m_configFileLines.end(); ++lineItr){
297 
298  if (((*lineItr).keyword() == "datafile") ||
299  ((*lineItr).keyword() == "genmcfile") ||
300  ((*lineItr).keyword() == "accmcfile") ||
301  ((*lineItr).keyword() == "data") ||
302  ((*lineItr).keyword() == "bkgnd") ||
303  ((*lineItr).keyword() == "genmc") ||
304  ((*lineItr).keyword() == "accmc")) doData(*lineItr);
305 
306  if ((*lineItr).keyword() == "normintfile") doNormInt(*lineItr);
307 
308  if ((*lineItr).keyword() == "sum") doSum(*lineItr);
309 
310  }
311 
312  if (m_verboseParsing)
313  cout << "ConfigFileParser INFO: Finished SECOND PASS" << endl;
314 
315 
316 
317  // THIRD PASS ("amplitude")
318 
319  if (m_verboseParsing)
320  cout << "ConfigFileParser INFO: Starting THIRD PASS (creating amplitudes)" << endl;
321 
322  for (vector<ConfigFileLine>::iterator lineItr = m_configFileLines.begin();
323  lineItr != m_configFileLines.end(); ++lineItr){
324 
325  if ((*lineItr).keyword() == "amplitude") doAmplitude(*lineItr);
326 
327  }
328 
329  if (m_verboseParsing)
330  cout << "ConfigFileParser INFO: Finished THIRD PASS" << endl;
331 
332 
333 
334  // FOURTH PASS (operations on amplitudes)
335 
336  if (m_verboseParsing)
337  cout << "ConfigFileParser INFO: Starting FOURTH PASS (filling amplitudes)" << endl;
338 
339  for (vector<ConfigFileLine>::iterator lineItr = m_configFileLines.begin();
340  lineItr != m_configFileLines.end(); ++lineItr){
341 
342  if ((*lineItr).keyword() == "constrain") doConstrain(*lineItr);
343 
344  if ((*lineItr).keyword() == "permute") doPermute(*lineItr);
345 
346  if ((*lineItr).keyword() == "initialize") doInitialize(*lineItr);
347 
348  if ((*lineItr).keyword() == "scale") doScale(*lineItr);
349 
350  }
351 
352  if (m_verboseParsing)
353  cout << "ConfigFileParser INFO: Finished FOURTH PASS" << endl;
354 
355 
356  // Do some quick syntax checks
357 
358  if (m_verboseParsing)
359  cout << "ConfigFileParser INFO: Begin Syntax Checking " << endl;
360 
361  checkSyntax();
362 
363  if (m_verboseParsing)
364  cout << "ConfigFileParser INFO: Finished Syntax Checking " << endl;
365 
366 }
367 
368 
369 
370 
371 
372 
373 
374 
375 
376 void
377 ConfigFileParser::checkSyntax() const{
378 
379  map<string, pair<int,int> > keywordParameters;
380  keywordParameters["define"] = pair<int,int>(1,100);
381  keywordParameters["keyword"] = pair<int,int>(3,3);
382  keywordParameters["fit"] = pair<int,int>(1,1);
383  keywordParameters["reaction"] = pair<int,int>(3,100);
384  keywordParameters["data"] = pair<int,int>(2,100);
385  keywordParameters["bkgnd"] = pair<int,int>(2,100);
386  keywordParameters["genmc"] = pair<int,int>(2,100);
387  keywordParameters["accmc"] = pair<int,int>(2,100);
388  keywordParameters["normintfile"] = pair<int,int>(2,3);
389  keywordParameters["sum"] = pair<int,int>(2,100);
390  keywordParameters["amplitude"] = pair<int,int>(4,100);
391  keywordParameters["initialize"] = pair<int,int>(6,8);
392  keywordParameters["constrain"] = pair<int,int>(6,100);
393  keywordParameters["permute"] = pair<int,int>(5,100);
394  keywordParameters["parameter"] = pair<int,int>(2,5);
395  keywordParameters["scale"] = pair<int,int>(4,4);
396  // these are deprecated, but print out an error message later
397  keywordParameters["datafile"] = pair<int,int>(2,100);
398  keywordParameters["genmcfile"] = pair<int,int>(2,100);
399  keywordParameters["accmcfile"] = pair<int,int>(2,100);
400 
401  for (vector<ConfigFileLine>::const_iterator lineItr = m_configFileLines.begin();
402  lineItr != m_configFileLines.end(); ++lineItr){
403  if (!lineItr->comment()){
404  map<string, pair<int,int> >::const_iterator mapItr = keywordParameters.find(lineItr->keyword());
405  if (mapItr == keywordParameters.end()){
406  cout << "ConfigFileParser ERROR: Undefined keyword: " << lineItr->keyword() << endl;
407  lineItr->printLine();
408  exit(1);
409  }
410  else if (((int)lineItr->arguments().size() > mapItr->second.second) ||
411  ((int)lineItr->arguments().size() < mapItr->second.first)){
412  cout << "ConfigFileParser ERROR: Keyword " << lineItr->keyword() <<
413  " has the wrong number of arguments: " << endl;
414  lineItr->printLine();
415  exit(1);
416  }
417  else if (lineItr->keyword() == "keyword"){
418  keywordParameters[lineItr->arguments()[0]] =
419  pair<int,int>(atoi(lineItr->arguments()[1].c_str()),atoi(lineItr->arguments()[2].c_str()));
420  }
421  }
422  }
423 
424 }
425 
426 
427 
428 
429 
430 void
431 ConfigFileParser::doFit(const ConfigFileLine& line){
432  vector<string> arguments = line.arguments();
433  m_fitName = arguments[0];
434 }
435 
436 
437 
438 void
439 ConfigFileParser::doReaction(const ConfigFileLine& line){
440  vector<string> arguments = line.arguments();
441  string reaction = arguments[0];
442  vector<string> particles (arguments.begin()+1, arguments.end());
443  m_configurationInfo->createReaction(reaction,particles);
444 }
445 
446 
447 void
448 ConfigFileParser::doKeyword(const ConfigFileLine& line){
449  m_configurationInfo->addUserKeyword(line.keyword(),line.arguments());
450 }
451 
452 
453 
454 void
455 ConfigFileParser::doData(const ConfigFileLine& line){
456  vector<string> arguments = line.arguments();
457  string reaction = arguments[0];
458  string classname = arguments[1];
459  vector<string> dataargs (arguments.begin()+2, arguments.end());
460  ReactionInfo* rct = m_configurationInfo->reaction(reaction);
461  if (!rct){
462  cout << "ConfigFileParser ERROR: Can't associate data with a reaction: " << endl;
463  line.printLine();
464  exit(1);
465  }
466  if (line.keyword() == "datafile"){
467  cout << "ConfigFileParser ERROR: datafile is deprecated, use data" << endl;
468  line.printLine();
469  exit(1);
470  }
471  if (line.keyword() == "genmcfile"){
472  cout << "ConfigFileParser ERROR: genmcfile is deprecated, use genmc" << endl;
473  line.printLine();
474  exit(1);
475  }
476  if (line.keyword() == "accmcfile"){
477  cout << "ConfigFileParser ERROR: accmcfile is deprecated, use accmc" << endl;
478  line.printLine();
479  exit(1);
480  }
481  if (line.keyword() == "data") rct->setData (classname, dataargs);
482  if (line.keyword() == "bkgnd") rct->setBkgnd(classname, dataargs);
483  if (line.keyword() == "genmc") rct->setGenMC(classname, dataargs);
484  if (line.keyword() == "accmc") rct->setAccMC(classname, dataargs);
485 }
486 
487 
488 void
489 ConfigFileParser::doNormInt(const ConfigFileLine& line){
490  vector<string> arguments = line.arguments();
491  string reaction = arguments[0];
492  string file = arguments[1];
493  bool input = false;
494  if (arguments.size() > 2 && arguments[2] == "input") input = true;
495  ReactionInfo* rct = m_configurationInfo->reaction(reaction);
496  if (!rct){
497  cout << "ConfigFileParser ERROR: Can't associate normintfile with a reaction: " << endl;
498  line.printLine();
499  exit(1);
500  }
501  if (line.keyword() == "normintfile"){
502  rct->setNormIntFile(file, input);
503  }
504 }
505 
506 
507 
508 void
509 ConfigFileParser::doSum(const ConfigFileLine& line){
510  vector<string> arguments = line.arguments();
511  string reaction = arguments[0];
512  ReactionInfo* rct = m_configurationInfo->reaction(reaction);
513  if (!rct){
514  cout << "ConfigFileParser ERROR: Can't associate sum with a reaction: " << endl;
515  line.printLine();
516  exit(1);
517  }
518  for (unsigned int i = 1; i < arguments.size(); i++){
519  string sum = arguments[i];
520  m_configurationInfo->createCoherentSum(reaction,sum);
521  }
522 }
523 
524 
525 void
526 ConfigFileParser::doParameter(const ConfigFileLine& line){
527  vector<string> arguments = line.arguments();
528  string parname = arguments[0];
529  double value = atof((arguments[1]).c_str());
530  ParameterInfo* parinfo = m_configurationInfo->createParameter(parname,value);
531  string type("floating");
532  double a = 0.0;
533  double b = 0.0;
534  if (arguments.size() > 2) type = arguments[2];
535  if (arguments.size() > 3) a = atof((arguments[3]).c_str());
536  if (arguments.size() > 4) b = atof((arguments[4]).c_str());
537  if (type == "floating"){
538  }
539  else if (type == "fixed"){
540  parinfo->setFixed(true);
541  }
542  else if (type == "bounded"){
543  parinfo->setBounded(true);
544  parinfo->setLowerBound(a);
545  parinfo->setUpperBound(b);
546  }
547  else if (type == "gaussian"){
548  parinfo->setGaussianBounded(true);
549  parinfo->setCentralValue(a);
550  parinfo->setGaussianError(b);
551  }
552  else{
553  cout << "ConfigFileParser ERROR: parameter type must be floating, fixed, bounded, or gaussian " << endl;
554  line.printLine();
555  exit(1);
556  }
557 }
558 
559 
560 void
561 ConfigFileParser::doAmplitude(const ConfigFileLine& line){
562  vector<string> arguments = line.arguments();
563  string reaction = arguments[0];
564  string sumname = arguments[1];
565  string ampname = arguments[2];
566  vector<string> ampargs (arguments.begin()+3, arguments.end());
567  AmplitudeInfo* ampinfo = m_configurationInfo->amplitude(reaction,sumname,ampname);
568  if (!ampinfo) ampinfo = m_configurationInfo->createAmplitude(reaction,sumname,ampname);
569  ampinfo->addFactor(ampargs);
570  for (unsigned int i = 1; i < ampargs.size(); i++){
571  unsigned int j = ampargs[i].size()-1;
572  if ((ampargs[i][0] == '[') && (ampargs[i][j] == ']')){
573  string parname("");
574  for (unsigned int k = 1; k < j; k++){
575  parname += ampargs[i][k];
576  }
577  ParameterInfo* parinfo = m_configurationInfo->parameter(parname);
578  if (!parinfo){
579  cout << "ConfigFileParser ERROR: can't find parameter " << parname << endl;
580  line.printLine();
581  exit(1);
582  }
583  ampinfo->addParameter(parinfo);
584  }
585  }
586 }
587 
588 
589 void
590 ConfigFileParser::doConstrain(const ConfigFileLine& line){
591  vector<string> arguments = line.arguments();
592  if (arguments.size()%3 != 0){
593  cout << "ConfigFileParser ERROR: wrong number of arguments for constrain keyword " << endl;
594  line.printLine();
595  exit(1);
596  }
597  string reaction1 = arguments[0];
598  string sumname1 = arguments[1];
599  string ampname1 = arguments[2];
600  for (unsigned int i = 1; i < arguments.size()/3; i++){
601  string reaction2 = arguments[i*3];
602  string sumname2 = arguments[i*3+1];
603  string ampname2 = arguments[i*3+2];
604  AmplitudeInfo* amplitude1 = m_configurationInfo->amplitude(reaction1,sumname1,ampname1);
605  AmplitudeInfo* amplitude2 = m_configurationInfo->amplitude(reaction2,sumname2,ampname2);
606  if ((!amplitude1) || (!amplitude2)){
607  cout << "ConfigFileParser ERROR: trying to constrain nonexistent amplitude " << endl;
608  line.printLine();
609  exit(1);
610  }
611  amplitude1->addConstraint(amplitude2);
612  }
613 }
614 
615 
616 
617 void
618 ConfigFileParser::doPermute(const ConfigFileLine& line){
619  vector<string> arguments = line.arguments();
620  string reaction = arguments[0];
621  string sumname = arguments[1];
622  string ampname = arguments[2];
623  vector<string> permutation(arguments.begin()+3, arguments.end());
624  AmplitudeInfo* amplitude = m_configurationInfo->amplitude(reaction,sumname,ampname);
625  if (!amplitude){
626  cout << "ConfigFileParser ERROR: trying to permute nonexistent amplitude " << endl;
627  line.printLine();
628  exit(1);
629  }
630  vector<string> particleList = m_configurationInfo->reaction(reaction)->particleList();
631  if (permutation.size() != particleList.size()){
632  cout << "ConfigFileParser ERROR: wrong number of arguments for permute keyword " << endl;
633  line.printLine();
634  exit(1);
635  }
636  vector<int> intpermutation;
637  for (unsigned int i = 0; i < permutation.size(); i++){
638  for (unsigned int j = 0; j < permutation[i].size(); j++){
639  if (!isdigit(permutation[i][j])){
640  cout << "ConfigFileParser ERROR: particle index is not an unsigned integer " << endl;
641  line.printLine();
642  exit(1);
643  }
644  }
645  int ipart = atoi(permutation[i].c_str());
646  if (ipart < 0 || ipart >= (int)particleList.size()){
647  cout << "ConfigFileParser ERROR: particle index is out of bounds " << endl;
648  line.printLine();
649  exit(1);
650  }
651  intpermutation.push_back(ipart);
652  }
653  for (unsigned int i = 0; i < intpermutation.size(); i++){
654  for (unsigned int j = i+1; j < intpermutation.size(); j++){
655  if (intpermutation[i] == intpermutation[j]){
656  cout << "ConfigFileParser ERROR: particle index repeated " << endl;
657  line.printLine();
658  exit(1);
659  }
660  }
661  }
662  amplitude->addPermutation(intpermutation);
663 }
664 
665 
666 void
667 ConfigFileParser::doInitialize(const ConfigFileLine& line){
668  vector<string> arguments = line.arguments();
669  string reaction = arguments[0];
670  string sumname = arguments[1];
671  string ampname = arguments[2];
672  string type = arguments[3];
673  double value1 = atof(arguments[4].c_str());
674  double value2 = atof(arguments[5].c_str());
675  string fixtype1("floating");
676  string fixtype2("");
677  if (arguments.size() >= 7) fixtype1 = arguments[6];
678  if (arguments.size() == 8) fixtype2 = arguments[7];
679  AmplitudeInfo* amplitude = m_configurationInfo->amplitude(reaction,sumname,ampname);
680  if (!amplitude){
681  cout << "ConfigFileParser ERROR: trying to initialize nonexistent amplitude " << endl;
682  line.printLine();
683  exit(1);
684  }
685  if (type == "cartesian"){
686  amplitude->setValue(complex<double>(value1,value2));
687  }
688  else if (type == "polar"){
689  amplitude->setValue(polar(value1,value2));
690  }
691  else if (type == "events"){
692  cout << "ConfigFileParser ERROR: initializing with events is not yet re-implemented " << endl;
693  line.printLine();
694  exit(1);
695  /*
696  string normIntFile = m_configurationInfo->reaction(reaction)->normIntFile();
697  if (normIntFile == ""){
698  cout << "ConfigFileParser ERROR: initializing with events, but no normalization integral " << endl;
699  cout << " file has been specified for this reaction " << endl;
700  line.printLine();
701  exit(1);
702  }
703  ifstream test(normIntFile.c_str());
704  if (!test){
705  cout << "ConfigFileParser ERROR: initializing with events, but can't find the normalization integral " << endl;
706  cout << " file specified for this reaction (" << normIntFile << ")" << endl;
707  line.printLine();
708  exit(1);
709  }
710  NormIntInterface normint(normIntFile);
711  if (!normint.hasAmpInt(ampname,ampname)){
712  cout << "ConfigFileParser ERROR: can't find the right amplitude in the normalization integral file " << endl;
713  line.printLine();
714  exit(1);
715  }
716  value1 = sqrt(value1/(abs(normint.ampInt(ampname,ampname))));
717  amplitude->setValue(polar(value1,value2));
718  */
719  }
720  else{
721  cout << "ConfigFileParser ERROR: initialize must use cartesian, polar, or events " << endl;
722  line.printLine();
723  exit(1);
724  }
725  if (fixtype1 == "floating") {
726  }
727  else if (fixtype1 == "real"){
728  amplitude->setReal(true);
729  }
730  else if (fixtype1 == "fixed"){
731  amplitude->setFixed(true);
732  }
733  else{
734  cout << "ConfigFileParser ERROR: initialize must use floating, fixed, or real " << endl;
735  line.printLine();
736  exit(1);
737  }
738  if (fixtype2 == "") {
739  }
740  else if (fixtype2 == "real"){
741  amplitude->setReal(true);
742  }
743  else if (fixtype2 == "fixed"){
744  amplitude->setFixed(true);
745  }
746  else if (fixtype2 == "floating"){
747  amplitude->setReal(false);
748  amplitude->setFixed(false);
749  }
750  else{
751  cout << "ConfigFileParser ERROR: initialize must use floating, fixed, or real " << endl;
752  line.printLine();
753  exit(1);
754  }
755 }
756 
757 
758 void
759 ConfigFileParser::doScale(const ConfigFileLine& line){
760  vector<string> arguments = line.arguments();
761  string reaction = arguments[0];
762  string sumname = arguments[1];
763  string ampname = arguments[2];
764  string value = arguments[3];
765  AmplitudeInfo* amplitude = m_configurationInfo->amplitude(reaction,sumname,ampname);
766  if (!amplitude){
767  cout << "ConfigFileParser ERROR: trying to scale nonexistent amplitude " << endl;
768  line.printLine();
769  exit(1);
770  }
771  if ((value.size() > 0) && (value[0] == '[') and (value[value.size()-1] == ']')){
772  string parname("");
773  for (unsigned int k = 1; k < value.size()-1; k++){
774  parname += value[k];
775  }
776  ParameterInfo* parinfo = m_configurationInfo->parameter(parname);
777  if (!parinfo){
778  cout << "ConfigFileParser ERROR: can't find parameter " << parname << endl;
779  line.printLine();
780  exit(1);
781  }
782  amplitude->addParameter(parinfo);
783  }
784  amplitude->setScale(value);
785 }
786 
787 
788 
789 void
791  for (unsigned int i = 0; i < m_configFileLines.size(); i++){
792  m_configFileLines[i].printLine();
793  }
794 }
795 
796 
797 
798 
799 
800 
801 
802 
803 
804 
805 
806 
807 ConfigFileLine::ConfigFileLine(const string& fileName, int lineNumber, const string& line){
808 
809  m_fileName = fileName;
810  m_lineNumber = lineNumber;
811  m_line = line;
812  m_keyword = "";
813  m_comment = false;
814 
815  // replace all "::" with " "
816  string newline(line);
817  while (newline.find("::") != string::npos){
818  newline.replace(newline.find("::"),2," ");
819  }
820 
821  // parse the line into words
822  vector<string> words;
823  string word("");
824  for (unsigned int j = 0; j < newline.size(); j++){
825  if (!isspace(newline[j])){
826  word += newline[j];
827  if ((j == (newline.size()-1))&&(!word.empty())){
828  words.push_back(word);
829  word = "";
830  }
831  }
832  else if (!word.empty()){
833  words.push_back(word);
834  word = "";
835  }
836  }
837 
838  // record the keyword
839  if (words.size() > 0) m_keyword = words[0];
840 
841  // check if this is a comment line
842  if (m_keyword.empty()) m_comment = true;
843  else if (m_keyword[0] == '#') m_comment = true;
844  if (m_comment) m_keyword = "";
845 
846  // record the arguments
847  if (!m_comment){
848  for (unsigned int i = 1; i < words.size(); i++){
849  m_arguments.push_back(words[i]);
850  }
851  }
852 }
853 
854 
855 void
856 ConfigFileLine::flushDefinition(const string& word, const vector<string>& definition){
857  bool hasWord = false;
858  for (unsigned int i = 0; i < m_arguments.size(); i++){
859  if (m_arguments[i] == word) hasWord = true;
860  }
861  if (!hasWord) return;
862  if ((m_keyword == "define") && (m_arguments[0] == word)) return;
863  vector<string> newArguments;
864  for (unsigned int i = 0; i < m_arguments.size(); i++){
865  if (m_arguments[i] != word){
866  newArguments.push_back(m_arguments[i]);
867  }
868  else{
869  for (unsigned int j = 0; j < definition.size(); j++){
870  newArguments.push_back(definition[j]);
871  }
872  }
873  }
874  m_arguments = newArguments;
875 }
876 
877 
ReactionInfo * reaction(const string &reactionName) const
void setLowerBound(double lowerBound)
void setFixed(bool fixed)
void setCentralValue(double centralValue)
void addFactor(const vector< string > &factor)
void setFixed(bool fixed)
void readConfigFile(const string &configFile)
void addUserKeyword(const string &uesrKeyword, const vector< string > &arguments)
ParameterInfo * parameter(const string &parName) const
const vector< string > & particleList() const
void displayConfigFile() const
void setBounded(bool bounded)
#define NULL
Definition: URtypes.h:72
void setUpperBound(double upperBound)
void setReal(bool real)
void setGaussianError(double gaussianError)
string keyword() const
AmplitudeInfo * amplitude(const string &reactionName, const string &sumName, const string &ampName) const
void setGaussianBounded(bool gaussianBounded)
ReactionInfo * createReaction(const string &reactionName, const vector< string > &particleList)
void addParameter(ParameterInfo *parameter)
void setNormIntFile(const string &normIntFile, bool input=false)
void setScale(string scale)
ParameterInfo * createParameter(const string &parName, double value)
ConfigFileLine(const string &fileName, int lineNumber, const string &line)
void setValue(complex< double > value)
void flushDefinition(const string &word, const vector< string > &definition)
vector< string > arguments() const
void printLine() const
CoherentSumInfo * createCoherentSum(const string &reactionName, const string &sumName)
AmplitudeInfo * createAmplitude(const string &reactionName, const string &sumName, const string &ampName)
void addConstraint(AmplitudeInfo *constraint)