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  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
891  //------------------ Number of parameters 'cal'
892  // (= No parameters to be fitted - No parameters 'unk' )
893  if ((*vecite)->quality() >= theMinimumEntryQuality) {
894  if (ALIUtils::debug >= 4) {
895  std::cout << nEnt << "PARAM" << std::setw(26) << (*vecite)->OptOCurrent()->name().c_str() << std::setw(8) << " "
896  << (*vecite)->name().c_str() << " " << std::setw(8) << " " << (*vecite)->value() << " "
897  << std::setw(8) << sqrt(AtWAMatrix->Mat()->me[nEnt][nEnt]) / (*vecite)->OutputSigmaDimensionFactor()
898  << " " << (*vecite)->sigma() / (*vecite)->OutputSigmaDimensionFactor() << " Q" << (*vecite)->quality()
899  << std::endl;
900  }
901  nEnt++;
902  }
903  }
904 
905  if (ALIUtils::debug >= 5)
906  yfMatrix->Dump("PD(y-f)Matrix final");
907 }
908 
909 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
910 //@@ check also that the fit_quality = SMat(0,0) is smaller for each new iteration
911 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
913  if (ALIUtils::debug >= 3)
914  std::cout << "@@@ Fit::getFitQuality" << std::endl;
915 
916  double fit_quality = GetSChi2(true);
917 
918  //---------- Calculate DS = Variable to recognize convergence (distance to minimum)
919  ALIMatrix* DatMatrix = new ALIMatrix(*DaMatrix);
920  // delete DaMatrix; //op
921  DatMatrix->transpose();
922  if (ALIUtils::debug >= 5)
923  DatMatrix->Dump("DatMatrix");
924  //op ALIMatrix* DSMat = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix * *PDMatrix);
925  ALIMatrix* DSMat = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix * *yfMatrix);
926  if (ALIUtils::debug >= 5) {
927  ALIMatrix* DSMattemp = new ALIMatrix(*DatMatrix * *AtMatrix * *WMatrix);
928  DSMattemp->Dump("DSMattempMatrix=Dat*At*W");
929  ALIMatrix* DSMattemp2 = new ALIMatrix(*AtMatrix * *WMatrix * *yfMatrix);
930  DSMattemp2->Dump("DSMattempMatrix2=At*W*yf");
931  ALIMatrix* DSMattemp3 = new ALIMatrix(*AtMatrix * *WMatrix);
932  DSMattemp3->Dump("DSMattempMatrix3=At*W");
933  AtMatrix->Dump("AtMatrix");
934  }
935 
936  /* for( int ii = 0; ii < DatMatrix->NoColumns(); ii++ ){
937  std::cout << ii << " DS term " << (*DatMatrix)(0,ii) * (*DSMattemp2)(ii,0) << std::endl;
938  }*/
939  // delete AtMatrix; //op
940  // delete WMatrix; //op
941 
942  //op if(ALIUtils::debug >= 5) (*PDMatrix).Dump("PDMatrix");
943  if (ALIUtils::debug >= 5)
944  (*yfMatrix).Dump("yfMatrix");
945  if (ALIUtils::debug >= 5)
946  DSMat->Dump("DSMatrix final");
947  // delete yfMatrix; //op
948 
949  ALIdouble fit_quality_cut = (*DSMat)(0, 0);
950  //- ALIdouble fit_quality_cut =std::abs( (*DSMat)(0,0) );
951  delete DSMat;
952  if (ALIUtils::debug >= 0)
954  << " Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
955  if (ALIUtils::report >= 2) {
957  fileout << theNoFitIterations
958  << " Fit quality predicted improvement in distance to minimum is = " << fit_quality_cut << std::endl;
959  }
960 
961  //- double fit_quality_cut = thePreviousIterationFitQuality - fit_quality;
962  //- double fit_quality_cut = fit_quality;
963  //- std::cout << " fit_quality_cut " << fit_quality_cut << " fit_quality " << fit_quality << std::endl;
964 
965  //----- Check quality
966  time_t now;
967  now = clock();
968  if (ALIUtils::debug >= 0)
969  std::cout << "TIME:QUALITY_CHECKED: " << now << " " << difftime(now, ALIUtils::time_now()) / 1.E6 << std::endl;
971 
972  FitQuality fitQuality;
973 
974  //----- Chi2 is bigger, bad
975  // if( theNoFitIterations != 0 && fit_quality_cut > 0. ) {
976  if (fit_quality_cut < 0.) {
977  fitQuality = FQchiSquareWorsened;
978  if (ALIUtils::debug >= 1)
979  std::cerr << "!!WARNING: Fit quality has worsened: Fit Quality now = " << fit_quality << " before "
980  << thePreviousIterationFitQuality << " diff " << fit_quality - thePreviousIterationFitQuality
981  << std::endl;
982 
983  //----- Chi2 is smaller, check if we make another iteration
984  } else {
985  ALIdouble rel_fit_quality = std::abs(thePreviousIterationFitQuality - fit_quality) / fit_quality;
986  //----- Small chi2 change: end
987  if ((fit_quality_cut < theFitQualityCut || rel_fit_quality < theRelativeFitQualityCut) && canBeGood) {
988  if (ALIUtils::debug >= 2)
989  std::cout << "$$ Fit::getFitQuality good " << fit_quality_cut << " <? " << theFitQualityCut << " || "
990  << rel_fit_quality << " <? " << theRelativeFitQualityCut << " GOOD " << canBeGood << std::endl;
991  fitQuality = FQsmallDistanceToMinimum;
992  if (ALIUtils::report >= 1) {
994  fileout << "STOP: SMALL IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " < "
995  << theFitQualityCut << " OR (RELATIVE) " << rel_fit_quality << " < " << theRelativeFitQualityCut
996  << std::endl;
997  }
998  if (ALIUtils::debug >= 4) {
999  std::cout << "STOP: SMALL IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut << " < "
1000  << theFitQualityCut << " OR (RELATIVE) " << rel_fit_quality << " < " << theRelativeFitQualityCut
1001  << std::endl;
1002  }
1003 
1004  //----- Big chi2 change: go to next iteration
1005  } else {
1006  if (ALIUtils::debug >= 2)
1007  std::cout << "$$ Fit::getFitQuality bad " << fit_quality_cut << " <? " << theFitQualityCut << " || "
1008  << rel_fit_quality << " <? " << theRelativeFitQualityCut << " GOOD " << canBeGood << std::endl;
1009  fitQuality = FQbigDistanceToMinimum;
1010  //----- set thePreviousIterationFitQuality for next iteration
1011  thePreviousIterationFitQuality = fit_quality;
1012 
1013  if (ALIUtils::report >= 2) {
1015  fileout << "CONTINUE: BIG IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut
1016  << " >= " << theFitQualityCut << " AND (RELATIVE) " << rel_fit_quality
1017  << " >= " << theRelativeFitQualityCut << std::endl;
1018  }
1019  if (ALIUtils::debug >= 4) {
1020  std::cout << "CONTINUE: BIG IMPROVEMENT IN ITERATION " << theNoFitIterations << " = " << fit_quality_cut
1021  << " >= " << theFitQualityCut << " AND (RELATIVE) " << rel_fit_quality
1022  << " >= " << theRelativeFitQualityCut << std::endl;
1023  }
1024  }
1025  }
1026 
1027  return fitQuality;
1028 }
1029 
1030 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1032  if (ALIUtils::debug >= 3)
1033  std::cout << "@@@ Fit::GetSChi2 useDa= " << useDa << std::endl;
1034 
1035  ALIMatrix* SMat = nullptr;
1036  if (useDa) {
1037  //----- Calculate variables to check quality of this set of parameters
1038 
1039  //----- Calculate Da = (At * W * A)-1 * At * W * (y-f)
1040  /*t DaMatrix = new ALIMatrix( *AtWAMatrix );
1041  *DaMatrix *= *AtMatrix * *WMatrix;
1042  if(ALIUtils::debug >= 5) DaMatrix->Dump("DaMatrix before yf ");
1043  *DaMatrix *= *yfMatrix;
1044  if(ALIUtils::debug >= 5) DaMatrix->Dump("DaMatrix");
1045  */
1046 
1047  DaMatrix = new ALIMatrix(0, 0);
1048  // if(ALIUtils::debug >= 5) AtWAMatrix->Dump("AtWAMatrix=0");
1049  *DaMatrix = (*AtWAMatrix * *AtMatrix * *WMatrix * *yfMatrix);
1050  if (ALIUtils::debug >= 5)
1051  DaMatrix->Dump("DaMatrix");
1052 
1053  //----- Calculate S = chi2 = Fit quality = r^T W r (r = residual = f + A*Da - y )
1054  //op ALIMatrix* tmpM = new ALIMatrix( *AMatrix * *DaMatrix + *PDMatrix );
1055  // ALIMatrix* tmpM = new ALIMatrix( *AMatrix * *DaMatrix + *yfMatrix );
1056 
1057  ALIMatrix* tmpM = new ALIMatrix(0, 0);
1058  *tmpM = *AMatrix * *DaMatrix - *yfMatrix;
1059  if (ALIUtils::debug >= 5)
1060  tmpM->Dump("A*Da + f - y Matrix ");
1061 
1062  ALIMatrix* tmptM = new ALIMatrix(*tmpM);
1063  tmptM->transpose();
1064  if (ALIUtils::debug >= 5)
1065  tmptM->Dump("tmptM after transpose");
1066  if (ALIUtils::debug >= 5)
1067  WMatrix->Dump("WMatrix");
1068 
1069  // std::cout << "smat " << std::endl;
1070  //o ALIMatrix* SMat = new ALIMatrix(*tmptM * *WMatrix * *tmpM);
1071  ALIMatrix* SMat1 = MatrixByMatrix(*tmptM, *WMatrix);
1072  // ALIMatrix* SMat1 = MatrixByMatrix(*AMatrix,*WMatrix);
1073  if (ALIUtils::debug >= 5)
1074  SMat1->Dump("(A*Da + f - y)^T * W Matrix");
1075  SMat = MatrixByMatrix(*SMat1, *tmpM);
1076  // std::cout << "smatc " << std::endl;
1077  delete tmpM;
1078  delete tmptM;
1079  if (ALIUtils::debug >= 5)
1080  SMat->Dump("SMatrix with Da");
1081  } else {
1082  ALIMatrix* yftMat = new ALIMatrix(*yfMatrix);
1083  yftMat->transpose();
1084  SMat = new ALIMatrix(*yftMat * *WMatrix * *yfMatrix);
1085  delete yftMat;
1086  if (ALIUtils::debug >= 5)
1087  SMat->Dump("SMatrix no Da");
1088  }
1089  ALIdouble fit_quality = (*SMat)(0, 0);
1090  delete SMat;
1091  if (ALIUtils::debug >= 5)
1092  std::cout << " GetSChi2 = " << fit_quality << std::endl;
1093 
1094  PrintChi2(fit_quality, !useDa);
1095 
1096  return fit_quality;
1097 }
1098 
1099 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1100 //@@ Correct entries with fitted values
1101 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1103  if (ALIUtils::debug >= 4) {
1104  std::cout << "@@ Adding correction (DaMatrix) to Entries " << std::endl;
1105  DaMatrix->Dump("DaMatrix =");
1106  }
1107 
1108  /*- Now there are other places where entries are changed
1109  if( ALIUtils::report >= 3 ) {
1110  ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
1111  fileout << std::endl << " CHANGE IN ENTRIES" << std::endl
1112  << " Optical Object Parameter xxxxxx " << std::endl;
1113  }*/
1114 
1115  ALIint nEnt = 0;
1116  std::vector<Entry*>::const_iterator vecite;
1117  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
1118  //------------------ Number of parameters 'cal'
1119  // (= No parameters to be fitted - No parameters 'unk' )
1120  //- std::cout << "qual" << (*vecite)->quality() << theMinimumEntryQuality << std::endl;
1121  if ((*vecite)->quality() >= theMinimumEntryQuality) {
1122  if (ALIUtils::debug >= 5) {
1123  std::cout << std::endl
1124  << " @@@ PENTRY change " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name() << " "
1125  << " change= " << (*DaMatrix)(nEnt, 0) << " value= " << (*vecite)->valueDisplacementByFitting()
1126  << std::endl;
1127  }
1128  /* if( ALIUtils::report >=3 ) {
1129  ALIFileOut& fileout = ALIFileOut::getInstance( Model::ReportFName() );
1130  fileout << "dd" << std::setw(30) << (*vecite)->OptOCurrent()->name()
1131  << std::setw(8) << " " << (*vecite)->name() << " "
1132  << std::setw(8) << " " << (*DaMatrix)(nEnt,0) / (*vecite)->OutputValueDimensionFactor()
1133  << " " << (*vecite)->valueDisplacementByFitting() / (*vecite)->OutputValueDimensionFactor() << " fitpos " << (*vecite)->fitPos()
1134  << std::endl;
1135  }*/
1136 
1137  //----- Store this displacement
1138  if (ALIUtils::debug >= 4)
1139  std::cout << " old valueDisplacementByFitting " << (*vecite)->name() << " "
1140  << (*vecite)->valueDisplacementByFitting() << " original value " << (*vecite)->value() << std::endl;
1141 
1142  (*vecite)->addFittedDisplacementToValue((*DaMatrix)(nEnt, 0));
1143 
1144  if (ALIUtils::debug >= 4)
1145  std::cout << nEnt << " new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() << " "
1146  << (*vecite)->name() << " = " << (*vecite)->valueDisplacementByFitting() << " "
1147  << (*DaMatrix)(nEnt, 0) << std::endl;
1148  nEnt++;
1149  }
1150  }
1151 }
1152 
1153 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1154 //@@ Delete the previous addition of fitted values (to try a new one daFactor times smaller )
1155 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1157  if (ALIUtils::debug >= 4) {
1158  std::cout << "@@ Fit::substractToHalfDaMatrixToEntries " << std::endl;
1159  }
1160 
1161  std::vector<Entry*>::const_iterator vecite;
1162  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
1163  if ((*vecite)->quality() >= theMinimumEntryQuality) {
1164  //-- (*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
1165 
1166  ALIdouble lastadd = (*vecite)->lastAdditionToValueDisplacementByFitting() * factor;
1167  //- if( lastadd < 0 ) lastadd *= -1;
1168  (*vecite)->addFittedDisplacementToValue(-lastadd);
1169  (*vecite)->setLastAdditionToValueDisplacementByFitting(-(*vecite)->lastAdditionToValueDisplacementByFitting());
1170  // (*vecite)->substractToHalfFittedDisplacementToValue();
1171 
1172  if (ALIUtils::debug >= 4)
1173  std::cout << " new valueDisplacementByFitting " << (*vecite)->OptOCurrent()->name() << " " << (*vecite)->name()
1174  << " = " << (*vecite)->valueDisplacementByFitting() << " " << std::endl;
1175  }
1176  }
1177 }
1178 
1179 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1180 //@@ Dump all the entries that have been fitted (those that were 'cal' or 'unk'
1181 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1182 void Fit::dumpFittedValues(ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig) {
1183  //---------- print
1184  if (ALIUtils::debug >= 0) {
1185  std::cout << "SRPRPOS "
1186  << " Optical Object "
1187  << " Parameter"
1188  << " Fit.Value "
1189  << " Orig.Value" << std::endl;
1190  }
1191  //---------- Dump header
1192  if (ALIUtils::debug >= 0)
1193  std::cout << std::endl << "FITTED VALUES " << std::endl;
1194  fileout << std::endl
1195  << "FITTED VALUES " << std::endl
1196  << "nEnt_unk"
1197  << " Optical Object"
1198  << " Parameter ";
1199  if (printErrors) {
1200  fileout << " value (+-error)"
1201  << " orig.val (+-error)";
1202  } else {
1203  fileout << " value "
1204  << " orig.val ";
1205  }
1206  fileout << " quality" << std::endl;
1207 
1208  //---------- Iterate over OptO list
1209  std::vector<Entry*> entries;
1210  // const Entry* entry;
1211  int ii, siz;
1212  std::vector<OpticalObject*>::const_iterator vocite;
1213  for (vocite = Model::OptOList().begin(); vocite != Model::OptOList().end(); ++vocite) {
1214  if ((*vocite)->type() == ALIstring("system"))
1215  continue;
1216 
1217  fileout << " %%%% Optical Object: " << (*vocite)->longName() << std::endl;
1218 
1219  entries = (*vocite)->CoordinateEntryList();
1220  siz = entries.size();
1221  if (siz != 6) {
1222  std::cerr << "!!! FATAL ERROR: strange number of coordinates = " << siz << std::endl;
1223  abort();
1224  }
1225 
1226  //----- 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)
1227  OpticalObject* opto = entries[0]->OptOCurrent();
1228  const OpticalObject* optoParent = opto->parent();
1229  printCentreInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1230 
1231  //----- Dump entry angles coordinates
1232  printRotationAnglesInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1233 
1234  //----- Dump extra entries
1235  entries = (*vocite)->ExtraEntryList();
1236  siz = entries.size();
1237  for (ii = 0; ii < siz; ii++) {
1238  double entryvalue = getEntryValue(entries[ii]);
1239  dumpEntryAfterFit(fileout, entries[ii], entryvalue, printErrors, printOrig);
1240  }
1241  }
1242 
1243  dumpEntryCorrelations(fileout);
1244 }
1245 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1246 //@@ Dump all the entries that have been fitted in reference frames of all ancestors
1247 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1248 void Fit::dumpFittedValuesInAllAncestorFrames(ALIFileOut& fileout, ALIbool printErrors, ALIbool printOrig) {
1249  //---------- print
1250  fileout << std::endl
1251  << "@@@@ FITTED VALUES IN ALL ANCESTORS " << std::endl
1252  << "nEnt_unk"
1253  << " Optical Object"
1254  << " Parameter ";
1255  if (printErrors) {
1256  fileout << " value (+-error)";
1257  if (printOrig) {
1258  fileout << " orig.val (+-error)";
1259  }
1260  } else {
1261  fileout << " value ";
1262  if (printOrig) {
1263  fileout << " orig.val ";
1264  }
1265  }
1266  fileout << " quality" << std::endl;
1267 
1268  //---------- Iterate over OptO list
1269  std::vector<Entry*> entries;
1270  std::vector<OpticalObject*>::const_iterator vocite;
1271  for (vocite = Model::OptOList().begin(); vocite != Model::OptOList().end(); ++vocite) {
1272  if ((*vocite)->type() == ALIstring("system"))
1273  continue;
1274 
1275  fileout << " %%%% Optical Object: " << (*vocite)->longName() << std::endl;
1276 
1277  entries = (*vocite)->CoordinateEntryList();
1278 
1279  //----- 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)
1280  OpticalObject* opto = *vocite;
1281  const OpticalObject* optoParent = opto->parent();
1282  do {
1283  fileout << " %% IN FRAME : " << optoParent->longName() << std::endl;
1284  printCentreInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1285 
1286  //----- Dump entry angles coordinates
1287  printRotationAnglesInOptOFrame(opto, optoParent, fileout, printErrors, printOrig);
1288  optoParent = optoParent->parent();
1289  } while (optoParent);
1290  }
1291 }
1292 
1293 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1295  const OpticalObject* optoAncestor,
1296  ALIFileOut& fileout,
1297  ALIbool printErrors,
1298  ALIbool printOrig) {
1299  CLHEP::Hep3Vector centreLocal;
1300  if (optoAncestor->type() == "system") {
1301  centreLocal = opto->centreGlob();
1302  } else {
1303  centreLocal = opto->centreGlob() - optoAncestor->centreGlob();
1304  CLHEP::HepRotation parentRmGlobInv = inverseOf(optoAncestor->rmGlob());
1305  centreLocal = parentRmGlobInv * centreLocal;
1306  }
1307  if (ALIUtils::debug >= 2) {
1308  std::cout << "CENTRE LOCAL " << opto->name() << " " << centreLocal << " GLOBL " << opto->centreGlob()
1309  << " parent GLOB " << optoAncestor->centreGlob() << std::endl;
1310  ALIUtils::dumprm(optoAncestor->rmGlob(), " parent rm ");
1311  }
1312  std::vector<Entry*> entries = opto->CoordinateEntryList();
1313  for (ALIuint ii = 0; ii < 3; ii++) {
1314  /* double entryvalue = getEntryValue( entries[ii] );
1315  ALIdouble entryvalue;
1316  if( ii == 0 ) {
1317  entryvalue = centreLocal.x();
1318  }else if( ii == 1 ) {
1319  entryvalue = centreLocal.y();
1320  }else if( ii == 2 ) {
1321  entryvalue = centreLocal.z();
1322  }*/
1324  fileout, entries[ii], centreLocal[ii] / entries[ii]->OutputValueDimensionFactor(), printErrors, printOrig);
1325  }
1326 }
1327 
1328 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1330  const OpticalObject* optoAncestor,
1331  ALIFileOut& fileout,
1332  ALIbool printErrors,
1333  ALIbool printOrig) {
1334  std::vector<Entry*> entries = opto->CoordinateEntryList();
1335  std::vector<double> entryvalues = opto->getRotationAnglesInOptOFrame(optoAncestor, entries);
1336  //- std::cout << " after return entryvalues[0] " << entryvalues[0] << " entryvalues[1] " << entryvalues[1] << " entryvalues[2] " << entryvalues[2] << std::endl;
1337  for (ALIuint ii = 3; ii < entries.size(); ii++) {
1339  fileout, entries[ii], entryvalues[ii - 3] / entries[ii]->OutputValueDimensionFactor(), printErrors, printOrig);
1340  }
1341 }
1342 
1343 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1345  double entryvalue;
1346  if (entry->type() == "angles") {
1347  if (ALIUtils::debug >= 2)
1348  std::cout << "WARNING valueDisplacementByFitting has no sense for angles " << std::endl;
1349 
1350  // commenting out the following line as it is a dead assignment due to the
1351  // subsequent assignment below
1352  // -> silence static analyzer warnings, but leaving the commented line in
1353  // case someone wants to actively use this code again
1354 
1355  // entryvalue = -999;
1356  }
1357  entryvalue = (entry->value() + entry->valueDisplacementByFitting()) / entry->OutputValueDimensionFactor();
1358  return entryvalue;
1359 }
1360 
1361 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1363  ALIFileOut& fileout, const Entry* entry, double entryvalue, ALIbool printErrors, ALIbool printOrig) {
1364  //- std::cout << " Fit::dumpEntryAfterFit " << entryvalue << std::endl;
1365  ALIdouble dimv = entry->OutputValueDimensionFactor();
1366  ALIdouble dims = entry->OutputSigmaDimensionFactor();
1367  //----- Dump to std::cout
1368  if (ALIUtils::debug >= 3) {
1369  std::cout << "ENTRY: " << std::setw(30) << entry->OptOCurrent()->name() << std::setw(8) << " " << entry->name()
1370  << " " << std::setw(8) << (entry->value() + entry->valueDisplacementByFitting()) << " " << entry->value()
1371  << " Q" << entry->quality() << std::endl;
1372  }
1373 
1374  if (entry->quality() == 2) {
1375  fileout << "UNK: " << entry->fitPos() << " ";
1376  } else if (entry->quality() == 1) {
1377  fileout << "CAL: " << entry->fitPos() << " ";
1378  // fileout << "CAL: -1 ";
1379  } else {
1380  fileout << "FIX: -1 ";
1381  }
1382 
1383  fileout << std::setw(30) << entry->OptOCurrent()->name() << std::setw(8) << " " << entry->name() << " "
1384  << std::setw(8) << std::setprecision(8) << entryvalue;
1385  if (entry->quality() >= theMinimumEntryQuality) {
1386  if (printErrors)
1387  fileout << " +- " << std::setw(8) << sqrt(AtWAMatrix->Mat()->me[entry->fitPos()][entry->fitPos()]) / dims;
1388  } else {
1389  if (printErrors)
1390  fileout << " +- " << std::setw(8) << 0.;
1391  }
1392  if (printOrig) {
1393  fileout << std::setw(8) << " " << entry->value() / dimv;
1394  if (printErrors)
1395  fileout << " +- " << std::setw(8) << entry->sigma() / dims << " Q" << entry->quality();
1396 
1397  if (ALIUtils::report >= 2) {
1398  float dif = (entry->value() + entry->valueDisplacementByFitting()) / dimv - entry->value() / dimv;
1399  if (std::abs(dif) < 1.E-9)
1400  dif = 0.;
1401  fileout << " DIFF= " << dif;
1402  // << " == " << ( entry->value() + entry->valueDisplacementByFitting() ) / dimv - entryvalue << " @@ " << ( entry->value() + entry->valueDisplacementByFitting() ) / dimv << " @@ " << entryvalue;
1403  } else {
1404  // fileout << std::endl;
1405  }
1406  }
1407 
1408  fileout << std::endl;
1409 }
1410 
1411 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1413  //----- Only dump correlations bigger than a factor
1414  ALIdouble minCorrel = 1.E-6;
1415  //----- Dump correlations
1416  fileout << std::endl
1417  << "CORRELATION BETWEEN 'unk' ENTRIES: (>= " << minCorrel << " )" << std::endl
1418  << "No_1 No_2 correlation " << std::endl;
1419 
1420  ALIuint i1, i2;
1421  std::vector<Entry*>::iterator veite1, veite2;
1422  std::string E1, E2;
1423  for (veite1 = Model::EntryList().begin(); veite1 != Model::EntryList().end(); ++veite1) {
1424  if ((*veite1)->quality() == 0) {
1425  continue;
1426  } else if ((*veite1)->quality() == 1) {
1427  E1 = "C";
1428  } else if ((*veite1)->quality() == 2) {
1429  E1 = "U";
1430  }
1431  i1 = (*veite1)->fitPos();
1432 
1433  for (veite2 = veite1 + 1; veite2 != Model::EntryList().end(); ++veite2) {
1434  i2 = (*veite2)->fitPos();
1435  if ((*veite2)->quality() == 0) {
1436  continue;
1437  } else if ((*veite2)->quality() == 1) {
1438  E2 = "C";
1439  } else if ((*veite2)->quality() == 2) {
1440  E2 = "U";
1441  }
1442  ALIdouble corr = AtWAMatrix->Mat()->me[i1][i2];
1443  ALIdouble corrf = corr / sqrt(AtWAMatrix->Mat()->me[i1][i1]) / sqrt(AtWAMatrix->Mat()->me[i2][i2]);
1444  if (std::abs(corrf) >= minCorrel) {
1445  if (ALIUtils::debug >= 0) {
1446  std::cout << "CORR:" << E1 << "" << E2 << " (" << i1 << ")"
1447  << " (" << i2 << ")"
1448  << " " << corrf << std::endl;
1449  }
1450  fileout << "CORR:" << E1 << "" << E2 << " (" << i1 << ")"
1451  << " (" << i2 << ")"
1452  << " " << corrf << std::endl;
1453  }
1454  }
1455  }
1456  //------- Dump optical object list
1457  if (ALIUtils::debug >= 2)
1459 }
1460 
1461 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1462 //@@ Dump matrices used for the fit
1463 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1465  //----- Dump matrices for this iteration
1467  // ofstream matout("matrices.out");
1468  matout << std::endl << " @@@@@@@@@@@@@@@ Iteration No : " << theNoFitIterations << std::endl;
1469  AMatrix->ostrDump(matout, "Matrix A");
1470  AtMatrix->ostrDump(matout, "Matrix At");
1471  WMatrix->ostrDump(matout, "Matrix W");
1472  AtWAMatrix->ostrDump(matout, "Matrix AtWA");
1473  //op VaMatrix->ostrDump( matout, "Matrix Va" );
1474  DaMatrix->ostrDump(matout, "Matrix Da");
1475  yfMatrix->ostrDump(matout, "Matrix y");
1476  //op fMatrix->ostrDump( matout, "Matrix f" );
1477  //op thePropagationMatrix->ostrDump( matout, "propagation Matrix " );
1478  matout.close();
1479 }
1480 
1481 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1482 //@@ findEntryFitPosition
1483 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1484 ALIint Fit::findEntryFitPosition(const ALIstring& opto_name, const ALIstring& entry_name) {
1485  ALIint fitposi = -99;
1486 
1487  OpticalObject* opto = Model::getOptOByName(opto_name);
1488  //- std::cout << "OPTO = " << opto->name() << std::endl;
1489  std::vector<Entry*>::const_iterator vecite;
1490  for (vecite = opto->CoordinateEntryList().begin(); vecite < opto->CoordinateEntryList().end(); ++vecite) {
1491  //- std::cout << "ENTRYLIST" << (*vecite)->name() << std::endl;
1492  if ((*vecite)->name() == entry_name) {
1493  //- std::cout << "FOUND " << std::endl;
1494  fitposi = (*vecite)->fitPos();
1495  }
1496  }
1497  for (vecite = opto->ExtraEntryList().begin(); vecite < opto->ExtraEntryList().end(); ++vecite) {
1498  //- std::cout << "ENTRYLIST" << (*vecite)->name() << std::endl;
1499  if ((*vecite)->name() == entry_name) {
1500  //- std::cout << "FOUND " << std::endl;
1501  fitposi = (*vecite)->fitPos();
1502  }
1503  }
1504 
1505  if (fitposi == -99) {
1506  std::cerr << "!!EXITING: entry name not found: " << entry_name << std::endl;
1507  exit(2);
1508  } else {
1509  return fitposi;
1510  }
1511 }
1512 
1513 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1515  double chi2meas = 0;
1516  double chi2cal = 0;
1517  ALIint nMeas = 0, nUnk = 0;
1518 
1519  //----- Calculate the chi2 of measurements
1520  std::vector<Measurement*>::const_iterator vmcite;
1521  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
1522  //--- Calculate Simulated Value Original
1523  for (ALIuint ii = 0; ii < ALIuint((*vmcite)->dim()); ii++) {
1524  nMeas++;
1525  double c2 = ((*vmcite)->value(ii) - (*vmcite)->valueSimulated(ii)) / (*vmcite)->sigma(ii);
1526  chi2meas += c2 * c2;
1527  if (ALIUtils::debug >= 2) {
1528  std::cout << c2 << " adding chi2meas " << chi2meas << " " << (*vmcite)->name() << ": " << ii
1529  << " (mm)R: " << (*vmcite)->value(ii) * 1000. << " S: " << (*vmcite)->valueSimulated(ii) * 1000.
1530  << " Diff= " << ((*vmcite)->value(ii) - (*vmcite)->valueSimulated(ii)) * 1000. << std::endl;
1531  }
1532  }
1533  }
1534 
1535  //----- Calculate the chi2 of calibrated parameters
1536  std::vector<Entry*>::iterator veite;
1537  for (veite = Model::EntryList().begin(); veite != Model::EntryList().end(); ++veite) {
1538  if ((*veite)->quality() == 2)
1539  nUnk++;
1540  if ((*veite)->quality() == 1) {
1541  double c2 = (*veite)->valueDisplacementByFitting() / (*veite)->sigma();
1542  //double c2 = (*veite)->value() / (*veite)->sigma();
1543  chi2cal += c2 * c2;
1544  if (ALIUtils::debug >= 2)
1545  std::cout << c2 << " adding chi2cal " << chi2cal << " " << (*veite)->OptOCurrent()->name() << " "
1546  << (*veite)->name() << std::endl;
1547  //- std::cout << " valueDisplacementByFitting " << (*veite)->valueDisplacementByFitting() << " sigma " << (*veite)->sigma() << std::endl;
1548  }
1549  }
1550 
1551  if (ALIUtils::report >= 1) {
1553  fileout << " Chi2= " << chi2meas + chi2cal << " / " << nMeas - nUnk << " dof "
1554  << " From measurements= " << chi2meas << " from calibrated parameters= " << chi2cal << std::endl;
1555  }
1556  if (ALIUtils::debug >= 3)
1557  std::cout << " quality Chi2 (no correlations) " << chi2meas + chi2cal << " " << chi2meas << " " << chi2cal
1558  << std::endl;
1559 
1560  if (!isFirst) {
1561  // double fit_quality_change = thePreviousIterationFitQuality - fit_quality;
1562 
1563  if (ALIUtils::debug >= 0) {
1564  std::cout << std::endl << "@@@@ Fit iteration " << theNoFitIterations << " ..." << std::endl;
1565  // std::cout << theNoFitIterations << " Chi2 improvement in this iteration = " << fit_quality_change << std::endl;
1566  }
1567  if (ALIUtils::report >= 1) {
1569  fileout << std::endl << "Fit iteration " << theNoFitIterations << " ..." << std::endl;
1570  // fileout << theNoFitIterations << " Chi2 improvement in this iteration = " << fit_quality_change << std::endl;
1571  }
1572  }
1573 
1574  //---- Print chi2
1575  if (ALIUtils::debug >= 0)
1576  std::cout << theNoFitIterations << " Chi2 after iteration = " << fit_quality << std::endl;
1577  if (ALIUtils::report >= 1) {
1578  //--------- Get report file handler
1580  fileout << theNoFitIterations << " Chi2 after iteration = " << fit_quality << std::endl;
1581  }
1582 }
1583 
1584 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1586  if (ALIUtils::debug >= 3)
1587  std::cout << "@@@ Fit::CheckIfFitPossible" << std::endl;
1588 
1589  //----- Check if there is an unknown parameter that is not affecting any measurement
1590  ALIint NolinMes = 0;
1591  std::vector<Measurement*>::const_iterator vmcite;
1592  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
1593  NolinMes += (*vmcite)->dim();
1594  }
1595 
1596  std::vector<Entry*>::const_iterator vecite;
1597  for (vecite = Model::EntryList().begin(); vecite != Model::EntryList().end(); ++vecite) {
1598  if (ALIUtils::debug >= 4)
1599  std::cout << "Fit::CheckIfFitPossible looping for entry " << (*vecite)->longName() << std::endl;
1600  if ((*vecite)->quality() == 2) {
1601  ALIint nCol = (*vecite)->fitPos();
1602  //--- Check all measurements
1603  ALIbool noDepend = TRUE;
1604  if (ALIUtils::debug >= 4)
1605  std::cout << "Fit::CheckIfFitPossible looping for entry " << nCol << std::endl;
1606  for (ALIint ii = 0; ii < NolinMes; ii++) {
1607  if (ALIUtils::debug >= 5)
1608  std::cout << " Derivative= (" << ii << "," << nCol << ") = " << (*AMatrix)(ii, nCol) << std::endl;
1609 
1610  if (std::abs((*AMatrix)(ii, nCol)) > ALI_DBL_MIN) {
1611  if (ALIUtils::debug >= 5)
1612  std::cout << "Fit::CheckIfFitIsPossible " << nCol << " " << ii << " = " << (*AMatrix)(ii, nCol)
1613  << std::endl;
1614  noDepend = FALSE;
1615  break;
1616  }
1617  }
1618  if (noDepend) {
1619  throw cms::Exception("SDFError")
1620  << "!!!FATAL ERROR: Fit::CheckIfFitPossible() no measurement depends on unknown entry "
1621  << (*vecite)->OptOCurrent()->name() << "/" << (*vecite)->name() << std::endl
1622  << "!!! Fit will not be possible! " << std::endl;
1623  }
1624  }
1625  }
1626 
1627  //------ 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!
1628 
1629  std::vector<Entry*>::const_iterator vecite1, vecite2;
1630  ALIint nLin = AMatrix->NoLines();
1631  ALIdouble derivPrec = ALI_DBL_MIN;
1632  //---------- Loop entries
1633  for (vecite1 = Model::EntryList().begin(); vecite1 != Model::EntryList().end(); ++vecite1) {
1634  if ((*vecite1)->quality() == 2) {
1635  vecite2 = vecite1;
1636  ++vecite2;
1637  for (; vecite2 != Model::EntryList().end(); ++vecite2) {
1638  if ((*vecite2)->quality() == 2) {
1639  ALIint fitpos1 = (*vecite1)->fitPos();
1640  ALIint fitpos2 = (*vecite2)->fitPos();
1641  if (ALIUtils::debug >= 5)
1642  std::cout << "Fit::CheckIfFitIsPossible checking " << (*vecite1)->longName() << " ( " << fitpos1 << " ) & "
1643  << (*vecite2)->longName() << " ( " << fitpos2 << " ) " << std::endl;
1644  ALIdouble prop = DBL_MAX;
1645  ALIbool isProp = TRUE;
1646  for (ALIint ii = 0; ii < nLin; ii++) {
1647  if (ALIUtils::debug >= 5)
1648  std::cout << "Fit::CheckIfFitIsPossible " << ii << " : " << (*AMatrix)(ii, fitpos1)
1649  << " ?= " << (*AMatrix)(ii, fitpos2) << std::endl;
1650  if (std::abs((*AMatrix)(ii, fitpos1)) < derivPrec) {
1651  if (std::abs((*AMatrix)(ii, fitpos2)) > derivPrec) {
1652  isProp = FALSE;
1653  break;
1654  }
1655  } else {
1656  ALIdouble propn = (*AMatrix)(ii, fitpos2) / (*AMatrix)(ii, fitpos1);
1657  if (prop != DBL_MAX && prop != propn) {
1658  isProp = FALSE;
1659  break;
1660  }
1661  prop = propn;
1662  }
1663  }
1664  if (isProp) {
1665  std::cerr << "!!!FATAL ERROR: Fit::CheckIfFitPossible() two entries for which the measurements have the "
1666  "same dependency (or 100% proportional) "
1667  << (*vecite1)->longName() << " & " << (*vecite2)->longName() << std::endl
1668  << "!!! Fit will not be possible! " << std::endl;
1669  throw cms::Exception("SDFError");
1670  }
1671  }
1672  }
1673  }
1674  }
1675 }
1676 
1677 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1679  int measProp = -1;
1680 
1681  std::set<ALIuint> columnsEqual;
1682  std::set<ALIuint> columnsEqualSave;
1683  ALIuint biggestColumn = 0;
1684  ALIdouble biggest = 0.;
1685  for (int ii = 0; ii < AMatrix->NoColumns(); ii++) {
1686  if (std::abs((*AMatrix)(measNo, ii)) > biggest) {
1687  biggest = std::abs((*AMatrix)(measNo, ii));
1688  biggestColumn = ii;
1689  }
1690  columnsEqualSave.insert(ii);
1691  }
1692 
1693  ALIdouble div;
1694 
1695  for (int jj = 0; jj < AMatrix->NoLines(); jj++) {
1696  if (jj == int(measNo))
1697  continue;
1698  columnsEqual = columnsEqualSave;
1699  // check if ratio of each column to 'biggestColumn' is the same as for the N measurement
1700  for (int ii = 0; ii < AMatrix->NoColumns(); ii++) {
1701  div = (*AMatrix)(measNo, ii) / (*AMatrix)(measNo, biggestColumn);
1702  if (std::abs((*AMatrix)(jj, ii)) > ALI_DBL_MIN &&
1703  std::abs(div - (*AMatrix)(jj, ii) / (*AMatrix)(jj, biggestColumn)) > ALI_DBL_MIN) {
1704  if (ALIUtils::debug >= 3)
1705  std::cout << "CheckIfMeasIsProportionalToAnother 2 columns = " << ii << " in " << measNo << " & " << jj
1706  << std::endl;
1707  } else {
1708  if (ALIUtils::debug >= 3)
1709  std::cout << "CheckIfMeasIsProportionalToAnother 2 columns != " << ii << " in " << measNo << " & " << jj
1710  << std::endl;
1711  // if it is not equal delete this column
1712  std::set<ALIuint>::iterator ite = columnsEqual.find(ii);
1713  if (ite != columnsEqual.end()) {
1714  columnsEqual.erase(ite);
1715  }
1716  }
1717  }
1718  // check if not all columns have been deleted from columnsEqual
1719  if (!columnsEqual.empty()) {
1720  if (ALIUtils::debug >= 3)
1721  std::cout << "CheckIfMeasIsProportionalToAnother " << measNo << " = " << jj << std::endl;
1722  measProp = jj;
1723  break;
1724  }
1725  }
1726 
1727  return measProp;
1728 }
1729 
1730 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1732  std::string measname = " ";
1733 
1734  std::cout << " imeas " << imeas << std::endl;
1735  int Aline = 0;
1736  std::vector<Measurement*>::const_iterator vmcite;
1737  for (vmcite = Model::MeasurementList().begin(); vmcite != Model::MeasurementList().end(); ++vmcite) {
1738  for (ALIuint jj = 0; jj < ALIuint((*vmcite)->dim()); jj++) {
1739  if (Aline == imeas) {
1740  char ctmp[20];
1741  gcvt(jj, 10, ctmp);
1742  return ((*vmcite)->name()) + ":" + std::string(ctmp);
1743  }
1744  Aline++;
1745  }
1746  }
1747 
1748  std::cout << " return measname " << measname << std::endl;
1749  return measname;
1750 }
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:1294
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:1362
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:1344
int nEvent
Definition: myFastSimVal.cc:44
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:1248
dictionary corr
static FitQuality getFitQuality(const ALIbool canBeGood=TRUE)
Definition: Fit.cc:912
static void substractLastDisplacementToEntries(const ALIdouble factor)
Definition: Fit.cc:1156
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:1484
static ALIMatrix * WMatrix
Definition: Fit.h:166
static std::string GetMeasurementName(int meas)
Definition: Fit.cc:1731
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:1464
static void dumpFittedValues(ALIFileOut &fileout, ALIbool printErrors=true, ALIbool printOrig=true)
Definition: Fit.cc:1182
static void dumpEntryCorrelations(ALIFileOut &file)
Definition: Fit.cc:1412
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:1585
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:1329
void FillOptObjects(MatrixMeschach *AtWAMatrix)
static int CheckIfMeasIsProportionalToAnother(ALIuint measNo)
Definition: Fit.cc:1678
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:1514
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:1102
static ALIdouble GetSChi2(ALIbool useDa)
Definition: Fit.cc:1031
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