GooFit  v2.1.3
FunctorWriter.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <goofit/FunctorWriter.h>
3 #include <goofit/PdfBase.h>
4 #include <goofit/Variable.h>
5 #include <map>
6 
7 namespace GooFit {
8 
9 void writeToFile(PdfBase *pdf, const char *fname) {
10  std::vector<Variable> params = pdf->getParameters();
11 
12  std::ofstream writer;
13  writer.open(fname);
14 
15  for(const Variable &p : params) {
16  writer << p.getName() << " " << p.getValue() << " " << p.getError() << " " << p.getLowerLimit() << " "
17  << p.getUpperLimit() << std::endl;
18  }
19 
20  writer.close();
21 }
22 
23 void readFromFile(PdfBase *pdf, const char *fname) {
24  std::vector<Variable> params = pdf->getParameters();
25 
26  std::map<std::string, Variable *> tempMap;
27 
28  for(Variable &p : params) {
29  tempMap[p.getName()] = &p;
30  }
31 
32  std::ifstream reader;
33  reader.open(fname);
34  std::string buffer;
35  char discard[1000];
36  int numSet = 0;
37 
38  while(true) {
39  reader >> buffer;
40 
41  if(reader.eof())
42  break;
43 
44  Variable *var = tempMap[buffer];
45 
46  fptype value, error, lowerlimit, upperlimit;
47  size_t numbins;
48 
49  if(var) {
50  reader >> value >> error >> numbins >> lowerlimit >> upperlimit;
51 
52  var->setValue(value);
53  var->setError(error);
54  var->setLowerLimit(lowerlimit);
55  var->setUpperLimit(upperlimit);
56 
57  if(++numSet == tempMap.size())
58  break;
59  } else {
60  reader.getline(discard, 1000);
61  }
62  }
63 
64  reader.close();
65 }
66 
67 void readListOfNumbers(thrust::host_vector<fptype> &target, const char *fname) {
68  std::ifstream reader;
69  reader.open(fname);
70  fptype buffer = 0;
71 
72  while(true) {
73  reader >> buffer;
74 
75  if(reader.eof())
76  break;
77 
78  target.push_back(buffer);
79  }
80 
81  reader.close();
82 }
83 
84 void writeListOfNumbers(thrust::host_vector<fptype> &target, const char *fname) {
85  std::ofstream writer;
86  writer.open(fname);
87 
88  for(fptype t : target) {
89  writer << t << " ";
90  }
91 
92  writer.close();
93 }
94 } // namespace GooFit
double fptype
void readListOfNumbers(thrust::host_vector< fptype > &target, const char *fname)
void writeToFile(PdfBase *pdf, const char *fname)
void setValue(fptype val)
Set the value.
Definition: Variable.h:70
void setUpperLimit(fptype val)
Set the upper limit.
Definition: Variable.h:75
void writeListOfNumbers(thrust::host_vector< fptype > &target, const char *fname)
void readFromFile(PdfBase *pdf, const char *fname)
void setError(fptype val)
Set the error.
Definition: Variable.h:191
void setLowerLimit(fptype val)
Set the lower limit.
Definition: Variable.h:80
virtual __host__ std::vector< Variable > getParameters() const
Definition: PdfBase.cpp:81