CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MinL3AlgoUnivErr.h
Go to the documentation of this file.
1 #ifndef MinL3AlgoUnivErr_H
2 #define MinL3AlgoUnivErr_H
3 
4 //=============================================================================
5 
69 //=============================================================================
70 
71 #include <vector>
72 #include <iostream>
73 #include <map>
74 #include <math.h>
75 
76 //=============================================================================
77 template <class IDdet>
78 class MinL3AlgoUnivErr
79 {
80 public:
81  typedef std::map<IDdet,float> IDmapF;
82  typedef typename IDmapF::value_type IDmapFvalue;
83  typedef typename IDmapF::iterator iter_IDmapF;
84  typedef typename IDmapF::const_iterator citer_IDmapF;
85  typedef std::map<IDdet,int> IDmapI;
86  typedef typename IDmapI::value_type IDmapIvalue;
87  typedef typename IDmapI::iterator iter_IDmapI;
88  typedef typename IDmapI::const_iterator citer_IDmapI;
89 
90  //----------------------------------------------
93 
94  MinL3AlgoUnivErr(float kweight_ = 0.);
95 
96 
97  //----------------------------------------------
99 
100  ~MinL3AlgoUnivErr();
101 
102 
103  //----------------------------------------------
116 
117  IDmapF iterate( const std::vector<std::vector<float> > & eventMatrix,
118  const std::vector<std::vector<IDdet> > & idMatrix,
119  const std::vector<float> & energyVector,
120  const int & nIter,
121  const bool & normalizeFlag
122  = false,
123  const int & nSubsamples = 10
124  );
125  //----------------------------------------------
133 
134  float getError( IDdet id ) const;
135 
136  //----------------------------------------------
145 
146  IDmapF getError() const;
147 
148  //----------------------------------------------
156 
157  int numberOfWrongErrors() const;
158 
159  //----------------------------------------------
170 
171  float getErrorQuality( IDdet id ) const;
172 
173  IDmapF getErrorQuality() const;
174 
175  //----------------------------------------------
181 
182  float getMeanPartialSolution( IDdet id ) const;
183 
184  //----------------------------------------------
189 
190  IDmapF getMeanPartialSolution() const;
191 
192  //----------------------------------------------
194 
195  void addEvent( const std::vector<float> & myCluster,
196  const std::vector<IDdet> & idCluster,
197  const float & energy
198  );
199 
200  //----------------------------------------------
203 
204  std::vector<float>
205  recalibrateEvent( const std::vector<float> & myCluster,
206  const std::vector<IDdet> & idCluster,
207  const IDmapF & newCalibration,
208  const int & isol = -1,
209  const int & iter = -1
210  );
211 
212  //----------------------------------------------
215 
216  IDmapF getSolution( const bool resetsolution=true );
217 
218  //----------------------------------------------
220 
221  void resetSolution();
222 
223  //----------------------------------------------
224 
225 private:
226 
227  float kweight;
228  int countEvents;
229  IDmapF wsum;
230  IDmapF Ewsum;
231  IDmapI idToIndex; // map: cell id -> index of cell info
232  // in sumPartSolu... vectors
233  std::vector<int> sumPartSolu0; // number of partial solutions
234  std::vector<float> sumPartSolu1; // sum of partial solutions
235  std::vector<float> sumPartSolu2; // sum of squared partial solutions
236 
237  int nOfSubsamples ; // a copy of an input argument of `iterate' function
238 
239  //----------------------------------------------
240  // register a partial solution in data members
241  void registerPartialSolution( const IDmapF & partialSolution );
242 
243  //----------------------------------------------
244 
245 }; //end of class MinL3AlgoUnivErr prototype
246 
247 
248 //=============================================================================
249 
250 
251 template<class IDdet>
252 MinL3AlgoUnivErr<IDdet>::MinL3AlgoUnivErr(float kweight_)
253  :kweight(kweight_), countEvents(0)
254 {
255  std::cout << "MinL3AlgoUnivErr : L3 algo with a calculation"
256  << " of stat.errors working..." << std::endl;
257  resetSolution();
258 }
259 
260 //=============================================================================
261 
262 template<class IDdet>
263 MinL3AlgoUnivErr<IDdet>::~MinL3AlgoUnivErr()
264 {
265 }
266 
267 //=============================================================================
268 
269 template<class IDdet>
270 typename MinL3AlgoUnivErr<IDdet>::IDmapF
271 MinL3AlgoUnivErr<IDdet>::
272 iterate( const std::vector < std::vector<float> > & eventMatrix,
273  const std::vector < std::vector<IDdet> > & idMatrix,
274  const std::vector <float> & energyVector,
275  const int & nIter,
276  const bool & normalizeFlag,
277  const int & nSubsamples
278  )
279 {
280  // clear the data members which are filled inside the function
281  // (in registerPartialSolution called from here)
282 
283  nOfSubsamples = nSubsamples; // keep the input argument for use in other
284  // functions
285 
286  idToIndex .clear();
287  sumPartSolu0 .clear();
288  sumPartSolu1 .clear();
289  sumPartSolu2 .clear();
290 
291  IDmapF totalSolution;
292 
293  // Loop over samples/solutions:
294  // isol = 0 : all events with the solution stored in
295  // totalSolution
296  // isol = 1,...,nSubsamples : partial solutions are found for sub-samples
297  // with the info on the solutions stored in the
298  // data members
299  // idToIndex, sumPartSolu...
300  // in order to be able to estimate the stat.
301  // errors later
302 
303  for (int isol = 0 ; isol <= nSubsamples; isol++) {
304 
305  IDmapF sampleSolution ; // solution for the sample
306  IDmapF iterSolution ; // intermediate solution after an iteration
307  std::vector < std::vector<float> > myEventMatrix ;
308  std::vector < std::vector<IDdet> > myIdMatrix ;
309  std::vector <float> myEnergyVector ;
310 
311  // Select the sample.
312  // Fill myEventMatrix, myIdMatrix and myEnergyVector
313  // either with all evs or with independent event subsamples
314 
315  if (isol == 0) // total solution
316  {
317  myEventMatrix = eventMatrix ;
318  myIdMatrix = idMatrix ;
319  myEnergyVector = energyVector ;
320  }
321  else // partial solution # isol
322  {
323  // clear containers filled for the previous sample
324  sampleSolution .clear() ;
325  myEventMatrix .clear() ;
326  myIdMatrix .clear() ;
327  myEnergyVector .clear() ;
328 
329  for (int i = 0; i < static_cast<int>( eventMatrix.size() ); i++)
330  {
331  // select every nSubsamples'th event to the subsample
332  if ( i % nSubsamples + 1 == isol )
333  {
334  myEventMatrix .push_back (eventMatrix [i]) ;
335  myIdMatrix .push_back (idMatrix [i]) ;
336  myEnergyVector .push_back (energyVector [i]) ;
337  }
338  }
339  }
340 
341  int Nevents = myEventMatrix.size(); // Number of events to calibrate with
342  countEvents = 0;
343 
344  int i;
345 
346  // Iterate the correction
347  for (int iter=1 ; iter <= nIter ; iter++)
348  {
349 
350  // if normalization flag is set, normalize energies
351  float sumOverEnergy;
352  if (normalizeFlag)
353  {
354  float scale = 0.;
355 
356  for (i=0; i<Nevents; i++)
357  {
358  sumOverEnergy = 0.;
359  for (unsigned j=0 ; j < myEventMatrix[i].size() ; j++)
360  {sumOverEnergy += myEventMatrix[i][j];}
361  sumOverEnergy /= myEnergyVector[i];
362  scale += sumOverEnergy;
363  }
364  scale /= Nevents;
365 
366  for (i=0; i<Nevents; i++) {myEnergyVector[i] *= scale;}
367  } // end normalize energies
368 
369  // now the real work starts:
370  for (int iEvt=0; iEvt < Nevents; iEvt++)
371  {
372  addEvent( myEventMatrix[iEvt], myIdMatrix[iEvt]
373  , myEnergyVector[iEvt] );
374  }
375  iterSolution = getSolution();
376  if (iterSolution.empty())
377  { sampleSolution.clear();
378  break; // exit the iteration loop leaving sampleSolution empty
379  }
380 
381  // re-calibrate eventMatrix with solution
382  for (int ievent = 0; ievent<Nevents; ievent++)
383  {
384  myEventMatrix[ievent] = recalibrateEvent (myEventMatrix[ievent],
385  myIdMatrix[ievent],
386  iterSolution,
387  isol,iter);
388  }
389 
390  // save solution into the sampleSolution map
391  for (iter_IDmapF i = iterSolution.begin(); i != iterSolution.end(); i++)
392  {
393  iter_IDmapF itotal = sampleSolution.find(i->first);
394  if (itotal == sampleSolution.end())
395  {
396  sampleSolution.insert(IDmapFvalue(i->first,i->second));
397  }
398  else
399  {
400  itotal->second *= i->second;
401  }
402  }
403 
404  // resetSolution(); // reset for new iteration,
405  // now: getSolution does it automatically if not vetoed
406  } // end iterate correction
407 
408  if (isol == 0) // total solution
409  {
410  totalSolution = sampleSolution;
411  }
412  else // partial solution => register it in sumPartSolu...
413  {
414  registerPartialSolution( sampleSolution );
415  }
416 
417  } // end of the loop over solutions/samples
418 
419  return totalSolution;
420 }
421 
422 
423 //=============================================================================
424 
425 template<class IDdet>
426 void MinL3AlgoUnivErr<IDdet>::
427 addEvent( const std::vector<float> & myCluster,
428  const std::vector<IDdet> & idCluster,
429  const float & energy
430  )
431 {
432  countEvents++;
433 
434  float w, invsumXmatrix;
435  float eventw;
436 
437  // Loop over the crystal matrix to find the sum
438  float sumXmatrix=0.;
439  for (unsigned i=0; i<myCluster.size(); i++) { sumXmatrix += myCluster[i]; }
440 
441  // event weighting
442  eventw = 1 - fabs(1 - sumXmatrix/energy);
443  eventw = pow(eventw,kweight);
444 
445  if (sumXmatrix != 0.)
446  {
447  invsumXmatrix = 1/sumXmatrix;
448  // Loop over the crystal matrix (3x3,5x5,7x7) again
449  // and calculate the weights for each xtal
450  for (unsigned i=0; i<myCluster.size(); i++)
451  {
452  w = myCluster[i] * invsumXmatrix;
453 
454  // include the weights into wsum, Ewsum
455  iter_IDmapF iwsum = wsum.find(idCluster[i]);
456  if (iwsum == wsum.end())
457  wsum.insert(IDmapFvalue(idCluster[i],w*eventw));
458  else iwsum->second += w*eventw;
459 
460  iter_IDmapF iEwsum = Ewsum.find(idCluster[i]);
461  if (iEwsum == Ewsum.end())
462  Ewsum.insert(IDmapFvalue (idCluster[i],
463  (w*eventw * energy * invsumXmatrix)
464  ));
465  else iEwsum->second += (w*eventw * energy * invsumXmatrix);
466  }
467  }
468  // else {std::cout << " Debug: dropping null event: " << countEvents << std::endl;}
469 }
470 
471 //=============================================================================
472 
473 template<class IDdet>
474 typename MinL3AlgoUnivErr<IDdet>::IDmapF
475 MinL3AlgoUnivErr<IDdet>::
476 getSolution( const bool resetsolution )
477 {
478  IDmapF solution;
479 
480  for (iter_IDmapF i = wsum.begin(); i != wsum.end(); i++)
481  {
482  iter_IDmapF iEwsum = Ewsum.find(i->first);
483  float myValue = 1;
484  if (i->second != 0) myValue = iEwsum->second / i->second;
485 
486  solution.insert( IDmapFvalue(i->first,myValue) );
487  }
488 
489  if (resetsolution) resetSolution();
490 
491  return solution;
492 }
493 
494 //=============================================================================
495 
496 template<class IDdet>
497 void MinL3AlgoUnivErr<IDdet>::resetSolution()
498 {
499  wsum.clear();
500  Ewsum.clear();
501 }
502 
503 //=============================================================================
504 
505 template<class IDdet>
506 std::vector<float>
507 MinL3AlgoUnivErr<IDdet>::
508 recalibrateEvent( const std::vector <float> & myCluster,
509  const std::vector <IDdet> & idCluster,
510  const IDmapF & newCalibration,
511  const int & isol, // for a printout only
512  const int & iter // for a printout only
513  )
514 {
515  std::vector<float> newCluster(myCluster);
516 
517  for (unsigned i=0; i<myCluster.size(); i++)
518  {
519  citer_IDmapF icalib = newCalibration.find(idCluster[i]);
520  if (icalib != newCalibration.end())
521  {
522  newCluster[i] *= icalib->second;
523  }
524  else
525  {
526  std::cout << "No calibration available for this element."
527  << std::endl;
528  std::cout << " isol = " << isol
529  << " iter = " << iter
530  << " idCluster[i] = " << idCluster[i] << "\n";
531  }
532 
533  }
534 
535  return newCluster;
536 }
537 
538 //=============================================================================
539 
540 template<class IDdet>
541 void
542 MinL3AlgoUnivErr<IDdet>::
543 registerPartialSolution( const IDmapF & partialSolution )
544 
545 {
546  int lastIndex = sumPartSolu0.size() - 1; // index of the last element
547  // of the parallel vectors
548 
549  for (citer_IDmapF cell = partialSolution.begin() ;
550  cell != partialSolution. end() ; ++cell)
551  {
552  IDdet id = cell->first ;
553  float corr = cell->second;
554 
555  // where is the cell in another map?
556  iter_IDmapI cell2 = idToIndex.find( id );
557 
558  if ( cell2 == idToIndex.end() )
559  {
560  // the cell is met for the first time in patial solutions
561  // => insert the info to the end of the vectors
562 
563  sumPartSolu0.push_back( 1 );
564  sumPartSolu1.push_back( corr );
565  sumPartSolu2.push_back( corr * corr );
566  idToIndex.insert( IDmapIvalue( id, ++lastIndex ) );
567  }
568  else
569  {
570  // add the info to the already registered cell
571 
572  int index = cell2 -> second;
573  sumPartSolu0[ index ] += 1;
574  sumPartSolu1[ index ] += corr;
575  sumPartSolu2[ index ] += corr * corr;
576  }
577 
578  }
579 
580 }
581 
582 //=============================================================================
583 
584 template<class IDdet>
585 float
586 MinL3AlgoUnivErr<IDdet>::
587 getError( IDdet id ) const
588 
589 {
590  float error;
591  citer_IDmapI cell = idToIndex.find( id );
592  if ( cell == idToIndex.end() ) error = -2.; // no info for the cell
593  else
594  {
595  int i = cell->second;
596  int n = sumPartSolu0[ i ];
597  if (n <= 1 ) error = -1.; // 1 entry => error estimate impossible
598  else
599  {
600  float meanX = sumPartSolu1[ i ] / n;
601  float meanX2 = sumPartSolu2[ i ] / n;
602 
603  error = sqrt( fabs(meanX2 - meanX * meanX) / (n - 1.) ) ;
604  }
605 
606  }
607  return error;
608 }
609 
610 //=============================================================================
611 
612 template<class IDdet>
613 typename MinL3AlgoUnivErr<IDdet>::IDmapF
614 MinL3AlgoUnivErr<IDdet>::
615 getError() const
616 
617 {
618  IDmapF errors;
619  float error;
620 
621  for (citer_IDmapI cell = idToIndex.begin();
622  cell != idToIndex. end(); ++cell )
623  {
624  int i = cell->second;
625  int n = sumPartSolu0[ i ];
626  if (n <= 1 ) error = -1.; // 1 entry => error estimate impossible
627  else
628  {
629  float meanX = sumPartSolu1[ i ] / n;
630  float meanX2 = sumPartSolu2[ i ] / n;
631 
632  error = sqrt( fabs(meanX2 - meanX * meanX) / (n - 1) ) ;
633  }
634 
635  errors.insert( IDmapFvalue( cell->first , error ) );
636  }
637  return errors;
638 }
639 //=============================================================================
640 
641 template<class IDdet>
642 float
643 MinL3AlgoUnivErr<IDdet>::
644 getErrorQuality( IDdet id ) const
645 
646 {
647  float fraction;
648  citer_IDmapI cell = idToIndex.find( id );
649  if ( cell == idToIndex.end() ) fraction = -1.; // no info for the cell
650  // => return a special value
651  else
652  {
653  int i = cell->second;
654  int n = sumPartSolu0[ i ];
655  if (n < nOfSubsamples ) fraction = float( n ) / nOfSubsamples;
656  else fraction = 1.;
657  }
658  return fraction;
659 }
660 
661 //=============================================================================
662 
663 template<class IDdet>
664 typename MinL3AlgoUnivErr<IDdet>::IDmapF
665 MinL3AlgoUnivErr<IDdet>::
666 getErrorQuality() const
667 
668 {
669  IDmapF fractions;
670  float fraction;
671 
672  for (citer_IDmapI cell = idToIndex.begin();
673  cell != idToIndex. end(); ++cell )
674  {
675  int i = cell->second;
676  int n = sumPartSolu0[ i ];
677 
678  if (n < nOfSubsamples ) fraction = float( n ) / nOfSubsamples;
679  else fraction = 1.;
680 
681  fractions.insert( IDmapFvalue( cell->first , fraction ) );
682  }
683 
684  return fractions;
685 }
686 
687 
688 //=============================================================================
689 
690 template<class IDdet>
691 int
692 MinL3AlgoUnivErr<IDdet>::
693 numberOfWrongErrors() const
694 
695 {
696  int nWrong = 0 ;
697 
698  for (citer_IDmapI cell = idToIndex.begin();
699  cell != idToIndex. end(); ++cell )
700  {
701  int i = cell->second;
702  int n = sumPartSolu0[ i ];
703 
704  if (n < nOfSubsamples ) nWrong++;
705  }
706 
707  return nWrong;
708 }
709 
710 //=============================================================================
711 
712 template<class IDdet>
713 float
714 MinL3AlgoUnivErr<IDdet>::
715 getMeanPartialSolution( IDdet id ) const
716 
717 {
718  float meanX;
719  citer_IDmapI cell = idToIndex.find( id );
720  if ( cell == idToIndex.end() ) meanX = 999.; // no info for the cell
721  else
722  {
723  int i = cell->second;
724  int n = sumPartSolu0[ i ];
725  float meanX = sumPartSolu1[ i ] / n;
726  }
727  return meanX;
728 
729 }
730 
731 //=============================================================================
732 
733 template<class IDdet>
734 typename MinL3AlgoUnivErr<IDdet>::IDmapF
735 MinL3AlgoUnivErr<IDdet>::
736 getMeanPartialSolution () const
737 
738 {
739  IDmapF solution;
740 
741  for (citer_IDmapI cell = idToIndex.begin();
742  cell != idToIndex. end(); ++cell )
743  {
744  int i = cell->second;
745  int n = sumPartSolu0[ i ];
746  float meanX = sumPartSolu1[ i ] / n;
747  solution.insert( IDmapFvalue( cell->first , meanX ) );
748  }
749  return solution;
750 }
751 
752 //=============================================================================
753 
754 #endif // MinL3AlgoUnivErr_H
int i
Definition: DBlmapReader.cc:9
U second(std::pair< T, U > const &p)
T sqrt(T t)
Definition: SSEVec.h:46
int j
Definition: DBlmapReader.cc:9
#define end
Definition: vmac.h:38
Container::value_type value_type
JetCorrectorParameters corr
Definition: classes.h:9
void resetSolution()
reset for new iteration
tuple cout
Definition: gather_cfg.py:121
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
T w() const