1 #ifndef MinL3AlgoUnivErr_H
2 #define MinL3AlgoUnivErr_H
77 template <
class IDdet>
78 class MinL3AlgoUnivErr
81 typedef std::map<IDdet,float> IDmapF;
83 typedef typename IDmapF::iterator iter_IDmapF;
84 typedef typename IDmapF::const_iterator citer_IDmapF;
85 typedef std::map<IDdet,int> IDmapI;
87 typedef typename IDmapI::iterator iter_IDmapI;
88 typedef typename IDmapI::const_iterator citer_IDmapI;
94 MinL3AlgoUnivErr(
float kweight_ = 0.);
117 IDmapF iterate(
const std::vector<std::vector<float> > & eventMatrix,
118 const std::vector<std::vector<IDdet> > & idMatrix,
119 const std::vector<float> & energyVector,
121 const bool & normalizeFlag
123 const int & nSubsamples = 10
134 float getError( IDdet
id )
const;
146 IDmapF getError()
const;
157 int numberOfWrongErrors()
const;
171 float getErrorQuality( IDdet
id )
const;
173 IDmapF getErrorQuality()
const;
182 float getMeanPartialSolution( IDdet
id )
const;
190 IDmapF getMeanPartialSolution()
const;
195 void addEvent(
const std::vector<float> & myCluster,
196 const std::vector<IDdet> & idCluster,
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
216 IDmapF getSolution(
const bool resetsolution=
true );
221 void resetSolution();
233 std::vector<int> sumPartSolu0;
234 std::vector<float> sumPartSolu1;
235 std::vector<float> sumPartSolu2;
241 void registerPartialSolution(
const IDmapF & partialSolution );
251 template<
class IDdet>
252 MinL3AlgoUnivErr<IDdet>::MinL3AlgoUnivErr(
float kweight_)
253 :kweight(kweight_), countEvents(0)
255 std::cout <<
"MinL3AlgoUnivErr : L3 algo with a calculation"
256 <<
" of stat.errors working..." << std::endl;
262 template<
class IDdet>
263 MinL3AlgoUnivErr<IDdet>::~MinL3AlgoUnivErr()
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,
276 const bool & normalizeFlag,
277 const int & nSubsamples
283 nOfSubsamples = nSubsamples;
287 sumPartSolu0 .clear();
288 sumPartSolu1 .clear();
289 sumPartSolu2 .clear();
291 IDmapF totalSolution;
303 for (
int isol = 0 ; isol <= nSubsamples; isol++) {
305 IDmapF sampleSolution ;
306 IDmapF iterSolution ;
307 std::vector < std::vector<float> > myEventMatrix ;
308 std::vector < std::vector<IDdet> > myIdMatrix ;
309 std::vector <float> myEnergyVector ;
317 myEventMatrix = eventMatrix ;
318 myIdMatrix = idMatrix ;
319 myEnergyVector = energyVector ;
324 sampleSolution .clear() ;
325 myEventMatrix .clear() ;
326 myIdMatrix .clear() ;
327 myEnergyVector .clear() ;
329 for (
int i = 0; i < static_cast<int>( eventMatrix.size() );
i++)
332 if (
i % nSubsamples + 1 == isol )
334 myEventMatrix .push_back (eventMatrix [
i]) ;
335 myIdMatrix .push_back (idMatrix [i]) ;
336 myEnergyVector .push_back (energyVector [i]) ;
341 int Nevents = myEventMatrix.size();
347 for (
int iter=1 ; iter <= nIter ; iter++)
356 for (i=0; i<Nevents; i++)
359 for (
unsigned j=0 ;
j < myEventMatrix[
i].size() ;
j++)
360 {sumOverEnergy += myEventMatrix[
i][
j];}
361 sumOverEnergy /= myEnergyVector[
i];
362 scale += sumOverEnergy;
366 for (i=0; i<Nevents; i++) {myEnergyVector[
i] *=
scale;}
370 for (
int iEvt=0; iEvt < Nevents; iEvt++)
372 addEvent( myEventMatrix[iEvt], myIdMatrix[iEvt]
373 , myEnergyVector[iEvt] );
375 iterSolution = getSolution();
376 if (iterSolution.empty())
377 { sampleSolution.clear();
382 for (
int ievent = 0; ievent<Nevents; ievent++)
384 myEventMatrix[ievent] = recalibrateEvent (myEventMatrix[ievent],
391 for (iter_IDmapF i = iterSolution.begin(); i != iterSolution.end(); i++)
393 iter_IDmapF itotal = sampleSolution.find(i->first);
394 if (itotal == sampleSolution.end())
396 sampleSolution.insert(IDmapFvalue(i->first,i->second));
400 itotal->second *= i->second;
410 totalSolution = sampleSolution;
414 registerPartialSolution( sampleSolution );
419 return totalSolution;
425 template<
class IDdet>
426 void MinL3AlgoUnivErr<IDdet>::
427 addEvent(
const std::vector<float> & myCluster,
428 const std::vector<IDdet> & idCluster,
434 float w, invsumXmatrix;
439 for (
unsigned i=0; i<myCluster.size(); i++) { sumXmatrix += myCluster[
i]; }
442 eventw = 1 - fabs(1 - sumXmatrix/energy);
443 eventw =
pow(eventw,kweight);
445 if (sumXmatrix != 0.)
447 invsumXmatrix = 1/sumXmatrix;
450 for (
unsigned i=0; i<myCluster.size(); i++)
452 w = myCluster[
i] * invsumXmatrix;
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;
460 iter_IDmapF iEwsum = Ewsum.find(idCluster[i]);
461 if (iEwsum == Ewsum.end())
462 Ewsum.insert(IDmapFvalue (idCluster[i],
463 (w*eventw * energy * invsumXmatrix)
465 else iEwsum->second += (w*eventw * energy * invsumXmatrix);
473 template<
class IDdet>
474 typename MinL3AlgoUnivErr<IDdet>::IDmapF
475 MinL3AlgoUnivErr<IDdet>::
476 getSolution(
const bool resetsolution )
480 for (iter_IDmapF i = wsum.begin(); i != wsum.end(); i++)
482 iter_IDmapF iEwsum = Ewsum.find(i->first);
484 if (i->second != 0) myValue = iEwsum->second / i->second;
486 solution.insert( IDmapFvalue(i->first,myValue) );
489 if (resetsolution) resetSolution();
496 template<
class IDdet>
497 void MinL3AlgoUnivErr<IDdet>::resetSolution()
505 template<
class IDdet>
507 MinL3AlgoUnivErr<IDdet>::
508 recalibrateEvent(
const std::vector <float> & myCluster,
509 const std::vector <IDdet> & idCluster,
510 const IDmapF & newCalibration,
515 std::vector<float> newCluster(myCluster);
517 for (
unsigned i=0; i<myCluster.size(); i++)
519 citer_IDmapF icalib = newCalibration.find(idCluster[i]);
520 if (icalib != newCalibration.end())
522 newCluster[
i] *= icalib->second;
526 std::cout <<
"No calibration available for this element."
529 <<
" iter = " << iter
530 <<
" idCluster[i] = " << idCluster[
i] <<
"\n";
540 template<
class IDdet>
542 MinL3AlgoUnivErr<IDdet>::
543 registerPartialSolution(
const IDmapF & partialSolution )
546 int lastIndex = sumPartSolu0.size() - 1;
549 for (citer_IDmapF cell = partialSolution.begin() ;
550 cell != partialSolution.
end() ; ++cell)
552 IDdet
id = cell->first ;
553 float corr = cell->second;
556 iter_IDmapI cell2 = idToIndex.find(
id );
558 if ( cell2 == idToIndex.end() )
563 sumPartSolu0.push_back( 1 );
564 sumPartSolu1.push_back( corr );
565 sumPartSolu2.push_back( corr * corr );
566 idToIndex.insert( IDmapIvalue(
id, ++lastIndex ) );
573 sumPartSolu0[
index ] += 1;
584 template<
class IDdet>
586 MinL3AlgoUnivErr<IDdet>::
587 getError( IDdet
id )
const
591 citer_IDmapI cell = idToIndex.find(
id );
592 if ( cell == idToIndex.end() ) error = -2.;
595 int i = cell->second;
596 int n = sumPartSolu0[
i ];
597 if (n <= 1 ) error = -1.;
600 float meanX = sumPartSolu1[
i ] /
n;
601 float meanX2 = sumPartSolu2[
i ] /
n;
603 error =
sqrt( fabs(meanX2 - meanX * meanX) / (n - 1.) ) ;
612 template<
class IDdet>
613 typename MinL3AlgoUnivErr<IDdet>::IDmapF
614 MinL3AlgoUnivErr<IDdet>::
621 for (citer_IDmapI cell = idToIndex.begin();
622 cell != idToIndex.
end(); ++cell )
624 int i = cell->second;
625 int n = sumPartSolu0[
i ];
626 if (n <= 1 ) error = -1.;
629 float meanX = sumPartSolu1[
i ] /
n;
630 float meanX2 = sumPartSolu2[
i ] /
n;
632 error =
sqrt( fabs(meanX2 - meanX * meanX) / (n - 1) ) ;
635 errors.insert( IDmapFvalue( cell->first , error ) );
641 template<
class IDdet>
643 MinL3AlgoUnivErr<IDdet>::
644 getErrorQuality( IDdet
id )
const
648 citer_IDmapI cell = idToIndex.find(
id );
649 if ( cell == idToIndex.end() ) fraction = -1.;
653 int i = cell->second;
654 int n = sumPartSolu0[
i ];
655 if (n < nOfSubsamples ) fraction = float( n ) / nOfSubsamples;
663 template<
class IDdet>
664 typename MinL3AlgoUnivErr<IDdet>::IDmapF
665 MinL3AlgoUnivErr<IDdet>::
666 getErrorQuality()
const
672 for (citer_IDmapI cell = idToIndex.begin();
673 cell != idToIndex.
end(); ++cell )
675 int i = cell->second;
676 int n = sumPartSolu0[
i ];
678 if (n < nOfSubsamples ) fraction = float( n ) / nOfSubsamples;
681 fractions.insert( IDmapFvalue( cell->first , fraction ) );
690 template<
class IDdet>
692 MinL3AlgoUnivErr<IDdet>::
693 numberOfWrongErrors()
const
698 for (citer_IDmapI cell = idToIndex.begin();
699 cell != idToIndex.
end(); ++cell )
701 int i = cell->second;
702 int n = sumPartSolu0[
i ];
704 if (n < nOfSubsamples ) nWrong++;
712 template<
class IDdet>
714 MinL3AlgoUnivErr<IDdet>::
715 getMeanPartialSolution( IDdet
id )
const
719 citer_IDmapI cell = idToIndex.find(
id );
720 if ( cell == idToIndex.end() ) meanX = 999.;
723 int i = cell->second;
724 int n = sumPartSolu0[
i ];
725 float meanX = sumPartSolu1[
i ] /
n;
733 template<
class IDdet>
734 typename MinL3AlgoUnivErr<IDdet>::IDmapF
735 MinL3AlgoUnivErr<IDdet>::
736 getMeanPartialSolution ()
const
741 for (citer_IDmapI cell = idToIndex.begin();
742 cell != idToIndex.
end(); ++cell )
744 int i = cell->second;
745 int n = sumPartSolu0[
i ];
746 float meanX = sumPartSolu1[
i ] /
n;
747 solution.insert( IDmapFvalue( cell->first , meanX ) );
754 #endif // MinL3AlgoUnivErr_H
U second(std::pair< T, U > const &p)
Container::value_type value_type
void resetSolution()
reset for new iteration
Power< A, B >::type pow(const A &a, const B &b)