1 #ifndef MinL3AlgoUnivErr_H
2 #define MinL3AlgoUnivErr_H
75 template <
class IDdet>
76 class MinL3AlgoUnivErr
79 typedef std::map<IDdet,float> IDmapF;
81 typedef typename IDmapF::iterator iter_IDmapF;
82 typedef typename IDmapF::const_iterator citer_IDmapF;
83 typedef std::map<IDdet,int> IDmapI;
85 typedef typename IDmapI::iterator iter_IDmapI;
86 typedef typename IDmapI::const_iterator citer_IDmapI;
92 MinL3AlgoUnivErr(
float kweight_ = 0.);
115 IDmapF iterate(
const std::vector<std::vector<float> > & eventMatrix,
116 const std::vector<std::vector<IDdet> > & idMatrix,
117 const std::vector<float> & energyVector,
119 const bool & normalizeFlag
121 const int & nSubsamples = 10
132 float getError( IDdet
id )
const;
144 IDmapF getError()
const;
155 int numberOfWrongErrors()
const;
169 float getErrorQuality( IDdet
id )
const;
171 IDmapF getErrorQuality()
const;
180 float getMeanPartialSolution( IDdet
id )
const;
188 IDmapF getMeanPartialSolution()
const;
193 void addEvent(
const std::vector<float> & myCluster,
194 const std::vector<IDdet> & idCluster,
203 recalibrateEvent(
const std::vector<float> & myCluster,
204 const std::vector<IDdet> & idCluster,
205 const IDmapF & newCalibration,
206 const int & isol = -1,
207 const int & iter = -1
214 IDmapF getSolution(
const bool resetsolution=
true );
219 void resetSolution();
231 std::vector<int> sumPartSolu0;
232 std::vector<float> sumPartSolu1;
233 std::vector<float> sumPartSolu2;
239 void registerPartialSolution(
const IDmapF & partialSolution );
249 template<
class IDdet>
250 MinL3AlgoUnivErr<IDdet>::MinL3AlgoUnivErr(
float kweight_)
251 :kweight(kweight_), countEvents(0)
253 std::cout <<
"MinL3AlgoUnivErr : L3 algo with a calculation"
254 <<
" of stat.errors working..." << std::endl;
260 template<
class IDdet>
261 MinL3AlgoUnivErr<IDdet>::~MinL3AlgoUnivErr()
267 template<
class IDdet>
268 typename MinL3AlgoUnivErr<IDdet>::IDmapF
269 MinL3AlgoUnivErr<IDdet>::
270 iterate(
const std::vector < std::vector<float> > & eventMatrix,
271 const std::vector < std::vector<IDdet> > & idMatrix,
272 const std::vector <float> & energyVector,
274 const bool & normalizeFlag,
275 const int & nSubsamples
281 nOfSubsamples = nSubsamples;
285 sumPartSolu0 .clear();
286 sumPartSolu1 .clear();
287 sumPartSolu2 .clear();
289 IDmapF totalSolution;
301 for (
int isol = 0 ; isol <= nSubsamples; isol++) {
303 IDmapF sampleSolution ;
304 IDmapF iterSolution ;
305 std::vector < std::vector<float> > myEventMatrix ;
306 std::vector < std::vector<IDdet> > myIdMatrix ;
307 std::vector <float> myEnergyVector ;
315 myEventMatrix = eventMatrix ;
316 myIdMatrix = idMatrix ;
317 myEnergyVector = energyVector ;
322 sampleSolution .clear() ;
323 myEventMatrix .clear() ;
324 myIdMatrix .clear() ;
325 myEnergyVector .clear() ;
327 for (
int i = 0; i < static_cast<int>( eventMatrix.size() );
i++)
330 if (
i % nSubsamples + 1 == isol )
332 myEventMatrix .push_back (eventMatrix [
i]) ;
333 myIdMatrix .push_back (idMatrix [i]) ;
334 myEnergyVector .push_back (energyVector [i]) ;
339 int Nevents = myEventMatrix.size();
345 for (
int iter=1 ; iter <= nIter ; iter++)
354 for (i=0; i<Nevents; i++)
357 for (
unsigned j=0 ;
j < myEventMatrix[
i].size() ;
j++)
358 {sumOverEnergy += myEventMatrix[
i][
j];}
359 sumOverEnergy /= myEnergyVector[
i];
360 scale += sumOverEnergy;
364 for (i=0; i<Nevents; i++) {myEnergyVector[
i] *=
scale;}
368 for (
int iEvt=0; iEvt < Nevents; iEvt++)
370 addEvent( myEventMatrix[iEvt], myIdMatrix[iEvt]
371 , myEnergyVector[iEvt] );
373 iterSolution = getSolution();
374 if (iterSolution.empty())
375 { sampleSolution.clear();
380 for (
int ievent = 0; ievent<Nevents; ievent++)
382 myEventMatrix[ievent] = recalibrateEvent (myEventMatrix[ievent],
389 for (iter_IDmapF i = iterSolution.begin(); i != iterSolution.end(); i++)
391 iter_IDmapF itotal = sampleSolution.find(i->first);
392 if (itotal == sampleSolution.end())
394 sampleSolution.insert(IDmapFvalue(i->first,i->second));
398 itotal->second *= i->second;
408 totalSolution = sampleSolution;
412 registerPartialSolution( sampleSolution );
417 return totalSolution;
423 template<
class IDdet>
424 void MinL3AlgoUnivErr<IDdet>::
425 addEvent(
const std::vector<float> & myCluster,
426 const std::vector<IDdet> & idCluster,
432 float w, invsumXmatrix;
437 for (
unsigned i=0; i<myCluster.size(); i++) { sumXmatrix += myCluster[
i]; }
440 eventw = 1 - fabs(1 - sumXmatrix/energy);
441 eventw =
pow(eventw,kweight);
443 if (sumXmatrix != 0.)
445 invsumXmatrix = 1/sumXmatrix;
448 for (
unsigned i=0; i<myCluster.size(); i++)
450 w = myCluster[
i] * invsumXmatrix;
453 iter_IDmapF iwsum = wsum.find(idCluster[i]);
454 if (iwsum == wsum.end())
455 wsum.insert(IDmapFvalue(idCluster[i],w*eventw));
456 else iwsum->second += w*eventw;
458 iter_IDmapF iEwsum = Ewsum.find(idCluster[i]);
459 if (iEwsum == Ewsum.end())
460 Ewsum.insert(IDmapFvalue (idCluster[i],
461 (w*eventw * energy * invsumXmatrix)
463 else iEwsum->second += (w*eventw * energy * invsumXmatrix);
471 template<
class IDdet>
472 typename MinL3AlgoUnivErr<IDdet>::IDmapF
473 MinL3AlgoUnivErr<IDdet>::
474 getSolution(
const bool resetsolution )
478 for (iter_IDmapF i = wsum.begin(); i != wsum.end(); i++)
480 iter_IDmapF iEwsum = Ewsum.find(i->first);
482 if (i->second != 0) myValue = iEwsum->second / i->second;
484 solution.insert( IDmapFvalue(i->first,myValue) );
487 if (resetsolution) resetSolution();
494 template<
class IDdet>
495 void MinL3AlgoUnivErr<IDdet>::resetSolution()
503 template<
class IDdet>
505 MinL3AlgoUnivErr<IDdet>::
506 recalibrateEvent(
const std::vector <float> & myCluster,
507 const std::vector <IDdet> & idCluster,
508 const IDmapF & newCalibration,
513 std::vector<float> newCluster(myCluster);
515 for (
unsigned i=0; i<myCluster.size(); i++)
517 citer_IDmapF icalib = newCalibration.find(idCluster[i]);
518 if (icalib != newCalibration.end())
520 newCluster[
i] *= icalib->second;
524 std::cout <<
"No calibration available for this element."
527 <<
" iter = " << iter
528 <<
" idCluster[i] = " << idCluster[
i] <<
"\n";
538 template<
class IDdet>
540 MinL3AlgoUnivErr<IDdet>::
541 registerPartialSolution(
const IDmapF & partialSolution )
547 for (citer_IDmapF cell = partialSolution.begin() ;
548 cell != partialSolution.
end() ; ++cell)
550 IDdet
id = cell->first ;
551 float corr = cell->second;
554 iter_IDmapI cell2 = idToIndex.find(
id );
556 if ( cell2 == idToIndex.end() )
561 sumPartSolu0.push_back( 1 );
562 sumPartSolu1.push_back( corr );
563 sumPartSolu2.push_back( corr * corr );
564 idToIndex.insert( IDmapIvalue(
id, ++lastIndex ) );
571 sumPartSolu0[
index ] += 1;
582 template<
class IDdet>
584 MinL3AlgoUnivErr<IDdet>::
585 getError( IDdet
id )
const
589 citer_IDmapI cell = idToIndex.find(
id );
590 if ( cell == idToIndex.end() ) error = -2.;
593 int i = cell->second;
594 int n = sumPartSolu0[
i ];
595 if (n <= 1 ) error = -1.;
598 float meanX = sumPartSolu1[
i ] /
n;
599 float meanX2 = sumPartSolu2[
i ] /
n;
601 error =
sqrt( fabs(meanX2 - meanX * meanX) / (n - 1.) ) ;
610 template<
class IDdet>
611 typename MinL3AlgoUnivErr<IDdet>::IDmapF
612 MinL3AlgoUnivErr<IDdet>::
619 for (citer_IDmapI cell = idToIndex.begin();
620 cell != idToIndex.
end(); ++cell )
622 int i = cell->second;
623 int n = sumPartSolu0[
i ];
624 if (n <= 1 ) error = -1.;
627 float meanX = sumPartSolu1[
i ] /
n;
628 float meanX2 = sumPartSolu2[
i ] /
n;
630 error =
sqrt( fabs(meanX2 - meanX * meanX) / (n - 1) ) ;
633 errors.insert( IDmapFvalue( cell->first , error ) );
639 template<
class IDdet>
641 MinL3AlgoUnivErr<IDdet>::
642 getErrorQuality( IDdet
id )
const
646 citer_IDmapI cell = idToIndex.find(
id );
647 if ( cell == idToIndex.end() ) fraction = -1.;
651 int i = cell->second;
652 int n = sumPartSolu0[
i ];
653 if (n < nOfSubsamples ) fraction = float( n ) / nOfSubsamples;
661 template<
class IDdet>
662 typename MinL3AlgoUnivErr<IDdet>::IDmapF
663 MinL3AlgoUnivErr<IDdet>::
664 getErrorQuality()
const
670 for (citer_IDmapI cell = idToIndex.begin();
671 cell != idToIndex.
end(); ++cell )
673 int i = cell->second;
674 int n = sumPartSolu0[
i ];
676 if (n < nOfSubsamples ) fraction = float( n ) / nOfSubsamples;
679 fractions.insert( IDmapFvalue( cell->first , fraction ) );
688 template<
class IDdet>
690 MinL3AlgoUnivErr<IDdet>::
691 numberOfWrongErrors()
const
696 for (citer_IDmapI cell = idToIndex.begin();
697 cell != idToIndex.
end(); ++cell )
699 int i = cell->second;
700 int n = sumPartSolu0[
i ];
702 if (n < nOfSubsamples ) nWrong++;
710 template<
class IDdet>
712 MinL3AlgoUnivErr<IDdet>::
713 getMeanPartialSolution( IDdet
id )
const
717 citer_IDmapI cell = idToIndex.find(
id );
718 if ( cell == idToIndex.end() ) meanX = 999.;
721 int i = cell->second;
722 int n = sumPartSolu0[
i ];
723 meanX = sumPartSolu1[
i ] /
n;
731 template<
class IDdet>
732 typename MinL3AlgoUnivErr<IDdet>::IDmapF
733 MinL3AlgoUnivErr<IDdet>::
734 getMeanPartialSolution ()
const
739 for (citer_IDmapI cell = idToIndex.begin();
740 cell != idToIndex.
end(); ++cell )
742 int i = cell->second;
743 int n = sumPartSolu0[
i ];
744 float meanX = sumPartSolu1[
i ] /
n;
745 solution.insert( IDmapFvalue( cell->first , meanX ) );
752 #endif // MinL3AlgoUnivErr_H
U second(std::pair< T, U > const &p)
Container::value_type value_type
static std::atomic< unsigned int > lastIndex
void resetSolution()
reset for new iteration
Power< A, B >::type pow(const A &a, const B &b)