53 bool overwriteDetector;
63 JParser<> zap(
"Program to fit time-residuals histogram in output of JCalibrateMuon.cc.");
65 zap[
'f'] =
make_field(inputFile,
"input files (output from JCalibrateMuon).");
67 zap[
'a'] =
make_field(detectorFile,
"detector file.");
68 zap[
'A'] =
make_field(overwriteDetector,
"overwrite detector file provided through '-a' with correct time offsets.");
69 zap[
'F'] =
make_field(
function,
"fit function") = gauss_t, landau_t, emg_t, breitwigner_t;
71 zap[
'O'] =
make_field(option,
"ROOT fit option, see TH1::Fit.") =
"";
73 zap[
'W'] =
make_field(WMin,
"minimal histogram contents.") = 100.0;
78 catch(
const exception &error) {
79 FATAL(error.what() << endl);
85 if (!T_ns.is_valid()) {
86 FATAL(
"Invalid fit range [ns] " << T_ns << endl);
98 if (option.find(
'S') == string::npos) { option +=
'S'; }
106 NOTICE(
"Processing " << *i << endl);
108 TFile
in(i->c_str(),
"exist");
111 FATAL(
"File " << *i <<
" not opened." << endl);
114 TH2D* p =
dynamic_cast<TH2D*
>(
in.Get(h2_t));
117 FATAL(
"File " << *i <<
" has no histogram <" << h2_t <<
">." << endl);
121 h2 = (TH2D*) p->Clone();
131 FATAL(
"No histogram <" << h2_t <<
">." << endl);
143 result_type(
double value,
160 const TAxis* x_axis = h2->GetXaxis();
161 const TAxis* y_axis = h2->GetYaxis();
163 TH1D h0(
"h0", NULL, x_axis->GetNbins(), -0.5, x_axis->GetNbins() - 0.5);
164 TH1D hc(
"hc", NULL, x_axis->GetNbins(), -0.5, x_axis->GetNbins() - 0.5);
165 TH1D hq(
"hq", NULL, x_axis->GetNbins(), -0.5, x_axis->GetNbins() - 0.5);
170 for (
int ix = 1; ix <= x_axis->GetNbins(); ++ix) {
172 TH1D
h1(
MAKE_CSTRING((
detector.empty() ? ix :
detector[ix - 1].getID()) <<
".1D"), NULL, y_axis->GetNbins(), y_axis->GetXmin(), y_axis->GetXmax());
178 Double_t sigma = 4.5;
181 for (
int iy = 1; iy <= y_axis->GetNbins(); ++iy) {
183 h1.SetBinContent(iy, h2->GetBinContent(ix,iy));
184 h1.SetBinError (iy, sqrt(h2->GetBinContent(ix,iy)));
186 const Double_t x =
h1.GetBinCenter (iy);
187 const Double_t y =
h1.GetBinContent(iy);
199 WARNING(
"Not enough entries for slice " << ix <<
' ' << W <<
"; skip fit." << endl);
205 const Double_t xmin = x0 + T_ns.getLowerLimit();
206 const Double_t xmax = x0 + T_ns.getUpperLimit();
211 if (
function == gauss_t) {
213 f1 =
new TF1(
function.c_str(),
"[0]*TMath::Gaus(x,[1],[2])");
215 f1->SetParameter(0, ymax);
216 f1->SetParameter(1, x0);
217 f1->SetParameter(2, sigma);
219 }
else if (
function == landau_t) {
221 f1 =
new TF1(
function.c_str(),
"[0]*TMath::Landau(x,[1],[2])");
223 f1->SetParameter(0, ymax);
224 f1->SetParameter(1, x0);
225 f1->SetParameter(2, sigma);
227 }
else if (
function == emg_t) {
229 f1 =
new TF1(
function.c_str(),
"[0]*TMath::Exp(0.5*[3]*(2.0*[1]+[3]*[2]*[2]-2.0*x))*TMath::Erfc(([1]+[3]*[2]*[2]-x)/(TMath::Sqrt(2.0)*[2]))");
231 f1->SetParameter(0, ymax);
232 f1->SetParameter(1, x0 - sigma);
233 f1->SetParameter(2, sigma);
234 f1->SetParameter(3, 0.04);
236 }
else if (
function == breitwigner_t) {
238 f1 =
new TF1(
function.c_str(),
"(x <= [1])*[0]*[2]*TMath::BreitWigner(x,[1],[2])+(x > [1])*[0]*[3]*TMath::BreitWigner(x,[1],[3])");
240 f1->SetParameter(0, ymax);
241 f1->SetParameter(1, x0);
242 f1->SetParameter(2, 15.0);
243 f1->SetParameter(3, 25.0);
247 FATAL(
"Unknown fit function " <<
function << endl);
251 DEBUG(
"Start values:" << endl);
253 for (
int i = 0; i != f1->GetNpar(); ++i) {
254 DEBUG(left << setw(12) << f1->GetParName (i) <<
' ' <<
255 SCIENTIFIC(12,5) << f1->GetParameter(i) << endl);
260 TFitResultPtr
result =
h1.Fit(f1, option.c_str(),
"same", xmin, xmax);
264 << setw(4) << ix <<
' '
265 << setw(16) <<
h1.GetName() <<
' '
266 <<
FIXED(7,3) << f1->GetParameter(1) <<
" +/- "
267 <<
FIXED(7,3) << f1->GetParError(1) <<
' '
268 <<
FIXED(7,3) << result->Chi2() <<
'/'
269 << result->Ndf() <<
' '
270 << (result->IsValid() ?
"" :
"failed") << endl;
273 if (result->IsValid()) {
274 zmap[ix] = result_type(f1->GetParameter(1), f1->GetParError (1));
277 if (result->Ndf() > 0) {
278 hc.SetBinContent(ix, result->Chi2() / result->Ndf());
280 hq.SetBinContent(ix, result->IsValid() ? 1.0 : 0.0);
294 for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
295 t0 += i->second.value;
300 NOTICE(
"Average time offset [ns] " <<
FIXED(7,2) << t0 << endl);
302 for (map_type::iterator i = zmap.begin(); i != zmap.end(); ++i) {
303 i->second.value -= t0;
306 for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
307 h0.SetBinContent(i->first, i->second.value);
308 h0.SetBinError (i->first, i->second.error);
317 string.getLength() + 1,
318 string.getLowerLimit() - 0.5,
319 string.getUpperLimit() + 0.5,
320 floor.getLength() + 1,
321 floor.getLowerLimit() - 0.5,
322 floor.getUpperLimit() + 0.5);
324 for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
325 hi.SetBinContent(
detector[i->first - 1].getString(),
334 if (overwriteDetector) {
336 NOTICE(
"Store calibration data on file " << detectorFile << endl);
340 for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
342 if (E_ns(i->second.error))
343 detector[i->first - 1].sub(i->second.value);
345 ERROR(
"Slice " << setw(4) << i->first <<
" fit uncertainty " <<
FIXED(5,2) << i->second.error <<
" outside specified range (option -E <E_ns>)" << endl);
353 NOTICE(
"No calibration results." << endl);
Utility class to parse command line options.
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
#define MAKE_CSTRING(A)
Make C-string.
then for HISTOGRAM in h0 h1
Auxiliary data structure for floating point format specification.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
void store(const JString &file_name, const JDetector &detector)
Store detector to output file.
Auxiliary data structure for floating point format specification.
#define DEBUG(A)
Message macros.