15 #include "TFitResult.h"
51 const std::string& option) :
67 result =
dynamic_cast<T&
>(object).
Fit(fcn, option.c_str(),
"same");
69 catch(
const std::exception&) {}
100 int main(
int argc,
char **argv)
123 JParser<> zap(
"General purpose fit program for 1D ROOT objects.");
125 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
127 zap[
'F'] =
make_field(formula,
"fit formula, e.g: \"[0]+[1]*x\"");
128 zap[
'@'] =
make_field(startValues,
"start values, e.g: \"p0 = GetMaximum;\"");
135 zap[
'O'] =
make_field(option,
"Fit option") =
"";
141 catch(
const exception &error) {
142 FATAL(error.what() << endl);
146 if (option.find(
'O') == string::npos) { option +=
"O"; }
147 if (option.find(
"R") == string::npos) { option +=
"R"; }
148 if (option.find(
"S") == string::npos) { option +=
"S"; }
150 if (
debug == 0 && option.find(
'Q') == string::npos) { option +=
"Q"; }
163 TF1* fcn =
new TF1(
"user", formula.c_str());
167 if (fcn->IsZombie()) {
168 FATAL(
"Function: " << formula <<
" is zombie." << endl);
173 DEBUG(
"Input: " << *input << endl);
178 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
182 const TRegexp regexp(input->getObjectName());
184 TIter iter(dir->GetListOfKeys());
186 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
188 const TString tag(key->GetName());
190 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
194 if (tag.Contains(regexp) &&
isTObject(key)) {
196 TObject*
object = key->ReadObj();
200 TProfile& h1 =
dynamic_cast<TProfile&
>(*object);
202 object = h1.ProjectionX();
208 TH2& h2 =
dynamic_cast<TH2&
>(*object);
212 object = h2.ProjectionX(
"_px",
213 h2.GetYaxis()->FindBin(
Y.getLowerLimit()),
214 h2.GetYaxis()->FindBin(
Y.getUpperLimit()));
218 object = h2.ProjectionY(
"_py",
219 h2.GetXaxis()->FindBin(
Y.getLowerLimit()),
220 h2.GetXaxis()->FindBin(
Y.getUpperLimit()));
224 ERROR(
"For 2D histograms, use option option -P for projections." << endl);
240 for (Int_t
i = 0;
i != fcn->GetNpar(); ++
i) {
241 fcn->SetParError (
i, 0.0);
257 FATAL(error << endl);
260 DEBUG(
"Start values " << object->GetName() << endl);
262 for (
int j = 0;
j != fcn->GetNpar(); ++
j) {
263 DEBUG(left << setw(12) << fcn->GetParName (
j) <<
' ' <<
267 Double_t
xmin = numeric_limits<Double_t>::max();
268 Double_t
xmax = numeric_limits<Double_t>::lowest();
271 TH1* h1 =
dynamic_cast<TH1*
>(object);
274 xmin = min(xmin, h1->GetXaxis()->GetXmin());
275 xmax = max(xmax, h1->GetXaxis()->GetXmax());
280 TGraph*
g1 =
dynamic_cast<TGraph*
>(object);
283 for (Int_t
i = 0;
i != g1->GetN(); ++
i) {
284 xmin = min(xmin, g1->GetX()[
i]);
285 xmax = max(xmax, g1->GetX()[
i]);
291 xmin =
X.getLowerLimit();
292 xmax =
X.getUpperLimit();
295 fcn->SetRange(xmin, xmax);
300 const chrono::steady_clock::time_point t0 = chrono::steady_clock::now();
302 JFit fit(*
object, fcn, option);
306 const chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
308 if (fit.result != -1) {
312 NOTICE(
"Fit values " << object->GetName() << endl);
313 NOTICE(
"Fit formula " << formula << endl);
314 NOTICE(
"chi2/NDF " <<
FIXED(7,3) << fit.result->Chi2() <<
'/' << fit.result->Ndf() <<
' ' << (fit.result->IsValid() ?
"" :
"failed") << endl);
315 NOTICE(
"Number of calls " << fit.result->NCalls() << endl);
316 NOTICE(
"Elapsed time [us] " << setw(8) << chrono::duration_cast<chrono::microseconds>(t1 - t0).
count() << endl);
318 for (
int j = 0;
j != fcn->GetNpar(); ++
j) {
319 NOTICE(left << setw(12) << fcn->GetParName (
j) <<
' ' <<
320 SCIENTIFIC(12,5) << fcn->GetParameter(
j) <<
" +/- " <<
326 WARNING(
"Object: not compatible with ROOT Fit." << endl);
Utility class to parse command line options.
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
int main(int argc, char *argv[])
int getParameter(const std::string &text)
Get parameter number from text string.
then usage $script< input file >[option] nPossible options count
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Auxiliary class for a type holder.
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
Auxiliary data structure for floating point format specification.
then set_variable PMT_FILE set_variable DAQ_FILE set_variable OUTPUT_FILE set_variable DETECTOR else fatal Wrong number of arguments fi JPrintTree f $DAQ_FILE type
Type definition of range.
I/O formatting auxiliaries.
static JMinimizer minimizer
ROOT minimizer.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JRootfit_t< JF1_t > Fit(const TH1 &h1, const JF1_t &f1, const index_list &ls=index_list(), const range_type &X=range_type())
Global fit fuction.
General purpose messaging.
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Auxiliary class to define a range between two values.
Utility class to parse command line options.
Wrapper class around string.
Exception for parsing value.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
no fit printf nominal n $STRING awk v X
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Auxiliary data structure to define ROOT minimizer.
Auxiliary data structure for floating point format specification.
#define DEBUG(A)
Message macros.
JFit()
Default constructor.
Double_t g1(const Double_t x)
Function.