36int main(
int argc,
char **argv)
42 double XMIN = -0.2*PI;
43 double XMAX = +0.2*PI;
64 JParser<> zap(
"Program to fit histogram in output of JMuonCompass.cc.");
67 zap[
'f'] =
make_field(inputFile,
"input files (output from JMuonCompass).");
69 zap[
'a'] =
make_field(detectorFile,
"detector file.");
70 zap[
'A'] =
make_field(overwriteDetector,
"overwrite detector file provided through '-a' with corrected orientations;"\
71 "\nUse option -AA to also correct compass calibrations.");
72 zap[
'O'] =
make_field(option,
"ROOT fit option, see TH1::Fit.") =
"";
77 catch(
const exception &error) {
78 FATAL(error.what() << endl);
82 gErrorIgnoreLevel = kFatal;
96 if (option.find(
'S') == string::npos) { option +=
'S'; }
101 for (vector<string>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
103 NOTICE(
"Processing " << *i << endl);
105 TFile in(i->c_str(),
"exist");
108 FATAL(
"File " << *i <<
" not opened." << endl);
111 TH2D* p =
dynamic_cast<TH2D*
>(in.Get(h2_t));
114 FATAL(
"File " << *i <<
" has no histogram <" << h2_t <<
">." << endl);
118 h2 = (TH2D*) p->Clone();
128 FATAL(
"No histogram <" << h2_t <<
">." << endl);
140 result_type(
double value,
157 const TAxis* x_axis = h2->GetXaxis();
158 const TAxis* y_axis = h2->GetYaxis();
160 TH1D h0(
"h0", NULL, x_axis->GetNbins(), -0.5, x_axis->GetNbins() - 0.5);
165 TH2D H2d(
"detector", NULL,
166 strings.size(), -0.5, strings.size() - 0.5,
169 for (Int_t ix = 1; ix <= H2d.GetXaxis()->GetNbins(); ++ix) {
170 H2d.GetXaxis()->SetBinLabel(ix,
MAKE_CSTRING(strings.at(ix-1)));
172 for (Int_t iy = 1; iy <= H2d.GetYaxis()->GetNbins(); ++iy) {
176 TH2D* H2c = (TH2D*) H2d.Clone(
"chi2");
177 TH2D* H2q = (TH2D*) H2d.Clone(
"status");
182 for (Int_t ix = 1; ix <= x_axis->GetNbins(); ++ix) {
184 const int index = ix - 1;
191 DEBUG(
"Bin " << setw(4) << ix <<
" -> " << setw(8) << module.
getID() << endl);
193 TH1D h1(
MAKE_CSTRING(module.
getID() <<
".1D"), NULL, y_axis->GetNbins(), y_axis->GetXmin(), y_axis->GetXmax());
197 Double_t ymin = numeric_limits<double>::max();
198 Double_t ymax = numeric_limits<double>::lowest();
201 for (Int_t iy = 1; iy <= y_axis->GetNbins(); ++iy) {
203 h1.SetBinContent(iy, h2->GetBinContent(ix,iy));
204 h1.SetBinError (iy, h2->GetEntries() * 1.0e-7 * parameters.FACTOR);
206 const Double_t x = h1.GetBinCenter (iy);
207 const Double_t y = h1.GetBinContent(iy);
213 if (y > ymax) { ymax = y; }
214 if (y < ymin) { ymin = y; }
217 if (ymax - ymin >= parameters.YMIN) {
219 Double_t xmin = x0 + parameters.XMIN;
220 Double_t xmax = x0 + parameters.XMAX;
224 if (xmin < y_axis->GetXmin()) { Rx = +PI; }
225 if (xmax > y_axis->GetXmax()) { Rx = -PI; }
231 NOTICE(
"Module " << setw(8) << module.
getID() <<
" offset " << Rx/PI <<
" [pi]" << endl);
239 const Double_t dx = (y_axis->GetXmax() - y_axis->GetXmin()) / y_axis->GetNbins();
241 for (Int_t iy = 1; iy <= y_axis->GetNbins(); ++iy) {
243 Int_t i = iy + (Int_t) (Rx / dx);
245 if (i < 1) { i += (Int_t) (2.0*PI / dx); }
246 if (i > y_axis->GetNbins()) { i -= (Int_t) (2.0*PI / dx); }
248 h1.SetBinContent(i, h2->GetBinContent(ix,iy));
249 h1.SetBinError (i, h2->GetEntries() * 1.0e-7 * parameters.FACTOR);
253 if (xmin < y_axis->GetXmin()) { xmin = y_axis->GetXmin(); xmax = xmin + 0.8*PI; }
254 if (xmax > y_axis->GetXmax()) { xmax = y_axis->GetXmax(); xmin = xmax - 0.8*PI; }
256 TF1* f1 =
new TF1(
"f1",
"[0] - [1]*cos(x - [2])");
258 f1->SetParameter(0, 0.5 * (ymin + ymax));
259 f1->SetParameter(1, 0.5 * (ymax - ymin));
260 f1->SetParameter(2, x0);
262 f1->SetParError(0, 0.01*(ymin+ymax));
263 f1->SetParError(1, 0.01*(ymin+ymax));
264 f1->SetParError(2, 0.05);
266 DEBUG(
"Start values:" << endl);
268 for (
int i = 0; i != f1->GetNpar(); ++i) {
269 DEBUG(left << setw(12) << f1->GetParName (i) <<
' ' <<
270 SCIENTIFIC(12,5) << f1->GetParameter(i) << endl);
275 TFitResultPtr result = h1.Fit(f1, option.c_str(),
"same", xmin, xmax);
277 const bool status = result->IsValid();
282 f1->SetParameter(2, f1->GetParameter(2) - Rx);
285 if (
debug >= notice_t || !result->IsValid()) {
287 << setw(4) << ix <<
' '
288 << setw(16) << h1.GetName() <<
' '
289 <<
FIXED(7,3) << f1->GetParameter(2) <<
" +/- "
290 <<
FIXED(7,3) << f1->GetParError(2) <<
" [rad] "
291 <<
FIXED(9,2) << result->Chi2() <<
'/'
292 << result->Ndf() <<
' '
294 << (status ?
"" :
"failed") << endl;
298 zmap[index] = result_type(f1->GetParameter(2), f1->GetParError(2));
310 H2d .SetBinContent(H2.ix, H2.iy, f1->GetParameter(2));
312 if (result->Ndf() > 0) {
313 H2c->SetBinContent(H2.ix, H2.iy, result->Chi2() / result->Ndf());
316 H2q->SetBinContent(H2.ix, H2.iy, status ? +1.0 : -1.0);
326 NOTICE(
"No calibration results." << endl);
329 for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
330 h0.SetBinContent(i->first, i->second.value);
331 h0.SetBinError (i->first, i->second.error);
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) {
343 const JVector3D center =
module.getPosition();
347 module.rotate(JRotation3Z(i->second.value));
351 if (overwriteDetector > 1) {
352 module.setQuaternion(module.getQuaternion() * JQuaternion3Z(i->second.value));