36int main(
int argc,
char **argv)
49 bool overwriteDetector;
60 JParser<> zap(
"Program to fit histogram in output of JMuonCompass.cc.");
63 zap[
'f'] =
make_field(inputFile,
"input files (output from JMuonCompass).");
65 zap[
'a'] =
make_field(detectorFile,
"detector file.");
66 zap[
'A'] =
make_field(overwriteDetector,
"overwrite detector file provided through '-a' with correct time offsets.");
67 zap[
'O'] =
make_field(option,
"ROOT fit option, see TH1::Fit.") =
"";
72 catch(
const exception &error) {
73 FATAL(error.what() << endl);
77 gErrorIgnoreLevel = kFatal;
91 if (option.find(
'S') == string::npos) { option +=
'S'; }
96 for (vector<string>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
98 NOTICE(
"Processing " << *i << endl);
100 TFile in(i->c_str(),
"exist");
103 FATAL(
"File " << *i <<
" not opened." << endl);
106 TH2D* p =
dynamic_cast<TH2D*
>(in.Get(h2_t));
109 FATAL(
"File " << *i <<
" has no histogram <" << h2_t <<
">." << endl);
113 h2 = (TH2D*) p->Clone();
123 FATAL(
"No histogram <" << h2_t <<
">." << endl);
135 result_type(
double value,
152 const TAxis* x_axis = h2->GetXaxis();
153 const TAxis* y_axis = h2->GetYaxis();
155 TH1D h0(
"h0", NULL, x_axis->GetNbins(), -0.5, x_axis->GetNbins() - 0.5);
160 TH2D H2d(
"detector", NULL,
161 strings.size(), -0.5, strings.size() - 0.5,
164 for (Int_t ix = 1; ix <= H2d.GetXaxis()->GetNbins(); ++ix) {
165 H2d.GetXaxis()->SetBinLabel(ix,
MAKE_CSTRING(strings.at(ix-1)));
167 for (Int_t iy = 1; iy <= H2d.GetYaxis()->GetNbins(); ++iy) {
171 TH2D* H2c = (TH2D*) H2d.Clone(
"chi2");
172 TH2D* H2q = (TH2D*) H2d.Clone(
"status");
177 for (Int_t ix = 1; ix <= x_axis->GetNbins(); ++ix) {
179 const int index = ix - 1;
186 DEBUG(
"Bin " << setw(4) << ix <<
" -> " << setw(8) << module.
getID() << endl);
188 TH1D h1(
MAKE_CSTRING(module.
getID() <<
".1D"), NULL, y_axis->GetNbins(), y_axis->GetXmin(), y_axis->GetXmax());
192 Double_t ymin = numeric_limits<double>::max();
193 Double_t ymax = numeric_limits<double>::lowest();
196 for (Int_t iy = 1; iy <= y_axis->GetNbins(); ++iy) {
198 h1.SetBinContent(iy, h2->GetBinContent(ix,iy));
199 h1.SetBinError (iy, h2->GetEntries() * 1.0e-7 * parameters.FACTOR);
201 const Double_t x = h1.GetBinCenter (iy);
202 const Double_t y = h1.GetBinContent(iy);
208 if (y > ymax) { ymax = y; }
209 if (y < ymin) { ymin = y; }
212 if (ymax - ymin >= parameters.YMIN) {
214 Double_t xmin = x0 - 0.4*PI;
215 Double_t xmax = x0 + 0.4*PI;
217 if (xmin < y_axis->GetXmin()) { xmin = y_axis->GetXmin(); xmax = xmin + 0.8*PI; }
218 if (xmax > y_axis->GetXmax()) { xmax = y_axis->GetXmax(); xmin = xmax - 0.8*PI; }
220 TF1* f1 =
new TF1(
"f1",
"[0] - [1]*cos(x - [2])");
222 f1->SetParameter(0, 0.5 * (ymin + ymax));
223 f1->SetParameter(1, 0.5 * (ymax - ymin));
224 f1->SetParameter(2, x0);
226 f1->SetParError(0, 0.01*(ymin+ymax));
227 f1->SetParError(1, 0.01*(ymin+ymax));
228 f1->SetParError(2, 0.05);
230 DEBUG(
"Start values:" << endl);
232 for (
int i = 0; i != f1->GetNpar(); ++i) {
233 DEBUG(left << setw(12) << f1->GetParName (i) <<
' ' <<
234 SCIENTIFIC(12,5) << f1->GetParameter(i) << endl);
239 TFitResultPtr result = h1.Fit(f1, option.c_str(),
"same", xmin, xmax);
241 const bool status = result->IsValid();
243 if (
debug >= notice_t || !result->IsValid()) {
245 << setw(4) << ix <<
' '
246 << setw(16) << h1.GetName() <<
' '
247 <<
FIXED(7,3) << f1->GetParameter(2) <<
" +/- "
248 <<
FIXED(7,3) << f1->GetParError(2) <<
' '
249 <<
FIXED(9,2) << result->Chi2() <<
'/'
250 << result->Ndf() <<
' '
252 << (status ?
"" :
"failed") << endl;
256 zmap[index] = result_type(f1->GetParameter(2), f1->GetParError(2));
268 H2d .SetBinContent(H2.ix, H2.iy, f1->GetParameter(2));
270 if (result->Ndf() > 0) {
271 H2c->SetBinContent(H2.ix, H2.iy, result->Chi2() / result->Ndf());
274 H2q->SetBinContent(H2.ix, H2.iy, status ? +1.0 : -1.0);
284 NOTICE(
"No calibration results." << endl);
287 for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
288 h0.SetBinContent(i->first, i->second.value);
289 h0.SetBinError (i->first, i->second.error);
292 if (overwriteDetector) {
294 NOTICE(
"Store calibration data on file " << detectorFile << endl);
298 for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
301 const JVector3D center =
module.getPosition();
305 module.rotate(JRotation3Z(i->second.value));