Jpp  pmt_effective_area_update_2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JFit2D.cc File Reference

General purpose fit program using ROOT. More...

#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/JRange.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

int main ( int  argc,
char **  argv 
)

Definition at line 96 of file JFit2D.cc.

97 {
98  using namespace std;
99  using namespace JPP;
100 
101  typedef JToken<';'> JToken_t;
102  typedef JRange<double> JRange_t;
103 
104  vector<JRootObjectID> inputFile;
105  string outputFile;
106  string formula;
107  vector<JToken_t> startValues;
108  vector<JToken_t> fixedValues;
109  vector<JToken_t> limitValues;
110  JRange_t X;
111  JRange_t Y;
112  string option;
113  int debug;
114 
115  try {
116 
117  JParser<> zap("General purpose fit program using ROOT.");
118 
119  zap['f'] = make_field(inputFile, "<input file>:<object name>");
120  zap['o'] = make_field(outputFile, "ROOT file with fit results") = "fit.root";
121  zap['F'] = make_field(formula, "fit formula, e.g: \"[0]+[1]*x\"");
122  zap['@'] = make_field(startValues, "start values, e.g: \"p0 = GetMaximum;\"");
123  zap['='] = make_field(fixedValues, "fixed values, e.g: \"p0 = GetMaximum;\"");
124  zap['R'] = make_field(limitValues, "limit values, e.g: \"p0 = <lower limit> <upper limit>;\"");
125  zap['x'] = make_field(X, "abscissa range") = JRange_t();
126  zap['y'] = make_field(Y, "abscissa range") = JRange_t();
127  zap['O'] = make_field(option, "Fit option") = "";
128  zap['d'] = make_field(debug) = 1;
129 
130  zap['='] = JPARSER::initialised();
131 
132  zap(argc, argv);
133  }
134  catch(const exception &error) {
135  FATAL(error.what() << endl);
136  }
137 
138 
139  if (option.find('O') == string::npos) { option += "O"; }
140  //if (option.find('N') == string::npos) { option += "N"; }
141  if (debug == 0 && option.find('Q') == string::npos) { option += "Q"; }
142 
143 
144  TFile out(outputFile.c_str(), "recreate");
145 
146 
147  TF2* fcn = new TF2("user", formula.c_str());
148 
149  fcn->SetNpx(10000);
150 
151  if (fcn->IsZombie()) {
152  FATAL("Function: " << formula << " is zombie." << endl);
153  }
154 
155  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
156 
157  DEBUG("Input: " << *input << endl);
158 
159  TDirectory* dir = getDirectory(*input);
160 
161  if (dir == NULL) {
162  ERROR("File: " << input->getFullFilename() << " not opened." << endl);
163  continue;
164  }
165 
166  const TRegexp regexp(input->getObjectName());
167 
168  TIter iter(dir->GetListOfKeys());
169 
170  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
171 
172  const TString tag(key->GetName());
173 
174  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
175 
176  // option match
177 
178  if (tag.Contains(regexp)) {
179 
180  TObject* object = key->ReadObj();
181 
182 
183  // set fit parameters
184 
185  try {
186 
187  for (vector<JToken_t>::const_iterator i = startValues.begin(); i != startValues.end(); ++i) {
188  fcn->SetParameter(getParameter(*i), getValue(*i,object));
189  }
190 
191  for (vector<JToken_t>::const_iterator i = fixedValues.begin(); i != fixedValues.end(); ++i) {
192  fcn->FixParameter(getParameter(*i), getValue(*i,object));
193  }
194 
195  for (vector<JToken_t>::const_iterator i = limitValues.begin(); i != limitValues.end(); ++i) {
196  fcn->SetParLimits(getParameter(*i), getValue(*i,0), getValue(*i,1));
197  }
198  }
199  catch(JLANG::JParseError& error) {
200  FATAL(error << endl);
201  }
202 
203  DEBUG("Start values " << object->GetName() << endl);
204 
205  for (int i = 0; i != fcn->GetNpar(); ++i) {
206  DEBUG(left << setw(12) << fcn->GetParName (i) << ' ' <<
207  SCIENTIFIC(12,5) << fcn->GetParameter(i) << endl);
208  }
209 
210  Double_t xmin = numeric_limits<Double_t>::max();
211  Double_t xmax = numeric_limits<Double_t>::lowest();
212  Double_t ymin = numeric_limits<Double_t>::max();
213  Double_t ymax = numeric_limits<Double_t>::lowest();
214 
215  {
216  TH2* h2 = dynamic_cast<TH2*>(object);
217 
218  if (h2 != NULL) {
219  xmin = min(xmin, h2->GetXaxis()->GetXmin());
220  xmax = max(xmax, h2->GetXaxis()->GetXmax());
221  ymin = min(ymin, h2->GetYaxis()->GetXmin());
222  ymax = max(ymax, h2->GetYaxis()->GetXmax());
223  }
224  }
225 
226  {
227  TGraph2D* g2 = dynamic_cast<TGraph2D*>(object);
228 
229  if (g2 != NULL) {
230  for (Int_t i = 0; i != g2->GetN(); ++i) {
231  xmin = min(xmin, g2->GetX()[i]);
232  xmax = max(xmax, g2->GetX()[i]);
233  ymin = min(ymin, g2->GetY()[i]);
234  ymax = max(ymax, g2->GetY()[i]);
235  }
236  }
237  }
238 
239  if (X != JRange_t()) {
240  xmin = X.getLowerLimit();
241  xmax = X.getUpperLimit();
242  }
243 
244  if (Y != JRange_t()) {
245  ymin = Y.getLowerLimit();
246  ymax = Y.getUpperLimit();
247  }
248 
249  fcn->SetRange(xmin, ymin, xmax, ymax);
250 
251  if (option.find("R") == string::npos) {
252  option += "R";
253  }
254 
255 
256  // execute fit
257 
258  JFit fit(*object, fcn, option);
259 
261 
262 
263  if (fit.result != -1) {
264 
265  // output fit results
266 
267  NOTICE("Fit values " << object->GetName() << endl);
268  NOTICE("Fit formula " << formula << endl);
269 
270  for (int j = 0; j != fcn->GetNpar(); ++j) {
271  NOTICE(left << setw(12) << fcn->GetParName (j) << ' ' <<
272  SCIENTIFIC(12,5) << fcn->GetParameter(j) << " +/- " <<
273  SCIENTIFIC(12,5) << fcn->GetParError (j) << endl);
274  }
275 
276  } else {
277 
278  WARNING("Object: not compatible with ROOT Fit." << endl);
279  }
280 
281  out.cd();
282 
283  object->Write();
284  fcn ->Write();
285  }
286  }
287 
288  dir->Close();
289  }
290 
291  out.Write();
292  out.Close();
293 }
Utility class to parse command line options.
Definition: JParser.hh:1500
#define WARNING(A)
Definition: JMessage.hh:65
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. does not require input)...
Definition: JParser.hh:66
Auxiliary class for a type holder.
Definition: JType.hh:19
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
string outputFile
Acoustic single fit.
Type list.
Definition: JTypeList.hh:22
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
then break fi done getCenter read X Y Z let X
int debug
debug level
Definition: JSirene.cc:63
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition: JTypeList.hh:415
#define FATAL(A)
Definition: JMessage.hh:67
Wrapper class around string.
Definition: JToken.hh:23
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
Exception for parsing value.
Definition: JException.hh:180
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
int j
Definition: JPolint.hh:666
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484