CMS 3D CMS Logo

Fit.cc
Go to the documentation of this file.
2 // COCOA class implementation file
3 //Id: Fit.cc
4 //CAT: Fit
5 //
6 // History: v1.0
7 // Pedro Arce
8 
9 #include <cstdlib>
10 #include <iomanip>
11 #include <cmath> // among others include also floating-point std::abs functions
12 #include <ctime>
13 #include <set>
14 
18 
28 #ifdef COCOA_VIS
29 #include "Alignment/CocoaVisMgr/interface/ALIVRMLMgr.h"
30 #include "Alignment/IgCocoaFileWriter/interface/IgCocoaFileMgr.h"
31 #endif
38 
39 Fit* Fit::instance = nullptr;
40 
45 //op ALIMatrix* Fit::VaMatrix;
47 //op ALIMatrix* Fit::PDMatrix;
48 //-ALIMatrix* Fit::VyMatrix;
50 //ALIMatrix* Fit::fMatrix;
51 
54 //op ALIMatrix* Fit::thePropagationMatrix;
55 
63 
65 
66 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
67 //@@ Gets the only instance of Model
68 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
70  if (!instance) {
71  instance = new Fit;
72  ALIdouble go;
74 
75  gomgr->getGlobalOptionValue("maxDeviDerivative", go);
77  if (ALIUtils::debug >= 3)
78  std::cout << " Fit::maximum_deviation_derivative " << ALIUtils::getMaximumDeviationDerivative() << std::endl;
79 
80  gomgr->getGlobalOptionValue("maxNoFitIterations", go);
81  MaxNoFitIterations = int(go);
82 
83  gomgr->getGlobalOptionValue("fitQualityCut", go);
84  theFitQualityCut = go;
85  if (ALIUtils::debug >= 3)
86  std::cout << " theFitQualityCut " << theFitQualityCut << std::endl;
87 
88  gomgr->getGlobalOptionValue("RelativeFitQualityCut", go);
90  if (ALIUtils::debug >= 3)
91  std::cout << " theRelativeFitQualityCut " << theRelativeFitQualityCut << std::endl;
92 
93  gomgr->getGlobalOptionValue("minDaFactor", go);
94  theMinDaFactor = go;
95  }
96 
97  return *instance;
98 }
99 
100 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
101 //@@ startFit: steering method to make the fit
102 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
104  // Model::setCocoaStatus( COCOA_InitFit );
106  if (GlobalOptionMgr::getInstance()->GlobalOptions()["rootResults"] > 0) {
107  NTmgr->BookNtuple();
108  }
109 
111 
113 
115  for (;;) {
116  bool bend = fitNextEvent(nEvent);
117  if (gomgr->GlobalOptions()["writeDBOptAlign"] > 0 || gomgr->GlobalOptions()["writeDBAlign"] > 0) {
119  }
120 
121  if (!bend) {
122  if (ALIUtils::debug >= 1)
123  std::cout << "@@@ Fit::startFit ended n events = " << nEvent << std::endl;
124  break;
125  }
126 
127  //- if ( ALIUtils::debug >= 0) std::cout << " FIT STATUS " << Model::printCocoaStatus( Model::getCocoaStatus() ) << std::endl;
128 
129  nEvent++;
130  }
131 
132  //---------- Program ended, fill histograms of fitted entries
133  if (gomgr->GlobalOptions()["histograms"] > 0) {
135  FEmgr->MakeHistos();
136  }
137 
138  if (GlobalOptionMgr::getInstance()->GlobalOptions()["rootResults"] > 0) {
139  NTmgr->WriteNtuple();
140  }
141 }
142 
143 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
145  if (Model::getFittedEntriesReader() != nullptr)
147 
148  //----- Reset coordinates to those read at the start
149  std::vector<OpticalObject*>::iterator voite;
150  for (voite = Model::OptOList().begin(); voite != Model::OptOList().end(); ++voite) {
151  (*voite)->resetOriginalOriginalCoordinates();
152  }
153 
154  //----- Reset entries displacements to 0.
155  std::vector<Entry*>::iterator veite;
156  for (veite = Model::EntryList().begin(); veite != Model::EntryList().end(); ++veite) {
157  (*veite)->resetValueDisplacementByFitting();
158  }
159 
160  ALIbool lastEvent = false;
161 
162  //- DeviationsFromFileSensor2D::setApply( 1 );
163 
164  //m ALIbool moreDataSets = Model::readMeasurementsFromFile( Measurement::only1Date, Measurement::only1Time );
165 
166  //----- Check if there are more data sets
167  ALIbool moreDataSets = true;
168  if (CocoaDaqReader::GetDaqReader() != nullptr)
169  moreDataSets = CocoaDaqReader::GetDaqReader()->ReadNextEvent();
170 
171  if (ALIUtils::debug >= 5)
172  std::cout << CocoaDaqReader::GetDaqReader() << "$$$$$$$$$$$$$$$ More Data Sets to be processed: " << moreDataSets
173  << std::endl;
174 
175  if (moreDataSets) {
176  if (ALIUtils::debug >= 2)
177  std::cout << std::endl << "@@@@@@@@@@@@@@@@@@ Starting data set fit : " << nEvent << std::endl;
178 
179  //----- Count entries to be fitted, and set their order in theFitPos
181 
182  //----- Dump dimensions of output in 'report.out' file
184  fileout << std::endl << "@@@@@@@ NEW MEASUREMENT SET " << nEvent << std::endl;
185  if (ALIUtils::report >= 1)
186  ALIUtils::dumpDimensions(fileout);
187 
188  //----- reset Number of iterations of non linear fit
189  theNoFitIterations = 0;
190 
192  ALIdouble dumpMat;
193  gomgr->getGlobalOptionValue("save_matrices", dumpMat);
194 
195  //----- Fit parameters
196  double daFactor = 1.;
198  for (;;) {
199  if (ALIUtils::debug >= 2) {
200  std::cout << std::endl << "Fit iteration " << theNoFitIterations << " ..." << std::endl;
201  }
202 
203  //---------- Calculate the original simulated values of each Measurement (when all entries have their read in values)
204  calculateSimulatedMeasurementsWithOriginalValues(); //?? original changed atfer each iteration
205 
206  FitQuality fq = fitParameters(daFactor);
207  if (dumpMat > 1)
208  dumpMatrices();
209 
210  //- evaluateFitQuality( fq, daFactor );
211 
212  if (ALIUtils::debug >= 2) {
213  std::cout << std::endl << "@@@@ Check fit quality for iteration " << theNoFitIterations << std::endl;
214  }
215 
216  //----- Check if new iteration must be done
217  if (fq == FQsmallDistanceToMinimum) {
218  if (ALIUtils::debug >= 2)
219  std::cout << std::endl << "@@@@ Fit quality: distance SMALLER than mininum " << std::endl;
221  if (ALIUtils::report >= 1)
223  //--- Print entries in all ancestor frames
224  ALIdouble go;
225  gomgr->getGlobalOptionValue("dumpInAllFrames", go);
226  if (go >= 1)
228 
229  break; // No more iterations
230  } else if (fq == FQbigDistanceToMinimum) {
231  if (ALIUtils::debug >= 2)
232  std::cout << std::endl << "@@@@ Fit quality: distance BIGGER than mininum " << std::endl;
234  if (ALIUtils::report >= 1)
236 
237  //----- Next iteration (if not too many already)
239  daFactor = 1.;
240 
241  //----- Too many iterations: end event here
243  if (ALIUtils::debug >= 1)
244  std::cerr << "!!!! WARNING: Too many iterations " << theNoFitIterations << " and fit DOES NOT CONVERGE "
245  << std::endl;
246 
247  if (ALIUtils::report >= 2) {
249  fileout << "!!!! WARNING: Too many iterations " << theNoFitIterations << " and fit DOES NOT CONVERGE "
250  << std::endl;
251  }
252  // Model::setCocoaStatus( COCOA_FitCannotImprove );
253  break; // No more iterations
254  }
255 
256  } else if (fq == FQchiSquareWorsened) {
257  if (ALIUtils::debug >= 1) {
258  //----- Recalculate fit quality with decreasing values of Da
259  std::cerr << "!! WARNING: fit quality has worsened, Recalculate fit quality with decreasing values of Da "
260  << std::endl;
261  std::cout << " quality daFactor= " << daFactor << " minimum= " << theMinDaFactor << std::endl;
262  }
263  daFactor *= 0.5;
264  if (daFactor > theMinDaFactor) {
266 
267  if (ALIUtils::report >= 2) {
269  fileout << " Redoing iteration with Da factor " << daFactor << std::endl;
270  }
271  } else {
272  daFactor *= 2.;
273  std::cerr << " !!!ERROR: not possible to get good fit quality even multiplying Da by " << daFactor
274  << std::endl;
275  if (ALIUtils::report >= 2) {
277  fileout << " !!!ERROR: not possible to get good fit quality even multiplying Da by " << daFactor
278  << std::endl;
279  }
280  // Model::setCocoaStatus( COCOA_FitCannotImprove );
281  //- std::cout << "fdsaf FIT STATUS " << Model::printCocoaStatus( Model::getCocoaStatus() ) << std::endl;
282  break; // No more iterations
283  }
284  }
286  }
287 
288  //----- Iteration is finished: dump fitted entries
289  if (ALIUtils::debug >= 1)
291  if (gomgr->GlobalOptions()["histograms"] > 0) {
293  }
294 
295  if (GlobalOptionMgr::getInstance()->GlobalOptions()["rootResults"] > 0) {
297  ntupleMgr->InitNtuple();
298  ntupleMgr->FillChi2();
299  ntupleMgr->FillOptObjects(AtWAMatrix);
300  ntupleMgr->FillMeasurements();
301  ntupleMgr->FillFitParameters(AtWAMatrix);
302  ntupleMgr->FillNtupleTree();
303  }
304 
305  //- only if not stopped in worsening quality state if(ALIUtils::report >= 0) dumpFittedValues( ALIFileOut::getInstance( Model::ReportFName() ));
306 
307  /*- std::vector< OpticalObject* >::iterator voite;
308  for( voite = Model::OptOList().begin(); voite != Model::OptOList().end(); voite++ ) {
309  //-?? (*voite)->resetOriginalOriginalCoordinates();
310  }*/
311 
312  //---- If no measurement file, break after looping once
313  //- std::cout << " Measurement::measurementsFileName() " << Measurement::measurementsFileName() << " Measurement::measurementsFileName()" <<std::endl;
314  if (CocoaDaqReader::GetDaqReader() == nullptr) {
315  //m if( Measurement::measurementsFileName() == "" ) {
316  if (ALIUtils::debug >= 1)
317  std::cout << std::endl << "@@@@@@@@@@@@@@@@@@ Fit has ended : only one measurement " << nEvent << std::endl;
318  lastEvent = true;
319  return !lastEvent;
320  }
321 
322  //- std::cout << " Measurement::only1" << Measurement::only1 << std::endl;
323  if (Measurement::only1) {
324  if (ALIUtils::debug >= 1)
325  std::cout << std::endl << "@@@@@@@@@@@@@@@@@@ Fit has ended : 'Measurement::only1' is set" << std::endl;
326 
327  lastEvent = true;
328  return !lastEvent;
329  }
330 
331  if (GlobalOptionMgr::getInstance()->GlobalOptions()["maxEvents"] <= nEvent) {
332  if (ALIUtils::debug >= 1)
333  std::cout << std::endl
334  << "@@@@@@@@@@@@@@@@@@ Fit has ended : 'Number of events exhausted " << nEvent << std::endl;
335 
336  lastEvent = true;
337  return !lastEvent;
338  }
339 
340  } else {
341  lastEvent = true;
342  if (ALIUtils::debug >= 1)
343  std::cout << std::endl << "@@@@@@@@@@@@@@@@@@ Fit has ended : ??no more data sets' " << nEvent << std::endl;
344  return !lastEvent;
345  }
346 
347  if (ALIUtils::debug >= 1)
348  std::cout << std::endl << "@@@@@@@@@@@@@@@@@@ Fit has ended : " << nEvent << std::endl;
349 
350  return !lastEvent;
351 }
352 
353 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
355 #ifdef COCOA_VIS
356  if (gomgr->GlobalOptions()["VisOnly"] == 1) {
357  calculateSimulatedMeasurementsWithOriginalValues(); //?? original changed atfer each iteration
358  }
359 
361  if (gomgr->GlobalOptions()["VisWriteVRML"] > 0) {
363  ALIVRMLMgr::getInstance().writeFile();
364  }
365  if (gomgr->GlobalOptions()["VisWriteIguana"] > 0) {
367  IgCocoaFileMgr::getInstance().writeFile();
368  }
369 
370  if (gomgr->GlobalOptions()["VisOnly"] == 1) {
371  if (ALIUtils::debug >= 1)
372  std::cout << " Visualiation file(s) succesfully written. Ending.... " << std::endl;
373  exit(1);
374  }
375 #endif
376 }
377 
378 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
379 //@@ Count how many entries are going to be fitted (have quality >= theMinimumEntryQuality)
380 //@@ Set for this entries the value of theFitPos
381 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
383  std::vector<Entry*>::const_iterator vecite;
384 
386  theMinimumEntryQuality = int(gomgr->GlobalOptions()[ALIstring("calcul_type")]) + 1;
387  if (ALIUtils::debug >= 3)
388  std::cout << "@@@ Fit::setFittableEntries: total Entry List size= " << Model::EntryList().size() << std::endl;
389 
390  int No_entry_to_fit = 0;
391  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
392  // Number the parameters that are going to be fitted
393  if ((*vecite)->quality() >= theMinimumEntryQuality) {
394  (*vecite)->setFitPos(No_entry_to_fit);
395  if (ALIUtils::debug >= 4)
396  std::cout << " Entry To Fit= " << No_entry_to_fit << " " << (*vecite)->OptOCurrent()->name() << " "
397  << (*vecite)->name() << " with quality= " << (*vecite)->quality() << std::endl;
398  No_entry_to_fit++;
399  }
400  }
401 }
402 
403 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
404 //@@ Main method in class Fit
405 //@@ fitParameters: get the parameters through the chi square fit
406 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
407 FitQuality Fit::fitParameters(const double daFactor) {
408  if (ALIUtils::debug >= 3)
409  std::cout << "@@@ Fit::fitParameters: Fit quality daFactor " << daFactor << std::endl;
410 
411  redoMatrices();
412 
413  //---- Get chi2 of first iteration
416 
418  if (gomgr->GlobalOptions()[ALIstring("stopAfter1stIteration")] == 1) {
419  std::cout << "@!! STOPPED by user after 1st iteration " << std::endl;
420  exit(1);
421  }
422  }
423 
424  /* //---------- Open output file
425  if( ALIUtils::report >= 1 ) {
426  ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
427  //t fileout << " REPORT.OUT " << std::endl;
428  //t ALIUtils::dumpDimensions( fileout );
429  fileout << std::endl << "Fit iteration " << theNoFitIterations << " ..." << std::endl;
430  }*/
431 
432  //- std::cout << "2 FIT STATUS " << Model::printCocoaStatus( Model::getCocoaStatus() ) << std::endl;
433 
434  if (ALIUtils::debug >= 10) {
435  std::cout << std::endl << " End fitParameters " << theNoFitIterations << " ..." << std::endl;
436  }
437 
438  return getFitQuality();
439 }
440 
441 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
443  if (ALIUtils::debug >= 3)
444  std::cout << "@@@ Fit::redoMatrices" << std::endl;
445 
446  deleteMatrices();
447 
449 
450  PropagateErrors();
451 }
452 
453 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
454 //@@ Propagate the Errors from the entries to the measurements
455 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
456 //cocoaStatus
458  if (ALIUtils::debug >= 3)
459  std::cout << "@@@ Fit::PropagateErrors" << std::endl;
460 
461  //----- Create empty matrices of appropiate size
462  CreateMatrices();
463 
464  //---- count running time
465  time_t now;
466  now = clock();
467  if (ALIUtils::debug >= 2)
468  std::cout << "TIME:CREATE_MAT : " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
470 
471  //----- Fill the A, W & y matrices with the measurements
473 
474  //---- count running time
475  now = clock();
476  if (ALIUtils::debug >= 2)
477  std::cout << "TIME:MAT_MEAS_FILLED: " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
479 
480  //----- Fill the A, W & y matrices with the calibrated parameters
482  if (gomgr->GlobalOptions()[ALIstring("calcul_type")] == 0) {
484 
485  //---- count running time
486  now = clock();
487  if (ALIUtils::debug >= 0)
488  std::cout << "TIME:MAT_CAL_FILLED : " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
490  }
491 
492  //----- Put by hand some correlations if known previously
494 
495  if (ALIUtils::debug >= 3)
496  WMatrix->Dump("WMatrix before inverse");
497 
498  //----- Check first that matrix can be inverted
499  if (m_norm1(WMatrix->MatNonConst()) == 0) {
500  // Model::setCocoaStatus( COCOA_FitMatrixNonInversable );
501  return; // Model::getCocoaStatus();
502  } else {
503  WMatrix->inverse();
504  }
505 
506  if (ALIUtils::debug >= 3)
507  AMatrix->Dump("AMatrix");
508  if (ALIUtils::debug >= 3)
509  WMatrix->Dump("WMatrix");
510  if (ALIUtils::debug >= 3)
511  yfMatrix->Dump("yfMatrix");
512 
513  if (gomgr->GlobalOptions()["onlyDeriv"] >= 1) {
514  std::cout << "ENDING after derivatives are calculated ('onlyDeriv' option set)" << std::endl;
515  exit(1);
516  }
517 
519 
520  now = clock();
521  if (ALIUtils::debug >= 0)
522  std::cout << "TIME:MAT_MULTIPLIED : " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
524 
525  if (ALIUtils::getFirstTime() == 1)
526  ALIUtils::setFirstTime(false);
527 }
528 
529 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
530 //@@ Calculate the simulated value of each Measurement propagating the LightRay when all the entries have their original values
531 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
533  // if( ALIUtils::debug >= 4) OpticalObjectMgr::getInstance()->dumpOptOs();
534 
535  //---------- Set DeviationsFromFileSensor2D::apply true
537 
538  if (ALIUtils::debug >= 3)
539  std::cout << "@@@ Fit::calculateSimulatedMeasurementsWithOriginalValues" << std::endl;
540  //---------- Loop Measurements
541  std::vector<Measurement*>::const_iterator vmcite;
542  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
543  //----- Calculate Simulated Value Original
544  (*vmcite)->calculateOriginalSimulatedValue();
545  }
546 
547  //---------- Set DeviationsFromFileSensor2D::apply false
548  // It cannot be applied when calculating derivatives, because after a displacement the laser could hit another square in matrix and then cause a big step in the derivative
550 }
551 
552 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
554  //delete matrices created in previous iteration
555  delete DaMatrix;
556  delete AMatrix;
557  delete WMatrix;
558  delete yfMatrix;
559  //op delete fMatrix;
560  delete AtMatrix;
561  delete AtWAMatrix;
562  //op delete VaMatrix;
563  //- delete VyMatrix;
564  //op delete PDMatrix;
565 }
566 
567 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
568 //@@ Calculate the NoLines & NoColumns and create matrices
569 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
571  //---------- Count number of measurements
572  ALIint NoMeas = 0;
573  std::vector<Measurement*>::const_iterator vmcite;
574  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
575  NoMeas += (*vmcite)->dim();
576  }
577  if (ALIUtils::debug >= 9)
578  std::cout << "NOMEAS" << NoMeas << std::endl;
579 
580  //-------- Count number of 'cal'ibrated parameters
581  ALIint nEnt_cal = 0;
582  ALIint noent = 0;
583  //- std::cout << Model::EntryList().size() << std::endl;
585  if (gomgr->GlobalOptions()["calcul_type"] == 0) { // fit also 'cal' parameters
586  //- if( ALIUtils::debug >= 9) std::cout << "NOENTCALLL " << nEnt_cal << std::endl;
587  if (ALIUtils::debug >= 5)
588  std::cout << " Count number of 'cal'ibrated parameters " << std::endl;
589  std::vector<Entry*>::iterator veite;
590  for (veite = Model::EntryList().begin(); veite != Model::EntryList().end(); ++veite) {
591  if ((*veite)->quality() == 1)
592  nEnt_cal++;
593  noent++;
594  if (ALIUtils::debug >= 6) {
595  std::cout << (*veite)->quality() << " " << (*veite)->OptOCurrent()->name() << " " << (*veite)->name()
596  << " # ENT CAL " << nEnt_cal << " # ENT " << noent << std::endl;
597  }
598  }
599  }
600 
601  //---------- Count number parameters to be fitted ('cal' + 'unk')
602  ALIint NoParamFit = 0;
603  std::vector<Entry*>::const_iterator vecite;
604  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
605  if ((*vecite)->quality() >= theMinimumEntryQuality) {
606  NoParamFit++;
607  if (ALIUtils::debug >= 99)
608  std::cout << (*vecite)->quality() << (*vecite)->OptOCurrent()->name() << (*vecite)->name() << "NoParamFit"
609  << NoParamFit << std::endl;
610  // break;
611  }
612  }
613 
614  //---------- Create Matrices
615  ALIint NoLinesA = NoMeas + nEnt_cal;
616  ALIint NoColumnsA = NoParamFit;
617  AMatrix = new ALIMatrix(NoLinesA, NoColumnsA);
618 
619  ALIint NoLinesW = NoLinesA;
620  ALIint NoColumnsW = NoLinesA;
621  WMatrix = new ALIMatrix(NoLinesW, NoColumnsW);
622 
623  ALIint NoLinesY = NoLinesA;
624  //op yMatrix = new ALIMatrix( NoLinesY, 1 );
625  yfMatrix = new ALIMatrix(NoLinesY, 1);
626 
627  //op fMatrix = new ALIMatrix( NoLinesY, 1 );
628 
629  if (ALIUtils::debug >= 4)
630  std::cout << "CreateMatrices: NoLinesA = " << NoLinesA << " NoColumnsA = " << NoColumnsA << std::endl;
631 }
632 
633 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
634 //@@ Loop Measurements:
635 //@@ Fill Matrix A with derivatives respect to affecting entries
636 //@@ Fill Matrix W, y & f with values and sigmas of measurement coordinate
637 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
639  if (ALIUtils::debug >= 3)
640  std::cout << "@@@ Fit::FillMatricesWithMeasurements" << std::endl;
641 
642  int Aline = 0;
643 
644  //---------- Loop Measurements
645  std::vector<Measurement*>::const_iterator vmcite;
646  std::vector<Entry*>::const_iterator vecite;
647  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
648  if (ALIUtils::debug >= 5)
649  std::cout << "FillMatricesWithMeasurements: measurement " << (*vmcite)->name() << " # entries affecting "
650  << (*vmcite)->affectingEntryList().size() << std::endl;
651 
652  //-------- Array of derivatives with respect to entries
653  ALIint measdim = (*vmcite)->dim();
654  std::vector<ALIdouble> derivRE;
655  // derivRE = new ALIdouble[measdim];
656 
657  //-------- Fill matrix A:
658  //------ Loop only Entries affecting this Measurement
659  //-std::cout << "number affecting entries: " << (*vmcite)->affectingEntryList().size() << std::endl;
660  for (vecite = (*vmcite)->affectingEntryList().begin(); vecite != (*vmcite)->affectingEntryList().end(); ++vecite) {
661  //-------- If good quality, get derivative of measurement with respect to this Entry
662  if ((*vecite)->quality() >= theMinimumEntryQuality) {
663  if (ALIUtils::debug >= 4) {
664  // std::cout << "FillMatricesWithMeasurements: filling element ( " << Aline << " - " << Aline+measdim-1 << " , " << (*vecite)->fitPos() << std::endl;
665  std::cout << "entry affecting: " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << std::endl;
666  }
667  derivRE = (*vmcite)->DerivativeRespectEntry(*vecite);
668  //---------- Fill matrix A with derivatives
669  for (ALIuint jj = 0; jj < ALIuint(measdim); jj++) {
670  AMatrix->AddData(Aline + jj, (*vecite)->fitPos(), derivRE[jj]);
671  if (ALIUtils::debug >= 5)
672  std::cout << "FillMatricesWithMeasurements: AMATRIX (" << Aline + jj << "," << (*vecite)->fitPos() << " = "
673  << derivRE[jj] << std::endl;
674  //---------- Reset Measurement simulated_value
675  (*vmcite)->setValueSimulated(jj, (*vmcite)->valueSimulated_orig(jj));
676  }
677  }
678  }
679  // delete[] derivRE;
680 
681  //---------- Fill matrices W, y and f:
682  //------ Loop Measurement coordinates
683  for (ALIuint jj = 0; jj < ALIuint((*vmcite)->dim()); jj++) {
684  ALIdouble sigma = (*vmcite)->sigma()[jj];
685  if (sigma == 0.) {
686  std::cerr << "EXITING: Measurement number " << vmcite - Model::MeasurementList().begin() << "has 0 error!!"
687  << std::endl;
688  } else {
689  //----- Fill W Matrix with inverse of sigma squared
690  // multiply error by cameraScaleFactor
691  ALIdouble sigmanew = sigma * Measurement::cameraScaleFactor;
692  // std::cout << Aline+jj << " WMATRIX FILLING " << sigmanew << " * " << Measurement::cameraScaleFactor << std::endl;
693  WMatrix->AddData(Aline + jj, Aline + jj, (sigmanew * sigmanew));
694  }
695  //op //----- Fill Matrices y with measurement value
696  //op yMatrix->AddData( Aline+jj, 0, (*vmcite)->value()[jj] );
697  //op //----- Fill f Matrix with simulated_value
698  //op fMatrix->AddData( Aline+jj, 0, (*vmcite)->valueSimulated_orig(jj) );
699  //----- Fill Matrix y - f with measurement value - simulated value
700  yfMatrix->AddData(Aline + jj, 0, (*vmcite)->value()[jj] - (*vmcite)->valueSimulated_orig(jj));
701  // std::cout << " yfMatrix FILLING " << Aline+jj << " + " << (*vmcite)->value()[jj] - (*vmcite)->valueSimulated_orig(jj) << " meas " << (*vmcite)->name() << " val " << (*vmcite)->value()[jj] << " simu val " << (*vmcite)->valueSimulated_orig(jj) << std::endl;
702  }
703  if (ALIUtils::debug >= 99)
704  std::cout << "change line" << Aline << std::endl;
705  Aline += measdim;
706  if (ALIUtils::debug >= 99)
707  std::cout << "change line" << Aline << std::endl;
708  }
709 
710  if (ALIUtils::debug >= 4)
711  AMatrix->Dump("Matrix A with meas");
712  if (ALIUtils::debug >= 4)
713  WMatrix->Dump("Matrix W with meas");
714  if (ALIUtils::debug >= 4)
715  yfMatrix->Dump("Matrix y with meas");
716 }
717 
718 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
719 //@@ Loop Measurements:
720 //@@ Fill Matrix A with derivatives respect to affecting entries
721 //@@ Fill Matrix W, y & f with values and sigmas of measurement coordinate
722 //@@
723 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
725  if (ALIUtils::debug >= 3)
726  std::cout << "@@@ Fit::FillMatricesWithCalibratedParameters" << std::endl;
727 
728  //---------- Count how many measurements
729  ALIint NolinMes = 0;
730  std::vector<Measurement*>::const_iterator vmcite;
731  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
732  NolinMes += (*vmcite)->dim();
733  }
734  if (ALIUtils::debug >= 4)
735  std::cout << "@@FillMatricesWithCalibratedParameters" << std::endl;
736 
737  std::vector<Entry*>::const_iterator vecite;
738  ALIint nEntcal = 0;
739  //---------- Loop entries
740  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
741  // (= No parameters to be fitted - No parameters 'unk' )
742  //- std::cout << "entry" << (*veite) << std::endl;
743  //----- Take entries of quality = 'cal'
744  if ((*vecite)->quality() == 1) {
745  //--- Matrix A: fill diagonals with 1. (derivatives of entry w.r.t itself)
746  ALIint lineNo = NolinMes + nEntcal;
747  ALIint columnNo = (*vecite)->fitPos(); //=? nEntcal
748  AMatrix->AddData(lineNo, columnNo, 1.);
749  if (ALIUtils::debug >= 4)
750  std::cout << "Fit::FillMatricesWithCalibratedParameters: AMatrix ( " << lineNo << " , " << columnNo
751  << ") = " << 1. << std::endl;
752 
753  //--- Matrix W: sigma*sigma
754  ALIdouble entsig = (*vecite)->sigma();
755  if (ALIUtils::debug >= 4)
756  std::cout << "Fit::FillMatricesWithCalibratedParameters: WMatrix ( " << lineNo << " , " << columnNo
757  << ") = " << entsig * entsig << std::endl;
758  WMatrix->AddData(lineNo, lineNo, entsig * entsig);
759 
760  //--- Matrix y & f: fill it with 0.
761  //op yMatrix->AddData( lineNo, 0, (*vecite)->value());
762  //op yfMatrix->AddData( lineNo, 0, (*vecite)->value());
763  ALIdouble calFit;
765  gomgr->getGlobalOptionValue("calParamInyfMatrix", calFit);
766  if (calFit) {
767  yfMatrix->AddData(lineNo, 0, -(*vecite)->valueDisplacementByFitting());
768  //- yfMatrix->AddData( lineNo, 0, (*vecite)->value() );
769  //- yfMatrix->AddData( lineNo, 0, (*vecite)->lastAdditionToValueDisplacementByFitting() );
770  //- ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
771  // fileout << "cal to yf " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " " << (*vecite)->valueDisplacementByFitting() << endl;
772  // std::cout << "call to yf " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " " << (*vecite)->valueDisplacementByFitting() << std::endl;
773 
774  } else {
775  yfMatrix->AddData(lineNo, 0, 0.);
776  }
777  //t if(ALIUtils::debug >= 5) std::cout << "Fit::FillMatricesWithCalibratedParameters: yfMatrix ( " << lineNo << " , " << columnNo << ") = " << (*yfMatrix)(lineNo)(0) << std::endl;
778  nEntcal++;
779  }
780  }
781 }
782 
783 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
784 //@@ Gets the only instance of Model
785 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
787  if (ALIUtils::debug >= 3)
788  std::cout << "@@@ Fit::setCorrelationsInWMatrix" << std::endl;
789 
790  //----- Check if there are correlations to input
792  ALIint siz = corrMgr->getNumberOfCorrelations();
793  if (siz == 0)
794  return;
795 
796  //----- Set correlations
797  ALIuint ii;
798  for (ii = 0; ii < ALIuint(siz); ii++) {
799  //t if(ALIUtils::debug >= 5) std::cout << "globaloption cmslink fit" << Model::GlobalOptions()["cms_link"] << std::endl;
800  ErrorCorrelation* corr = corrMgr->getCorrelation(ii);
801  setCorrelationFromParamFitted(corr->getEntry1(), corr->getEntry2(), corr->getCorrelation());
802  }
803 }
804 
805 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
806 //@@ set correlation between two entries of two OptOs
807 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
808 void Fit::setCorrelationFromParamFitted(const pss& entry1, const pss& entry2, ALIdouble correl) {
809  ALIint pmsize = WMatrix->NoLines();
810  ALIint fit_pos1 = Model::getEntryByName(entry1.first, entry1.second)->fitPos();
811  ALIint fit_pos2 = Model::getEntryByName(entry2.first, entry2.second)->fitPos();
812  std::cout << "CHECKsetCorrelatiFPF " << fit_pos1 << " " << fit_pos2 << std::endl;
813 
814  if (fit_pos1 >= 0 && fit_pos1 < pmsize && fit_pos2 >= 0 && fit_pos2 < pmsize) {
815  setCorrelationFromParamFitted(fit_pos1, fit_pos2, correl);
816  }
817 }
818 
819 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
820 void Fit::setCorrelationFromParamFitted(const ALIint fit_pos1, const ALIint fit_pos2, ALIdouble correl) {
821  // ALIdouble error1 = sqrt( (*WMatrix)(fit_pos1, fit_pos1) );
822  // ALIdouble error2 = sqrt( (*WMatrix)(fit_pos2, fit_pos2) );
823  WMatrix->SetCorrelation(fit_pos1, fit_pos2, correl);
824  std::cout << "setCorrelatiFPF " << fit_pos1 << " " << fit_pos2 << " " << correl << std::endl;
825 }
826 
827 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
828 //@@ multiply matrices needed for fit
829 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
831  if (ALIUtils::debug >= 3)
832  std::cout << "@@@ Fit::multiplyMatrices " << std::endl;
833  //---------- Calculate transpose of A
834  AtMatrix = new ALIMatrix(*AMatrix);
835  if (ALIUtils::debug >= 5)
836  AtMatrix->Dump("AtMatrix=A");
837  //- std::cout << "call transpose";
838  AtMatrix->transpose();
839  if (ALIUtils::debug >= 4)
840  AtMatrix->Dump("AtMatrix");
841 
842  //---------- Calculate At * W * A
843  AtWAMatrix = new ALIMatrix(0, 0);
844  // if(ALIUtils::debug >= 5) AtWAMatrix->Dump("AtWAMatrix=0");
846  if (ALIUtils::debug >= 5)
847  AtWAMatrix->Dump("AtWAMatrix");
848 
850 
851  //t AtWAMatrix->EliminateLines(0,48);
852  //t AtWAMatrix->EliminateColumns(0,48);
853  time_t now;
854  now = clock();
855  if (ALIUtils::debug >= 0)
856  std::cout << "TIME:BEFORE_INVERSE : " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
858 
859  /* std::cout << " DETERMINANT W " << m_norm1( AtWAMatrix->MatNonConst() ) << std::endl;
860  if( m_norm1( AtWAMatrix->MatNonConst() ) == 0 ) {
861  std::cout << " DETERMINANT W " << m_norm1( AtWAMatrix->MatNonConst() ) << std::endl;
862  std::exception();
863  } */
864 
865  AtWAMatrix->inverse();
866  if (ALIUtils::debug >= 4)
867  AtWAMatrix->Dump("inverse AtWAmatrix");
868  now = clock();
869  if (ALIUtils::debug >= 0)
870  std::cout << "TIME:AFTER_INVERSE : " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
872 
873  //op thePropagationMatrix = AtWAMatrix;
874 
875  //op VaMatrix = new ALIMatrix( *AtWAMatrix );
876 
877  //----- Print out propagated errors of parameters (=AtWA diagonal elements)
878  std::vector<Entry*>::const_iterator vecite;
879 
880  if (ALIUtils::debug >= 4) {
881  std::cout << "PARAM"
882  << " Optical Object "
883  << " entry name "
884  << " Param.Value "
885  << " Prog.Error"
886  << " Orig.Error" << std::endl;
887  }
888 
889  ALIint nEnt = 0;
890  ALIint nEntUnk = 0;
891  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
892  //------------------ Number of parameters 'cal'
893  // (= No parameters to be fitted - No parameters 'unk' )
894  if ((*vecite)->quality() >= theMinimumEntryQuality) {
895  if (ALIUtils::debug >= 4) {
896  std::cout << nEnt << "PARAM" << std::setw(26) << (*vecite)->OptOCurrent()->name().c_str() << std::setw(8) << " "
897  << (*vecite)->name().c_str() << " " << std::setw(8) << " " << (*vecite)->value() << " "
898  << std::setw(8) << sqrt(AtWAMatrix->Mat()->me[nEnt][nEnt]) / (*vecite)->OutputSigmaDimensionFactor()
899  << " " << (*vecite)->sigma() / (*vecite)->OutputSigmaDimensionFactor() << " Q" << (*vecite)->quality()
900  << std::endl;
901  }
902  nEnt++;
903  }
904  if ((*vecite)->quality() == 2)
905  nEntUnk++;
906  }
907 
908  if (ALIUtils::debug >= 5)
909  yfMatrix->Dump("PD(y-f)Matrix final");
910 }
911 
912 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
913 //@@ check also that the fit_quality = SMat(0,0) is smaller for each new iteration
914 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
916  if (ALIUtils::debug >= 3)
917  std::cout << "@@@ Fit::getFitQuality" << std::endl;
918 
919  double fit_quality = GetSChi2(true);
920 
921  //---------- Calculate DS = Variable to recognize convergence (distance to minimum)
922  ALIMatrix* DatMatrix = new ALIMatrix(*DaMatrix);
923  // delete DaMatrix; //op
924  DatMatrix->transpose();
925  if (ALIUtils::debug >= 5)
926  DatMatrix->Dump("DatMatrix");
927  //op ALIMatrix* DSMat = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix * *PDMatrix);
928  ALIMatrix* DSMat = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix * *yfMatrix);
929  if (ALIUtils::debug >= 5) {
930  ALIMatrix* DSMattemp = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix);
931  DSMattemp->Dump("DSMattempMatrix=Dat*At*W");
932  ALIMatrix* DSMattemp2 = new ALIMatrix(*AtMatrix * *WMatrix * *yfMatrix);
933  DSMattemp2->Dump("DSMattempMatrix2=At*W*yf");
934  ALIMatrix* DSMattemp3 = new ALIMatrix(*AtMatrix * *WMatrix);
935  DSMattemp3->Dump("DSMattempMatrix3=At*W");
936  AtMatrix->Dump("AtMatrix");
937  }
938 
939  /* for( int ii = 0; ii < DatMatrix->NoColumns(); ii++ ){
940  std::cout << ii << " DS term " << (*DatMatrix)(0,ii) * (*DSMattemp2)(ii,0) << std::endl;
941  }*/
942  // delete AtMatrix; //op
943  // delete WMatrix; //op
944 
945  //op if(ALIUtils::debug >= 5) (*PDMatrix).Dump("PDMatrix");
946  if (ALIUtils::debug >= 5)
947  (*yfMatrix).Dump("yfMatrix");
948  if (ALIUtils::debug >= 5)
949  DSMat->Dump("DSMatrix final");
950  // delete yfMatrix; //op
951 
952  ALIdouble fit_quality_cut = (*DSMat)(0, 0);
953  //- ALIdouble fit_quality_cut =std::abs( (*DSMat)(0,0) );
954  delete DSMat;
955  if (ALIUtils::debug >= 0)
957  << " Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
958  if (ALIUtils::report >= 2) {
960  fileout << theNoFitIterations
961  << " Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
962  }
963 
964  //- double fit_quality_cut = thePreviousIterationFitQuality - fit_quality;
965  //- double fit_quality_cut = fit_quality;
966  //- std::cout << " fit_quality_cut " << fit_quality_cut << " fit_quality " << fit_quality << std::endl;
967 
968  //----- Check quality
969  time_t now;
970  now = clock();
971  if (ALIUtils::debug >= 0)
972  std::cout << "TIME:QUALITY_CHECKED: " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
974 
975  FitQuality fitQuality;
976 
977  //----- Chi2 is bigger, bad
978  // if( theNoFitIterations != 0 && fit_quality_cut > 0. ) {
979  if (fit_quality_cut < 0.) {
980  fitQuality = FQchiSquareWorsened;
981  if (ALIUtils::debug >= 1)
982  std::cerr << "!!WARNING: Fit quality has worsened: Fit Quality now = " << fit_quality << " before "
983  << thePreviousIterationFitQuality << " diff " << fit_quality - thePreviousIterationFitQuality
984  << std::endl;
985 
986  //----- Chi2 is smaller, check if we make another iteration
987  } else {
988  ALIdouble rel_fit_quality = std::abs(thePreviousIterationFitQuality - fit_quality) / fit_quality;
989  //----- Small chi2 change: end
990  if ((fit_quality_cut < theFitQualityCut || rel_fit_quality < theRelativeFitQualityCut) && canBeGood) {
991  if (ALIUtils::debug >= 2)
992  std::cout << "$$ Fit::getFitQuality good " << fit_quality_cut << " <? " << theFitQualityCut << " || "
993  << rel_fit_quality << " <? " << theRelativeFitQualityCut << " GOOD " << canBeGood << std::endl;
994  fitQuality = FQsmallDistanceToMinimum;
995  if (ALIUtils::report >= 1) {
997  fileout << "STOP: SMALL IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " < "
998  << theFitQualityCut << " OR (RELATIVE) " << rel_fit_quality << " < " << theRelativeFitQualityCut
999  << std::endl;
1000  }
1001  if (ALIUtils::debug >= 4) {
1002  std::cout << "STOP: SMALL IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " < "
1003  << theFitQualityCut << " OR (RELATIVE) " << rel_fit_quality << " < " << theRelativeFitQualityCut
1004  << std::endl;
1005  }
1006 
1007  //----- Big chi2 change: go to next iteration
1008  } else {
1009  if (ALIUtils::debug >= 2)
1010  std::cout << "$$ Fit::getFitQuality bad " << fit_quality_cut << " <? " << theFitQualityCut << " || "
1011  << rel_fit_quality << " <? " << theRelativeFitQualityCut << " GOOD " << canBeGood << std::endl;
1012  fitQuality = FQbigDistanceToMinimum;
1013  //----- set thePreviousIterationFitQuality for next iteration
1014  thePreviousIterationFitQuality = fit_quality;
1015 
1016  if (ALIUtils::report >= 2) {
1018  fileout << "CONTINUE: BIG IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut
1019  << " >= " << theFitQualityCut << " AND (RELATIVE) " << rel_fit_quality
1020  << " >= " << theRelativeFitQualityCut << std::endl;
1021  }
1022  if (ALIUtils::debug >= 4) {
1023  std::cout << "CONTINUE: BIG IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut
1024  << " >= " << theFitQualityCut << " AND (RELATIVE) " << rel_fit_quality
1025  << " >= " << theRelativeFitQualityCut << std::endl;
1026  }
1027  }
1028  }
1029 
1030  return fitQuality;
1031 }
1032 
1033 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1035  if (ALIUtils::debug >= 3)
1036  std::cout << "@@@ Fit::GetSChi2 useDa= " << useDa << std::endl;
1037 
1038  ALIMatrix* SMat = nullptr;
1039  if (useDa) {
1040  //----- Calculate variables to check quality of this set of parameters
1041 
1042  //----- Calculate Da = (At * W * A)-1 * At * W * (y-f)
1043  /*t DaMatrix = new ALIMatrix( *AtWAMatrix );
1044  *DaMatrix *= *AtMatrix * *WMatrix;
1045  if(ALIUtils::debug >= 5) DaMatrix->Dump("DaMatrix before yf ");
1046  *DaMatrix *= *yfMatrix;
1047  if(ALIUtils::debug >= 5) DaMatrix->Dump("DaMatrix");
1048  */
1049 
1050  DaMatrix = new ALIMatrix(0, 0);
1051  // if(ALIUtils::debug >= 5) AtWAMatrix->Dump("AtWAMatrix=0");
1052  *DaMatrix = (*AtWAMatrix * *AtMatrix * *WMatrix * *yfMatrix);
1053  if (ALIUtils::debug >= 5)
1054  DaMatrix->Dump("DaMatrix");
1055 
1056  //----- Calculate S = chi2 = Fit quality = r^T W r (r = residual = f + A*Da - y )
1057  //op ALIMatrix* tmpM = new ALIMatrix( *AMatrix * *DaMatrix + *PDMatrix );
1058  // ALIMatrix* tmpM = new ALIMatrix( *AMatrix * *DaMatrix + *yfMatrix );
1059 
1060  ALIMatrix* tmpM = new ALIMatrix(0, 0);
1061  *tmpM = *AMatrix * *DaMatrix - *yfMatrix;
1062  if (ALIUtils::debug >= 5)
1063  tmpM->Dump("A*Da + f - y Matrix ");
1064 
1065  ALIMatrix* tmptM = new ALIMatrix(*tmpM);
1066  tmptM->transpose();
1067  if (ALIUtils::debug >= 5)
1068  tmptM->Dump("tmptM after transpose");
1069  if (ALIUtils::debug >= 5)
1070  WMatrix->Dump("WMatrix");
1071 
1072  // std::cout << "smat " << std::endl;
1073  //o ALIMatrix* SMat = new ALIMatrix(*tmptM * *WMatrix * *tmpM);
1074  ALIMatrix* SMat1 = MatrixByMatrix(*tmptM, *WMatrix);
1075  // ALIMatrix* SMat1 = MatrixByMatrix(*AMatrix,*WMatrix);
1076  if (ALIUtils::debug >= 5)
1077  SMat1->Dump("(A*Da + f - y)^T * W Matrix");
1078  SMat = MatrixByMatrix(*SMat1, *tmpM);
1079  // std::cout << "smatc " << std::endl;
1080  delete tmpM;
1081  delete tmptM;
1082  if (ALIUtils::debug >= 5)
1083  SMat->Dump("SMatrix with Da");
1084  } else {
1085  ALIMatrix* yftMat = new ALIMatrix(*yfMatrix);
1086  yftMat->transpose();
1087  SMat = new ALIMatrix(*yftMat * *WMatrix * *yfMatrix);
1088  delete yftMat;
1089  if (ALIUtils::debug >= 5)
1090  SMat->Dump("SMatrix no Da");
1091  }
1092  ALIdouble fit_quality = (*SMat)(0, 0);
1093  delete SMat;
1094  if (ALIUtils::debug >= 5)
1095  std::cout << " GetSChi2 = " << fit_quality << std::endl;
1096 
1097  PrintChi2(fit_quality, !useDa);
1098 
1099  return fit_quality;
1100 }
1101 
1102 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1103 //@@ Correct entries with fitted values
1104 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1106  if (ALIUtils::debug >= 4) {
1107  std::cout << "@@ Adding correction (DaMatrix) to Entries " << std::endl;
1108  DaMatrix->Dump("DaMatrix =");
1109  }
1110 
1111  /*- Now there are other places where entries are changed
1112  if( ALIUtils::report >= 3 ) {
1113  ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
1114  fileout << std::endl << " CHANGE IN ENTRIES" << std::endl
1115  << " Optical Object Parameter xxxxxx " << std::endl;
1116  }*/
1117 
1118  ALIint nEnt = 0;
1119  std::vector<Entry*>::const_iterator vecite;
1120  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
1121  //------------------ Number of parameters 'cal'
1122  // (= No parameters to be fitted - No parameters 'unk' )
1123  //- std::cout << "qual" << (*vecite)->quality() << theMinimumEntryQuality << std::endl;
1124  if ((*vecite)->quality() >= theMinimumEntryQuality) {
1125  if (ALIUtils::debug >= 5) {
1126  std::cout << std::endl
1127  << " @@@ PENTRY change " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " "
1128  << " change= " << (*DaMatrix)(nEnt, 0) << " value= " << (*vecite)->valueDisplacementByFitting()
1129  << std::endl;
1130  }
1131  /* if( ALIUtils::report >=3 ) {
1132  ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
1133  fileout << "dd" << std::setw(30) << (*vecite)->OptOCurrent()->name()
1134  << std::setw(8) << " " << (*vecite)->name() << " "
1135  << std::setw(8) << " " << (*DaMatrix)(nEnt,0) / (*vecite)->OutputValueDimensionFactor()
1136  << " " << (*vecite)->valueDisplacementByFitting() / (*vecite)->OutputValueDimensionFactor() << " fitpos " << (*vecite)->fitPos()
1137  << std::endl;
1138  }*/
1139 
1140  //----- Store this displacement
1141  if (ALIUtils::debug >= 4)
1142  std::cout << " old valueDisplacementByFitting " << (*vecite)->name() << " "
1143  << (*vecite)->valueDisplacementByFitting() << " original value " << (*vecite)->value() << std::endl;
1144 
1145  (*vecite)->addFittedDisplacementToValue((*DaMatrix)(nEnt, 0));
1146 
1147  if (ALIUtils::debug >= 4)
1148  std::cout << nEnt << " new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() << " "
1149  << (*vecite)->name() << " = " << (*vecite)->valueDisplacementByFitting() << " "
1150  << (*DaMatrix)(nEnt, 0) << std::endl;
1151  nEnt++;
1152  }
1153  }
1154 }
1155 
1156 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1157 //@@ Delete the previous addition of fitted values (to try a new one daFactor times smaller )
1158 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1160  if (ALIUtils::debug >= 4) {
1161  std::cout << "@@ Fit::substractToHalfDaMatrixToEntries " << std::endl;
1162  }
1163 
1164  std::vector<Entry*>::const_iterator vecite;
1165  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
1166  if ((*vecite)->quality() >= theMinimumEntryQuality) {
1167  //-- (*vecite)->addFittedDisplacementToValue( -(*DaMatrix)(nEnt,0) );!!! it is not substracting the new value of DaMatrix, but substracting the value that was added last iteration, with which the new value of DaMatrix has been calculated for this iteration
1168 
1169  ALIdouble lastadd = (*vecite)->lastAdditionToValueDisplacementByFitting() * factor;
1170  //- if( lastadd < 0 ) lastadd *= -1;
1171  (*vecite)->addFittedDisplacementToValue(-lastadd);
1172  (*vecite)->setLastAdditionToValueDisplacementByFitting(-(*vecite)->lastAdditionToValueDisplacementByFitting());
1173  // (*vecite)->substractToHalfFittedDisplacementToValue();
1174 
1175  if (ALIUtils::debug >= 4)
1176  std::cout << " new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name()
1177  << " = " << (*vecite)->valueDisplacementByFitting() << " " << std::endl;
1178  }
1179  }
1180 }
1181 
1182 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1183 //@@ Dump all the entries that have been fitted (those that were 'cal' or 'unk'
1184 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1185 void Fit::dumpFittedValues(ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig) {
1186  //---------- print
1187  if (ALIUtils::debug >= 0) {
1188  std::cout << "SRPRPOS "
1189  << " Optical Object "
1190  << " Parameter"
1191  << " Fit.Value "
1192  << " Orig.Value" << std::endl;
1193  }
1194  //---------- Dump header
1195  if (ALIUtils::debug >= 0)
1196  std::cout << std::endl << "FITTED VALUES " << std::endl;
1197  fileout << std::endl
1198  << "FITTED VALUES " << std::endl
1199  << "nEnt_unk"
1200  << " Optical Object"
1201  << " Parameter ";
1202  if (printErrors) {
1203  fileout << " value (+-error)"
1204  << " orig.val (+-error)";
1205  } else {
1206  fileout << " value "
1207  << " orig.val ";
1208  }
1209  fileout << " quality" << std::endl;
1210 
1211  //---------- Iterate over OptO list
1212  std::vector<Entry*> entries;
1213  // const Entry* entry;
1214  int ii, siz;
1215  std::vector<OpticalObject*>::const_iterator vocite;
1216  for (vocite = Model::OptOList().begin(); vocite != Model::OptOList().end(); ++vocite) {
1217  if ((*vocite)->type() == ALIstring("system"))
1218  continue;
1219 
1220  fileout << " %%%% Optical Object: " << (*vocite)->longName() << std::endl;
1221 
1222  entries = (*vocite)->CoordinateEntryList();
1223  siz = entries.size();
1224  if (siz != 6) {
1225  std::cerr << "!!! FATAL ERROR: strange number of coordinates = " << siz << std::endl;
1226  abort();
1227  }
1228 
1229  //----- Dump entry centre coordinates (centre in current coordinates of parent frame <> summ of displacements, as each displacement is done with a different rotation of parent frame)
1230  OpticalObject* opto = entries[0]->OptOCurrent();
1231  const OpticalObject* optoParent = opto->parent();
1232  printCentreInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1233 
1234  //----- Dump entry angles coordinates
1235  printRotationAnglesInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1236 
1237  //----- Dump extra entries
1238  entries = (*vocite)->ExtraEntryList();
1239  siz = entries.size();
1240  for (ii = 0; ii < siz; ii++) {
1241  double entryvalue = getEntryValue(entries[ii]);
1242  dumpEntryAfterFit(fileout, entries[ii], entryvalue, printErrors, printOrig);
1243  }
1244  }
1245 
1246  dumpEntryCorrelations(fileout);
1247 }
1248 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1249 //@@ Dump all the entries that have been fitted in reference frames of all ancestors
1250 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1251 void Fit::dumpFittedValuesInAllAncestorFrames(ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig) {
1252  //---------- print
1253  fileout << std::endl
1254  << "@@@@ FITTED VALUES IN ALL ANCESTORS " << std::endl
1255  << "nEnt_unk"
1256  << " Optical Object"
1257  << " Parameter ";
1258  if (printErrors) {
1259  fileout << " value (+-error)";
1260  if (printOrig) {
1261  fileout << " orig.val (+-error)";
1262  }
1263  } else {
1264  fileout << " value ";
1265  if (printOrig) {
1266  fileout << " orig.val ";
1267  }
1268  }
1269  fileout << " quality" << std::endl;
1270 
1271  //---------- Iterate over OptO list
1272  std::vector<Entry*> entries;
1273  std::vector<OpticalObject*>::const_iterator vocite;
1274  for (vocite = Model::OptOList().begin(); vocite != Model::OptOList().end(); ++vocite) {
1275  if ((*vocite)->type() == ALIstring("system"))
1276  continue;
1277 
1278  fileout << " %%%% Optical Object: " << (*vocite)->longName() << std::endl;
1279 
1280  entries = (*vocite)->CoordinateEntryList();
1281 
1282  //----- Dump entry centre coordinates (centre in current coordinates of parent frame <> summ of displacements, as each displacement is done with a different rotation of parent frame)
1283  OpticalObject* opto = *vocite;
1284  const OpticalObject* optoParent = opto->parent();
1285  do {
1286  fileout << " %% IN FRAME : " << optoParent->longName() << std::endl;
1287  printCentreInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1288 
1289  //----- Dump entry angles coordinates
1290  printRotationAnglesInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1291  optoParent = optoParent->parent();
1292  } while (optoParent);
1293  }
1294 }
1295 
1296 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1298  const OpticalObject* optoAncestor,
1299  ALIFileOut& fileout,
1300  ALIbool printErrors,
1301  ALIbool printOrig) {
1302  CLHEP::Hep3Vector centreLocal;
1303  if (optoAncestor->type() == "system") {
1304  centreLocal = opto->centreGlob();
1305  } else {
1306  centreLocal = opto->centreGlob() - optoAncestor->centreGlob();
1307  CLHEP::HepRotation parentRmGlobInv = inverseOf(optoAncestor->rmGlob());
1308  centreLocal = parentRmGlobInv * centreLocal;
1309  }
1310  if (ALIUtils::debug >= 2) {
1311  std::cout << "CENTRE LOCAL " << opto->name() << " " << centreLocal << " GLOBL " << opto->centreGlob()
1312  << " parent GLOB " << optoAncestor->centreGlob() << std::endl;
1313  ALIUtils::dumprm(optoAncestor->rmGlob(), " parent rm ");
1314  }
1315  std::vector<Entry*> entries = opto->CoordinateEntryList();
1316  for (ALIuint ii = 0; ii < 3; ii++) {
1317  /* double entryvalue = getEntryValue( entries[ii] );
1318  ALIdouble entryvalue;
1319  if( ii == 0 ) {
1320  entryvalue = centreLocal.x();
1321  }else if( ii == 1 ) {
1322  entryvalue = centreLocal.y();
1323  }else if( ii == 2 ) {
1324  entryvalue = centreLocal.z();
1325  }*/
1327  fileout, entries[ii], centreLocal[ii] / entries[ii]->OutputValueDimensionFactor(), printErrors, printOrig);
1328  }
1329 }
1330 
1331 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1333  const OpticalObject* optoAncestor,
1334  ALIFileOut& fileout,
1335  ALIbool printErrors,
1336  ALIbool printOrig) {
1337  std::vector<Entry*> entries = opto->CoordinateEntryList();
1338  std::vector<double> entryvalues = opto->getRotationAnglesInOptOFrame(optoAncestor, entries);
1339  //- std::cout << " after return entryvalues[0] " << entryvalues[0] << " entryvalues[1] " << entryvalues[1] << " entryvalues[2] " << entryvalues[2] << std::endl;
1340  for (ALIuint ii = 3; ii < entries.size(); ii++) {
1342  fileout, entries[ii], entryvalues[ii - 3] / entries[ii]->OutputValueDimensionFactor(), printErrors, printOrig);
1343  }
1344 }
1345 
1346 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1348  double entryvalue;
1349  if (entry->type() == "angles") {
1350  if (ALIUtils::debug >= 2)
1351  std::cout << "WARNING valueDisplacementByFitting has no sense for angles " << std::endl;
1352 
1353  // commenting out the following line as it is a dead assignment due to the
1354  // subsequent assignment below
1355  // -> silence static analyzer warnings, but leaving the commented line in
1356  // case someone wants to actively use this code again
1357 
1358  // entryvalue = -999;
1359  }
1360  entryvalue = (entry->value() + entry->valueDisplacementByFitting()) / entry->OutputValueDimensionFactor();
1361  return entryvalue;
1362 }
1363 
1364 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1366  ALIFileOut& fileout, const Entry* entry, double entryvalue, ALIbool printErrors, ALIbool printOrig) {
1367  //- std::cout << " Fit::dumpEntryAfterFit " << entryvalue << std::endl;
1368  ALIdouble dimv = entry->OutputValueDimensionFactor();
1369  ALIdouble dims = entry->OutputSigmaDimensionFactor();
1370  //----- Dump to std::cout
1371  if (ALIUtils::debug >= 3) {
1372  std::cout << "ENTRY: " << std::setw(30) << entry->OptOCurrent()->name() << std::setw(8) << " " << entry->name()
1373  << " " << std::setw(8) << (entry->value() + entry->valueDisplacementByFitting()) << " " << entry->value()
1374  << " Q" << entry->quality() << std::endl;
1375  }
1376 
1377  if (entry->quality() == 2) {
1378  fileout << "UNK: " << entry->fitPos() << " ";
1379  } else if (entry->quality() == 1) {
1380  fileout << "CAL: " << entry->fitPos() << " ";
1381  // fileout << "CAL: -1 ";
1382  } else {
1383  fileout << "FIX: -1 ";
1384  }
1385 
1386  fileout << std::setw(30) << entry->OptOCurrent()->name() << std::setw(8) << " " << entry->name() << " "
1387  << std::setw(8) << std::setprecision(8) << entryvalue;
1388  if (entry->quality() >= theMinimumEntryQuality) {
1389  if (printErrors)
1390  fileout << " +- " << std::setw(8) << sqrt(AtWAMatrix->Mat()->me[entry->fitPos()][entry->fitPos()]) / dims;
1391  } else {
1392  if (printErrors)
1393  fileout << " +- " << std::setw(8) << 0.;
1394  }
1395  if (printOrig) {
1396  fileout << std::setw(8) << " " << entry->value() / dimv;
1397  if (printErrors)
1398  fileout << " +- " << std::setw(8) << entry->sigma() / dims << " Q" << entry->quality();
1399 
1400  if (ALIUtils::report >= 2) {
1401  float dif = (entry->value() + entry->valueDisplacementByFitting()) / dimv - entry->value() / dimv;
1402  if (std::abs(dif) < 1.E-9)
1403  dif = 0.;
1404  fileout << " DIFF= " << dif;
1405  // << " == " << ( entry->value() + entry->valueDisplacementByFitting() ) / dimv - entryvalue << " @@ " << ( entry->value() + entry->valueDisplacementByFitting() ) / dimv << " @@ " << entryvalue;
1406  } else {
1407  // fileout << std::endl;
1408  }
1409  }
1410 
1411  fileout << std::endl;
1412 }
1413 
1414 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1416  //----- Only dump correlations bigger than a factor
1417  ALIdouble minCorrel = 1.E-6;
1418  //----- Dump correlations
1419  fileout << std::endl
1420  << "CORRELATION BETWEEN 'unk' ENTRIES: (>= " << minCorrel << " )" << std::endl
1421  << "No_1 No_2 correlation " << std::endl;
1422 
1423  ALIuint i1, i2;
1424  std::vector<Entry*>::iterator veite1, veite2;
1425  std::string E1, E2;
1426  for (veite1 = Model::EntryList().begin(); veite1 != Model::EntryList().end(); ++veite1) {
1427  if ((*veite1)->quality() == 0) {
1428  continue;
1429  } else if ((*veite1)->quality() == 1) {
1430  E1 = "C";
1431  } else if ((*veite1)->quality() == 2) {
1432  E1 = "U";
1433  }
1434  i1 = (*veite1)->fitPos();
1435 
1436  for (veite2 = veite1 + 1; veite2 != Model::EntryList().end(); ++veite2) {
1437  i2 = (*veite2)->fitPos();
1438  if ((*veite2)->quality() == 0) {
1439  continue;
1440  } else if ((*veite2)->quality() == 1) {
1441  E2 = "C";
1442  } else if ((*veite2)->quality() == 2) {
1443  E2 = "U";
1444  }
1445  ALIdouble corr = AtWAMatrix->Mat()->me[i1][i2];
1446  ALIdouble corrf = corr / sqrt(AtWAMatrix->Mat()->me[i1][i1]) / sqrt(AtWAMatrix->Mat()->me[i2][i2]);
1447  if (std::abs(corrf) >= minCorrel) {
1448  if (ALIUtils::debug >= 0) {
1449  std::cout << "CORR:" << E1 << "" << E2 << " (" << i1 << ")"
1450  << " (" << i2 << ")"
1451  << " " << corrf << std::endl;
1452  }
1453  fileout << "CORR:" << E1 << "" << E2 << " (" << i1 << ")"
1454  << " (" << i2 << ")"
1455  << " " << corrf << std::endl;
1456  }
1457  }
1458  }
1459  //------- Dump optical object list
1460  if (ALIUtils::debug >= 2)
1462 }
1463 
1464 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1465 //@@ Dump matrices used for the fit
1466 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1468  //----- Dump matrices for this iteration
1470  // ofstream matout("matrices.out");
1471  matout << std::endl << " @@@@@@@@@@@@@@@ Iteration No : " << theNoFitIterations << std::endl;
1472  AMatrix->ostrDump(matout, "Matrix A");
1473  AtMatrix->ostrDump(matout, "Matrix At");
1474  WMatrix->ostrDump(matout, "Matrix W");
1475  AtWAMatrix->ostrDump(matout, "Matrix AtWA");
1476  //op VaMatrix->ostrDump( matout, "Matrix Va" );
1477  DaMatrix->ostrDump(matout, "Matrix Da");
1478  yfMatrix->ostrDump(matout, "Matrix y");
1479  //op fMatrix->ostrDump( matout, "Matrix f" );
1480  //op thePropagationMatrix->ostrDump( matout, "propagation Matrix " );
1481  matout.close();
1482 }
1483 
1484 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1485 //@@ findEntryFitPosition
1486 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1487 ALIint Fit::findEntryFitPosition(const ALIstring& opto_name, const ALIstring& entry_name) {
1488  ALIint fitposi = -99;
1489 
1490  OpticalObject* opto = Model::getOptOByName(opto_name);
1491  //- std::cout << "OPTO = " << opto->name() << std::endl;
1492  std::vector<Entry*>::const_iterator vecite;
1493  for (vecite = opto->CoordinateEntryList().begin(); vecite < opto->CoordinateEntryList().end(); ++vecite) {
1494  //- std::cout << "ENTRYLIST" << (*vecite)->name() << std::endl;
1495  if ((*vecite)->name() == entry_name) {
1496  //- std::cout << "FOUND " << std::endl;
1497  fitposi = (*vecite)->fitPos();
1498  }
1499  }
1500  for (vecite = opto->ExtraEntryList().begin(); vecite < opto->ExtraEntryList().end(); ++vecite) {
1501  //- std::cout << "ENTRYLIST" << (*vecite)->name() << std::endl;
1502  if ((*vecite)->name() == entry_name) {
1503  //- std::cout << "FOUND " << std::endl;
1504  fitposi = (*vecite)->fitPos();
1505  }
1506  }
1507 
1508  if (fitposi == -99) {
1509  std::cerr << "!!EXITING: entry name not found: " << entry_name << std::endl;
1510  exit(2);
1511  } else {
1512  return fitposi;
1513  }
1514 }
1515 
1516 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1518  double chi2meas = 0;
1519  double chi2cal = 0;
1520  ALIint nMeas = 0, nUnk = 0;
1521 
1522  //----- Calculate the chi2 of measurements
1523  std::vector<Measurement*>::const_iterator vmcite;
1524  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
1525  //--- Calculate Simulated Value Original
1526  for (ALIuint ii = 0; ii < ALIuint((*vmcite)->dim()); ii++) {
1527  nMeas++;
1528  double c2 = ((*vmcite)->value(ii) - (*vmcite)->valueSimulated(ii)) / (*vmcite)->sigma(ii);
1529  chi2meas += c2 * c2;
1530  if (ALIUtils::debug >= 2) {
1531  std::cout << c2 << " adding chi2meas " << chi2meas << " " << (*vmcite)->name() << ": " << ii
1532  << " (mm)R: " << (*vmcite)->value(ii) * 1000. << " S: " << (*vmcite)->valueSimulated(ii) * 1000.
1533  << " Diff= " << ((*vmcite)->value(ii) - (*vmcite)->valueSimulated(ii)) * 1000. << std::endl;
1534  }
1535  }
1536  }
1537 
1538  //----- Calculate the chi2 of calibrated parameters
1539  std::vector<Entry*>::iterator veite;
1540  for (veite = Model::EntryList().begin(); veite != Model::EntryList().end(); ++veite) {
1541  if ((*veite)->quality() == 2)
1542  nUnk++;
1543  if ((*veite)->quality() == 1) {
1544  double c2 = (*veite)->valueDisplacementByFitting() / (*veite)->sigma();
1545  //double c2 = (*veite)->value() / (*veite)->sigma();
1546  chi2cal += c2 * c2;
1547  if (ALIUtils::debug >= 2)
1548  std::cout << c2 << " adding chi2cal " << chi2cal << " " << (*veite)->OptOCurrent()->name() << " "
1549  << (*veite)->name() << std::endl;
1550  //- std::cout << " valueDisplacementByFitting " << (*veite)->valueDisplacementByFitting() << " sigma " << (*veite)->sigma() << std::endl;
1551  }
1552  }
1553 
1554  if (ALIUtils::report >= 1) {
1556  fileout << " Chi2= " << chi2meas + chi2cal << " / " << nMeas - nUnk << " dof "
1557  << " From measurements= " << chi2meas << " from calibrated parameters= " << chi2cal << std::endl;
1558  }
1559  if (ALIUtils::debug >= 3)
1560  std::cout << " quality Chi2 (no correlations) " << chi2meas + chi2cal << " " << chi2meas << " " << chi2cal
1561  << std::endl;
1562 
1563  if (!isFirst) {
1564  // double fit_quality_change = thePreviousIterationFitQuality - fit_quality;
1565 
1566  if (ALIUtils::debug >= 0) {
1567  std::cout << std::endl << "@@@@ Fit iteration " << theNoFitIterations << " ..." << std::endl;
1568  // std::cout << theNoFitIterations << " Chi2 improvement in this iteration = " << fit_quality_change << std::endl;
1569  }
1570  if (ALIUtils::report >= 1) {
1572  fileout << std::endl << "Fit iteration " << theNoFitIterations << " ..." << std::endl;
1573  // fileout << theNoFitIterations << " Chi2 improvement in this iteration = " << fit_quality_change << std::endl;
1574  }
1575  }
1576 
1577  //---- Print chi2
1578  if (ALIUtils::debug >= 0)
1579  std::cout << theNoFitIterations << " Chi2 after iteration = " << fit_quality << std::endl;
1580  if (ALIUtils::report >= 1) {
1581  //--------- Get report file handler
1583  fileout << theNoFitIterations << " Chi2 after iteration = " << fit_quality << std::endl;
1584  }
1585 }
1586 
1587 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1589  if (ALIUtils::debug >= 3)
1590  std::cout << "@@@ Fit::CheckIfFitPossible" << std::endl;
1591 
1592  //----- Check if there is an unknown parameter that is not affecting any measurement
1593  ALIint NolinMes = 0;
1594  std::vector<Measurement*>::const_iterator vmcite;
1595  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
1596  NolinMes += (*vmcite)->dim();
1597  }
1598 
1599  std::vector<Entry*>::const_iterator vecite;
1600  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
1601  if (ALIUtils::debug >= 4)
1602  std::cout << "Fit::CheckIfFitPossible looping for entry " << (*vecite)->longName() << std::endl;
1603  if ((*vecite)->quality() == 2) {
1604  ALIint nCol = (*vecite)->fitPos();
1605  //--- Check all measurements
1606  ALIbool noDepend = TRUE;
1607  if (ALIUtils::debug >= 4)
1608  std::cout << "Fit::CheckIfFitPossible looping for entry " << nCol << std::endl;
1609  for (ALIint ii = 0; ii < NolinMes; ii++) {
1610  if (ALIUtils::debug >= 5)
1611  std::cout << " Derivative= (" << ii << "," << nCol << ") = " << (*AMatrix)(ii, nCol) << std::endl;
1612 
1613  if (std::abs((*AMatrix)(ii, nCol)) > ALI_DBL_MIN) {
1614  if (ALIUtils::debug >= 5)
1615  std::cout << "Fit::CheckIfFitIsPossible " << nCol << " " << ii << " = " << (*AMatrix)(ii, nCol)
1616  << std::endl;
1617  noDepend = FALSE;
1618  break;
1619  }
1620  }
1621  if (noDepend) {
1622  throw cms::Exception("SDFError")
1623  << "!!!FATAL ERROR: Fit::CheckIfFitPossible() no measurement depends on unknown entry "
1624  << (*vecite)->OptOCurrent()->name() << "/" << (*vecite)->name() << std::endl
1625  << "!!! Fit will not be possible! " << std::endl;
1626  }
1627  }
1628  }
1629 
1630  //------ Check if there are two unknown entries that have the derivatives of all measurements w.r.t. then equal (or 100% proportional). In this case any value of the first entry can be fully compensated by another value in the second entry without change in any measurement ---> the two entries cannot be fitted!
1631 
1632  std::vector<Entry*>::const_iterator vecite1, vecite2;
1633  ALIint nLin = AMatrix->NoLines();
1634  ALIdouble derivPrec = ALI_DBL_MIN;
1635  //---------- Loop entries
1636  for (vecite1 = Model::EntryList().begin(); vecite1 != Model::EntryList().end(); ++vecite1) {
1637  if ((*vecite1)->quality() == 2) {
1638  vecite2 = vecite1;
1639  ++vecite2;
1640  for (; vecite2 != Model::EntryList().end(); ++vecite2) {
1641  if ((*vecite2)->quality() == 2) {
1642  ALIint fitpos1 = (*vecite1)->fitPos();
1643  ALIint fitpos2 = (*vecite2)->fitPos();
1644  if (ALIUtils::debug >= 5)
1645  std::cout << "Fit::CheckIfFitIsPossible checking " << (*vecite1)->longName() << " ( " << fitpos1 << " ) & "
1646  << (*vecite2)->longName() << " ( " << fitpos2 << " ) " << std::endl;
1647  ALIdouble prop = DBL_MAX;
1648  ALIbool isProp = TRUE;
1649  for (ALIint ii = 0; ii < nLin; ii++) {
1650  if (ALIUtils::debug >= 5)
1651  std::cout << "Fit::CheckIfFitIsPossible " << ii << " : " << (*AMatrix)(ii, fitpos1)
1652  << " ?= " << (*AMatrix)(ii, fitpos2) << std::endl;
1653  if (std::abs((*AMatrix)(ii, fitpos1)) < derivPrec) {
1654  if (std::abs((*AMatrix)(ii, fitpos2)) > derivPrec) {
1655  isProp = FALSE;
1656  break;
1657  }
1658  } else {
1659  ALIdouble propn = (*AMatrix)(ii, fitpos2) / (*AMatrix)(ii, fitpos1);
1660  if (prop != DBL_MAX && prop != propn) {
1661  isProp = FALSE;
1662  break;
1663  }
1664  prop = propn;
1665  }
1666  }
1667  if (isProp) {
1668  std::cerr << "!!!FATAL ERROR: Fit::CheckIfFitPossible() two entries for which the measurements have the "
1669  "same dependency (or 100% proportional) "
1670  << (*vecite1)->longName() << " & " << (*vecite2)->longName() << std::endl
1671  << "!!! Fit will not be possible! " << std::endl;
1672  throw cms::Exception("SDFError");
1673  }
1674  }
1675  }
1676  }
1677  }
1678 }
1679 
1680 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1682  int measProp = -1;
1683 
1684  std::set<ALIuint> columnsEqual;
1685  std::set<ALIuint> columnsEqualSave;
1686  ALIuint biggestColumn = 0;
1687  ALIdouble biggest = 0.;
1688  for (int ii = 0; ii < AMatrix->NoColumns(); ii++) {
1689  if (std::abs((*AMatrix)(measNo, ii)) > biggest) {
1690  biggest = std::abs((*AMatrix)(measNo, ii));
1691  biggestColumn = ii;
1692  }
1693  columnsEqualSave.insert(ii);
1694  }
1695 
1696  ALIdouble div;
1697 
1698  for (int jj = 0; jj < AMatrix->NoLines(); jj++) {
1699  if (jj == int(measNo))
1700  continue;
1701  columnsEqual = columnsEqualSave;
1702  // check if ratio of each column to 'biggestColumn' is the same as for the N measurement
1703  for (int ii = 0; ii < AMatrix->NoColumns(); ii++) {
1704  div = (*AMatrix)(measNo, ii) / (*AMatrix)(measNo, biggestColumn);
1705  if (std::abs((*AMatrix)(jj, ii)) > ALI_DBL_MIN &&
1706  std::abs(div - (*AMatrix)(jj, ii) / (*AMatrix)(jj, biggestColumn)) > ALI_DBL_MIN) {
1707  if (ALIUtils::debug >= 3)
1708  std::cout << "CheckIfMeasIsProportionalToAnother 2 columns = " << ii << " in " << measNo << " & " << jj
1709  << std::endl;
1710  } else {
1711  if (ALIUtils::debug >= 3)
1712  std::cout << "CheckIfMeasIsProportionalToAnother 2 columns != " << ii << " in " << measNo << " & " << jj
1713  << std::endl;
1714  // if it is not equal delete this column
1715  std::set<ALIuint>::iterator ite = columnsEqual.find(ii);
1716  if (ite != columnsEqual.end()) {
1717  columnsEqual.erase(ite);
1718  }
1719  }
1720  }
1721  // check if not all columns have been deleted from columnsEqual
1722  if (!columnsEqual.empty()) {
1723  if (ALIUtils::debug >= 3)
1724  std::cout << "CheckIfMeasIsProportionalToAnother " << measNo << " = " << jj << std::endl;
1725  measProp = jj;
1726  break;
1727  }
1728  }
1729 
1730  return measProp;
1731 }
1732 
1733 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1735  std::string measname = " ";
1736 
1737  std::cout << " imeas " << imeas << std::endl;
1738  int Aline = 0;
1739  std::vector<Measurement*>::const_iterator vmcite;
1740  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
1741  for (ALIuint jj = 0; jj < ALIuint((*vmcite)->dim()); jj++) {
1742  if (Aline == imeas) {
1743  char ctmp[20];
1744  gcvt(jj, 10, ctmp);
1745  return ((*vmcite)->name()) + ":" + std::string(ctmp);
1746  }
1747  Aline++;
1748  }
1749  }
1750 
1751  std::cout << " return measname " << measname << std::endl;
1752  return measname;
1753 }
static void set_time_now(time_t now)
Definition: ALIUtils.h:40
static ALIbool only1
Definition: Measurement.h:212
static void multiplyMatrices()
Definition: Fit.cc:830
static ALIdouble theMinDaFactor
Definition: Fit.h:205
const CLHEP::Hep3Vector & centreGlob() const
Definition: OpticalObject.h:75
long double ALIdouble
Definition: CocoaGlobals.h:11
void AddFittedEntriesSet(FittedEntriesSet *fents)
static ALIMatrix * AMatrix
Definition: Fit.h:164
#define TRUE
Definition: scimark2.h:12
static void setCorrelationFromParamFitted(const pss &entry1, const pss &entry2, ALIdouble correl)
Definition: Fit.cc:808
static CocoaDaqReader * GetDaqReader()
static void setCorrelationsInWMatrix()
Definition: Fit.cc:786
std::pair< ALIstring, ALIstring > pss
Definition: Fit.h:25
const double ALI_DBL_MIN
Definition: CocoaGlobals.h:25
static cocoaStatus getCocoaStatus()
Definition: Model.h:63
Definition: Entry.h:18
static void setCocoaStatus(const cocoaStatus cs)
Definition: Model.h:64
static void printCentreInOptOFrame(const OpticalObject *opto, const OpticalObject *optoAncestor, ALIFileOut &fileout, ALIbool printErrors=true, ALIbool printOrig=true)
Definition: Fit.cc:1297
std::vector< double > getRotationAnglesInOptOFrame(const OpticalObject *optoAncestor, const std::vector< Entry *> &entries) const
ErrorCorrelation * getCorrelation(ALIint ii)
static ALIint theMinimumEntryQuality
Definition: Fit.h:188
static std::vector< OpticalObject * > & OptOList()
Definition: Model.h:84
static void dumpEntryAfterFit(ALIFileOut &fileout, const Entry *entry, double entryvalue, ALIbool printErrors=true, ALIbool printOrig=true)
Definition: Fit.cc:1365
bool DumpCocoaResults()
Definition: CocoaDBMgr.cc:49
static void dumprm(const CLHEP::HepRotation &rm, const std::string &msg, std::ostream &out=std::cout)
Definition: ALIUtils.cc:71
MatrixMeschach * MatrixByMatrix(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
static ALIdouble getMaximumDeviationDerivative()
Definition: ALIUtils.h:89
int ALIint
Definition: CocoaGlobals.h:15
static OpticalObject * getOptOByName(const ALIstring &opto_name)
--— Find an OptO name in theOptOList and return a pointer to it
Definition: Model.cc:562
static ALIint debug
Definition: ALIUtils.h:34
static ALIMatrix * AtMatrix
Definition: Fit.h:165
static ALIstring & ReportFName()
the name of the report File
Definition: Model.h:99
static void setMaximumDeviationDerivative(ALIdouble val)
Definition: ALIUtils.h:90
static ErrorCorrelationMgr * getInstance()
const ALIstring & name() const
Definition: OpticalObject.h:58
#define FALSE
Definition: scimark2.h:16
static Fit & getInstance()
Definition: Fit.cc:69
static GlobalOptionMgr * getInstance()
static void setFittableEntries()
Definition: Fit.cc:382
void FillMeasurements()
MAT * MatNonConst() const
void dumpOptOs(std::ostream &out=std::cout) const
static Entry * getEntryByName(const ALIstring &opto_name, const ALIstring &entry_name)
--— Search an Entry name in the Entry* list and return a pointer to it
Definition: Model.cc:615
static double getEntryValue(const Entry *entry)
Definition: Fit.cc:1347
int nEvent
Definition: myFastSimVal.cc:45
static FittedEntriesReader * getFittedEntriesReader()
Definition: Model.h:254
ALIint NoLines() const
static ALIint _NoLinesA
Definition: Fit.h:177
const ALIstring longName() const
static OpticalObjectMgr * getInstance()
Get the only instance.
ALIint NoColumns() const
const std::vector< Entry * > & ExtraEntryList() const
Definition: OpticalObject.h:65
static ALIMatrix * AtWAMatrix
Definition: Fit.h:167
static ALIdouble theFitQualityCut
Definition: Fit.h:194
ALIint fitPos() const
Definition: Entry.h:58
int getGlobalOptionValue(const ALIstring &sstr, ALIdouble &val)
--— Search a string in theGlobalOptions and return 1 if found
bool ALIbool
Definition: CocoaGlobals.h:19
static void dumpFittedValuesInAllAncestorFrames(ALIFileOut &fileout, ALIbool printErrors=true, ALIbool printOrig=true)
Definition: Fit.cc:1251
dictionary corr
static FitQuality getFitQuality(const ALIbool canBeGood=TRUE)
Definition: Fit.cc:915
static void substractLastDisplacementToEntries(const ALIdouble factor)
Definition: Fit.cc:1159
Definition: Fit.h:32
static ALIint MaxNoFitIterations
Definition: Fit.h:202
T sqrt(T t)
Definition: SSEVec.h:19
static ALIint findEntryFitPosition(const ALIstring &opto_name, const ALIstring &entry_name)
Definition: Fit.cc:1487
static ALIMatrix * WMatrix
Definition: Fit.h:166
static std::string GetMeasurementName(int meas)
Definition: Fit.cc:1734
static void setFirstTime(ALIbool val)
Definition: ALIUtils.h:88
static ALIdouble thePreviousIterationFitQuality
Definition: Fit.h:191
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static ALIMatrix * DaMatrix
Definition: Fit.h:169
static void PropagateErrors()
Definition: Fit.cc:457
virtual bool ReadNextEvent()=0
static ALIbool fitNextEvent(ALIuint &nEvent)
Definition: Fit.cc:144
static ALIMatrix * yfMatrix
Definition: Fit.h:174
static Fit * instance
Definition: Fit.h:162
static FittedEntriesManager * getInstance()
static ALIuint nEvent
Definition: Fit.h:208
void SetCorrelation(ALIint i1, ALIint i2, ALIdouble corr)
const std::vector< Entry * > & CoordinateEntryList() const
Definition: OpticalObject.h:63
isFirst
Definition: cuy.py:418
static void WriteVisualisationFiles()
Definition: Fit.cc:354
static ALIint _NoColumnsA
Definition: Fit.h:178
ii
Definition: cuy.py:589
MatrixMeschach ALIMatrix
static void setApply(ALIbool val)
const MAT * Mat() const
static void FillMatricesWithMeasurements()
Definition: Fit.cc:638
void ostrDump(std::ostream &fout, const ALIstring &mtext)
static void CreateMatrices()
Definition: Fit.cc:570
static void redoMatrices()
Definition: Fit.cc:442
static NtupleManager * getInstance()
static void dumpMatrices()
Definition: Fit.cc:1467
static void dumpFittedValues(ALIFileOut &fileout, ALIbool printErrors=true, ALIbool printOrig=true)
Definition: Fit.cc:1185
static void dumpEntryCorrelations(ALIFileOut &file)
Definition: Fit.cc:1415
const CLHEP::HepRotation & rmGlob() const
Definition: OpticalObject.h:83
static CocoaDBMgr * getInstance()
Definition: CocoaDBMgr.cc:38
void AddData(ALIuint col, ALIuint lin, ALIdouble data)
static void startFit()
Definition: Fit.cc:103
static ALIint report
Definition: ALIUtils.h:33
static ALIdouble cameraScaleFactor
Definition: Measurement.h:158
static ALIbool getFirstTime()
Definition: ALIUtils.h:87
static void deleteMatrices()
Definition: Fit.cc:553
static void CheckIfFitPossible()
Definition: Fit.cc:1588
std::map< ALIstring, ALIdouble, std::less< ALIstring > > & GlobalOptions()
static void printRotationAnglesInOptOFrame(const OpticalObject *opto, const OpticalObject *optoAncestor, ALIFileOut &fileout, ALIbool printErrors=true, ALIbool printOrig=true)
Definition: Fit.cc:1332
void FillOptObjects(MatrixMeschach *AtWAMatrix)
static int CheckIfMeasIsProportionalToAnother(ALIuint measNo)
Definition: Fit.cc:1681
Fit()
Definition: Fit.h:35
std::string ALIstring
Definition: CocoaGlobals.h:9
void FillFitParameters(MatrixMeschach *AtWAMatrix)
static ALIdouble theRelativeFitQualityCut
Definition: Fit.h:197
const OpticalObject * parent() const
Definition: OpticalObject.h:60
static void calculateSimulatedMeasurementsWithOriginalValues()
Definition: Fit.cc:532
static ALIstring & MatricesFName()
the name of the File for storing the matrices
Definition: Model.h:102
static void PrintChi2(ALIdouble fit_quality, ALIbool isFirst)
Definition: Fit.cc:1517
static FitQuality fitParameters(const double daFactor)
Definition: Fit.cc:407
static std::vector< Measurement * > & MeasurementList()
Definition: Model.h:88
FitQuality
Definition: Fit.h:30
static ALIFileOut & getInstance(const ALIstring &filename)
Definition: ALIFileOut.cc:18
static std::vector< Entry * > & EntryList()
Definition: Model.h:86
void Dump(const ALIstring &mtext)
static void addDaMatrixToEntries()
Definition: Fit.cc:1105
static ALIdouble GetSChi2(ALIbool useDa)
Definition: Fit.cc:1034
static ALIint theNoFitIterations
Definition: Fit.h:200
static void dumpDimensions(std::ofstream &fout)
Definition: ALIUtils.cc:308
void FillNtupleTree()
const ALIstring & type() const
Definition: OpticalObject.h:59
unsigned int ALIuint
Definition: CocoaGlobals.h:17
def exit(msg="")
static void FillMatricesWithCalibratedParameters()
Definition: Fit.cc:724
static time_t time_now()
Definition: ALIUtils.h:39