Jpp
Functions
JFit2D.cc File Reference
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include "TROOT.h"
#include "TFile.h"
#include "TKey.h"
#include "TH2.h"
#include "TGraph2D.h"
#include "TProfile.h"
#include "TF12.h"
#include "TString.h"
#include "TRegexp.h"
#include "JLang/JType.hh"
#include "JLang/JTypeList.hh"
#include "JLang/JToken.hh"
#include "JTools/JTuple.hh"
#include "JGizmo/JRootObjectID.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

General purpose fit program using ROOT. The option -f corresponds to <file name>:<object name>.

The expressions for the fit function, start and fixed values should comply with ROOT class TFormula.

In the expressions of the start and fixed values, names of member methods of corresponding class of the fit object may appear, such as TH1::GetMaximum, TH1::GetRMS, etc., e.g:

     -F "[0]*exp(-0.5*x*x/([1]*[1])*exp(-0.5*y*y/([2]*[2])"
     -@ "p0 = GetMaximum; p1 = 2*GetRMS"
     -= "p2 = 0"

The result of the formulas for the start and fixed values will be evaluated for each histogram separately.

Author
mdejong

Definition in file JFit2D.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 92 of file JFit2D.cc.

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.Contains(regexp) << endl);
169 
170  // option match
171 
172  if (tag.Contains(regexp)) {
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 }
TObject
Definition: JRoot.hh:19
JGIZMO::getParameter
int getParameter(const std::string &text)
Get parameter number from text string.
Definition: JGizmoToolkit.hh:288
JLANG::JType
Auxiliary class for a type holder.
Definition: JType.hh:19
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
std::vector
Definition: JSTDTypes.hh:12
JTOOLS::j
int j
Definition: JPolint.hh:634
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
NOTICE
#define NOTICE(A)
Definition: JMessage.hh:64
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
ERROR
#define ERROR(A)
Definition: JMessage.hh:66
WARNING
#define WARNING(A)
Definition: JMessage.hh:65
debug
int debug
debug level
Definition: JSirene.cc:59
JGIZMO::getDirectory
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Definition: JGizmoToolkit.hh:121
SCIENTIFIC
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:518
JLANG::for_each
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition: JTypeList.hh:572
JLANG::JTypeList
Type list.
Definition: JTypeList.hh:22
JLANG::JParseError
Exception for parsing value.
Definition: JException.hh:180
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
JLANG::JToken
Wrapper class around string.
Definition: JToken.hh:23
std
Definition: jaanetDictionary.h:36
JEEP::getValue
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37
JTOOLS::make_tuple
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
JFIT::JFit
Data structure for track fit results.
Definition: JEvt.hh:31