Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFit2D.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <algorithm>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TKey.h"
10 #include "TH2.h"
11 #include "TGraph2D.h"
12 #include "TProfile.h"
13 #include "TF12.h"
14 #include "TString.h"
15 #include "TRegexp.h"
16 
17 #include "JLang/JType.hh"
18 #include "JLang/JTypeList.hh"
19 #include "JLang/JToken.hh"
20 //#include "JTools/JRange.hh"
21 #include "JTools/JTuple.hh"
22 
23 #include "JGizmo/JRootObjectID.hh"
24 #include "JGizmo/JGizmoToolkit.hh"
25 
26 #include "Jeep/JPrint.hh"
27 #include "Jeep/JParser.hh"
28 #include "Jeep/JMessage.hh"
29 
30 
31 namespace {
32 
33  using namespace std;
34  using namespace JPP;
35 
36  /**
37  * Auxiliary class for ROOT fit.
38  */
39  struct JFit {
40  /**
41  * Constructor.
42  *
43  * \param object object
44  */
45  JFit(TObject& object) :
46  object(object)
47  {}
48 
49 
50  /**
51  * Attempt fit for given data type.
52  *
53  * \param type data type
54  * \param tuple argument data
55  */
56  template<class T>
57  void operator()(const JType<T>& type, const JTuple<JTYPELIST<TF2*, string>::typelist>& tuple)
58  {
59  try {
60  result = dynamic_cast<T&>(object).Fit(tuple.first, tuple.second.c_str(), "same");
61  }
62  catch(const std::exception&) {}
63  }
64 
65 
66  TObject& object;
67  TFitResultPtr result;
68  };
69 }
70 
71 
72 /**
73  * \file
74  * General purpose fit program using ROOT.
75  * The option <tt>-f</tt> corresponds to <tt><file name>:<object name></tt>.
76  *
77  * The expressions for the fit function, start and fixed values should comply
78  * with ROOT class TFormula.
79  *
80  * In the expressions of the start and fixed values, names of member methods
81  * of corresponding class of the fit object may appear,
82  * such as TH1::GetMaximum, TH1::GetRMS, etc., e.g:
83  * <pre>
84  * -F "[0]*exp(-0.5*x*x/([1]*[1])*exp(-0.5*y*y/([2]*[2])"
85  * -@ "p0 = GetMaximum; p1 = 2*GetRMS"
86  * -= "p2 = 0"
87  * </pre>
88  * The result of the formulas for the start and fixed values will be evaluated
89  * for each histogram separately.
90  * \author mdejong
91  */
92 int main(int argc, char **argv)
93 {
94  using namespace std;
95  using namespace JPP;
96 
97  typedef JLANG::JToken<';'> JToken_t;
98  //typedef JRange<double> JRange_t;
99 
100  vector<JRootObjectID> inputFile;
101  string outputFile;
102  string formula;
103  vector<JToken_t> startValues;
104  vector<JToken_t> fixedValues;
105  //JRange_t X;
106  //JRange_t Y;
107  string option;
108  int debug;
109 
110  try {
111 
112  JParser<> zap("General purpose fit program using ROOT.");
113 
114  zap['f'] = make_field(inputFile, "<input file>:<object name>");
115  zap['o'] = make_field(outputFile, "ROOT file with fit results") = "fit.root";
116  zap['F'] = make_field(formula, "fit formula, e.g: \"[0]+[1]*x\"");
117  zap['@'] = make_field(startValues, "start values, e.g: \"p0 = GetMaximum;\"");
118  zap['='] = make_field(fixedValues, "fixed values, e.g: \"p0 = GetMaximum;\"");
119  //zap['x'] = make_field(X, "abscissa range") = JRange_t();
120  //zap['y'] = make_field(Y, "abscissa range") = JRange_t();
121  zap['O'] = make_field(option, "Fit option") = "";
122  zap['d'] = make_field(debug) = 1;
123 
124  zap['='] = JPARSER::initialised();
125 
126  zap(argc, argv);
127  }
128  catch(const exception &error) {
129  FATAL(error.what() << endl);
130  }
131 
132 
133  if (option.find('O') == string::npos) { option += "O"; }
134  //if (option.find('N') == string::npos) { option += "N"; }
135  if (debug == 0 && option.find('Q') == string::npos) { option += "Q"; }
136 
137 
138  TFile out(outputFile.c_str(), "recreate");
139 
140 
141  TF2* fcn = new TF2("user", formula.c_str());
142 
143  fcn->SetNpx(1000);
144 
145  if (fcn->IsZombie()) {
146  FATAL("Function: " << formula << " is zombie." << endl);
147  }
148 
149  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
150 
151  DEBUG("Input: " << *input << endl);
152 
153  TDirectory* dir = getDirectory(*input);
154 
155  if (dir == NULL) {
156  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
157  continue;
158  }
159 
160  const TRegexp regexp(input->getObjectName());
161 
162  TIter iter(dir->GetListOfKeys());
163 
164  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
165 
166  const TString tag(key->GetName());
167 
168  DEBUG("Key: " << tag << " match = " << tag.Index(regexp) << " (-1 /= OK)" << endl);
169 
170  // option match
171 
172  if (tag.Index(regexp) != -1) {
173 
174  TObject* object = key->ReadObj();
175 
176 
177  // set fit parameters
178 
179  try {
180 
181  for (vector<JToken_t>::const_iterator j = startValues.begin(); j != startValues.end(); ++j) {
182  fcn->SetParameter(getParameter(*j), getValue(*j,object));
183  }
184 
185  for (vector<JToken_t>::const_iterator j = fixedValues.begin(); j != fixedValues.end(); ++j) {
186  fcn->FixParameter(getParameter(*j), getValue(*j,object));
187  }
188  }
189  catch(JLANG::JParseError& error) {
190  FATAL(error << endl);
191  }
192 
193  DEBUG("Start values " << object->GetName() << endl);
194 
195  for (int j = 0; j != fcn->GetNpar(); ++j) {
196  DEBUG(left << setw(12) << fcn->GetParName (j) << ' ' <<
197  SCIENTIFIC(12,5) << fcn->GetParameter(j) << endl);
198  }
199 
200 
201  // execute fit
202 
203  JFit fit(*object);
204 
206 
207 
208  if (fit.result != -1) {
209 
210  // output fit results
211 
212  NOTICE("Fit values " << object->GetName() << endl);
213  NOTICE("Fit formula " << formula << endl);
214 
215  for (int j = 0; j != fcn->GetNpar(); ++j) {
216  NOTICE(left << setw(12) << fcn->GetParName (j) << ' ' <<
217  SCIENTIFIC(12,5) << fcn->GetParameter(j) << " +/- " <<
218  SCIENTIFIC(12,5) << fcn->GetParError (j) << endl);
219  }
220 
221  } else {
222 
223  WARNING("Object: not compatible with ROOT Fit." << endl);
224  }
225 
226  out.cd();
227 
228  object->Write();
229  fcn ->Write();
230  }
231  }
232 
233  dir->Close();
234  }
235 
236  out.Write();
237  out.Close();
238 }
Utility class to parse command line options.
Definition: JParser.hh:1410
Template data structure.
Definition: JTuple.hh:45
#define WARNING(A)
Definition: JMessage.hh:63
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
int getParameter(const std::string &text)
Get parameter number from text string.
Definition: JRoot.hh:19
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:64
Auxiliary class for a type holder.
Definition: JType.hh:19
string outputFile
Data structure for track fit results.
Definition: JEvt.hh:32
Type list.
Definition: JTypeList.hh:22
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
#define NOTICE(A)
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:59
General purpose messaging.
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition: JTypeList.hh:572
#define FATAL(A)
Definition: JMessage.hh:65
Utility class to parse command line options.
Wrapper class around string.
Definition: JToken.hh:23
Data structure based on type list.
Exception for parsing value.
Definition: JException.hh:162
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
JTuple< JTypeList< JNullType, JNullType > > make_tuple(const JNullType &b, const JNullType &c, const JNullType &d, const JNullType &e, const JNullType &f, const JNullType &g, const JNullType &h, const JNullType &i, const JNullType &j, const JNullType &k, const JNullType &l, const JNullType &m, const JNullType &n, const JNullType &o, const JNullType &p, const JNullType &q, const JNullType &r, const JNullType &s, const JNullType &t, const JNullType &u, const JNullType &v, const JNullType &w, const JNullType &x, const JNullType &y, const JNullType &z)
Helper method for tuple.
Definition: JTuple.hh:724
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:498
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
int main(int argc, char *argv[])
Definition: Main.cpp:15